hal_uart.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 hal_uart.h
  15. * @brief UART Driver macros and structures.
  16. *
  17. * @addtogroup UART
  18. * @{
  19. */
  20. #ifndef HAL_UART_H
  21. #define HAL_UART_H
  22. #if (HAL_USE_UART == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name UART status flags
  28. * @{
  29. */
  30. #define UART_NO_ERROR 0 /**< @brief No pending conditions. */
  31. #define UART_PARITY_ERROR 4 /**< @brief Parity error happened. */
  32. #define UART_FRAMING_ERROR 8 /**< @brief Framing error happened. */
  33. #define UART_OVERRUN_ERROR 16 /**< @brief Overflow happened. */
  34. #define UART_NOISE_ERROR 32 /**< @brief Noise on the line. */
  35. #define UART_BREAK_DETECTED 64 /**< @brief Break detected. */
  36. /** @} */
  37. /**
  38. * @name UART error conditions
  39. * @{
  40. */
  41. #define UART_ERR_NOT_ACTIVE (size_t)-1
  42. /** @} */
  43. /*===========================================================================*/
  44. /* Driver pre-compile time settings. */
  45. /*===========================================================================*/
  46. /**
  47. * @name UART configuration options
  48. * @{
  49. */
  50. /**
  51. * @brief Enables synchronous APIs.
  52. * @note Disabling this option saves both code and data space.
  53. */
  54. #if !defined(UART_USE_WAIT) || defined(__DOXYGEN__)
  55. #define UART_USE_WAIT FALSE
  56. #endif
  57. /**
  58. * @brief Enables the @p uartAcquireBus() and @p uartReleaseBus() APIs.
  59. * @note Disabling this option saves both code and data space.
  60. */
  61. #if !defined(UART_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
  62. #define UART_USE_MUTUAL_EXCLUSION FALSE
  63. #endif
  64. /** @} */
  65. /*===========================================================================*/
  66. /* Derived constants and error checks. */
  67. /*===========================================================================*/
  68. /*===========================================================================*/
  69. /* Driver data structures and types. */
  70. /*===========================================================================*/
  71. /**
  72. * @brief Driver state machine possible states.
  73. */
  74. typedef enum {
  75. UART_UNINIT = 0, /**< Not initialized. */
  76. UART_STOP = 1, /**< Stopped. */
  77. UART_READY = 2 /**< Ready. */
  78. } uartstate_t;
  79. /**
  80. * @brief Transmitter state machine states.
  81. */
  82. typedef enum {
  83. UART_TX_IDLE = 0, /**< Not transmitting. */
  84. UART_TX_ACTIVE = 1, /**< Transmitting. */
  85. UART_TX_COMPLETE = 2 /**< Buffer complete. */
  86. } uarttxstate_t;
  87. /**
  88. * @brief Receiver state machine states.
  89. */
  90. typedef enum {
  91. UART_RX_IDLE = 0, /**< Not receiving. */
  92. UART_RX_ACTIVE = 1, /**< Receiving. */
  93. UART_RX_COMPLETE = 2 /**< Buffer complete. */
  94. } uartrxstate_t;
  95. #include "hal_uart_lld.h"
  96. /*===========================================================================*/
  97. /* Driver macros. */
  98. /*===========================================================================*/
  99. /**
  100. * @name Low level driver helper macros
  101. * @{
  102. */
  103. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  104. /**
  105. * @brief Wakes up the waiting thread in case of early TX complete.
  106. *
  107. * @param[in] uartp pointer to the @p UARTDriver object
  108. *
  109. * @notapi
  110. */
  111. #define _uart_wakeup_tx1_isr(uartp) { \
  112. if ((uartp)->early == true) { \
  113. osalSysLockFromISR(); \
  114. osalThreadResumeI(&(uartp)->threadtx, MSG_OK); \
  115. osalSysUnlockFromISR(); \
  116. } \
  117. }
  118. #else /* !UART_USE_WAIT */
  119. #define _uart_wakeup_tx1_isr(uartp)
  120. #endif /* !UART_USE_WAIT */
  121. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  122. /**
  123. * @brief Wakes up the waiting thread in case of late TX complete.
  124. *
  125. * @param[in] uartp pointer to the @p UARTDriver object
  126. *
  127. * @notapi
  128. */
  129. #define _uart_wakeup_tx2_isr(uartp) { \
  130. if ((uartp)->early == false) { \
  131. osalSysLockFromISR(); \
  132. osalThreadResumeI(&(uartp)->threadtx, MSG_OK); \
  133. osalSysUnlockFromISR(); \
  134. } \
  135. }
  136. #else /* !UART_USE_WAIT */
  137. #define _uart_wakeup_tx2_isr(uartp)
  138. #endif /* !UART_USE_WAIT */
  139. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  140. /**
  141. * @brief Wakes up the waiting thread in case of RX complete.
  142. *
  143. * @param[in] uartp pointer to the @p UARTDriver object
  144. *
  145. * @notapi
  146. */
  147. #define _uart_wakeup_rx_complete_isr(uartp) { \
  148. osalSysLockFromISR(); \
  149. osalThreadResumeI(&(uartp)->threadrx, MSG_OK); \
  150. osalSysUnlockFromISR(); \
  151. }
  152. #else /* !UART_USE_WAIT */
  153. #define _uart_wakeup_rx_complete_isr(uartp)
  154. #endif /* !UART_USE_WAIT */
  155. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  156. /**
  157. * @brief Wakes up the waiting thread in case of RX error.
  158. *
  159. * @param[in] uartp pointer to the @p UARTDriver object
  160. *
  161. * @notapi
  162. */
  163. #define _uart_wakeup_rx_error_isr(uartp) { \
  164. osalSysLockFromISR(); \
  165. osalThreadResumeI(&(uartp)->threadrx, MSG_RESET); \
  166. osalSysUnlockFromISR(); \
  167. }
  168. #else /* !UART_USE_WAIT */
  169. #define _uart_wakeup_rx_error_isr(uartp)
  170. #endif /* !UART_USE_WAIT */
  171. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  172. /**
  173. * @brief Wakes up the waiting thread in case of RX character match.
  174. *
  175. * @param[in] uartp pointer to the @p UARTDriver object
  176. *
  177. * @notapi
  178. */
  179. #define _uart_wakeup_rx_cm_isr(uartp) { \
  180. osalSysLockFromISR(); \
  181. osalThreadResumeI(&(uartp)->threadrx, MSG_TIMEOUT); \
  182. osalSysUnlockFromISR(); \
  183. }
  184. #else /* !UART_USE_WAIT */
  185. #define _uart_wakeup_rx_cm_isr(uartp)
  186. #endif /* !UART_USE_WAIT */
  187. #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  188. /**
  189. * @brief Wakes up the waiting thread in case of RX timeout.
  190. *
  191. * @param[in] uartp pointer to the @p UARTDriver object
  192. *
  193. * @notapi
  194. */
  195. #define _uart_wakeup_rx_timeout_isr(uartp) { \
  196. osalSysLockFromISR(); \
  197. osalThreadResumeI(&(uartp)->threadrx, MSG_TIMEOUT); \
  198. osalSysUnlockFromISR(); \
  199. }
  200. #else /* !UART_USE_WAIT */
  201. #define _uart_wakeup_rx_timeout_isr(uartp)
  202. #endif /* !UART_USE_WAIT */
  203. /**
  204. * @brief Common ISR code for early TX.
  205. * @details This code handles the portable part of the ISR code:
  206. * - Callback invocation.
  207. * - Waiting thread wakeup, if any.
  208. * - Driver state transitions.
  209. * .
  210. * @note This macro is meant to be used in the low level drivers
  211. * implementation only.
  212. *
  213. * @param[in] uartp pointer to the @p UARTDriver object
  214. *
  215. * @notapi
  216. */
  217. #define _uart_tx1_isr_code(uartp) { \
  218. (uartp)->txstate = UART_TX_COMPLETE; \
  219. if ((uartp)->config->txend1_cb != NULL) { \
  220. (uartp)->config->txend1_cb(uartp); \
  221. } \
  222. if ((uartp)->txstate == UART_TX_COMPLETE) { \
  223. (uartp)->txstate = UART_TX_IDLE; \
  224. } \
  225. _uart_wakeup_tx1_isr(uartp); \
  226. }
  227. /**
  228. * @brief Common ISR code for late TX.
  229. * @details This code handles the portable part of the ISR code:
  230. * - Callback invocation.
  231. * - Waiting thread wakeup, if any.
  232. * - Driver state transitions.
  233. * .
  234. * @note This macro is meant to be used in the low level drivers
  235. * implementation only.
  236. *
  237. * @param[in] uartp pointer to the @p UARTDriver object
  238. *
  239. * @notapi
  240. */
  241. #define _uart_tx2_isr_code(uartp) { \
  242. if ((uartp)->config->txend2_cb != NULL) { \
  243. (uartp)->config->txend2_cb(uartp); \
  244. } \
  245. _uart_wakeup_tx2_isr(uartp); \
  246. }
  247. /**
  248. * @brief Common ISR code for RX complete.
  249. * @details This code handles the portable part of the ISR code:
  250. * - Callback invocation.
  251. * - Waiting thread wakeup, if any.
  252. * - Driver state transitions.
  253. * .
  254. * @note This macro is meant to be used in the low level drivers
  255. * implementation only.
  256. *
  257. * @param[in] uartp pointer to the @p UARTDriver object
  258. *
  259. * @notapi
  260. */
  261. #define _uart_rx_complete_isr_code(uartp) { \
  262. (uartp)->rxstate = UART_RX_COMPLETE; \
  263. if ((uartp)->config->rxend_cb != NULL) { \
  264. (uartp)->config->rxend_cb(uartp); \
  265. } \
  266. if ((uartp)->rxstate == UART_RX_COMPLETE) { \
  267. (uartp)->rxstate = UART_RX_IDLE; \
  268. uart_enter_rx_idle_loop(uartp); \
  269. } \
  270. _uart_wakeup_rx_complete_isr(uartp); \
  271. }
  272. /**
  273. * @brief Common ISR code for RX error.
  274. * @details This code handles the portable part of the ISR code:
  275. * - Callback invocation.
  276. * - Waiting thread wakeup, if any.
  277. * - Driver state transitions.
  278. * .
  279. * @note This macro is meant to be used in the low level drivers
  280. * implementation only.
  281. *
  282. * @param[in] uartp pointer to the @p UARTDriver object
  283. * @param[in] errors mask of errors to be reported
  284. *
  285. * @notapi
  286. */
  287. #define _uart_rx_error_isr_code(uartp, errors) { \
  288. if ((uartp)->config->rxerr_cb != NULL) { \
  289. (uartp)->config->rxerr_cb(uartp, errors); \
  290. } \
  291. _uart_wakeup_rx_error_isr(uartp); \
  292. }
  293. /**
  294. * @brief Common ISR code for RX on idle.
  295. * @details This code handles the portable part of the ISR code:
  296. * - Callback invocation.
  297. * - Waiting thread wakeup, if any.
  298. * - Driver state transitions.
  299. * .
  300. * @note This macro is meant to be used in the low level drivers
  301. * implementation only.
  302. *
  303. * @param[in] uartp pointer to the @p UARTDriver object
  304. *
  305. * @notapi
  306. */
  307. #define _uart_rx_idle_code(uartp) { \
  308. if ((uartp)->config->rxchar_cb != NULL) \
  309. (uartp)->config->rxchar_cb(uartp, (uartp)->rxbuf); \
  310. }
  311. /**
  312. * @brief Timeout ISR code for receiver.
  313. * @details This code handles the portable part of the ISR code:
  314. * - Callback invocation.
  315. * - Waiting thread wakeup, if any.
  316. * - Driver state transitions.
  317. * .
  318. * @note This macro is meant to be used in the low level drivers
  319. * implementation only.
  320. *
  321. * @param[in] uartp pointer to the @p UARTDriver object
  322. *
  323. * @notapi
  324. */
  325. #define _uart_timeout_isr_code(uartp) { \
  326. if ((uartp)->config->timeout_cb != NULL) { \
  327. (uartp)->config->timeout_cb(uartp); \
  328. } \
  329. _uart_wakeup_rx_timeout_isr(uartp); \
  330. }
  331. /**
  332. * @brief Character match ISR code for receiver.
  333. * @details This code handles the portable part of the ISR code:
  334. * - Callback invocation.
  335. * - Waiting thread wakeup, if any.
  336. * - Driver state transitions.
  337. * .
  338. * @note This macro is meant to be used in the low level drivers
  339. * implementation only.
  340. *
  341. * @param[in] uartp pointer to the @p UARTDriver object
  342. *
  343. * @notapi
  344. */
  345. #define _uart_rx_char_match_isr_code(uartp) { \
  346. if ((uartp)->config->rx_cm_cb != NULL) { \
  347. (uartp)->config->rx_cm_cb(uartp); \
  348. } \
  349. _uart_wakeup_rx_cm_isr(uartp); \
  350. }
  351. /** @} */
  352. /*===========================================================================*/
  353. /* External declarations. */
  354. /*===========================================================================*/
  355. #ifdef __cplusplus
  356. extern "C" {
  357. #endif
  358. void uartInit(void);
  359. void uartObjectInit(UARTDriver *uartp);
  360. void uartStart(UARTDriver *uartp, const UARTConfig *config);
  361. void uartStop(UARTDriver *uartp);
  362. void uartStartSend(UARTDriver *uartp, size_t n, const void *txbuf);
  363. void uartStartSendI(UARTDriver *uartp, size_t n, const void *txbuf);
  364. size_t uartStopSend(UARTDriver *uartp);
  365. size_t uartStopSendI(UARTDriver *uartp);
  366. void uartStartReceive(UARTDriver *uartp, size_t n, void *rxbuf);
  367. void uartStartReceiveI(UARTDriver *uartp, size_t n, void *rxbuf);
  368. size_t uartStopReceive(UARTDriver *uartp);
  369. size_t uartStopReceiveI(UARTDriver *uartp);
  370. #if UART_USE_WAIT == TRUE
  371. msg_t uartSendTimeout(UARTDriver *uartp, size_t *np,
  372. const void *txbuf, sysinterval_t timeout);
  373. msg_t uartSendFullTimeout(UARTDriver *uartp, size_t *np,
  374. const void *txbuf, sysinterval_t timeout);
  375. msg_t uartReceiveTimeout(UARTDriver *uartp, size_t *np,
  376. void *rxbuf, sysinterval_t timeout);
  377. #endif
  378. #if UART_USE_MUTUAL_EXCLUSION == TRUE
  379. void uartAcquireBus(UARTDriver *uartp);
  380. void uartReleaseBus(UARTDriver *uartp);
  381. #endif
  382. #ifdef __cplusplus
  383. }
  384. #endif
  385. #endif /* HAL_USE_UART == TRUE */
  386. #endif /* HAL_UART_H */
  387. /** @} */