LPC_Timer.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*************************************************************************
  2. *
  3. * Used with ICCARM and AARM.
  4. *
  5. * (c) Copyright IAR Systems 2003
  6. *
  7. * File name : LPC_Timer.c
  8. * Description : Define API for Timer system
  9. *
  10. * History :
  11. * 1. Date: July 08, 2004
  12. * Author: Wilson Liu
  13. * Description: Create the basic function
  14. *
  15. * 2. Date: August 12, 2004
  16. * Author: Shawn Zhang
  17. * Description: Re-write some API interface
  18. *
  19. * 3. Data : Oct 10, 2004
  20. * Author : Stanimir Bonev
  21. * Description : Modify some function and interface
  22. *
  23. * $Revision: 1.1 $
  24. **************************************************************************/
  25. #include "LPC_Timer.h"
  26. LPC_Timer_Config_t Timer0Config, Timer1Config;
  27. /*************************************************************************
  28. * Function Name: TIMER_Init
  29. * Parameters: LPC_TimerChanel_t DevNum
  30. * unsigned int precision -- the timer precision (Unit: us), general setting is 10 us
  31. * Return: int
  32. * 0: success
  33. * non-zero: error number
  34. * Description: Initialize Timer, Set the PR register that represent the precision of timer.
  35. *
  36. *************************************************************************/
  37. int TIMER_Init(LPC_TimerChanel_t DevNum, unsigned long precision)
  38. {
  39. int i;
  40. //all registers are set to 0;
  41. switch (DevNum)
  42. {
  43. case TIMER0:
  44. // Set globe variable
  45. Timer0Config.Precision = precision;
  46. // PR = Precision(us) * Pclk
  47. Timer0Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
  48. for (i=0; i<CH_MAXNUM; ++i)
  49. {
  50. Timer0Config.MatchCH[i].Enable = false;
  51. Timer0Config.MatchCH[i].Action = 0;
  52. Timer0Config.MatchCH[i].TimeValue =0;
  53. Timer0Config.MatchCH[i].Fnpr = NULL;
  54. Timer0Config.MatchCH[i].FnprArg = (void *)0;
  55. Timer0Config.CaptureCH[i].Enable = false;
  56. Timer0Config.CaptureCH[i].TriggerType= 0;
  57. Timer0Config.CaptureCH[i].EnableInt = 0;
  58. Timer0Config.CaptureCH[i].Fnpr = NULL;
  59. Timer0Config.CaptureCH[i].FnprArg = (void *)0;
  60. Timer0Config.CaptureCH[i].CPValue= 0;
  61. Timer0Config.ExtAction[i]= DONOTHING;
  62. Timer0Config.ExtBitValue[i]= 0;
  63. }
  64. // Clear interrupts flags
  65. T0IR=0xFF;
  66. // Disable counting
  67. T0TCR=0;
  68. // Clear timer counter
  69. T0TC=0;
  70. // PR = Presclare - 1
  71. T0PR= Timer0Config.Prescaler - 1;
  72. // Clear prescaler timer counter
  73. T0PC=0;
  74. // Reset Compare modules
  75. T0MCR=0;
  76. T0MR0=0;
  77. T0MR1=0;
  78. T0MR2=0;
  79. T0MR3=0;
  80. // Reset Capture modules
  81. T0CCR=0;
  82. // Reset External Compare module
  83. T0EMR=0;
  84. break;
  85. case TIMER1:
  86. // Set globe variable
  87. Timer1Config.Precision = precision;
  88. // PR = Precision(us) * Pclk
  89. Timer1Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
  90. for (i=0; i<CH_MAXNUM; ++i)
  91. {
  92. Timer1Config.MatchCH[i].Enable = false;
  93. Timer1Config.MatchCH[i].Action = 0;
  94. Timer1Config.MatchCH[i].TimeValue =0;
  95. Timer1Config.MatchCH[i].Fnpr = NULL;
  96. Timer1Config.MatchCH[i].FnprArg = (void *)0;
  97. Timer1Config.CaptureCH[i].Enable = false;
  98. Timer1Config.CaptureCH[i].TriggerType= 0;
  99. Timer1Config.CaptureCH[i].EnableInt = 0;
  100. Timer1Config.CaptureCH[i].Fnpr = NULL;
  101. Timer1Config.CaptureCH[i].FnprArg = (void *)0;
  102. Timer1Config.CaptureCH[i].CPValue= 0;
  103. Timer1Config.ExtAction[i]= DONOTHING;
  104. Timer1Config.ExtBitValue[i]= 0;
  105. }
  106. // Clear interrupts flags
  107. T1IR=0xFF;
  108. // Disable counting
  109. T1TCR=0;
  110. // Clear timer counter
  111. T1TC=0;
  112. // PR = Prescaler - 1
  113. T1PR=Timer1Config.Prescaler - 1;
  114. // Clear prescaler timer counter
  115. T1PC=0;
  116. // Reset Compare modules
  117. T1MCR=0;
  118. T1MR0=0;
  119. T1MR1=0;
  120. T1MR2=0;
  121. T1MR3=0;
  122. // Reset Capture modules
  123. T1CCR=0;
  124. // Reset External Compare module
  125. T1EMR=0;
  126. break;
  127. default:
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. /*************************************************************************
  133. * Function Name: TIMER_GetPrescaler
  134. * Parameters: LPC_TimerChanel_t DevNum
  135. * Return: unsigned int
  136. *
  137. *
  138. * Description: Return prescaler value
  139. *
  140. *
  141. *************************************************************************/
  142. unsigned long TIMER_GetPrescaler(LPC_TimerChanel_t DevNum)
  143. {
  144. switch (DevNum)
  145. {
  146. case TIMER0:
  147. return Timer0Config.Prescaler;
  148. case TIMER1:
  149. return Timer1Config.Prescaler;
  150. }
  151. return 0;
  152. }
  153. /*************************************************************************
  154. * Function Name: TIMER_Reset
  155. * Parameters: LPC_TimerChanel_t DevNum
  156. * Return: int
  157. * 0: success
  158. * non-zero: error number
  159. * Description: When next pclk arrives, only the TC and PC will be reset.
  160. * Whilst other registers remain.
  161. *
  162. *************************************************************************/
  163. int TIMER_Reset(LPC_TimerChanel_t DevNum)
  164. {
  165. switch (DevNum)
  166. {
  167. case TIMER0:
  168. T0TCR |= 2;
  169. return 0;
  170. case TIMER1:
  171. T1TCR |= 2;
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. /*************************************************************************
  177. * Function Name: TIMER_Start
  178. * Parameters: LPC_TimerChanel_t DevNum
  179. * Return: int
  180. * 0: success
  181. * non-zero: error number
  182. * Description: Start Timer or enable the Timer. if the Timer stop now, the Timer will
  183. * resume running after calling this function.
  184. *
  185. *************************************************************************/
  186. int TIMER_Start(LPC_TimerChanel_t DevNum)
  187. {
  188. switch (DevNum)
  189. {
  190. case TIMER0:
  191. // Clear reset flag
  192. T0TCR &= ~2;
  193. // Counting enable
  194. T0TCR_bit.CE = true;
  195. return 0;
  196. case TIMER1:
  197. // Clear reset flag
  198. T1TCR &= ~2;
  199. // Counting enable
  200. T1TCR_bit.CE = true;
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. /*************************************************************************
  206. * Function Name: TIMER_Stop
  207. * Parameters: LPC_TimerChanel_t DevNum
  208. * Return: int
  209. * 0: success
  210. * non-zero: error number
  211. * Description: Just stop Timer or disable Timer, all registers remain.
  212. *
  213. *************************************************************************/
  214. int TIMER_Stop(LPC_TimerChanel_t DevNum)
  215. {
  216. switch (DevNum)
  217. {
  218. case TIMER0:
  219. T0TCR_bit.CE = false;
  220. return 0;
  221. case TIMER1:
  222. T1TCR_bit.CE = false;
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. /*************************************************************************
  228. * Function Name: TIMER_GetTimerMatch
  229. * Parameters: LPC_TimerChanel_t DevNum,
  230. * unsigned int MRNum,
  231. * unsigned int * pAction ,
  232. * unsigned int * pMatchValue
  233. * Return: int
  234. * 0: success
  235. * non-zero: error number
  236. * Description: Get correponding matching information for specific timer.
  237. *
  238. *************************************************************************/
  239. int TIMER_GetTimerMatch(LPC_TimerChanel_t DevNum, unsigned int MRNum,
  240. unsigned int * pAction , unsigned int * pMatchValue)
  241. {
  242. if (MRNum >= CH_MAXNUM)
  243. return 1;
  244. switch(DevNum)
  245. {
  246. case TIMER0:
  247. *pMatchValue = Timer0Config.MatchCH[MRNum].TimeValue;
  248. *pAction = Timer0Config.MatchCH[MRNum].Action;
  249. break;
  250. case TIMER1:
  251. *pMatchValue = Timer1Config.MatchCH[MRNum].TimeValue;
  252. *pAction = Timer1Config.MatchCH[MRNum].Action;
  253. break;
  254. default:
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. /*************************************************************************
  260. * Function Name: TIMER_SetMatchAction
  261. * Parameters: LPC_TimerChanel_t DevNum -- Device Number
  262. * unsigned int MRNum -- Match Channel Number
  263. * unsigned int action -- General Interrupt | Reset Timer | Stop Timer
  264. * unsigned int Timevalue -- the time value (Unit: us)
  265. * void (* Fnpr)(void *) -- ISR function pointer
  266. * void * FnprArg -- relative argument
  267. * LPC_Timer_ExtAction_t ExtAction -- External Match Control Action
  268. *
  269. * Return: int
  270. * 0: success
  271. * non-zero: error number
  272. * Description: Set correponding matching action and other information to the channel
  273. * for specific timer.
  274. *
  275. *************************************************************************/
  276. int TIMER_SetMatchAction(LPC_TimerChanel_t DevNum,
  277. unsigned int MRNum,
  278. unsigned int action ,
  279. unsigned long TimeValue,
  280. void (* Fnpr)(void *),
  281. void * FnprArg,
  282. LPC_Timer_ExtAction_t ExtAction)
  283. {
  284. // Check parameter valid
  285. if (action>TimerAction_StopTimer+TimerAction_ResetTimer+TimerAction_Interrupt)
  286. return 1;
  287. if (ExtAction > TOGGLE)
  288. return 1;
  289. switch (DevNum)
  290. {
  291. case TIMER0:
  292. // Set Mash register
  293. switch (MRNum)
  294. {
  295. case CH0:
  296. T0MR0=TimeValue;
  297. break;
  298. case CH1:
  299. T0MR1=TimeValue;
  300. break;
  301. case CH2:
  302. T0MR2=TimeValue;
  303. break;
  304. case CH3:
  305. T0MR3=TimeValue;
  306. break;
  307. default:
  308. return 1;
  309. }
  310. Timer0Config.MatchCH[MRNum].Enable = true;
  311. Timer0Config.MatchCH[MRNum].Action = action;
  312. Timer0Config.MatchCH[MRNum].TimeValue= TimeValue;
  313. Timer0Config.ExtAction[MRNum]= ExtAction;
  314. // Clear actions
  315. T0MCR &= ~(7<<(MRNum*3));
  316. //Set Reset on Match
  317. if (action & TimerAction_ResetTimer)
  318. T0MCR |= TimerAction_ResetTimer << (3*MRNum);
  319. //Set StopWatch on Match
  320. if (action & TimerAction_StopTimer)
  321. T0MCR |= TimerAction_StopTimer << (3*MRNum);
  322. //Set Interrupt on Match
  323. if (action & TimerAction_Interrupt)
  324. {
  325. Timer0Config.MatchCH[MRNum].Fnpr = Fnpr;
  326. Timer0Config.MatchCH[MRNum].FnprArg = FnprArg;
  327. T0MCR |= TimerAction_Interrupt << (3*MRNum);
  328. }
  329. // Clear External action
  330. T0EMR &= ~(3 << (4 + 2*MRNum));
  331. // Set External action
  332. switch (MRNum)
  333. {
  334. case CH0:
  335. // Set External action type
  336. T0EMR_bit.EMC0 = ExtAction;
  337. // Assign pin to match modyle
  338. if (ExtAction != DONOTHING)
  339. PINSEL0_bit.P0_3 = 0x2;
  340. break;
  341. case CH1:
  342. // Set External action type
  343. T0EMR_bit.EMC1 = ExtAction;
  344. // Assign pin to match modyle
  345. if (ExtAction != DONOTHING)
  346. PINSEL0_bit.P0_5 = 0x2;
  347. break;
  348. case CH2:
  349. // Set External action type
  350. T0EMR_bit.EMC2 = ExtAction;
  351. // Assign pin to match modyle
  352. if (ExtAction != DONOTHING)
  353. PINSEL1_bit.P0_16 = 0x2;
  354. break;
  355. case CH3:
  356. // Set External action type
  357. T0EMR_bit.EMC3 = ExtAction;
  358. break;
  359. }
  360. return 0;
  361. case TIMER1:
  362. // Set Mash register
  363. switch (MRNum)
  364. {
  365. case CH0:
  366. T1MR0=TimeValue;
  367. break;
  368. case CH1:
  369. T1MR1=TimeValue;
  370. break;
  371. case CH2:
  372. T1MR2=TimeValue;
  373. break;
  374. case CH3:
  375. T1MR3=TimeValue;
  376. break;
  377. default:
  378. return 1;
  379. }
  380. Timer1Config.MatchCH[MRNum].Enable = true;
  381. Timer1Config.MatchCH[MRNum].Action = action;
  382. Timer1Config.MatchCH[MRNum].TimeValue= TimeValue;
  383. Timer1Config.ExtAction[MRNum]= ExtAction;
  384. // Clear actions
  385. T1MCR &= ~(7<<(MRNum*3));
  386. //Set Reset on Match
  387. if (action & TimerAction_ResetTimer)
  388. T1MCR |= TimerAction_ResetTimer << (3*MRNum);
  389. //Set StopWatch on Match
  390. if (action & TimerAction_StopTimer)
  391. T1MCR |= TimerAction_StopTimer << (3*MRNum);
  392. //Set Interrupt on Match
  393. if (action & TimerAction_Interrupt)
  394. {
  395. Timer1Config.MatchCH[MRNum].Fnpr = Fnpr;
  396. Timer1Config.MatchCH[MRNum].FnprArg = FnprArg;
  397. T1MCR |= TimerAction_Interrupt << (3*MRNum);
  398. }
  399. // Clear External action
  400. T1EMR &= ~(3 << (4 + 2*MRNum));
  401. // Set External action
  402. switch (MRNum)
  403. {
  404. case CH0:
  405. // Set External action type
  406. T1EMR_bit.EMC0 = ExtAction;
  407. // Assign pin to match modyle
  408. if (ExtAction != DONOTHING)
  409. PINSEL0_bit.P0_12 = 0x2;
  410. break;
  411. case CH1:
  412. // Set External action type
  413. T1EMR_bit.EMC1 = ExtAction;
  414. // Assign pin to match modyle
  415. if (ExtAction != DONOTHING)
  416. PINSEL0_bit.P0_13 = 0x2;
  417. break;
  418. case CH2:
  419. // Set External action type
  420. T1EMR_bit.EMC2 = ExtAction;
  421. // Assign pin to match modyle
  422. if (ExtAction != DONOTHING)
  423. PINSEL1_bit.P0_19 = 0x2;
  424. break;
  425. case CH3:
  426. // Set External action type
  427. T0EMR_bit.EMC3 = ExtAction;
  428. // Assign pin to match modyle
  429. if (ExtAction != DONOTHING)
  430. PINSEL1_bit.P0_20 = 0x2;
  431. break;
  432. }
  433. return 0;
  434. }
  435. return 1;
  436. }
  437. /*************************************************************************
  438. * Function Name: TIMER_GetTimerExternalMatch
  439. * Parameters: LPC_TimerChanel_t DevNum
  440. * unsigned int MRNum,
  441. * unsigned int *pAction,
  442. * unsigned int *pExternalMatchValue
  443. * Return: int
  444. * 0: success
  445. * non-zero: error number
  446. * Description: Get correponding external matching information from specific timer.
  447. *
  448. *************************************************************************/
  449. int TIMER_GetTimerExternalMatch(LPC_TimerChanel_t DevNum, unsigned int MRNum,
  450. unsigned int * pAction , unsigned int *pExternalMatchValue)
  451. {
  452. if (MRNum >= CH_MAXNUM)
  453. return 1;
  454. switch(DevNum)
  455. {
  456. case TIMER0:
  457. *pExternalMatchValue = Timer0Config.ExtAction[MRNum];
  458. *pAction = Timer0Config.MatchCH[MRNum].Action;
  459. break;
  460. case TIMER1:
  461. *pExternalMatchValue = Timer1Config.ExtAction[MRNum];
  462. *pAction = Timer1Config.MatchCH[MRNum].Action;
  463. break;
  464. default:
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. /*************************************************************************
  470. * Function Name: TIMER_GetTimerCapture
  471. * Parameters: LPC_TimerChanel_t DevNum,
  472. * unsigned int CRNum,
  473. * unsigned int * pCaptureValue
  474. * Return: int
  475. * 0: success
  476. * non-zero: error number
  477. * Description: Get correponding capture information from specific timer.
  478. *
  479. *************************************************************************/
  480. int TIMER_GetTimerCapture(LPC_TimerChanel_t DevNum, unsigned int CRNum,
  481. unsigned int * pCaptureValue)
  482. {
  483. switch(DevNum)
  484. {
  485. case TIMER0:
  486. if (CRNum >= CPCH_MAXNUM-1)
  487. return 1;
  488. *pCaptureValue = Timer0Config.CaptureCH[CRNum].CPValue;
  489. break;
  490. case TIMER1:
  491. if (CRNum >= CPCH_MAXNUM)
  492. return 1;
  493. *pCaptureValue = Timer1Config.CaptureCH[CRNum].CPValue;
  494. break;
  495. default:
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. /*************************************************************************
  501. * Function Name: TIMER_SetCaptureAction
  502. * Parameters: LPC_TimerChanel_t DevNum -- Device Number
  503. * unsigned char CPCHNum -- Capture Channel Number
  504. * unsigned char TriggerType -- Rising edge | Falling edge
  505. * bool EnableInt -- whether interrupt is generated
  506. * void (* Fnpr)(void *) -- ISR function pointer
  507. * void * FnprArg -- relative argument
  508. *
  509. * Return: int
  510. * 0: success
  511. * non-zero: error number
  512. * Description: Set correponding capture trigger type and other information to the channel
  513. * for specific timer.
  514. *
  515. *************************************************************************/
  516. int TIMER_SetCaptureAction (LPC_TimerChanel_t DevNum,
  517. unsigned char CPCHNum,
  518. unsigned char TriggerType,
  519. bool EnableInt,
  520. void (* Fnpr)(void *),
  521. void * FnprArg )
  522. {
  523. // Check parameter valid
  524. if (TriggerType > TimerCPTrigger_Rising+TimerCPTrigger_Falling)
  525. return 1;
  526. switch (DevNum)
  527. {
  528. case TIMER0:
  529. if (CPCHNum > CPCH_MAXNUM-2) // for timer 0 cature chanel 3 is not exist
  530. return 1;
  531. Timer0Config.CaptureCH[CPCHNum].Enable = true;
  532. Timer0Config.CaptureCH[CPCHNum].TriggerType = TriggerType;
  533. // Clear Capture actions
  534. T0CCR &= ~(7 << (3*CPCHNum));
  535. // Assign of pin to capture module
  536. switch (CPCHNum)
  537. {
  538. case CH0:
  539. if (TriggerType == 0)
  540. PINSEL0_bit.P0_2 = 0;
  541. else
  542. PINSEL0_bit.P0_2 = 0x2;
  543. break;
  544. case CH1:
  545. if (TriggerType == 0)
  546. PINSEL0_bit.P0_4 = 0;
  547. else
  548. PINSEL0_bit.P0_4 = 0x2;
  549. break;
  550. case CH2:
  551. if (TriggerType == 0)
  552. PINSEL0_bit.P0_6 = 0;
  553. else
  554. PINSEL0_bit.P0_6 = 0x2;
  555. break;
  556. }
  557. //Set Capture on Rising Edge
  558. if (TriggerType & TimerCPTrigger_Rising)
  559. T0CCR |= TimerCPTrigger_Rising << (3*CPCHNum);
  560. //Set Capture on Falling Edge
  561. if (TriggerType & TimerCPTrigger_Falling)
  562. T0CCR |= TimerCPTrigger_Falling << (3*CPCHNum);
  563. //Set Interrupt on Capture
  564. if (EnableInt && TriggerType)
  565. {
  566. Timer0Config.CaptureCH[CPCHNum].EnableInt = true;
  567. Timer0Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
  568. Timer0Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
  569. T0CCR |= 0x4 << (3*CPCHNum);
  570. }
  571. else
  572. {
  573. Timer0Config.CaptureCH[CPCHNum].EnableInt = false;
  574. Timer0Config.CaptureCH[CPCHNum].Fnpr = NULL;
  575. Timer0Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
  576. }
  577. return 0;
  578. case TIMER1:
  579. if (CPCHNum > CPCH_MAXNUM-1)
  580. return 1;
  581. Timer1Config.CaptureCH[CPCHNum].Enable = true;
  582. Timer1Config.CaptureCH[CPCHNum].TriggerType = TriggerType;
  583. // Clear Capture actions
  584. T1CCR &= ~(7 << (3*CPCHNum));
  585. // Assign of pin to capture module
  586. switch (CPCHNum)
  587. {
  588. case CH0:
  589. if (TriggerType == 0)
  590. PINSEL0_bit.P0_10 = 0;
  591. else
  592. PINSEL0_bit.P0_10 = 0x2;
  593. break;
  594. case CH1:
  595. if (TriggerType)
  596. PINSEL0_bit.P0_11 = 0x2;
  597. break;
  598. case CH2:
  599. if (TriggerType == 0)
  600. PINSEL1_bit.P0_17 = 0;
  601. else
  602. PINSEL1_bit.P0_17 = 0x2;
  603. break;
  604. case CH3:
  605. if (TriggerType == 0)
  606. PINSEL1_bit.P0_18 = 0;
  607. else
  608. PINSEL1_bit.P0_18 = 0x2;
  609. break;
  610. }
  611. //Set Capture on Rising Edge
  612. if (TriggerType & TimerCPTrigger_Rising)
  613. T1CCR |= TimerCPTrigger_Rising << (3*CPCHNum);
  614. //Set Capture on Falling Edge
  615. if (TriggerType & TimerCPTrigger_Falling)
  616. T1CCR |= TimerCPTrigger_Falling << (3*CPCHNum);
  617. //Set Interrupt on Capture
  618. if (EnableInt && TriggerType)
  619. {
  620. Timer1Config.CaptureCH[CPCHNum].EnableInt = true;
  621. Timer1Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
  622. Timer1Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
  623. T1CCR |= 0x4 << (3*CPCHNum);
  624. }
  625. else
  626. {
  627. Timer1Config.CaptureCH[CPCHNum].EnableInt = false;
  628. Timer1Config.CaptureCH[CPCHNum].Fnpr = NULL;
  629. Timer1Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
  630. }
  631. return 0;
  632. }
  633. return 1;
  634. }
  635. /*************************************************************************
  636. * Function Name: TIMER_GetREGValue_CR
  637. * Parameters: LPC_TimerChanel_t DevNum
  638. * int CRNum
  639. * Return: unsigned long
  640. *
  641. * Description: Get CR register value
  642. *
  643. *************************************************************************/
  644. unsigned long TIMER_GetREGValue_CR(LPC_TimerChanel_t DevNum, int CRNum)
  645. {
  646. switch (DevNum)
  647. {
  648. case TIMER0:
  649. switch(CRNum)
  650. {
  651. case CPCH0:
  652. return T0CR0;
  653. case CPCH1:
  654. return T0CR1;
  655. case CPCH2:
  656. return T0CR2;
  657. case CPCH3:
  658. return T0CR3;
  659. }
  660. case TIMER1:
  661. switch(CRNum)
  662. {
  663. case CPCH0:
  664. return T1CR0;
  665. case CPCH1:
  666. return T1CR1;
  667. case CPCH2:
  668. return T1CR2;
  669. case CPCH3:
  670. return T1CR3;
  671. }
  672. }
  673. return (unsigned int)-1;
  674. }
  675. /*************************************************************************
  676. * Function Name: TIMER_GetREGValue_TC
  677. * Parameters: LPC_TimerChanel_t DevNum
  678. * Return: unsigned long
  679. *
  680. * Description: Get TC register value
  681. *
  682. *************************************************************************/
  683. unsigned long TIMER_GetREGValue_TC(LPC_TimerChanel_t DevNum)
  684. {
  685. switch (DevNum)
  686. {
  687. case TIMER0:
  688. return T0TC;
  689. case TIMER1:
  690. return T1TC;
  691. }
  692. return (unsigned int)-1;
  693. }
  694. /*************************************************************************
  695. * Function Name: TIMER_CheckIntSrc
  696. * Parameters: LPC_TimerChanel_t DevNum
  697. * Return: unsigned long
  698. * TIMERMR0...3Int | TIMERCR0...3Int
  699. *
  700. * Description: Get Timer interrupt Type
  701. *
  702. *************************************************************************/
  703. unsigned long TIMER_CheckIntType(LPC_TimerChanel_t DevNum)
  704. {
  705. switch (DevNum)
  706. {
  707. case TIMER0:
  708. return (T0IR & 0xFF);
  709. case TIMER1:
  710. return (T1IR & 0xFF);
  711. default:
  712. return (unsigned long)-1;
  713. }
  714. }
  715. /*************************************************************************
  716. * Function Name: RTC_ClearInt
  717. * Parameters: LPC_TimerChanel_t DevNum
  718. * int IntType
  719. *
  720. * Return: unsigned long
  721. * 0: sucess
  722. * 1: fail
  723. *
  724. * Description: Clear Timer interrupt.
  725. *
  726. *************************************************************************/
  727. unsigned long TIMER_ClearInt(LPC_TimerChanel_t DevNum, int IntType)
  728. {
  729. if (IntType<1 || IntType>0xFF)
  730. return 1;
  731. switch (DevNum)
  732. {
  733. case TIMER0:
  734. T0IR = (IntType & 0xFF);
  735. break;
  736. case TIMER1:
  737. T1IR = (IntType & 0xFF);
  738. break;
  739. default:
  740. return 1;
  741. }
  742. return 0;
  743. }
  744. /*************************************************************************
  745. * Function Name: T0ISR
  746. * Parameters: void
  747. * Return: void
  748. *
  749. * Description: TIMER0 interrupt subroutine
  750. *
  751. *************************************************************************/
  752. void TIMER0_ISR ()
  753. {
  754. int IntStatus;
  755. IntStatus = TIMER_CheckIntType(TIMER0);
  756. TIMER_ClearInt(TIMER0, IntStatus);
  757. /* Match Register Interrupt */
  758. if (IntStatus & TIMERMR0Int)
  759. {
  760. (Timer0Config.MatchCH[0].Fnpr)((void *)Timer0Config.MatchCH[0].FnprArg);
  761. }
  762. if (IntStatus & TIMERMR1Int)
  763. {
  764. (Timer0Config.MatchCH[1].Fnpr)((void *)Timer0Config.MatchCH[1].FnprArg);
  765. }
  766. if (IntStatus & TIMERMR2Int)
  767. {
  768. (Timer0Config.MatchCH[2].Fnpr)((void *)Timer0Config.MatchCH[2].FnprArg);
  769. }
  770. if (IntStatus & TIMERMR3Int)
  771. {
  772. (Timer0Config.MatchCH[3].Fnpr)((void *)Timer0Config.MatchCH[3].FnprArg);
  773. }
  774. /* Capture Register Interrupt */
  775. if (IntStatus & TIMERCR0Int)
  776. {
  777. Timer0Config.CaptureCH[0].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH0);
  778. (Timer0Config.CaptureCH[0].Fnpr)((void *)Timer0Config.CaptureCH[0].FnprArg);
  779. }
  780. if (IntStatus & TIMERCR1Int)
  781. {
  782. Timer0Config.CaptureCH[1].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH1);
  783. (Timer0Config.CaptureCH[1].Fnpr)((void *)Timer0Config.CaptureCH[1].FnprArg);
  784. }
  785. if (IntStatus & TIMERCR2Int)
  786. {
  787. Timer0Config.CaptureCH[2].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH2);
  788. (Timer0Config.CaptureCH[2].Fnpr)((void *)Timer0Config.CaptureCH[2].FnprArg);
  789. }
  790. if (IntStatus & TIMERCR3Int)
  791. {
  792. Timer0Config.CaptureCH[3].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH3);
  793. (Timer0Config.CaptureCH[3].Fnpr)((void *)Timer0Config.CaptureCH[3].FnprArg);
  794. }
  795. VICVectAddr = 0; // Clear interrupt in VIC.
  796. }
  797. /*************************************************************************
  798. * Function Name: T1ISR
  799. * Parameters: void
  800. * Return: void
  801. *
  802. * Description: TIMER1 interrupt subroutine
  803. *
  804. *************************************************************************/
  805. void TIMER1_ISR ()
  806. {
  807. int IntStatus;
  808. IntStatus = TIMER_CheckIntType(TIMER1);
  809. TIMER_ClearInt(TIMER1, IntStatus);
  810. /* Match Register Interrupt */
  811. if (IntStatus & TIMERMR0Int)
  812. {
  813. (Timer1Config.MatchCH[0].Fnpr)((void *)Timer1Config.MatchCH[0].FnprArg);
  814. }
  815. if (IntStatus & TIMERMR1Int)
  816. {
  817. (Timer1Config.MatchCH[1].Fnpr)((void *)Timer1Config.MatchCH[1].FnprArg);
  818. }
  819. if (IntStatus & TIMERMR2Int)
  820. {
  821. (Timer1Config.MatchCH[2].Fnpr)((void *)Timer1Config.MatchCH[2].FnprArg);
  822. }
  823. if (IntStatus & TIMERMR3Int)
  824. {
  825. (Timer1Config.MatchCH[3].Fnpr)((void *)Timer1Config.MatchCH[3].FnprArg);
  826. }
  827. /* Capture Register Interrupt */
  828. if (IntStatus & TIMERCR0Int)
  829. {
  830. Timer1Config.CaptureCH[0].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH0);
  831. (Timer1Config.CaptureCH[0].Fnpr)((void *)Timer1Config.CaptureCH[0].FnprArg);
  832. }
  833. if (IntStatus & TIMERCR1Int)
  834. {
  835. Timer1Config.CaptureCH[1].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH1);
  836. (Timer1Config.CaptureCH[1].Fnpr)((void *)Timer1Config.CaptureCH[1].FnprArg);
  837. }
  838. if (IntStatus & TIMERCR2Int)
  839. {
  840. Timer1Config.CaptureCH[2].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH2);
  841. (Timer1Config.CaptureCH[2].Fnpr)((void *)Timer1Config.CaptureCH[2].FnprArg);
  842. }
  843. if (IntStatus & TIMERCR3Int)
  844. {
  845. Timer1Config.CaptureCH[3].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH3);
  846. (Timer1Config.CaptureCH[3].Fnpr)((void *)Timer1Config.CaptureCH[3].FnprArg);
  847. }
  848. VICVectAddr = 0; // Clear interrupt in VIC.
  849. }