osal.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. /*===========================================================================*/
  27. /* Module constants. */
  28. /*===========================================================================*/
  29. /**
  30. * @name Common constants
  31. * @{
  32. */
  33. #if !defined(FALSE) || defined(__DOXYGEN__)
  34. #define FALSE 0
  35. #endif
  36. #if !defined(TRUE) || defined(__DOXYGEN__)
  37. #define TRUE 1
  38. #endif
  39. #define OSAL_SUCCESS false
  40. #define OSAL_FAILED true
  41. /** @} */
  42. #if 0
  43. /**
  44. * @name Messages
  45. * @{
  46. */
  47. #define MSG_OK (msg_t)0
  48. #define MSG_TIMEOUT (msg_t)-1
  49. #define MSG_RESET (msg_t)-2
  50. /** @} */
  51. #endif
  52. #if 0
  53. /**
  54. * @name Special time constants
  55. * @{
  56. */
  57. #define TIME_IMMEDIATE ((sysinterval_t)0)
  58. #define TIME_INFINITE ((sysinterval_t)-1)
  59. /** @} */
  60. #endif
  61. /**
  62. * @name Systick modes.
  63. * @{
  64. */
  65. #define OSAL_ST_MODE_NONE 0
  66. #define OSAL_ST_MODE_PERIODIC 1
  67. #define OSAL_ST_MODE_FREERUNNING 2
  68. /** @} */
  69. /**
  70. * @name Systick parameters.
  71. * @{
  72. */
  73. /**
  74. * @brief Size in bits of the @p systick_t type.
  75. */
  76. #define OSAL_ST_RESOLUTION CH_CFG_ST_RESOLUTION
  77. /**
  78. * @brief Required systick frequency or resolution.
  79. */
  80. #define OSAL_ST_FREQUENCY CH_CFG_ST_FREQUENCY
  81. /**
  82. * @brief Systick mode required by the underlying OS.
  83. */
  84. #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__)
  85. #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
  86. #else
  87. #define OSAL_ST_MODE OSAL_ST_MODE_FREERUNNING
  88. #endif
  89. /** @} */
  90. /*===========================================================================*/
  91. /* Module pre-compile time settings. */
  92. /*===========================================================================*/
  93. /*===========================================================================*/
  94. /* Derived constants and error checks. */
  95. /*===========================================================================*/
  96. #if !(OSAL_ST_MODE == OSAL_ST_MODE_NONE) && \
  97. !(OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) && \
  98. !(OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING)
  99. #error "invalid OSAL_ST_MODE setting in osal.h"
  100. #endif
  101. #if (OSAL_ST_RESOLUTION != 16) && (OSAL_ST_RESOLUTION != 32)
  102. #error "invalid OSAL_ST_RESOLUTION, must be 16 or 32"
  103. #endif
  104. /*===========================================================================*/
  105. /* Module data structures and types. */
  106. /*===========================================================================*/
  107. #if 0
  108. /**
  109. * @brief Type of a system status word.
  110. */
  111. typedef uint32_t syssts_t;
  112. #endif
  113. #if 0
  114. /**
  115. * @brief Type of a message.
  116. */
  117. typedef int32_t msg_t;
  118. #endif
  119. #if 0
  120. /**
  121. * @brief Type of system time counter.
  122. */
  123. typedef uint32_t systime_t;
  124. #endif
  125. #if 0
  126. /**
  127. * @brief Type of system time interval.
  128. */
  129. typedef uint32_t sysinterval_t;
  130. #endif
  131. #if 0
  132. /**
  133. * @brief Type of realtime counter.
  134. */
  135. typedef uint32_t rtcnt_t;
  136. #endif
  137. #if 0
  138. /**
  139. * @brief Type of a thread reference.
  140. */
  141. typedef thread_t * thread_reference_t;
  142. #endif
  143. #if 0
  144. /**
  145. * @brief Type of an event flags mask.
  146. */
  147. typedef uint32_t eventflags_t;
  148. #endif
  149. #if (CH_CFG_USE_EVENTS == FALSE) || defined(__DOXYGEN__)
  150. /**
  151. * @brief Type of an event flags object.
  152. * @note The content of this structure is not part of the API and should
  153. * not be relied upon. Implementers may define this structure in
  154. * an entirely different way.
  155. * @note Retrieval and clearing of the flags are not defined in this
  156. * API and are implementation-dependent.
  157. */
  158. typedef struct event_source event_source_t;
  159. /**
  160. * @brief Type of an event source callback.
  161. * @note This type is not part of the OSAL API and is provided
  162. * exclusively as an example and for convenience.
  163. */
  164. typedef void (*eventcallback_t)(event_source_t *esp);
  165. /**
  166. * @brief Events source object.
  167. * @note The content of this structure is not part of the API and should
  168. * not be relied upon. Implementers may define this structure in
  169. * an entirely different way.
  170. * @note Retrieval and clearing of the flags are not defined in this
  171. * API and are implementation-dependent.
  172. */
  173. struct event_source {
  174. volatile eventflags_t flags; /**< @brief Stored event flags. */
  175. eventcallback_t cb; /**< @brief Event source callback. */
  176. void *param; /**< @brief User defined field. */
  177. };
  178. #endif /* CH_CFG_USE_EVENTS == FALSE */
  179. /**
  180. * @brief Type of a mutex.
  181. * @note If the OS does not support mutexes or there is no OS then the
  182. * mechanism can be simulated.
  183. */
  184. #if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__)
  185. #elif CH_CFG_USE_SEMAPHORES
  186. typedef semaphore_t mutex_t;
  187. #else
  188. typedef uint32_t mutex_t;
  189. #endif
  190. #if 0
  191. /**
  192. * @brief Type of a thread queue.
  193. * @details A thread queue is a queue of sleeping threads, queued threads
  194. * can be dequeued one at time or all together.
  195. * @note In this implementation it is implemented as a single reference
  196. * because there are no real threads.
  197. */
  198. typedef struct {
  199. thread_reference_t tr;
  200. } threads_queue_t;
  201. #endif
  202. /*===========================================================================*/
  203. /* Module macros. */
  204. /*===========================================================================*/
  205. /**
  206. * @name Debug related macros
  207. * @{
  208. */
  209. /**
  210. * @brief Condition assertion.
  211. * @details If the condition check fails then the OSAL panics with a
  212. * message and halts.
  213. * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS
  214. * switch is enabled.
  215. * @note The remark string is not currently used except for putting a
  216. * comment in the code about the assertion.
  217. *
  218. * @param[in] c the condition to be verified to be true
  219. * @param[in] remark a remark string
  220. *
  221. * @api
  222. */
  223. #define osalDbgAssert(c, remark) chDbgAssert(c, remark)
  224. /**
  225. * @brief Function parameters check.
  226. * @details If the condition check fails then the OSAL panics and halts.
  227. * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
  228. * is enabled.
  229. *
  230. * @param[in] c the condition to be verified to be true
  231. *
  232. * @api
  233. */
  234. #define osalDbgCheck(c) chDbgCheck(c)
  235. /**
  236. * @brief I-Class state check.
  237. * @note Not implemented in this simplified OSAL.
  238. */
  239. #define osalDbgCheckClassI() chDbgCheckClassI()
  240. /**
  241. * @brief S-Class state check.
  242. * @note Not implemented in this simplified OSAL.
  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] delay 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 delay) {
  611. chThdSleepS(delay);
  612. }
  613. /**
  614. * @brief Suspends the invoking thread for the specified time.
  615. *
  616. * @param[in] delay 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 delay) {
  626. chThdSleep(delay);
  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. chThdResumeS(trp, msg);
  689. }
  690. /**
  691. * @brief Initializes a threads queue object.
  692. *
  693. * @param[out] tqp pointer to the threads queue object
  694. *
  695. * @init
  696. */
  697. static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
  698. chThdQueueObjectInit(tqp);
  699. }
  700. /**
  701. * @brief Enqueues the caller thread.
  702. * @details The caller thread is enqueued and put to sleep until it is
  703. * dequeued or the specified timeouts expires.
  704. *
  705. * @param[in] tqp pointer to the threads queue object
  706. * @param[in] timeout the timeout in system ticks, the special values are
  707. * handled as follow:
  708. * - @a TIME_INFINITE the thread enters an infinite sleep
  709. * state.
  710. * - @a TIME_IMMEDIATE the thread is not enqueued and
  711. * the function returns @p MSG_TIMEOUT as if a timeout
  712. * occurred.
  713. * .
  714. * @return The message from @p osalQueueWakeupOneI() or
  715. * @p osalQueueWakeupAllI() functions.
  716. * @retval MSG_TIMEOUT if the thread has not been dequeued within the
  717. * specified timeout or if the function has been
  718. * invoked with @p TIME_IMMEDIATE as timeout
  719. * specification.
  720. *
  721. * @sclass
  722. */
  723. static inline msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp,
  724. sysinterval_t timeout) {
  725. return chThdEnqueueTimeoutS(tqp, timeout);
  726. }
  727. /**
  728. * @brief Dequeues and wakes up one thread from the queue, if any.
  729. *
  730. * @param[in] tqp pointer to the threads queue object
  731. * @param[in] msg the message code
  732. *
  733. * @iclass
  734. */
  735. static inline void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg) {
  736. chThdDequeueNextI(tqp, msg);
  737. }
  738. /**
  739. * @brief Dequeues and wakes up all threads from the queue.
  740. *
  741. * @param[in] tqp pointer to the threads queue object
  742. * @param[in] msg the message code
  743. *
  744. * @iclass
  745. */
  746. static inline void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg) {
  747. chThdDequeueAllI(tqp, msg);
  748. }
  749. #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  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. chEvtObjectInit(esp);
  759. }
  760. #else
  761. static inline void osalEventObjectInit(event_source_t *esp) {
  762. osalDbgCheck(esp != NULL);
  763. esp->flags = (eventflags_t)0;
  764. esp->cb = NULL;
  765. esp->param = NULL;
  766. }
  767. #endif
  768. #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  769. /**
  770. * @brief Add flags to an event source object.
  771. *
  772. * @param[in] esp pointer to the event flags object
  773. * @param[in] flags flags to be ORed to the flags mask
  774. *
  775. * @iclass
  776. */
  777. static inline void osalEventBroadcastFlagsI(event_source_t *esp,
  778. eventflags_t flags) {
  779. chEvtBroadcastFlagsI(esp, flags);
  780. }
  781. #else
  782. static inline void osalEventBroadcastFlagsI(event_source_t *esp,
  783. eventflags_t flags) {
  784. osalDbgCheck(esp != NULL);
  785. esp->flags |= flags;
  786. if (esp->cb != NULL) {
  787. esp->cb(esp);
  788. }
  789. }
  790. #endif
  791. #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  792. /**
  793. * @brief Add flags to an event source object.
  794. *
  795. * @param[in] esp pointer to the event flags object
  796. * @param[in] flags flags to be ORed to the flags mask
  797. *
  798. * @iclass
  799. */
  800. static inline void osalEventBroadcastFlags(event_source_t *esp,
  801. eventflags_t flags) {
  802. chEvtBroadcastFlags(esp, flags);
  803. }
  804. #else
  805. static inline void osalEventBroadcastFlags(event_source_t *esp,
  806. eventflags_t flags) {
  807. osalDbgCheck(esp != NULL);
  808. osalSysLock();
  809. esp->flags |= flags;
  810. if (esp->cb != NULL) {
  811. esp->cb(esp);
  812. }
  813. osalSysUnlock();
  814. }
  815. #endif
  816. #if (CH_CFG_USE_EVENTS == FALSE) || defined(__DOXYGEN__)
  817. /**
  818. * @brief Event callback setup.
  819. * @note The callback is invoked from ISR context and can
  820. * only invoke I-Class functions. The callback is meant
  821. * to wakeup the task that will handle the event by
  822. * calling @p osalEventGetAndClearFlagsI().
  823. *
  824. * @param[in] esp pointer to the event flags object
  825. * @param[in] cb pointer to the callback function
  826. * @param[in] param parameter to be passed to the callback function
  827. *
  828. * @api
  829. */
  830. static inline void osalEventSetCallback(event_source_t *esp,
  831. eventcallback_t cb,
  832. void *param) {
  833. osalDbgCheck(esp != NULL);
  834. esp->cb = cb;
  835. esp->param = param;
  836. }
  837. #endif
  838. /**
  839. * @brief Initializes s @p mutex_t object.
  840. *
  841. * @param[out] mp pointer to the @p mutex_t object
  842. *
  843. * @init
  844. */
  845. static inline void osalMutexObjectInit(mutex_t *mp) {
  846. #if CH_CFG_USE_MUTEXES
  847. chMtxObjectInit(mp);
  848. #elif CH_CFG_USE_SEMAPHORES
  849. chSemObjectInit((semaphore_t *)mp, 1);
  850. #else
  851. *mp = 0;
  852. #endif
  853. }
  854. /**
  855. * @brief Locks the specified mutex.
  856. * @post The mutex is locked and inserted in the per-thread stack of owned
  857. * mutexes.
  858. *
  859. * @param[in,out] mp pointer to the @p mutex_t object
  860. *
  861. * @api
  862. */
  863. static inline void osalMutexLock(mutex_t *mp) {
  864. #if CH_CFG_USE_MUTEXES
  865. chMtxLock(mp);
  866. #elif CH_CFG_USE_SEMAPHORES
  867. chSemWait((semaphore_t *)mp);
  868. #else
  869. *mp = 1;
  870. #endif
  871. }
  872. /**
  873. * @brief Unlocks the specified mutex.
  874. * @note The HAL guarantees to release mutex in reverse lock order. The
  875. * mutex being unlocked is guaranteed to be the last locked mutex
  876. * by the invoking thread.
  877. * The implementation can rely on this behavior and eventually
  878. * ignore the @p mp parameter which is supplied in order to support
  879. * those OSes not supporting a stack of the owned mutexes.
  880. *
  881. * @param[in,out] mp pointer to the @p mutex_t object
  882. *
  883. * @api
  884. */
  885. static inline void osalMutexUnlock(mutex_t *mp) {
  886. #if CH_CFG_USE_MUTEXES
  887. chMtxUnlock(mp);
  888. #elif CH_CFG_USE_SEMAPHORES
  889. chSemSignal((semaphore_t *)mp);
  890. #else
  891. *mp = 0;
  892. #endif
  893. }
  894. #endif /* OSAL_H */
  895. /** @} */