stm32f4_discovery_audio.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4_discovery_audio.c
  4. * @author MCD Application Team
  5. * @brief This file provides the Audio driver for the STM32F4-Discovery
  6. * board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2017 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /*==============================================================================
  20. User NOTES
  21. 1. How To use this driver:
  22. --------------------------
  23. - This driver supports STM32F4xx devices on STM32F4-Discovery Kit:
  24. a) to play an audio file (all functions names start by BSP_AUDIO_OUT_xxx)
  25. b) to record an audio file through MP45DT02, ST MEMS (all functions names start by AUDIO_IN_xxx)
  26. a) PLAY A FILE:
  27. ==============
  28. + Call the function BSP_AUDIO_OUT_Init(
  29. OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER,
  30. OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_AUTO or
  31. OUTPUT_DEVICE_BOTH)
  32. Volume: initial volume to be set (0 is min (mute), 100 is max (100%)
  33. AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...)
  34. this parameter is relative to the audio file/stream type.
  35. )
  36. This function configures all the hardware required for the audio application (codec, I2C, I2S,
  37. GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK.
  38. If the returned value is different from 0 or the function is stuck then the communication with
  39. the codec (try to un-plug the power or reset device in this case).
  40. - OUTPUT_DEVICE_SPEAKER: only speaker will be set as output for the audio stream.
  41. - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream.
  42. - OUTPUT_DEVICE_AUTO: Selection of output device is made through external switch (implemented
  43. into the audio jack on the discovery board). When the Headphone is connected it is used
  44. as output. When the headphone is disconnected from the audio jack, the output is
  45. automatically switched to Speaker.
  46. - OUTPUT_DEVICE_BOTH: both Speaker and Headphone are used as outputs for the audio stream
  47. at the same time.
  48. + Call the function BSP_AUDIO_OUT_Play(
  49. pBuffer: pointer to the audio data file address
  50. Size: size of the buffer to be sent in Bytes
  51. )
  52. to start playing (for the first time) from the audio file/stream.
  53. + Call the function BSP_AUDIO_OUT_Pause() to pause playing
  54. + Call the function BSP_AUDIO_OUT_Resume() to resume playing.
  55. Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called
  56. for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case).
  57. Note. This function should be called only when the audio file is played or paused (not stopped).
  58. + For each mode, you may need to implement the relative callback functions into your code.
  59. The Callback functions are named BSP_AUDIO_OUT_XXXCallBack() and only their prototypes are declared in
  60. the stm32f4_discovery_audio.h file. (refer to the example for more details on the callbacks implementations)
  61. + To Stop playing, to modify the volume level, the frequency or to mute, use the functions
  62. BSP_AUDIO_OUT_Stop(), BSP_AUDIO_OUT_SetVolume(), AUDIO_OUT_SetFrequency() BSP_AUDIO_OUT_SetOutputMode and BSP_AUDIO_OUT_SetMute().
  63. + The driver API and the callback functions are at the end of the stm32f4_discovery_audio.h file.
  64. Driver architecture:
  65. --------------------
  66. + This driver provide the High Audio Layer: consists of the function API exported in the stm32f4_discovery_audio.h file
  67. (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...)
  68. + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/
  69. providing the audio file/stream. These functions are also included as local functions into
  70. the stm32f4_discovery_audio.c file (I2S3_Init()...)
  71. Known Limitations:
  72. -------------------
  73. 1- When using the Speaker, if the audio file quality is not high enough, the speaker output
  74. may produce high and uncomfortable noise level. To avoid this issue, to use speaker
  75. output properly, try to increase audio file sampling rate (typically higher than 48KHz).
  76. This operation will lead to larger file size.
  77. 2- Communication with the audio codec (through I2C) may be corrupted if it is interrupted by some
  78. user interrupt routines (in this case, interrupts could be disabled just before the start of
  79. communication then re-enabled when it is over). Note that this communication is only done at
  80. the configuration phase (BSP_AUDIO_OUT_Init() or BSP_AUDIO_OUT_Stop()) and when Volume control modification is
  81. performed (BSP_AUDIO_OUT_SetVolume() or BSP_AUDIO_OUT_SetMute()or BSP_AUDIO_OUT_SetOutputMode()).
  82. When the audio data is played, no communication is required with the audio codec.
  83. 3- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size,
  84. File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file.
  85. 4- Supports only Stereo audio streaming. To play mono audio streams, each data should be sent twice
  86. on the I2S or should be duplicated on the source buffer. Or convert the stream in stereo before playing.
  87. 5- Supports only 16-bits audio data size.
  88. b) RECORD A FILE:
  89. ================
  90. + Call the function BSP_AUDIO_IN_Init(
  91. AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...)
  92. )
  93. This function configures all the hardware required for the audio application (I2S,
  94. GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK.
  95. + Call the function BSP_AUDIO_IN_Record(
  96. pbuf Main buffer pointer for the recorded data storing
  97. size Current size of the recorded buffer
  98. )
  99. to start recording from the microphone.
  100. + User needs to implement user callbacks to retrieve data saved in the record buffer
  101. (AUDIO_IN_RxHalfCpltCallback/BSP_AUDIO_IN_ReceiveComplete_CallBack)
  102. + Call the function AUDIO_IN_STOP() to stop recording
  103. ==============================================================================*/
  104. /* Includes ------------------------------------------------------------------*/
  105. #include "stm32f4_discovery_audio.h"
  106. /** @addtogroup BSP
  107. * @{
  108. */
  109. /** @addtogroup STM32F4_DISCOVERY
  110. * @{
  111. */
  112. /** @defgroup STM32F4_DISCOVERY_AUDIO STM32F4 DISCOVERY AUDIO
  113. * @brief This file includes the low layer audio driver available on STM32F4-Discovery
  114. * discovery board.
  115. * @{
  116. */
  117. /** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Types STM32F4 DISCOVERY AUDIO Private Types
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Defines STM32F4 DISCOVERY AUDIO Private Defines
  124. * @{
  125. */
  126. /* These PLL parameters are valid when the f(VCO clock) = 1Mhz */
  127. const uint32_t I2SFreq[8] = {8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000};
  128. const uint32_t I2SPLLN[8] = {256, 429, 213, 429, 426, 271, 258, 344};
  129. const uint32_t I2SPLLR[8] = {5, 4, 4, 4, 4, 6, 3, 1};
  130. /**
  131. * @}
  132. */
  133. /** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Macros STM32F4 DISCOVERY AUDIO Private Macros
  134. * @{
  135. */
  136. /**
  137. * @}
  138. */
  139. /** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Variables STM32F4 DISCOVERY AUDIO Private Variables
  140. * @{
  141. */
  142. /*##### PLAY #####*/
  143. static AUDIO_DrvTypeDef *pAudioDrv;
  144. I2S_HandleTypeDef hAudioOutI2s;
  145. /*### RECORDER ###*/
  146. I2S_HandleTypeDef hAudioInI2s;
  147. /* PDM filters params */
  148. PDM_Filter_Handler_t PDM_FilterHandler[2];
  149. PDM_Filter_Config_t PDM_FilterConfig[2];
  150. __IO uint16_t AudioInVolume = DEFAULT_AUDIO_IN_VOLUME;
  151. /**
  152. * @}
  153. */
  154. /** @defgroup STM32F4_DISCOVERY_AUDIO_Private_Function_Prototypes STM32F4 DISCOVERY AUDIO Private Function Prototypes
  155. * @{
  156. */
  157. static uint8_t I2S3_Init(uint32_t AudioFreq);
  158. static uint8_t I2S2_Init(uint32_t AudioFreq);
  159. static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbrIn, uint32_t ChnlNbrOut);
  160. /**
  161. * @}
  162. */
  163. /** @defgroup STM32F4_DISCOVERY_AUDIO_OUT_Private_Functions STM32F4 DISCOVERY AUDIO OUT Private Functions
  164. * @{
  165. */
  166. /**
  167. * @brief Configures the audio peripherals.
  168. * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
  169. * OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO .
  170. * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
  171. * @param AudioFreq: Audio frequency used to play the audio stream.
  172. * @retval AUDIO_OK if correct communication, else wrong communication
  173. */
  174. uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
  175. {
  176. uint8_t ret = AUDIO_OK;
  177. /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
  178. BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL);
  179. /* I2S data transfer preparation:
  180. Prepare the Media to be used for the audio transfer from memory to I2S peripheral */
  181. hAudioOutI2s.Instance = I2S3;
  182. if(HAL_I2S_GetState(&hAudioOutI2s) == HAL_I2S_STATE_RESET)
  183. {
  184. /* Init the I2S MSP: this __weak function can be redefined by the application*/
  185. BSP_AUDIO_OUT_MspInit(&hAudioOutI2s, NULL);
  186. }
  187. /* I2S data transfer preparation:
  188. Prepare the Media to be used for the audio transfer from memory to I2S peripheral */
  189. /* Configure the I2S peripheral */
  190. if(I2S3_Init(AudioFreq) != AUDIO_OK)
  191. {
  192. ret = AUDIO_ERROR;
  193. }
  194. if(ret == AUDIO_OK)
  195. {
  196. /* Retieve audio codec identifier */
  197. if(((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID)
  198. {
  199. /* Initialize the audio driver structure */
  200. pAudioDrv = &cs43l22_drv;
  201. }
  202. else
  203. {
  204. ret = AUDIO_ERROR;
  205. }
  206. }
  207. if(ret == AUDIO_OK)
  208. {
  209. pAudioDrv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq);
  210. }
  211. return ret;
  212. }
  213. /**
  214. * @brief Starts playing audio stream from a data buffer for a determined size.
  215. * @param pBuffer: Pointer to the buffer
  216. * @param Size: Number of audio data BYTES.
  217. * @retval AUDIO_OK if correct communication, else wrong communication
  218. */
  219. uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size)
  220. {
  221. /* Call the audio Codec Play function */
  222. if(pAudioDrv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
  223. {
  224. return AUDIO_ERROR;
  225. }
  226. else
  227. {
  228. /* Update the Media layer and enable it for play */
  229. HAL_I2S_Transmit_DMA(&hAudioOutI2s, pBuffer, DMA_MAX(Size/AUDIODATA_SIZE));
  230. /* Return AUDIO_OK when all operations are correctly done */
  231. return AUDIO_OK;
  232. }
  233. }
  234. /**
  235. * @brief Sends n-Bytes on the I2S interface.
  236. * @param pData: Pointer to data address
  237. * @param Size: Number of data to be written
  238. */
  239. void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
  240. {
  241. HAL_I2S_Transmit_DMA(&hAudioOutI2s, pData, Size);
  242. }
  243. /**
  244. * @brief Pauses the audio file stream. In case of using DMA, the DMA Pause
  245. * feature is used.
  246. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only the
  247. * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
  248. * function for resume could lead to unexpected behavior).
  249. * @retval AUDIO_OK if correct communication, else wrong communication
  250. */
  251. uint8_t BSP_AUDIO_OUT_Pause(void)
  252. {
  253. /* Call the Audio Codec Pause/Resume function */
  254. if(pAudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0)
  255. {
  256. return AUDIO_ERROR;
  257. }
  258. else
  259. {
  260. /* Call the Media layer pause function */
  261. HAL_I2S_DMAPause(&hAudioOutI2s);
  262. /* Return AUDIO_OK when all operations are correctly done */
  263. return AUDIO_OK;
  264. }
  265. }
  266. /**
  267. * @brief Resumes the audio file streaming.
  268. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only
  269. * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play()
  270. * function for resume could lead to unexpected behavior).
  271. * @retval AUDIO_OK if correct communication, else wrong communication
  272. */
  273. uint8_t BSP_AUDIO_OUT_Resume(void)
  274. {
  275. /* Call the Audio Codec Pause/Resume function */
  276. if(pAudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0)
  277. {
  278. return AUDIO_ERROR;
  279. }
  280. else
  281. {
  282. /* Call the Media layer resume function */
  283. HAL_I2S_DMAResume(&hAudioOutI2s);
  284. /* Return AUDIO_OK when all operations are correctly done */
  285. return AUDIO_OK;
  286. }
  287. }
  288. /**
  289. * @brief Stops audio playing and Power down the Audio Codec.
  290. * @param Option: could be one of the following parameters
  291. * - CODEC_PDWN_HW: completely shut down the codec (physically).
  292. * Then need to reconfigure the Codec after power on.
  293. * @retval AUDIO_OK if correct communication, else wrong communication
  294. */
  295. uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
  296. {
  297. /* Call DMA Stop to disable DMA stream before stopping codec */
  298. HAL_I2S_DMAStop(&hAudioOutI2s);
  299. /* Call Audio Codec Stop function */
  300. if(pAudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
  301. {
  302. return AUDIO_ERROR;
  303. }
  304. else
  305. {
  306. if(Option == CODEC_PDWN_HW)
  307. {
  308. /* Wait at least 1ms */
  309. HAL_Delay(1);
  310. /* Reset the pin */
  311. HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
  312. }
  313. /* Return AUDIO_OK when all operations are correctly done */
  314. return AUDIO_OK;
  315. }
  316. }
  317. /**
  318. * @brief Controls the current audio volume level.
  319. * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
  320. * Mute and 100 for Max volume level).
  321. * @retval AUDIO_OK if correct communication, else wrong communication
  322. */
  323. uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
  324. {
  325. /* Call the codec volume control function with converted volume value */
  326. if(pAudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
  327. {
  328. return AUDIO_ERROR;
  329. }
  330. else
  331. {
  332. /* Return AUDIO_OK when all operations are correctly done */
  333. return AUDIO_OK;
  334. }
  335. }
  336. /**
  337. * @brief Enables or disables the MUTE mode by software
  338. * @param Cmd: could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to
  339. * unmute the codec and restore previous volume level.
  340. * @retval AUDIO_OK if correct communication, else wrong communication
  341. */
  342. uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
  343. {
  344. /* Call the Codec Mute function */
  345. if(pAudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
  346. {
  347. return AUDIO_ERROR;
  348. }
  349. else
  350. {
  351. /* Return AUDIO_OK when all operations are correctly done */
  352. return AUDIO_OK;
  353. }
  354. }
  355. /**
  356. * @brief Switch dynamically (while audio file is played) the output target
  357. * (speaker or headphone).
  358. * @note This function modifies a global variable of the audio codec driver: OutputDev.
  359. * @param Output: specifies the audio output target: OUTPUT_DEVICE_SPEAKER,
  360. * OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO
  361. * @retval AUDIO_OK if correct communication, else wrong communication
  362. */
  363. uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
  364. {
  365. /* Call the Codec output Device function */
  366. if(pAudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
  367. {
  368. return AUDIO_ERROR;
  369. }
  370. else
  371. {
  372. /* Return AUDIO_OK when all operations are correctly done */
  373. return AUDIO_OK;
  374. }
  375. }
  376. /**
  377. * @brief Update the audio frequency.
  378. * @param AudioFreq: Audio frequency used to play the audio stream.
  379. * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the
  380. * audio frequency.
  381. */
  382. void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
  383. {
  384. /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
  385. BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL);
  386. /* Update the I2S audio frequency configuration */
  387. I2S3_Init(AudioFreq);
  388. }
  389. /**
  390. * @brief Tx Transfer completed callbacks.
  391. * @param hi2s: I2S handle
  392. */
  393. void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
  394. {
  395. if(hi2s->Instance == I2S3)
  396. {
  397. /* Call the user function which will manage directly transfer complete */
  398. BSP_AUDIO_OUT_TransferComplete_CallBack();
  399. }
  400. }
  401. /**
  402. * @brief Tx Half Transfer completed callbacks.
  403. * @param hi2s: I2S handle
  404. */
  405. void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  406. {
  407. if(hi2s->Instance == I2S3)
  408. {
  409. /* Manage the remaining file size and new address offset: This function should
  410. be coded by user (its prototype is already declared in stm32f4_discovery_audio.h) */
  411. BSP_AUDIO_OUT_HalfTransfer_CallBack();
  412. }
  413. }
  414. /**
  415. * @brief Clock Config.
  416. * @param hi2s: might be required to set audio peripheral predivider if any.
  417. * @param AudioFreq: Audio frequency used to play the audio stream.
  418. * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency()
  419. * Being __weak it can be overwritten by the application
  420. * @param Params : pointer on additional configuration parameters, can be NULL.
  421. */
  422. __weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
  423. {
  424. RCC_PeriphCLKInitTypeDef rccclkinit;
  425. uint8_t index = 0, freqindex = 0xFF;
  426. for(index = 0; index < 8; index++)
  427. {
  428. if(I2SFreq[index] == AudioFreq)
  429. {
  430. freqindex = index;
  431. }
  432. }
  433. /* Enable PLLI2S clock */
  434. HAL_RCCEx_GetPeriphCLKConfig(&rccclkinit);
  435. /* PLLI2S_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
  436. if ((freqindex & 0x7) == 0)
  437. {
  438. /* I2S clock config
  439. PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) × (PLLI2SN/PLLM)
  440. I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  441. rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  442. rccclkinit.PLLI2S.PLLI2SN = I2SPLLN[freqindex];
  443. rccclkinit.PLLI2S.PLLI2SR = I2SPLLR[freqindex];
  444. HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
  445. }
  446. else
  447. {
  448. /* I2S clock config
  449. PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) × (PLLI2SN/PLLM)
  450. I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  451. rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  452. rccclkinit.PLLI2S.PLLI2SN = 258;
  453. rccclkinit.PLLI2S.PLLI2SR = 3;
  454. HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
  455. }
  456. }
  457. /**
  458. * @brief AUDIO OUT I2S MSP Init.
  459. * @param hi2s: might be required to set audio peripheral predivider if any.
  460. * @param Params : pointer on additional configuration parameters, can be NULL.
  461. */
  462. __weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
  463. {
  464. static DMA_HandleTypeDef hdma_i2sTx;
  465. GPIO_InitTypeDef GPIO_InitStruct;
  466. /* Enable I2S3 clock */
  467. I2S3_CLK_ENABLE();
  468. /*** Configure the GPIOs ***/
  469. /* Enable I2S GPIO clocks */
  470. I2S3_SCK_SD_CLK_ENABLE();
  471. I2S3_WS_CLK_ENABLE();
  472. /* I2S3 pins configuration: WS, SCK and SD pins ----------------------------*/
  473. GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN;
  474. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  475. GPIO_InitStruct.Pull = GPIO_NOPULL;
  476. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  477. GPIO_InitStruct.Alternate = I2S3_SCK_SD_WS_AF;
  478. HAL_GPIO_Init(I2S3_SCK_SD_GPIO_PORT, &GPIO_InitStruct);
  479. GPIO_InitStruct.Pin = I2S3_WS_PIN ;
  480. HAL_GPIO_Init(I2S3_WS_GPIO_PORT, &GPIO_InitStruct);
  481. /* I2S3 pins configuration: MCK pin */
  482. I2S3_MCK_CLK_ENABLE();
  483. GPIO_InitStruct.Pin = I2S3_MCK_PIN;
  484. HAL_GPIO_Init(I2S3_MCK_GPIO_PORT, &GPIO_InitStruct);
  485. /* Enable the I2S DMA clock */
  486. I2S3_DMAx_CLK_ENABLE();
  487. if(hi2s->Instance == I2S3)
  488. {
  489. /* Configure the hdma_i2sTx handle parameters */
  490. hdma_i2sTx.Init.Channel = I2S3_DMAx_CHANNEL;
  491. hdma_i2sTx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  492. hdma_i2sTx.Init.PeriphInc = DMA_PINC_DISABLE;
  493. hdma_i2sTx.Init.MemInc = DMA_MINC_ENABLE;
  494. hdma_i2sTx.Init.PeriphDataAlignment = I2S3_DMAx_PERIPH_DATA_SIZE;
  495. hdma_i2sTx.Init.MemDataAlignment = I2S3_DMAx_MEM_DATA_SIZE;
  496. hdma_i2sTx.Init.Mode = DMA_NORMAL;
  497. hdma_i2sTx.Init.Priority = DMA_PRIORITY_HIGH;
  498. hdma_i2sTx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  499. hdma_i2sTx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  500. hdma_i2sTx.Init.MemBurst = DMA_MBURST_SINGLE;
  501. hdma_i2sTx.Init.PeriphBurst = DMA_PBURST_SINGLE;
  502. hdma_i2sTx.Instance = I2S3_DMAx_STREAM;
  503. /* Associate the DMA handle */
  504. __HAL_LINKDMA(hi2s, hdmatx, hdma_i2sTx);
  505. /* Deinitialize the Stream for new transfer */
  506. HAL_DMA_DeInit(&hdma_i2sTx);
  507. /* Configure the DMA Stream */
  508. HAL_DMA_Init(&hdma_i2sTx);
  509. }
  510. /* I2S DMA IRQ Channel configuration */
  511. HAL_NVIC_SetPriority(I2S3_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0);
  512. HAL_NVIC_EnableIRQ(I2S3_DMAx_IRQ);
  513. }
  514. /**
  515. * @brief De-Initializes BSP_AUDIO_OUT MSP.
  516. * @param hi2s: might be required to set audio peripheral predivider if any.
  517. * @param Params : pointer on additional configuration parameters, can be NULL.
  518. */
  519. __weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
  520. {
  521. GPIO_InitTypeDef GPIO_InitStruct;
  522. /* I2S DMA IRQ Channel deactivation */
  523. HAL_NVIC_DisableIRQ(I2S3_DMAx_IRQ);
  524. if(hi2s->Instance == I2S3)
  525. {
  526. /* Deinitialize the Stream for new transfer */
  527. HAL_DMA_DeInit(hi2s->hdmatx);
  528. }
  529. /* Disable I2S block */
  530. __HAL_I2S_DISABLE(hi2s);
  531. /* CODEC_I2S pins configuration: SCK and SD pins */
  532. GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN;
  533. HAL_GPIO_DeInit(I2S3_SCK_SD_GPIO_PORT, GPIO_InitStruct.Pin);
  534. /* CODEC_I2S pins configuration: WS pin */
  535. GPIO_InitStruct.Pin = I2S3_WS_PIN;
  536. HAL_GPIO_DeInit(I2S3_WS_GPIO_PORT, GPIO_InitStruct.Pin);
  537. /* CODEC_I2S pins configuration: MCK pin */
  538. GPIO_InitStruct.Pin = I2S3_MCK_PIN;
  539. HAL_GPIO_DeInit(I2S3_MCK_GPIO_PORT, GPIO_InitStruct.Pin);
  540. /* Disable I2S clock */
  541. I2S3_CLK_DISABLE();
  542. /* GPIO pins clock and DMA clock can be shut down in the applic
  543. by surcgarging this __weak function */
  544. }
  545. /**
  546. * @brief Manages the DMA full Transfer complete event.
  547. */
  548. __weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
  549. {
  550. }
  551. /**
  552. * @brief Manages the DMA Half Transfer complete event.
  553. */
  554. __weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
  555. {
  556. }
  557. /**
  558. * @brief Manages the DMA FIFO error event.
  559. */
  560. __weak void BSP_AUDIO_OUT_Error_CallBack(void)
  561. {
  562. }
  563. /*******************************************************************************
  564. Static Functions
  565. *******************************************************************************/
  566. /**
  567. * @brief Initializes the Audio Codec audio interface (I2S).
  568. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
  569. */
  570. static uint8_t I2S3_Init(uint32_t AudioFreq)
  571. {
  572. /* Initialize the hAudioOutI2s Instance parameter */
  573. hAudioOutI2s.Instance = I2S3;
  574. /* Disable I2S block */
  575. __HAL_I2S_DISABLE(&hAudioOutI2s);
  576. /* I2S3 peripheral configuration */
  577. hAudioOutI2s.Init.AudioFreq = AudioFreq;
  578. hAudioOutI2s.Init.ClockSource = I2S_CLOCK_PLL;
  579. hAudioOutI2s.Init.CPOL = I2S_CPOL_LOW;
  580. hAudioOutI2s.Init.DataFormat = I2S_DATAFORMAT_16B;
  581. hAudioOutI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
  582. hAudioOutI2s.Init.Mode = I2S_MODE_MASTER_TX;
  583. hAudioOutI2s.Init.Standard = I2S_STANDARD;
  584. /* Initialize the I2S peripheral with the structure above */
  585. if(HAL_I2S_Init(&hAudioOutI2s) != HAL_OK)
  586. {
  587. return AUDIO_ERROR;
  588. }
  589. else
  590. {
  591. return AUDIO_OK;
  592. }
  593. }
  594. /**
  595. * @}
  596. */
  597. /** @defgroup STM32F4_DISCOVERY_AUDIO_IN_Private_Functions STM32F4 DISCOVERY AUDIO IN Private Functions
  598. * @{
  599. */
  600. /**
  601. * @brief Initializes wave recording.
  602. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
  603. * @param BitRes: Audio frequency to be configured for the I2S peripheral.
  604. * @param ChnlNbr: Audio frequency to be configured for the I2S peripheral.
  605. * @retval AUDIO_OK if correct communication, else wrong communication
  606. */
  607. uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
  608. {
  609. /* Configure PLL clock */
  610. BSP_AUDIO_IN_ClockConfig(&hAudioInI2s, AudioFreq, NULL);
  611. /* Configure the PDM library */
  612. /* On STM32F4-Discovery a single microphone is mounted, samples are duplicated
  613. to make stereo audio streams */
  614. PDMDecoder_Init(AudioFreq, ChnlNbr, 2);
  615. /* Configure the I2S peripheral */
  616. hAudioInI2s.Instance = I2S2;
  617. if(HAL_I2S_GetState(&hAudioInI2s) == HAL_I2S_STATE_RESET)
  618. {
  619. /* Initialize the I2S Msp: this __weak function can be rewritten by the application */
  620. BSP_AUDIO_IN_MspInit(&hAudioInI2s, NULL);
  621. }
  622. /* Configure the I2S2 */
  623. I2S2_Init(AudioFreq);
  624. /* Return AUDIO_OK when all operations are correctly done */
  625. return AUDIO_OK;
  626. }
  627. /**
  628. * @brief Starts audio recording.
  629. * @param pbuf: Main buffer pointer for the recorded data storing
  630. * @param size: Current size of the recorded buffer
  631. * @retval AUDIO_OK if correct communication, else wrong communication
  632. */
  633. uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size)
  634. {
  635. uint32_t ret = AUDIO_ERROR;
  636. /* Start the process receive DMA */
  637. HAL_I2S_Receive_DMA(&hAudioInI2s, pbuf, size);
  638. /* Return AUDIO_OK when all operations are correctly done */
  639. ret = AUDIO_OK;
  640. return ret;
  641. }
  642. /**
  643. * @brief Stops audio recording.
  644. * @retval AUDIO_OK if correct communication, else wrong communication
  645. */
  646. uint8_t BSP_AUDIO_IN_Stop(void)
  647. {
  648. uint32_t ret = AUDIO_ERROR;
  649. /* Call the Media layer pause function */
  650. HAL_I2S_DMAStop(&hAudioInI2s);
  651. /* Return AUDIO_OK when all operations are correctly done */
  652. ret = AUDIO_OK;
  653. return ret;
  654. }
  655. /**
  656. * @brief Pauses the audio file stream.
  657. * @retval AUDIO_OK if correct communication, else wrong communication
  658. */
  659. uint8_t BSP_AUDIO_IN_Pause(void)
  660. {
  661. /* Call the Media layer pause function */
  662. HAL_I2S_DMAPause(&hAudioInI2s);
  663. /* Return AUDIO_OK when all operations are correctly done */
  664. return AUDIO_OK;
  665. }
  666. /**
  667. * @brief Resumes the audio file stream.
  668. * @retval AUDIO_OK if correct communication, else wrong communication
  669. */
  670. uint8_t BSP_AUDIO_IN_Resume(void)
  671. {
  672. /* Call the Media layer pause/resume function */
  673. HAL_I2S_DMAResume(&hAudioInI2s);
  674. /* Return AUDIO_OK when all operations are correctly done */
  675. return AUDIO_OK;
  676. }
  677. /**
  678. * @brief Controls the audio in volume level.
  679. * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
  680. * Mute and 100 for Max volume level).
  681. * @retval AUDIO_OK if correct communication, else wrong communication
  682. */
  683. uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume)
  684. {
  685. /* Set the Global variable AudioInVolume */
  686. AudioInVolume = Volume;
  687. /* Return AUDIO_OK when all operations are correctly done */
  688. return AUDIO_OK;
  689. }
  690. /**
  691. * @brief Converts audio format from PDM to PCM.
  692. * @param PDMBuf: Pointer to data PDM buffer
  693. * @param PCMBuf: Pointer to data PCM buffer
  694. * @retval AUDIO_OK if correct communication, else wrong communication
  695. */
  696. uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t *PDMBuf, uint16_t *PCMBuf)
  697. {
  698. uint16_t AppPDM[INTERNAL_BUFF_SIZE/2];
  699. uint32_t index = 0;
  700. /* PDM Demux */
  701. for(index = 0; index<INTERNAL_BUFF_SIZE/2; index++)
  702. {
  703. AppPDM[index] = HTONS(PDMBuf[index]);
  704. }
  705. for(index = 0; index < DEFAULT_AUDIO_IN_CHANNEL_NBR; index++)
  706. {
  707. /* PDM to PCM filter */
  708. PDM_Filter((uint8_t*)&AppPDM[index], (uint16_t*)&(PCMBuf[index]), &PDM_FilterHandler[index]);
  709. }
  710. /* Duplicate samples since a single microphone in mounted on STM32F4-Discovery */
  711. for(index = 0; index < PCM_OUT_SIZE; index++)
  712. {
  713. PCMBuf[(index<<1)+1] = PCMBuf[index<<1];
  714. }
  715. /* Return AUDIO_OK when all operations are correctly done */
  716. return AUDIO_OK;
  717. }
  718. /**
  719. * @brief Rx Transfer completed callbacks
  720. * @param hi2s: I2S handle
  721. */
  722. void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
  723. {
  724. /* Call the record update function to get the next buffer to fill and its size (size is ignored) */
  725. BSP_AUDIO_IN_TransferComplete_CallBack();
  726. }
  727. /**
  728. * @brief Rx Half Transfer completed callbacks.
  729. * @param hi2s: I2S handle
  730. */
  731. void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
  732. {
  733. /* Manage the remaining file size and new address offset: This function
  734. should be coded by user (its prototype is already declared in stm32f4_discovery_audio.h) */
  735. BSP_AUDIO_IN_HalfTransfer_CallBack();
  736. }
  737. /**
  738. * @brief Audio In Clock Config.
  739. * @param hi2s: I2S handle
  740. * @param AudioFreq: Audio frequency used to record the audio stream.
  741. * @param Params : pointer on additional configuration parameters, can be NULL.
  742. * @note This API is called by BSP_AUDIO_IN_Init()
  743. * Being __weak it can be overwritten by the application
  744. */
  745. __weak void BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
  746. {
  747. RCC_PeriphCLKInitTypeDef rccclkinit;
  748. /*Enable PLLI2S clock*/
  749. HAL_RCCEx_GetPeriphCLKConfig(&rccclkinit);
  750. /* PLLI2S_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
  751. if ((AudioFreq & 0x7) == 0)
  752. {
  753. /* Audio frequency multiple of 8 (8/16/32/48/96/192)*/
  754. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN = 192 Mhz */
  755. /* I2SCLK = PLLI2S_VCO Output/PLLI2SR = 192/6 = 32 Mhz */
  756. rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  757. rccclkinit.PLLI2S.PLLI2SN = 192;
  758. rccclkinit.PLLI2S.PLLI2SR = 6;
  759. HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
  760. }
  761. else
  762. {
  763. /* Other Frequency (11.025/22.500/44.100) */
  764. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN = 290 Mhz */
  765. /* I2SCLK = PLLI2S_VCO Output/PLLI2SR = 290/2 = 145 Mhz */
  766. rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  767. rccclkinit.PLLI2S.PLLI2SN = 290;
  768. rccclkinit.PLLI2S.PLLI2SR = 2;
  769. HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
  770. }
  771. }
  772. /**
  773. * @brief BSP AUDIO IN MSP Init.
  774. * @param hi2s: I2S handle
  775. * @param Params : pointer on additional configuration parameters, can be NULL.
  776. */
  777. __weak void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
  778. {
  779. static DMA_HandleTypeDef hdma_i2sRx;
  780. GPIO_InitTypeDef GPIO_InitStruct;
  781. /* Enable the I2S2 peripheral clock */
  782. I2S2_CLK_ENABLE();
  783. /* Enable I2S GPIO clocks */
  784. I2S2_SCK_GPIO_CLK_ENABLE();
  785. I2S2_MOSI_GPIO_CLK_ENABLE();
  786. /* I2S2 pins configuration: SCK and MOSI pins ------------------------------*/
  787. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  788. GPIO_InitStruct.Pull = GPIO_NOPULL;
  789. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  790. GPIO_InitStruct.Pin = I2S2_SCK_PIN;
  791. GPIO_InitStruct.Alternate = I2S2_SCK_AF;
  792. HAL_GPIO_Init(I2S2_SCK_GPIO_PORT, &GPIO_InitStruct);
  793. GPIO_InitStruct.Pin = I2S2_MOSI_PIN ;
  794. GPIO_InitStruct.Alternate = I2S2_MOSI_AF;
  795. HAL_GPIO_Init(I2S2_MOSI_GPIO_PORT, &GPIO_InitStruct);
  796. /* Enable the DMA clock */
  797. I2S2_DMAx_CLK_ENABLE();
  798. if(hi2s->Instance == I2S2)
  799. {
  800. /* Configure the hdma_i2sRx handle parameters */
  801. hdma_i2sRx.Init.Channel = I2S2_DMAx_CHANNEL;
  802. hdma_i2sRx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  803. hdma_i2sRx.Init.PeriphInc = DMA_PINC_DISABLE;
  804. hdma_i2sRx.Init.MemInc = DMA_MINC_ENABLE;
  805. hdma_i2sRx.Init.PeriphDataAlignment = I2S2_DMAx_PERIPH_DATA_SIZE;
  806. hdma_i2sRx.Init.MemDataAlignment = I2S2_DMAx_MEM_DATA_SIZE;
  807. hdma_i2sRx.Init.Mode = DMA_CIRCULAR;
  808. hdma_i2sRx.Init.Priority = DMA_PRIORITY_HIGH;
  809. hdma_i2sRx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  810. hdma_i2sRx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  811. hdma_i2sRx.Init.MemBurst = DMA_MBURST_SINGLE;
  812. hdma_i2sRx.Init.PeriphBurst = DMA_MBURST_SINGLE;
  813. hdma_i2sRx.Instance = I2S2_DMAx_STREAM;
  814. /* Associate the DMA handle */
  815. __HAL_LINKDMA(hi2s, hdmarx, hdma_i2sRx);
  816. /* Deinitialize the Stream for new transfer */
  817. HAL_DMA_DeInit(&hdma_i2sRx);
  818. /* Configure the DMA Stream */
  819. HAL_DMA_Init(&hdma_i2sRx);
  820. }
  821. /* I2S DMA IRQ Channel configuration */
  822. HAL_NVIC_SetPriority(I2S2_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0);
  823. HAL_NVIC_EnableIRQ(I2S2_DMAx_IRQ);
  824. }
  825. /**
  826. * @brief DeInitializes BSP_AUDIO_IN MSP.
  827. * @param hi2s: I2S handle
  828. * @param Params : pointer on additional configuration parameters, can be NULL.
  829. */
  830. __weak void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
  831. {
  832. GPIO_InitTypeDef gpio_init_structure;
  833. /* I2S DMA IRQ Channel deactivation */
  834. HAL_NVIC_DisableIRQ(I2S2_DMAx_IRQ);
  835. if(hi2s->Instance == I2S2)
  836. {
  837. /* Deinitialize the Stream for new transfer */
  838. HAL_DMA_DeInit(hi2s->hdmarx);
  839. }
  840. /* Disable I2S block */
  841. __HAL_I2S_DISABLE(hi2s);
  842. /* Disable pins: SCK and SD pins */
  843. gpio_init_structure.Pin = I2S2_SCK_PIN;
  844. HAL_GPIO_DeInit(I2S2_SCK_GPIO_PORT, gpio_init_structure.Pin);
  845. gpio_init_structure.Pin = I2S2_MOSI_PIN;
  846. HAL_GPIO_DeInit(I2S2_MOSI_GPIO_PORT, gpio_init_structure.Pin);
  847. /* Disable I2S clock */
  848. I2S2_CLK_DISABLE();
  849. /* GPIO pins clock and DMA clock can be shut down in the applic
  850. by surcgarging this __weak function */
  851. }
  852. /**
  853. * @brief User callback when record buffer is filled.
  854. */
  855. __weak void BSP_AUDIO_IN_TransferComplete_CallBack(void)
  856. {
  857. /* This function should be implemented by the user application.
  858. It is called into this driver when the current buffer is filled
  859. to prepare the next buffer pointer and its size. */
  860. }
  861. /**
  862. * @brief Manages the DMA Half Transfer complete event.
  863. */
  864. __weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
  865. {
  866. /* This function should be implemented by the user application.
  867. It is called into this driver when the current buffer is filled
  868. to prepare the next buffer pointer and its size. */
  869. }
  870. /**
  871. * @brief Audio IN Error callback function.
  872. */
  873. __weak void BSP_AUDIO_IN_Error_Callback(void)
  874. {
  875. /* This function is called when an Interrupt due to transfer error on or peripheral
  876. error occurs. */
  877. }
  878. /*******************************************************************************
  879. Static Functions
  880. *******************************************************************************/
  881. /**
  882. * @brief Initializes the PDM library.
  883. * @param AudioFreq: Audio sampling frequency
  884. * @param ChnlNbrIn: Number of input audio channels in the PDM buffer
  885. * @param ChnlNbrOut: Number of desired output audio channels in the resulting PCM buffer
  886. * Number of audio channels (1: mono; 2: stereo)
  887. */
  888. static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbrIn, uint32_t ChnlNbrOut)
  889. {
  890. uint32_t index = 0;
  891. /* Enable CRC peripheral to unlock the PDM library */
  892. __HAL_RCC_CRC_CLK_ENABLE();
  893. for(index = 0; index < ChnlNbrIn; index++)
  894. {
  895. /* Init PDM filters */
  896. PDM_FilterHandler[index].bit_order = PDM_FILTER_BIT_ORDER_LSB;
  897. PDM_FilterHandler[index].endianness = PDM_FILTER_ENDIANNESS_LE;
  898. PDM_FilterHandler[index].high_pass_tap = 2122358088;
  899. PDM_FilterHandler[index].out_ptr_channels = ChnlNbrOut;
  900. PDM_FilterHandler[index].in_ptr_channels = ChnlNbrIn;
  901. PDM_Filter_Init((PDM_Filter_Handler_t *)(&PDM_FilterHandler[index]));
  902. /* PDM lib config phase */
  903. PDM_FilterConfig[index].output_samples_number = AudioFreq/1000;
  904. PDM_FilterConfig[index].mic_gain = 24;
  905. PDM_FilterConfig[index].decimation_factor = PDM_FILTER_DEC_FACTOR_64;
  906. PDM_Filter_setConfig((PDM_Filter_Handler_t *)&PDM_FilterHandler[index], &PDM_FilterConfig[index]);
  907. }
  908. }
  909. /**
  910. * @brief Initializes the Audio Codec audio interface (I2S)
  911. * @note This function assumes that the I2S input clock (through PLL_R in
  912. * Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y)
  913. * is already configured and ready to be used.
  914. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral.
  915. */
  916. static uint8_t I2S2_Init(uint32_t AudioFreq)
  917. {
  918. /* Initialize the hAudioInI2s Instance parameter */
  919. hAudioInI2s.Instance = I2S2;
  920. /* Disable I2S block */
  921. __HAL_I2S_DISABLE(&hAudioInI2s);
  922. /* I2S2 peripheral configuration */
  923. hAudioInI2s.Init.AudioFreq = 2 * AudioFreq;
  924. hAudioInI2s.Init.ClockSource = I2S_CLOCK_PLL;
  925. hAudioInI2s.Init.CPOL = I2S_CPOL_HIGH;
  926. hAudioInI2s.Init.DataFormat = I2S_DATAFORMAT_16B;
  927. hAudioInI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  928. hAudioInI2s.Init.Mode = I2S_MODE_MASTER_RX;
  929. hAudioInI2s.Init.Standard = I2S_STANDARD_LSB;
  930. /* Initialize the I2S peripheral with the structure above */
  931. if(HAL_I2S_Init(&hAudioInI2s) != HAL_OK)
  932. {
  933. return AUDIO_ERROR;
  934. }
  935. else
  936. {
  937. return AUDIO_OK;
  938. }
  939. }
  940. /**
  941. * @}
  942. */
  943. /** @defgroup STM32F4_DISCOVERY_AUDIO_IN_OUT_Private_Functions STM32F4 DISCOVERY AUDIO IN OUT Private Functions
  944. * @{
  945. */
  946. /**
  947. * @brief I2S error callbacks.
  948. * @param hi2s: I2S handle
  949. */
  950. void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
  951. {
  952. /* Manage the error generated on DMA FIFO: This function
  953. should be coded by user (its prototype is already declared in stm32f4_discovery_audio.h) */
  954. if(hi2s->Instance == I2S3)
  955. {
  956. BSP_AUDIO_OUT_Error_CallBack();
  957. }
  958. if(hi2s->Instance == I2S2)
  959. {
  960. BSP_AUDIO_IN_Error_Callback();
  961. }
  962. }
  963. /**
  964. * @}
  965. */
  966. /**
  967. * @}
  968. */
  969. /**
  970. * @}
  971. */
  972. /**
  973. * @}
  974. */