Skip to content
Extraits de code Groupes Projets
Valider 0c282fc8 rédigé par Félix Willaumez's avatar Félix Willaumez
Parcourir les fichiers

sound detector on mcu

parent 7ad92561
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
{ {
"files.associations": {
"chrono": "c",
"algorithm": "c"
},
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "", "C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false, "files.associations": {
"C_Cpp_Runner.warnings": [ "arm_math.h": "c"
"-Wall", }
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
} }
\ No newline at end of file
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
#define ADC_BUF_SIZE SAMPLES_PER_MELVEC #define ADC_BUF_SIZE SAMPLES_PER_MELVEC
int StartADCAcq(int32_t n_bufs); int StartADCAcq_recording(int32_t n_bufs);
int StartADCAcq_detecting(int32_t n_bufs);
int IsADCFinished(void); int IsADCFinished(void);
extern ADC_HandleTypeDef hadc1; extern ADC_HandleTypeDef hadc1;
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#define ENABLE_RADIO 1 #define ENABLE_RADIO 1
// General UART enable/disable (disable for low-power operation) // General UART enable/disable (disable for low-power operation)
#define ENABLE_UART 0 #define ENABLE_UART 1
// In continuous mode, we start and stop continuous acquisition on button press. // In continuous mode, we start and stop continuous acquisition on button press.
// In non-continuous mode, we send a single packet on button press. // In non-continuous mode, we send a single packet on button press.
...@@ -27,12 +27,15 @@ ...@@ -27,12 +27,15 @@
#define SAMPLES_PER_MELVEC 512 #define SAMPLES_PER_MELVEC 512
#define MELVEC_LENGTH 20 #define MELVEC_LENGTH 20
#define N_MELVECS 20 #define N_MELVECS 20
#define N_MELVECS_DETECTION 20 // nombre de melvecs sur lequel on regarde si il y a un son ou non
#define DETECTION_FACTOR 10
#define NUMBER_OF_PACKETS_WHEN_DETECTED 3
// Enable performance measurements // Enable performance measurements
#define PERF_COUNT 0 #define PERF_COUNT 0
// Enable debug print // Enable debug print
#define DEBUGP 0 #define DEBUGP 1
#if (DEBUGP == 1) #if (DEBUGP == 1)
#define DEBUG_PRINT(...) do{ printf(__VA_ARGS__ ); } while( 0 ) #define DEBUG_PRINT(...) do{ printf(__VA_ARGS__ ); } while( 0 )
......
...@@ -12,14 +12,28 @@ static volatile uint16_t ADCDoubleBuf[2*ADC_BUF_SIZE]; /* ADC group regular conv ...@@ -12,14 +12,28 @@ static volatile uint16_t ADCDoubleBuf[2*ADC_BUF_SIZE]; /* ADC group regular conv
static volatile uint16_t* ADCData[2] = {&ADCDoubleBuf[0], &ADCDoubleBuf[ADC_BUF_SIZE]}; static volatile uint16_t* ADCData[2] = {&ADCDoubleBuf[0], &ADCDoubleBuf[ADC_BUF_SIZE]};
static volatile uint8_t ADCDataRdy[2] = {0, 0}; static volatile uint8_t ADCDataRdy[2] = {0, 0};
static volatile uint16_t ADCDoubleBuf_detecting[2*ADC_BUF_SIZE]; /* ADC group regular conversion data (array of data) */
static volatile uint16_t* ADCData_detecting[2] = {&ADCDoubleBuf_detecting[0], &ADCDoubleBuf_detecting[ADC_BUF_SIZE]};
static volatile uint8_t ADCDataRdy_detecting[2] = {0, 0};
static volatile uint8_t cur_melvec = 0; static volatile uint8_t cur_melvec = 0;
static volatile uint8_t cur_melvec_detecting = 0;
static q15_t mel_vectors[N_MELVECS][MELVEC_LENGTH]; static q15_t mel_vectors[N_MELVECS][MELVEC_LENGTH];
static uint32_t packet_cnt = 0; static uint32_t packet_cnt = 0;
static volatile int32_t rem_n_bufs = 0; static volatile int32_t rem_n_bufs = 0;
static volatile int32_t rem_n_bufs_detecting = 0;
extern int8_t recording;
extern uint8_t detecting;
static volatile uint8_t noise_sampled = 0;
static volatile float noise_power = 0;
static volatile float current_power = 0;
int StartADCAcq(int32_t n_bufs) { int StartADCAcq_recording(int32_t n_bufs) {
rem_n_bufs = n_bufs; rem_n_bufs = n_bufs;
cur_melvec = 0; cur_melvec = 0;
if (rem_n_bufs != 0) { if (rem_n_bufs != 0) {
...@@ -29,10 +43,21 @@ int StartADCAcq(int32_t n_bufs) { ...@@ -29,10 +43,21 @@ int StartADCAcq(int32_t n_bufs) {
} }
} }
int StartADCAcq_detecting(int32_t n_bufs) {
rem_n_bufs_detecting = n_bufs;
cur_melvec_detecting = 0;
if (rem_n_bufs_detecting != 0) {
return HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADCDoubleBuf_detecting, 2*ADC_BUF_SIZE);
} else {
return HAL_OK;
}
}
int IsADCFinished(void) { int IsADCFinished(void) {
return (rem_n_bufs == 0); return (rem_n_bufs == 0 && rem_n_bufs_detecting == 0);
} }
static void StopADCAcq() { static void StopADCAcq() {
HAL_ADC_Stop_DMA(&hadc1); HAL_ADC_Stop_DMA(&hadc1);
} }
...@@ -81,13 +106,13 @@ static void encode_packet(uint8_t *packet, uint32_t* packet_cnt) { ...@@ -81,13 +106,13 @@ static void encode_packet(uint8_t *packet, uint32_t* packet_cnt) {
static void send_spectrogram() { static void send_spectrogram() {
uint8_t packet[PACKET_LENGTH]; uint8_t packet[PACKET_LENGTH];
start_cycle_count(); //start_cycle_count();
encode_packet(packet, &packet_cnt); encode_packet(packet, &packet_cnt);
stop_cycle_count("Encode packet"); //stop_cycle_count("Encode packet");
start_cycle_count(); //start_cycle_count();
S2LP_Send(packet, PACKET_LENGTH); S2LP_Send(packet, PACKET_LENGTH);
stop_cycle_count("Send packet"); //stop_cycle_count("Send packet");
print_encoded_packet(packet); print_encoded_packet(packet);
} }
...@@ -116,12 +141,77 @@ static void ADC_Callback(int buf_cplt) { ...@@ -116,12 +141,77 @@ static void ADC_Callback(int buf_cplt) {
} }
} }
static float compute_power(q15_t *buf){
float y;
float power = 0;
for (int i = 0; i < ADC_BUF_SIZE; i++)
{
y = q15_to_float(buf[i]);
power += y*y;
}
return power;
}
static void ADC_Callback_detecting(int buf_cplt){
if (rem_n_bufs_detecting != -1) {
rem_n_bufs_detecting--;
}if (rem_n_bufs_detecting == 0) {
StopADCAcq();
}
else if (ADCDataRdy_detecting[1-buf_cplt]) {
DEBUG_PRINT("Error: ADC Data buffer full for detection %u\r\n",ADCDataRdy_detecting[1-buf_cplt]);
Error_Handler();
}
ADCDataRdy_detecting[buf_cplt] = 1;
//start_cycle_count();
if (noise_sampled <= N_MELVECS_DETECTION)
{
Spectrogram_Format((q15_t *)ADCData_detecting[buf_cplt]);
noise_power += compute_power(ADCData_detecting[buf_cplt])/22;
noise_sampled ++;
}else{
Spectrogram_Format((q15_t *)ADCData_detecting[buf_cplt]);
current_power += compute_power(ADCData_detecting[buf_cplt])/(N_MELVECS_DETECTION);
}
cur_melvec_detecting++;
ADCDataRdy_detecting[buf_cplt] = 0;
if (rem_n_bufs_detecting == 0) {
DEBUG_PRINT(" noise = %f actual = %f noise power - actual power: %i \n",noise_power, current_power, noise_power - current_power);
if(current_power > DETECTION_FACTOR*noise_power){
detecting = 0;
recording = NUMBER_OF_PACKETS_WHEN_DETECTED;
noise_power = (noise_power*9/10 + current_power*1/10);
}else{
noise_power = (noise_power + current_power)/2;
}
}
if (rem_n_bufs_detecting == 0) {
current_power = 0;
}
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{ {
ADC_Callback(1); if (recording > 0)
{
ADC_Callback(1);
}else if (detecting)
{
ADC_Callback_detecting(1);
}
} }
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc) void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
{ {
ADC_Callback(0); if (recording > 0)
{
ADC_Callback(0);
}else if (detecting)
{
ADC_Callback_detecting(0);
}
} }
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
* EXTI * EXTI
* Free pins are configured automatically as Analog (this feature is enabled through * Free pins are configured automatically as Analog (this feature is enabled through
* the Code Generation settings) * the Code Generation settings)
PA0 ------> ADCx_IN5
PA8 ------> USB_OTG_FS_SOF PA8 ------> USB_OTG_FS_SOF
PA10 ------> USB_OTG_FS_ID PA10 ------> USB_OTG_FS_ID
PA11 ------> USB_OTG_FS_DM PA11 ------> USB_OTG_FS_DM
...@@ -129,10 +130,10 @@ void MX_GPIO_Init(void) ...@@ -129,10 +130,10 @@ void MX_GPIO_Init(void)
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PA1 PA2 PA4 PA5 /*Configure GPIO pins : PA0 PA1 PA2 PA4
PA15 */ PA5 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4
|GPIO_PIN_15; |GPIO_PIN_5|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
......
...@@ -62,6 +62,8 @@ ...@@ -62,6 +62,8 @@
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
volatile uint8_t btn_press; volatile uint8_t btn_press;
volatile int8_t recording;
volatile uint8_t detecting;
/* USER CODE END PV */ /* USER CODE END PV */
...@@ -84,17 +86,39 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) ...@@ -84,17 +86,39 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
} }
static void acquire_and_send_packet() { static void acquire_and_send_packet() {
if (StartADCAcq(N_MELVECS) != HAL_OK) {
DEBUG_PRINT("Error while enabling the DMA\r\n"); if (recording > 0)
} {
if (StartADCAcq_recording(N_MELVECS) != HAL_OK) {
DEBUG_PRINT("Error while enabling the DMA for recording\r\n");
}
DEBUG_PRINT("recording : %d\n",recording);
}
else if (detecting == 1)
{
int error = StartADCAcq_detecting(N_MELVECS_DETECTION);
if (error != HAL_OK) {
DEBUG_PRINT("Error while enabling the DMA for detection: %i\r\n",error);
}
}
while (!IsADCFinished()) { while (!IsADCFinished()) {
__WFI(); //wait for adc callback __WFI(); //wait for adc callback (when we call stop ADC)
} }
if(recording <= 0){
detecting = 1;
DEBUG_PRINT("detecting : %u\n",detecting);
}else{
recording--;
}
} }
void run(void) void run(void)
{ {
btn_press = 0; btn_press = 0;
detecting = 1;
recording = 0;
while (1) while (1)
{ {
......
...@@ -238,7 +238,7 @@ ProjectManager.ToolChainLocation= ...@@ -238,7 +238,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath= ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=true ProjectManager.UnderRoot=true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-false,4-MX_LPUART1_UART_Init-LPUART1-true-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM3_Init-TIM3-false-HAL-true,7-MX_ADC1_Init-ADC1-false-HAL-true ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-false,4-MX_LPUART1_UART_Init-LPUART1-true-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM3_Init-TIM3-false-HAL-true,7-MX_ADC1_Init-ADC1-false-HAL-true,8-MX_ADC2_Init-ADC2-false-HAL-true
RCC.ADCCLockSelection=RCC_ADCCLKSOURCE_SYSCLK RCC.ADCCLockSelection=RCC_ADCCLKSOURCE_SYSCLK
RCC.ADCFreq_Value=48000000 RCC.ADCFreq_Value=48000000
RCC.AHBFreq_Value=48000000 RCC.AHBFreq_Value=48000000
...@@ -297,7 +297,8 @@ RCC.VCOOutputFreq_Value=1152000000 ...@@ -297,7 +297,8 @@ RCC.VCOOutputFreq_Value=1152000000
RCC.VCOSAI1OutputFreq_Value=1152000000 RCC.VCOSAI1OutputFreq_Value=1152000000
RCC.VCOSAI2OutputFreq_Value=384000000 RCC.VCOSAI2OutputFreq_Value=384000000
SH.ADCx_IN5.0=ADC1_IN5,IN5-Single-Ended SH.ADCx_IN5.0=ADC1_IN5,IN5-Single-Ended
SH.ADCx_IN5.ConfNb=1 SH.ADCx_IN5.1=ADC2_IN5
SH.ADCx_IN5.ConfNb=2
SH.GPXTI13.0=GPIO_EXTI13 SH.GPXTI13.0=GPIO_EXTI13
SH.GPXTI13.ConfNb=1 SH.GPXTI13.ConfNb=1
SH.GPXTI3.0=GPIO_EXTI3 SH.GPXTI3.0=GPIO_EXTI3
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter