osal.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /**
  29. * @name Common constants
  30. * @{
  31. */
  32. #if !defined(FALSE) || defined(__DOXYGEN__)
  33. #define FALSE 0
  34. #endif
  35. #if !defined(TRUE) || defined(__DOXYGEN__)
  36. #define TRUE 1
  37. #endif
  38. #define OSAL_SUCCESS false
  39. #define OSAL_FAILED true
  40. /** @} */
  41. /**
  42. * @name Messages
  43. * @{
  44. */
  45. #define MSG_OK (msg_t)0
  46. #define MSG_TIMEOUT (msg_t)-1
  47. #define MSG_RESET (msg_t)-2
  48. /** @} */
  49. /**
  50. * @name Special time constants
  51. * @{
  52. */
  53. #define TIME_IMMEDIATE ((sysinterval_t)0)
  54. #define TIME_INFINITE ((sysinterval_t)-1)
  55. /** @} */
  56. /**
  57. * @name Systick modes.
  58. * @{
  59. */
  60. #define OSAL_ST_MODE_NONE 0
  61. #define OSAL_ST_MODE_PERIODIC 1
  62. #define OSAL_ST_MODE_FREERUNNING 2
  63. /** @} */
  64. /**
  65. * @name Systick parameters.
  66. * @{
  67. */
  68. /**
  69. * @brief Size in bits of the @p systick_t type.
  70. */
  71. #define OSAL_ST_RESOLUTION 32
  72. /**
  73. * @brief Required systick frequency or resolution.
  74. */
  75. #define OSAL_ST_FREQUENCY 1000
  76. /**
  77. * @brief Systick mode required by the underlying OS.
  78. */
  79. #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
  80. /** @} */
  81. /**
  82. * @name IRQ-related constants
  83. * @{
  84. */
  85. /**
  86. * @brief Total priority levels.
  87. * @brief Implementation not mandatory.
  88. */
  89. #define OSAL_IRQ_PRIORITY_LEVELS 16U
  90. /**
  91. * @brief Highest IRQ priority for HAL drivers.
  92. * @brief Implementation not mandatory.
  93. */
  94. #define OSAL_IRQ_MAXIMUM_PRIORITY 0U
  95. /** @} */
  96. /*===========================================================================*/
  97. /* Module pre-compile time settings. */
  98. /*===========================================================================*/
  99. /**
  100. * @brief Enables OSAL assertions.
  101. */
  102. #if !defined(OSAL_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
  103. #define OSAL_DBG_ENABLE_ASSERTS FALSE
  104. #endif
  105. /**
  106. * @brief Enables OSAL functions parameters checks.
  107. */
  108. #if !defined(OSAL_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__)
  109. #define OSAL_DBG_ENABLE_CHECKS FALSE
  110. #endif
  111. /*===========================================================================*/
  112. /* Derived constants and error checks. */
  113. /*===========================================================================*/
  114. #if !(OSAL_ST_MODE == OSAL_ST_MODE_NONE) && \
  115. !(OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) && \
  116. !(OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING)
  117. #error "invalid OSAL_ST_MODE setting in osal.h"
  118. #endif
  119. #if (OSAL_ST_RESOLUTION != 16) && (OSAL_ST_RESOLUTION != 32)
  120. #error "invalid OSAL_ST_RESOLUTION, must be 16 or 32"
  121. #endif
  122. /*===========================================================================*/
  123. /* Module data structures and types. */
  124. /*===========================================================================*/
  125. /**
  126. * @brief Type of a system status word.
  127. */
  128. typedef uint32_t syssts_t;
  129. /**
  130. * @brief Type of a message.
  131. */
  132. typedef int32_t msg_t;
  133. /**
  134. * @brief Type of system time counter.
  135. */
  136. typedef uint32_t systime_t;
  137. /**
  138. * @brief Type of system time interval.
  139. */
  140. typedef uint32_t sysinterval_t;
  141. /**
  142. * @brief Type of realtime counter.
  143. */
  144. typedef uint32_t rtcnt_t;
  145. /**
  146. * @brief Type of a thread reference.
  147. */
  148. typedef void * thread_reference_t;
  149. /**
  150. * @brief Type of an event flags mask.
  151. */
  152. typedef uint32_t eventflags_t;
  153. /**
  154. * @brief Type of an event flags object.
  155. * @note The content of this structure is not part of the API and should
  156. * not be relied upon. Implementers may define this structure in
  157. * an entirely different way.
  158. * @note Retrieval and clearing of the flags are not defined in this
  159. * API and are implementation-dependent.
  160. */
  161. typedef struct event_source event_source_t;
  162. /**
  163. * @brief Type of an event source callback.
  164. * @note This type is not part of the OSAL API and is provided
  165. * exclusively as an example and for convenience.
  166. */
  167. typedef void (*eventcallback_t)(event_source_t *esp);
  168. /**
  169. * @brief Events source object.
  170. * @note The content of this structure is not part of the API and should
  171. * not be relied upon. Implementers may define this structure in
  172. * an entirely different way.
  173. * @note Retrieval and clearing of the flags are not defined in this
  174. * API and are implementation-dependent.
  175. */
  176. struct event_source {
  177. volatile eventflags_t flags; /**< @brief Stored event flags. */
  178. eventcallback_t cb; /**< @brief Event source callback. */
  179. void *param; /**< @brief User defined field. */
  180. };
  181. /**
  182. * @brief Type of a mutex.
  183. * @note If the OS does not support mutexes or there is no OS then them
  184. * mechanism can be simulated.
  185. */
  186. typedef uint32_t mutex_t;
  187. /**
  188. * @brief Type of a thread queue.
  189. * @details A thread queue is a queue of sleeping threads, queued threads
  190. * can be dequeued one at time or all together.
  191. * @note If the OSAL is implemented on a bare metal machine without RTOS
  192. * then the queue can be implemented as a single thread reference.
  193. */
  194. typedef struct {
  195. thread_reference_t tr;
  196. } threads_queue_t;
  197. /*===========================================================================*/
  198. /* Module macros. */
  199. /*===========================================================================*/
  200. /**
  201. * @name Debug related macros
  202. * @{
  203. */
  204. /**
  205. * @brief Condition assertion.
  206. * @details If the condition check fails then the OSAL panics with a
  207. * message and halts.
  208. * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS
  209. * switch is enabled.
  210. * @note The remark string is not currently used except for putting a
  211. * comment in the code about the assertion.
  212. *
  213. * @param[in] c the condition to be verified to be true
  214. * @param[in] remark a remark string
  215. *
  216. * @api
  217. */
  218. #define osalDbgAssert(c, remark) do { \
  219. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  220. if (OSAL_DBG_ENABLE_ASSERTS != FALSE) { \
  221. if (!(c)) { \
  222. /*lint -restore*/ \
  223. osalSysHalt(__func__); \
  224. } \
  225. } \
  226. } while (false)
  227. /**
  228. * @brief Function parameters check.
  229. * @details If the condition check fails then the OSAL panics and halts.
  230. * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
  231. * is enabled.
  232. *
  233. * @param[in] c the condition to be verified to be true
  234. *
  235. * @api
  236. */
  237. #define osalDbgCheck(c) do { \
  238. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  239. if (OSAL_DBG_ENABLE_CHECKS != FALSE) { \
  240. if (!(c)) { \
  241. /*lint -restore*/ \
  242. osalSysHalt(__func__); \
  243. } \
  244. } \
  245. } while (false)
  246. /**
  247. * @brief I-Class state check.
  248. * @note Implementation is optional.
  249. */
  250. #define osalDbgCheckClassI()
  251. /**
  252. * @brief S-Class state check.
  253. * @note Implementation is optional.
  254. */
  255. #define osalDbgCheckClassS()
  256. /** @} */
  257. /**
  258. * @name IRQ service routines wrappers
  259. * @{
  260. */
  261. /**
  262. * @brief Priority level verification macro.
  263. */
  264. #define OSAL_IRQ_IS_VALID_PRIORITY(n) \
  265. (((n) >= OSAL_IRQ_MAXIMUM_PRIORITY) && ((n) < OSAL_IRQ_PRIORITY_LEVELS))
  266. /**
  267. * @brief IRQ prologue code.
  268. * @details This macro must be inserted at the start of all IRQ handlers.
  269. */
  270. #define OSAL_IRQ_PROLOGUE()
  271. /**
  272. * @brief IRQ epilogue code.
  273. * @details This macro must be inserted at the end of all IRQ handlers.
  274. */
  275. #define OSAL_IRQ_EPILOGUE()
  276. /**
  277. * @brief IRQ handler function declaration.
  278. * @details This macro hides the details of an ISR function declaration.
  279. *
  280. * @param[in] id a vector name as defined in @p vectors.s
  281. */
  282. #define OSAL_IRQ_HANDLER(id) void id(void)
  283. /** @} */
  284. /**
  285. * @name Time conversion utilities
  286. * @{
  287. */
  288. /**
  289. * @brief Seconds to system ticks.
  290. * @details Converts from seconds to system ticks number.
  291. * @note The result is rounded upward to the next tick boundary.
  292. *
  293. * @param[in] secs number of seconds
  294. * @return The number of ticks.
  295. *
  296. * @api
  297. */
  298. #define OSAL_S2I(secs) \
  299. ((sysinterval_t)((uint32_t)(secs) * (uint32_t)OSAL_ST_FREQUENCY))
  300. /**
  301. * @brief Milliseconds to system ticks.
  302. * @details Converts from milliseconds to system ticks number.
  303. * @note The result is rounded upward to the next tick boundary.
  304. *
  305. * @param[in] msecs number of milliseconds
  306. * @return The number of ticks.
  307. *
  308. * @api
  309. */
  310. #define OSAL_MS2I(msecs) \
  311. ((sysinterval_t)((((((uint32_t)(msecs)) * \
  312. ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000UL) + 1UL))
  313. /**
  314. * @brief Microseconds to system ticks.
  315. * @details Converts from microseconds to system ticks number.
  316. * @note The result is rounded upward to the next tick boundary.
  317. *
  318. * @param[in] usecs number of microseconds
  319. * @return The number of ticks.
  320. *
  321. * @api
  322. */
  323. #define OSAL_US2I(usecs) \
  324. ((sysinterval_t)((((((uint32_t)(usecs)) * \
  325. ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000000UL) + 1UL))
  326. /** @} */
  327. /**
  328. * @name Time conversion utilities for the realtime counter
  329. * @{
  330. */
  331. /**
  332. * @brief Seconds to realtime counter.
  333. * @details Converts from seconds to realtime counter cycles.
  334. * @note The macro assumes that @p freq >= @p 1.
  335. *
  336. * @param[in] freq clock frequency, in Hz, of the realtime counter
  337. * @param[in] sec number of seconds
  338. * @return The number of cycles.
  339. *
  340. * @api
  341. */
  342. #define OSAL_S2RTC(freq, sec) ((freq) * (sec))
  343. /**
  344. * @brief Milliseconds to realtime counter.
  345. * @details Converts from milliseconds to realtime counter cycles.
  346. * @note The result is rounded upward to the next millisecond boundary.
  347. * @note The macro assumes that @p freq >= @p 1000.
  348. *
  349. * @param[in] freq clock frequency, in Hz, of the realtime counter
  350. * @param[in] msec number of milliseconds
  351. * @return The number of cycles.
  352. *
  353. * @api
  354. */
  355. #define OSAL_MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec))
  356. /**
  357. * @brief Microseconds to realtime counter.
  358. * @details Converts from microseconds to realtime counter cycles.
  359. * @note The result is rounded upward to the next microsecond boundary.
  360. * @note The macro assumes that @p freq >= @p 1000000.
  361. *
  362. * @param[in] freq clock frequency, in Hz, of the realtime counter
  363. * @param[in] usec number of microseconds
  364. * @return The number of cycles.
  365. *
  366. * @api
  367. */
  368. #define OSAL_US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))
  369. /** @} */
  370. /**
  371. * @name Sleep macros using absolute time
  372. * @{
  373. */
  374. /**
  375. * @brief Delays the invoking thread for the specified number of seconds.
  376. * @note The specified time is rounded up to a value allowed by the real
  377. * system tick clock.
  378. * @note The maximum specifiable value is implementation dependent.
  379. *
  380. * @param[in] secs time in seconds, must be different from zero
  381. *
  382. * @api
  383. */
  384. #define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs))
  385. /**
  386. * @brief Delays the invoking thread for the specified number of
  387. * milliseconds.
  388. * @note The specified time is rounded up to a value allowed by the real
  389. * system tick clock.
  390. * @note The maximum specifiable value is implementation dependent.
  391. *
  392. * @param[in] msecs time in milliseconds, must be different from zero
  393. *
  394. * @api
  395. */
  396. #define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs))
  397. /**
  398. * @brief Delays the invoking thread for the specified number of
  399. * microseconds.
  400. * @note The specified time is rounded up to a value allowed by the real
  401. * system tick clock.
  402. * @note The maximum specifiable value is implementation dependent.
  403. *
  404. * @param[in] usecs time in microseconds, must be different from zero
  405. *
  406. * @api
  407. */
  408. #define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs))
  409. /** @} */
  410. /*===========================================================================*/
  411. /* External declarations. */
  412. /*===========================================================================*/
  413. extern const char *osal_halt_msg;
  414. #ifdef __cplusplus
  415. extern "C" {
  416. #endif
  417. void osalInit(void);
  418. void osalSysHalt(const char *reason);
  419. void osalSysPolledDelayX(rtcnt_t cycles);
  420. void osalOsTimerHandlerI(void);
  421. void osalOsRescheduleS(void);
  422. systime_t osalOsGetSystemTimeX(void);
  423. void osalThreadSleepS(sysinterval_t time);
  424. void osalThreadSleep(sysinterval_t time);
  425. msg_t osalThreadSuspendS(thread_reference_t *trp);
  426. msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout);
  427. void osalThreadResumeI(thread_reference_t *trp, msg_t msg);
  428. void osalThreadResumeS(thread_reference_t *trp, msg_t msg);
  429. msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout);
  430. void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg);
  431. void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg);
  432. void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags);
  433. void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags);
  434. void osalEventSetCallback(event_source_t *esp,
  435. eventcallback_t cb,
  436. void *param);
  437. void osalMutexLock(mutex_t *mp);
  438. void osalMutexUnlock(mutex_t *mp);
  439. #ifdef __cplusplus
  440. }
  441. #endif
  442. /*===========================================================================*/
  443. /* Module inline functions. */
  444. /*===========================================================================*/
  445. /**
  446. * @brief Disables interrupts globally.
  447. *
  448. * @special
  449. */
  450. static inline void osalSysDisable(void) {
  451. }
  452. /**
  453. * @brief Enables interrupts globally.
  454. *
  455. * @special
  456. */
  457. static inline void osalSysEnable(void) {
  458. }
  459. /**
  460. * @brief Enters a critical zone from thread context.
  461. * @note This function cannot be used for reentrant critical zones.
  462. *
  463. * @special
  464. */
  465. static inline void osalSysLock(void) {
  466. }
  467. /**
  468. * @brief Leaves a critical zone from thread context.
  469. * @note This function cannot be used for reentrant critical zones.
  470. *
  471. * @special
  472. */
  473. static inline void osalSysUnlock(void) {
  474. }
  475. /**
  476. * @brief Enters a critical zone from ISR context.
  477. * @note This function cannot be used for reentrant critical zones.
  478. *
  479. * @special
  480. */
  481. static inline void osalSysLockFromISR(void) {
  482. }
  483. /**
  484. * @brief Leaves a critical zone from ISR context.
  485. * @note This function cannot be used for reentrant critical zones.
  486. *
  487. * @special
  488. */
  489. static inline void osalSysUnlockFromISR(void) {
  490. }
  491. /**
  492. * @brief Returns the execution status and enters a critical zone.
  493. * @details This functions enters into a critical zone and can be called
  494. * from any context. Because its flexibility it is less efficient
  495. * than @p chSysLock() which is preferable when the calling context
  496. * is known.
  497. * @post The system is in a critical zone.
  498. *
  499. * @return The previous system status, the encoding of this
  500. * status word is architecture-dependent and opaque.
  501. *
  502. * @xclass
  503. */
  504. static inline syssts_t osalSysGetStatusAndLockX(void) {
  505. return (syssts_t)0;
  506. }
  507. /**
  508. * @brief Restores the specified execution status and leaves a critical zone.
  509. * @note A call to @p chSchRescheduleS() is automatically performed
  510. * if exiting the critical zone and if not in ISR context.
  511. *
  512. * @param[in] sts the system status to be restored.
  513. *
  514. * @xclass
  515. */
  516. static inline void osalSysRestoreStatusX(syssts_t sts) {
  517. (void)sts;
  518. }
  519. /**
  520. * @brief Adds an interval to a system time returning a system time.
  521. *
  522. * @param[in] systime base system time
  523. * @param[in] interval interval to be added
  524. * @return The new system time.
  525. *
  526. * @xclass
  527. */
  528. static inline systime_t osalTimeAddX(systime_t systime,
  529. sysinterval_t interval) {
  530. return systime + (systime_t)interval;
  531. }
  532. /**
  533. * @brief Subtracts two system times returning an interval.
  534. *
  535. * @param[in] start first system time
  536. * @param[in] end second system time
  537. * @return The interval representing the time difference.
  538. *
  539. * @xclass
  540. */
  541. static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) {
  542. return (sysinterval_t)((systime_t)(end - start));
  543. }
  544. /**
  545. * @brief Checks if the specified time is within the specified time window.
  546. * @note When start==end then the function returns always true because the
  547. * whole time range is specified.
  548. * @note This function can be called from any context.
  549. *
  550. * @param[in] time the time to be verified
  551. * @param[in] start the start of the time window (inclusive)
  552. * @param[in] end the end of the time window (non inclusive)
  553. * @retval true current time within the specified time window.
  554. * @retval false current time not within the specified time window.
  555. *
  556. * @xclass
  557. */
  558. static inline bool osalTimeIsInRangeX(systime_t time,
  559. systime_t start,
  560. systime_t end) {
  561. return (bool)((time - start) < (end - start));
  562. }
  563. /**
  564. * @brief Initializes a threads queue object.
  565. *
  566. * @param[out] tqp pointer to the threads queue object
  567. *
  568. * @init
  569. */
  570. static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
  571. osalDbgCheck(tqp != NULL);
  572. }
  573. /**
  574. * @brief Initializes an event source object.
  575. *
  576. * @param[out] esp pointer to the event source object
  577. *
  578. * @init
  579. */
  580. static inline void osalEventObjectInit(event_source_t *esp) {
  581. osalDbgCheck(esp != NULL);
  582. esp->flags = (eventflags_t)0;
  583. esp->cb = NULL;
  584. esp->param = NULL;
  585. }
  586. /**
  587. * @brief Initializes s @p mutex_t object.
  588. *
  589. * @param[out] mp pointer to the @p mutex_t object
  590. *
  591. * @init
  592. */
  593. static inline void osalMutexObjectInit(mutex_t *mp) {
  594. osalDbgCheck(mp != NULL);
  595. *mp = 0;
  596. }
  597. #endif /* OSAL_H */
  598. /** @} */