i2s_callback.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * \file
  3. *
  4. * \brief SAM I2S - Inter-IC Sound Controller
  5. *
  6. * Copyright (C) 2014-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef I2S_CALLBACK_H_INCLUDED
  47. #define I2S_CALLBACK_H_INCLUDED
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * \addtogroup asfdoc_sam0_i2s_group
  53. *
  54. * @{
  55. */
  56. #include <i2s.h>
  57. /**
  58. * Enum for the possible types of I<SUP>2</SUP>S asynchronous jobs that may be issued to
  59. * the driver.
  60. */
  61. enum i2s_job_type {
  62. /** Asynchronous I<SUP>2</SUP>S write from a user provided buffer */
  63. I2S_JOB_WRITE_BUFFER,
  64. /** Asynchronous I<SUP>2</SUP>S read into a user provided buffer */
  65. I2S_JOB_READ_BUFFER
  66. };
  67. /**
  68. * \name Callback Management
  69. * @{
  70. */
  71. /**
  72. * \brief Registers a callback for serializer
  73. *
  74. * Registers a callback function which is implemented by the user.
  75. *
  76. * \note The callback must be enabled by for the interrupt handler to call it
  77. * when the condition for the callback is met.
  78. *
  79. * \param[in] module Pointer to ADC software instance struct
  80. * \param[in] serializer The serializer that generates callback
  81. * \param[in] callback_func Pointer to callback function
  82. * \param[in] callback_type Callback type given by an enum
  83. *
  84. */
  85. static inline void i2s_serializer_register_callback(
  86. struct i2s_module *const module_inst,
  87. const enum i2s_serializer serializer,
  88. const i2s_serializer_callback_t callback_func,
  89. const enum i2s_serializer_callback callback_type)
  90. {
  91. /* Sanity check arguments */
  92. Assert(module_inst);
  93. Assert(serializer < I2S_SERIALIZER_N);
  94. module_inst->serializer[serializer].callback[callback_type] = callback_func;
  95. module_inst->serializer[serializer].registered_callback_mask |=
  96. (1u << callback_type);
  97. }
  98. /**
  99. * \brief Unregisters a callback for serializer
  100. *
  101. * Unregisters a callback function which is implemented by the user.
  102. *
  103. * \param[in] module Pointer to ADC software instance struct
  104. * \param[in] serializer The serializer that generates callback
  105. * \param[in] callback_type Callback type given by an enum
  106. *
  107. */
  108. static inline void i2s_serializer_unregister_callback(
  109. struct i2s_module *const module_inst,
  110. const enum i2s_serializer serializer,
  111. const enum i2s_serializer_callback callback_type)
  112. {
  113. /* Sanity check arguments */
  114. Assert(module_inst);
  115. Assert(serializer < I2S_SERIALIZER_N);
  116. module_inst->serializer[serializer].callback[callback_type] = NULL;
  117. module_inst->serializer[serializer].registered_callback_mask &=
  118. ~(1u << callback_type);
  119. }
  120. /**
  121. * \brief Enables callback for serializer
  122. *
  123. * Enables the callback function registered by \ref
  124. * i2s_serializer_register_callback. The callback function will be called from
  125. * the interrupt handler when the conditions for the callback type are met.
  126. *
  127. * \param[in] module Pointer to ADC software instance struct
  128. * \param[in] serializer The serializer that generates callback
  129. * \param[in] callback_type Callback type given by an enum
  130. *
  131. */
  132. static inline void i2s_serializer_enable_callback(
  133. struct i2s_module *const module_inst,
  134. const enum i2s_serializer serializer,
  135. const enum i2s_serializer_callback callback_type)
  136. {
  137. /* Sanity check arguments */
  138. Assert(module_inst);
  139. Assert(module_inst->hw);
  140. Assert(serializer < I2S_SERIALIZER_N);
  141. module_inst->serializer[serializer].enabled_callback_mask |=
  142. (1u << callback_type);
  143. if (I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN != callback_type) {
  144. return;
  145. }
  146. module_inst->hw->INTENSET.reg =
  147. (module_inst->serializer[serializer].mode == I2S_SERIALIZER_TRANSMIT) ?
  148. (I2S_INTFLAG_TXUR0 << serializer) :
  149. (I2S_INTFLAG_RXOR0 << serializer);
  150. }
  151. /**
  152. * \brief Disables callback for Serializer
  153. *
  154. * Disables the callback function registered by the \ref
  155. * i2s_serializer_register_callback.
  156. *
  157. * \param[in] module Pointer to ADC software instance struct
  158. * \param[in] serializer The serializer that generates callback
  159. * \param[in] callback_type Callback type given by an enum
  160. *
  161. */
  162. static inline void i2s_serializer_disable_callback(
  163. struct i2s_module *const module_inst,
  164. const enum i2s_serializer serializer,
  165. const enum i2s_serializer_callback callback_type)
  166. {
  167. /* Sanity check arguments */
  168. Assert(module_inst);
  169. Assert(module_inst->hw);
  170. Assert(serializer < I2S_SERIALIZER_N);
  171. module_inst->serializer[serializer].enabled_callback_mask &=
  172. ~(1u << callback_type);
  173. if (I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN != callback_type) {
  174. return;
  175. }
  176. module_inst->hw->INTENCLR.reg =
  177. (module_inst->serializer[serializer].mode == I2S_SERIALIZER_TRANSMIT) ?
  178. (I2S_INTFLAG_TXUR0 << serializer) :
  179. (I2S_INTFLAG_RXOR0 << serializer);
  180. }
  181. /** @} */
  182. /**
  183. * \name Job Management
  184. *
  185. * @{
  186. */
  187. enum status_code i2s_serializer_write_buffer_job(
  188. struct i2s_module *const module_inst,
  189. const enum i2s_serializer serializer,
  190. const void *buffer,
  191. const uint32_t size);
  192. enum status_code i2s_serializer_read_buffer_job(
  193. struct i2s_module *const module_inst,
  194. const enum i2s_serializer serializer,
  195. void *buffer,
  196. const uint32_t size);
  197. void i2s_serializer_abort_job(
  198. struct i2s_module *const module_inst,
  199. const enum i2s_serializer serializer,
  200. const enum i2s_job_type job_type);
  201. enum status_code i2s_serializer_get_job_status(
  202. const struct i2s_module *const module_inst,
  203. const enum i2s_serializer serializer,
  204. const enum i2s_job_type job_type);
  205. /** @} */
  206. /** @} */
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif /* #ifndef I2S_CALLBACK_H_INCLUDED */