/CoX/CoX_Peripheral/CoX_Peripheral_M051/timer/test/suite1/src/xtimertest2.c

https://github.com/coocox/cox · C · 373 lines · 171 code · 41 blank · 161 comment · 23 complexity · 214956384291f101cf0d20c971ab7490 MD5 · raw file

  1. //*****************************************************************************
  2. //
  3. //! @page xtimer_testcase xtimer mode test
  4. //!
  5. //! File: @ref xtimertest2.c
  6. //!
  7. //! <h2>Description</h2>
  8. //! This module implements the test sequence for the xtimer sub component.<br><br>
  9. //! - \p Board: M051 board <br><br>
  10. //! - \p Last-Time(about): 0.5s <br><br>
  11. //! - \p Phenomenon: Success or failure information will be printed on the UART. <br><br>
  12. //! .
  13. //!
  14. //! <h2>Preconditions</h2>
  15. //! The module requires the following options:<br><br>
  16. //! - \p Option-define:
  17. //! <br>(1)None.<br><br>
  18. //! - \p Option-hardware:
  19. //! <br>(1)Connect an USB cable to the development board.<br><br>
  20. //! - \p Option-OtherModule:
  21. //! <br>Connect an COM cable to the development board.<br>
  22. //! .
  23. //! In case some of the required options are not enabled then some or all tests
  24. //! may be skipped or result FAILED.<br>
  25. //!
  26. //! <h2>Test Cases</h2>
  27. //! The module contain those sub tests:<br><br>
  28. //! - \subpage test_xtimer_mode
  29. //! .
  30. //! \file xtimertest2.c
  31. //! \brief xtimer test source file
  32. //! \brief xtimer test header file <br>
  33. //
  34. //*****************************************************************************
  35. #include "test.h"
  36. #include "xhw_timer.h"
  37. #include "xtimer.h"
  38. //*****************************************************************************
  39. //
  40. //!\page test_xtimer_register test_xtimer_mode
  41. //!
  42. //!<h2>Description</h2>
  43. //!Test xtimer mode. <br>
  44. //!
  45. //
  46. //*****************************************************************************
  47. //*****************************************************************************
  48. //
  49. // Define The 4 timer base address array, and Peripheral ID array.
  50. //
  51. //*****************************************************************************
  52. //
  53. // Timer base array
  54. //
  55. static unsigned long ulTimerBase[4] = {TIMER0_BASE, TIMER1_BASE,
  56. TIMER2_BASE, TIMER3_BASE};
  57. //
  58. // Timer ID array
  59. //
  60. static unsigned long ulTimerID[4] = {xSYSCTL_PERIPH_TIMER0,
  61. xSYSCTL_PERIPH_TIMER1,
  62. xSYSCTL_PERIPH_TIMER2,
  63. xSYSCTL_PERIPH_TIMER3};
  64. //
  65. // Timer interrupt ID
  66. //
  67. static unsigned long ulTimerIntID[4] = {xINT_TIMER0, xINT_TIMER1,
  68. xINT_TIMER2, xINT_TIMER3};
  69. //
  70. // Install callback function
  71. //
  72. unsigned long Timer0Callback(void *pvCBData, unsigned long ulEvent,
  73. unsigned long ulMsgParam,
  74. void *pvMsgData)
  75. {
  76. TestEmitToken('a');
  77. return 0;
  78. }
  79. unsigned long Timer1Callback(void *pvCBData, unsigned long ulEvent,
  80. unsigned long ulMsgParam,
  81. void *pvMsgData)
  82. {
  83. TestEmitToken('a');
  84. return 0;
  85. }
  86. unsigned long Timer2Callback(void *pvCBData, unsigned long ulEvent,
  87. unsigned long ulMsgParam,
  88. void *pvMsgData)
  89. {
  90. TestEmitToken('a');
  91. return 0;
  92. }
  93. unsigned long Timer3Callback(void *pvCBData, unsigned long ulEvent,
  94. unsigned long ulMsgParam,
  95. void *pvMsgData)
  96. {
  97. TestEmitToken('a');
  98. return 0;
  99. }
  100. //
  101. // Callback function
  102. //
  103. xtEventCallback TimerCallbackFunc[4] = {Timer0Callback,
  104. Timer1Callback,
  105. Timer2Callback,
  106. Timer3Callback};
  107. //*****************************************************************************
  108. //
  109. //! \brief Get the Test description of xtimer001 register test.
  110. //!
  111. //! \return the desccription of the xtimer001 test.
  112. //
  113. //*****************************************************************************
  114. static char* xTimer001GetTest(void)
  115. {
  116. return "xTimer [001]: xtimer mode test";
  117. }
  118. //*****************************************************************************
  119. //
  120. //! \brief something should do before the test execute of xtimer001 test.
  121. //!
  122. //! \return None.
  123. //
  124. //*****************************************************************************
  125. static void xTimer001Setup(void)
  126. {
  127. int i;
  128. //
  129. //Set the external 12MHZ clock as system clock
  130. //
  131. SysCtlKeyAddrUnlock();
  132. xSysCtlClockSet(12000000, xSYSCTL_XTAL_6MHZ | xSYSCTL_OSC_MAIN);
  133. //
  134. // Set the timer clock
  135. //
  136. SysCtlPeripheralClockSourceSet(SYSCTL_PERIPH_TMR0_S_EXT12M);
  137. SysCtlPeripheralClockSourceSet(SYSCTL_PERIPH_TMR1_S_EXT12M);
  138. SysCtlPeripheralClockSourceSet(SYSCTL_PERIPH_TMR2_S_EXT12M);
  139. SysCtlPeripheralClockSourceSet(SYSCTL_PERIPH_TMR3_S_EXT12M);
  140. //
  141. // Enable the tiemr0-3 peripheral
  142. //
  143. for(i = 0; i < 4; i++)
  144. {
  145. xSysCtlPeripheralEnable(ulTimerID[i]);
  146. }
  147. }
  148. //*****************************************************************************
  149. //
  150. //! \brief something should do after the test execute of xtimer001 test.
  151. //!
  152. //! \return None.
  153. //
  154. //*****************************************************************************
  155. static void xTimer001TearDown(void)
  156. {
  157. int i;
  158. //
  159. // Enable the tiemr0-3 peripheral
  160. //
  161. for(i = 0; i < 4; i++)
  162. {
  163. xSysCtlPeripheralDisable(ulTimerID[i]);
  164. }
  165. }
  166. //*****************************************************************************
  167. //
  168. //! \brief xtimer 001 test execute main body.
  169. //!
  170. //! \return None.
  171. //
  172. //*****************************************************************************
  173. static void xTimer001Execute(void)
  174. {
  175. unsigned long ulTemp;
  176. unsigned long ulBase;
  177. int i, j ;
  178. //
  179. // One shot mode test.
  180. //
  181. for(i = 0; i < 4; i++)
  182. {
  183. ulBase = ulTimerBase[i];
  184. //
  185. // Clear the flag first
  186. //
  187. TimerIntClear(ulBase, TIMER_INT_MATCH);
  188. while(TimerIntStatus(ulBase, TIMER_INT_MATCH));
  189. //
  190. // Config as One shot mode
  191. //
  192. TimerInitConfig(ulBase, TIMER_MODE_ONESHOT, 1000);
  193. TimerIntEnable(ulBase, TIMER_INT_MATCH);
  194. xIntEnable(ulTimerIntID[i]);
  195. xTimerIntCallbackInit(ulBase, TimerCallbackFunc[i]);
  196. TimerStart(ulBase);
  197. //
  198. // wait until the timer data register reach equel to compare register
  199. //
  200. TestAssertQBreak("a","One shot mode Intterrupt test fail", -1);
  201. xIntDisable(ulTimerIntID[i]);
  202. }
  203. //
  204. // Periodic mode
  205. //
  206. for(i = 0; i < 4; i++)
  207. {
  208. ulBase = ulTimerBase[i];
  209. ulTemp = 0;
  210. //
  211. // Clear the flag first
  212. //
  213. TimerIntClear(ulBase, TIMER_INT_MATCH);
  214. //
  215. // Config as periodic mode
  216. //
  217. TimerInitConfig(ulBase, TIMER_MODE_PERIODIC, 1000);
  218. TimerIntEnable(ulBase, TIMER_INT_MATCH);
  219. TimerStart(ulBase);
  220. //
  221. // wait the periodic repeat 5 times
  222. //
  223. for (j = 0; j < 5; j++)
  224. {
  225. while(!TimerIntStatus(ulBase, TIMER_INT_MATCH));
  226. ulTemp++;
  227. if(ulTemp == 5)
  228. {
  229. break;
  230. }
  231. TimerIntClear(ulBase, TIMER_INT_MATCH);
  232. }
  233. TestAssert(ulTemp == 5,
  234. "xtimer mode \" periodic test\" error!");
  235. TimerStop(ulBase);
  236. }
  237. //
  238. // Toggle mode test
  239. //
  240. for(i = 0; i < 4; i++)
  241. {
  242. ulBase = ulTimerBase[i];
  243. ulTemp = 0;
  244. //
  245. // Clear the Int flag first
  246. //
  247. TimerIntClear(ulBase, TIMER_INT_MATCH);
  248. //
  249. // Config as toggle mode
  250. //
  251. TimerInitConfig(ulBase, TIMER_MODE_PERIODIC, 1000);
  252. TimerIntEnable(ulBase, TIMER_INT_MATCH);
  253. TimerStart(ulBase);
  254. //
  255. // wait the toggle repeat 5 times
  256. //
  257. for (j = 0; j < 5; j++)
  258. {
  259. while(!TimerIntStatus(ulBase, TIMER_INT_MATCH));
  260. ulTemp++;
  261. if(ulTemp == 5)
  262. {
  263. break;
  264. }
  265. TimerIntClear(ulBase, TIMER_INT_MATCH);
  266. }
  267. TestAssert(ulTemp == 5,
  268. "xtimer mode \" Toggle mode test\" error!");
  269. TimerStop(ulBase);
  270. }
  271. //
  272. // Continuous mode test.
  273. //
  274. for(i = 0; i < 4; i++)
  275. {
  276. ulBase = ulTimerBase[i];
  277. ulTemp = 0;
  278. //
  279. // Clear the flag first
  280. //
  281. TimerIntClear(ulBase, TIMER_INT_MATCH);
  282. while(TimerIntStatus(ulBase, TIMER_INT_MATCH));
  283. //
  284. // Config as One shot mode
  285. //
  286. TimerInitConfig(ulBase, TIMER_MODE_CONTINUOUS, 10000);
  287. TimerMatchSet(ulBase, 10);
  288. TimerIntEnable(ulBase, TIMER_INT_MATCH);
  289. TimerStart(ulBase);
  290. //
  291. // Delay some time to wait the count register reach to 200.
  292. //
  293. xSysCtlDelay(100);
  294. if(TimerIntStatus(ulBase, TIMER_INT_MATCH) == xtrue)
  295. {
  296. ulTemp++;
  297. }
  298. TimerIntClear(ulBase, TIMER_INT_MATCH);
  299. TimerMatchSet(ulBase, 2000);
  300. //
  301. // Wait until reach the 1000
  302. //
  303. //while(!TimerIntStatus(ulBase, TIMER_INT_MATCH));
  304. xSysCtlDelay(10000);
  305. if(TimerIntStatus(ulBase, TIMER_INT_MATCH) == xtrue)
  306. {
  307. ulTemp++;
  308. }
  309. TimerIntClear(ulBase, TIMER_INT_MATCH);
  310. TestAssert(ulTemp == 2,
  311. "xtimer mode \" Continuous mode test\" error!");
  312. TimerStop(ulBase);
  313. xIntDisable(ulTimerIntID[i]);
  314. }
  315. }
  316. //
  317. // xtimer register test case struct.
  318. //
  319. const tTestCase sTestxTimer001Mode = {
  320. xTimer001GetTest,
  321. xTimer001Setup,
  322. xTimer001TearDown,
  323. xTimer001Execute
  324. };
  325. //
  326. // xtimer test suits.
  327. //
  328. const tTestCase * const psPatternXtimer002[] =
  329. {
  330. &sTestxTimer001Mode,
  331. 0
  332. };