fsl_flexio_i2s.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_flexio_i2s.h"
  31. /*******************************************************************************
  32. * Definitations
  33. ******************************************************************************/
  34. enum _sai_transfer_state
  35. {
  36. kFLEXIO_I2S_Busy = 0x0U, /*!< FLEXIO_I2S is busy */
  37. kFLEXIO_I2S_Idle, /*!< Transfer is done. */
  38. };
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. extern uint32_t FLEXIO_GetInstance(FLEXIO_Type *base);
  43. /*!
  44. * @brief Receive a piece of data in non-blocking way.
  45. *
  46. * @param base FLEXIO I2S base pointer
  47. * @param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  48. * @param buffer Pointer to the data to be read.
  49. * @param size Bytes to be read.
  50. */
  51. static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size);
  52. /*!
  53. * @brief sends a piece of data in non-blocking way.
  54. *
  55. * @param base FLEXIO I2S base pointer
  56. * @param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  57. * @param buffer Pointer to the data to be written.
  58. * @param size Bytes to be written.
  59. */
  60. static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size);
  61. /*******************************************************************************
  62. * Variables
  63. ******************************************************************************/
  64. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  65. extern const clock_ip_name_t s_flexioClocks[];
  66. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  67. extern FLEXIO_Type *const s_flexioBases[];
  68. /*******************************************************************************
  69. * Code
  70. ******************************************************************************/
  71. uint32_t FLEXIO_I2S_GetInstance(FLEXIO_I2S_Type *base)
  72. {
  73. return FLEXIO_GetInstance(base->flexioBase);
  74. }
  75. static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
  76. {
  77. uint32_t i = 0;
  78. uint8_t j = 0;
  79. uint8_t bytesPerWord = bitWidth / 8U;
  80. uint32_t data = 0;
  81. uint32_t temp = 0;
  82. for (i = 0; i < size / bytesPerWord; i++)
  83. {
  84. for (j = 0; j < bytesPerWord; j++)
  85. {
  86. temp = (uint32_t)(*txData);
  87. data |= (temp << (8U * j));
  88. txData++;
  89. }
  90. base->flexioBase->SHIFTBUFBIS[base->txShifterIndex] = (data << (32U - bitWidth));
  91. data = 0;
  92. }
  93. }
  94. static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
  95. {
  96. uint32_t i = 0;
  97. uint8_t j = 0;
  98. uint8_t bytesPerWord = bitWidth / 8U;
  99. uint32_t data = 0;
  100. for (i = 0; i < size / bytesPerWord; i++)
  101. {
  102. data = (base->flexioBase->SHIFTBUFBIS[base->rxShifterIndex] >> (32U - bitWidth));
  103. for (j = 0; j < bytesPerWord; j++)
  104. {
  105. *rxData = (data >> (8U * j)) & 0xFF;
  106. rxData++;
  107. }
  108. }
  109. }
  110. void FLEXIO_I2S_Init(FLEXIO_I2S_Type *base, const flexio_i2s_config_t *config)
  111. {
  112. assert(base && config);
  113. flexio_shifter_config_t shifterConfig = {0};
  114. flexio_timer_config_t timerConfig = {0};
  115. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  116. /* Ungate flexio clock. */
  117. CLOCK_EnableClock(s_flexioClocks[FLEXIO_I2S_GetInstance(base)]);
  118. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  119. /* Set shifter for I2S Tx data */
  120. shifterConfig.timerSelect = base->bclkTimerIndex;
  121. shifterConfig.pinSelect = base->txPinIndex;
  122. shifterConfig.timerPolarity = config->txTimerPolarity;
  123. shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
  124. shifterConfig.pinPolarity = config->txPinPolarity;
  125. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  126. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  127. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  128. if (config->masterSlave == kFLEXIO_I2S_Master)
  129. {
  130. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnShift;
  131. }
  132. else
  133. {
  134. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  135. }
  136. FLEXIO_SetShifterConfig(base->flexioBase, base->txShifterIndex, &shifterConfig);
  137. /* Set shifter for I2S Rx Data */
  138. shifterConfig.timerSelect = base->bclkTimerIndex;
  139. shifterConfig.pinSelect = base->rxPinIndex;
  140. shifterConfig.timerPolarity = config->rxTimerPolarity;
  141. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  142. shifterConfig.pinPolarity = config->rxPinPolarity;
  143. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  144. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  145. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  146. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  147. FLEXIO_SetShifterConfig(base->flexioBase, base->rxShifterIndex, &shifterConfig);
  148. /* Set Timer to I2S frame sync */
  149. if (config->masterSlave == kFLEXIO_I2S_Master)
  150. {
  151. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->txPinIndex);
  152. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  153. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceExternal;
  154. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  155. timerConfig.pinSelect = base->fsPinIndex;
  156. timerConfig.pinPolarity = config->fsPinPolarity;
  157. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  158. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  159. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  160. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  161. timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
  162. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPrevTimerEnable;
  163. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  164. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  165. }
  166. else
  167. {
  168. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->bclkPinIndex);
  169. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  170. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  171. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  172. timerConfig.pinSelect = base->fsPinIndex;
  173. timerConfig.pinPolarity = config->fsPinPolarity;
  174. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  175. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  176. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnTriggerInputShiftTriggerInput;
  177. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  178. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  179. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdge;
  180. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  181. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  182. }
  183. FLEXIO_SetTimerConfig(base->flexioBase, base->fsTimerIndex, &timerConfig);
  184. /* Set Timer to I2S bit clock */
  185. if (config->masterSlave == kFLEXIO_I2S_Master)
  186. {
  187. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->txShifterIndex);
  188. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  189. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  190. timerConfig.pinSelect = base->bclkPinIndex;
  191. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  192. timerConfig.pinPolarity = config->bclkPinPolarity;
  193. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  194. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  195. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  196. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  197. timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
  198. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
  199. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  200. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  201. }
  202. else
  203. {
  204. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_TIMn(base->fsTimerIndex);
  205. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  206. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  207. timerConfig.pinSelect = base->bclkPinIndex;
  208. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  209. timerConfig.pinPolarity = config->bclkPinPolarity;
  210. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  211. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  212. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
  213. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  214. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompareTriggerLow;
  215. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdgeTriggerHigh;
  216. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  217. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  218. }
  219. FLEXIO_SetTimerConfig(base->flexioBase, base->bclkTimerIndex, &timerConfig);
  220. /* If enable flexio I2S */
  221. if (config->enableI2S)
  222. {
  223. base->flexioBase->CTRL |= FLEXIO_CTRL_FLEXEN_MASK;
  224. }
  225. else
  226. {
  227. base->flexioBase->CTRL &= ~FLEXIO_CTRL_FLEXEN_MASK;
  228. }
  229. }
  230. void FLEXIO_I2S_GetDefaultConfig(flexio_i2s_config_t *config)
  231. {
  232. config->masterSlave = kFLEXIO_I2S_Master;
  233. config->enableI2S = true;
  234. config->txPinPolarity = kFLEXIO_PinActiveHigh;
  235. config->rxPinPolarity = kFLEXIO_PinActiveHigh;
  236. config->bclkPinPolarity = kFLEXIO_PinActiveHigh;
  237. config->fsPinPolarity = kFLEXIO_PinActiveLow;
  238. config->txTimerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  239. config->rxTimerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  240. }
  241. void FLEXIO_I2S_Deinit(FLEXIO_I2S_Type *base)
  242. {
  243. base->flexioBase->SHIFTCFG[base->txShifterIndex] = 0;
  244. base->flexioBase->SHIFTCTL[base->txShifterIndex] = 0;
  245. base->flexioBase->SHIFTCFG[base->rxShifterIndex] = 0;
  246. base->flexioBase->SHIFTCTL[base->rxShifterIndex] = 0;
  247. base->flexioBase->TIMCFG[base->fsTimerIndex] = 0;
  248. base->flexioBase->TIMCMP[base->fsTimerIndex] = 0;
  249. base->flexioBase->TIMCTL[base->fsTimerIndex] = 0;
  250. base->flexioBase->TIMCFG[base->bclkTimerIndex] = 0;
  251. base->flexioBase->TIMCMP[base->bclkTimerIndex] = 0;
  252. base->flexioBase->TIMCTL[base->bclkTimerIndex] = 0;
  253. }
  254. void FLEXIO_I2S_EnableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
  255. {
  256. if (mask & kFLEXIO_I2S_TxDataRegEmptyInterruptEnable)
  257. {
  258. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->txShifterIndex);
  259. }
  260. if (mask & kFLEXIO_I2S_RxDataRegFullInterruptEnable)
  261. {
  262. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->rxShifterIndex);
  263. }
  264. }
  265. uint32_t FLEXIO_I2S_GetStatusFlags(FLEXIO_I2S_Type *base)
  266. {
  267. uint32_t status = 0;
  268. status = ((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->txShifterIndex)) >> base->txShifterIndex);
  269. status |=
  270. (((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->rxShifterIndex)) >> (base->rxShifterIndex))
  271. << 1U);
  272. return status;
  273. }
  274. void FLEXIO_I2S_DisableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
  275. {
  276. if (mask & kFLEXIO_I2S_TxDataRegEmptyInterruptEnable)
  277. {
  278. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->txShifterIndex);
  279. }
  280. if (mask & kFLEXIO_I2S_RxDataRegFullInterruptEnable)
  281. {
  282. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->rxShifterIndex);
  283. }
  284. }
  285. void FLEXIO_I2S_MasterSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format, uint32_t srcClock_Hz)
  286. {
  287. uint32_t timDiv = srcClock_Hz / (format->sampleRate_Hz * 32U * 2U);
  288. uint32_t bclkDiv = 0;
  289. /* Shall keep bclk and fs div an integer */
  290. if (timDiv % 2)
  291. {
  292. timDiv += 1U;
  293. }
  294. /* Set Frame sync timer cmp */
  295. base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(32U * timDiv - 1U);
  296. /* Set bit clock timer cmp */
  297. bclkDiv = ((timDiv / 2U - 1U) | (63U << 8U));
  298. base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(bclkDiv);
  299. }
  300. void FLEXIO_I2S_SlaveSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format)
  301. {
  302. /* Set Frame sync timer cmp */
  303. base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(32U * 4U - 3U);
  304. /* Set bit clock timer cmp */
  305. base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(32U * 2U - 1U);
  306. }
  307. void FLEXIO_I2S_WriteBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
  308. {
  309. uint32_t i = 0;
  310. uint8_t bytesPerWord = bitWidth / 8U;
  311. for (i = 0; i < size / bytesPerWord; i++)
  312. {
  313. /* Wait until it can write data */
  314. while ((FLEXIO_I2S_GetStatusFlags(base) & kFLEXIO_I2S_TxDataRegEmptyFlag) == 0)
  315. {
  316. }
  317. FLEXIO_I2S_WriteNonBlocking(base, bitWidth, txData, bytesPerWord);
  318. txData += bytesPerWord;
  319. }
  320. /* Wait until the last data is sent */
  321. while ((FLEXIO_I2S_GetStatusFlags(base) & kFLEXIO_I2S_TxDataRegEmptyFlag) == 0)
  322. {
  323. }
  324. }
  325. void FLEXIO_I2S_ReadBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
  326. {
  327. uint32_t i = 0;
  328. uint8_t bytesPerWord = bitWidth / 8U;
  329. for (i = 0; i < size / bytesPerWord; i++)
  330. {
  331. /* Wait until data is received */
  332. while (!(FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->rxShifterIndex)))
  333. {
  334. }
  335. FLEXIO_I2S_ReadNonBlocking(base, bitWidth, rxData, bytesPerWord);
  336. rxData += bytesPerWord;
  337. }
  338. }
  339. void FLEXIO_I2S_TransferTxCreateHandle(FLEXIO_I2S_Type *base,
  340. flexio_i2s_handle_t *handle,
  341. flexio_i2s_callback_t callback,
  342. void *userData)
  343. {
  344. assert(handle);
  345. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  346. /* Zero the handle. */
  347. memset(handle, 0, sizeof(*handle));
  348. /* Store callback and user data. */
  349. handle->callback = callback;
  350. handle->userData = userData;
  351. /* Save the context in global variables to support the double weak mechanism. */
  352. FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferTxHandleIRQ);
  353. /* Set the TX/RX state. */
  354. handle->state = kFLEXIO_I2S_Idle;
  355. /* Enable interrupt in NVIC. */
  356. EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
  357. }
  358. void FLEXIO_I2S_TransferRxCreateHandle(FLEXIO_I2S_Type *base,
  359. flexio_i2s_handle_t *handle,
  360. flexio_i2s_callback_t callback,
  361. void *userData)
  362. {
  363. assert(handle);
  364. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  365. /* Zero the handle. */
  366. memset(handle, 0, sizeof(*handle));
  367. /* Store callback and user data. */
  368. handle->callback = callback;
  369. handle->userData = userData;
  370. /* Save the context in global variables to support the double weak mechanism. */
  371. FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferRxHandleIRQ);
  372. /* Set the TX/RX state. */
  373. handle->state = kFLEXIO_I2S_Idle;
  374. /* Enable interrupt in NVIC. */
  375. EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
  376. }
  377. void FLEXIO_I2S_TransferSetFormat(FLEXIO_I2S_Type *base,
  378. flexio_i2s_handle_t *handle,
  379. flexio_i2s_format_t *format,
  380. uint32_t srcClock_Hz)
  381. {
  382. assert(handle && format);
  383. /* Set the bitWidth to handle */
  384. handle->bitWidth = format->bitWidth;
  385. /* Set sample rate */
  386. if (srcClock_Hz != 0)
  387. {
  388. /* It is master */
  389. FLEXIO_I2S_MasterSetFormat(base, format, srcClock_Hz);
  390. }
  391. else
  392. {
  393. FLEXIO_I2S_SlaveSetFormat(base, format);
  394. }
  395. }
  396. status_t FLEXIO_I2S_TransferSendNonBlocking(FLEXIO_I2S_Type *base,
  397. flexio_i2s_handle_t *handle,
  398. flexio_i2s_transfer_t *xfer)
  399. {
  400. assert(handle);
  401. /* Check if the queue is full */
  402. if (handle->queue[handle->queueUser].data)
  403. {
  404. return kStatus_FLEXIO_I2S_QueueFull;
  405. }
  406. if ((xfer->dataSize == 0) || (xfer->data == NULL))
  407. {
  408. return kStatus_InvalidArgument;
  409. }
  410. /* Add into queue */
  411. handle->queue[handle->queueUser].data = xfer->data;
  412. handle->queue[handle->queueUser].dataSize = xfer->dataSize;
  413. handle->transferSize[handle->queueUser] = xfer->dataSize;
  414. handle->queueUser = (handle->queueUser + 1) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  415. /* Set the state to busy */
  416. handle->state = kFLEXIO_I2S_Busy;
  417. FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
  418. /* Enable Tx transfer */
  419. FLEXIO_I2S_Enable(base, true);
  420. return kStatus_Success;
  421. }
  422. status_t FLEXIO_I2S_TransferReceiveNonBlocking(FLEXIO_I2S_Type *base,
  423. flexio_i2s_handle_t *handle,
  424. flexio_i2s_transfer_t *xfer)
  425. {
  426. assert(handle);
  427. /* Check if the queue is full */
  428. if (handle->queue[handle->queueUser].data)
  429. {
  430. return kStatus_FLEXIO_I2S_QueueFull;
  431. }
  432. if ((xfer->dataSize == 0) || (xfer->data == NULL))
  433. {
  434. return kStatus_InvalidArgument;
  435. }
  436. /* Add into queue */
  437. handle->queue[handle->queueUser].data = xfer->data;
  438. handle->queue[handle->queueUser].dataSize = xfer->dataSize;
  439. handle->transferSize[handle->queueUser] = xfer->dataSize;
  440. handle->queueUser = (handle->queueUser + 1) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  441. /* Set state to busy */
  442. handle->state = kFLEXIO_I2S_Busy;
  443. /* Enable interrupt */
  444. FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
  445. /* Enable Rx transfer */
  446. FLEXIO_I2S_Enable(base, true);
  447. return kStatus_Success;
  448. }
  449. void FLEXIO_I2S_TransferAbortSend(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
  450. {
  451. assert(handle);
  452. /* Stop Tx transfer and disable interrupt */
  453. FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
  454. handle->state = kFLEXIO_I2S_Idle;
  455. /* Clear the queue */
  456. memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
  457. handle->queueDriver = 0;
  458. handle->queueUser = 0;
  459. }
  460. void FLEXIO_I2S_TransferAbortReceive(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
  461. {
  462. assert(handle);
  463. /* Stop rx transfer and disable interrupt */
  464. FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
  465. handle->state = kFLEXIO_I2S_Idle;
  466. /* Clear the queue */
  467. memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
  468. handle->queueDriver = 0;
  469. handle->queueUser = 0;
  470. }
  471. status_t FLEXIO_I2S_TransferGetSendCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
  472. {
  473. assert(handle);
  474. status_t status = kStatus_Success;
  475. if (handle->state != kFLEXIO_I2S_Busy)
  476. {
  477. status = kStatus_NoTransferInProgress;
  478. }
  479. else
  480. {
  481. *count = (handle->transferSize[handle->queueDriver] - handle->queue[handle->queueDriver].dataSize);
  482. }
  483. return status;
  484. }
  485. status_t FLEXIO_I2S_TransferGetReceiveCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
  486. {
  487. assert(handle);
  488. status_t status = kStatus_Success;
  489. if (handle->state != kFLEXIO_I2S_Busy)
  490. {
  491. status = kStatus_NoTransferInProgress;
  492. }
  493. else
  494. {
  495. *count = (handle->transferSize[handle->queueDriver] - handle->queue[handle->queueDriver].dataSize);
  496. }
  497. return status;
  498. }
  499. void FLEXIO_I2S_TransferTxHandleIRQ(void *i2sBase, void *i2sHandle)
  500. {
  501. assert(i2sHandle);
  502. flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
  503. FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
  504. uint8_t *buffer = handle->queue[handle->queueDriver].data;
  505. uint8_t dataSize = handle->bitWidth / 8U;
  506. /* Handle error */
  507. if (FLEXIO_GetShifterErrorFlags(base->flexioBase) & (1U << base->txShifterIndex))
  508. {
  509. FLEXIO_ClearShifterErrorFlags(base->flexioBase, (1U << base->txShifterIndex));
  510. }
  511. /* Handle transfer */
  512. if (((FLEXIO_I2S_GetStatusFlags(base) & kFLEXIO_I2S_TxDataRegEmptyFlag) != 0) &&
  513. (handle->queue[handle->queueDriver].data != NULL))
  514. {
  515. FLEXIO_I2S_WriteNonBlocking(base, handle->bitWidth, buffer, dataSize);
  516. /* Update internal counter */
  517. handle->queue[handle->queueDriver].dataSize -= dataSize;
  518. handle->queue[handle->queueDriver].data += dataSize;
  519. }
  520. /* If finished a blcok, call the callback function */
  521. if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
  522. {
  523. memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
  524. handle->queueDriver = (handle->queueDriver + 1) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  525. if (handle->callback)
  526. {
  527. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  528. }
  529. }
  530. /* If all data finished, just stop the transfer */
  531. if (handle->queue[handle->queueDriver].data == NULL)
  532. {
  533. FLEXIO_I2S_TransferAbortSend(base, handle);
  534. }
  535. }
  536. void FLEXIO_I2S_TransferRxHandleIRQ(void *i2sBase, void *i2sHandle)
  537. {
  538. assert(i2sHandle);
  539. flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
  540. FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
  541. uint8_t *buffer = handle->queue[handle->queueDriver].data;
  542. uint8_t dataSize = handle->bitWidth / 8U;
  543. /* Handle transfer */
  544. if (((FLEXIO_I2S_GetStatusFlags(base) & kFLEXIO_I2S_RxDataRegFullFlag) != 0) &&
  545. (handle->queue[handle->queueDriver].data != NULL))
  546. {
  547. FLEXIO_I2S_ReadNonBlocking(base, handle->bitWidth, buffer, dataSize);
  548. /* Update internal state */
  549. handle->queue[handle->queueDriver].dataSize -= dataSize;
  550. handle->queue[handle->queueDriver].data += dataSize;
  551. }
  552. /* If finished a blcok, call the callback function */
  553. if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
  554. {
  555. memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
  556. handle->queueDriver = (handle->queueDriver + 1) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  557. if (handle->callback)
  558. {
  559. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  560. }
  561. }
  562. /* If all data finished, just stop the transfer */
  563. if (handle->queue[handle->queueDriver].data == NULL)
  564. {
  565. FLEXIO_I2S_TransferAbortReceive(base, handle);
  566. }
  567. }