osal.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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.h
  15. * @brief OSAL module header.
  16. *
  17. * @addtogroup OSAL
  18. * @{
  19. */
  20. #ifndef OSAL_H
  21. #define OSAL_H
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include "ch.h"
  26. #if defined(__SPC5_HAL__)
  27. #include "platform.h"
  28. #endif
  29. /*===========================================================================*/
  30. /* Module constants. */
  31. /*===========================================================================*/
  32. /**
  33. * @name Common constants
  34. * @{
  35. */
  36. #if !defined(FALSE) || defined(__DOXYGEN__)
  37. #define FALSE 0
  38. #endif
  39. #if !defined(TRUE) || defined(__DOXYGEN__)
  40. #define TRUE 1
  41. #endif
  42. #define OSAL_SUCCESS false
  43. #define OSAL_FAILED true
  44. /** @} */
  45. #if 0
  46. /**
  47. * @name Messages
  48. * @{
  49. */
  50. #define MSG_OK (msg_t)0
  51. #define MSG_TIMEOUT (msg_t)-1
  52. #define MSG_RESET (msg_t)-2
  53. /** @} */
  54. #endif
  55. #if 0
  56. /**
  57. * @name Special time constants
  58. * @{
  59. */
  60. #define TIME_IMMEDIATE ((sysinterval_t)0)
  61. #define TIME_INFINITE ((sysinterval_t)-1)
  62. /** @} */
  63. #endif
  64. /**
  65. * @name Systick modes.
  66. * @{
  67. */
  68. #define OSAL_ST_MODE_NONE 0
  69. #define OSAL_ST_MODE_PERIODIC 1
  70. #define OSAL_ST_MODE_FREERUNNING 2
  71. /** @} */
  72. /**
  73. * @name Systick parameters.
  74. * @{
  75. */
  76. /**
  77. * @brief Size in bits of the @p systick_t type.
  78. */
  79. #define OSAL_ST_RESOLUTION CH_CFG_ST_RESOLUTION
  80. /**
  81. * @brief Required systick frequency or resolution.
  82. */
  83. #define OSAL_ST_FREQUENCY CH_CFG_ST_FREQUENCY
  84. /**
  85. * @brief Systick mode required by the underlying OS.
  86. */
  87. #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__)
  88. #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
  89. #else
  90. #define OSAL_ST_MODE OSAL_ST_MODE_FREERUNNING
  91. #endif
  92. /** @} */
  93. /*===========================================================================*/
  94. /* Module pre-compile time settings. */
  95. /*===========================================================================*/
  96. /*===========================================================================*/
  97. /* Derived constants and error checks. */
  98. /*===========================================================================*/
  99. #if CH_CFG_USE_SEMAPHORES == FALSE
  100. #error "OSAL requires CH_CFG_USE_SEMAPHORES=TRUE"
  101. #endif
  102. #if CH_CFG_USE_EVENTS == FALSE
  103. #error "OSAL requires CH_CFG_USE_EVENTS=TRUE"
  104. #endif
  105. #if !(OSAL_ST_MODE == OSAL_ST_MODE_NONE) && \
  106. !(OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) && \
  107. !(OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING)
  108. #error "invalid OSAL_ST_MODE setting in osal.h"
  109. #endif
  110. #if (OSAL_ST_RESOLUTION != 16) && (OSAL_ST_RESOLUTION != 32)
  111. #error "invalid OSAL_ST_RESOLUTION, must be 16 or 32"
  112. #endif
  113. /*===========================================================================*/
  114. /* Module data structures and types. */
  115. /*===========================================================================*/
  116. #if 0
  117. /**
  118. * @brief Type of a system status word.
  119. */
  120. typedef uint32_t syssts_t;
  121. #endif
  122. #if 0
  123. /**
  124. * @brief Type of a message.
  125. */
  126. typedef int32_t msg_t;
  127. #endif
  128. #if 0
  129. /**
  130. * @brief Type of system time counter.
  131. */
  132. typedef uint32_t systime_t;
  133. #endif
  134. #if 0
  135. /**
  136. * @brief Type of system time interval.
  137. */
  138. typedef uint32_t sysinterval_t;
  139. #endif
  140. #if 0
  141. /**
  142. * @brief Type of realtime counter.
  143. */
  144. typedef uint32_t rtcnt_t;
  145. #endif
  146. #if 0
  147. /**
  148. * @brief Type of a thread reference.
  149. */
  150. typedef thread_t * thread_reference_t;
  151. #endif
  152. #if 0
  153. /**
  154. * @brief Type of an event flags mask.
  155. */
  156. typedef uint32_t eventflags_t;
  157. #endif
  158. /**
  159. * @brief Type of an event flags object.
  160. * @note The content of this structure is not part of the API and should
  161. * not be relied upon. Implementers may define this structure in
  162. * an entirely different way.
  163. * @note Retrieval and clearing of the flags are not defined in this
  164. * API and are implementation-dependent.
  165. */
  166. typedef struct event_source event_source_t;
  167. /**
  168. * @brief Type of an event source callback.
  169. * @note This type is not part of the OSAL API and is provided
  170. * exclusively as an example and for convenience.
  171. */
  172. typedef void (*eventcallback_t)(event_source_t *esp);
  173. /**
  174. * @brief Events source object.
  175. * @note The content of this structure is not part of the API and should
  176. * not be relied upon. Implementers may define this structure in
  177. * an entirely different way.
  178. * @note Retrieval and clearing of the flags are not defined in this
  179. * API and are implementation-dependent.
  180. */
  181. struct event_source {
  182. volatile eventflags_t flags; /**< @brief Stored event flags. */
  183. eventcallback_t cb; /**< @brief Event source callback. */
  184. void *param; /**< @brief User defined field. */
  185. };
  186. /**
  187. * @brief Type of a mutex.
  188. * @note If the OS does not support mutexes or there is no OS then them
  189. * mechanism can be simulated.
  190. */
  191. typedef semaphore_t mutex_t;
  192. #if 0
  193. /**
  194. * @brief Type of a thread queue.
  195. * @details A thread queue is a queue of sleeping threads, queued threads
  196. * can be dequeued one at time or all together.
  197. * @note In this implementation it is implemented as a single reference
  198. * because there are no real threads.
  199. */
  200. typedef struct {
  201. thread_reference_t tr;
  202. } threads_queue_t;
  203. #endif
  204. /*===========================================================================*/
  205. /* Module macros. */
  206. /*===========================================================================*/
  207. /**
  208. * @name Debug related macros
  209. * @{
  210. */
  211. /**
  212. * @brief Condition assertion.
  213. * @details If the condition check fails then the OSAL panics with a
  214. * message and halts.
  215. * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS
  216. * switch is enabled.
  217. * @note The remark string is not currently used except for putting a
  218. * comment in the code about the assertion.
  219. *
  220. * @param[in] c the condition to be verified to be true
  221. * @param[in] remark a remark string
  222. *
  223. * @api
  224. */
  225. #define osalDbgAssert(c, remark) chDbgAssert(c, remark)
  226. /**
  227. * @brief Function parameters check.
  228. * @details If the condition check fails then the OSAL panics and halts.
  229. * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
  230. * is enabled.
  231. *
  232. * @param[in] c the condition to be verified to be true
  233. *
  234. * @api
  235. */
  236. #define osalDbgCheck(c) chDbgCheck(c)
  237. /**
  238. * @brief I-Class state check.
  239. */
  240. #define osalDbgCheckClassI() chDbgCheckClassI()
  241. /**
  242. * @brief S-Class state check.
  243. */
  244. #define osalDbgCheckClassS() chDbgCheckClassS()
  245. /** @} */
  246. /**
  247. * @name IRQ service routines wrappers
  248. * @{
  249. */
  250. /**
  251. * @brief Priority level verification macro.
  252. */
  253. #define OSAL_IRQ_IS_VALID_PRIORITY(n) CH_IRQ_IS_VALID_KERNEL_PRIORITY(n)
  254. /**
  255. * @brief IRQ prologue code.
  256. * @details This macro must be inserted at the start of all IRQ handlers.
  257. */
  258. #define OSAL_IRQ_PROLOGUE() CH_IRQ_PROLOGUE()
  259. /**
  260. * @brief IRQ epilogue code.
  261. * @details This macro must be inserted at the end of all IRQ handlers.
  262. */
  263. #define OSAL_IRQ_EPILOGUE() CH_IRQ_EPILOGUE()
  264. /**
  265. * @brief IRQ handler function declaration.
  266. * @details This macro hides the details of an ISR function declaration.
  267. *
  268. * @param[in] id a vector name as defined in @p vectors.s
  269. */
  270. #define OSAL_IRQ_HANDLER(id) CH_IRQ_HANDLER(id)
  271. /** @} */
  272. /**
  273. * @name Time conversion utilities
  274. * @{
  275. */
  276. /**
  277. * @brief Seconds to system ticks.
  278. * @details Converts from seconds to system ticks number.
  279. * @note The result is rounded upward to the next tick boundary.
  280. *
  281. * @param[in] secs number of seconds
  282. * @return The number of ticks.
  283. *
  284. * @api
  285. */
  286. #define OSAL_S2I(secs) TIME_S2I(secs)
  287. /**
  288. * @brief Milliseconds to system ticks.
  289. * @details Converts from milliseconds to system ticks number.
  290. * @note The result is rounded upward to the next tick boundary.
  291. *
  292. * @param[in] msecs number of milliseconds
  293. * @return The number of ticks.
  294. *
  295. * @api
  296. */
  297. #define OSAL_MS2I(msecs) TIME_MS2I(msecs)
  298. /**
  299. * @brief Microseconds to system ticks.
  300. * @details Converts from microseconds to system ticks number.
  301. * @note The result is rounded upward to the next tick boundary.
  302. *
  303. * @param[in] usecs number of microseconds
  304. * @return The number of ticks.
  305. *
  306. * @api
  307. */
  308. #define OSAL_US2I(usecs) TIME_US2I(usecs)
  309. /** @} */
  310. /**
  311. * @name Time conversion utilities for the realtime counter
  312. * @{
  313. */
  314. /**
  315. * @brief Seconds to realtime counter.
  316. * @details Converts from seconds to realtime counter cycles.
  317. * @note The macro assumes that @p freq >= @p 1.
  318. *
  319. * @param[in] freq clock frequency, in Hz, of the realtime counter
  320. * @param[in] sec number of seconds
  321. * @return The number of cycles.
  322. *
  323. * @api
  324. */
  325. #define OSAL_S2RTC(freq, sec) S2RTC(freq, sec)
  326. /**
  327. * @brief Milliseconds to realtime counter.
  328. * @details Converts from milliseconds to realtime counter cycles.
  329. * @note The result is rounded upward to the next millisecond boundary.
  330. * @note The macro assumes that @p freq >= @p 1000.
  331. *
  332. * @param[in] freq clock frequency, in Hz, of the realtime counter
  333. * @param[in] msec number of milliseconds
  334. * @return The number of cycles.
  335. *
  336. * @api
  337. */
  338. #define OSAL_MS2RTC(freq, msec) MS2RTC(freq, msec)
  339. /**
  340. * @brief Microseconds to realtime counter.
  341. * @details Converts from microseconds to realtime counter cycles.
  342. * @note The result is rounded upward to the next microsecond boundary.
  343. * @note The macro assumes that @p freq >= @p 1000000.
  344. *
  345. * @param[in] freq clock frequency, in Hz, of the realtime counter
  346. * @param[in] usec number of microseconds
  347. * @return The number of cycles.
  348. *
  349. * @api
  350. */
  351. #define OSAL_US2RTC(freq, usec) US2RTC(freq, usec)
  352. /** @} */
  353. /**
  354. * @name Sleep macros using absolute time
  355. * @{
  356. */
  357. /**
  358. * @brief Delays the invoking thread for the specified number of seconds.
  359. * @note The specified time is rounded up to a value allowed by the real
  360. * system tick clock.
  361. * @note The maximum specifiable value is implementation dependent.
  362. *
  363. * @param[in] secs time in seconds, must be different from zero
  364. *
  365. * @api
  366. */
  367. #define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs))
  368. /**
  369. * @brief Delays the invoking thread for the specified number of
  370. * milliseconds.
  371. * @note The specified time is rounded up to a value allowed by the real
  372. * system tick clock.
  373. * @note The maximum specifiable value is implementation dependent.
  374. *
  375. * @param[in] msecs time in milliseconds, must be different from zero
  376. *
  377. * @api
  378. */
  379. #define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs))
  380. /**
  381. * @brief Delays the invoking thread for the specified number of
  382. * microseconds.
  383. * @note The specified time is rounded up to a value allowed by the real
  384. * system tick clock.
  385. * @note The maximum specifiable value is implementation dependent.
  386. *
  387. * @param[in] usecs time in microseconds, must be different from zero
  388. *
  389. * @api
  390. */
  391. #define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs))
  392. /** @} */
  393. /*===========================================================================*/
  394. /* External declarations. */
  395. /*===========================================================================*/
  396. #ifdef __cplusplus
  397. extern "C" {
  398. #endif
  399. #ifdef __cplusplus
  400. }
  401. #endif
  402. /*===========================================================================*/
  403. /* Module inline functions. */
  404. /*===========================================================================*/
  405. /**
  406. * @brief OSAL module initialization.
  407. *
  408. * @api
  409. */
  410. static inline void osalInit(void) {
  411. }
  412. /**
  413. * @brief System halt with error message.
  414. *
  415. * @param[in] reason the halt message pointer
  416. *
  417. * @api
  418. */
  419. static inline void osalSysHalt(const char *reason) {
  420. chSysHalt(reason);
  421. }
  422. /**
  423. * @brief Disables interrupts globally.
  424. *
  425. * @special
  426. */
  427. static inline void osalSysDisable(void) {
  428. chSysDisable();
  429. }
  430. /**
  431. * @brief Enables interrupts globally.
  432. *
  433. * @special
  434. */
  435. static inline void osalSysEnable(void) {
  436. chSysEnable();
  437. }
  438. /**
  439. * @brief Enters a critical zone from thread context.
  440. * @note This function cannot be used for reentrant critical zones.
  441. *
  442. * @special
  443. */
  444. static inline void osalSysLock(void) {
  445. chSysLock();
  446. }
  447. /**
  448. * @brief Leaves a critical zone from thread context.
  449. * @note This function cannot be used for reentrant critical zones.
  450. *
  451. * @special
  452. */
  453. static inline void osalSysUnlock(void) {
  454. chSysUnlock();
  455. }
  456. /**
  457. * @brief Enters a critical zone from ISR context.
  458. * @note This function cannot be used for reentrant critical zones.
  459. *
  460. * @special
  461. */
  462. static inline void osalSysLockFromISR(void) {
  463. chSysLockFromISR();
  464. }
  465. /**
  466. * @brief Leaves a critical zone from ISR context.
  467. * @note This function cannot be used for reentrant critical zones.
  468. *
  469. * @special
  470. */
  471. static inline void osalSysUnlockFromISR(void) {
  472. chSysUnlockFromISR();
  473. }
  474. /**
  475. * @brief Returns the execution status and enters a critical zone.
  476. * @details This functions enters into a critical zone and can be called
  477. * from any context. Because its flexibility it is less efficient
  478. * than @p chSysLock() which is preferable when the calling context
  479. * is known.
  480. * @post The system is in a critical zone.
  481. *
  482. * @return The previous system status, the encoding of this
  483. * status word is architecture-dependent and opaque.
  484. *
  485. * @xclass
  486. */
  487. static inline syssts_t osalSysGetStatusAndLockX(void) {
  488. return chSysGetStatusAndLockX();
  489. }
  490. /**
  491. * @brief Restores the specified execution status and leaves a critical zone.
  492. * @note A call to @p chSchRescheduleS() is automatically performed
  493. * if exiting the critical zone and if not in ISR context.
  494. *
  495. * @param[in] sts the system status to be restored.
  496. *
  497. * @xclass
  498. */
  499. static inline void osalSysRestoreStatusX(syssts_t sts) {
  500. chSysRestoreStatusX(sts);
  501. }
  502. /**
  503. * @brief Polled delay.
  504. * @note The real delay is always few cycles in excess of the specified
  505. * value.
  506. *
  507. * @param[in] cycles number of cycles
  508. *
  509. * @xclass
  510. */
  511. #if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__)
  512. static inline void osalSysPolledDelayX(rtcnt_t cycles) {
  513. chSysPolledDelayX(cycles);
  514. }
  515. #endif
  516. /**
  517. * @brief Systick callback for the underlying OS.
  518. * @note This callback is only defined if the OSAL requires such a
  519. * service from the HAL.
  520. */
  521. #if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__)
  522. static inline void osalOsTimerHandlerI(void) {
  523. chSysTimerHandlerI();
  524. }
  525. #endif
  526. /**
  527. * @brief Checks if a reschedule is required and performs it.
  528. * @note I-Class functions invoked from thread context must not reschedule
  529. * by themselves, an explicit reschedule using this function is
  530. * required in this scenario.
  531. * @note Not implemented in this simplified OSAL.
  532. *
  533. * @sclass
  534. */
  535. static inline void osalOsRescheduleS(void) {
  536. chSchRescheduleS();
  537. }
  538. /**
  539. * @brief Current system time.
  540. * @details Returns the number of system ticks since the @p osalInit()
  541. * invocation.
  542. * @note The counter can reach its maximum and then restart from zero.
  543. * @note This function can be called from any context but its atomicity
  544. * is not guaranteed on architectures whose word size is less than
  545. * @p systime_t size.
  546. *
  547. * @return The system time in ticks.
  548. *
  549. * @xclass
  550. */
  551. static inline systime_t osalOsGetSystemTimeX(void) {
  552. return chVTGetSystemTimeX();
  553. }
  554. /**
  555. * @brief Adds an interval to a system time returning a system time.
  556. *
  557. * @param[in] systime base system time
  558. * @param[in] interval interval to be added
  559. * @return The new system time.
  560. *
  561. * @xclass
  562. */
  563. static inline systime_t osalTimeAddX(systime_t systime,
  564. sysinterval_t interval) {
  565. return chTimeAddX(systime, interval);
  566. }
  567. /**
  568. * @brief Subtracts two system times returning an interval.
  569. *
  570. * @param[in] start first system time
  571. * @param[in] end second system time
  572. * @return The interval representing the time difference.
  573. *
  574. * @xclass
  575. */
  576. static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) {
  577. return chTimeDiffX(start, end);
  578. }
  579. /**
  580. * @brief Checks if the specified time is within the specified time window.
  581. * @note When start==end then the function returns always true because the
  582. * whole time range is specified.
  583. * @note This function can be called from any context.
  584. *
  585. * @param[in] time the time to be verified
  586. * @param[in] start the start of the time window (inclusive)
  587. * @param[in] end the end of the time window (non inclusive)
  588. * @retval true current time within the specified time window.
  589. * @retval false current time not within the specified time window.
  590. *
  591. * @xclass
  592. */
  593. static inline bool osalTimeIsInRangeX(systime_t time,
  594. systime_t start,
  595. systime_t end) {
  596. return chTimeIsInRangeX(time, start, end);
  597. }
  598. /**
  599. * @brief Suspends the invoking thread for the specified time.
  600. *
  601. * @param[in] time the delay in system ticks, the special values are
  602. * handled as follow:
  603. * - @a TIME_INFINITE is allowed but interpreted as a
  604. * normal time specification.
  605. * - @a TIME_IMMEDIATE this value is not allowed.
  606. * .
  607. *
  608. * @sclass
  609. */
  610. static inline void osalThreadSleepS(sysinterval_t time) {
  611. chThdSleepS(time);
  612. }
  613. /**
  614. * @brief Suspends the invoking thread for the specified time.
  615. *
  616. * @param[in] time the delay in system ticks, the special values are
  617. * handled as follow:
  618. * - @a TIME_INFINITE is allowed but interpreted as a
  619. * normal time specification.
  620. * - @a TIME_IMMEDIATE this value is not allowed.
  621. * .
  622. *
  623. * @api
  624. */
  625. static inline void osalThreadSleep(sysinterval_t time) {
  626. chThdSleep(time);
  627. }
  628. /**
  629. * @brief Sends the current thread sleeping and sets a reference variable.
  630. * @note This function must reschedule, it can only be called from thread
  631. * context.
  632. *
  633. * @param[in] trp a pointer to a thread reference object
  634. * @return The wake up message.
  635. *
  636. * @sclass
  637. */
  638. static inline msg_t osalThreadSuspendS(thread_reference_t *trp) {
  639. return chThdSuspendTimeoutS(trp, TIME_INFINITE);
  640. }
  641. /**
  642. * @brief Sends the current thread sleeping and sets a reference variable.
  643. * @note This function must reschedule, it can only be called from thread
  644. * context.
  645. *
  646. * @param[in] trp a pointer to a thread reference object
  647. * @param[in] timeout the timeout in system ticks, the special values are
  648. * handled as follow:
  649. * - @a TIME_INFINITE the thread enters an infinite sleep
  650. * state.
  651. * - @a TIME_IMMEDIATE the thread is not enqueued and
  652. * the function returns @p MSG_TIMEOUT as if a timeout
  653. * occurred.
  654. * .
  655. * @return The wake up message.
  656. * @retval MSG_TIMEOUT if the operation timed out.
  657. *
  658. * @sclass
  659. */
  660. static inline msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp,
  661. sysinterval_t timeout) {
  662. return chThdSuspendTimeoutS(trp, timeout);
  663. }
  664. /**
  665. * @brief Wakes up a thread waiting on a thread reference object.
  666. * @note This function must not reschedule because it can be called from
  667. * ISR context.
  668. *
  669. * @param[in] trp a pointer to a thread reference object
  670. * @param[in] msg the message code
  671. *
  672. * @iclass
  673. */
  674. static inline void osalThreadResumeI(thread_reference_t *trp, msg_t msg) {
  675. chThdResumeI(trp, msg);
  676. }
  677. /**
  678. * @brief Wakes up a thread waiting on a thread reference object.
  679. * @note This function must reschedule, it can only be called from thread
  680. * context.
  681. *
  682. * @param[in] trp a pointer to a thread reference object
  683. * @param[in] msg the message code
  684. *
  685. * @iclass
  686. */
  687. static inline void osalThreadResumeS(thread_reference_t *trp, msg_t msg) {
  688. chThdResumeI(trp, msg);
  689. chSchRescheduleS();
  690. }
  691. /**
  692. * @brief Initializes a threads queue object.
  693. *
  694. * @param[out] tqp pointer to the threads queue object
  695. *
  696. * @init
  697. */
  698. static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
  699. chThdQueueObjectInit(tqp);
  700. }
  701. /**
  702. * @brief Enqueues the caller thread.
  703. * @details The caller thread is enqueued and put to sleep until it is
  704. * dequeued or the specified timeouts expires.
  705. *
  706. * @param[in] tqp pointer to the threads queue object
  707. * @param[in] time the timeout in system ticks, the special values are
  708. * handled as follow:
  709. * - @a TIME_INFINITE the thread enters an infinite sleep
  710. * state.
  711. * - @a TIME_IMMEDIATE the thread is not enqueued and
  712. * the function returns @p MSG_TIMEOUT as if a timeout
  713. * occurred.
  714. * .
  715. * @return The message from @p osalQueueWakeupOneI() or
  716. * @p osalQueueWakeupAllI() functions.
  717. * @retval MSG_TIMEOUT if the thread has not been dequeued within the
  718. * specified timeout or if the function has been
  719. * invoked with @p TIME_IMMEDIATE as timeout
  720. * specification.
  721. *
  722. * @sclass
  723. */
  724. static inline msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp,
  725. sysinterval_t time) {
  726. return chThdEnqueueTimeoutS(tqp, time);
  727. }
  728. /**
  729. * @brief Dequeues and wakes up one thread from the queue, if any.
  730. *
  731. * @param[in] tqp pointer to the threads queue object
  732. * @param[in] msg the message code
  733. *
  734. * @iclass
  735. */
  736. static inline void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg) {
  737. chThdDequeueNextI(tqp, msg);
  738. }
  739. /**
  740. * @brief Dequeues and wakes up all threads from the queue.
  741. *
  742. * @param[in] tqp pointer to the threads queue object
  743. * @param[in] msg the message code
  744. *
  745. * @iclass
  746. */
  747. static inline void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg) {
  748. chThdDequeueAllI(tqp, msg);
  749. }
  750. /**
  751. * @brief Initializes an event source object.
  752. *
  753. * @param[out] esp pointer to the event source object
  754. *
  755. * @init
  756. */
  757. static inline void osalEventObjectInit(event_source_t *esp) {
  758. osalDbgCheck(esp != NULL);
  759. esp->flags = (eventflags_t)0;
  760. esp->cb = NULL;
  761. esp->param = NULL;
  762. }
  763. /**
  764. * @brief Add flags to an event source object.
  765. *
  766. * @param[in] esp pointer to the event flags object
  767. * @param[in] flags flags to be ORed to the flags mask
  768. *
  769. * @iclass
  770. */
  771. static inline void osalEventBroadcastFlagsI(event_source_t *esp,
  772. eventflags_t flags) {
  773. osalDbgCheck(esp != NULL);
  774. esp->flags |= flags;
  775. if (esp->cb != NULL) {
  776. esp->cb(esp);
  777. }
  778. }
  779. /**
  780. * @brief Add flags to an event source object.
  781. *
  782. * @param[in] esp pointer to the event flags object
  783. * @param[in] flags flags to be ORed to the flags mask
  784. *
  785. * @iclass
  786. */
  787. static inline void osalEventBroadcastFlags(event_source_t *esp,
  788. eventflags_t flags) {
  789. osalDbgCheck(esp != NULL);
  790. chSysLock();
  791. esp->flags |= flags;
  792. if (esp->cb != NULL) {
  793. esp->cb(esp);
  794. }
  795. chSysUnlock();
  796. }
  797. /**
  798. * @brief Event callback setup.
  799. * @note The callback is invoked from ISR context and can
  800. * only invoke I-Class functions. The callback is meant
  801. * to wakeup the task that will handle the event by
  802. * calling @p osalEventGetAndClearFlagsI().
  803. *
  804. * @param[in] esp pointer to the event flags object
  805. * @param[in] cb pointer to the callback function
  806. * @param[in] param parameter to be passed to the callback function
  807. *
  808. * @api
  809. */
  810. static inline void osalEventSetCallback(event_source_t *esp,
  811. eventcallback_t cb,
  812. void *param) {
  813. osalDbgCheck(esp != NULL);
  814. esp->cb = cb;
  815. esp->param = param;
  816. }
  817. /**
  818. * @brief Initializes s @p mutex_t object.
  819. *
  820. * @param[out] mp pointer to the @p mutex_t object
  821. *
  822. * @init
  823. */
  824. static inline void osalMutexObjectInit(mutex_t *mp) {
  825. chSemObjectInit((semaphore_t *)mp, (cnt_t)1);
  826. }
  827. /**
  828. * @brief Locks the specified mutex.
  829. * @post The mutex is locked and inserted in the per-thread stack of owned
  830. * mutexes.
  831. *
  832. * @param[in,out] mp pointer to the @p mutex_t object
  833. *
  834. * @api
  835. */
  836. static inline void osalMutexLock(mutex_t *mp) {
  837. (void) chSemWait((semaphore_t *)mp);
  838. }
  839. /**
  840. * @brief Unlocks the specified mutex.
  841. * @note The HAL guarantees to release mutex in reverse lock order. The
  842. * mutex being unlocked is guaranteed to be the last locked mutex
  843. * by the invoking thread.
  844. * The implementation can rely on this behavior and eventually
  845. * ignore the @p mp parameter which is supplied in order to support
  846. * those OSes not supporting a stack of the owned mutexes.
  847. *
  848. * @param[in,out] mp pointer to the @p mutex_t object
  849. *
  850. * @api
  851. */
  852. static inline void osalMutexUnlock(mutex_t *mp) {
  853. chSemSignal((semaphore_t *)mp);
  854. }
  855. #endif /* OSAL_H */
  856. /** @} */