STSW-STLKT01: Projects/SensorTile/Applications/AudioLoop/Src/main.c Source File

STSW-STLKT01

main.c
Go to the documentation of this file.
1 
38 /* Includes ------------------------------------------------------------------*/
39 
40 #include <string.h> /* strlen */
41 #include <stdio.h> /* sprintf */
42 #include <math.h> /* trunc */
43 #include "main.h"
44 
45 /* Private typedef -----------------------------------------------------------*/
46 /* Private define ------------------------------------------------------------*/
47 #define AUDIO_CHANNELS 1
48 #define AUDIO_SAMPLING_FREQUENCY 48000
49 #define AUDIO_IN_BUF_LEN (AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000)
50 #define AUDIO_OUT_BUF_LEN (AUDIO_IN_BUF_LEN*8)
51 
52 /* Private macro -------------------------------------------------------------*/
53 /* Private variables ---------------------------------------------------------*/
54 
55 uint16_t PCM_Buffer[AUDIO_IN_BUF_LEN];
56 volatile int16_t audio_out_buffer[AUDIO_OUT_BUF_LEN];
57 
58 USBD_HandleTypeDef hUSBDDevice;
59 extern USBD_AUDIO_ItfTypeDef USBD_AUDIO_fops;
60 
61 static void *PCM1774_X_0_handle = NULL;
62 /* Private function prototypes -----------------------------------------------*/
63 
64 /* Private functions ---------------------------------------------------------*/
65 
71 int main( void )
72 {
73  /* STM32F4xx HAL library initialization:
74  - Configure the Flash prefetch, instruction and Data caches
75  - Configure the Systick to generate an interrupt each 1 msec
76  - Set NVIC Group Priority to 4
77  - Global MSP (MCU Support Package) initialization
78  */
79  HAL_Init();
80 
81  /* Configure the system clock */
83 
84  /* Configure Audio Output peripheral (SAI) and external DAC */
85  BSP_AUDIO_OUT_Init(PCM1774_0, &PCM1774_X_0_handle, 0, 20, AUDIO_SAMPLING_FREQUENCY);
86  BSP_AUDIO_OUT_SetVolume(PCM1774_X_0_handle, 20);
87 
88  /* Configure Audio Input peripheral - DFSDM */
89  BSP_AUDIO_IN_Init(AUDIO_SAMPLING_FREQUENCY, 16, AUDIO_CHANNELS);
90 
91  /* enable USB power on Pwrctrl CR2 register */
92  HAL_PWREx_EnableVddUSB();
93  /*Init USB*/
94  /* Initialize USB descriptor basing on channels number and sampling frequency */
95  USBD_AUDIO_Init_Microphone_Descriptor(&hUSBDDevice, AUDIO_SAMPLING_FREQUENCY, AUDIO_CHANNELS);
96  /* Init Device Library */
97  USBD_Init(&hUSBDDevice, &AUDIO_Desc, 0);
98  /* Add Supported Class */
99  USBD_RegisterClass(&hUSBDDevice, &USBD_AUDIO);
100  /* Add Interface callbacks for AUDIO Class */
101  USBD_AUDIO_RegisterInterface(&hUSBDDevice, &USBD_AUDIO_fops);
102  /* Start Device Process */
103  USBD_Start(&hUSBDDevice);
104 
105  /* Start Microphone acquisition */
106  BSP_AUDIO_IN_Record(PCM_Buffer,0);
107 
108 
109  while (1)
110  {
111  /* Go to Sleep, everything is done in the interrupt service routines */
112  __WFI();
113  }
114 }
115 
116 
125 void AudioProcess(void)
126 {
127  /*for L4 PDM to PCM conversion is performed in hardware by DFSDM peripheral*/
128  static uint32_t IndexOut = 0;
129  static uint32_t AudioOutActive = 0;
130  uint32_t indexIn;
131 
132  Send_Audio_to_USB((int16_t *)PCM_Buffer, AUDIO_SAMPLING_FREQUENCY/1000*AUDIO_CHANNELS);
133 
134  for(indexIn=0;indexIn<AUDIO_IN_BUF_LEN;indexIn++)
135  {
136  audio_out_buffer[IndexOut++] = PCM_Buffer[indexIn];
137  audio_out_buffer[IndexOut++] = PCM_Buffer[indexIn];
138  }
139 
140  if(!AudioOutActive && IndexOut==AUDIO_OUT_BUF_LEN/2)
141  {
142  BSP_AUDIO_OUT_Play(PCM1774_X_0_handle,(uint16_t*)audio_out_buffer, AUDIO_OUT_BUF_LEN);
143  AudioOutActive=1;
144  }
145  if(IndexOut==AUDIO_OUT_BUF_LEN)
146  {
147  IndexOut=0;
148  }
149 
150 }
151 
158 {
159  AudioProcess();
160 }
161 
168 {
169  AudioProcess();
170 }
171 
177 void Error_Handler( void )
178 {
179 
180  while (1)
181  {}
182 }
183 
184 
185 
186 #ifdef USE_FULL_ASSERT
187 
195 void assert_failed( uint8_t *file, uint32_t line )
196 {
197 
198  /* User can add his own implementation to report the file name and line number,
199  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
200 
201  while (1)
202  {}
203 }
204 
205 #endif
206 
207 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint8_t BSP_AUDIO_OUT_SetVolume(void *handle, uint8_t Volume)
Controls the current audio volume level.
uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
Initializes audio acquisition.
void AudioProcess(void)
User function that is called when 1 ms of PDM data is available. In this application only PDM to PCM ...
Definition: main.c:125
void BSP_AUDIO_IN_TransferComplete_CallBack(void)
Transfer Complete user callback, called by BSP functions.
Definition: main.c:157
uint8_t BSP_AUDIO_OUT_Init(CODEX_ID_t id, void **handle, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
Configures the audio peripherals.
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition: main.c:177
void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
Half Transfer Complete user callback, called by BSP functions.
Definition: main.c:167
void Send_Audio_to_USB(int16_t *audioData, uint16_t PCMSamples)
Fills USB audio buffer with the right amount of data, depending on the channel/frequency configuratio...
int main(void)
Main program.
Definition: main.c:71
void SystemClock_Config(void)
System Clock Configuration.
Definition: cube_hal_l4.c:48
uint8_t BSP_AUDIO_IN_Record(uint16_t *pbuf, uint32_t size)
Starts audio recording.
uint8_t BSP_AUDIO_OUT_Play(void *handle, uint16_t *pBuffer, uint32_t Size)
Starts playing audio stream from a data buffer for a determined size.
Generated by   doxygen 1.8.13