rtx_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: Timer functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // OS Runtime Object Memory Usage
  27. #if ((defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0)))
  28. osRtxObjectMemUsage_t osRtxTimerMemUsage \
  29. __attribute__((section(".data.os.timer.obj"))) =
  30. { 0U, 0U, 0U };
  31. #endif
  32. // ==== Helper functions ====
  33. /// Insert Timer into the Timer List sorted by Time.
  34. /// \param[in] timer timer object.
  35. /// \param[in] tick timer tick.
  36. static void TimerInsert (os_timer_t *timer, uint32_t tick) {
  37. os_timer_t *prev, *next;
  38. prev = NULL;
  39. next = osRtxInfo.timer.list;
  40. while ((next != NULL) && (next->tick <= tick)) {
  41. tick -= next->tick;
  42. prev = next;
  43. next = next->next;
  44. }
  45. timer->tick = tick;
  46. timer->prev = prev;
  47. timer->next = next;
  48. if (next != NULL) {
  49. next->tick -= timer->tick;
  50. next->prev = timer;
  51. }
  52. if (prev != NULL) {
  53. prev->next = timer;
  54. } else {
  55. osRtxInfo.timer.list = timer;
  56. }
  57. }
  58. /// Remove Timer from the Timer List.
  59. /// \param[in] timer timer object.
  60. static void TimerRemove (const os_timer_t *timer) {
  61. if (timer->next != NULL) {
  62. timer->next->tick += timer->tick;
  63. timer->next->prev = timer->prev;
  64. }
  65. if (timer->prev != NULL) {
  66. timer->prev->next = timer->next;
  67. } else {
  68. osRtxInfo.timer.list = timer->next;
  69. }
  70. }
  71. /// Unlink Timer from the Timer List Head.
  72. /// \param[in] timer timer object.
  73. static void TimerUnlink (const os_timer_t *timer) {
  74. if (timer->next != NULL) {
  75. timer->next->prev = timer->prev;
  76. }
  77. osRtxInfo.timer.list = timer->next;
  78. }
  79. // ==== Library functions ====
  80. /// Timer Tick (called each SysTick).
  81. static void osRtxTimerTick (void) {
  82. os_timer_t *timer;
  83. osStatus_t status;
  84. timer = osRtxInfo.timer.list;
  85. if (timer == NULL) {
  86. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  87. return;
  88. }
  89. timer->tick--;
  90. while ((timer != NULL) && (timer->tick == 0U)) {
  91. TimerUnlink(timer);
  92. status = osMessageQueuePut(osRtxInfo.timer.mq, &timer->finfo, 0U, 0U);
  93. if (status != osOK) {
  94. (void)osRtxErrorNotify(osRtxErrorTimerQueueOverflow, timer);
  95. }
  96. if (timer->type == osRtxTimerPeriodic) {
  97. TimerInsert(timer, timer->load);
  98. } else {
  99. timer->state = osRtxTimerStopped;
  100. }
  101. timer = osRtxInfo.timer.list;
  102. }
  103. }
  104. /// Timer Thread
  105. __WEAK void osRtxTimerThread (void *argument) {
  106. os_timer_finfo_t finfo;
  107. osStatus_t status;
  108. (void) argument;
  109. osRtxInfo.timer.mq = osRtxMessageQueueId(
  110. osMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr)
  111. );
  112. if (osRtxInfo.timer.mq == NULL) {
  113. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  114. return;
  115. }
  116. osRtxInfo.timer.tick = osRtxTimerTick;
  117. for (;;) {
  118. //lint -e{934} "Taking address of near auto variable"
  119. status = osMessageQueueGet(osRtxInfo.timer.mq, &finfo, NULL, osWaitForever);
  120. if (status == osOK) {
  121. EvrRtxTimerCallback(finfo.func, finfo.arg);
  122. (finfo.func)(finfo.arg);
  123. }
  124. }
  125. }
  126. // ==== Service Calls ====
  127. /// Create and Initialize a timer.
  128. /// \note API identical to osTimerNew
  129. static osTimerId_t svcRtxTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  130. os_timer_t *timer;
  131. uint8_t flags;
  132. const char *name;
  133. // Check parameters
  134. if ((func == NULL) || ((type != osTimerOnce) && (type != osTimerPeriodic))) {
  135. EvrRtxTimerError(NULL, (int32_t)osErrorParameter);
  136. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  137. return NULL;
  138. }
  139. // Process attributes
  140. if (attr != NULL) {
  141. name = attr->name;
  142. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  143. timer = attr->cb_mem;
  144. if (timer != NULL) {
  145. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  146. if ((((uint32_t)timer & 3U) != 0U) || (attr->cb_size < sizeof(os_timer_t))) {
  147. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  148. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  149. return NULL;
  150. }
  151. } else {
  152. if (attr->cb_size != 0U) {
  153. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  154. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  155. return NULL;
  156. }
  157. }
  158. } else {
  159. name = NULL;
  160. timer = NULL;
  161. }
  162. // Allocate object memory if not provided
  163. if (timer == NULL) {
  164. if (osRtxInfo.mpi.timer != NULL) {
  165. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  166. timer = osRtxMemoryPoolAlloc(osRtxInfo.mpi.timer);
  167. } else {
  168. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  169. timer = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_timer_t), 1U);
  170. }
  171. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  172. if (timer != NULL) {
  173. uint32_t used;
  174. osRtxTimerMemUsage.cnt_alloc++;
  175. used = osRtxTimerMemUsage.cnt_alloc - osRtxTimerMemUsage.cnt_free;
  176. if (osRtxTimerMemUsage.max_used < used) {
  177. osRtxTimerMemUsage.max_used = used;
  178. }
  179. }
  180. #endif
  181. flags = osRtxFlagSystemObject;
  182. } else {
  183. flags = 0U;
  184. }
  185. if (timer != NULL) {
  186. // Initialize control block
  187. timer->id = osRtxIdTimer;
  188. timer->state = osRtxTimerStopped;
  189. timer->flags = flags;
  190. timer->type = (uint8_t)type;
  191. timer->name = name;
  192. timer->prev = NULL;
  193. timer->next = NULL;
  194. timer->tick = 0U;
  195. timer->load = 0U;
  196. timer->finfo.func = func;
  197. timer->finfo.arg = argument;
  198. EvrRtxTimerCreated(timer, timer->name);
  199. } else {
  200. EvrRtxTimerError(NULL, (int32_t)osErrorNoMemory);
  201. }
  202. return timer;
  203. }
  204. /// Get name of a timer.
  205. /// \note API identical to osTimerGetName
  206. static const char *svcRtxTimerGetName (osTimerId_t timer_id) {
  207. os_timer_t *timer = osRtxTimerId(timer_id);
  208. // Check parameters
  209. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  210. EvrRtxTimerGetName(timer, NULL);
  211. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  212. return NULL;
  213. }
  214. // Check object state
  215. if (timer->state == osRtxObjectInactive) {
  216. EvrRtxTimerGetName(timer, NULL);
  217. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  218. return NULL;
  219. }
  220. EvrRtxTimerGetName(timer, timer->name);
  221. return timer->name;
  222. }
  223. /// Start or restart a timer.
  224. /// \note API identical to osTimerStart
  225. static osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  226. os_timer_t *timer = osRtxTimerId(timer_id);
  227. // Check parameters
  228. if ((timer == NULL) || (timer->id != osRtxIdTimer) || (ticks == 0U)) {
  229. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  230. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  231. return osErrorParameter;
  232. }
  233. // Check object state
  234. if (timer->state == osRtxTimerInactive) {
  235. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  236. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  237. return osErrorResource;
  238. }
  239. if (timer->state == osRtxTimerRunning) {
  240. TimerRemove(timer);
  241. } else {
  242. if (osRtxInfo.timer.tick == NULL) {
  243. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  244. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  245. return osErrorResource;
  246. } else {
  247. timer->state = osRtxTimerRunning;
  248. timer->load = ticks;
  249. }
  250. }
  251. TimerInsert(timer, ticks);
  252. EvrRtxTimerStarted(timer);
  253. return osOK;
  254. }
  255. /// Stop a timer.
  256. /// \note API identical to osTimerStop
  257. static osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
  258. os_timer_t *timer = osRtxTimerId(timer_id);
  259. // Check parameters
  260. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  261. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  262. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  263. return osErrorParameter;
  264. }
  265. // Check object state
  266. if (timer->state != osRtxTimerRunning) {
  267. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  268. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  269. return osErrorResource;
  270. }
  271. timer->state = osRtxTimerStopped;
  272. TimerRemove(timer);
  273. EvrRtxTimerStopped(timer);
  274. return osOK;
  275. }
  276. /// Check if a timer is running.
  277. /// \note API identical to osTimerIsRunning
  278. static uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
  279. os_timer_t *timer = osRtxTimerId(timer_id);
  280. uint32_t is_running;
  281. // Check parameters
  282. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  283. EvrRtxTimerIsRunning(timer, 0U);
  284. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  285. return 0U;
  286. }
  287. // Check object state
  288. if (timer->state == osRtxTimerRunning) {
  289. EvrRtxTimerIsRunning(timer, 1U);
  290. is_running = 1U;
  291. } else {
  292. EvrRtxTimerIsRunning(timer, 0U);
  293. is_running = 0;
  294. }
  295. return is_running;
  296. }
  297. /// Delete a timer.
  298. /// \note API identical to osTimerDelete
  299. static osStatus_t svcRtxTimerDelete (osTimerId_t timer_id) {
  300. os_timer_t *timer = osRtxTimerId(timer_id);
  301. // Check parameters
  302. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  303. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  304. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  305. return osErrorParameter;
  306. }
  307. // Check object state
  308. if (timer->state == osRtxTimerInactive) {
  309. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  310. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  311. return osErrorResource;
  312. }
  313. if (timer->state == osRtxTimerRunning) {
  314. TimerRemove(timer);
  315. }
  316. // Mark object as inactive
  317. timer->state = osRtxTimerInactive;
  318. // Free object memory
  319. if ((timer->flags & osRtxFlagSystemObject) != 0U) {
  320. if (osRtxInfo.mpi.timer != NULL) {
  321. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.timer, timer);
  322. } else {
  323. (void)osRtxMemoryFree(osRtxInfo.mem.common, timer);
  324. }
  325. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  326. osRtxTimerMemUsage.cnt_free++;
  327. #endif
  328. }
  329. EvrRtxTimerDestroyed(timer);
  330. return osOK;
  331. }
  332. // Service Calls definitions
  333. //lint ++flb "Library Begin" [MISRA Note 11]
  334. SVC0_4(TimerNew, osTimerId_t, osTimerFunc_t, osTimerType_t, void *, const osTimerAttr_t *)
  335. SVC0_1(TimerGetName, const char *, osTimerId_t)
  336. SVC0_2(TimerStart, osStatus_t, osTimerId_t, uint32_t)
  337. SVC0_1(TimerStop, osStatus_t, osTimerId_t)
  338. SVC0_1(TimerIsRunning, uint32_t, osTimerId_t)
  339. SVC0_1(TimerDelete, osStatus_t, osTimerId_t)
  340. //lint --flb "Library End"
  341. // ==== Public API ====
  342. /// Create and Initialize a timer.
  343. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  344. osTimerId_t timer_id;
  345. EvrRtxTimerNew(func, type, argument, attr);
  346. if (IsIrqMode() || IsIrqMasked()) {
  347. EvrRtxTimerError(NULL, (int32_t)osErrorISR);
  348. timer_id = NULL;
  349. } else {
  350. timer_id = __svcTimerNew(func, type, argument, attr);
  351. }
  352. return timer_id;
  353. }
  354. /// Get name of a timer.
  355. const char *osTimerGetName (osTimerId_t timer_id) {
  356. const char *name;
  357. if (IsIrqMode() || IsIrqMasked()) {
  358. EvrRtxTimerGetName(timer_id, NULL);
  359. name = NULL;
  360. } else {
  361. name = __svcTimerGetName(timer_id);
  362. }
  363. return name;
  364. }
  365. /// Start or restart a timer.
  366. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  367. osStatus_t status;
  368. EvrRtxTimerStart(timer_id, ticks);
  369. if (IsIrqMode() || IsIrqMasked()) {
  370. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  371. status = osErrorISR;
  372. } else {
  373. status = __svcTimerStart(timer_id, ticks);
  374. }
  375. return status;
  376. }
  377. /// Stop a timer.
  378. osStatus_t osTimerStop (osTimerId_t timer_id) {
  379. osStatus_t status;
  380. EvrRtxTimerStop(timer_id);
  381. if (IsIrqMode() || IsIrqMasked()) {
  382. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  383. status = osErrorISR;
  384. } else {
  385. status = __svcTimerStop(timer_id);
  386. }
  387. return status;
  388. }
  389. /// Check if a timer is running.
  390. uint32_t osTimerIsRunning (osTimerId_t timer_id) {
  391. uint32_t is_running;
  392. if (IsIrqMode() || IsIrqMasked()) {
  393. EvrRtxTimerIsRunning(timer_id, 0U);
  394. is_running = 0U;
  395. } else {
  396. is_running = __svcTimerIsRunning(timer_id);
  397. }
  398. return is_running;
  399. }
  400. /// Delete a timer.
  401. osStatus_t osTimerDelete (osTimerId_t timer_id) {
  402. osStatus_t status;
  403. EvrRtxTimerDelete(timer_id);
  404. if (IsIrqMode() || IsIrqMasked()) {
  405. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  406. status = osErrorISR;
  407. } else {
  408. status = __svcTimerDelete(timer_id);
  409. }
  410. return status;
  411. }