stm322xg_eval_audio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_audio.c
  4. * @author MCD Application Team
  5. * @brief This file provides the Audio driver for the STM322xG-EVAL evaluation
  6. * board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /*==============================================================================
  37. User NOTES
  38. How To use this driver:
  39. -----------------------
  40. + This driver supports STM32F2xx devices on STM322xG-EVAL Evaluation board.
  41. + Call the function BSP_AUDIO_OUT_Init(
  42. OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER,
  43. OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_AUTO or
  44. OUTPUT_DEVICE_BOTH)
  45. Volume: initial volume to be set (0 is min (mute), 100 is max (100%)
  46. AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...)
  47. this parameter is relative to the audio file/stream type.
  48. )
  49. This function configures all the hardware required for the audio application (codec, I2C, I2S,
  50. GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK.
  51. If the returned value is different from 0 or the function is stuck then the communication with
  52. the codec or the IOExpander has failed (try to un-plug the power or reset device in this case).
  53. - OUTPUT_DEVICE_SPEAKER: only speaker will be set as output for the audio stream.
  54. - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream.
  55. - OUTPUT_DEVICE_AUTO: Selection of output device is made through external switch (implemented
  56. into the audio jack on the evaluation board). When the Headphone is connected it is used
  57. as output. When the headphone is disconnected from the audio jack, the output is
  58. automatically switched to Speaker.
  59. - OUTPUT_DEVICE_BOTH: both Speaker and Headphone are used as outputs for the audio stream
  60. at the same time.
  61. + Call the function BSP_AUDIO_OUT_Play(
  62. pBuffer: pointer to the audio data file address
  63. Size: size of the buffer to be sent in Bytes
  64. )
  65. to start playing (for the first time) from the audio file/stream.
  66. + Call the function BSP_AUDIO_OUT_Pause() to pause playing
  67. + Call the function BSP_AUDIO_OUT_Resume() to resume playing.
  68. Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called
  69. for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case).
  70. Note. This function should be called only when the audio file is played or paused (not stopped).
  71. + For each mode, you may need to implement the relative callback functions into your code.
  72. The Callback functions are named AUDIO_OUT_XXX_CallBack() and only their prototypes are declared in
  73. the stm322xg_eval_audio.h file. (refer to the example for more details on the callbacks implementations)
  74. + To Stop playing, to modify the volume level or to mute, use the functions
  75. BSP_AUDIO_OUT_Stop(), BSP_AUDIO_OUT_SetVolume(), AUDIO_OUT_SetFrequency() BSP_AUDIO_OUT_SetOutputMode and BSP_AUDIO_OUT_SetMute().
  76. + The driver API and the callback functions are at the end of the stm322xg_eval_audio.h file.
  77. Driver architecture:
  78. -------------------
  79. + This driver provide the High Audio Layer: consists of the function API exported in the stm322xg_eval_audio.h file
  80. (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...)
  81. + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/
  82. providing the audio file/stream. These functions are also included as local functions into
  83. the stm322xg_eval_audio_codec.c file (I2Sx_MspInit() and I2Sx_Init())
  84. Known Limitations:
  85. -------------------
  86. 1- When using the Speaker, if the audio file quality is not high enough, the speaker output
  87. may produce high and uncomfortable noise level. To avoid this issue, to use speaker
  88. output properly, try to increase audio file sampling rate (typically higher than 48KHz).
  89. This operation will lead to larger file size.
  90. 2- Communication with the audio codec (through I2C) may be corrupted if it is interrupted by some
  91. user interrupt routines (in this case, interrupts could be disabled just before the start of
  92. communication then re-enabled when it is over). Note that this communication is only done at
  93. the configuration phase (BSP_AUDIO_OUT_Init() or BSP_AUDIO_OUT_Stop()) and when Volume control modification is
  94. performed (BSP_AUDIO_OUT_SetVolume() or AUDIO_OUT_Mute() or BSP_AUDIO_OUT_SetOutputMode()).
  95. When the audio data is played, no communication is required with the audio codec.
  96. 3- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size,
  97. File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file.
  98. 4- Supports only Stereo audio streaming. To play mono audio streams, each data should be sent twice
  99. on the I2S or should be duplicated on the source buffer. Or convert the stream in stereo before playing.
  100. 5- Supports only 16-bits audio data size.
  101. ==============================================================================*/
  102. /* Includes ------------------------------------------------------------------*/
  103. #include "stm322xg_eval_audio.h"
  104. #include "stm322xg_eval_io.h" /* IOExpander driver is included in order to allow
  105. CS43L22 codec reset pin managment on the evaluation board */
  106. /** @addtogroup BSP
  107. * @{
  108. */
  109. /** @addtogroup STM322xG_EVAL
  110. * @{
  111. */
  112. /** @defgroup STM322xG_EVAL_AUDIO STM322xG EVAL AUDIO
  113. * @brief This file includes the low layer audio driver available on STM322xG-EVAL
  114. * evaluation board.
  115. * @{
  116. */
  117. /** @defgroup STM322xG_EVAL_AUDIO_Private_Defines STM322xG EVAL AUDIO Private Defines
  118. * @{
  119. */
  120. /* These PLL parameters are valide when the f(VCO clock) = 1Mhz */
  121. const uint32_t I2SFreq[8] = {8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000};
  122. const uint32_t I2SPLLN[8] = {256, 429, 213, 429, 426, 271, 258, 344};
  123. const uint32_t I2SPLLR[8] = {5, 4, 4, 4, 4, 6, 3, 1};
  124. /**
  125. * @}
  126. */
  127. /** @defgroup STM322xG_EVAL_AUDIO_Private_Variables STM322xG EVAL AUDIO Private Variables
  128. * @{
  129. */
  130. AUDIO_DrvTypeDef *audio_drv;
  131. I2S_HandleTypeDef haudio_i2s;
  132. /**
  133. * @}
  134. */
  135. /** @defgroup STM322xG_EVAL_AUDIO_Private_Function_Prototypes STM322xG EVAL AUDIO Private Function Prototypes
  136. * @{
  137. */
  138. static void I2Sx_Init(uint32_t AudioFreq);
  139. static void I2Sx_DeInit(void);
  140. static void CODEC_Reset(void);
  141. /**
  142. * @}
  143. */
  144. /** @defgroup STM322xG_EVAL_AUDIO_Private_Functions STM322xG EVAL AUDIO Private Functions
  145. * @{
  146. */
  147. /**
  148. * @brief Configures the audio peripherals.
  149. * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
  150. * OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO .
  151. * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
  152. * @param AudioFreq: Audio frequency used to play the audio stream.
  153. * @note This function configure also that the I2S PLL input clock.
  154. * @retval 0 if correct communication, else wrong communication
  155. */
  156. uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
  157. {
  158. uint8_t ret = AUDIO_ERROR;
  159. /* Disable I2S */
  160. I2Sx_DeInit();
  161. /* Configure PLL clock depending on AudioFreq */
  162. BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
  163. /* I2S data transfer preparation:
  164. Prepare the Media to be used for the audio transfer from memory to I2S peripheral */
  165. haudio_i2s.Instance = AUDIO_I2Sx;
  166. if(HAL_I2S_GetState(&haudio_i2s) == HAL_I2S_STATE_RESET)
  167. {
  168. /* Init the I2S MSP: this __weak function can be redefined by the application*/
  169. BSP_AUDIO_OUT_MspInit(&haudio_i2s, NULL);
  170. }
  171. /* Configure the I2S peripheral */
  172. I2Sx_Init(AudioFreq);
  173. /* Reset the Codec Registers */
  174. CODEC_Reset();
  175. if(((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID)
  176. {
  177. /* Initialize the audio driver structure */
  178. audio_drv = &cs43l22_drv;
  179. ret = AUDIO_OK;
  180. }
  181. else
  182. {
  183. ret = AUDIO_ERROR;
  184. }
  185. if(ret == AUDIO_OK)
  186. {
  187. audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq);
  188. }
  189. return ret;
  190. }
  191. /**
  192. * @brief De-initialize the audio peripherals.
  193. * @retval None
  194. */
  195. void BSP_AUDIO_OUT_DeInit(void)
  196. {
  197. I2Sx_DeInit();
  198. /* DeInit the I2S MSP : this __weak function can be rewritten by the application */
  199. BSP_AUDIO_OUT_MspDeInit(&haudio_i2s, NULL);
  200. }
  201. /**
  202. * @brief Starts playing audio stream from a data buffer for a determined size.
  203. * @param pBuffer: Pointer to the buffer
  204. * @param Size: Number of audio data BYTES.
  205. * @retval AUDIO_OK if correct communication, else wrong communication
  206. */
  207. uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size)
  208. {
  209. /* Call the audio Codec Play function */
  210. if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
  211. {
  212. return AUDIO_ERROR;
  213. }
  214. else
  215. {
  216. /* Update the Media layer and enable it for play */
  217. HAL_I2S_Transmit_DMA(&haudio_i2s, pBuffer, DMA_MAX(Size/AUDIODATA_SIZE));
  218. return AUDIO_OK;
  219. }
  220. }
  221. /**
  222. * @brief Sends n-Bytes on the I2S interface.
  223. * @param pData: Pointer to data address
  224. * @param Size: Number of data to be written.
  225. */
  226. void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
  227. {
  228. HAL_I2S_Transmit_DMA(&haudio_i2s, pData, Size);
  229. }
  230. /**
  231. * @brief Pauses the audio file stream.
  232. * In case of using DMA, the DMA Pause feature is used.
  233. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only
  234. * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
  235. * function for resume could lead to unexpected behavior).
  236. * @retval AUDIO_OK if correct communication, else wrong communication
  237. */
  238. uint8_t BSP_AUDIO_OUT_Pause(void)
  239. {
  240. /* Call the Audio Codec Pause/Resume function */
  241. if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0)
  242. {
  243. return AUDIO_ERROR;
  244. }
  245. else
  246. {
  247. /* Call the Media layer pause function */
  248. HAL_I2S_DMAPause(&haudio_i2s);
  249. /* Return AUDIO_OK when all operations are correctly done */
  250. return AUDIO_OK;
  251. }
  252. }
  253. /**
  254. * @brief Resumes the audio file stream.
  255. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only
  256. * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
  257. * function for resume could lead to unexpected behavior).
  258. * @retval AUDIO_OK if correct communication, else wrong communication
  259. */
  260. uint8_t BSP_AUDIO_OUT_Resume(void)
  261. {
  262. /* Call the Audio Codec Pause/Resume function */
  263. if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0)
  264. {
  265. return AUDIO_ERROR;
  266. }
  267. else
  268. {
  269. /* Call the Media layer pause/resume function */
  270. HAL_I2S_DMAResume(&haudio_i2s);
  271. /* Return AUDIO_OK when all operations are correctly done */
  272. return AUDIO_OK;
  273. }
  274. }
  275. /**
  276. * @brief Stops audio playing and Power down the Audio Codec.
  277. * @param Option: could be one of the following parameters
  278. * - CODEC_PDWN_SW: for software power off (by writing registers).
  279. * Then no need to reconfigure the Codec after power on.
  280. * - CODEC_PDWN_HW: completely shut down the codec (physically).
  281. * Then need to reconfigure the Codec after power on.
  282. * @retval AUDIO_OK if correct communication, else wrong communication
  283. */
  284. uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
  285. {
  286. /* Call the Media layer stop function */
  287. HAL_I2S_DMAStop(&haudio_i2s);
  288. /* Call Audio Codec Stop function */
  289. if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
  290. {
  291. return AUDIO_ERROR;
  292. }
  293. else
  294. {
  295. if(Option == CODEC_PDWN_HW)
  296. {
  297. /* Wait at least 1ms */
  298. HAL_Delay(1);
  299. /* Reset the pin */
  300. BSP_IO_WritePin(AUDIO_RESET_PIN, RESET);
  301. }
  302. /* Return AUDIO_OK when all operations are correctly done */
  303. return AUDIO_OK;
  304. }
  305. }
  306. /**
  307. * @brief Controls the current audio volume level.
  308. * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
  309. * Mute and 100 for Max volume level).
  310. * @retval AUDIO_OK if correct communication, else wrong communication
  311. */
  312. uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
  313. {
  314. /* Call the codec volume control function with converted volume value */
  315. if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
  316. {
  317. return AUDIO_ERROR;
  318. }
  319. else
  320. {
  321. /* Return AUDIO_OK when all operations are correctly done */
  322. return AUDIO_OK;
  323. }
  324. }
  325. /**
  326. * @brief Enables or disables the MUTE mode by software
  327. * @param Cmd: could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to
  328. * unmute the codec and restore previous volume level.
  329. * @retval AUDIO_OK if correct communication, else wrong communication
  330. */
  331. uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
  332. {
  333. /* Call the Codec Mute function */
  334. if(audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
  335. {
  336. return AUDIO_ERROR;
  337. }
  338. else
  339. {
  340. /* Return AUDIO_OK when all operations are correctly done */
  341. return AUDIO_OK;
  342. }
  343. }
  344. /**
  345. * @brief Switch dynamically (while audio file is played) the output target
  346. * (speaker or headphone).
  347. * @note This function modifies a global variable of the audio codec driver: OutputDev.
  348. * @param Output: specifies the audio output target: OUTPUT_DEVICE_SPEAKER,
  349. * OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO
  350. * @retval AUDIO_OK if correct communication, else wrong communication
  351. */
  352. uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
  353. {
  354. /* Call the Codec output Device function */
  355. if(audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
  356. {
  357. return AUDIO_ERROR;
  358. }
  359. else
  360. {
  361. /* Return AUDIO_OK when all operations are correctly done */
  362. return AUDIO_OK;
  363. }
  364. }
  365. /**
  366. * @brief Updates the audio frequency.
  367. * @param AudioFreq: Audio frequency used to play the audio stream.
  368. * @retval AUDIO_OK if correct communication, else wrong communication
  369. */
  370. void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
  371. {
  372. /* Configure PLL clock depending on AudioFreq */
  373. BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
  374. /* Update the I2S audio frequency configuration */
  375. I2Sx_Init(AudioFreq);
  376. }
  377. /**
  378. * @brief Clock Config.
  379. * @param hi2s: might be required to set audio peripheral predivider if any.
  380. * @param AudioFreq: Audio frequency used to play the audio stream.
  381. * @param Params
  382. * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency()
  383. * Being __weak it can be overwritten by the application
  384. * @retval None
  385. */
  386. __weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
  387. {
  388. RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct;
  389. uint8_t index = 0, freqindex = 0xFF;
  390. for(index = 0; index < 8; index++)
  391. {
  392. if(I2SFreq[index] == AudioFreq)
  393. {
  394. freqindex = index;
  395. }
  396. }
  397. HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct);
  398. if(freqindex != 0xFF)
  399. {
  400. /* I2S clock config
  401. PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) × (PLLI2SN/PLLM)
  402. I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  403. RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  404. RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = I2SPLLN[freqindex];
  405. RCC_ExCLKInitStruct.PLLI2S.PLLI2SR = I2SPLLR[freqindex];
  406. HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
  407. }
  408. else /* Default PLL I2S configuration */
  409. {
  410. /* I2S clock config
  411. PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) × (PLLI2SN/PLLM)
  412. I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  413. RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  414. RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = 258;
  415. RCC_ExCLKInitStruct.PLLI2S.PLLI2SR = 3;
  416. HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
  417. }
  418. }
  419. /**
  420. * @brief Initializes BSP_AUDIO_OUT MSP.
  421. * @param hi2s: I2S handle
  422. * @param Params
  423. */
  424. __weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
  425. {
  426. static DMA_HandleTypeDef hdma_i2sTx;
  427. GPIO_InitTypeDef GPIO_InitStruct;
  428. /* Enable I2S clock */
  429. AUDIO_I2Sx_CLK_ENABLE();
  430. /* Enable SCK, SD and WS GPIO clock */
  431. AUDIO_I2Sx_SCK_SD_WS_CLK_ENABLE();
  432. /* CODEC_I2S pins configuration: WS, SCK and SD pins */
  433. GPIO_InitStruct.Pin = AUDIO_I2Sx_WS_PIN | AUDIO_I2Sx_SCK_PIN | AUDIO_I2Sx_SD_PIN;
  434. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  435. GPIO_InitStruct.Pull = GPIO_NOPULL;
  436. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  437. GPIO_InitStruct.Alternate = AUDIO_I2Sx_SCK_SD_WS_AF;
  438. HAL_GPIO_Init(AUDIO_I2Sx_SCK_SD_WS_GPIO_PORT, &GPIO_InitStruct);
  439. /* Enable MCK GPIO clock */
  440. AUDIO_I2Sx_MCK_CLK_ENABLE();
  441. /* CODEC_I2S pins configuration: MCK pin */
  442. GPIO_InitStruct.Pin = AUDIO_I2Sx_MCK_PIN;
  443. HAL_GPIO_Init(AUDIO_I2Sx_MCK_GPIO_PORT, &GPIO_InitStruct);
  444. /* Enable the DMA clock */
  445. AUDIO_I2Sx_DMAx_CLK_ENABLE();
  446. if(hi2s->Instance == AUDIO_I2Sx)
  447. {
  448. /* Configure the hdma_i2sTx handle parameters */
  449. hdma_i2sTx.Init.Channel = AUDIO_I2Sx_DMAx_CHANNEL;
  450. hdma_i2sTx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  451. hdma_i2sTx.Init.PeriphInc = DMA_PINC_DISABLE;
  452. hdma_i2sTx.Init.MemInc = DMA_MINC_ENABLE;
  453. hdma_i2sTx.Init.PeriphDataAlignment = AUDIO_I2Sx_DMAx_PERIPH_DATA_SIZE;
  454. hdma_i2sTx.Init.MemDataAlignment = AUDIO_I2Sx_DMAx_MEM_DATA_SIZE;
  455. hdma_i2sTx.Init.Mode = DMA_NORMAL;
  456. hdma_i2sTx.Init.Priority = DMA_PRIORITY_HIGH;
  457. hdma_i2sTx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  458. hdma_i2sTx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  459. hdma_i2sTx.Init.MemBurst = DMA_MBURST_SINGLE;
  460. hdma_i2sTx.Init.PeriphBurst = DMA_PBURST_SINGLE;
  461. hdma_i2sTx.Instance = AUDIO_I2Sx_DMAx_STREAM;
  462. /* Associate the DMA handle */
  463. __HAL_LINKDMA(hi2s, hdmatx, hdma_i2sTx);
  464. /* Deinitialize the Stream for new transfer */
  465. HAL_DMA_DeInit(&hdma_i2sTx);
  466. /* Configure the DMA Stream */
  467. HAL_DMA_Init(&hdma_i2sTx);
  468. }
  469. /* I2S DMA IRQ Channel configuration */
  470. HAL_NVIC_SetPriority(AUDIO_I2Sx_DMAx_IRQ, AUDIO_IRQ_PREPRIO, 0);
  471. HAL_NVIC_EnableIRQ(AUDIO_I2Sx_DMAx_IRQ);
  472. }
  473. /**
  474. * @brief De-Initializes BSP_AUDIO_OUT MSP.
  475. * @param hi2s: I2S handle
  476. * @param Params
  477. */
  478. __weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
  479. {
  480. GPIO_InitTypeDef GPIO_InitStruct;
  481. /* Disable I2S clock */
  482. AUDIO_I2Sx_CLK_DISABLE();
  483. /* CODEC_I2S pins configuration: WS, SCK and SD pins */
  484. GPIO_InitStruct.Pin = AUDIO_I2Sx_WS_PIN | AUDIO_I2Sx_SCK_PIN | AUDIO_I2Sx_SD_PIN;
  485. HAL_GPIO_DeInit(AUDIO_I2Sx_SCK_SD_WS_GPIO_PORT, GPIO_InitStruct.Pin);
  486. /* CODEC_I2S pins configuration: MCK pin */
  487. GPIO_InitStruct.Pin = AUDIO_I2Sx_MCK_PIN;
  488. HAL_GPIO_DeInit(AUDIO_I2Sx_MCK_GPIO_PORT, GPIO_InitStruct.Pin);
  489. }
  490. /**
  491. * @brief Tx Transfer completed callbacks
  492. * @param hi2s: I2S handle
  493. */
  494. void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  495. {
  496. /* Manage the remaining file size and new address offset: This function
  497. should be coded by user (its prototype is already declared in stm322xg_eval_audio.h) */
  498. BSP_AUDIO_OUT_TransferComplete_CallBack();
  499. }
  500. /**
  501. * @brief Tx Transfer Half completed callbacks
  502. * @param hi2s: I2S handle
  503. */
  504. void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  505. {
  506. /* Manage the remaining file size and new address offset: This function
  507. should be coded by user (its prototype is already declared in stm322xg_eval_audio.h) */
  508. BSP_AUDIO_OUT_HalfTransfer_CallBack();
  509. }
  510. /**
  511. * @brief I2S error callbacks.
  512. * @param hi2s: I2S handle
  513. */
  514. void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  515. {
  516. BSP_AUDIO_OUT_Error_CallBack();
  517. }
  518. /**
  519. * @brief Manages the DMA full Transfer complete event.
  520. */
  521. __weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
  522. {
  523. }
  524. /**
  525. * @brief Manages the DMA Half Transfer complete event.
  526. */
  527. __weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
  528. {
  529. }
  530. /**
  531. * @brief Manages the DMA FIFO error event.
  532. */
  533. __weak void BSP_AUDIO_OUT_Error_CallBack(void)
  534. {
  535. }
  536. /*******************************************************************************
  537. Static Functions
  538. *******************************************************************************/
  539. /**
  540. * @brief Initializes the Audio Codec audio interface (I2S).
  541. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
  542. */
  543. static void I2Sx_Init(uint32_t AudioFreq)
  544. {
  545. /* Initialize the haudio_i2s Instance parameter */
  546. haudio_i2s.Instance = AUDIO_I2Sx;
  547. /* Disable I2S block */
  548. __HAL_I2S_DISABLE(&haudio_i2s);
  549. haudio_i2s.Init.Mode = I2S_MODE_MASTER_TX;
  550. haudio_i2s.Init.Standard = I2S_STANDARD;
  551. haudio_i2s.Init.DataFormat = I2S_DATAFORMAT_16B;
  552. haudio_i2s.Init.AudioFreq = AudioFreq;
  553. haudio_i2s.Init.CPOL = I2S_CPOL_LOW;
  554. haudio_i2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
  555. /* Init the I2S */
  556. HAL_I2S_Init(&haudio_i2s);
  557. }
  558. /**
  559. * @brief Deinitialize the Audio Codec audio interface (I2S).
  560. */
  561. static void I2Sx_DeInit(void)
  562. {
  563. /* Initialize the haudio_i2s Instance parameter */
  564. haudio_i2s.Instance = AUDIO_I2Sx;
  565. /* Disable I2S peripheral */
  566. __HAL_I2S_DISABLE(&haudio_i2s);
  567. HAL_I2S_DeInit(&haudio_i2s);
  568. }
  569. /**
  570. * @brief Resets the audio codec. It restores the default configuration of the
  571. * codec (this function shall be called before initializing the codec).
  572. * @note This function calls an external driver function: The IO Expander driver.
  573. */
  574. static void CODEC_Reset(void)
  575. {
  576. /* Configure the IO Expander (to use the Codec Reset pin mapped on the IOExpander) */
  577. BSP_IO_Init();
  578. BSP_IO_ConfigPin(AUDIO_RESET_PIN, IO_MODE_OUTPUT);
  579. /* Power Down the codec */
  580. BSP_IO_WritePin(AUDIO_RESET_PIN, RESET);
  581. /* Wait for a delay to insure registers erasing */
  582. HAL_Delay(CODEC_RESET_DELAY);
  583. /* Power on the codec */
  584. BSP_IO_WritePin(AUDIO_RESET_PIN, SET);
  585. /* Wait for a delay to insure registers erasing */
  586. HAL_Delay(CODEC_RESET_DELAY);
  587. }
  588. /**
  589. * @}
  590. */
  591. /**
  592. * @}
  593. */
  594. /**
  595. * @}
  596. */
  597. /**
  598. * @}
  599. */
  600. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/