hal_buffers.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_buffers.h
  15. * @brief I/O Buffers macros and structures.
  16. *
  17. * @addtogroup HAL_BUFFERS
  18. * @{
  19. */
  20. #ifndef HAL_BUFFERS_H
  21. #define HAL_BUFFERS_H
  22. /*===========================================================================*/
  23. /* Driver constants. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver pre-compile time settings. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Maximum size of blocks copied in critical sections.
  30. * @note Increasing this value increases performance at expense of
  31. * IRQ servicing efficiency.
  32. * @note It must be a power of two.
  33. */
  34. #if !defined(BUFFERS_CHUNKS_SIZE) || defined(__DOXYGEN__)
  35. #define BUFFERS_CHUNKS_SIZE 64
  36. #endif
  37. /*===========================================================================*/
  38. /* Derived constants and error checks. */
  39. /*===========================================================================*/
  40. /*lint -save -e9027 [10.1] It is meant to be this way, not an error.*/
  41. #if (BUFFERS_CHUNKS_SIZE & (BUFFERS_CHUNKS_SIZE - 1)) != 0
  42. /*lint -restore*/
  43. #error "BUFFERS_CHUNKS_SIZE must be a power of two"
  44. #endif
  45. /*===========================================================================*/
  46. /* Driver data structures and types. */
  47. /*===========================================================================*/
  48. /**
  49. * @brief Type of a generic queue of buffers.
  50. */
  51. typedef struct io_buffers_queue io_buffers_queue_t;
  52. /**
  53. * @brief Double buffer notification callback type.
  54. *
  55. * @param[in] iodbp the buffers queue pointer
  56. */
  57. typedef void (*bqnotify_t)(io_buffers_queue_t *bqp);
  58. /**
  59. * @brief Structure of a generic buffers queue.
  60. */
  61. struct io_buffers_queue {
  62. /**
  63. * @brief Queue of waiting threads.
  64. */
  65. threads_queue_t waiting;
  66. /**
  67. * @brief Queue suspended state flag.
  68. */
  69. bool suspended;
  70. /**
  71. * @brief Active buffers counter.
  72. */
  73. volatile size_t bcounter;
  74. /**
  75. * @brief Buffer write pointer.
  76. */
  77. uint8_t *bwrptr;
  78. /**
  79. * @brief Buffer read pointer.
  80. */
  81. uint8_t *brdptr;
  82. /**
  83. * @brief Pointer to the buffers boundary.
  84. */
  85. uint8_t *btop;
  86. /**
  87. * @brief Size of buffers.
  88. * @note The buffer size must be not lower than <tt>sizeof(size_t) + 2</tt>
  89. * because the first bytes are used to store the used size of the
  90. * buffer.
  91. */
  92. size_t bsize;
  93. /**
  94. * @brief Number of buffers.
  95. */
  96. size_t bn;
  97. /**
  98. * @brief Queue of buffer objects.
  99. */
  100. uint8_t *buffers;
  101. /**
  102. * @brief Pointer for R/W sequential access.
  103. * @note It is @p NULL if a new buffer must be fetched from the queue.
  104. */
  105. uint8_t *ptr;
  106. /**
  107. * @brief Boundary for R/W sequential access.
  108. */
  109. uint8_t *top;
  110. /**
  111. * @brief Data notification callback.
  112. */
  113. bqnotify_t notify;
  114. /**
  115. * @brief Application defined field.
  116. */
  117. void *link;
  118. };
  119. /**
  120. * @brief Type of an input buffers queue.
  121. */
  122. typedef io_buffers_queue_t input_buffers_queue_t;
  123. /**
  124. * @brief Type of an output buffers queue.
  125. */
  126. typedef io_buffers_queue_t output_buffers_queue_t;
  127. /*===========================================================================*/
  128. /* Driver macros. */
  129. /*===========================================================================*/
  130. /**
  131. * @brief Computes the size of a buffers queue buffer size.
  132. *
  133. * @param[in] n number of buffers in the queue
  134. * @param[in] size size of the buffers
  135. */
  136. #define BQ_BUFFER_SIZE(n, size) \
  137. (((size_t)(size) + sizeof (size_t)) * (size_t)(n))
  138. /**
  139. * @name Macro Functions
  140. * @{
  141. */
  142. /**
  143. * @brief Returns the queue's number of buffers.
  144. *
  145. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  146. * @return The number of buffers.
  147. *
  148. * @xclass
  149. */
  150. #define bqSizeX(bqp) ((bqp)->bn)
  151. /**
  152. * @brief Return the ready buffers number.
  153. * @details Returns the number of filled buffers if used on an input queue
  154. * or the number of empty buffers if used on an output queue.
  155. *
  156. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  157. * @return The number of ready buffers.
  158. *
  159. * @iclass
  160. */
  161. #define bqSpaceI(bqp) ((bqp)->bcounter)
  162. /**
  163. * @brief Returns the queue application-defined link.
  164. *
  165. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  166. * @return The application-defined link.
  167. *
  168. * @special
  169. */
  170. #define bqGetLinkX(bqp) ((bqp)->link)
  171. /**
  172. * @brief Sets the queue application-defined link.
  173. *
  174. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  175. * @param[in] lk The application-defined link.
  176. *
  177. * @special
  178. */
  179. #define bqSetLinkX(bqp, lk) ((bqp)->link = lk)
  180. /**
  181. * @brief Return the suspended state of the queue.
  182. *
  183. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  184. * @return The suspended state.
  185. * @retval false if blocking access to the queue is enabled.
  186. * @retval true if blocking access to the queue is suspended.
  187. *
  188. * @xclass
  189. */
  190. #define bqIsSuspendedX(bqp) ((bqp)->suspended)
  191. /**
  192. * @brief Puts the queue in suspended state.
  193. * @details When the queue is put in suspended state all waiting threads are
  194. * woken with message @p MSG_RESET and subsequent attempt at waiting
  195. * on the queue will result in an immediate return with @p MSG_RESET
  196. * message.
  197. * @note The content of the queue is not altered, queues can be accessed
  198. * is suspended state until a blocking operation is met then a
  199. * @p MSG_RESET occurs.
  200. *
  201. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  202. *
  203. * @iclass
  204. */
  205. #define bqSuspendI(bqp) { \
  206. (bqp)->suspended = true; \
  207. osalThreadDequeueAllI(&(bqp)->waiting, MSG_RESET); \
  208. }
  209. /**
  210. * @brief Resumes normal queue operations.
  211. *
  212. * @param[in] bqp pointer to an @p io_buffers_queue_t structure
  213. *
  214. * @xclass
  215. */
  216. #define bqResumeX(bqp) { \
  217. (bqp)->suspended = false; \
  218. }
  219. /**
  220. * @brief Evaluates to @p true if the specified input buffers queue is empty.
  221. *
  222. * @param[in] ibqp pointer to an @p input_buffers_queue_t structure
  223. * @return The queue status.
  224. * @retval false if the queue is not empty.
  225. * @retval true if the queue is empty.
  226. *
  227. * @iclass
  228. */
  229. #define ibqIsEmptyI(ibqp) ((bool)(bqSpaceI(ibqp) == 0U))
  230. /**
  231. * @brief Evaluates to @p true if the specified input buffers queue is full.
  232. *
  233. * @param[in] ibqp pointer to an @p input_buffers_queue_t structure
  234. * @return The queue status.
  235. * @retval false if the queue is not full.
  236. * @retval true if the queue is full.
  237. *
  238. * @iclass
  239. */
  240. #define ibqIsFullI(ibqp) \
  241. /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
  242. ((bool)(((ibqp)->bwrptr == (ibqp)->brdptr) && ((ibqp)->bcounter != 0U))) \
  243. /*lint -restore*/
  244. /**
  245. * @brief Evaluates to @p true if the specified output buffers queue is empty.
  246. *
  247. * @param[in] obqp pointer to an @p output_buffers_queue_t structure
  248. * @return The queue status.
  249. * @retval false if the queue is not empty.
  250. * @retval true if the queue is empty.
  251. *
  252. * @iclass
  253. */
  254. #define obqIsEmptyI(obqp) \
  255. /*lint -save -e9007 [13.5] No side effects, a pointer is passed.*/ \
  256. ((bool)(((obqp)->bwrptr == (obqp)->brdptr) && ((obqp)->bcounter != 0U))) \
  257. /*lint -restore*/
  258. /**
  259. * @brief Evaluates to @p true if the specified output buffers queue is full.
  260. *
  261. * @param[in] obqp pointer to an @p output_buffers_queue_t structure
  262. * @return The queue status.
  263. * @retval false if the queue is not full.
  264. * @retval true if the queue is full.
  265. *
  266. * @iclass
  267. */
  268. #define obqIsFullI(obqp) ((bool)(bqSpaceI(obqp) == 0U))
  269. /** @} */
  270. /*===========================================================================*/
  271. /* External declarations. */
  272. /*===========================================================================*/
  273. #ifdef __cplusplus
  274. extern "C" {
  275. #endif
  276. void ibqObjectInit(input_buffers_queue_t *ibqp, bool suspended, uint8_t *bp,
  277. size_t size, size_t n, bqnotify_t infy, void *link);
  278. void ibqResetI(input_buffers_queue_t *ibqp);
  279. uint8_t *ibqGetEmptyBufferI(input_buffers_queue_t *ibqp);
  280. void ibqPostFullBufferI(input_buffers_queue_t *ibqp, size_t size);
  281. msg_t ibqGetFullBufferTimeout(input_buffers_queue_t *ibqp,
  282. sysinterval_t timeout);
  283. msg_t ibqGetFullBufferTimeoutS(input_buffers_queue_t *ibqp,
  284. sysinterval_t timeout);
  285. void ibqReleaseEmptyBuffer(input_buffers_queue_t *ibqp);
  286. void ibqReleaseEmptyBufferS(input_buffers_queue_t *ibqp);
  287. msg_t ibqGetTimeout(input_buffers_queue_t *ibqp, sysinterval_t timeout);
  288. size_t ibqReadTimeout(input_buffers_queue_t *ibqp, uint8_t *bp,
  289. size_t n, sysinterval_t timeout);
  290. void obqObjectInit(output_buffers_queue_t *obqp, bool suspended, uint8_t *bp,
  291. size_t size, size_t n, bqnotify_t onfy, void *link);
  292. void obqResetI(output_buffers_queue_t *obqp);
  293. uint8_t *obqGetFullBufferI(output_buffers_queue_t *obqp,
  294. size_t *sizep);
  295. void obqReleaseEmptyBufferI(output_buffers_queue_t *obqp);
  296. msg_t obqGetEmptyBufferTimeout(output_buffers_queue_t *obqp,
  297. sysinterval_t timeout);
  298. msg_t obqGetEmptyBufferTimeoutS(output_buffers_queue_t *obqp,
  299. sysinterval_t timeout);
  300. void obqPostFullBuffer(output_buffers_queue_t *obqp, size_t size);
  301. void obqPostFullBufferS(output_buffers_queue_t *obqp, size_t size);
  302. msg_t obqPutTimeout(output_buffers_queue_t *obqp, uint8_t b,
  303. sysinterval_t timeout);
  304. size_t obqWriteTimeout(output_buffers_queue_t *obqp, const uint8_t *bp,
  305. size_t n, sysinterval_t timeout);
  306. bool obqTryFlushI(output_buffers_queue_t *obqp);
  307. void obqFlush(output_buffers_queue_t *obqp);
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif /* HAL_BUFFERS_H */
  312. /** @} */