i2s_callback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * \file
  3. *
  4. * \brief SAM I<SUP>2</SUP>S - 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. #include "i2s_callback.h"
  47. struct i2s_module *_i2s_instances[I2S_INST_NUM];
  48. static void _i2s_interrupt_handler(const uint8_t instance)
  49. {
  50. struct i2s_module *module = _i2s_instances[instance];
  51. struct i2s_serializer_module *data_module;
  52. /* Get interrupt flags */
  53. uint32_t intflag = module->hw->INTFLAG.reg;
  54. uint32_t inten = intflag & module->hw->INTENSET.reg;
  55. uint32_t run_flags = (I2S_INTFLAG_TXUR0 | I2S_INTFLAG_RXOR0);
  56. uint32_t ready_flags = (I2S_INTFLAG_TXRDY0 | I2S_INTFLAG_RXRDY0);
  57. uint32_t call_mask;
  58. uint8_t serializer;
  59. for (serializer = 0; serializer < 2; serializer ++) {
  60. data_module = &module->serializer[serializer];
  61. call_mask = data_module->registered_callback_mask &
  62. data_module->enabled_callback_mask;
  63. if (intflag & (run_flags | ready_flags)) {
  64. /* Serializer Tx ready */
  65. if ((I2S_INTFLAG_TXRDY0 << serializer) & inten) {
  66. if (data_module->transferred_words <
  67. data_module->requested_words) {
  68. /* Write data word */
  69. while (module->hw->SYNCBUSY.reg &
  70. (I2S_SYNCBUSY_DATA0 << serializer)) {
  71. /* Wait sync */
  72. }
  73. switch(data_module->data_size) {
  74. case I2S_DATA_SIZE_32BIT:
  75. case I2S_DATA_SIZE_24BIT:
  76. case I2S_DATA_SIZE_20BIT:
  77. case I2S_DATA_SIZE_18BIT:
  78. module->hw->DATA[serializer].reg =
  79. ((uint32_t*)data_module->job_buffer) \
  80. [data_module->transferred_words];
  81. break;
  82. case I2S_DATA_SIZE_16BIT:
  83. case I2S_DATA_SIZE_16BIT_COMPACT:
  84. module->hw->DATA[serializer].reg =
  85. ((uint16_t*)data_module->job_buffer) \
  86. [data_module->transferred_words];
  87. break;
  88. default:
  89. module->hw->DATA[serializer].reg =
  90. ((uint8_t*)data_module->job_buffer) \
  91. [data_module->transferred_words];
  92. }
  93. /* Clear interrupt status */
  94. module->hw->INTFLAG.reg = I2S_INTFLAG_TXRDY0 << serializer;
  95. /* Count data */
  96. data_module->transferred_words ++;
  97. }
  98. /* Check if the buffer is done */
  99. if (data_module->transferred_words >=
  100. data_module->requested_words) {
  101. /* It's done */
  102. data_module->job_status = STATUS_OK;
  103. /* Disable interrupt */
  104. module->hw->INTENCLR.reg =
  105. I2S_INTFLAG_TXRDY0 << serializer;
  106. /* Invoke callback */
  107. if ((1 << I2S_SERIALIZER_CALLBACK_BUFFER_DONE) &
  108. call_mask) {
  109. (data_module->callback \
  110. [I2S_SERIALIZER_CALLBACK_BUFFER_DONE])(module);
  111. }
  112. }
  113. return;
  114. }
  115. /* Serializer Rx ready */
  116. if ((I2S_INTFLAG_RXRDY0 << serializer) & inten) {
  117. /* Read data word */
  118. switch(data_module->data_size) {
  119. case I2S_DATA_SIZE_32BIT:
  120. case I2S_DATA_SIZE_24BIT:
  121. case I2S_DATA_SIZE_20BIT:
  122. case I2S_DATA_SIZE_18BIT:
  123. ((uint32_t*)data_module->job_buffer) \
  124. [data_module->transferred_words] =
  125. module->hw->DATA[serializer].reg;
  126. break;
  127. case I2S_DATA_SIZE_16BIT:
  128. case I2S_DATA_SIZE_16BIT_COMPACT:
  129. ((uint16_t*)data_module->job_buffer) \
  130. [data_module->transferred_words] =
  131. (uint16_t)module->hw->DATA[serializer].reg;
  132. break;
  133. default:
  134. ((uint8_t*)data_module->job_buffer) \
  135. [data_module->transferred_words] =
  136. (uint8_t)module->hw->DATA[serializer].reg;
  137. }
  138. /* Clear interrupt status */
  139. module->hw->INTFLAG.reg = I2S_INTFLAG_RXRDY0 << serializer;
  140. /* Count data */
  141. data_module->transferred_words ++;
  142. /* Check if the buffer is done */
  143. if (data_module->transferred_words >=
  144. data_module->requested_words) {
  145. if (data_module->job_status == STATUS_BUSY) {
  146. data_module->job_status = STATUS_OK;
  147. /* Disable interrupt */
  148. module->hw->INTENCLR.reg =
  149. I2S_INTFLAG_RXRDY0 << serializer;
  150. /* Invoke callback */
  151. if ((1 << I2S_SERIALIZER_CALLBACK_BUFFER_DONE) &
  152. call_mask) {
  153. (data_module->callback \
  154. [I2S_SERIALIZER_CALLBACK_BUFFER_DONE])(module);
  155. }
  156. }
  157. }
  158. return;
  159. }
  160. /* Serializer Tx undrerun or Rx overrun */
  161. if (run_flags & inten) {
  162. module->hw->INTFLAG.reg = I2S_INTFLAG_TXUR0 << serializer;
  163. if ((1 << I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN) &
  164. call_mask) {
  165. (data_module->callback \
  166. [I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN])(module);
  167. }
  168. return;
  169. }
  170. }
  171. run_flags <<= 1;
  172. ready_flags <<= 1;
  173. }
  174. }
  175. /** Interrupt handler for the I<SUP>2</SUP>S module */
  176. void I2S_Handler(void)
  177. {
  178. _i2s_interrupt_handler(0);
  179. }
  180. /**
  181. * \brief Write buffer to the specified Serializer of I<SUP>2</SUP>S module
  182. *
  183. * \param[in] module_inst Pointer to the software module instance struct
  184. * \param[in] serializer The serializer to write to
  185. * \param[in] buffer The data buffer to write
  186. * \param[in] size Number of data words to write
  187. *
  188. * \return Status of the initialization procedure.
  189. *
  190. * \retval STATUS_OK The data was sent successfully
  191. * \retval STATUS_ERR_DENIED The serializer is not in transmit mode
  192. * \retval STATUS_ERR_INVALID_ARG An invalid buffer pointer was supplied
  193. */
  194. enum status_code i2s_serializer_write_buffer_job(
  195. struct i2s_module *const module_inst,
  196. const enum i2s_serializer serializer,
  197. const void *buffer,
  198. const uint32_t size)
  199. {
  200. struct i2s_serializer_module *data_module;
  201. /* Sanity check arguments */
  202. Assert(module_inst);
  203. Assert(module_inst->hw);
  204. Assert(serializer < I2S_SERIALIZER_N);
  205. data_module = &module_inst->serializer[serializer];
  206. /* Serializer must in transmit mode */
  207. if (data_module->mode != I2S_SERIALIZER_TRANSMIT) {
  208. return STATUS_ERR_DENIED;
  209. }
  210. /* Buffer should be aligned */
  211. switch(data_module->data_size) {
  212. case I2S_DATA_SIZE_32BIT:
  213. case I2S_DATA_SIZE_24BIT:
  214. case I2S_DATA_SIZE_20BIT:
  215. case I2S_DATA_SIZE_18BIT:
  216. if ((uint32_t)buffer & 0x3) {
  217. return STATUS_ERR_INVALID_ARG;
  218. }
  219. break;
  220. case I2S_DATA_SIZE_16BIT:
  221. case I2S_DATA_SIZE_16BIT_COMPACT:
  222. if ((uint32_t)buffer & 0x1) {
  223. return STATUS_ERR_INVALID_ARG;
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. data_module = &module_inst->serializer[serializer];
  230. if (data_module->job_status == STATUS_BUSY) {
  231. return STATUS_BUSY;
  232. }
  233. data_module->job_status = STATUS_BUSY;
  234. data_module->requested_words = size;
  235. data_module->transferred_words = 0;
  236. data_module->job_buffer = (void*)buffer;
  237. module_inst->hw->INTENSET.reg = (I2S_INTENSET_TXRDY0 << serializer);
  238. return STATUS_OK;
  239. }
  240. /**
  241. * \brief Read from the specified Serializer of I<SUP>2</SUP>S module to a buffer
  242. *
  243. * \param[in] module_inst Pointer to the software module instance struct
  244. * \param[in] serializer The serializer to write to
  245. * \param[out] buffer The buffer to fill read data
  246. * \param[in] size Number of data words to read
  247. *
  248. * \return Status of the initialization procedure.
  249. *
  250. * \retval STATUS_OK The data was sent successfully
  251. * \retval STATUS_ERR_DENIED The serializer is not in receive mode
  252. * \retval STATUS_ERR_INVALID_ARG An invalid buffer pointer was supplied
  253. */
  254. enum status_code i2s_serializer_read_buffer_job(
  255. struct i2s_module *const module_inst,
  256. const enum i2s_serializer serializer,
  257. void *buffer,
  258. const uint32_t size)
  259. {
  260. struct i2s_serializer_module *data_module;
  261. /* Sanity check arguments */
  262. Assert(module_inst);
  263. Assert(module_inst->hw);
  264. Assert(serializer < I2S_SERIALIZER_N);
  265. data_module = &module_inst->serializer[serializer];
  266. /* Serializer must in receive mode */
  267. if (data_module->mode == I2S_SERIALIZER_TRANSMIT) {
  268. return STATUS_ERR_DENIED;
  269. }
  270. /* Data buffer must be aligned */
  271. switch(data_module->data_size) {
  272. case I2S_DATA_SIZE_32BIT:
  273. case I2S_DATA_SIZE_24BIT:
  274. case I2S_DATA_SIZE_20BIT:
  275. case I2S_DATA_SIZE_18BIT:
  276. if ((uint32_t)buffer & 0x3) {
  277. return STATUS_ERR_INVALID_ARG;
  278. }
  279. break;
  280. case I2S_DATA_SIZE_16BIT:
  281. case I2S_DATA_SIZE_16BIT_COMPACT:
  282. if ((uint32_t)buffer & 0x1) {
  283. return STATUS_ERR_INVALID_ARG;
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. data_module = &module_inst->serializer[serializer];
  290. if (data_module->job_status == STATUS_BUSY) {
  291. return STATUS_BUSY;
  292. }
  293. data_module->job_status = STATUS_BUSY;
  294. data_module->requested_words = size;
  295. data_module->transferred_words = 0;
  296. data_module->job_buffer = (void*)buffer;
  297. module_inst->hw->INTENCLR.reg = (I2S_INTENSET_RXRDY0 << serializer);
  298. module_inst->hw->INTENSET.reg = (I2S_INTENSET_RXRDY0 << serializer);
  299. return STATUS_OK;
  300. }
  301. /**
  302. * \brief Aborts an ongoing job running on serializer
  303. *
  304. * Aborts an ongoing job.
  305. *
  306. * \param[in] module_inst Pointer to the software module instance struct
  307. * \param[in] serializer The serializer which runs the job
  308. * \param[in] job_type Type of job to abort
  309. */
  310. void i2s_serializer_abort_job(
  311. struct i2s_module *const module_inst,
  312. const enum i2s_serializer serializer,
  313. const enum i2s_job_type job_type)
  314. {
  315. /* Sanity check arguments */
  316. Assert(module_inst);
  317. Assert(module_inst->hw);
  318. Assert(serializer < I2S_SERIALIZER_N);
  319. if (job_type == I2S_JOB_WRITE_BUFFER) {
  320. /* Disable interrupt */
  321. module_inst->hw->INTENCLR.reg = (I2S_INTENCLR_TXRDY0 << serializer);
  322. /* Mark job as aborted */
  323. module_inst->serializer[serializer].job_status = STATUS_ABORTED;
  324. } else if (job_type == I2S_JOB_READ_BUFFER) {
  325. /* Disable interrupt */
  326. module_inst->hw->INTENCLR.reg = (I2S_INTENCLR_RXRDY0 << serializer);
  327. /* Mark job as aborted */
  328. module_inst->serializer[serializer].job_status = STATUS_ABORTED;
  329. }
  330. }
  331. /**
  332. * \brief Gets the status of a job running on serializer
  333. *
  334. * Gets the status of an ongoing or the last job.
  335. *
  336. * \param[in] module_inst Pointer to the software module instance struct
  337. * \param[in] serializer The serializer which runs the job
  338. * \param[in] job_type Type of job to abort
  339. *
  340. * \return Status of the job.
  341. */
  342. enum status_code i2s_serializer_get_job_status(
  343. const struct i2s_module *const module_inst,
  344. const enum i2s_serializer serializer,
  345. const enum i2s_job_type job_type)
  346. {
  347. /* Sanity check arguments */
  348. Assert(module_inst);
  349. Assert(serializer < I2S_SERIALIZER_N);
  350. if (job_type == I2S_JOB_WRITE_BUFFER || job_type == I2S_JOB_READ_BUFFER) {
  351. return module_inst->serializer[serializer].job_status;
  352. } else {
  353. return STATUS_ERR_INVALID_ARG;
  354. }
  355. }