rtx_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright (c) 2013-2022 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. #ifdef RTX_OBJ_MEM_USAGE
  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_thread_t *thread_running;
  83. os_timer_t *timer;
  84. osStatus_t status;
  85. timer = osRtxInfo.timer.list;
  86. if (timer == NULL) {
  87. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  88. return;
  89. }
  90. thread_running = osRtxThreadGetRunning();
  91. timer->tick--;
  92. while ((timer != NULL) && (timer->tick == 0U)) {
  93. TimerUnlink(timer);
  94. status = osMessageQueuePut(osRtxInfo.timer.mq, &timer->finfo, 0U, 0U);
  95. if (status != osOK) {
  96. const os_thread_t *thread = osRtxThreadGetRunning();
  97. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  98. (void)osRtxKernelErrorNotify(osRtxErrorTimerQueueOverflow, timer);
  99. if (osRtxThreadGetRunning() == NULL) {
  100. if (thread_running == thread) {
  101. thread_running = NULL;
  102. }
  103. }
  104. }
  105. if (timer->type == osRtxTimerPeriodic) {
  106. TimerInsert(timer, timer->load);
  107. } else {
  108. timer->state = osRtxTimerStopped;
  109. }
  110. timer = osRtxInfo.timer.list;
  111. }
  112. osRtxThreadSetRunning(thread_running);
  113. }
  114. /// Setup Timer Thread objects.
  115. //lint -esym(714,osRtxTimerSetup) "Referenced from library configuration"
  116. //lint -esym(759,osRtxTimerSetup) "Prototype in header"
  117. //lint -esym(765,osRtxTimerSetup) "Global scope"
  118. int32_t osRtxTimerSetup (void) {
  119. int32_t ret = -1;
  120. if (osRtxMessageQueueTimerSetup() == 0) {
  121. osRtxInfo.timer.tick = osRtxTimerTick;
  122. ret = 0;
  123. }
  124. return ret;
  125. }
  126. /// Timer Thread
  127. //lint -esym(714,osRtxTimerThread) "Referenced from library configuration"
  128. //lint -esym(759,osRtxTimerThread) "Prototype in header"
  129. //lint -esym(765,osRtxTimerThread) "Global scope"
  130. __NO_RETURN void osRtxTimerThread (void *argument) {
  131. os_timer_finfo_t finfo;
  132. osStatus_t status;
  133. osMessageQueueId_t mq = (osMessageQueueId_t)argument;
  134. for (;;) {
  135. //lint -e{934} "Taking address of near auto variable"
  136. status = osMessageQueueGet(mq, &finfo, NULL, osWaitForever);
  137. if (status == osOK) {
  138. EvrRtxTimerCallback(finfo.func, finfo.arg);
  139. (finfo.func)(finfo.arg);
  140. }
  141. }
  142. }
  143. // ==== Service Calls ====
  144. /// Create and Initialize a timer.
  145. /// \note API identical to osTimerNew
  146. static osTimerId_t svcRtxTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  147. os_timer_t *timer;
  148. uint8_t flags;
  149. const char *name;
  150. // Check parameters
  151. if ((func == NULL) || ((type != osTimerOnce) && (type != osTimerPeriodic))) {
  152. EvrRtxTimerError(NULL, (int32_t)osErrorParameter);
  153. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  154. return NULL;
  155. }
  156. // Process attributes
  157. if (attr != NULL) {
  158. name = attr->name;
  159. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  160. timer = attr->cb_mem;
  161. if (timer != NULL) {
  162. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  163. if ((((uint32_t)timer & 3U) != 0U) || (attr->cb_size < sizeof(os_timer_t))) {
  164. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  165. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  166. return NULL;
  167. }
  168. } else {
  169. if (attr->cb_size != 0U) {
  170. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  171. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  172. return NULL;
  173. }
  174. }
  175. } else {
  176. name = NULL;
  177. timer = NULL;
  178. }
  179. // Allocate object memory if not provided
  180. if (timer == NULL) {
  181. if (osRtxInfo.mpi.timer != NULL) {
  182. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  183. timer = osRtxMemoryPoolAlloc(osRtxInfo.mpi.timer);
  184. } else {
  185. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  186. timer = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_timer_t), 1U);
  187. }
  188. #ifdef RTX_OBJ_MEM_USAGE
  189. if (timer != NULL) {
  190. uint32_t used;
  191. osRtxTimerMemUsage.cnt_alloc++;
  192. used = osRtxTimerMemUsage.cnt_alloc - osRtxTimerMemUsage.cnt_free;
  193. if (osRtxTimerMemUsage.max_used < used) {
  194. osRtxTimerMemUsage.max_used = used;
  195. }
  196. }
  197. #endif
  198. flags = osRtxFlagSystemObject;
  199. } else {
  200. flags = 0U;
  201. }
  202. if (timer != NULL) {
  203. // Initialize control block
  204. timer->id = osRtxIdTimer;
  205. timer->state = osRtxTimerStopped;
  206. timer->flags = flags;
  207. timer->type = (uint8_t)type;
  208. timer->name = name;
  209. timer->prev = NULL;
  210. timer->next = NULL;
  211. timer->tick = 0U;
  212. timer->load = 0U;
  213. timer->finfo.func = func;
  214. timer->finfo.arg = argument;
  215. EvrRtxTimerCreated(timer, timer->name);
  216. } else {
  217. EvrRtxTimerError(NULL, (int32_t)osErrorNoMemory);
  218. }
  219. return timer;
  220. }
  221. /// Get name of a timer.
  222. /// \note API identical to osTimerGetName
  223. static const char *svcRtxTimerGetName (osTimerId_t timer_id) {
  224. os_timer_t *timer = osRtxTimerId(timer_id);
  225. // Check parameters
  226. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  227. EvrRtxTimerGetName(timer, NULL);
  228. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  229. return NULL;
  230. }
  231. EvrRtxTimerGetName(timer, timer->name);
  232. return timer->name;
  233. }
  234. /// Start or restart a timer.
  235. /// \note API identical to osTimerStart
  236. static osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  237. os_timer_t *timer = osRtxTimerId(timer_id);
  238. // Check parameters
  239. if ((timer == NULL) || (timer->id != osRtxIdTimer) || (ticks == 0U)) {
  240. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  241. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  242. return osErrorParameter;
  243. }
  244. if (timer->state == osRtxTimerRunning) {
  245. timer->load = ticks;
  246. TimerRemove(timer);
  247. } else {
  248. if (osRtxInfo.timer.tick == NULL) {
  249. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  250. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  251. return osErrorResource;
  252. } else {
  253. timer->state = osRtxTimerRunning;
  254. timer->load = ticks;
  255. }
  256. }
  257. TimerInsert(timer, ticks);
  258. EvrRtxTimerStarted(timer);
  259. return osOK;
  260. }
  261. /// Stop a timer.
  262. /// \note API identical to osTimerStop
  263. static osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
  264. os_timer_t *timer = osRtxTimerId(timer_id);
  265. // Check parameters
  266. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  267. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  268. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  269. return osErrorParameter;
  270. }
  271. // Check object state
  272. if (timer->state != osRtxTimerRunning) {
  273. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  274. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  275. return osErrorResource;
  276. }
  277. timer->state = osRtxTimerStopped;
  278. TimerRemove(timer);
  279. EvrRtxTimerStopped(timer);
  280. return osOK;
  281. }
  282. /// Check if a timer is running.
  283. /// \note API identical to osTimerIsRunning
  284. static uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
  285. os_timer_t *timer = osRtxTimerId(timer_id);
  286. uint32_t is_running;
  287. // Check parameters
  288. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  289. EvrRtxTimerIsRunning(timer, 0U);
  290. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  291. return 0U;
  292. }
  293. if (timer->state == osRtxTimerRunning) {
  294. EvrRtxTimerIsRunning(timer, 1U);
  295. is_running = 1U;
  296. } else {
  297. EvrRtxTimerIsRunning(timer, 0U);
  298. is_running = 0;
  299. }
  300. return is_running;
  301. }
  302. /// Delete a timer.
  303. /// \note API identical to osTimerDelete
  304. static osStatus_t svcRtxTimerDelete (osTimerId_t timer_id) {
  305. os_timer_t *timer = osRtxTimerId(timer_id);
  306. // Check parameters
  307. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  308. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  309. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  310. return osErrorParameter;
  311. }
  312. if (timer->state == osRtxTimerRunning) {
  313. TimerRemove(timer);
  314. }
  315. // Mark object as inactive and invalid
  316. timer->state = osRtxTimerInactive;
  317. timer->id = osRtxIdInvalid;
  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. #ifdef RTX_OBJ_MEM_USAGE
  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 (IsException() || 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 (IsException() || 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 (IsException() || 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 (IsException() || 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 (IsException() || 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 (IsException() || IsIrqMasked()) {
  405. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  406. status = osErrorISR;
  407. } else {
  408. status = __svcTimerDelete(timer_id);
  409. }
  410. return status;
  411. }