rtx_timer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (c) 2013-2017 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. // ==== Helper functions ====
  27. /// Insert Timer into the Timer List sorted by Time.
  28. /// \param[in] timer timer object.
  29. /// \param[in] tick timer tick.
  30. static void TimerInsert (os_timer_t *timer, uint32_t tick) {
  31. os_timer_t *prev, *next;
  32. prev = NULL;
  33. next = osRtxInfo.timer.list;
  34. while ((next != NULL) && (next->tick <= tick)) {
  35. tick -= next->tick;
  36. prev = next;
  37. next = next->next;
  38. }
  39. timer->tick = tick;
  40. timer->prev = prev;
  41. timer->next = next;
  42. if (next != NULL) {
  43. next->tick -= timer->tick;
  44. next->prev = timer;
  45. }
  46. if (prev != NULL) {
  47. prev->next = timer;
  48. } else {
  49. osRtxInfo.timer.list = timer;
  50. }
  51. }
  52. /// Remove Timer from the Timer List.
  53. /// \param[in] timer timer object.
  54. static void TimerRemove (os_timer_t *timer) {
  55. if (timer->next != NULL) {
  56. timer->next->tick += timer->tick;
  57. timer->next->prev = timer->prev;
  58. }
  59. if (timer->prev != NULL) {
  60. timer->prev->next = timer->next;
  61. } else {
  62. osRtxInfo.timer.list = timer->next;
  63. }
  64. }
  65. /// Unlink Timer from the Timer List Head.
  66. /// \param[in] timer timer object.
  67. static void TimerUnlink (os_timer_t *timer) {
  68. if (timer->next != NULL) {
  69. timer->next->prev = timer->prev;
  70. }
  71. osRtxInfo.timer.list = timer->next;
  72. }
  73. // ==== Library functions ====
  74. /// Timer Tick (called each SysTick).
  75. void osRtxTimerTick (void) {
  76. os_timer_t *timer;
  77. osStatus_t status;
  78. timer = osRtxInfo.timer.list;
  79. if (timer == NULL) {
  80. return;
  81. }
  82. timer->tick--;
  83. while ((timer != NULL) && (timer->tick == 0U)) {
  84. TimerUnlink(timer);
  85. status = osMessageQueuePut(osRtxInfo.timer.mq, &timer->finfo, 0U, 0U);
  86. if (status != osOK) {
  87. osRtxErrorNotify(osRtxErrorTimerQueueOverflow, timer);
  88. }
  89. if (timer->type == osRtxTimerPeriodic) {
  90. TimerInsert(timer, timer->load);
  91. } else {
  92. timer->state = osRtxTimerStopped;
  93. }
  94. timer = osRtxInfo.timer.list;
  95. }
  96. }
  97. /// Timer Thread
  98. __WEAK void osRtxTimerThread (void *argument) {
  99. os_timer_finfo_t finfo;
  100. osStatus_t status;
  101. (void) argument;
  102. osRtxInfo.timer.mq = osMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr);
  103. if (osRtxInfo.timer.mq == NULL) {
  104. return;
  105. }
  106. osRtxInfo.timer.tick = osRtxTimerTick;
  107. for (;;) {
  108. status = osMessageQueueGet(osRtxInfo.timer.mq, &finfo, NULL, osWaitForever);
  109. if (status == osOK) {
  110. EvrRtxTimerCallback(*(osTimerFunc_t)finfo.fp, finfo.arg);
  111. (*(osTimerFunc_t)finfo.fp)(finfo.arg);
  112. }
  113. }
  114. }
  115. // ==== Service Calls ====
  116. // Service Calls definitions
  117. SVC0_4M(TimerNew, osTimerId_t, osTimerFunc_t, osTimerType_t, void *, const osTimerAttr_t *)
  118. SVC0_1 (TimerGetName, const char *, osTimerId_t)
  119. SVC0_2 (TimerStart, osStatus_t, osTimerId_t, uint32_t)
  120. SVC0_1 (TimerStop, osStatus_t, osTimerId_t)
  121. SVC0_1 (TimerIsRunning, uint32_t, osTimerId_t)
  122. SVC0_1 (TimerDelete, osStatus_t, osTimerId_t)
  123. /// Create and Initialize a timer.
  124. /// \note API identical to osTimerNew
  125. 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, osErrorParameter);
  132. return NULL;
  133. }
  134. // Process attributes
  135. if (attr != NULL) {
  136. name = attr->name;
  137. timer = attr->cb_mem;
  138. if (timer != NULL) {
  139. if (((uint32_t)timer & 3U) || (attr->cb_size < sizeof(os_timer_t))) {
  140. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  141. return NULL;
  142. }
  143. } else {
  144. if (attr->cb_size != 0U) {
  145. EvrRtxTimerError(NULL, osRtxErrorInvalidControlBlock);
  146. return NULL;
  147. }
  148. }
  149. } else {
  150. name = NULL;
  151. timer = NULL;
  152. }
  153. // Allocate object memory if not provided
  154. if (timer == NULL) {
  155. if (osRtxInfo.mpi.timer != NULL) {
  156. timer = osRtxMemoryPoolAlloc(osRtxInfo.mpi.timer);
  157. } else {
  158. timer = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_timer_t), 1U);
  159. }
  160. if (timer == NULL) {
  161. EvrRtxTimerError(NULL, osErrorNoMemory);
  162. return NULL;
  163. }
  164. flags = osRtxFlagSystemObject;
  165. } else {
  166. flags = 0U;
  167. }
  168. // Initialize control block
  169. timer->id = osRtxIdTimer;
  170. timer->state = osRtxTimerStopped;
  171. timer->flags = flags;
  172. timer->type = (uint8_t)type;
  173. timer->name = name;
  174. timer->prev = NULL;
  175. timer->next = NULL;
  176. timer->tick = 0U;
  177. timer->load = 0U;
  178. timer->finfo.fp = (void *)func;
  179. timer->finfo.arg = argument;
  180. EvrRtxTimerCreated(timer);
  181. return timer;
  182. }
  183. /// Get name of a timer.
  184. /// \note API identical to osTimerGetName
  185. const char *svcRtxTimerGetName (osTimerId_t timer_id) {
  186. os_timer_t *timer = (os_timer_t *)timer_id;
  187. // Check parameters
  188. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  189. EvrRtxTimerGetName(timer, NULL);
  190. return NULL;
  191. }
  192. // Check object state
  193. if (timer->state == osRtxObjectInactive) {
  194. EvrRtxTimerGetName(timer, NULL);
  195. return NULL;
  196. }
  197. EvrRtxTimerGetName(timer, timer->name);
  198. return timer->name;
  199. }
  200. /// Start or restart a timer.
  201. /// \note API identical to osTimerStart
  202. osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  203. os_timer_t *timer = (os_timer_t *)timer_id;
  204. // Check parameters
  205. if ((timer == NULL) || (timer->id != osRtxIdTimer) || (ticks == 0U)) {
  206. EvrRtxTimerError(timer, osErrorParameter);
  207. return osErrorParameter;
  208. }
  209. // Check object state
  210. switch (timer->state) {
  211. case osRtxTimerStopped:
  212. if (osRtxInfo.timer.tick == NULL) {
  213. EvrRtxTimerError(timer, osErrorResource);
  214. return osErrorResource;
  215. }
  216. timer->state = osRtxTimerRunning;
  217. timer->load = ticks;
  218. break;
  219. case osRtxTimerRunning:
  220. TimerRemove(timer);
  221. break;
  222. case osRtxTimerInactive:
  223. default:
  224. EvrRtxTimerError(timer, osErrorResource);
  225. return osErrorResource;
  226. }
  227. TimerInsert(timer, ticks);
  228. EvrRtxTimerStarted(timer);
  229. return osOK;
  230. }
  231. /// Stop a timer.
  232. /// \note API identical to osTimerStop
  233. osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
  234. os_timer_t *timer = (os_timer_t *)timer_id;
  235. // Check parameters
  236. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  237. EvrRtxTimerError(timer, osErrorParameter);
  238. return osErrorParameter;
  239. }
  240. // Check object state
  241. if (timer->state != osRtxTimerRunning) {
  242. EvrRtxTimerError(timer, osErrorResource);
  243. return osErrorResource;
  244. }
  245. timer->state = osRtxTimerStopped;
  246. TimerRemove(timer);
  247. EvrRtxTimerStopped(timer);
  248. return osOK;
  249. }
  250. /// Check if a timer is running.
  251. /// \note API identical to osTimerIsRunning
  252. uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
  253. os_timer_t *timer = (os_timer_t *)timer_id;
  254. // Check parameters
  255. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  256. EvrRtxTimerIsRunning(timer, 0U);
  257. return 0U;
  258. }
  259. // Check object state
  260. if (timer->state == osRtxTimerRunning) {
  261. EvrRtxTimerIsRunning(timer, 1U);
  262. return 1U;
  263. }
  264. EvrRtxTimerIsRunning(timer, 0U);
  265. return 0U;
  266. }
  267. /// Delete a timer.
  268. /// \note API identical to osTimerDelete
  269. osStatus_t svcRtxTimerDelete (osTimerId_t timer_id) {
  270. os_timer_t *timer = (os_timer_t *)timer_id;
  271. // Check parameters
  272. if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
  273. EvrRtxTimerError(timer, osErrorParameter);
  274. return osErrorParameter;
  275. }
  276. // Check object state
  277. switch (timer->state) {
  278. case osRtxTimerStopped:
  279. break;
  280. case osRtxTimerRunning:
  281. TimerRemove(timer);
  282. break;
  283. case osRtxTimerInactive:
  284. default:
  285. EvrRtxTimerError(timer, osErrorResource);
  286. return osErrorResource;
  287. }
  288. // Mark object as inactive
  289. timer->state = osRtxTimerInactive;
  290. // Free object memory
  291. if (timer->flags & osRtxFlagSystemObject) {
  292. if (osRtxInfo.mpi.timer != NULL) {
  293. osRtxMemoryPoolFree(osRtxInfo.mpi.timer, timer);
  294. } else {
  295. osRtxMemoryFree(osRtxInfo.mem.common, timer);
  296. }
  297. }
  298. EvrRtxTimerDestroyed(timer);
  299. return osOK;
  300. }
  301. // ==== Public API ====
  302. /// Create and Initialize a timer.
  303. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  304. EvrRtxTimerNew(func, type, argument, attr);
  305. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  306. EvrRtxTimerError(NULL, osErrorISR);
  307. return NULL;
  308. }
  309. return __svcTimerNew(func, type, argument, attr);
  310. }
  311. /// Get name of a timer.
  312. const char *osTimerGetName (osTimerId_t timer_id) {
  313. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  314. EvrRtxTimerGetName(timer_id, NULL);
  315. return NULL;
  316. }
  317. return __svcTimerGetName(timer_id);
  318. }
  319. /// Start or restart a timer.
  320. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  321. EvrRtxTimerStart(timer_id, ticks);
  322. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  323. EvrRtxTimerError(timer_id, osErrorISR);
  324. return osErrorISR;
  325. }
  326. return __svcTimerStart(timer_id, ticks);
  327. }
  328. /// Stop a timer.
  329. osStatus_t osTimerStop (osTimerId_t timer_id) {
  330. EvrRtxTimerStop(timer_id);
  331. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  332. EvrRtxTimerError(timer_id, osErrorISR);
  333. return osErrorISR;
  334. }
  335. return __svcTimerStop(timer_id);
  336. }
  337. /// Check if a timer is running.
  338. uint32_t osTimerIsRunning (osTimerId_t timer_id) {
  339. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  340. EvrRtxTimerIsRunning(timer_id, 0U);
  341. return 0U;
  342. }
  343. return __svcTimerIsRunning(timer_id);
  344. }
  345. /// Delete a timer.
  346. osStatus_t osTimerDelete (osTimerId_t timer_id) {
  347. EvrRtxTimerDelete(timer_id);
  348. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  349. EvrRtxTimerError(timer_id, osErrorISR);
  350. return osErrorISR;
  351. }
  352. return __svcTimerDelete(timer_id);
  353. }