Update contrib.
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 <integratorap.h>
21 // We use 2 different timers on this test:
22 // 1) the Core module FRC - 32-bit clocked at 24MHz, non-interruptible, for the time stamps
23 // 2) IntegratorAP Timer 1 - 16-bit, clocked at 1.5MHz , interruptible, for the Interrupts latency measurements
26 #define KHwFrcFreqHz 24000000 // uses the core module FRC clocked at 24MHz
27 #define KHwApFreqHz 24000000 // uses the IntegratorAP Timer 1 clocked at 24MHz
31 class DBMMi920Device : public DPhysicalDevice
35 virtual TInt Install();
36 virtual void GetCaps(TDes8& aDes) const;
37 virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
38 virtual TInt Validate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
41 class DBMMi920Channel : public DBMPChannel
46 TInt InitialiseTimer();
47 virtual TBMTicks TimerPeriod();
48 virtual TBMTicks TimerStamp();
49 virtual TBMNs TimerTicksToNs(TBMTicks);
50 virtual TBMTicks TimerNsToTicks(TBMNs);
51 virtual TInt BindInterrupt(MBMIsr*);
52 virtual TInt BindInterrupt(MBMInterruptLatencyIsr*);
53 virtual void RequestInterrupt();
54 virtual void CancelInterrupt();
58 static void ClearAp1TimerInt();
61 static const TBMTicks KBMMi920FrcPeriod = (((TBMTicks) 1) << 32); // FRC
62 static const TUint KBMMi920ApPeriod = (((TBMTicks) 1) << 16); // FRC
63 static const TBMNs KBMMi920NsPerTick = (1000*1000*1000) / KHwFrcFreqHz; // FRC
64 static const TUint KBMMi920InterruptDelayTicks = KHwApFreqHz / 1000; // 1ms in AP Timer 1 ticks
66 static void Isr(TAny*);
69 MBMInterruptLatencyIsr* iInterruptLatencyIsr;
73 DECLARE_STANDARD_PDD()
75 // Create a new device
79 return new DBMMi920Device;
82 DBMMi920Device::DBMMi920Device()
88 iVersion = TVersion(1,0,1);
91 TInt DBMMi920Device::Install()
93 // Install the device driver.
96 TInt r = SetName(&KBMPdName);
100 void DBMMi920Device::GetCaps(TDes8& aDes) const
102 // Return the Comm capabilities.
107 TInt DBMMi920Device::Create(DBase*& aChannel, TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
109 // Create a channel on the device.
113 DBMMi920Channel* pD= new DBMMi920Channel;
117 r=pD->InitialiseTimer();
121 TInt DBMMi920Device::Validate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
123 if (!Kern::QueryVersionSupported(iVersion,aVer))
125 return KErrNotSupported;
130 void DBMMi920Channel::ClearAp1TimerInt()
133 TIntegratorAP::ClearTimerInt(TIntegratorAP::ECounterTimer1);
134 // WARNING: Integrator bug! The timer interrupts are not always cleared the first time round
135 // so keep on clearing them until they are no longer active.
136 while (*(volatile TUint*)(KHwIrqBaseSet0) & KHtIntTimer1);
139 DBMMi920Channel::DBMMi920Channel()
142 // iInterruptLatencyIsr = NULL;
143 iInterruptId = KIntIdMatchGeneral2;
146 DBMMi920Channel::~DBMMi920Channel()
148 // Disable the IntegratorAP Timer 1 and associated interrupt
149 if (iIsr || iInterruptLatencyIsr)
152 Interrupt::Disable(iInterruptId);
153 Interrupt::Unbind(iInterruptId);
155 TIntegratorAP::EnableTimer(TIntegratorAP::ECounterTimer1, TIntegratorAP::EDisable);
158 TInt DBMMi920Channel::InitialiseTimer()
160 // Set up the IntegratorAP Timer 1 to run at 1.5MHz, free-running mode, and enable it
161 TIntegratorAP::SetTimerMode(TIntegratorAP::ECounterTimer1, TIntegratorAP::ETimerModeFreeRunning);
162 TIntegratorAP::SetTimerPreScale(TIntegratorAP::ECounterTimer1, TIntegratorAP::ETimerPreScaleNone); // 24MHz
163 TIntegratorAP::EnableTimer(TIntegratorAP::ECounterTimer1, TIntegratorAP::EEnable);
167 TBMTicks DBMMi920Channel::TimerPeriod()
169 return KBMMi920FrcPeriod;
172 TBMTicks DBMMi920Channel::TimerStamp()
174 return (*(volatile TUint*)(KHwRwCoreClkCounter)); // FRC
177 TBMNs DBMMi920Channel::TimerTicksToNs(TBMTicks ticks)
179 return ticks * KBMMi920NsPerTick; // FRC
182 TBMTicks DBMMi920Channel::TimerNsToTicks(TBMNs ns)
184 return ns / KBMMi920NsPerTick; // FRC
188 void DBMMi920Channel::Isr(TAny* ptr)
190 DBMMi920Channel* mCh = (DBMMi920Channel*) ptr;
191 BM_ASSERT(mCh->iIsr || mCh->iInterruptLatencyIsr);
194 mCh->iIsr->Isr(*(volatile TUint*)(KHwRwCoreClkCounter)); // read timestamp off FRC
197 { // calculate time elapsed between the interrupt going off and now in FRC ticks
198 TUint value = TIntegratorAP::TimerValue(TIntegratorAP::ECounterTimer1);
199 BM_ASSERT(((KHwFrcFreqHz/KHwApFreqHz)*KHwApFreqHz) == KHwFrcFreqHz);
200 mCh->iInterruptLatencyIsr->InterruptLatencyIsr((KBMMi920ApPeriod - value) * (KHwFrcFreqHz/KHwApFreqHz));
203 Interrupt::Disable(mCh->iInterruptId);
206 TInt DBMMi920Channel::BindInterrupt()
208 BM_ASSERT(iInterruptId==KIntIdMatchGeneral2);
209 TInt r=Interrupt::Bind(iInterruptId, Isr, this);
218 TInt DBMMi920Channel::BindInterrupt(MBMIsr* aIsr)
221 BM_ASSERT(!iInterruptLatencyIsr);
223 return BindInterrupt();
226 TInt DBMMi920Channel::BindInterrupt(MBMInterruptLatencyIsr* aIsr)
229 BM_ASSERT(!iInterruptLatencyIsr);
230 iInterruptLatencyIsr = aIsr;
231 return BindInterrupt();
235 void DBMMi920Channel::RequestInterrupt()
237 BM_ASSERT(iIsr || iInterruptLatencyIsr);
238 TIntegratorAP::SetTimerLoad(TIntegratorAP::ECounterTimer1, KBMMi920InterruptDelayTicks);
240 Interrupt::Enable(iInterruptId); // Timer interrupts on Integrator are always enabled!
243 void DBMMi920Channel::CancelInterrupt()
245 if (iIsr || iInterruptLatencyIsr)
247 TIntegratorAP::EnableTimer(TIntegratorAP::ECounterTimer1, TIntegratorAP::EDisable);