rtx_timer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2013-2016 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. * http://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 os_TimerInsert (os_timer_t *timer, uint32_t tick) {
  31. os_timer_t *prev, *next;
  32. prev = NULL;
  33. next = os_Info.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. os_Info.timer.list = timer;
  50. }
  51. }
  52. /// Remove Timer from the Timer List.
  53. /// \param[in] timer timer object.
  54. static void os_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. os_Info.timer.list = timer->next;
  63. }
  64. }
  65. /// Unlink Timer from the Timer List Head.
  66. /// \param[in] timer timer object.
  67. static void os_TimerUnlink (os_timer_t *timer) {
  68. if (timer->next != NULL) {
  69. timer->next->prev = timer->prev;
  70. }
  71. os_Info.timer.list = timer->next;
  72. }
  73. // ==== Library functions ====
  74. /// Timer Tick (called each SysTick).
  75. void os_TimerTick (void) {
  76. os_timer_t *timer;
  77. osStatus_t status;
  78. timer = os_Info.timer.list;
  79. if (timer == NULL) {
  80. return;
  81. }
  82. timer->tick--;
  83. while ((timer != NULL) && (timer->tick == 0U)) {
  84. os_TimerUnlink(timer);
  85. status = osMessageQueuePut((osMessageQueueId_t)os_Info.timer.mq, &timer->finfo, 0U, 0U);
  86. if (status != osOK) {
  87. os_Error(os_ErrorTimerQueueOverflow, timer);
  88. }
  89. if (timer->type == os_TimerPeriodic) {
  90. os_TimerInsert(timer, timer->load);
  91. } else {
  92. timer->state = os_TimerStopped;
  93. }
  94. timer = os_Info.timer.list;
  95. }
  96. }
  97. /// Timer Thread
  98. void *os_TimerThread (void *argument) {
  99. os_timer_finfo_t finfo;
  100. osStatus_t status;
  101. (void) argument;
  102. for (;;) {
  103. status = osMessageQueueGet((osMessageQueueId_t)os_Info.timer.mq, &finfo, NULL, osWaitForever);
  104. if (status == osOK) {
  105. (*(os_timer_func_t)finfo.fp)(finfo.arg);
  106. }
  107. }
  108. return NULL;
  109. }
  110. // ==== Service Calls ====
  111. // Service Calls definitions
  112. SVC0_4(TimerNew, osTimerId_t, os_timer_func_t, osTimerType_t, void *, const osTimerAttr_t *)
  113. SVC0_2(TimerStart, osStatus_t, osTimerId_t, uint32_t)
  114. SVC0_1(TimerStop, osStatus_t, osTimerId_t)
  115. SVC0_1(TimerIsRunning, uint32_t, osTimerId_t)
  116. SVC0_1(TimerDelete, osStatus_t, osTimerId_t)
  117. /// Create and Initialize a timer.
  118. /// \note API identical to osTimerNew
  119. osTimerId_t os_svcTimerNew (os_timer_func_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  120. os_timer_t *timer;
  121. uint8_t flags;
  122. const char *name;
  123. // Create common timer message queue if not yet active
  124. if (os_Info.timer.mq == NULL) {
  125. os_Info.timer.mq = (os_message_queue_t *)(os_svcMessageQueueNew(
  126. os_Config.timer_mq_mcnt,
  127. sizeof(os_timer_finfo_t),
  128. os_Config.timer_mq_attr));
  129. if (os_Info.timer.mq == NULL) {
  130. return (osTimerId_t)NULL;
  131. }
  132. }
  133. // Create common timer thread if not yet active
  134. if (os_Info.timer.thread == NULL) {
  135. os_Info.timer.thread = (os_thread_t *)(os_svcThreadNew(
  136. os_TimerThread,
  137. NULL,
  138. os_Config.timer_thread_attr));
  139. if (os_Info.timer.thread == NULL) {
  140. return (osTimerId_t)NULL;
  141. }
  142. }
  143. // Check parameters
  144. if ((func == NULL) || ((type != osTimerOnce) && (type != osTimerPeriodic))) {
  145. return (osTimerId_t)NULL;
  146. }
  147. // Check timer objects
  148. if ((os_Info.timer.thread == NULL) || (os_Info.timer.mq == NULL)) {
  149. return (osTimerId_t)NULL;
  150. }
  151. // Process attributes
  152. if (attr != NULL) {
  153. name = attr->name;
  154. timer = attr->cb_mem;
  155. if (timer != NULL) {
  156. if (((uint32_t)timer & 3U) || (attr->cb_size < sizeof(os_timer_t))) {
  157. return (osTimerId_t)NULL;
  158. }
  159. } else {
  160. if (attr->cb_size != 0U) {
  161. return (osTimerId_t)NULL;
  162. }
  163. }
  164. } else {
  165. name = NULL;
  166. timer = NULL;
  167. }
  168. // Allocate object memory if not provided
  169. if (timer == NULL) {
  170. if (os_Info.mpi.timer != NULL) {
  171. timer = os_MemoryPoolAlloc(os_Info.mpi.timer);
  172. } else {
  173. timer = os_MemoryAlloc(os_Info.mem.cb, sizeof(os_timer_t));
  174. }
  175. if (timer == NULL) {
  176. return (osTimerId_t)NULL;
  177. }
  178. flags = os_FlagSystemObject;
  179. } else {
  180. flags = 0U;
  181. }
  182. // Initialize control block
  183. timer->id = os_IdTimer;
  184. timer->state = os_TimerStopped;
  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.fp = (void *)func;
  193. timer->finfo.arg = argument;
  194. return (osTimerId_t)timer;
  195. }
  196. /// Start or restart a timer.
  197. /// \note API identical to osTimerStart
  198. osStatus_t os_svcTimerStart (osTimerId_t timer_id, uint32_t millisec) {
  199. os_timer_t *timer = (os_timer_t *)timer_id;
  200. // Check parameters
  201. if ((timer == NULL) ||
  202. (timer->id != os_IdTimer)) {
  203. return osErrorParameter;
  204. }
  205. if (millisec == 0U) {
  206. return osErrorParameter;
  207. }
  208. // Check object state
  209. switch (timer->state) {
  210. case os_TimerStopped:
  211. timer->state = os_TimerRunning;
  212. timer->load = millisec;
  213. break;
  214. case os_TimerRunning:
  215. os_TimerRemove(timer);
  216. break;
  217. case os_TimerInactive:
  218. default:
  219. return osErrorResource;
  220. }
  221. os_TimerInsert(timer, millisec);
  222. return osOK;
  223. }
  224. /// Stop a timer.
  225. /// \note API identical to osTimerStop
  226. osStatus_t os_svcTimerStop (osTimerId_t timer_id) {
  227. os_timer_t *timer = (os_timer_t *)timer_id;
  228. // Check parameters
  229. if ((timer == NULL) ||
  230. (timer->id != os_IdTimer)) {
  231. return osErrorParameter;
  232. }
  233. // Check object state
  234. if (timer->state != os_TimerRunning) {
  235. return osErrorResource;
  236. }
  237. timer->state = os_TimerStopped;
  238. os_TimerRemove(timer);
  239. return osOK;
  240. }
  241. /// Check if a timer is running.
  242. /// \note API identical to osTimerIsRunning
  243. uint32_t os_svcTimerIsRunning (osTimerId_t timer_id) {
  244. os_timer_t *timer = (os_timer_t *)timer_id;
  245. // Check parameters
  246. if ((timer == NULL) ||
  247. (timer->id != os_IdTimer)) {
  248. return 0U;
  249. }
  250. // Check object state
  251. if (timer->state == os_TimerRunning) {
  252. return 1U;
  253. }
  254. return 0U;
  255. }
  256. /// Delete a timer.
  257. /// \note API identical to osTimerDelete
  258. osStatus_t os_svcTimerDelete (osTimerId_t timer_id) {
  259. os_timer_t *timer = (os_timer_t *)timer_id;
  260. // Check parameters
  261. if ((timer == NULL) ||
  262. (timer->id != os_IdTimer)) {
  263. return osErrorParameter;
  264. }
  265. // Check object state
  266. switch (timer->state) {
  267. case os_TimerStopped:
  268. break;
  269. case os_TimerRunning:
  270. os_TimerRemove(timer);
  271. break;
  272. case os_TimerInactive:
  273. default:
  274. return osErrorResource;
  275. }
  276. // Mark object as inactive
  277. timer->state = os_TimerInactive;
  278. // Free object memory
  279. if (timer->flags & os_FlagSystemObject) {
  280. if (os_Info.mpi.timer != NULL) {
  281. os_MemoryPoolFree(os_Info.mpi.timer, timer);
  282. } else {
  283. os_MemoryFree(os_Info.mem.cb, timer);
  284. }
  285. }
  286. return osOK;
  287. }
  288. // ==== Public API ====
  289. /// Create and Initialize a timer.
  290. osTimerId_t osTimerNew (os_timer_func_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  291. if (__get_IPSR() != 0U) {
  292. return (osTimerId_t)NULL; // Not allowed in ISR
  293. }
  294. if ((os_KernelGetState() == os_KernelReady) && ((__get_CONTROL() & 1U) == 0U)) {
  295. // Kernel Ready (not running) and in Priviledged mode
  296. return os_svcTimerNew(func, type, argument, attr);
  297. } else {
  298. return __svcTimerNew(func, type, argument, attr);
  299. }
  300. }
  301. /// Start or restart a timer.
  302. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t millisec) {
  303. if (__get_IPSR() != 0U) {
  304. return osErrorISR; // Not allowed in ISR
  305. }
  306. return __svcTimerStart(timer_id, millisec);
  307. }
  308. /// Stop a timer.
  309. osStatus_t osTimerStop (osTimerId_t timer_id) {
  310. if (__get_IPSR() != 0U) {
  311. return osErrorISR; // Not allowed in ISR
  312. }
  313. return __svcTimerStop(timer_id);
  314. }
  315. /// Check if a timer is running.
  316. uint32_t osTimerIsRunning (osTimerId_t timer_id) {
  317. if (__get_IPSR() != 0U) {
  318. return 0U; // Not allowed in ISR
  319. }
  320. return __svcTimerIsRunning(timer_id);
  321. }
  322. /// Delete a timer.
  323. osStatus_t osTimerDelete (osTimerId_t timer_id) {
  324. if (__get_IPSR() != 0U) {
  325. return osErrorISR; // Not allowed in ISR
  326. }
  327. return __svcTimerDelete(timer_id);
  328. }