osal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 <avr/io.h>
  26. #include <avr/interrupt.h>
  27. #include "osalconf.h"
  28. /*===========================================================================*/
  29. /* Module constants. */
  30. /*===========================================================================*/
  31. /**
  32. * @name Common constants
  33. * @{
  34. */
  35. #if !defined(FALSE) || defined(__DOXYGEN__)
  36. #define FALSE 0
  37. #endif
  38. #if !defined(TRUE) || defined(__DOXYGEN__)
  39. #define TRUE 1
  40. #endif
  41. #define OSAL_SUCCESS false
  42. #define OSAL_FAILED true
  43. /** @} */
  44. /**
  45. * @name Messages
  46. * @{
  47. */
  48. #define MSG_OK (msg_t)0
  49. #define MSG_RESET (msg_t)-1
  50. #define MSG_TIMEOUT (msg_t)-2
  51. #define MSG_WAIT (msg_t)-10
  52. /** @} */
  53. /**
  54. * @name Special time constants
  55. * @{
  56. */
  57. #define TIME_IMMEDIATE ((systime_t)0)
  58. #define TIME_INFINITE ((systime_t)-1)
  59. /** @} */
  60. /**
  61. * @name Systick modes.
  62. * @{
  63. */
  64. #define OSAL_ST_MODE_NONE 0
  65. #define OSAL_ST_MODE_PERIODIC 1
  66. #define OSAL_ST_MODE_FREERUNNING 2
  67. /** @} */
  68. /**
  69. * @name Systick parameters.
  70. * @{
  71. */
  72. /**
  73. * @brief Size in bits of the @p systick_t type.
  74. */
  75. #define OSAL_ST_RESOLUTION 32
  76. /**
  77. * @brief Systick mode required by the underlying OS.
  78. */
  79. #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
  80. /** @} */
  81. /**
  82. * @brief ROM constant modifier.
  83. * @note It is set to use the "const" keyword in this port.
  84. */
  85. #define ROMCONST const
  86. /*===========================================================================*/
  87. /* Module pre-compile time settings. */
  88. /*===========================================================================*/
  89. /**
  90. * @brief Frequency in Hertz of the system tick.
  91. */
  92. #if !defined(OSAL_ST_FREQUENCY) || defined(__DOXYGEN__)
  93. #define OSAL_ST_FREQUENCY 1000
  94. #endif
  95. /**
  96. * @brief Enables OSAL assertions.
  97. */
  98. #if !defined(OSAL_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
  99. #define OSAL_DBG_ENABLE_ASSERTS FALSE
  100. #endif
  101. /**
  102. * @brief Enables OSAL functions parameters checks.
  103. */
  104. #if !defined(OSAL_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__)
  105. #define OSAL_DBG_ENABLE_CHECKS FALSE
  106. #endif
  107. /**
  108. * @brief OSAL initialization hook.
  109. */
  110. #if !defined(OSAL_INIT_HOOK) || defined(__DOXYGEN__)
  111. #define OSAL_INIT_HOOK()
  112. #endif
  113. /**
  114. * @brief Idle loop hook macro.
  115. */
  116. #if !defined(OSAL_IDLE_HOOK) || defined(__DOXYGEN__)
  117. #define OSAL_IDLE_HOOK()
  118. #endif
  119. /*===========================================================================*/
  120. /* Derived constants and error checks. */
  121. /*===========================================================================*/
  122. /*===========================================================================*/
  123. /* Module data structures and types. */
  124. /*===========================================================================*/
  125. /**
  126. * @brief Type of a system status word.
  127. */
  128. typedef uint8_t syssts_t;
  129. /**
  130. * @brief Type of a message.
  131. */
  132. typedef int16_t msg_t;
  133. /**
  134. * @brief Type of system time counter.
  135. */
  136. typedef uint32_t systime_t;
  137. /**
  138. * @brief Type of realtime counter.
  139. */
  140. typedef uint32_t rtcnt_t;
  141. /**
  142. * @brief Type of a thread.
  143. * @note The content of this structure is not part of the API and should
  144. * not be relied upon. Implementers may define this structure in
  145. * an entirely different way.
  146. */
  147. typedef struct {
  148. volatile msg_t message;
  149. } thread_t;
  150. /**
  151. * @brief Type of a thread reference.
  152. */
  153. typedef thread_t * thread_reference_t;
  154. /**
  155. * @brief Type of an event flags object.
  156. * @note The content of this structure is not part of the API and should
  157. * not be relied upon. Implementers may define this structure in
  158. * an entirely different way.
  159. * @note Retrieval and clearing of the flags are not defined in this
  160. * API and are implementation-dependent.
  161. */
  162. typedef struct event_source event_source_t;
  163. /**
  164. * @brief Type of an event source callback.
  165. * @note This type is not part of the OSAL API and is provided
  166. * exclusively as an example and for convenience.
  167. */
  168. typedef void (*eventcallback_t)(event_source_t *esp);
  169. /**
  170. * @brief Type of an event flags mask.
  171. */
  172. typedef uint8_t eventflags_t;
  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 uint8_t mutex_t;
  192. /**
  193. * @brief Type of a thread queue.
  194. * @details A thread queue is a queue of sleeping threads, queued threads
  195. * can be dequeued one at time or all together.
  196. * @note If the OSAL is implemented on a bare metal machine without RTOS
  197. * then the queue can be implemented as a single thread reference.
  198. */
  199. typedef struct {
  200. thread_reference_t tr;
  201. } threads_queue_t;
  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) do { \
  224. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  225. if (OSAL_DBG_ENABLE_ASSERTS != FALSE) { \
  226. if (!(c)) { \
  227. /*lint -restore*/ \
  228. osalSysHalt(__func__); \
  229. } \
  230. } \
  231. } while (false)
  232. /**
  233. * @brief Function parameters check.
  234. * @details If the condition check fails then the OSAL panics and halts.
  235. * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
  236. * is enabled.
  237. *
  238. * @param[in] c the condition to be verified to be true
  239. *
  240. * @api
  241. */
  242. #define osalDbgCheck(c) do { \
  243. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  244. if (OSAL_DBG_ENABLE_CHECKS != FALSE) { \
  245. if (!(c)) { \
  246. /*lint -restore*/ \
  247. osalSysHalt(__func__); \
  248. } \
  249. } \
  250. } while (false)
  251. /**
  252. * @brief I-Class state check.
  253. * @note Implementation is optional.
  254. */
  255. #define osalDbgCheckClassI()
  256. /**
  257. * @brief S-Class state check.
  258. * @note Implementation is optional.
  259. */
  260. #define osalDbgCheckClassS()
  261. /** @} */
  262. /**
  263. * @name IRQ service routines wrappers
  264. * @{
  265. */
  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) ISR(id)
  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] sec number of seconds
  294. * @return The number of ticks.
  295. *
  296. * @api
  297. */
  298. #define OSAL_S2ST(sec) \
  299. ((systime_t)((uint32_t)(sec) * (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] msec number of milliseconds
  306. * @return The number of ticks.
  307. *
  308. * @api
  309. */
  310. #define OSAL_MS2ST(msec) \
  311. ((systime_t)((((uint32_t)(msec)) * \
  312. ((uint32_t)OSAL_ST_FREQUENCY) + 999UL) / 1000UL))
  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] usec number of microseconds
  319. * @return The number of ticks.
  320. *
  321. * @api
  322. */
  323. #define OSAL_US2ST(usec) \
  324. ((systime_t)((((uint32_t)(usec)) * \
  325. ((uint32_t)OSAL_ST_FREQUENCY) + 999999UL) / 1000000UL))
  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] sec time in seconds, must be different from zero
  381. *
  382. * @api
  383. */
  384. #define osalThreadSleepSeconds(sec) osalThreadSleep(OSAL_S2ST(sec))
  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] msec time in milliseconds, must be different from zero
  393. *
  394. * @api
  395. */
  396. #define osalThreadSleepMilliseconds(msec) osalThreadSleep(OSAL_MS2ST(msec))
  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] usec time in microseconds, must be different from zero
  405. *
  406. * @api
  407. */
  408. #define osalThreadSleepMicroseconds(usec) osalThreadSleep(OSAL_US2ST(usec))
  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(systime_t time);
  424. void osalThreadSleep(systime_t time);
  425. msg_t osalThreadSuspendS(thread_reference_t *trp);
  426. msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, systime_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, systime_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. asm volatile ("cli" : : : "memory");
  452. }
  453. /**
  454. * @brief Enables interrupts globally.
  455. *
  456. * @special
  457. */
  458. static inline void osalSysEnable(void) {
  459. asm volatile ("sei" : : : "memory");
  460. asm volatile ("nop");
  461. }
  462. /**
  463. * @brief Enters a critical zone from thread context.
  464. * @note This function cannot be used for reentrant critical zones.
  465. *
  466. * @special
  467. */
  468. static inline void osalSysLock(void) {
  469. asm volatile ("cli" : : : "memory");
  470. }
  471. /**
  472. * @brief Leaves a critical zone from thread context.
  473. * @note This function cannot be used for reentrant critical zones.
  474. *
  475. * @special
  476. */
  477. static inline void osalSysUnlock(void) {
  478. asm volatile ("sei" : : : "memory");
  479. asm volatile ("nop");
  480. }
  481. /**
  482. * @brief Enters a critical zone from ISR context.
  483. * @note This function cannot be used for reentrant critical zones.
  484. * @note This function is empty in this port.
  485. *
  486. * @special
  487. */
  488. static inline void osalSysLockFromISR(void) {
  489. }
  490. /**
  491. * @brief Leaves a critical zone from ISR context.
  492. * @note This function cannot be used for reentrant critical zones.
  493. * @note This function is empty in this port.
  494. *
  495. * @special
  496. */
  497. static inline void osalSysUnlockFromISR(void) {
  498. }
  499. /**
  500. * @brief Returns the execution status and enters a critical zone.
  501. * @details This functions enters into a critical zone and can be called
  502. * from any context. Because its flexibility it is less efficient
  503. * than @p chSysLock() which is preferable when the calling context
  504. * is known.
  505. * @post The system is in a critical zone.
  506. *
  507. * @return The previous system status, the encoding of this
  508. * status word is architecture-dependent and opaque.
  509. *
  510. * @xclass
  511. */
  512. static inline syssts_t osalSysGetStatusAndLockX(void) {
  513. syssts_t sts;
  514. sts = SREG;
  515. asm volatile ("cli" : : : "memory");
  516. return sts;
  517. }
  518. /**
  519. * @brief Restores the specified execution status and leaves a critical zone.
  520. * @note A call to @p chSchRescheduleS() is automatically performed
  521. * if exiting the critical zone and if not in ISR context.
  522. *
  523. * @param[in] sts the system status to be restored.
  524. *
  525. * @xclass
  526. */
  527. static inline void osalSysRestoreStatusX(syssts_t sts) {
  528. if ((sts & 0x80) != 0) {
  529. asm volatile ("sei" : : : "memory");
  530. asm volatile ("nop");
  531. }
  532. }
  533. /**
  534. * @brief Checks if the specified time is within the specified time window.
  535. * @note When start==end then the function returns always true because the
  536. * whole time range is specified.
  537. * @note This function can be called from any context.
  538. *
  539. * @param[in] time the time to be verified
  540. * @param[in] start the start of the time window (inclusive)
  541. * @param[in] end the end of the time window (non inclusive)
  542. * @retval true current time within the specified time window.
  543. * @retval false current time not within the specified time window.
  544. *
  545. * @xclass
  546. */
  547. static inline bool osalOsIsTimeWithinX(systime_t time,
  548. systime_t start,
  549. systime_t end) {
  550. return (bool)((time - start) < (end - start));
  551. }
  552. /**
  553. * @brief Initializes a threads queue object.
  554. *
  555. * @param[out] tqp pointer to the threads queue object
  556. *
  557. * @init
  558. */
  559. static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
  560. osalDbgCheck(tqp != NULL);
  561. (void)tqp;
  562. }
  563. /**
  564. * @brief Initializes an event flags object.
  565. *
  566. * @param[out] esp pointer to the event flags object
  567. *
  568. * @init
  569. */
  570. static inline void osalEventObjectInit(event_source_t *esp) {
  571. osalDbgCheck(esp != NULL);
  572. esp->flags = (eventflags_t)0;
  573. esp->cb = NULL;
  574. esp->param = NULL;
  575. }
  576. /**
  577. * @brief Initializes s @p mutex_t object.
  578. *
  579. * @param[out] mp pointer to the @p mutex_t object
  580. *
  581. * @init
  582. */
  583. static inline void osalMutexObjectInit(mutex_t *mp) {
  584. osalDbgCheck(mp != NULL);
  585. *mp = 0;
  586. }
  587. #endif /* OSAL_H */
  588. /** @} */