osal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /*===========================================================================*/
  22. /* Module local definitions. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Module exported variables. */
  26. /*===========================================================================*/
  27. /**
  28. * @brief Pointer to a halt error message.
  29. * @note The message is meant to be retrieved by the debugger after the
  30. * system halt caused by an unexpected error.
  31. */
  32. const char *osal_halt_msg;
  33. /*===========================================================================*/
  34. /* Module local types. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Module local variables. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Module local functions. */
  41. /*===========================================================================*/
  42. /*===========================================================================*/
  43. /* Module exported functions. */
  44. /*===========================================================================*/
  45. /**
  46. * @brief OSAL module initialization.
  47. *
  48. * @api
  49. */
  50. void osalInit(void) {
  51. }
  52. /**
  53. * @brief System halt with error message.
  54. *
  55. * @param[in] reason the halt message pointer
  56. *
  57. * @api
  58. */
  59. #if !defined(__DOXYGEN__)
  60. __attribute__((weak, noreturn))
  61. #endif
  62. void osalSysHalt(const char *reason) {
  63. osalSysDisable();
  64. osal_halt_msg = reason;
  65. while (true) {
  66. }
  67. }
  68. /**
  69. * @brief Polled delay.
  70. * @note The real delay is always few cycles in excess of the specified
  71. * value.
  72. *
  73. * @param[in] cycles number of cycles
  74. *
  75. * @xclass
  76. */
  77. void osalSysPolledDelayX(rtcnt_t cycles) {
  78. (void)cycles;
  79. }
  80. /**
  81. * @brief System timer handler.
  82. * @details The handler is used for scheduling and Virtual Timers management.
  83. *
  84. * @iclass
  85. */
  86. void osalOsTimerHandlerI(void) {
  87. osalDbgCheckClassI();
  88. }
  89. /**
  90. * @brief Checks if a reschedule is required and performs it.
  91. * @note I-Class functions invoked from thread context must not reschedule
  92. * by themselves, an explicit reschedule using this function is
  93. * required in this scenario.
  94. * @note Not implemented in this simplified OSAL.
  95. *
  96. * @sclass
  97. */
  98. void osalOsRescheduleS(void) {
  99. }
  100. /**
  101. * @brief Current system time.
  102. * @details Returns the number of system ticks since the @p osalInit()
  103. * invocation.
  104. * @note The counter can reach its maximum and then restart from zero.
  105. * @note This function can be called from any context but its atomicity
  106. * is not guaranteed on architectures whose word size is less than
  107. * @p systime_t size.
  108. *
  109. * @return The system time in ticks.
  110. *
  111. * @xclass
  112. */
  113. systime_t osalOsGetSystemTimeX(void) {
  114. return (systime_t)0;
  115. }
  116. /**
  117. * @brief Suspends the invoking thread for the specified time.
  118. *
  119. * @param[in] time the delay in system ticks, the special values are
  120. * handled as follow:
  121. * - @a TIME_INFINITE is allowed but interpreted as a
  122. * normal time specification.
  123. * - @a TIME_IMMEDIATE this value is not allowed.
  124. * .
  125. *
  126. * @sclass
  127. */
  128. void osalThreadSleepS(sysinterval_t time) {
  129. (void)time;
  130. }
  131. /**
  132. * @brief Suspends the invoking thread for the specified time.
  133. *
  134. * @param[in] time the delay in system ticks, the special values are
  135. * handled as follow:
  136. * - @a TIME_INFINITE is allowed but interpreted as a
  137. * normal time specification.
  138. * - @a TIME_IMMEDIATE this value is not allowed.
  139. * .
  140. *
  141. * @api
  142. */
  143. void osalThreadSleep(sysinterval_t time) {
  144. (void)time;
  145. }
  146. /**
  147. * @brief Sends the current thread sleeping and sets a reference variable.
  148. * @note This function must reschedule, it can only be called from thread
  149. * context.
  150. *
  151. * @param[in] trp a pointer to a thread reference object
  152. * @return The wake up message.
  153. *
  154. * @sclass
  155. */
  156. msg_t osalThreadSuspendS(thread_reference_t *trp) {
  157. osalDbgCheck(trp != NULL);
  158. return MSG_OK;
  159. }
  160. /**
  161. * @brief Sends the current thread sleeping and sets a reference variable.
  162. * @note This function must reschedule, it can only be called from thread
  163. * context.
  164. *
  165. * @param[in] trp a pointer to a thread reference object
  166. * @param[in] timeout the timeout in system ticks, the special values are
  167. * handled as follow:
  168. * - @a TIME_INFINITE the thread enters an infinite sleep
  169. * state.
  170. * - @a TIME_IMMEDIATE the thread is not enqueued and
  171. * the function returns @p MSG_TIMEOUT as if a timeout
  172. * occurred.
  173. * .
  174. * @return The wake up message.
  175. * @retval MSG_TIMEOUT if the operation timed out.
  176. *
  177. * @sclass
  178. */
  179. msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout) {
  180. osalDbgCheck(trp != NULL);
  181. (void)timeout;
  182. return MSG_OK;
  183. }
  184. /**
  185. * @brief Wakes up a thread waiting on a thread reference object.
  186. * @note This function must not reschedule because it can be called from
  187. * ISR context.
  188. *
  189. * @param[in] trp a pointer to a thread reference object
  190. * @param[in] msg the message code
  191. *
  192. * @iclass
  193. */
  194. void osalThreadResumeI(thread_reference_t *trp, msg_t msg) {
  195. osalDbgCheck(trp != NULL);
  196. (void)msg;
  197. }
  198. /**
  199. * @brief Wakes up a thread waiting on a thread reference object.
  200. * @note This function must reschedule, it can only be called from thread
  201. * context.
  202. *
  203. * @param[in] trp a pointer to a thread reference object
  204. * @param[in] msg the message code
  205. *
  206. * @iclass
  207. */
  208. void osalThreadResumeS(thread_reference_t *trp, msg_t msg) {
  209. osalDbgCheck(trp != NULL);
  210. (void)msg;
  211. }
  212. /**
  213. * @brief Enqueues the caller thread.
  214. * @details The caller thread is enqueued and put to sleep until it is
  215. * dequeued or the specified timeouts expires.
  216. *
  217. * @param[in] tqp pointer to the threads queue object
  218. * @param[in] timeout the timeout in system ticks, the special values are
  219. * handled as follow:
  220. * - @a TIME_INFINITE the thread enters an infinite sleep
  221. * state.
  222. * - @a TIME_IMMEDIATE the thread is not enqueued and
  223. * the function returns @p MSG_TIMEOUT as if a timeout
  224. * occurred.
  225. * .
  226. * @return The message from @p osalQueueWakeupOneI() or
  227. * @p osalQueueWakeupAllI() functions.
  228. * @retval MSG_TIMEOUT if the thread has not been dequeued within the
  229. * specified timeout or if the function has been
  230. * invoked with @p TIME_IMMEDIATE as timeout
  231. * specification.
  232. *
  233. * @sclass
  234. */
  235. msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout) {
  236. osalDbgCheck(tqp != NULL);
  237. (void)timeout;
  238. return MSG_OK;
  239. }
  240. /**
  241. * @brief Dequeues and wakes up one thread from the queue, if any.
  242. *
  243. * @param[in] tqp pointer to the threads queue object
  244. * @param[in] msg the message code
  245. *
  246. * @iclass
  247. */
  248. void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg) {
  249. osalDbgCheck(tqp != NULL);
  250. (void)msg;
  251. }
  252. /**
  253. * @brief Dequeues and wakes up all threads from the queue.
  254. *
  255. * @param[in] tqp pointer to the threads queue object
  256. * @param[in] msg the message code
  257. *
  258. * @iclass
  259. */
  260. void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg) {
  261. (void)tqp;
  262. (void)msg;
  263. }
  264. /**
  265. * @brief Add flags to an event source object.
  266. *
  267. * @param[in] esp pointer to the event flags object
  268. * @param[in] flags flags to be ORed to the flags mask
  269. *
  270. * @iclass
  271. */
  272. void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags) {
  273. osalDbgCheck(esp != NULL);
  274. esp->flags |= flags;
  275. if (esp->cb != NULL) {
  276. esp->cb(esp);
  277. }
  278. }
  279. /**
  280. * @brief Add flags to an event source object.
  281. *
  282. * @param[in] esp pointer to the event flags object
  283. * @param[in] flags flags to be ORed to the flags mask
  284. *
  285. * @iclass
  286. */
  287. void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags) {
  288. osalDbgCheck(esp != NULL);
  289. osalSysLock();
  290. osalEventBroadcastFlagsI(esp, flags);
  291. osalSysUnlock();
  292. }
  293. /**
  294. * @brief Event callback setup.
  295. * @note The callback is invoked from ISR context and can
  296. * only invoke I-Class functions. The callback is meant
  297. * to wakeup the task that will handle the event by
  298. * calling @p osalEventGetAndClearFlagsI().
  299. * @note This function is not part of the OSAL API and is provided
  300. * exclusively as an example and for convenience.
  301. *
  302. * @param[in] esp pointer to the event flags object
  303. * @param[in] cb pointer to the callback function
  304. * @param[in] param parameter to be passed to the callback function
  305. *
  306. * @api
  307. */
  308. void osalEventSetCallback(event_source_t *esp,
  309. eventcallback_t cb,
  310. void *param) {
  311. osalDbgCheck(esp != NULL);
  312. esp->cb = cb;
  313. esp->param = param;
  314. }
  315. /**
  316. * @brief Locks the specified mutex.
  317. * @post The mutex is locked and inserted in the per-thread stack of owned
  318. * mutexes.
  319. *
  320. * @param[in,out] mp pointer to the @p mutex_t object
  321. *
  322. * @api
  323. */
  324. void osalMutexLock(mutex_t *mp) {
  325. osalDbgCheck(mp != NULL);
  326. *mp = 1;
  327. }
  328. /**
  329. * @brief Unlocks the specified mutex.
  330. * @note The HAL guarantees to release mutex in reverse lock order. The
  331. * mutex being unlocked is guaranteed to be the last locked mutex
  332. * by the invoking thread.
  333. * The implementation can rely on this behavior and eventually
  334. * ignore the @p mp parameter which is supplied in order to support
  335. * those OSes not supporting a stack of the owned mutexes.
  336. *
  337. * @param[in,out] mp pointer to the @p mutex_t object
  338. *
  339. * @api
  340. */
  341. void osalMutexUnlock(mutex_t *mp) {
  342. osalDbgCheck(mp != NULL);
  343. *mp = 0;
  344. }
  345. /** @} */