osal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file osal.c
  15. * @brief OSAL module code.
  16. *
  17. * @addtogroup OSAL
  18. * @{
  19. */
  20. #include "osal.h"
  21. #include "osal_vt.h"
  22. /*===========================================================================*/
  23. /* Module local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Module exported variables. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Pointer to a halt error message.
  30. * @note The message is meant to be retrieved by the debugger after the
  31. * system halt caused by an unexpected error.
  32. */
  33. const char *osal_halt_msg;
  34. /*===========================================================================*/
  35. /* Module local types. */
  36. /*===========================================================================*/
  37. /*===========================================================================*/
  38. /* Module local variables. */
  39. /*===========================================================================*/
  40. /*===========================================================================*/
  41. /* Module local functions. */
  42. /*===========================================================================*/
  43. static void callback_timeout(void *p) {
  44. osalSysLockFromISR();
  45. osalThreadResumeI((thread_reference_t *)p, MSG_TIMEOUT);
  46. osalSysUnlockFromISR();
  47. }
  48. /*===========================================================================*/
  49. /* Module exported functions. */
  50. /*===========================================================================*/
  51. /**
  52. * @brief OSAL module initialization.
  53. *
  54. * @api
  55. */
  56. void osalInit(void) {
  57. vtInit();
  58. OSAL_INIT_HOOK();
  59. }
  60. /**
  61. * @brief System halt with error message.
  62. *
  63. * @param[in] reason the halt message pointer
  64. *
  65. * @api
  66. */
  67. #if !defined(__DOXYGEN__)
  68. __attribute__((weak, noreturn))
  69. #endif
  70. void osalSysHalt(const char *reason) {
  71. osalSysDisable();
  72. osal_halt_msg = reason;
  73. while (true) {
  74. }
  75. }
  76. /**
  77. * @brief Polled delay.
  78. * @note The real delay is always few cycles in excess of the specified
  79. * value.
  80. *
  81. * @param[in] cycles number of cycles
  82. *
  83. * @xclass
  84. */
  85. void osalSysPolledDelayX(rtcnt_t cycles) {
  86. (void)cycles;
  87. }
  88. /**
  89. * @brief System timer handler.
  90. * @details The handler is used for scheduling and Virtual Timers management.
  91. *
  92. * @iclass
  93. */
  94. void osalOsTimerHandlerI(void) {
  95. osalDbgCheckClassI();
  96. vtDoTickI();
  97. }
  98. /**
  99. * @brief Checks if a reschedule is required and performs it.
  100. * @note I-Class functions invoked from thread context must not reschedule
  101. * by themselves, an explicit reschedule using this function is
  102. * required in this scenario.
  103. * @note Not implemented in this simplified OSAL.
  104. *
  105. * @sclass
  106. */
  107. void osalOsRescheduleS(void) {
  108. }
  109. /**
  110. * @brief Current system time.
  111. * @details Returns the number of system ticks since the @p osalInit()
  112. * invocation.
  113. * @note The counter can reach its maximum and then restart from zero.
  114. * @note This function can be called from any context but its atomicity
  115. * is not guaranteed on architectures whose word size is less than
  116. * @p systime_t size.
  117. *
  118. * @return The system time in ticks.
  119. *
  120. * @xclass
  121. */
  122. systime_t osalOsGetSystemTimeX(void) {
  123. return vtlist.vt_systime;
  124. }
  125. /**
  126. * @brief Suspends the invoking thread for the specified time.
  127. *
  128. * @param[in] time the delay in system ticks, the special values are
  129. * handled as follow:
  130. * - @a TIME_INFINITE is allowed but interpreted as a
  131. * normal time specification.
  132. * - @a TIME_IMMEDIATE this value is not allowed.
  133. * .
  134. *
  135. * @sclass
  136. */
  137. void osalThreadSleepS(systime_t time) {
  138. virtual_timer_t vt;
  139. thread_reference_t tr;
  140. tr = NULL;
  141. vtSetI(&vt, time, callback_timeout, (void *)&tr);
  142. osalThreadSuspendS(&tr);
  143. }
  144. /**
  145. * @brief Suspends the invoking thread for the specified time.
  146. *
  147. * @param[in] time the delay in system ticks, the special values are
  148. * handled as follow:
  149. * - @a TIME_INFINITE is allowed but interpreted as a
  150. * normal time specification.
  151. * - @a TIME_IMMEDIATE this value is not allowed.
  152. * .
  153. *
  154. * @api
  155. */
  156. void osalThreadSleep(systime_t time) {
  157. osalSysLock();
  158. osalThreadSleepS(time);
  159. osalSysUnlock();
  160. }
  161. /**
  162. * @brief Sends the current thread sleeping and sets a reference variable.
  163. * @note This function must reschedule, it can only be called from thread
  164. * context.
  165. *
  166. * @param[in] trp a pointer to a thread reference object
  167. * @return The wake up message.
  168. *
  169. * @sclass
  170. */
  171. msg_t osalThreadSuspendS(thread_reference_t *trp) {
  172. thread_t self = {MSG_WAIT};
  173. osalDbgCheck(trp != NULL);
  174. *trp = &self;
  175. while (self.message == MSG_WAIT) {
  176. osalSysUnlock();
  177. /* A state-changing interrupt could occur here and cause the loop to
  178. terminate, an hook macro is executed while waiting.*/
  179. OSAL_IDLE_HOOK();
  180. osalSysLock();
  181. }
  182. return self.message;
  183. }
  184. /**
  185. * @brief Sends the current thread sleeping and sets a reference variable.
  186. * @note This function must reschedule, it can only be called from thread
  187. * context.
  188. *
  189. * @param[in] trp a pointer to a thread reference object
  190. * @param[in] timeout the timeout in system ticks, the special values are
  191. * handled as follow:
  192. * - @a TIME_INFINITE the thread enters an infinite sleep
  193. * state.
  194. * - @a TIME_IMMEDIATE the thread is not enqueued and
  195. * the function returns @p MSG_TIMEOUT as if a timeout
  196. * occurred.
  197. * .
  198. * @return The wake up message.
  199. * @retval MSG_TIMEOUT if the operation timed out.
  200. *
  201. * @sclass
  202. */
  203. msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, systime_t timeout) {
  204. msg_t msg;
  205. virtual_timer_t vt;
  206. osalDbgCheck(trp != NULL);
  207. if (TIME_INFINITE == timeout)
  208. return osalThreadSuspendS(trp);
  209. vtSetI(&vt, timeout, callback_timeout, (void *)trp);
  210. msg = osalThreadSuspendS(trp);
  211. if (vtIsArmedI(&vt))
  212. vtResetI(&vt);
  213. return msg;
  214. }
  215. /**
  216. * @brief Wakes up a thread waiting on a thread reference object.
  217. * @note This function must not reschedule because it can be called from
  218. * ISR context.
  219. *
  220. * @param[in] trp a pointer to a thread reference object
  221. * @param[in] msg the message code
  222. *
  223. * @iclass
  224. */
  225. void osalThreadResumeI(thread_reference_t *trp, msg_t msg) {
  226. osalDbgCheck(trp != NULL);
  227. if (*trp != NULL) {
  228. (*trp)->message = msg;
  229. *trp = NULL;
  230. }
  231. }
  232. /**
  233. * @brief Wakes up a thread waiting on a thread reference object.
  234. * @note This function must reschedule, it can only be called from thread
  235. * context.
  236. *
  237. * @param[in] trp a pointer to a thread reference object
  238. * @param[in] msg the message code
  239. *
  240. * @iclass
  241. */
  242. void osalThreadResumeS(thread_reference_t *trp, msg_t msg) {
  243. osalDbgCheck(trp != NULL);
  244. if (*trp != NULL) {
  245. (*trp)->message = msg;
  246. *trp = NULL;
  247. }
  248. }
  249. /**
  250. * @brief Enqueues the caller thread.
  251. * @details The caller thread is enqueued and put to sleep until it is
  252. * dequeued or the specified timeouts expires.
  253. *
  254. * @param[in] tqp pointer to the threads queue object
  255. * @param[in] timeout the timeout in system ticks, the special values are
  256. * handled as follow:
  257. * - @a TIME_INFINITE the thread enters an infinite sleep
  258. * state.
  259. * - @a TIME_IMMEDIATE the thread is not enqueued and
  260. * the function returns @p MSG_TIMEOUT as if a timeout
  261. * occurred.
  262. * .
  263. * @return The message from @p osalQueueWakeupOneI() or
  264. * @p osalQueueWakeupAllI() functions.
  265. * @retval MSG_TIMEOUT if the thread has not been dequeued within the
  266. * specified timeout or if the function has been
  267. * invoked with @p TIME_IMMEDIATE as timeout
  268. * specification.
  269. *
  270. * @sclass
  271. */
  272. msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, systime_t timeout) {
  273. msg_t msg;
  274. virtual_timer_t vt;
  275. osalDbgCheck(tqp != NULL);
  276. if (TIME_IMMEDIATE == timeout)
  277. return MSG_TIMEOUT;
  278. tqp->tr = NULL;
  279. if (TIME_INFINITE == timeout)
  280. return osalThreadSuspendS(&tqp->tr);
  281. vtSetI(&vt, timeout, callback_timeout, (void *)&tqp->tr);
  282. msg = osalThreadSuspendS(&tqp->tr);
  283. if (vtIsArmedI(&vt))
  284. vtResetI(&vt);
  285. return msg;
  286. }
  287. /**
  288. * @brief Dequeues and wakes up one thread from the queue, if any.
  289. *
  290. * @param[in] tqp pointer to the threads queue object
  291. * @param[in] msg the message code
  292. *
  293. * @iclass
  294. */
  295. void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg) {
  296. osalDbgCheck(tqp != NULL);
  297. osalThreadResumeI(&tqp->tr, msg);
  298. }
  299. /**
  300. * @brief Dequeues and wakes up all threads from the queue.
  301. *
  302. * @param[in] tqp pointer to the threads queue object
  303. * @param[in] msg the message code
  304. *
  305. * @iclass
  306. */
  307. void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg) {
  308. osalDbgCheck(tqp != NULL);
  309. osalThreadResumeI(&tqp->tr, msg);
  310. }
  311. /**
  312. * @brief Add flags to an event source object.
  313. *
  314. * @param[in] esp pointer to the event flags object
  315. * @param[in] flags flags to be ORed to the flags mask
  316. *
  317. * @iclass
  318. */
  319. void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags) {
  320. osalDbgCheck(esp != NULL);
  321. esp->flags |= flags;
  322. if (esp->cb != NULL) {
  323. esp->cb(esp);
  324. }
  325. }
  326. /**
  327. * @brief Add flags to an event source object.
  328. *
  329. * @param[in] esp pointer to the event flags object
  330. * @param[in] flags flags to be ORed to the flags mask
  331. *
  332. * @iclass
  333. */
  334. void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags) {
  335. osalDbgCheck(esp != NULL);
  336. osalSysLock();
  337. osalEventBroadcastFlagsI(esp, flags);
  338. osalSysUnlock();
  339. }
  340. /**
  341. * @brief Event callback setup.
  342. * @note The callback is invoked from ISR context and can
  343. * only invoke I-Class functions. The callback is meant
  344. * to wakeup the task that will handle the event by
  345. * calling @p osalEventGetAndClearFlagsI().
  346. * @note This function is not part of the OSAL API and is provided
  347. * exclusively as an example and for convenience.
  348. *
  349. * @param[in] esp pointer to the event flags object
  350. * @param[in] cb pointer to the callback function
  351. * @param[in] param parameter to be passed to the callback function
  352. *
  353. * @api
  354. */
  355. void osalEventSetCallback(event_source_t *esp,
  356. eventcallback_t cb,
  357. void *param) {
  358. osalDbgCheck(esp != NULL);
  359. esp->cb = cb;
  360. esp->param = param;
  361. }
  362. /**
  363. * @brief Locks the specified mutex.
  364. * @post The mutex is locked and inserted in the per-thread stack of owned
  365. * mutexes.
  366. *
  367. * @param[in,out] mp pointer to the @p mutex_t object
  368. *
  369. * @api
  370. */
  371. void osalMutexLock(mutex_t *mp) {
  372. osalDbgCheck(mp != NULL);
  373. *mp = 1;
  374. }
  375. /**
  376. * @brief Unlocks the specified mutex.
  377. * @note The HAL guarantees to release mutex in reverse lock order. The
  378. * mutex being unlocked is guaranteed to be the last locked mutex
  379. * by the invoking thread.
  380. * The implementation can rely on this behavior and eventually
  381. * ignore the @p mp parameter which is supplied in order to support
  382. * those OSes not supporting a stack of the owned mutexes.
  383. *
  384. * @param[in,out] mp pointer to the @p mutex_t object
  385. *
  386. * @api
  387. */
  388. void osalMutexUnlock(mutex_t *mp) {
  389. osalDbgCheck(mp != NULL);
  390. *mp = 0;
  391. }
  392. /** @} */