hal_queues.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_queues.h
  15. * @brief I/O Queues macros and structures.
  16. *
  17. * @addtogroup HAL_QUEUES
  18. * @{
  19. */
  20. #ifndef HAL_QUEUES_H
  21. #define HAL_QUEUES_H
  22. /*===========================================================================*/
  23. /* Driver constants. */
  24. /*===========================================================================*/
  25. /**
  26. * @name Queue functions returned status value
  27. * @{
  28. */
  29. #define Q_OK MSG_OK /**< @brief Operation successful. */
  30. #define Q_TIMEOUT MSG_TIMEOUT /**< @brief Timeout condition. */
  31. #define Q_RESET MSG_RESET /**< @brief Queue has been reset. */
  32. #define Q_EMPTY MSG_TIMEOUT /**< @brief Queue empty. */
  33. #define Q_FULL MSG_TIMEOUT /**< @brief Queue full, */
  34. /** @} */
  35. /*===========================================================================*/
  36. /* Driver pre-compile time settings. */
  37. /*===========================================================================*/
  38. /*===========================================================================*/
  39. /* Derived constants and error checks. */
  40. /*===========================================================================*/
  41. /*===========================================================================*/
  42. /* Driver data structures and types. */
  43. /*===========================================================================*/
  44. /**
  45. * @brief Type of a generic I/O queue structure.
  46. */
  47. typedef struct io_queue io_queue_t;
  48. /**
  49. * @brief Queue notification callback type.
  50. *
  51. * @param[in] qp the queue pointer
  52. */
  53. typedef void (*qnotify_t)(io_queue_t *qp);
  54. /**
  55. * @brief Generic I/O queue structure.
  56. * @details This structure represents a generic Input or Output asymmetrical
  57. * queue. The queue is asymmetrical because one end is meant to be
  58. * accessed from a thread context, and thus can be blocking, the other
  59. * end is accessible from interrupt handlers or from within a kernel
  60. * lock zone and is non-blocking.
  61. */
  62. struct io_queue {
  63. threads_queue_t q_waiting; /**< @brief Queue of waiting threads. */
  64. volatile size_t q_counter; /**< @brief Resources counter. */
  65. uint8_t *q_buffer; /**< @brief Pointer to the queue buffer.*/
  66. uint8_t *q_top; /**< @brief Pointer to the first
  67. location after the buffer. */
  68. uint8_t *q_wrptr; /**< @brief Write pointer. */
  69. uint8_t *q_rdptr; /**< @brief Read pointer. */
  70. qnotify_t q_notify; /**< @brief Data notification callback. */
  71. void *q_link; /**< @brief Application defined field. */
  72. };
  73. /**
  74. * @extends io_queue_t
  75. *
  76. * @brief Type of an input queue structure.
  77. * @details This structure represents a generic asymmetrical input queue.
  78. * Writing to the queue is non-blocking and can be performed from
  79. * interrupt handlers or from within a kernel lock zone.
  80. * Reading the queue can be a blocking operation and is supposed to
  81. * be performed by a system thread.
  82. */
  83. typedef io_queue_t input_queue_t;
  84. /**
  85. * @extends io_queue_t
  86. *
  87. * @brief Type of an output queue structure.
  88. * @details This structure represents a generic asymmetrical output queue.
  89. * Reading from the queue is non-blocking and can be performed from
  90. * interrupt handlers or from within a kernel lock zone.
  91. * Writing the queue can be a blocking operation and is supposed to
  92. * be performed by a system thread.
  93. */
  94. typedef io_queue_t output_queue_t;
  95. /*===========================================================================*/
  96. /* Driver macros. */
  97. /*===========================================================================*/
  98. /**
  99. * @name Macro Functions
  100. * @{
  101. */
  102. /**
  103. * @brief Returns the queue's buffer size.
  104. *
  105. * @param[in] qp pointer to a @p io_queue_t structure
  106. * @return The buffer size.
  107. *
  108. * @xclass
  109. */
  110. #define qSizeX(qp) \
  111. /*lint -save -e9033 [10.8] The cast is safe.*/ \
  112. ((size_t)((qp)->q_top - (qp)->q_buffer)) \
  113. /*lint -restore*/
  114. /**
  115. * @brief Queue space.
  116. * @details Returns the used space if used on an input queue or the empty
  117. * space if used on an output queue.
  118. *
  119. * @param[in] qp pointer to a @p io_queue_t structure
  120. * @return The buffer space.
  121. *
  122. * @iclass
  123. */
  124. #define qSpaceI(qp) ((qp)->q_counter)
  125. /**
  126. * @brief Returns the queue application-defined link.
  127. * @note This function can be called in any context.
  128. *
  129. * @param[in] qp pointer to a @p io_queue_t structure
  130. * @return The application-defined link.
  131. *
  132. * @special
  133. */
  134. #define qGetLink(qp) ((qp)->q_link)
  135. /**
  136. * @brief Sets the queue application-defined link.
  137. * @note This function can be called in any context.
  138. *
  139. * @param[in] qp pointer to a @p io_queue_t structure
  140. * @param[in] lk The application-defined link.
  141. *
  142. * @special
  143. */
  144. #define qSetLink(qp, lk) ((qp)->q_link = lk)
  145. /**
  146. * @brief Returns the filled space into an input queue.
  147. *
  148. * @param[in] iqp pointer to an @p input_queue_t structure
  149. * @return The number of full bytes in the queue.
  150. * @retval 0 if the queue is empty.
  151. *
  152. * @iclass
  153. */
  154. #define iqGetFullI(iqp) qSpaceI(iqp)
  155. /**
  156. * @brief Returns the empty space into an input queue.
  157. *
  158. * @param[in] iqp pointer to an @p input_queue_t structure
  159. * @return The number of empty bytes in the queue.
  160. * @retval 0 if the queue is full.
  161. *
  162. * @iclass
  163. */
  164. #define iqGetEmptyI(iqp) (qSizeX(iqp) - qSpaceI(iqp))
  165. /**
  166. * @brief Evaluates to @p true if the specified input queue is empty.
  167. *
  168. * @param[in] iqp pointer to an @p input_queue_t structure
  169. * @return The queue status.
  170. * @retval false if the queue is not empty.
  171. * @retval true if the queue is empty.
  172. *
  173. * @iclass
  174. */
  175. #define iqIsEmptyI(iqp) ((bool)(qSpaceI(iqp) == 0U))
  176. /**
  177. * @brief Evaluates to @p true if the specified input queue is full.
  178. *
  179. * @param[in] iqp pointer to an @p input_queue_t structure
  180. * @return The queue status.
  181. * @retval false if the queue is not full.
  182. * @retval true if the queue is full.
  183. *
  184. * @iclass
  185. */
  186. #define iqIsFullI(iqp) \
  187. /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
  188. ((bool)(((iqp)->q_wrptr == (iqp)->q_rdptr) && ((iqp)->q_counter != 0U))) \
  189. /*lint -restore*/
  190. /**
  191. * @brief Input queue read.
  192. * @details This function reads a byte value from an input queue. If the queue
  193. * is empty then the calling thread is suspended until a byte arrives
  194. * in the queue.
  195. *
  196. * @param[in] iqp pointer to an @p input_queue_t structure
  197. * @return A byte value from the queue.
  198. * @retval MSG_RESET if the queue has been reset.
  199. *
  200. * @api
  201. */
  202. #define iqGet(iqp) iqGetTimeout(iqp, TIME_INFINITE)
  203. /**
  204. * @brief Returns the filled space into an output queue.
  205. *
  206. * @param[in] oqp pointer to an @p output_queue_t structure
  207. * @return The number of full bytes in the queue.
  208. * @retval 0 if the queue is empty.
  209. *
  210. * @iclass
  211. */
  212. #define oqGetFullI(oqp) (qSizeX(oqp) - qSpaceI(oqp))
  213. /**
  214. * @brief Returns the empty space into an output queue.
  215. *
  216. * @param[in] oqp pointer to an @p output_queue_t structure
  217. * @return The number of empty bytes in the queue.
  218. * @retval 0 if the queue is full.
  219. *
  220. * @iclass
  221. */
  222. #define oqGetEmptyI(oqp) qSpaceI(oqp)
  223. /**
  224. * @brief Evaluates to @p true if the specified output queue is empty.
  225. *
  226. * @param[in] oqp pointer to an @p output_queue_t structure
  227. * @return The queue status.
  228. * @retval false if the queue is not empty.
  229. * @retval true if the queue is empty.
  230. *
  231. * @iclass
  232. */
  233. #define oqIsEmptyI(oqp) \
  234. /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
  235. ((bool)(((oqp)->q_wrptr == (oqp)->q_rdptr) && ((oqp)->q_counter != 0U))) \
  236. /*lint -restore*/
  237. /**
  238. * @brief Evaluates to @p true if the specified output queue is full.
  239. *
  240. * @param[in] oqp pointer to an @p output_queue_t structure
  241. * @return The queue status.
  242. * @retval false if the queue is not full.
  243. * @retval true if the queue is full.
  244. *
  245. * @iclass
  246. */
  247. #define oqIsFullI(oqp) ((bool)(qSpaceI(oqp) == 0U))
  248. /**
  249. * @brief Output queue write.
  250. * @details This function writes a byte value to an output queue. If the queue
  251. * is full then the calling thread is suspended until there is space
  252. * in the queue.
  253. *
  254. * @param[in] oqp pointer to an @p output_queue_t structure
  255. * @param[in] b the byte value to be written in the queue
  256. * @return The operation status.
  257. * @retval MSG_OK if the operation succeeded.
  258. * @retval MSG_RESET if the queue has been reset.
  259. *
  260. * @api
  261. */
  262. #define oqPut(oqp, b) oqPutTimeout(oqp, b, TIME_INFINITE)
  263. /** @} */
  264. /*===========================================================================*/
  265. /* External declarations. */
  266. /*===========================================================================*/
  267. /** @} */
  268. #ifdef __cplusplus
  269. extern "C" {
  270. #endif
  271. void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size,
  272. qnotify_t infy, void *link);
  273. void iqResetI(input_queue_t *iqp);
  274. msg_t iqPutI(input_queue_t *iqp, uint8_t b);
  275. msg_t iqGetI(input_queue_t *iqp);
  276. msg_t iqGetTimeout(input_queue_t *iqp, sysinterval_t timeout);
  277. size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n);
  278. size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp,
  279. size_t n, sysinterval_t timeout);
  280. void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size,
  281. qnotify_t onfy, void *link);
  282. void oqResetI(output_queue_t *oqp);
  283. msg_t oqPutI(output_queue_t *oqp, uint8_t b);
  284. msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout);
  285. msg_t oqGetI(output_queue_t *oqp);
  286. size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n);
  287. size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp,
  288. size_t n, sysinterval_t timeout);
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif /* HAL_QUEUES_H */
  293. /** @} */