First public contribution.
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include <kernel/kernel.h>
17 #include <upd35001_timer.h>
21 class DBMNE1Device : public DPhysicalDevice
25 virtual TInt Install();
26 virtual void GetCaps(TDes8& aDes) const;
27 virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
28 virtual TInt Validate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
31 class DBMNE1Channel : public DBMPChannel
36 virtual TBMTicks TimerPeriod();
37 virtual TBMTicks TimerStamp();
38 virtual TBMNs TimerTicksToNs(TBMTicks);
39 virtual TBMTicks TimerNsToTicks(TBMNs);
40 virtual TInt BindInterrupt(MBMIsr*);
41 virtual TInt BindInterrupt(MBMInterruptLatencyIsr*);
42 virtual void RequestInterrupt();
43 virtual void CancelInterrupt();
46 static const TBMTicks KBMNE1Period = (((TBMTicks) 1) << 32);
48 static void Isr(TAny*);
51 MBMInterruptLatencyIsr* iInterruptLatencyIsr;
55 volatile TUint iStartCount;
56 volatile TUint iRunCount;
57 volatile TUint iCancelCount;
61 DECLARE_STANDARD_PDD()
63 // Create a new device
67 return new DBMNE1Device;
70 DBMNE1Device::DBMNE1Device()
76 iVersion = TVersion(1,0,1);
79 TInt DBMNE1Device::Install()
81 // Install the device driver.
84 TInt r = SetName(&KBMPdName);
88 void DBMNE1Device::GetCaps(TDes8& aDes) const
90 // Return the Comm capabilities.
95 TInt DBMNE1Device::Create(DBase*& aChannel, TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
97 // Create a channel on the device.
101 aChannel = new DBMNE1Channel;
102 return aChannel?KErrNone:KErrNoMemory;
105 TInt DBMNE1Device::Validate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
107 if (!Kern::QueryVersionSupported(iVersion,aVer))
109 return KErrNotSupported;
114 DBMNE1Channel::DBMNE1Channel()
118 // iInterruptLatencyIsr = NULL;
119 NETimer& T2 = NETimer::Timer(2);
120 iPrescale = __e32_find_ms1_32(T2.iPrescaler & 0x3f);
121 iNsPerTick = 15u << iPrescale;
124 DBMNE1Channel::~DBMNE1Channel()
129 TBMTicks DBMNE1Channel::TimerPeriod()
134 TBMTicks DBMNE1Channel::TimerStamp()
136 return NETimer::Timer(2).iTimerCount;
139 TBMNs DBMNE1Channel::TimerTicksToNs(TBMTicks ticks)
141 return ticks * (TBMTicks)iNsPerTick;
144 TBMTicks DBMNE1Channel::TimerNsToTicks(TBMNs ns)
146 return ns / (TBMTicks)iNsPerTick;
149 void DBMNE1Channel::Isr(TAny* ptr)
151 DBMNE1Channel* mCh = (DBMNE1Channel*) ptr;
152 BM_ASSERT(mCh->iIsr || mCh->iInterruptLatencyIsr);
155 mCh->iIsr->Isr(NETimer::Timer(2).iTimerCount);
159 TUint x = NETimer::Timer(0).iTimerCount;
160 x = (x + (1u<<mCh->iPrescale) - 1) >> mCh->iPrescale;
161 mCh->iInterruptLatencyIsr->InterruptLatencyIsr(x);
163 __e32_atomic_add_ord32(&mCh->iRunCount, 1);
166 TInt DBMNE1Channel::BindInterrupt(MBMIsr* aIsr)
169 BM_ASSERT(!iInterruptLatencyIsr);
174 TInt DBMNE1Channel::BindInterrupt(MBMInterruptLatencyIsr* aIsr)
177 BM_ASSERT(!iInterruptLatencyIsr);
178 iInterruptLatencyIsr = aIsr;
183 void DBMNE1Channel::RequestInterrupt()
185 BM_ASSERT(iIsr || iInterruptLatencyIsr);
186 if (iTimer.OneShot(1)==KErrNone)
187 __e32_atomic_add_ord32(&iStartCount, 1);
190 void DBMNE1Channel::CancelInterrupt()
193 __e32_atomic_add_ord32(&iCancelCount, 1);
194 while (iStartCount != iCancelCount + iRunCount)