rtx_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2013-2019 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 __NO_RETURN 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. osRtxInfo.timer.tick = osRtxTimerTick;
  113. for (;;) {
  114. //lint -e{934} "Taking address of near auto variable"
  115. status = osMessageQueueGet(osRtxInfo.timer.mq, &finfo, NULL, osWaitForever);
  116. if (status == osOK) {
  117. EvrRtxTimerCallback(finfo.func, finfo.arg);
  118. (finfo.func)(finfo.arg);
  119. }
  120. }
  121. }
  122. // ==== Service Calls ====
  123. /// Create and Initialize a timer.
  124. /// \note API identical to osTimerNew
  125. static osTimerId_t svcRtxTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  126. os_timer_t *timer;
  127. uint8_t flags;
  128. const char *name;
  129. // Check parameters
  130. if ((func == NULL) || ((type != osTimerOnce) && (type != osTimerPeriodic))) {
  131. EvrRtxTimerError(NULL, (int32_t)osErrorParameter);
  132. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  133. return NULL;
  134. }
  135. // Process attributes
  136. if (attr != NULL) {
  137. name = attr->name;
  138. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  139. timer = attr->cb_mem;
  140. if (timer != NULL) {
  141. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  142. if ((((uint32_t)timer & 3U) != 0U) || (attr->cb_size < sizeof(os_timer_t))) {
  143. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  144. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  145. return NULL;
  146. }
  147. } else {
  148. if (attr->cb_size != 0U) {
  149. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  150. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  151. return NULL;
  152. }
  153. }
  154. } else {
  155. name = NULL;
  156. timer = NULL;
  157. }
  158. // Allocate object memory if not provided
  159. if (timer == NULL) {
  160. if (osRtxInfo.mpi.timer != NULL) {
  161. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  162. timer = osRtxMemoryPoolAlloc(osRtxInfo.mpi.timer);
  163. } else {
  164. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  165. timer = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_timer_t), 1U);
  166. }
  167. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  168. if (timer != NULL) {
  169. uint32_t used;
  170. osRtxTimerMemUsage.cnt_alloc++;
  171. used = osRtxTimerMemUsage.cnt_alloc - osRtxTimerMemUsage.cnt_free;
  172. if (osRtxTimerMemUsage.max_used < used) {
  173. osRtxTimerMemUsage.max_used = used;
  174. }
  175. }
  176. #endif
  177. flags = osRtxFlagSystemObject;
  178. } else {
  179. flags = 0U;
  180. }
  181. if (timer != NULL) {
  182. // Initialize control block
  183. timer->id = osRtxIdTimer;
  184. timer->state = osRtxTimerStopped;
  185. timer->flags = flags;
  186. timer->type = (uint8_t)type;
  187. timer->name = name;
  188. timer->prev = NULL;
  189. timer->next = NULL;
  190. timer->tick = 0U;
  191. timer->load = 0U;
  192. timer->finfo.func = func;
  193. timer->finfo.arg = argument;
  194. EvrRtxTimerCreated(timer, timer->name);
  195. } else {
  196. EvrRtxTimerError(NULL, (int32_t)osErrorNoMemory);
  197. }
  198. return timer;
  199. }
  200. /// Get name of a timer.
  201. /// \note API identical to osTimerGetName
  202. static const char *svcRtxTimerGetName (osTimerId_t timer_id) {
  203. os_timer_t *timer = osRtxTimerId(timer_id);
  204. // Check parameters
  205. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  206. EvrRtxTimerGetName(timer, NULL);
  207. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  208. return NULL;
  209. }
  210. EvrRtxTimerGetName(timer, timer->name);
  211. return timer->name;
  212. }
  213. /// Start or restart a timer.
  214. /// \note API identical to osTimerStart
  215. static osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  216. os_timer_t *timer = osRtxTimerId(timer_id);
  217. // Check parameters
  218. if ((timer == NULL) || (timer->id != osRtxIdTimer) || (ticks == 0U)) {
  219. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  220. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  221. return osErrorParameter;
  222. }
  223. if (timer->state == osRtxTimerRunning) {
  224. TimerRemove(timer);
  225. } else {
  226. if (osRtxInfo.timer.tick == NULL) {
  227. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  228. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  229. return osErrorResource;
  230. } else {
  231. timer->state = osRtxTimerRunning;
  232. timer->load = ticks;
  233. }
  234. }
  235. TimerInsert(timer, ticks);
  236. EvrRtxTimerStarted(timer);
  237. return osOK;
  238. }
  239. /// Stop a timer.
  240. /// \note API identical to osTimerStop
  241. static osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
  242. os_timer_t *timer = osRtxTimerId(timer_id);
  243. // Check parameters
  244. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  245. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  246. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  247. return osErrorParameter;
  248. }
  249. // Check object state
  250. if (timer->state != osRtxTimerRunning) {
  251. EvrRtxTimerError(timer, (int32_t)osErrorResource);
  252. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  253. return osErrorResource;
  254. }
  255. timer->state = osRtxTimerStopped;
  256. TimerRemove(timer);
  257. EvrRtxTimerStopped(timer);
  258. return osOK;
  259. }
  260. /// Check if a timer is running.
  261. /// \note API identical to osTimerIsRunning
  262. static uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
  263. os_timer_t *timer = osRtxTimerId(timer_id);
  264. uint32_t is_running;
  265. // Check parameters
  266. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  267. EvrRtxTimerIsRunning(timer, 0U);
  268. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  269. return 0U;
  270. }
  271. if (timer->state == osRtxTimerRunning) {
  272. EvrRtxTimerIsRunning(timer, 1U);
  273. is_running = 1U;
  274. } else {
  275. EvrRtxTimerIsRunning(timer, 0U);
  276. is_running = 0;
  277. }
  278. return is_running;
  279. }
  280. /// Delete a timer.
  281. /// \note API identical to osTimerDelete
  282. static osStatus_t svcRtxTimerDelete (osTimerId_t timer_id) {
  283. os_timer_t *timer = osRtxTimerId(timer_id);
  284. // Check parameters
  285. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  286. EvrRtxTimerError(timer, (int32_t)osErrorParameter);
  287. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  288. return osErrorParameter;
  289. }
  290. if (timer->state == osRtxTimerRunning) {
  291. TimerRemove(timer);
  292. }
  293. // Mark object as inactive and invalid
  294. timer->state = osRtxTimerInactive;
  295. timer->id = osRtxIdInvalid;
  296. // Free object memory
  297. if ((timer->flags & osRtxFlagSystemObject) != 0U) {
  298. if (osRtxInfo.mpi.timer != NULL) {
  299. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.timer, timer);
  300. } else {
  301. (void)osRtxMemoryFree(osRtxInfo.mem.common, timer);
  302. }
  303. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  304. osRtxTimerMemUsage.cnt_free++;
  305. #endif
  306. }
  307. EvrRtxTimerDestroyed(timer);
  308. return osOK;
  309. }
  310. // Service Calls definitions
  311. //lint ++flb "Library Begin" [MISRA Note 11]
  312. SVC0_4(TimerNew, osTimerId_t, osTimerFunc_t, osTimerType_t, void *, const osTimerAttr_t *)
  313. SVC0_1(TimerGetName, const char *, osTimerId_t)
  314. SVC0_2(TimerStart, osStatus_t, osTimerId_t, uint32_t)
  315. SVC0_1(TimerStop, osStatus_t, osTimerId_t)
  316. SVC0_1(TimerIsRunning, uint32_t, osTimerId_t)
  317. SVC0_1(TimerDelete, osStatus_t, osTimerId_t)
  318. //lint --flb "Library End"
  319. // ==== Public API ====
  320. /// Create and Initialize a timer.
  321. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  322. osTimerId_t timer_id;
  323. EvrRtxTimerNew(func, type, argument, attr);
  324. if (IsIrqMode() || IsIrqMasked()) {
  325. EvrRtxTimerError(NULL, (int32_t)osErrorISR);
  326. timer_id = NULL;
  327. } else {
  328. timer_id = __svcTimerNew(func, type, argument, attr);
  329. }
  330. return timer_id;
  331. }
  332. /// Get name of a timer.
  333. const char *osTimerGetName (osTimerId_t timer_id) {
  334. const char *name;
  335. if (IsIrqMode() || IsIrqMasked()) {
  336. EvrRtxTimerGetName(timer_id, NULL);
  337. name = NULL;
  338. } else {
  339. name = __svcTimerGetName(timer_id);
  340. }
  341. return name;
  342. }
  343. /// Start or restart a timer.
  344. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  345. osStatus_t status;
  346. EvrRtxTimerStart(timer_id, ticks);
  347. if (IsIrqMode() || IsIrqMasked()) {
  348. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  349. status = osErrorISR;
  350. } else {
  351. status = __svcTimerStart(timer_id, ticks);
  352. }
  353. return status;
  354. }
  355. /// Stop a timer.
  356. osStatus_t osTimerStop (osTimerId_t timer_id) {
  357. osStatus_t status;
  358. EvrRtxTimerStop(timer_id);
  359. if (IsIrqMode() || IsIrqMasked()) {
  360. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  361. status = osErrorISR;
  362. } else {
  363. status = __svcTimerStop(timer_id);
  364. }
  365. return status;
  366. }
  367. /// Check if a timer is running.
  368. uint32_t osTimerIsRunning (osTimerId_t timer_id) {
  369. uint32_t is_running;
  370. if (IsIrqMode() || IsIrqMasked()) {
  371. EvrRtxTimerIsRunning(timer_id, 0U);
  372. is_running = 0U;
  373. } else {
  374. is_running = __svcTimerIsRunning(timer_id);
  375. }
  376. return is_running;
  377. }
  378. /// Delete a timer.
  379. osStatus_t osTimerDelete (osTimerId_t timer_id) {
  380. osStatus_t status;
  381. EvrRtxTimerDelete(timer_id);
  382. if (IsIrqMode() || IsIrqMasked()) {
  383. EvrRtxTimerError(timer_id, (int32_t)osErrorISR);
  384. status = osErrorISR;
  385. } else {
  386. status = __svcTimerDelete(timer_id);
  387. }
  388. return status;
  389. }