hal_i2s.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_i2s.h
  15. * @brief I2S Driver macros and structures.
  16. *
  17. * @addtogroup I2S
  18. * @{
  19. */
  20. #ifndef HAL_I2S_H
  21. #define HAL_I2S_H
  22. #if (HAL_USE_I2S == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name I2S modes
  28. * @{
  29. */
  30. #define I2S_MODE_SLAVE 0
  31. #define I2S_MODE_MASTER 1
  32. /** @} */
  33. /*===========================================================================*/
  34. /* Driver pre-compile time settings. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Derived constants and error checks. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Driver data structures and types. */
  41. /*===========================================================================*/
  42. /**
  43. * @brief Driver state machine possible states.
  44. */
  45. typedef enum {
  46. I2S_UNINIT = 0, /**< Not initialized. */
  47. I2S_STOP = 1, /**< Stopped. */
  48. I2S_READY = 2, /**< Ready. */
  49. I2S_ACTIVE = 3, /**< Active. */
  50. I2S_COMPLETE = 4 /**< Transmission complete. */
  51. } i2sstate_t;
  52. /**
  53. * @brief Type of a structure representing an I2S driver.
  54. */
  55. typedef struct hal_i2s_driver I2SDriver;
  56. /**
  57. * @brief Type of a structure representing an I2S driver configuration.
  58. */
  59. typedef struct hal_i2s_config I2SConfig;
  60. /**
  61. * @brief I2S notification callback type.
  62. *
  63. * @param[in] i2sp pointer to the @p I2SDriver object
  64. */
  65. typedef void (*i2scallback_t)(I2SDriver *i2sp);
  66. /* Including the low level driver header, it exports information required
  67. for completing types.*/
  68. #include "hal_i2s_lld.h"
  69. /**
  70. * @brief Structure representing an I2S driver.
  71. */
  72. struct hal_i2s_driver {
  73. /**
  74. * @brief Driver state.
  75. */
  76. i2sstate_t state;
  77. /**
  78. * @brief Current configuration data.
  79. */
  80. const I2SConfig *config;
  81. /* End of the mandatory fields.*/
  82. i2s_lld_driver_fields;
  83. };
  84. /**
  85. * @brief Driver configuration structure.
  86. */
  87. struct hal_i2s_config {
  88. /**
  89. * @brief Transmission buffer pointer.
  90. * @note Can be @p NULL if TX is not required.
  91. */
  92. const void *tx_buffer;
  93. /**
  94. * @brief Receive buffer pointer.
  95. * @note Can be @p NULL if RX is not required.
  96. */
  97. void *rx_buffer;
  98. /**
  99. * @brief TX and RX buffers size as number of samples.
  100. */
  101. size_t size;
  102. /**
  103. * @brief Callback function called during streaming.
  104. */
  105. i2scallback_t end_cb;
  106. /* End of the mandatory fields.*/
  107. i2s_lld_config_fields;
  108. };
  109. /*===========================================================================*/
  110. /* Driver macros. */
  111. /*===========================================================================*/
  112. /**
  113. * @name Macro Functions
  114. * @{
  115. */
  116. /**
  117. * @brief Buffer state.
  118. * @note This function is meant to be called from the SPI callback only.
  119. *
  120. * @param[in] i2sp pointer to the @p I2SDriver object
  121. * @return The buffer state.
  122. * @retval false if the driver filled/sent the first half of the
  123. * buffer.
  124. * @retval true if the driver filled/sent the second half of the
  125. * buffer.
  126. *
  127. * @special
  128. */
  129. #define i2sIsBufferComplete(i2sp) ((bool)((i2sp)->state == I2S_COMPLETE))
  130. /**
  131. * @brief Starts a I2S data exchange.
  132. *
  133. * @param[in] i2sp pointer to the @p I2SDriver object
  134. *
  135. * @iclass
  136. */
  137. #define i2sStartExchangeI(i2sp) { \
  138. i2s_lld_start_exchange(i2sp); \
  139. (i2sp)->state = I2S_ACTIVE; \
  140. }
  141. /**
  142. * @brief Stops the ongoing data exchange.
  143. * @details The ongoing data exchange, if any, is stopped, if the driver
  144. * was not active the function does nothing.
  145. *
  146. * @param[in] i2sp pointer to the @p I2SDriver object
  147. *
  148. * @iclass
  149. */
  150. #define i2sStopExchangeI(i2sp) { \
  151. i2s_lld_stop_exchange(i2sp); \
  152. (i2sp)->state = I2S_READY; \
  153. }
  154. /**
  155. * @brief Common ISR code, half buffer event.
  156. * @details This code handles the portable part of the ISR code:
  157. * - Callback invocation.
  158. * .
  159. * @note This macro is meant to be used in the low level drivers
  160. * implementation only.
  161. *
  162. * @param[in] i2sp pointer to the @p I2CDriver object
  163. *
  164. * @notapi
  165. */
  166. #define _i2s_isr_half_code(i2sp) { \
  167. if ((i2sp)->config->end_cb != NULL) { \
  168. (i2sp)->config->end_cb(i2sp); \
  169. } \
  170. }
  171. /**
  172. * @brief Common ISR code.
  173. * @details This code handles the portable part of the ISR code:
  174. * - Callback invocation.
  175. * - Driver state transitions.
  176. * .
  177. * @note This macro is meant to be used in the low level drivers
  178. * implementation only.
  179. *
  180. * @param[in] i2sp pointer to the @p I2CDriver object
  181. *
  182. * @notapi
  183. */
  184. #define _i2s_isr_full_code(i2sp) { \
  185. if ((i2sp)->config->end_cb) { \
  186. (i2sp)->state = I2S_COMPLETE; \
  187. (i2sp)->config->end_cb(i2sp); \
  188. if ((i2sp)->state == I2S_COMPLETE) { \
  189. (i2sp)->state = I2S_ACTIVE; \
  190. } \
  191. } \
  192. }
  193. /** @} */
  194. /*===========================================================================*/
  195. /* External declarations. */
  196. /*===========================================================================*/
  197. #ifdef __cplusplus
  198. extern "C" {
  199. #endif
  200. void i2sInit(void);
  201. void i2sObjectInit(I2SDriver *i2sp);
  202. void i2sStart(I2SDriver *i2sp, const I2SConfig *config);
  203. void i2sStop(I2SDriver *i2sp);
  204. void i2sStartExchange(I2SDriver *i2sp);
  205. void i2sStopExchange(I2SDriver *i2sp);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif /* HAL_USE_I2S == TRUE */
  210. #endif /* HAL_I2S_H */
  211. /** @} */