fsl_flexio_i2s.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_flexio_i2s.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.flexio_i2s"
  12. #endif
  13. /*******************************************************************************
  14. * Definitations
  15. ******************************************************************************/
  16. /*!@brief _sai_transfer_state*/
  17. enum
  18. {
  19. kFLEXIO_I2S_Busy = 0x0U, /*!< FLEXIO_I2S is busy */
  20. kFLEXIO_I2S_Idle, /*!< Transfer is done. */
  21. };
  22. /*******************************************************************************
  23. * Prototypes
  24. ******************************************************************************/
  25. /*!
  26. * @brief Receive a piece of data in non-blocking way.
  27. *
  28. * @param base FLEXIO I2S base pointer
  29. * @param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  30. * @param buffer Pointer to the data to be read.
  31. * @param size Bytes to be read.
  32. */
  33. static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size);
  34. /*!
  35. * @brief sends a piece of data in non-blocking way.
  36. *
  37. * @param base FLEXIO I2S base pointer
  38. * @param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  39. * @param buffer Pointer to the data to be written.
  40. * @param size Bytes to be written.
  41. */
  42. static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size);
  43. /*******************************************************************************
  44. * Variables
  45. ******************************************************************************/
  46. /*******************************************************************************
  47. * Code
  48. ******************************************************************************/
  49. static uint32_t FLEXIO_I2S_GetInstance(FLEXIO_I2S_Type *base)
  50. {
  51. return FLEXIO_GetInstance(base->flexioBase);
  52. }
  53. static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
  54. {
  55. uint32_t i = 0;
  56. uint8_t j = 0;
  57. uint8_t bytesPerWord = bitWidth / 8U;
  58. uint32_t data = 0;
  59. uint32_t temp = 0;
  60. for (i = 0; i < size / bytesPerWord; i++)
  61. {
  62. for (j = 0; j < bytesPerWord; j++)
  63. {
  64. temp = (uint32_t)(*txData);
  65. data |= (temp << (8U * j));
  66. txData++;
  67. }
  68. base->flexioBase->SHIFTBUFBIS[base->txShifterIndex] = data << (32U - bitWidth);
  69. data = 0;
  70. }
  71. }
  72. static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
  73. {
  74. uint32_t i = 0;
  75. uint8_t j = 0;
  76. uint8_t bytesPerWord = bitWidth / 8U;
  77. uint32_t data = 0;
  78. for (i = 0; i < size / bytesPerWord; i++)
  79. {
  80. data = (base->flexioBase->SHIFTBUFBIS[base->rxShifterIndex]);
  81. for (j = 0; j < bytesPerWord; j++)
  82. {
  83. *rxData = (uint8_t)((data >> (8U * j)) & 0xFFU);
  84. rxData++;
  85. }
  86. }
  87. }
  88. /*!
  89. * brief Initializes the FlexIO I2S.
  90. *
  91. * This API configures FlexIO pins and shifter to I2S and configures the FlexIO I2S with a configuration structure.
  92. * The configuration structure can be filled by the user, or be set with default values by
  93. * FLEXIO_I2S_GetDefaultConfig().
  94. *
  95. * note This API should be called at the beginning of the application to use
  96. * the FlexIO I2S driver. Otherwise, any access to the FlexIO I2S module can cause hard fault
  97. * because the clock is not enabled.
  98. *
  99. * param base FlexIO I2S base pointer
  100. * param config FlexIO I2S configure structure.
  101. */
  102. void FLEXIO_I2S_Init(FLEXIO_I2S_Type *base, const flexio_i2s_config_t *config)
  103. {
  104. assert((base != NULL) && (config != NULL));
  105. flexio_shifter_config_t shifterConfig = {0};
  106. flexio_timer_config_t timerConfig = {0};
  107. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  108. /* Ungate flexio clock. */
  109. CLOCK_EnableClock(s_flexioClocks[FLEXIO_I2S_GetInstance(base)]);
  110. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  111. /* reset Flexio */
  112. FLEXIO_Reset(base->flexioBase);
  113. /* Set shifter for I2S Tx data */
  114. shifterConfig.timerSelect = base->bclkTimerIndex;
  115. shifterConfig.pinSelect = base->txPinIndex;
  116. shifterConfig.timerPolarity = config->txTimerPolarity;
  117. shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
  118. shifterConfig.pinPolarity = config->txPinPolarity;
  119. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  120. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  121. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  122. if (config->masterSlave == kFLEXIO_I2S_Master)
  123. {
  124. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnShift;
  125. }
  126. else
  127. {
  128. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  129. }
  130. FLEXIO_SetShifterConfig(base->flexioBase, base->txShifterIndex, &shifterConfig);
  131. /* Set shifter for I2S Rx Data */
  132. shifterConfig.timerSelect = base->bclkTimerIndex;
  133. shifterConfig.pinSelect = base->rxPinIndex;
  134. shifterConfig.timerPolarity = config->rxTimerPolarity;
  135. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  136. shifterConfig.pinPolarity = config->rxPinPolarity;
  137. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  138. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  139. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  140. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  141. FLEXIO_SetShifterConfig(base->flexioBase, base->rxShifterIndex, &shifterConfig);
  142. /* Set Timer to I2S frame sync */
  143. if (config->masterSlave == kFLEXIO_I2S_Master)
  144. {
  145. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->txPinIndex);
  146. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  147. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceExternal;
  148. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  149. timerConfig.pinSelect = base->fsPinIndex;
  150. timerConfig.pinPolarity = config->fsPinPolarity;
  151. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  152. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  153. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  154. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  155. timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
  156. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPrevTimerEnable;
  157. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  158. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  159. }
  160. else
  161. {
  162. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->bclkPinIndex);
  163. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  164. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  165. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  166. timerConfig.pinSelect = base->fsPinIndex;
  167. timerConfig.pinPolarity = config->fsPinPolarity;
  168. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  169. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  170. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnTriggerInputShiftTriggerInput;
  171. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  172. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  173. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdge;
  174. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  175. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  176. }
  177. FLEXIO_SetTimerConfig(base->flexioBase, base->fsTimerIndex, &timerConfig);
  178. /* Set Timer to I2S bit clock */
  179. if (config->masterSlave == kFLEXIO_I2S_Master)
  180. {
  181. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->txShifterIndex);
  182. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  183. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  184. timerConfig.pinSelect = base->bclkPinIndex;
  185. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  186. timerConfig.pinPolarity = config->bclkPinPolarity;
  187. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  188. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  189. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  190. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  191. timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
  192. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
  193. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  194. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  195. }
  196. else
  197. {
  198. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_TIMn(base->fsTimerIndex);
  199. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  200. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  201. timerConfig.pinSelect = base->bclkPinIndex;
  202. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  203. timerConfig.pinPolarity = config->bclkPinPolarity;
  204. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  205. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  206. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
  207. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  208. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompareTriggerLow;
  209. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdgeTriggerHigh;
  210. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  211. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  212. }
  213. FLEXIO_SetTimerConfig(base->flexioBase, base->bclkTimerIndex, &timerConfig);
  214. /* If enable flexio I2S */
  215. if (config->enableI2S)
  216. {
  217. base->flexioBase->CTRL |= FLEXIO_CTRL_FLEXEN_MASK;
  218. }
  219. else
  220. {
  221. base->flexioBase->CTRL &= ~FLEXIO_CTRL_FLEXEN_MASK;
  222. }
  223. }
  224. /*!
  225. * brief Sets the FlexIO I2S configuration structure to default values.
  226. *
  227. * The purpose of this API is to get the configuration structure initialized for use in FLEXIO_I2S_Init().
  228. * Users may use the initialized structure unchanged in FLEXIO_I2S_Init() or modify
  229. * some fields of the structure before calling FLEXIO_I2S_Init().
  230. *
  231. * param config pointer to master configuration structure
  232. */
  233. void FLEXIO_I2S_GetDefaultConfig(flexio_i2s_config_t *config)
  234. {
  235. /* Initializes the configure structure to zero. */
  236. (void)memset(config, 0, sizeof(*config));
  237. config->masterSlave = kFLEXIO_I2S_Master;
  238. config->enableI2S = true;
  239. config->txPinPolarity = kFLEXIO_PinActiveHigh;
  240. config->rxPinPolarity = kFLEXIO_PinActiveHigh;
  241. config->bclkPinPolarity = kFLEXIO_PinActiveHigh;
  242. config->fsPinPolarity = kFLEXIO_PinActiveLow;
  243. config->txTimerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  244. config->rxTimerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  245. }
  246. /*!
  247. * brief De-initializes the FlexIO I2S.
  248. *
  249. * Calling this API resets the FlexIO I2S shifter and timer config. After calling this API,
  250. * call the FLEXO_I2S_Init to use the FlexIO I2S module.
  251. *
  252. * param base FlexIO I2S base pointer
  253. */
  254. void FLEXIO_I2S_Deinit(FLEXIO_I2S_Type *base)
  255. {
  256. base->flexioBase->SHIFTCFG[base->txShifterIndex] = 0;
  257. base->flexioBase->SHIFTCTL[base->txShifterIndex] = 0;
  258. base->flexioBase->SHIFTCFG[base->rxShifterIndex] = 0;
  259. base->flexioBase->SHIFTCTL[base->rxShifterIndex] = 0;
  260. base->flexioBase->TIMCFG[base->fsTimerIndex] = 0;
  261. base->flexioBase->TIMCMP[base->fsTimerIndex] = 0;
  262. base->flexioBase->TIMCTL[base->fsTimerIndex] = 0;
  263. base->flexioBase->TIMCFG[base->bclkTimerIndex] = 0;
  264. base->flexioBase->TIMCMP[base->bclkTimerIndex] = 0;
  265. base->flexioBase->TIMCTL[base->bclkTimerIndex] = 0;
  266. }
  267. /*!
  268. * brief Enables the FlexIO I2S interrupt.
  269. *
  270. * This function enables the FlexIO UART interrupt.
  271. *
  272. * param base Pointer to FLEXIO_I2S_Type structure
  273. * param mask interrupt source
  274. */
  275. void FLEXIO_I2S_EnableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
  276. {
  277. if ((mask & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyInterruptEnable) != 0UL)
  278. {
  279. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1UL << base->txShifterIndex);
  280. }
  281. if ((mask & (uint32_t)kFLEXIO_I2S_RxDataRegFullInterruptEnable) != 0UL)
  282. {
  283. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1UL << base->rxShifterIndex);
  284. }
  285. }
  286. /*!
  287. * brief Gets the FlexIO I2S status flags.
  288. *
  289. * param base Pointer to FLEXIO_I2S_Type structure
  290. * return Status flag, which are ORed by the enumerators in the _flexio_i2s_status_flags.
  291. */
  292. uint32_t FLEXIO_I2S_GetStatusFlags(FLEXIO_I2S_Type *base)
  293. {
  294. uint32_t status = 0;
  295. status = ((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->txShifterIndex)) >> base->txShifterIndex);
  296. status |=
  297. (((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) >> (base->rxShifterIndex))
  298. << 1U);
  299. return status;
  300. }
  301. /*!
  302. * brief Disables the FlexIO I2S interrupt.
  303. *
  304. * This function enables the FlexIO UART interrupt.
  305. *
  306. * param base pointer to FLEXIO_I2S_Type structure
  307. * param mask interrupt source
  308. */
  309. void FLEXIO_I2S_DisableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
  310. {
  311. if ((mask & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyInterruptEnable) != 0UL)
  312. {
  313. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1UL << base->txShifterIndex);
  314. }
  315. if ((mask & (uint32_t)kFLEXIO_I2S_RxDataRegFullInterruptEnable) != 0UL)
  316. {
  317. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1UL << base->rxShifterIndex);
  318. }
  319. }
  320. /*!
  321. * brief Configures the FlexIO I2S audio format in master mode.
  322. *
  323. * Audio format can be changed in run-time of FlexIO I2S. This function configures the sample rate and audio data
  324. * format to be transferred.
  325. *
  326. * param base Pointer to FLEXIO_I2S_Type structure
  327. * param format Pointer to FlexIO I2S audio data format structure.
  328. * param srcClock_Hz I2S master clock source frequency in Hz.
  329. */
  330. void FLEXIO_I2S_MasterSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format, uint32_t srcClock_Hz)
  331. {
  332. uint32_t timDiv = srcClock_Hz / (format->sampleRate_Hz * format->bitWidth * 2U);
  333. uint32_t bclkDiv = 0;
  334. /* Shall keep bclk and fs div an integer */
  335. if ((timDiv % 2UL) != 0UL)
  336. {
  337. timDiv += 1U;
  338. }
  339. /* Set Frame sync timer cmp */
  340. base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * timDiv - 1U);
  341. /* Set bit clock timer cmp */
  342. bclkDiv = ((timDiv / 2U - 1U) | ((format->bitWidth * 2UL - 1UL) << 8U));
  343. base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(bclkDiv);
  344. }
  345. /*!
  346. * brief Configures the FlexIO I2S audio format in slave mode.
  347. *
  348. * Audio format can be changed in run-time of FlexIO I2S. This function configures the sample rate and audio data
  349. * format to be transferred.
  350. *
  351. * param base Pointer to FLEXIO_I2S_Type structure
  352. * param format Pointer to FlexIO I2S audio data format structure.
  353. */
  354. void FLEXIO_I2S_SlaveSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format)
  355. {
  356. /* Set Frame sync timer cmp */
  357. base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * 4UL - 3UL);
  358. /* Set bit clock timer cmp */
  359. base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * 2UL - 1UL);
  360. }
  361. /*!
  362. * brief Sends data using a blocking method.
  363. *
  364. * note This function blocks via polling until data is ready to be sent.
  365. *
  366. * param base FlexIO I2S base pointer.
  367. * param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  368. * param txData Pointer to the data to be written.
  369. * param size Bytes to be written.
  370. * retval kStatus_Success Successfully write data.
  371. * retval kStatus_FLEXIO_I2C_Timeout Timeout polling status flags.
  372. */
  373. status_t FLEXIO_I2S_WriteBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
  374. {
  375. uint32_t i = 0;
  376. uint8_t bytesPerWord = bitWidth / 8U;
  377. #if I2S_RETRY_TIMES
  378. uint32_t waitTimes = I2S_RETRY_TIMES;
  379. #endif
  380. for (i = 0; i < size / bytesPerWord; i++)
  381. {
  382. /* Wait until it can write data */
  383. #if I2S_RETRY_TIMES
  384. waitTimes = I2S_RETRY_TIMES;
  385. while (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL) &&
  386. (--waitTimes != 0U))
  387. {
  388. }
  389. if (waitTimes == 0U)
  390. {
  391. return kStatus_FLEXIO_I2S_Timeout;
  392. }
  393. #else
  394. while ((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL)
  395. {
  396. }
  397. #endif
  398. FLEXIO_I2S_WriteNonBlocking(base, bitWidth, txData, bytesPerWord);
  399. txData = (uint8_t *)((uint32_t)txData + bytesPerWord);
  400. }
  401. /* Wait until the last data is sent */
  402. #if I2S_RETRY_TIMES
  403. waitTimes = I2S_RETRY_TIMES;
  404. while (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL) && (--waitTimes != 0U))
  405. {
  406. }
  407. if (waitTimes == 0U)
  408. {
  409. return kStatus_FLEXIO_I2S_Timeout;
  410. }
  411. #else
  412. while ((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL)
  413. {
  414. }
  415. #endif
  416. return kStatus_Success;
  417. }
  418. /*!
  419. * brief Receives a piece of data using a blocking method.
  420. *
  421. * note This function blocks via polling until data is ready to be sent.
  422. *
  423. * param base FlexIO I2S base pointer
  424. * param bitWidth How many bits in a audio word, usually 8/16/24/32 bits.
  425. * param rxData Pointer to the data to be read.
  426. * param size Bytes to be read.
  427. * retval kStatus_Success Successfully read data.
  428. * retval kStatus_FLEXIO_I2C_Timeout Timeout polling status flags.
  429. */
  430. status_t FLEXIO_I2S_ReadBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
  431. {
  432. uint32_t i = 0;
  433. uint8_t bytesPerWord = bitWidth / 8U;
  434. #if I2S_RETRY_TIMES
  435. uint32_t waitTimes = I2S_RETRY_TIMES;
  436. #endif
  437. for (i = 0; i < size / bytesPerWord; i++)
  438. {
  439. /* Wait until data is received */
  440. #if I2S_RETRY_TIMES
  441. waitTimes = I2S_RETRY_TIMES;
  442. while ((!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) != 0UL)) &&
  443. (--waitTimes != 0U))
  444. {
  445. }
  446. if (waitTimes == 0U)
  447. {
  448. return kStatus_FLEXIO_I2S_Timeout;
  449. }
  450. #else
  451. while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) != 0UL))
  452. {
  453. }
  454. #endif
  455. FLEXIO_I2S_ReadNonBlocking(base, bitWidth, rxData, bytesPerWord);
  456. rxData = (uint8_t *)((uint32_t)rxData + bytesPerWord);
  457. }
  458. return kStatus_Success;
  459. }
  460. /*!
  461. * brief Initializes the FlexIO I2S handle.
  462. *
  463. * This function initializes the FlexIO I2S handle which can be used for other
  464. * FlexIO I2S transactional APIs. Call this API once to get the
  465. * initialized handle.
  466. *
  467. * param base Pointer to FLEXIO_I2S_Type structure
  468. * param handle Pointer to flexio_i2s_handle_t structure to store the transfer state.
  469. * param callback FlexIO I2S callback function, which is called while finished a block.
  470. * param userData User parameter for the FlexIO I2S callback.
  471. */
  472. void FLEXIO_I2S_TransferTxCreateHandle(FLEXIO_I2S_Type *base,
  473. flexio_i2s_handle_t *handle,
  474. flexio_i2s_callback_t callback,
  475. void *userData)
  476. {
  477. assert(handle != NULL);
  478. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  479. /* Zero the handle. */
  480. (void)memset(handle, 0, sizeof(*handle));
  481. /* Store callback and user data. */
  482. handle->callback = callback;
  483. handle->userData = userData;
  484. /* Save the context in global variables to support the double weak mechanism. */
  485. (void)FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferTxHandleIRQ);
  486. /* Set the TX/RX state. */
  487. handle->state = (uint32_t)kFLEXIO_I2S_Idle;
  488. /* Enable interrupt in NVIC. */
  489. (void)EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
  490. }
  491. /*!
  492. * brief Initializes the FlexIO I2S receive handle.
  493. *
  494. * This function initializes the FlexIO I2S handle which can be used for other
  495. * FlexIO I2S transactional APIs. Call this API once to get the
  496. * initialized handle.
  497. *
  498. * param base Pointer to FLEXIO_I2S_Type structure.
  499. * param handle Pointer to flexio_i2s_handle_t structure to store the transfer state.
  500. * param callback FlexIO I2S callback function, which is called while finished a block.
  501. * param userData User parameter for the FlexIO I2S callback.
  502. */
  503. void FLEXIO_I2S_TransferRxCreateHandle(FLEXIO_I2S_Type *base,
  504. flexio_i2s_handle_t *handle,
  505. flexio_i2s_callback_t callback,
  506. void *userData)
  507. {
  508. assert(handle != NULL);
  509. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  510. /* Zero the handle. */
  511. (void)memset(handle, 0, sizeof(*handle));
  512. /* Store callback and user data. */
  513. handle->callback = callback;
  514. handle->userData = userData;
  515. /* Save the context in global variables to support the double weak mechanism. */
  516. (void)FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferRxHandleIRQ);
  517. /* Set the TX/RX state. */
  518. handle->state = (uint32_t)kFLEXIO_I2S_Idle;
  519. /* Enable interrupt in NVIC. */
  520. (void)EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
  521. }
  522. /*!
  523. * brief Configures the FlexIO I2S audio format.
  524. *
  525. * Audio format can be changed at run-time of FlexIO I2S. This function configures the sample rate and audio data
  526. * format to be transferred.
  527. *
  528. * param base Pointer to FLEXIO_I2S_Type structure.
  529. * param handle FlexIO I2S handle pointer.
  530. * param format Pointer to audio data format structure.
  531. * param srcClock_Hz FlexIO I2S bit clock source frequency in Hz. This parameter should be 0 while in slave mode.
  532. */
  533. void FLEXIO_I2S_TransferSetFormat(FLEXIO_I2S_Type *base,
  534. flexio_i2s_handle_t *handle,
  535. flexio_i2s_format_t *format,
  536. uint32_t srcClock_Hz)
  537. {
  538. assert((handle != NULL) && (format != NULL));
  539. /* Set the bitWidth to handle */
  540. handle->bitWidth = format->bitWidth;
  541. /* Set sample rate */
  542. if (srcClock_Hz != 0UL)
  543. {
  544. /* It is master */
  545. FLEXIO_I2S_MasterSetFormat(base, format, srcClock_Hz);
  546. }
  547. else
  548. {
  549. FLEXIO_I2S_SlaveSetFormat(base, format);
  550. }
  551. }
  552. /*!
  553. * brief Performs an interrupt non-blocking send transfer on FlexIO I2S.
  554. *
  555. * note The API returns immediately after transfer initiates.
  556. * Call FLEXIO_I2S_GetRemainingBytes to poll the transfer status and check whether
  557. * the transfer is finished. If the return status is 0, the transfer is finished.
  558. *
  559. * param base Pointer to FLEXIO_I2S_Type structure.
  560. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  561. * param xfer Pointer to flexio_i2s_transfer_t structure
  562. * retval kStatus_Success Successfully start the data transmission.
  563. * retval kStatus_FLEXIO_I2S_TxBusy Previous transmission still not finished, data not all written to TX register yet.
  564. * retval kStatus_InvalidArgument The input parameter is invalid.
  565. */
  566. status_t FLEXIO_I2S_TransferSendNonBlocking(FLEXIO_I2S_Type *base,
  567. flexio_i2s_handle_t *handle,
  568. flexio_i2s_transfer_t *xfer)
  569. {
  570. assert(handle != NULL);
  571. /* Check if the queue is full */
  572. if (handle->queue[handle->queueUser].data != NULL)
  573. {
  574. return kStatus_FLEXIO_I2S_QueueFull;
  575. }
  576. if ((xfer->dataSize == 0U) || (xfer->data == NULL))
  577. {
  578. return kStatus_InvalidArgument;
  579. }
  580. /* Add into queue */
  581. handle->queue[handle->queueUser].data = xfer->data;
  582. handle->queue[handle->queueUser].dataSize = xfer->dataSize;
  583. handle->transferSize[handle->queueUser] = xfer->dataSize;
  584. handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  585. /* Set the state to busy */
  586. handle->state = (uint32_t)kFLEXIO_I2S_Busy;
  587. FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
  588. /* Enable Tx transfer */
  589. FLEXIO_I2S_Enable(base, true);
  590. return kStatus_Success;
  591. }
  592. /*!
  593. * brief Performs an interrupt non-blocking receive transfer on FlexIO I2S.
  594. *
  595. * note The API returns immediately after transfer initiates.
  596. * Call FLEXIO_I2S_GetRemainingBytes to poll the transfer status to check whether
  597. * the transfer is finished. If the return status is 0, the transfer is finished.
  598. *
  599. * param base Pointer to FLEXIO_I2S_Type structure.
  600. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  601. * param xfer Pointer to flexio_i2s_transfer_t structure
  602. * retval kStatus_Success Successfully start the data receive.
  603. * retval kStatus_FLEXIO_I2S_RxBusy Previous receive still not finished.
  604. * retval kStatus_InvalidArgument The input parameter is invalid.
  605. */
  606. status_t FLEXIO_I2S_TransferReceiveNonBlocking(FLEXIO_I2S_Type *base,
  607. flexio_i2s_handle_t *handle,
  608. flexio_i2s_transfer_t *xfer)
  609. {
  610. assert(handle != NULL);
  611. /* Check if the queue is full */
  612. if (handle->queue[handle->queueUser].data != NULL)
  613. {
  614. return kStatus_FLEXIO_I2S_QueueFull;
  615. }
  616. if ((xfer->dataSize == 0U) || (xfer->data == NULL))
  617. {
  618. return kStatus_InvalidArgument;
  619. }
  620. /* Add into queue */
  621. handle->queue[handle->queueUser].data = xfer->data;
  622. handle->queue[handle->queueUser].dataSize = xfer->dataSize;
  623. handle->transferSize[handle->queueUser] = xfer->dataSize;
  624. handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  625. /* Set state to busy */
  626. handle->state = (uint32_t)kFLEXIO_I2S_Busy;
  627. /* Enable interrupt */
  628. FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
  629. /* Enable Rx transfer */
  630. FLEXIO_I2S_Enable(base, true);
  631. return kStatus_Success;
  632. }
  633. /*!
  634. * brief Aborts the current send.
  635. *
  636. * note This API can be called at any time when interrupt non-blocking transfer initiates
  637. * to abort the transfer in a early time.
  638. *
  639. * param base Pointer to FLEXIO_I2S_Type structure.
  640. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  641. */
  642. void FLEXIO_I2S_TransferAbortSend(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
  643. {
  644. assert(handle != NULL);
  645. /* Stop Tx transfer and disable interrupt */
  646. FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
  647. handle->state = (uint32_t)kFLEXIO_I2S_Idle;
  648. /* Clear the queue */
  649. (void)memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
  650. handle->queueDriver = 0;
  651. handle->queueUser = 0;
  652. }
  653. /*!
  654. * brief Aborts the current receive.
  655. *
  656. * note This API can be called at any time when interrupt non-blocking transfer initiates
  657. * to abort the transfer in a early time.
  658. *
  659. * param base Pointer to FLEXIO_I2S_Type structure.
  660. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  661. */
  662. void FLEXIO_I2S_TransferAbortReceive(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
  663. {
  664. assert(handle != NULL);
  665. /* Stop rx transfer and disable interrupt */
  666. FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
  667. handle->state = (uint32_t)kFLEXIO_I2S_Idle;
  668. /* Clear the queue */
  669. (void)memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
  670. handle->queueDriver = 0;
  671. handle->queueUser = 0;
  672. }
  673. /*!
  674. * brief Gets the remaining bytes to be sent.
  675. *
  676. * param base Pointer to FLEXIO_I2S_Type structure.
  677. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  678. * param count Bytes sent.
  679. * retval kStatus_Success Succeed get the transfer count.
  680. * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  681. */
  682. status_t FLEXIO_I2S_TransferGetSendCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
  683. {
  684. assert(handle != NULL);
  685. status_t status = kStatus_Success;
  686. uint8_t queueDriver = handle->queueDriver;
  687. if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
  688. {
  689. status = kStatus_NoTransferInProgress;
  690. }
  691. else
  692. {
  693. *count = (handle->transferSize[queueDriver] - handle->queue[queueDriver].dataSize);
  694. }
  695. return status;
  696. }
  697. /*!
  698. * brief Gets the remaining bytes to be received.
  699. *
  700. * param base Pointer to FLEXIO_I2S_Type structure.
  701. * param handle Pointer to flexio_i2s_handle_t structure which stores the transfer state
  702. * return count Bytes received.
  703. * retval kStatus_Success Succeed get the transfer count.
  704. * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  705. */
  706. status_t FLEXIO_I2S_TransferGetReceiveCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
  707. {
  708. assert(handle != NULL);
  709. status_t status = kStatus_Success;
  710. uint8_t queueDriver = handle->queueDriver;
  711. if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
  712. {
  713. status = kStatus_NoTransferInProgress;
  714. }
  715. else
  716. {
  717. *count = (handle->transferSize[queueDriver] - handle->queue[queueDriver].dataSize);
  718. }
  719. return status;
  720. }
  721. /*!
  722. * brief Tx interrupt handler.
  723. *
  724. * param i2sBase Pointer to FLEXIO_I2S_Type structure.
  725. * param i2sHandle Pointer to flexio_i2s_handle_t structure
  726. */
  727. void FLEXIO_I2S_TransferTxHandleIRQ(void *i2sBase, void *i2sHandle)
  728. {
  729. assert(i2sHandle != NULL);
  730. flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
  731. FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
  732. uint8_t *buffer = handle->queue[handle->queueDriver].data;
  733. uint8_t dataSize = handle->bitWidth / 8U;
  734. /* Handle error */
  735. if ((FLEXIO_GetShifterErrorFlags(base->flexioBase) & (1UL << base->txShifterIndex)) != 0UL)
  736. {
  737. FLEXIO_ClearShifterErrorFlags(base->flexioBase, (1UL << base->txShifterIndex));
  738. }
  739. /* Handle transfer */
  740. if (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) != 0UL) &&
  741. (handle->queue[handle->queueDriver].data != NULL))
  742. {
  743. FLEXIO_I2S_WriteNonBlocking(base, handle->bitWidth, buffer, dataSize);
  744. /* Update internal counter */
  745. handle->queue[handle->queueDriver].dataSize -= dataSize;
  746. handle->queue[handle->queueDriver].data =
  747. (uint8_t *)((uint32_t)handle->queue[handle->queueDriver].data + dataSize);
  748. }
  749. /* If finished a block, call the callback function */
  750. if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
  751. {
  752. (void)memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
  753. handle->queueDriver = (handle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  754. if (handle->callback != NULL)
  755. {
  756. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  757. }
  758. }
  759. /* If all data finished, just stop the transfer */
  760. if (handle->queue[handle->queueDriver].data == NULL)
  761. {
  762. FLEXIO_I2S_TransferAbortSend(base, handle);
  763. }
  764. }
  765. /*!
  766. * brief Rx interrupt handler.
  767. *
  768. * param i2sBase Pointer to FLEXIO_I2S_Type structure.
  769. * param i2sHandle Pointer to flexio_i2s_handle_t structure.
  770. */
  771. void FLEXIO_I2S_TransferRxHandleIRQ(void *i2sBase, void *i2sHandle)
  772. {
  773. assert(i2sHandle != NULL);
  774. flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
  775. FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
  776. uint8_t *buffer = handle->queue[handle->queueDriver].data;
  777. uint8_t dataSize = handle->bitWidth / 8U;
  778. /* Handle transfer */
  779. if (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_RxDataRegFullFlag) != 0UL) &&
  780. (handle->queue[handle->queueDriver].data != NULL))
  781. {
  782. FLEXIO_I2S_ReadNonBlocking(base, handle->bitWidth, buffer, dataSize);
  783. /* Update internal state */
  784. handle->queue[handle->queueDriver].dataSize -= dataSize;
  785. handle->queue[handle->queueDriver].data =
  786. (uint8_t *)((uint32_t)handle->queue[handle->queueDriver].data + dataSize);
  787. }
  788. /* If finished a block, call the callback function */
  789. if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
  790. {
  791. (void)memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
  792. handle->queueDriver = (handle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
  793. if (handle->callback != NULL)
  794. {
  795. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  796. }
  797. }
  798. /* If all data finished, just stop the transfer */
  799. if (handle->queue[handle->queueDriver].data == NULL)
  800. {
  801. FLEXIO_I2S_TransferAbortReceive(base, handle);
  802. }
  803. }