hal_serial.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_serial.h
  15. * @brief Serial Driver macros and structures.
  16. *
  17. * @addtogroup SERIAL
  18. * @{
  19. */
  20. #ifndef HAL_SERIAL_H
  21. #define HAL_SERIAL_H
  22. #if (HAL_USE_SERIAL == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name Serial status flags
  28. * @{
  29. */
  30. #define SD_PARITY_ERROR (eventflags_t)32 /**< @brief Parity. */
  31. #define SD_FRAMING_ERROR (eventflags_t)64 /**< @brief Framing. */
  32. #define SD_OVERRUN_ERROR (eventflags_t)128 /**< @brief Overflow. */
  33. #define SD_NOISE_ERROR (eventflags_t)256 /**< @brief Line noise. */
  34. #define SD_BREAK_DETECTED (eventflags_t)512 /**< @brief LIN Break. */
  35. #define SD_QUEUE_FULL_ERROR (eventflags_t)1024 /**< @brief Queue full. */
  36. /** @} */
  37. /*===========================================================================*/
  38. /* Driver pre-compile time settings. */
  39. /*===========================================================================*/
  40. /**
  41. * @name Serial configuration options
  42. * @{
  43. */
  44. /**
  45. * @brief Default bit rate.
  46. * @details Configuration parameter, this is the baud rate selected for the
  47. * default configuration.
  48. */
  49. #if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
  50. #define SERIAL_DEFAULT_BITRATE 38400
  51. #endif
  52. /**
  53. * @brief Serial buffers size.
  54. * @details Configuration parameter, you can change the depth of the queue
  55. * buffers depending on the requirements of your application.
  56. * @note The default is 16 bytes for both the transmission and receive
  57. * buffers.
  58. * @note This is a global setting and it can be overridden by low level
  59. * driver specific settings.
  60. */
  61. #if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__)
  62. #define SERIAL_BUFFERS_SIZE 16
  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. SD_UNINIT = 0, /**< Not initialized. */
  76. SD_STOP = 1, /**< Stopped. */
  77. SD_READY = 2 /**< Ready. */
  78. } sdstate_t;
  79. /**
  80. * @brief Structure representing a serial driver.
  81. */
  82. typedef struct SerialDriver SerialDriver;
  83. #include "hal_serial_lld.h"
  84. /**
  85. * @brief @p SerialDriver specific methods.
  86. */
  87. #define _serial_driver_methods \
  88. _base_asynchronous_channel_methods
  89. /**
  90. * @extends BaseAsynchronousChannelVMT
  91. *
  92. * @brief @p SerialDriver virtual methods table.
  93. */
  94. struct SerialDriverVMT {
  95. _serial_driver_methods
  96. };
  97. /**
  98. * @extends BaseAsynchronousChannel
  99. *
  100. * @brief Full duplex serial driver class.
  101. * @details This class extends @p BaseAsynchronousChannel by adding physical
  102. * I/O queues.
  103. */
  104. struct SerialDriver {
  105. /** @brief Virtual Methods Table.*/
  106. const struct SerialDriverVMT *vmt;
  107. _serial_driver_data
  108. };
  109. /*===========================================================================*/
  110. /* Driver macros. */
  111. /*===========================================================================*/
  112. /**
  113. * @name Macro Functions
  114. * @{
  115. */
  116. /**
  117. * @brief Direct write to a @p SerialDriver.
  118. * @note This function bypasses the indirect access to the channel and
  119. * writes directly on the output queue. This is faster but cannot
  120. * be used to write to different channels implementations.
  121. *
  122. * @iclass
  123. */
  124. #define sdPutI(sdp, b) oqPutI(&(sdp)->oqueue, b)
  125. /**
  126. * @brief Direct write to a @p SerialDriver.
  127. * @note This function bypasses the indirect access to the channel and
  128. * writes directly on the output queue. This is faster but cannot
  129. * be used to write to different channels implementations.
  130. *
  131. * @api
  132. */
  133. #define sdPut(sdp, b) oqPut(&(sdp)->oqueue, b)
  134. /**
  135. * @brief Direct write to a @p SerialDriver with timeout specification.
  136. * @note This function bypasses the indirect access to the channel and
  137. * writes directly on the output queue. This is faster but cannot
  138. * be used to write to different channels implementations.
  139. *
  140. * @api
  141. */
  142. #define sdPutTimeout(sdp, b, t) oqPutTimeout(&(sdp)->oqueue, b, t)
  143. /**
  144. * @brief Direct read from a @p SerialDriver.
  145. * @note This function bypasses the indirect access to the channel and
  146. * reads directly from the input queue. This is faster but cannot
  147. * be used to read from different channels implementations.
  148. *
  149. * @iclass
  150. */
  151. #define sdGetI(sdp) iqGetI(&(sdp)->iqueue)
  152. /**
  153. * @brief Direct read from a @p SerialDriver.
  154. * @note This function bypasses the indirect access to the channel and
  155. * reads directly from the input queue. This is faster but cannot
  156. * be used to read from different channels implementations.
  157. *
  158. * @api
  159. */
  160. #define sdGet(sdp) iqGet(&(sdp)->iqueue)
  161. /**
  162. * @brief Direct read from a @p SerialDriver with timeout specification.
  163. * @note This function bypasses the indirect access to the channel and
  164. * reads directly from the input queue. This is faster but cannot
  165. * be used to read from different channels implementations.
  166. *
  167. * @api
  168. */
  169. #define sdGetTimeout(sdp, t) iqGetTimeout(&(sdp)->iqueue, t)
  170. /**
  171. * @brief Direct blocking write to a @p SerialDriver.
  172. * @note This function bypasses the indirect access to the channel and
  173. * writes directly to the output queue. This is faster but cannot
  174. * be used to write from different channels implementations.
  175. *
  176. * @iclass
  177. */
  178. #define sdWriteI(sdp, b, n) oqWriteI(&(sdp)->oqueue, b, n)
  179. /**
  180. * @brief Direct blocking write to a @p SerialDriver.
  181. * @note This function bypasses the indirect access to the channel and
  182. * writes directly to the output queue. This is faster but cannot
  183. * be used to write from different channels implementations.
  184. *
  185. * @api
  186. */
  187. #define sdWrite(sdp, b, n) oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE)
  188. /**
  189. * @brief Direct blocking write to a @p SerialDriver with timeout
  190. * specification.
  191. * @note This function bypasses the indirect access to the channel and
  192. * writes directly to the output queue. This is faster but cannot
  193. * be used to write to different channels implementations.
  194. *
  195. * @api
  196. */
  197. #define sdWriteTimeout(sdp, b, n, t) \
  198. oqWriteTimeout(&(sdp)->oqueue, b, n, t)
  199. /**
  200. * @brief Direct non-blocking write to a @p SerialDriver.
  201. * @note This function bypasses the indirect access to the channel and
  202. * writes directly to the output queue. This is faster but cannot
  203. * be used to write to different channels implementations.
  204. *
  205. * @api
  206. */
  207. #define sdAsynchronousWrite(sdp, b, n) \
  208. oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE)
  209. /**
  210. * @brief Direct blocking read from a @p SerialDriver.
  211. * @note This function bypasses the indirect access to the channel and
  212. * reads directly from the input queue. This is faster but cannot
  213. * be used to read from different channels implementations.
  214. *
  215. * @iclass
  216. */
  217. #define sdReadI(sdp, b, n) iqReadI(&(sdp)->iqueue, b, n, TIME_INFINITE)
  218. /**
  219. * @brief Direct blocking read from a @p SerialDriver.
  220. * @note This function bypasses the indirect access to the channel and
  221. * reads directly from the input queue. This is faster but cannot
  222. * be used to read from different channels implementations.
  223. *
  224. * @api
  225. */
  226. #define sdRead(sdp, b, n) iqReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE)
  227. /**
  228. * @brief Direct blocking read from a @p SerialDriver with timeout
  229. * specification.
  230. * @note This function bypasses the indirect access to the channel and
  231. * reads directly from the input queue. This is faster but cannot
  232. * be used to read from different channels implementations.
  233. *
  234. * @api
  235. */
  236. #define sdReadTimeout(sdp, b, n, t) iqReadTimeout(&(sdp)->iqueue, b, n, t)
  237. /**
  238. * @brief Direct non-blocking read from a @p SerialDriver.
  239. * @note This function bypasses the indirect access to the channel and
  240. * reads directly from the input queue. This is faster but cannot
  241. * be used to read from different channels implementations.
  242. *
  243. * @api
  244. */
  245. #define sdAsynchronousRead(sdp, b, n) \
  246. iqReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE)
  247. /** @} */
  248. /*===========================================================================*/
  249. /* External declarations. */
  250. /*===========================================================================*/
  251. #ifdef __cplusplus
  252. extern "C" {
  253. #endif
  254. void sdInit(void);
  255. #if !defined(SERIAL_ADVANCED_BUFFERING_SUPPORT) || \
  256. (SERIAL_ADVANCED_BUFFERING_SUPPORT == FALSE)
  257. void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify);
  258. #else
  259. void sdObjectInit(SerialDriver *sdp);
  260. #endif
  261. void sdStart(SerialDriver *sdp, const SerialConfig *config);
  262. void sdStop(SerialDriver *sdp);
  263. void sdIncomingDataI(SerialDriver *sdp, uint8_t b);
  264. msg_t sdRequestDataI(SerialDriver *sdp);
  265. bool sdPutWouldBlock(SerialDriver *sdp);
  266. bool sdGetWouldBlock(SerialDriver *sdp);
  267. msg_t sdControl(SerialDriver *sdp, unsigned int operation, void *arg);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* HAL_USE_SERIAL == TRUE */
  272. #endif /* HAL_SERIAL_H */
  273. /** @} */