Update contrib.
1 // Copyright (c) 1997-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.
14 // e32test\misc\d_rndtim.cpp
15 // LDD for generating random interrupts
20 #include <kernel/kern_priv.h>
22 #include "../misc/prbs.h"
25 #include <windermere.h>
26 #elif defined(__MISA__)
27 #define INT_ID KIntIdOstMatchGeneral
29 #elif defined(__MCOT__)
30 #define INT_ID KIntIdOstMatchGeneral
32 #elif defined(__MI920__) || defined(__NI1136__)
33 #include <integratorap.h>
34 #elif defined(__EPOC32__) && defined(__CPU_X86)
39 #error Random timer ISR not supported on this platform
43 const TInt KMajorVersionNumber=0;
44 const TInt KMinorVersionNumber=1;
45 const TInt KBuildVersionNumber=1;
47 class DRndTimFactory : public DLogicalDevice
49 // IPC copy LDD factory
54 virtual TInt Install(); //overriding pure virtual
55 virtual void GetCaps(TDes8& aDes) const; //overriding pure virtual
56 virtual TInt Create(DLogicalChannelBase*& aChannel); //overriding pure virtual
59 class DRndTim : public DLogicalChannelBase
61 // Millisecond timer LDD channel
68 virtual TInt DoCreate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
69 virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
71 static void TimerIsr(TAny* aPtr);
72 static void IDfcFn(TAny* aPtr);
75 TInt SetPriority(TInt aHandle, TInt aPriority);
76 TInt Calibrate(TInt aMilliseconds);
80 volatile TUint32 iSeed[2];
81 volatile TUint32 iIsrCount;
86 DECLARE_STANDARD_LDD()
88 return new DRndTimFactory;
91 DRndTimFactory::DRndTimFactory()
96 iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
97 //iParseMask=0;//No units, no info, no PDD
98 //iUnitsMask=0;//Only one thing
101 TInt DRndTimFactory::Create(DLogicalChannelBase*& aChannel)
103 // Create a new DRndTim on this logical device
106 aChannel=new DRndTim;
107 return aChannel?KErrNone:KErrNoMemory;
110 TInt DRndTimFactory::Install()
112 // Install the LDD - overriding pure virtual
115 return SetName(&KRndTimLddName);
118 void DRndTimFactory::GetCaps(TDes8& aDes) const
120 // Get capabilities - overriding pure virtual
124 b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
125 Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
135 iThread=&Kern::CurrentThread();
137 iSem.iOwningThread = &iThread->iNThread;
138 iSeed[0] = 0xb504f333u;
139 iSeed[1] = 0xf9de6484u;
146 Interrupt::Unbind(iIntId);
147 Kern::SafeClose((DObject*&)iThread, NULL);
150 TInt DRndTim::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
156 if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
157 return KErrNotSupported;
159 TInt r = Interrupt::Bind(INT_ID, &TimerIsr, this);
165 TInt DRndTim::Request(TInt aFunction, TAny* a1, TAny* a2)
167 TInt r = KErrNotSupported;
170 case RRndTim::EControlWait:
171 NKern::FSWait(&iSem);
174 case RRndTim::EControlSetPriority:
175 r = SetPriority(TInt(a1), TInt(a2));
177 case RRndTim::EControlStartTimer:
178 NKern::ThreadEnterCS();
180 NKern::ThreadLeaveCS();
182 case RRndTim::EControlStopTimer:
183 NKern::ThreadEnterCS();
185 NKern::ThreadLeaveCS();
187 case RRndTim::EControlCalibrate:
188 NKern::ThreadEnterCS();
189 r = Calibrate(TInt(a1));
190 NKern::ThreadLeaveCS();
198 TInt DRndTim::SetPriority(TInt aHandle, TInt aPriority)
200 TInt r = KErrBadHandle;
201 DThread& c = Kern::CurrentThread();
202 NKern::ThreadEnterCS();
204 DThread* t = (DThread*)Kern::ObjectFromHandle(&c, aHandle, EThread);
207 NKern::UnlockSystem();
208 r = Kern::SetThreadPriority(aPriority, t);
212 NKern::UnlockSystem();
213 NKern::ThreadLeaveCS();
217 TInt DRndTim::Calibrate(TInt aMilliseconds)
220 TInt ticks = NKern::TimerTicks(aMilliseconds);
224 return (TInt)(n2-n1);
227 void DRndTim::StartTimer()
229 #if defined(__MISA__)
230 // for SA11x0 use OST match 0
231 TSa1100::ModifyIntLevels(0,KHtIntsOstMatchGeneral); // route new timer interrupt to FIQ
232 TSa1100::SetOstMatchEOI(KHwOstMatchGeneral);
233 TUint oscr=TSa1100::OstData();
234 TSa1100::SetOstMatch(KHwOstMatchGeneral, oscr + 5000);
235 TSa1100::EnableOstInterrupt(KHwOstMatchGeneral);
236 #elif defined(__MCOT__)
237 // for SA11x0 use OST match 0
238 TCotulla::ModifyIntLevels(0,KHtIntsOstMatchGeneral); // route new timer interrupt to FIQ
239 TCotulla::SetOstMatchEOI(KHwOstMatchGeneral);
240 TUint oscr=TCotulla::OstData();
241 TCotulla::SetOstMatch(KHwOstMatchGeneral, oscr + 5000);
242 TCotulla::EnableOstInterrupt(KHwOstMatchGeneral);
244 Interrupt::Enable(INT_ID);
247 void DRndTim::StopTimer()
249 #if defined(__MISA__)
250 Interrupt::Disable(KIntIdOstMatchGeneral);
251 TSa1100::DisableOstInterrupt(KHwOstMatchGeneral);
252 TSa1100::SetOstMatchEOI(KHwOstMatchGeneral);
253 #elif defined(__MCOT__)
254 Interrupt::Disable(KIntIdOstMatchGeneral);
255 TCotulla::DisableOstInterrupt(KHwOstMatchGeneral);
256 TCotulla::SetOstMatchEOI(KHwOstMatchGeneral);
260 void DRndTim::TimerIsr(TAny* aPtr)
262 DRndTim* d = (DRndTim*)aPtr;
264 #if defined(__MISA__)
265 TUint interval = Random((TUint*)d->iSeed);
267 interval += 256; // 256-1279 ticks = approx 69 to 347 microseconds
268 TUint oscr=TSa1100::OstData();
269 TSa1100::SetOstMatch(KHwOstMatchGeneral, oscr + interval);
270 TSa1100::SetOstMatchEOI(KHwOstMatchGeneral);
271 #elif defined(__MCOT__)
272 TUint interval = Random((TUint*)d->iSeed);
274 interval += 256; // 256-1279 ticks = approx 69 to 347 microseconds
275 TUint oscr=TCotulla::OstData();
276 TCotulla::SetOstMatch(KHwOstMatchGeneral, oscr + interval);
277 TCotulla::SetOstMatchEOI(KHwOstMatchGeneral);
282 void DRndTim::IDfcFn(TAny* aPtr)
284 DRndTim* d = (DRndTim*)aPtr;