nrf_drv_i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "nrf_drv_i2s.h"
  13. #include "nrf_drv_common.h"
  14. #include "nrf_gpio.h"
  15. #include "nrf_assert.h"
  16. #include "app_util_platform.h"
  17. #include "sdk_common.h"
  18. #if (I2S_ENABLED == 0)
  19. #error "I2S not enabled in driver configuration file."
  20. #endif
  21. // Control block - driver instance local data.
  22. typedef struct
  23. {
  24. nrf_drv_i2s_data_handler_t handler;
  25. nrf_drv_state_t state;
  26. bool synchronized_mode : 1;
  27. bool rx_ready : 1;
  28. bool tx_ready : 1;
  29. bool just_started : 1;
  30. uint16_t buffer_half_size;
  31. uint32_t * p_rx_buffer;
  32. uint32_t * p_tx_buffer;
  33. } i2s_control_block_t;
  34. static i2s_control_block_t m_cb;
  35. #define MODULE_INITIALIZED (m_cb.state == NRF_DRV_STATE_INITIALIZED)
  36. #include "sdk_macros.h"
  37. static nrf_drv_i2s_config_t const m_default_config = NRF_DRV_I2S_DEFAULT_CONFIG;
  38. static void configure_pins(nrf_drv_i2s_config_t const * p_config)
  39. {
  40. uint32_t mck_pin, sdout_pin, sdin_pin;
  41. // Configure pins used by the peripheral:
  42. // - SCK and LRCK (required) - depending on the mode of operation these
  43. // pins are configured as outputs (in Master mode) or inputs (in Slave
  44. // mode).
  45. if (p_config->mode == NRF_I2S_MODE_MASTER)
  46. {
  47. nrf_gpio_cfg_output(p_config->sck_pin);
  48. nrf_gpio_cfg_output(p_config->lrck_pin);
  49. }
  50. else
  51. {
  52. nrf_gpio_cfg_input(p_config->sck_pin, NRF_GPIO_PIN_NOPULL);
  53. nrf_gpio_cfg_input(p_config->lrck_pin, NRF_GPIO_PIN_NOPULL);
  54. }
  55. // - MCK (optional) - always output,
  56. if (p_config->mck_pin != NRF_DRV_I2S_PIN_NOT_USED)
  57. {
  58. mck_pin = p_config->mck_pin;
  59. nrf_gpio_cfg_output(mck_pin);
  60. }
  61. else
  62. {
  63. mck_pin = NRF_I2S_PIN_NOT_CONNECTED;
  64. }
  65. // - SDOUT (optional) - always output,
  66. if (p_config->sdout_pin != NRF_DRV_I2S_PIN_NOT_USED)
  67. {
  68. sdout_pin = p_config->sdout_pin;
  69. nrf_gpio_cfg_output(sdout_pin);
  70. }
  71. else
  72. {
  73. sdout_pin = NRF_I2S_PIN_NOT_CONNECTED;
  74. }
  75. // - SDIN (optional) - always input.
  76. if (p_config->sdin_pin != NRF_DRV_I2S_PIN_NOT_USED)
  77. {
  78. sdin_pin = p_config->sdin_pin;
  79. nrf_gpio_cfg_input(sdin_pin, NRF_GPIO_PIN_NOPULL);
  80. }
  81. else
  82. {
  83. sdin_pin = NRF_I2S_PIN_NOT_CONNECTED;
  84. }
  85. nrf_i2s_pins_set(NRF_I2S, p_config->sck_pin, p_config->lrck_pin,
  86. mck_pin, sdout_pin, sdin_pin);
  87. }
  88. ret_code_t nrf_drv_i2s_init(nrf_drv_i2s_config_t const * p_config,
  89. nrf_drv_i2s_data_handler_t handler)
  90. {
  91. ASSERT(handler);
  92. if (m_cb.state != NRF_DRV_STATE_UNINITIALIZED)
  93. {
  94. return NRF_ERROR_INVALID_STATE;
  95. }
  96. if (p_config == NULL)
  97. {
  98. p_config = &m_default_config;
  99. }
  100. if (!nrf_i2s_configure(NRF_I2S, p_config->mode,
  101. p_config->format,
  102. p_config->alignment,
  103. p_config->sample_width,
  104. p_config->channels,
  105. p_config->mck_setup,
  106. p_config->ratio))
  107. {
  108. return NRF_ERROR_INVALID_PARAM;
  109. }
  110. configure_pins(p_config);
  111. m_cb.handler = handler;
  112. nrf_drv_common_irq_enable(I2S_IRQn, p_config->irq_priority);
  113. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  114. return NRF_SUCCESS;
  115. }
  116. void nrf_drv_i2s_uninit(void)
  117. {
  118. ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED);
  119. nrf_drv_i2s_stop();
  120. nrf_drv_common_irq_disable(I2S_IRQn);
  121. m_cb.state = NRF_DRV_STATE_UNINITIALIZED;
  122. }
  123. ret_code_t nrf_drv_i2s_start(uint32_t * p_rx_buffer,
  124. uint32_t * p_tx_buffer,
  125. uint16_t buffer_size,
  126. uint8_t flags)
  127. {
  128. ASSERT((p_rx_buffer != NULL) || (p_tx_buffer != NULL));
  129. uint16_t buffer_half_size = buffer_size / 2;
  130. ASSERT(buffer_half_size != 0);
  131. VERIFY_MODULE_INITIALIZED();
  132. if ((p_rx_buffer != NULL) && !nrf_drv_is_in_RAM(p_rx_buffer))
  133. {
  134. return NRF_ERROR_INVALID_ADDR;
  135. }
  136. if ((p_tx_buffer != NULL) && !nrf_drv_is_in_RAM(p_tx_buffer))
  137. {
  138. return NRF_ERROR_INVALID_ADDR;
  139. }
  140. // Initially we set up the peripheral to use the first half of each buffer,
  141. // then in 'I2S_IRQHandler' we will switch to the second half.
  142. nrf_i2s_transfer_set(NRF_I2S, buffer_half_size, p_rx_buffer, p_tx_buffer);
  143. m_cb.p_rx_buffer = p_rx_buffer;
  144. m_cb.p_tx_buffer = p_tx_buffer;
  145. m_cb.buffer_half_size = buffer_half_size;
  146. m_cb.just_started = true;
  147. if ((flags & NRF_DRV_I2S_FLAG_SYNCHRONIZED_MODE) &&
  148. // [synchronized mode makes sense only when both RX and TX are enabled]
  149. (m_cb.p_rx_buffer != NULL) && (m_cb.p_tx_buffer != NULL))
  150. {
  151. m_cb.synchronized_mode = true;
  152. m_cb.rx_ready = false;
  153. m_cb.tx_ready = false;
  154. }
  155. else
  156. {
  157. m_cb.synchronized_mode = false;
  158. }
  159. nrf_i2s_enable(NRF_I2S);
  160. m_cb.state = NRF_DRV_STATE_POWERED_ON;
  161. if (m_cb.p_tx_buffer != NULL)
  162. {
  163. // Get from the application the first portion of data to be sent - we
  164. // need to have it in the transmit buffer before we start the transfer.
  165. // Unless the synchronized mode is active. In this mode we must wait
  166. // with this until the first portion of data is received, so here we
  167. // just make sure that there will be silence on the SDOUT line prior
  168. // to that moment.
  169. if (m_cb.synchronized_mode)
  170. {
  171. memset(m_cb.p_tx_buffer, 0, buffer_size);
  172. }
  173. else
  174. {
  175. m_cb.handler(NULL, m_cb.p_tx_buffer, m_cb.buffer_half_size);
  176. }
  177. }
  178. nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_RXPTRUPD);
  179. nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_TXPTRUPD);
  180. nrf_i2s_int_enable(NRF_I2S,
  181. NRF_I2S_INT_RXPTRUPD_MASK | NRF_I2S_INT_TXPTRUPD_MASK);
  182. nrf_i2s_task_trigger(NRF_I2S, NRF_I2S_TASK_START);
  183. return NRF_SUCCESS;
  184. }
  185. void nrf_drv_i2s_stop(void)
  186. {
  187. ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED);
  188. // First disable interrupts, then trigger the STOP task, so no spurious
  189. // RXPTRUPD and TXPTRUPD events (see FTPAN-55) will be processed.
  190. nrf_i2s_int_disable(NRF_I2S,
  191. NRF_I2S_INT_RXPTRUPD_MASK | NRF_I2S_INT_TXPTRUPD_MASK);
  192. nrf_i2s_task_trigger(NRF_I2S, NRF_I2S_TASK_STOP);
  193. nrf_i2s_disable(NRF_I2S);
  194. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  195. }
  196. void I2S_IRQHandler(void)
  197. {
  198. uint32_t * p_data_received = NULL;
  199. uint32_t * p_data_to_send = NULL;
  200. if (nrf_i2s_event_check(NRF_I2S, NRF_I2S_EVENT_TXPTRUPD))
  201. {
  202. nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_TXPTRUPD);
  203. // If transmission is not enabled, but for some reason the TXPTRUPD
  204. // event has been generated, just ignore it.
  205. if (m_cb.p_tx_buffer != NULL)
  206. {
  207. uint32_t * p_tx_buffer_next;
  208. if (nrf_i2s_tx_buffer_get(NRF_I2S) == m_cb.p_tx_buffer)
  209. {
  210. p_tx_buffer_next = m_cb.p_tx_buffer + m_cb.buffer_half_size;
  211. }
  212. else
  213. {
  214. p_tx_buffer_next = m_cb.p_tx_buffer;
  215. }
  216. nrf_i2s_tx_buffer_set(NRF_I2S, p_tx_buffer_next);
  217. m_cb.tx_ready = true;
  218. // Now the part of the buffer that we've configured as "next" should
  219. // be filled by the application with proper data to be sent;
  220. // the peripheral is sending data from the other part of the buffer
  221. // (but it will finish soon...).
  222. p_data_to_send = p_tx_buffer_next;
  223. }
  224. }
  225. if (nrf_i2s_event_check(NRF_I2S, NRF_I2S_EVENT_RXPTRUPD))
  226. {
  227. nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_RXPTRUPD);
  228. // If reception is not enabled, but for some reason the RXPTRUPD event
  229. // has been generated, just ignore it.
  230. if (m_cb.p_rx_buffer != NULL)
  231. {
  232. uint32_t * p_rx_buffer_next;
  233. if (nrf_i2s_rx_buffer_get(NRF_I2S) == m_cb.p_rx_buffer)
  234. {
  235. p_rx_buffer_next = m_cb.p_rx_buffer + m_cb.buffer_half_size;
  236. }
  237. else
  238. {
  239. p_rx_buffer_next = m_cb.p_rx_buffer;
  240. }
  241. nrf_i2s_rx_buffer_set(NRF_I2S, p_rx_buffer_next);
  242. m_cb.rx_ready = true;
  243. // The RXPTRUPD event is generated for the first time right after
  244. // the transfer is started. Since there is no data received yet at
  245. // this point we only update the buffer pointer (it is done above),
  246. // there is no callback to the application.
  247. // [for synchronized mode this has to be handled differently -
  248. // see below]
  249. if (m_cb.just_started && !m_cb.synchronized_mode)
  250. {
  251. m_cb.just_started = false;
  252. }
  253. else
  254. {
  255. // The RXPTRUPD event indicates that from now on the peripheral
  256. // will be filling the part of the buffer that was pointed at
  257. // the time the event has been generated, hence now we can let
  258. // the application process the data stored in the other part of
  259. // the buffer - the one that we've just set to be filled next.
  260. p_data_received = p_rx_buffer_next;
  261. }
  262. }
  263. }
  264. // Call the data handler passing received data to the application and/or
  265. // requesting data to be sent.
  266. if (!m_cb.synchronized_mode)
  267. {
  268. if ((p_data_received != NULL) || (p_data_to_send != NULL))
  269. {
  270. m_cb.handler(p_data_received, p_data_to_send,
  271. m_cb.buffer_half_size);
  272. }
  273. }
  274. // In the synchronized mode wait until the events for both RX and TX occur.
  275. // And ignore the initial occurrences of these events, since they only
  276. // indicate that the transfer has started - no data is received yet at
  277. // that moment, so we have got nothing to pass to the application.
  278. else
  279. {
  280. if (m_cb.rx_ready && m_cb.tx_ready)
  281. {
  282. m_cb.rx_ready = false;
  283. m_cb.tx_ready = false;
  284. if (m_cb.just_started)
  285. {
  286. m_cb.just_started = false;
  287. }
  288. else
  289. {
  290. m_cb.handler(nrf_i2s_rx_buffer_get(NRF_I2S),
  291. nrf_i2s_tx_buffer_get(NRF_I2S),
  292. m_cb.buffer_half_size);
  293. }
  294. }
  295. }
  296. }