From 7b8c3c5f2af944059b466dcc82fd2e3a20e2b588 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Thu, 20 Jun 2024 13:28:45 +0200 Subject: [PATCH 01/23] PDU-microphone DMA acquisition operative. --- sw_stm32/.cproject | 690 ++++++++++++----------- sw_stm32/.gitignore | 1 + sw_stm32/Core/Src/MPU_initialization.cpp | 2 +- sw_stm32/Drivers/Custom/microphone.cpp | 119 ++++ sw_stm32/Drivers/Custom/spi.cpp | 19 +- sw_stm32/Drivers/Custom/spi.h | 1 + sw_stm32/lib | 2 +- 7 files changed, 502 insertions(+), 332 deletions(-) create mode 100644 sw_stm32/Drivers/Custom/microphone.cpp diff --git a/sw_stm32/.cproject b/sw_stm32/.cproject index f181c60..e1b97ed 100644 --- a/sw_stm32/.cproject +++ b/sw_stm32/.cproject @@ -1,330 +1,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -540,9 +216,6 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sw_stm32/.gitignore b/sw_stm32/.gitignore index 461c816..0cf5179 100644 --- a/sw_stm32/.gitignore +++ b/sw_stm32/.gitignore @@ -1,3 +1,4 @@ ./larus_sensor_V2_image.bin ./.metadata/ /.metadata/ +/debug/ diff --git a/sw_stm32/Core/Src/MPU_initialization.cpp b/sw_stm32/Core/Src/MPU_initialization.cpp index 0cde776..807fe56 100644 --- a/sw_stm32/Core/Src/MPU_initialization.cpp +++ b/sw_stm32/Core/Src/MPU_initialization.cpp @@ -111,7 +111,7 @@ extern "C" void MPU_initialization(void) MX_I2C2_Init(); MX_SDIO_SD_Init(); MX_SPI1_Init(); - MX_SPI2_Init(); +// MX_SPI2_Init(); // MX_USART2_UART_Init(); MX_USART6_UART_Init(); MX_FATFS_Init(); diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp new file mode 100644 index 0000000..601d214 --- /dev/null +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -0,0 +1,119 @@ +#include "system_configuration.h" +#include "FreeRTOS_wrapper.h" + +#include "generic_CAN_driver.h" +#include "candriver.h" + +#include "main.h" + +#include "stm32f4xx_hal.h" +#include "stm32f4xx_hal_cortex.h" +#include "stm32f4xx_hal_spi.h" +#include "spi.h" + +extern SPI_HandleTypeDef hspi2; +extern DMA_HandleTypeDef hdma_spi2_rx; + +void configure_SPI_interface (void) +{ + __HAL_RCC_SPI2_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct = { 0 }; + + /*Configure GPIO pin : SPI2_NSS_Pin */ + GPIO_InitStruct.Pin = SPI2_NSS_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + HAL_GPIO_Init (SPI2_NSS_GPIO_Port, &GPIO_InitStruct); + + HAL_GPIO_WritePin (SPI2_NSS_GPIO_Port, SPI2_NSS_Pin, GPIO_PIN_SET); // disable + + /**SPI2 GPIO Configuration + PB13 ------> SPI2_SCK + PB14 ------> SPI2_MISO + PB15 ------> SPI2_MOSI */ + GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; + hspi2.Init.DataSize = SPI_DATASIZE_16BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 10; + if (HAL_SPI_Init(&hspi2) != HAL_OK) + { + Error_Handler(); + } + + /* SPI2 DMA Init */ + /* SPI2_RX Init */ + hdma_spi2_rx.Instance = DMA1_Stream3; + hdma_spi2_rx.Init.Channel = DMA_CHANNEL_0; + hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE; + hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_spi2_rx.Init.MemDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_spi2_rx.Init.Mode = DMA_CIRCULAR; + hdma_spi2_rx.Init.Priority = DMA_PRIORITY_LOW; + hdma_spi2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; + if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA( &hspi2 , hdmarx, hdma_spi2_rx); +} + +#define MIC_DMA_BUFSIZE_HALFWORDS 256 + +static uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * 2) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; + +COMMON unsigned isr_counter; +COMMON unsigned bits_seen; + +static void runnable (void*) +{ + configure_SPI_interface (); + register_SPI_usertask( &hspi2); + + volatile HAL_StatusTypeDef status; + status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + + uint32_t NotificationValue=0; + while (true) + { + xTaskNotifyWait( 0, 0, &NotificationValue, INFINITE_WAIT); + bits_seen |= NotificationValue == 1 ? 1 : 2; + } +} + +static TaskParameters_t p = + { + runnable, + "MIC", + configMINIMAL_STACK_SIZE * 4, + 0, + STANDARD_TASK_PRIORITY, + 0, + { + { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, + { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, 0 }, + { 0, 0, 0 } + } + }; + +static RestrictedTask mic_task ( p); diff --git a/sw_stm32/Drivers/Custom/spi.cpp b/sw_stm32/Drivers/Custom/spi.cpp index dc36777..422a184 100644 --- a/sw_stm32/Drivers/Custom/spi.cpp +++ b/sw_stm32/Drivers/Custom/spi.cpp @@ -34,7 +34,7 @@ extern DMA_HandleTypeDef hdma_spi2_tx; COMMON static TaskHandle_t SPI1_task_Id = NULL; COMMON static TaskHandle_t SPI2_task_Id = NULL; -static inline void register_SPI_usertask(SPI_HandleTypeDef *hspi) +void register_SPI_usertask(SPI_HandleTypeDef *hspi) { if (hspi->Instance == SPI1) SPI1_task_Id = xTaskGetCurrentTaskHandle(); @@ -88,8 +88,11 @@ void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) } else if (hspi->Instance == SPI2) { - ASSERT( SPI2_task_Id); - vTaskNotifyGiveFromISR( SPI2_task_Id, &HigherPriorityTaskWoken); + ASSERT( SPI2_task_Id !=0); + + BaseType_t xYieldRequired = pdFALSE;; + xTaskNotifyFromISR( SPI2_task_Id, 1, eSetValueWithOverwrite, &xYieldRequired); + portEND_SWITCHING_ISR(xYieldRequired); } else { @@ -108,6 +111,16 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) HAL_SPI_CpltCallback( hspi); } +void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi) +{ + if (hspi->Instance != SPI2) + return; + + BaseType_t xYieldRequired = pdFALSE;; + ASSERT( SPI2_task_Id !=0); + xTaskNotifyFromISR( SPI2_task_Id, 0, eSetValueWithOverwrite, &xYieldRequired); + portEND_SWITCHING_ISR(xYieldRequired); +} void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { diff --git a/sw_stm32/Drivers/Custom/spi.h b/sw_stm32/Drivers/Custom/spi.h index 1657ef9..d10f96f 100644 --- a/sw_stm32/Drivers/Custom/spi.h +++ b/sw_stm32/Drivers/Custom/spi.h @@ -20,6 +20,7 @@ void SPI_Init(SPI_HandleTypeDef *hspi); void SPI_Transceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size); void SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint16_t Size, uint32_t timeout=0); void SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pRxData, uint16_t Size, uint32_t timeout=0); +void register_SPI_usertask(SPI_HandleTypeDef *hspi); #ifdef __cplusplus } diff --git a/sw_stm32/lib b/sw_stm32/lib index ba92a77..8388bc8 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit ba92a77e38f078cbb496d078b9e184dbb4670c70 +Subproject commit 8388bc85fe4a0a2e683dfd379719c5c2dc6aa6b3 From d57b616a1769093cc2f9764e660c0190555eff9b Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Thu, 20 Jun 2024 17:25:48 +0200 Subject: [PATCH 02/23] Implemented downsampling to 20 kHz. --- sw_stm32/Drivers/Custom/microphone.cpp | 61 ++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 601d214..4286be6 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -10,6 +10,19 @@ #include "stm32f4xx_hal_cortex.h" #include "stm32f4xx_hal_spi.h" #include "spi.h" +#include "embedded_math.h" + +ROM int8_t bit_count_table[] = +{ + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 +}; extern SPI_HandleTypeDef hspi2; extern DMA_HandleTypeDef hdma_spi2_rx; @@ -48,7 +61,8 @@ void configure_SPI_interface (void) hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; - hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; + // bit samplerate will be 168Mhz / 4 / 16 => 2.625 Mhz + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLE; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; @@ -79,11 +93,29 @@ void configure_SPI_interface (void) } #define MIC_DMA_BUFSIZE_HALFWORDS 256 +#define FLOAT_BUFSIZE 512 -static uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * 2) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; +uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * 2) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; +float __ALIGNED( FLOAT_BUFSIZE * sizeof(float)) samples_at_20_kHz[FLOAT_BUFSIZE]; -COMMON unsigned isr_counter; -COMMON unsigned bits_seen; +COMMON unsigned count32; + +// our byte samples have 328kHz sampling-rate +// resampling by 16 gives 20.5 kHz +void resample_by_16( uint8_t * bytes, float * samples, unsigned bytecount) +{ + while( true) + { + int accumulator = -1 * 4 /* bits */ * 16 /* samples */; // remove DC bias + for( unsigned i=0; i<16; ++i) + accumulator += bit_count_table[*bytes++]; + *samples++ = (float)accumulator; + + bytecount -= 16; + if( bytecount == 0) + return; + } +} static void runnable (void*) { @@ -93,11 +125,22 @@ static void runnable (void*) volatile HAL_StatusTypeDef status; status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - uint32_t NotificationValue=0; + uint32_t BufferIndex; // 0 -> first half, 1 second half while (true) { - xTaskNotifyWait( 0, 0, &NotificationValue, INFINITE_WAIT); - bits_seen |= NotificationValue == 1 ? 1 : 2; + xTaskNotifyWait( 0, 0, &BufferIndex, INFINITE_WAIT); + if( BufferIndex != 0) + continue; // synchronize + // now we have 256 byte samples @ the beginning of our buffer + resample_by_16( (uint8_t *)mic_DMA_buffer, samples_at_20_kHz, 256); + + xTaskNotifyWait( 0, 0, &BufferIndex, INFINITE_WAIT); + if( BufferIndex != 1) + continue; // synchronize + // now we have another 256 byte samples @ the middle of our buffer + resample_by_16( (uint8_t *)(mic_DMA_buffer + (MIC_DMA_BUFSIZE_HALFWORDS / 2)), samples_at_20_kHz + 16, 256); + + ++count32; } } @@ -111,8 +154,8 @@ static TaskParameters_t p = 0, { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, - { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, 0 }, - { 0, 0, 0 } + { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, portMPU_REGION_READ_ONLY }, + { samples_at_20_kHz, FLOAT_BUFSIZE * sizeof(float), portMPU_REGION_READ_WRITE } } }; From 4c035946a822498eeea0ffd2254fb3012630a724 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 21 Jun 2024 14:03:29 +0200 Subject: [PATCH 03/23] Missing function added static inline. --- sw_stm32/Core/Src/system_support.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sw_stm32/Core/Src/system_support.cpp b/sw_stm32/Core/Src/system_support.cpp index 1d59a8d..2bde5dd 100644 --- a/sw_stm32/Core/Src/system_support.cpp +++ b/sw_stm32/Core/Src/system_support.cpp @@ -107,7 +107,21 @@ uint64_t getTime_usec_privileged(void) return time + present_systick; } -extern "C" BaseType_t xPortRaisePrivilege( void ); +inline BaseType_t xPortRaisePrivilege( void ) /* FREERTOS_SYSTEM_CALL */ +{ +BaseType_t xRunningPrivileged; + + /* Check whether the processor is already privileged. */ + xRunningPrivileged = portIS_PRIVILEGED(); + + /* If the processor is not already privileged, raise privilege. */ + if( xRunningPrivileged != pdTRUE ) + { + portRAISE_PRIVILEGE(); + } + + return xRunningPrivileged; +} typedef int ( *FPTR)( void *); // declare void* -> int function pointer int call_function_privileged( void * parameters, FPTR function) From 2c60b23dcdd775e549aff38c7cabdc782c85dbde Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 21 Jun 2024 14:04:48 +0200 Subject: [PATCH 04/23] Audio intensity measurment basically up and running. Strange statistical properties seen ?! --- sw_stm32/Drivers/Custom/microphone.cpp | 126 +++++++++++++++++++++---- 1 file changed, 106 insertions(+), 20 deletions(-) diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 4286be6..99e765d 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -93,30 +93,48 @@ void configure_SPI_interface (void) } #define MIC_DMA_BUFSIZE_HALFWORDS 256 -#define FLOAT_BUFSIZE 512 +#define SAMPLE_BUFSIZE 512 -uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * 2) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; -float __ALIGNED( FLOAT_BUFSIZE * sizeof(float)) samples_at_20_kHz[FLOAT_BUFSIZE]; +uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * sizeof( uint16_t)) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; +int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[SAMPLE_BUFSIZE]; -COMMON unsigned count32; +COMMON uint32_t out_of_range_count; +COMMON uint32_t zero_seen; // our byte samples have 328kHz sampling-rate -// resampling by 16 gives 20.5 kHz -void resample_by_16( uint8_t * bytes, float * samples, unsigned bytecount) +// resampling by 64 gives 5.125 kHz sampling-rate +void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) { while( true) { - int accumulator = -1 * 4 /* bits */ * 16 /* samples */; // remove DC bias - for( unsigned i=0; i<16; ++i) + int accumulator = 0; + + for( unsigned i=0; i<64; ++i) + { + if( *bytes ==0) + ++zero_seen; accumulator += bit_count_table[*bytes++]; - *samples++ = (float)accumulator; + } + + if( (accumulator > 256 + 127) || ( accumulator < 128)) + ++out_of_range_count; + *samples++ = (int8_t)(accumulator - 256); - bytecount -= 16; + bytecount -= 64; if( bytecount == 0) return; } } +uint64_t getTime_usec(void); + +COMMON int32_t ac_power; +COMMON int32_t dc_content; +COMMON volatile uint64_t time_delta; +COMMON volatile uint64_t time_delta_max; +COMMON volatile uint64_t time_delta_min; +COMMON uint32_t overrun_count; + static void runnable (void*) { configure_SPI_interface (); @@ -126,21 +144,89 @@ static void runnable (void*) status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); uint32_t BufferIndex; // 0 -> first half, 1 second half + + time_delta_max=0; + time_delta_min=0xffffffffffffffff; + + int8_t * target = samples_at_5_kHz; + int32_t sum = 0; + int32_t qsum = 0; + while (true) { - xTaskNotifyWait( 0, 0, &BufferIndex, INFINITE_WAIT); - if( BufferIndex != 0) - continue; // synchronize + BufferIndex = 0xffffffff; + + uint64_t timestamp = getTime_usec(); + xTaskNotifyWait( 0, 0, &BufferIndex, 2); + + time_delta = getTime_usec() - timestamp; + if( time_delta < time_delta_min) + time_delta_min = time_delta; + if( time_delta > time_delta_max) + time_delta_max = time_delta; + + if( BufferIndex != 0) // be sure to be behind the half transfer interrupt + { + target = samples_at_5_kHz; + sum = 0; + qsum = 0; + ++overrun_count; + HAL_SPI_DMAStop( &hspi2); + status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + continue; // re-synchronize + } // now we have 256 byte samples @ the beginning of our buffer - resample_by_16( (uint8_t *)mic_DMA_buffer, samples_at_20_kHz, 256); + resample_by_64( (uint8_t *)mic_DMA_buffer, target, MIC_DMA_BUFSIZE_HALFWORDS); - xTaskNotifyWait( 0, 0, &BufferIndex, INFINITE_WAIT); - if( BufferIndex != 1) - continue; // synchronize + // do statistics and advance pointer + for( unsigned i=0; i<8; ++i) + { + sum += (int32_t)*target; + qsum += (int32_t)*target * *target; + ++target; + } + + BufferIndex = 0xffffffff; + timestamp = getTime_usec(); + xTaskNotifyWait( 0, 0, &BufferIndex, 2); + + time_delta = getTime_usec() - timestamp; + if( time_delta < time_delta_min) + time_delta_min = time_delta; + if( time_delta > time_delta_max) + time_delta_max = time_delta; + + if( BufferIndex != 1) // be sure to be behind the transfer complete interrupt + { + target = samples_at_5_kHz; + sum = 0; + qsum = 0; + ++overrun_count; + HAL_SPI_DMAStop( &hspi2); + status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + continue; // re-synchronize + } // now we have another 256 byte samples @ the middle of our buffer - resample_by_16( (uint8_t *)(mic_DMA_buffer + (MIC_DMA_BUFSIZE_HALFWORDS / 2)), samples_at_20_kHz + 16, 256); + resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), target, MIC_DMA_BUFSIZE_HALFWORDS); + + // do statistics and advance pointer + for( unsigned i=0; i<8; ++i) + { + sum += (int32_t)*target; + qsum += (int32_t)*target * *target; + ++target; + } - ++count32; + // summa summarum ... + if( target >= samples_at_5_kHz + SAMPLE_BUFSIZE) + { + // ac_energy = qsum - sum * sum; + ac_power = qsum / 16; + dc_content= sum / 16; + target = samples_at_5_kHz; + sum = 0; + qsum = 0; + } } } @@ -155,7 +241,7 @@ static TaskParameters_t p = { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, portMPU_REGION_READ_ONLY }, - { samples_at_20_kHz, FLOAT_BUFSIZE * sizeof(float), portMPU_REGION_READ_WRITE } + { samples_at_5_kHz, SAMPLE_BUFSIZE, portMPU_REGION_READ_WRITE } } }; From 8b2ac16d1ef5e9b3733022eca5c77263f7e52a49 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 21 Jun 2024 15:21:26 +0200 Subject: [PATCH 05/23] Signal statistics fixed. --- sw_stm32/Drivers/Custom/microphone.cpp | 58 +++++++++++++------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 99e765d..9b89302 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -105,7 +105,7 @@ COMMON uint32_t zero_seen; // resampling by 64 gives 5.125 kHz sampling-rate void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) { - while( true) + do { int accumulator = 0; @@ -121,15 +121,14 @@ void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) *samples++ = (int8_t)(accumulator - 256); bytecount -= 64; - if( bytecount == 0) - return; } + while( bytecount > 0); } uint64_t getTime_usec(void); -COMMON int32_t ac_power; -COMMON int32_t dc_content; +COMMON float ac_power; +COMMON float dc_content; COMMON volatile uint64_t time_delta; COMMON volatile uint64_t time_delta_max; COMMON volatile uint64_t time_delta_min; @@ -148,14 +147,13 @@ static void runnable (void*) time_delta_max=0; time_delta_min=0xffffffffffffffff; - int8_t * target = samples_at_5_kHz; + int8_t * samples_pointer = samples_at_5_kHz; int32_t sum = 0; int32_t qsum = 0; while (true) { BufferIndex = 0xffffffff; - uint64_t timestamp = getTime_usec(); xTaskNotifyWait( 0, 0, &BufferIndex, 2); @@ -167,23 +165,25 @@ static void runnable (void*) if( BufferIndex != 0) // be sure to be behind the half transfer interrupt { - target = samples_at_5_kHz; - sum = 0; - qsum = 0; ++overrun_count; HAL_SPI_DMAStop( &hspi2); status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + + samples_pointer = samples_at_5_kHz; + sum = 0; + qsum = 0; + continue; // re-synchronize } // now we have 256 byte samples @ the beginning of our buffer - resample_by_64( (uint8_t *)mic_DMA_buffer, target, MIC_DMA_BUFSIZE_HALFWORDS); + resample_by_64( (uint8_t *)mic_DMA_buffer, samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS); // do statistics and advance pointer - for( unsigned i=0; i<8; ++i) + for( unsigned i=0; i<4; ++i) { - sum += (int32_t)*target; - qsum += (int32_t)*target * *target; - ++target; + sum += (int32_t)*samples_pointer; + qsum += (int32_t)*samples_pointer * *samples_pointer; + ++samples_pointer; } BufferIndex = 0xffffffff; @@ -198,32 +198,34 @@ static void runnable (void*) if( BufferIndex != 1) // be sure to be behind the transfer complete interrupt { - target = samples_at_5_kHz; - sum = 0; - qsum = 0; ++overrun_count; HAL_SPI_DMAStop( &hspi2); status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + + samples_pointer = samples_at_5_kHz; + sum = 0; + qsum = 0; + continue; // re-synchronize } // now we have another 256 byte samples @ the middle of our buffer - resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), target, MIC_DMA_BUFSIZE_HALFWORDS); + resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS); // do statistics and advance pointer - for( unsigned i=0; i<8; ++i) + for( unsigned i=0; i<4; ++i) { - sum += (int32_t)*target; - qsum += (int32_t)*target * *target; - ++target; + sum += (int32_t)*samples_pointer; + qsum += (int32_t)*samples_pointer * *samples_pointer; + ++samples_pointer; } // summa summarum ... - if( target >= samples_at_5_kHz + SAMPLE_BUFSIZE) + if( samples_pointer >= samples_at_5_kHz + SAMPLE_BUFSIZE) { - // ac_energy = qsum - sum * sum; - ac_power = qsum / 16; - dc_content= sum / 16; - target = samples_at_5_kHz; + ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / (float)SAMPLE_BUFSIZE; + dc_content = sum / (float)SAMPLE_BUFSIZE; + + samples_pointer = samples_at_5_kHz; sum = 0; qsum = 0; } From 981f609f7d3390977e0eab7e724f7b645ccb3d10 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 21 Jun 2024 17:27:37 +0200 Subject: [PATCH 06/23] Cleanup. Added switch for microphone driver. --- sw_stm32/Core/Inc/system_configuration.h | 1 + sw_stm32/Drivers/Custom/microphone.cpp | 49 ++++++------------------ 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index 52ff892..3410a29 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -46,6 +46,7 @@ #define RUN_L3GD20 0 #define RUN_FXOS8700 0 #define RUN_PITOT_MODULE 1 +#define RUN_MICROPHONE 1 #define RUN_CAN_TESTER 0 #define TEST_EEPROM 0 diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 9b89302..3abbb9f 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -12,6 +12,8 @@ #include "spi.h" #include "embedded_math.h" +#if RUN_MICROPHONE + ROM int8_t bit_count_table[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, @@ -98,9 +100,6 @@ void configure_SPI_interface (void) uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * sizeof( uint16_t)) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[SAMPLE_BUFSIZE]; -COMMON uint32_t out_of_range_count; -COMMON uint32_t zero_seen; - // our byte samples have 328kHz sampling-rate // resampling by 64 gives 5.125 kHz sampling-rate void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) @@ -110,14 +109,8 @@ void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) int accumulator = 0; for( unsigned i=0; i<64; ++i) - { - if( *bytes ==0) - ++zero_seen; - accumulator += bit_count_table[*bytes++]; - } + accumulator += bit_count_table[*bytes++]; - if( (accumulator > 256 + 127) || ( accumulator < 128)) - ++out_of_range_count; *samples++ = (int8_t)(accumulator - 256); bytecount -= 64; @@ -129,24 +122,16 @@ uint64_t getTime_usec(void); COMMON float ac_power; COMMON float dc_content; -COMMON volatile uint64_t time_delta; -COMMON volatile uint64_t time_delta_max; -COMMON volatile uint64_t time_delta_min; -COMMON uint32_t overrun_count; static void runnable (void*) { configure_SPI_interface (); register_SPI_usertask( &hspi2); - volatile HAL_StatusTypeDef status; - status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); uint32_t BufferIndex; // 0 -> first half, 1 second half - time_delta_max=0; - time_delta_min=0xffffffffffffffff; - int8_t * samples_pointer = samples_at_5_kHz; int32_t sum = 0; int32_t qsum = 0; @@ -157,17 +142,10 @@ static void runnable (void*) uint64_t timestamp = getTime_usec(); xTaskNotifyWait( 0, 0, &BufferIndex, 2); - time_delta = getTime_usec() - timestamp; - if( time_delta < time_delta_min) - time_delta_min = time_delta; - if( time_delta > time_delta_max) - time_delta_max = time_delta; - if( BufferIndex != 0) // be sure to be behind the half transfer interrupt { - ++overrun_count; HAL_SPI_DMAStop( &hspi2); - status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); samples_pointer = samples_at_5_kHz; sum = 0; @@ -187,20 +165,12 @@ static void runnable (void*) } BufferIndex = 0xffffffff; - timestamp = getTime_usec(); xTaskNotifyWait( 0, 0, &BufferIndex, 2); - time_delta = getTime_usec() - timestamp; - if( time_delta < time_delta_min) - time_delta_min = time_delta; - if( time_delta > time_delta_max) - time_delta_max = time_delta; - if( BufferIndex != 1) // be sure to be behind the transfer complete interrupt { - ++overrun_count; HAL_SPI_DMAStop( &hspi2); - status = HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); + HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); samples_pointer = samples_at_5_kHz; sum = 0; @@ -208,7 +178,7 @@ static void runnable (void*) continue; // re-synchronize } - // now we have another 256 byte samples @ the middle of our buffer + // now we have another 256 byte samples starting @ the middle of our buffer resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS); // do statistics and advance pointer @@ -222,7 +192,8 @@ static void runnable (void*) // summa summarum ... if( samples_pointer >= samples_at_5_kHz + SAMPLE_BUFSIZE) { - ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / (float)SAMPLE_BUFSIZE; + // this scaling delivers ac_power = 13.0 @ 94dB + ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); dc_content = sum / (float)SAMPLE_BUFSIZE; samples_pointer = samples_at_5_kHz; @@ -248,3 +219,5 @@ static TaskParameters_t p = }; static RestrictedTask mic_task ( p); + +#endif From 9a74729d6476e3b4424b0dd53ff593d89ceba8df Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 21 Jun 2024 17:43:34 +0200 Subject: [PATCH 07/23] Removed superfluous DC output. --- sw_stm32/Drivers/Custom/microphone.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 3abbb9f..80e8663 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -121,7 +121,6 @@ void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) uint64_t getTime_usec(void); COMMON float ac_power; -COMMON float dc_content; static void runnable (void*) { @@ -194,7 +193,6 @@ static void runnable (void*) { // this scaling delivers ac_power = 13.0 @ 94dB ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); - dc_content = sum / (float)SAMPLE_BUFSIZE; samples_pointer = samples_at_5_kHz; sum = 0; From 01f212795a34e5b717676580c8652232436381ad Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Sun, 23 Jun 2024 10:38:44 +0200 Subject: [PATCH 08/23] WIP: Rising sample rate to 10 kHz --- sw_stm32/Communication/uSD_handler.cpp | 21 +++++++++- sw_stm32/Core/Inc/system_configuration.h | 1 + sw_stm32/Drivers/Custom/microphone.cpp | 49 +++++++++++++++--------- sw_stm32/Drivers/Custom/microphone.h | 22 +++++++++++ 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 sw_stm32/Drivers/Custom/microphone.h diff --git a/sw_stm32/Communication/uSD_handler.cpp b/sw_stm32/Communication/uSD_handler.cpp index d9f0a8d..a0652fc 100644 --- a/sw_stm32/Communication/uSD_handler.cpp +++ b/sw_stm32/Communication/uSD_handler.cpp @@ -37,6 +37,7 @@ #include "uSD_handler.h" #include "watchdog_handler.h" #include "magnetic_induction_report.h" +#include "microphone.h" #include "SHA256.h" ROM uint8_t SHA_INITIALIZATION[] = "presently a well-known string"; @@ -631,6 +632,8 @@ void uSD_handler_runnable (void*) write_crash_dump(); } +#if RECORD_AUDIO_SAMPLES +#else // wait until a GNSS timestamp is available. while (output_data.c.sat_fix_type == 0) { @@ -638,6 +641,7 @@ void uSD_handler_runnable (void*) write_crash_dump(); delay (100); } +#endif // generate filename based on timestamp char out_filename[30]; @@ -646,9 +650,13 @@ void uSD_handler_runnable (void*) write_EEPROM_dump( out_filename); // now we have date+time, start logging +#if RECORD_AUDIO_SAMPLES + append_string( out_filename, "audio.byte"); +#else *next++ = '.'; *next++ = 'f'; next = format_2_digits( next, (sizeof(coordinates_t) + sizeof(measurement_data_t)) / sizeof(float)); +#endif fresult = f_open (&the_file, out_filename, FA_CREATE_ALWAYS | FA_WRITE); if (fresult != FR_OK) @@ -665,6 +673,12 @@ void uSD_handler_runnable (void*) while( true) // logger loop synchronized by communicator { +#if RECORD_AUDIO_SAMPLES + uint8_t * audio_data_ptr; + mic_data_pinter_Q.receive( audio_data_ptr); + memcpy (buf_ptr, audio_data_ptr, SAMPLE_BUFSIZE); + buf_ptr += SAMPLE_BUFSIZE; +#else notify_take (true); // wait for synchronization by from communicator OR crash detection if( crashfile) @@ -672,6 +686,7 @@ void uSD_handler_runnable (void*) memcpy (buf_ptr, (uint8_t*) &output_data.m, sizeof(measurement_data_t)+sizeof(coordinates_t)); buf_ptr += sizeof(measurement_data_t)+sizeof(coordinates_t); +#endif if (buf_ptr < mem_buffer + MEM_BUFSIZE) continue; // buffer only filled partially @@ -729,7 +744,11 @@ static TaskParameters_t p = { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { (void *)0x80f8000, 0x8000, portMPU_REGION_READ_WRITE }, - { 0, 0, 0 } +#if RECORD_AUDIO_SAMPLES + { samples_at_5_kHz, sizeof(samples_at_5_kHz), portMPU_REGION_READ_ONLY} +#else + { 0, 0, 0} +#endif } }; diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index 3410a29..ac3ace7 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -47,6 +47,7 @@ #define RUN_FXOS8700 0 #define RUN_PITOT_MODULE 1 #define RUN_MICROPHONE 1 +#define RECORD_AUDIO_SAMPLES 1 #define RUN_CAN_TESTER 0 #define TEST_EEPROM 0 diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 80e8663..104e11b 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -10,10 +10,13 @@ #include "stm32f4xx_hal_cortex.h" #include "stm32f4xx_hal_spi.h" #include "spi.h" +#include "microphone.h" #include "embedded_math.h" #if RUN_MICROPHONE +COMMON Queue mic_data_pinter_Q(1); + ROM int8_t bit_count_table[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, @@ -94,28 +97,26 @@ void configure_SPI_interface (void) __HAL_LINKDMA( &hspi2 , hdmarx, hdma_spi2_rx); } -#define MIC_DMA_BUFSIZE_HALFWORDS 256 -#define SAMPLE_BUFSIZE 512 - uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * sizeof( uint16_t)) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; -int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[SAMPLE_BUFSIZE]; +int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[2][SAMPLE_BUFSIZE]; + // our byte samples have 328kHz sampling-rate // resampling by 64 gives 5.125 kHz sampling-rate -void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount) +void resample( uint8_t * bytes, int8_t * samples, unsigned output_bytecount) { do { int accumulator = 0; - for( unsigned i=0; i<64; ++i) + for( unsigned i=0; i 0); + while( output_bytecount > 0); } uint64_t getTime_usec(void); @@ -126,12 +127,15 @@ static void runnable (void*) { configure_SPI_interface (); register_SPI_usertask( &hspi2); + uint32_t double_buffer_index=0; + + delay(3000); HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); uint32_t BufferIndex; // 0 -> first half, 1 second half - int8_t * samples_pointer = samples_at_5_kHz; + int8_t * samples_pointer = samples_at_5_kHz[double_buffer_index]; int32_t sum = 0; int32_t qsum = 0; @@ -146,17 +150,18 @@ static void runnable (void*) HAL_SPI_DMAStop( &hspi2); HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - samples_pointer = samples_at_5_kHz; + double_buffer_index=0; + samples_pointer = samples_at_5_kHz[0]; sum = 0; qsum = 0; continue; // re-synchronize } // now we have 256 byte samples @ the beginning of our buffer - resample_by_64( (uint8_t *)mic_DMA_buffer, samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS); + resample( (uint8_t *)mic_DMA_buffer, samples_pointer, SAMPLE_BUFSIZE); // do statistics and advance pointer - for( unsigned i=0; i<4; ++i) + for( unsigned i=0; i < SAMPLE_BUFSIZE; ++i) { sum += (int32_t)*samples_pointer; qsum += (int32_t)*samples_pointer * *samples_pointer; @@ -171,17 +176,18 @@ static void runnable (void*) HAL_SPI_DMAStop( &hspi2); HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - samples_pointer = samples_at_5_kHz; + double_buffer_index=0; + samples_pointer = samples_at_5_kHz[0]; sum = 0; qsum = 0; continue; // re-synchronize } // now we have another 256 byte samples starting @ the middle of our buffer - resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS); + resample( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, SAMPLE_BUFSIZE); // do statistics and advance pointer - for( unsigned i=0; i<4; ++i) + for( unsigned i=0; i < SAMPLE_BUFSIZE; ++i) { sum += (int32_t)*samples_pointer; qsum += (int32_t)*samples_pointer * *samples_pointer; @@ -189,12 +195,17 @@ static void runnable (void*) } // summa summarum ... - if( samples_pointer >= samples_at_5_kHz + SAMPLE_BUFSIZE) + if( samples_pointer >= samples_at_5_kHz[double_buffer_index] + SAMPLE_BUFSIZE) { + bool success = mic_data_pinter_Q.send((uint8_t *)&samples_at_5_kHz[double_buffer_index], 1); + assert( success); + + double_buffer_index ^= 1; // switch our twin buffer + // this scaling delivers ac_power = 13.0 @ 94dB ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); - samples_pointer = samples_at_5_kHz; + samples_pointer = samples_at_5_kHz[double_buffer_index]; sum = 0; qsum = 0; } @@ -212,7 +223,7 @@ static TaskParameters_t p = { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, portMPU_REGION_READ_ONLY }, - { samples_at_5_kHz, SAMPLE_BUFSIZE, portMPU_REGION_READ_WRITE } + { samples_at_5_kHz, SAMPLE_BUFSIZE * 2, portMPU_REGION_READ_WRITE } } }; diff --git a/sw_stm32/Drivers/Custom/microphone.h b/sw_stm32/Drivers/Custom/microphone.h new file mode 100644 index 0000000..454a4d8 --- /dev/null +++ b/sw_stm32/Drivers/Custom/microphone.h @@ -0,0 +1,22 @@ +/* + * microphone.h + * + * Created on: Jun 22, 2024 + * Author: schaefer + */ + +#ifndef CUSTOM_MICROPHONE_H_ +#define CUSTOM_MICROPHONE_H_ + +// byte samples come with 2.625 MHz / 8 = 328.125 kHz +// resampling by 32 gives 10.254 kHz +#define RESAMPLING_RATIO 32 +#define NUM_BUFFERS 2 // double buffered for DMA and for uSD writing +#define MIC_DMA_BUFSIZE_HALFWORDS 8192 // half filled after 25ms +#define MIC_DMA_BUFSIZE_BYTES (MIC_DMA_BUFSIZE_HALFWORDS * 2) +#define SAMPLE_BUFSIZE (MIC_DMA_BUFSIZE_BYTES / RESAMPLING_RATIO / NUM_BUFFERS) + +extern Queue mic_data_pinter_Q; +extern int8_t samples_at_5_kHz[2][SAMPLE_BUFSIZE]; + +#endif /* CUSTOM_MICROPHONE_H_ */ From 2df13dc65978c393faac11f1212f6740f3237a05 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 26 Jun 2024 18:12:23 +0200 Subject: [PATCH 09/23] Sample rate changed to 10kHz. Trace config changed. --- sw_stm32/Communication/uSD_handler.cpp | 13 +++--- sw_stm32/Drivers/Custom/microphone.cpp | 51 +++++++++++------------- sw_stm32/Drivers/Custom/microphone.h | 13 +++--- sw_stm32/Tracealyzer/trcSnapshotConfig.h | 6 +-- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/sw_stm32/Communication/uSD_handler.cpp b/sw_stm32/Communication/uSD_handler.cpp index a0652fc..0cb359f 100644 --- a/sw_stm32/Communication/uSD_handler.cpp +++ b/sw_stm32/Communication/uSD_handler.cpp @@ -632,7 +632,10 @@ void uSD_handler_runnable (void*) write_crash_dump(); } + char out_filename[30]; + #if RECORD_AUDIO_SAMPLES + append_string( out_filename, "audio.byte"); #else // wait until a GNSS timestamp is available. while (output_data.c.sat_fix_type == 0) @@ -641,18 +644,12 @@ void uSD_handler_runnable (void*) write_crash_dump(); delay (100); } -#endif - // generate filename based on timestamp - char out_filename[30]; char * next = append_string( out_filename, "logger/"); next = format_date_time( next); write_EEPROM_dump( out_filename); // now we have date+time, start logging -#if RECORD_AUDIO_SAMPLES - append_string( out_filename, "audio.byte"); -#else *next++ = '.'; *next++ = 'f'; next = format_2_digits( next, (sizeof(coordinates_t) + sizeof(measurement_data_t)) / sizeof(float)); @@ -675,7 +672,7 @@ void uSD_handler_runnable (void*) { #if RECORD_AUDIO_SAMPLES uint8_t * audio_data_ptr; - mic_data_pinter_Q.receive( audio_data_ptr); + mic_data_pointer_Q.receive( audio_data_ptr); memcpy (buf_ptr, audio_data_ptr, SAMPLE_BUFSIZE); buf_ptr += SAMPLE_BUFSIZE; #else @@ -745,7 +742,7 @@ static TaskParameters_t p = { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { (void *)0x80f8000, 0x8000, portMPU_REGION_READ_WRITE }, #if RECORD_AUDIO_SAMPLES - { samples_at_5_kHz, sizeof(samples_at_5_kHz), portMPU_REGION_READ_ONLY} + { audio_samples, sizeof(audio_samples), portMPU_REGION_READ_ONLY} #else { 0, 0, 0} #endif diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 104e11b..29d1111 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -15,7 +15,7 @@ #if RUN_MICROPHONE -COMMON Queue mic_data_pinter_Q(1); +COMMON Queue mic_data_pointer_Q(1); ROM int8_t bit_count_table[] = { @@ -98,8 +98,7 @@ void configure_SPI_interface (void) } uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * sizeof( uint16_t)) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS]; -int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[2][SAMPLE_BUFSIZE]; - +int8_t __ALIGNED( SAMPLE_BUFSIZE) audio_samples[2][SAMPLE_BUFSIZE]; // our byte samples have 328kHz sampling-rate // resampling by 64 gives 5.125 kHz sampling-rate @@ -114,7 +113,7 @@ void resample( uint8_t * bytes, int8_t * samples, unsigned output_bytecount) *samples++ = (int8_t)(accumulator - RESAMPLING_RATIO * 4); - output_bytecount -= RESAMPLING_RATIO; + --output_bytecount; } while( output_bytecount > 0); } @@ -133,35 +132,34 @@ static void runnable (void*) HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - uint32_t BufferIndex; // 0 -> first half, 1 second half + uint32_t BufferIndex=0; // 0 -> first half, 1 second half - int8_t * samples_pointer = samples_at_5_kHz[double_buffer_index]; + int8_t * samples_pointer = audio_samples[double_buffer_index]; int32_t sum = 0; int32_t qsum = 0; while (true) { BufferIndex = 0xffffffff; - uint64_t timestamp = getTime_usec(); - xTaskNotifyWait( 0, 0, &BufferIndex, 2); + xTaskNotifyWait( 0, 0, &BufferIndex, 40); // trigger on half complete interrupt if( BufferIndex != 0) // be sure to be behind the half transfer interrupt { HAL_SPI_DMAStop( &hspi2); HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - double_buffer_index=0; - samples_pointer = samples_at_5_kHz[0]; + double_buffer_index = 0; + samples_pointer = audio_samples[double_buffer_index]; sum = 0; qsum = 0; continue; // re-synchronize } - // now we have 256 byte samples @ the beginning of our buffer - resample( (uint8_t *)mic_DMA_buffer, samples_pointer, SAMPLE_BUFSIZE); + // now we have 128 byte samples @ the beginning of our buffer + resample( (uint8_t *)mic_DMA_buffer, samples_pointer, SAMPLE_BUFSIZE_HALF); // do statistics and advance pointer - for( unsigned i=0; i < SAMPLE_BUFSIZE; ++i) + for( unsigned i=0; i < SAMPLE_BUFSIZE_HALF; ++i) { sum += (int32_t)*samples_pointer; qsum += (int32_t)*samples_pointer * *samples_pointer; @@ -169,25 +167,25 @@ static void runnable (void*) } BufferIndex = 0xffffffff; - xTaskNotifyWait( 0, 0, &BufferIndex, 2); + xTaskNotifyWait( 0, 0, &BufferIndex, 40); if( BufferIndex != 1) // be sure to be behind the transfer complete interrupt { HAL_SPI_DMAStop( &hspi2); HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS); - double_buffer_index=0; - samples_pointer = samples_at_5_kHz[0]; + double_buffer_index = 0; + samples_pointer = audio_samples[double_buffer_index]; sum = 0; qsum = 0; continue; // re-synchronize } - // now we have another 256 byte samples starting @ the middle of our buffer - resample( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, SAMPLE_BUFSIZE); + // now we have 128 byte samples @ the middle of our buffer + resample( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, SAMPLE_BUFSIZE_HALF); // do statistics and advance pointer - for( unsigned i=0; i < SAMPLE_BUFSIZE; ++i) + for( unsigned i=0; i < SAMPLE_BUFSIZE_HALF; ++i) { sum += (int32_t)*samples_pointer; qsum += (int32_t)*samples_pointer * *samples_pointer; @@ -195,19 +193,18 @@ static void runnable (void*) } // summa summarum ... - if( samples_pointer >= samples_at_5_kHz[double_buffer_index] + SAMPLE_BUFSIZE) + if( samples_pointer >= audio_samples[double_buffer_index] + SAMPLE_BUFSIZE) { - bool success = mic_data_pinter_Q.send((uint8_t *)&samples_at_5_kHz[double_buffer_index], 1); - assert( success); - - double_buffer_index ^= 1; // switch our twin buffer + bool success = mic_data_pointer_Q.send((uint8_t *)&audio_samples[double_buffer_index], 1); + ASSERT( success); // this scaling delivers ac_power = 13.0 @ 94dB ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); - - samples_pointer = samples_at_5_kHz[double_buffer_index]; sum = 0; qsum = 0; + + double_buffer_index ^= 1; // switch our twin buffer + samples_pointer = audio_samples[double_buffer_index]; } } } @@ -223,7 +220,7 @@ static TaskParameters_t p = { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, portMPU_REGION_READ_ONLY }, - { samples_at_5_kHz, SAMPLE_BUFSIZE * 2, portMPU_REGION_READ_WRITE } + { audio_samples, sizeof(audio_samples), portMPU_REGION_READ_WRITE} } }; diff --git a/sw_stm32/Drivers/Custom/microphone.h b/sw_stm32/Drivers/Custom/microphone.h index 454a4d8..89246d2 100644 --- a/sw_stm32/Drivers/Custom/microphone.h +++ b/sw_stm32/Drivers/Custom/microphone.h @@ -12,11 +12,14 @@ // resampling by 32 gives 10.254 kHz #define RESAMPLING_RATIO 32 #define NUM_BUFFERS 2 // double buffered for DMA and for uSD writing -#define MIC_DMA_BUFSIZE_HALFWORDS 8192 // half filled after 25ms -#define MIC_DMA_BUFSIZE_BYTES (MIC_DMA_BUFSIZE_HALFWORDS * 2) -#define SAMPLE_BUFSIZE (MIC_DMA_BUFSIZE_BYTES / RESAMPLING_RATIO / NUM_BUFFERS) +#define MIC_DMA_BUFSIZE_HALFWORDS 4096 // filled after 50ms +#define MIC_DMA_BUFSIZE_BYTES (MIC_DMA_BUFSIZE_HALFWORDS * sizeof(uint16_t)) +// the sample buffer contains 512 bytes sampled at 10.254 kHz +// acquisition time 50ms -> 20 Hz min freq. +#define SAMPLE_BUFSIZE (MIC_DMA_BUFSIZE_BYTES / RESAMPLING_RATIO) +#define SAMPLE_BUFSIZE_HALF (SAMPLE_BUFSIZE / 2) -extern Queue mic_data_pinter_Q; -extern int8_t samples_at_5_kHz[2][SAMPLE_BUFSIZE]; +extern Queue mic_data_pointer_Q; +extern int8_t audio_samples[2][SAMPLE_BUFSIZE]; #endif /* CUSTOM_MICROPHONE_H_ */ diff --git a/sw_stm32/Tracealyzer/trcSnapshotConfig.h b/sw_stm32/Tracealyzer/trcSnapshotConfig.h index c3c66cf..ab897f4 100644 --- a/sw_stm32/Tracealyzer/trcSnapshotConfig.h +++ b/sw_stm32/Tracealyzer/trcSnapshotConfig.h @@ -106,9 +106,9 @@ * check the actual usage by selecting View menu -> Trace Details -> * Resource Usage -> Object Table. ******************************************************************************/ -#define TRC_CFG_NTASK 15 -#define TRC_CFG_NISR 5 -#define TRC_CFG_NQUEUE 10 +#define TRC_CFG_NTASK 20 +#define TRC_CFG_NISR 20 +#define TRC_CFG_NQUEUE 20 #define TRC_CFG_NSEMAPHORE 10 #define TRC_CFG_NMUTEX 10 #define TRC_CFG_NTIMER 5 From 5f7876907df81f35ef4a2bc14b540ef489a4039d Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 3 Jul 2024 10:10:56 +0200 Subject: [PATCH 10/23] test --- sw_stm32/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_stm32/lib b/sw_stm32/lib index ead8697..8388bc8 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit ead86972bb89caafba78e75f8886d2070e96ef4d +Subproject commit 8388bc85fe4a0a2e683dfd379719c5c2dc6aa6b3 From 6a04271141247a7fead40c6f031c33e7b0a6f222 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 3 Jul 2024 11:01:33 +0200 Subject: [PATCH 11/23] Preparing test release. --- sw_stm32/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_stm32/lib b/sw_stm32/lib index 8388bc8..ec27bd2 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit 8388bc85fe4a0a2e683dfd379719c5c2dc6aa6b3 +Subproject commit ec27bd243675500c4b721a8622cc82a3e504f592 From 2955e5fe1849e5e67a7c50e1ecce28040166d2eb Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Thu, 4 Jul 2024 11:46:17 +0200 Subject: [PATCH 12/23] Fast NMEA position reporting for test purposes. --- sw_stm32/Communication/NMEA_Output.cpp | 5 ++++- sw_stm32/Core/Inc/system_configuration.h | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sw_stm32/Communication/NMEA_Output.cpp b/sw_stm32/Communication/NMEA_Output.cpp index d3537ca..0f8a33c 100644 --- a/sw_stm32/Communication/NMEA_Output.cpp +++ b/sw_stm32/Communication/NMEA_Output.cpp @@ -93,12 +93,15 @@ static void runnable (void* data) { NMEA_buf.length = 0; // start at the beginning of the buffer format_NMEA_string_fast( output_data, NMEA_buf, horizon_available); +#if FAST_NMEA_POSITION_OUTPUT + format_NMEA_string_slow( output_data, NMEA_buf); +#else if( --decimating_counter == 0) { decimating_counter = NMEA_DECIMATION_RATIO; format_NMEA_string_slow( output_data, NMEA_buf); } - +#endif //Check if there is a CAN Message received which needs to be replayed via a Larus NMEA PLARS Sentence. float32_t value; char *next = NMEA_buf.string + NMEA_buf.length; diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index 21419a1..47e18ac 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -92,6 +92,7 @@ #define NMEA_REPORTING_PERIOD 250 // period in clock ticks for NMEA output #define NMEA_DECIMATION_RATIO 6 // slow-down factor for the slow properties +#define FAST_NMEA_POSITION_OUTPUT 1 #define ACTIVATE_FPU_EXCEPTION_TRAP 1 // I want to be SET ! #define SET_FPU_FLUSH_TO_ZERO 1 From 93bd9d13b5a8bedda823fd3ce4439b21ad42be0d Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Tue, 9 Jul 2024 13:57:14 +0200 Subject: [PATCH 13/23] Added speed-accuracy in sensor dump output. --- sw_stm32/Communication/sensor_dump.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sw_stm32/Communication/sensor_dump.cpp b/sw_stm32/Communication/sensor_dump.cpp index d96bfa3..2d7e0fd 100644 --- a/sw_stm32/Communication/sensor_dump.cpp +++ b/sw_stm32/Communication/sensor_dump.cpp @@ -142,6 +142,9 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s=append_string( s, "Sats: "); s = format_2_digits( s, (float32_t)(output_data.c.SATS_number)); + s=append_string( s, "Speed-Accuracy: "); + s = to_ascii_2_decimals( 100.0f * (float32_t)(output_data.c.speed_acc), s); + s=append_string( s, " GNSS time: "); s = format_2_digits( s, output_data.c.hour); *s ++ = ':'; From c44b273a76f307e153b9b831d6325fd217ac51d0 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Tue, 9 Jul 2024 14:10:16 +0200 Subject: [PATCH 14/23] Tiny beautifications in sonsor output. --- sw_stm32/Communication/sensor_dump.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sw_stm32/Communication/sensor_dump.cpp b/sw_stm32/Communication/sensor_dump.cpp index 2d7e0fd..5ad7aa7 100644 --- a/sw_stm32/Communication/sensor_dump.cpp +++ b/sw_stm32/Communication/sensor_dump.cpp @@ -142,7 +142,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s=append_string( s, "Sats: "); s = format_2_digits( s, (float32_t)(output_data.c.SATS_number)); - s=append_string( s, "Speed-Accuracy: "); + s=append_string( s, " Speed-Accuracy: "); s = to_ascii_2_decimals( 100.0f * (float32_t)(output_data.c.speed_acc), s); s=append_string( s, " GNSS time: "); @@ -160,7 +160,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA *s++=' '; } - s=append_string( s, " Strength: "); + s=append_string( s, " Strength = "); s = to_ascii_2_decimals( 100.0f * output_data.nav_induction_gnss.abs(), s); newline( s); @@ -173,8 +173,9 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s=append_string( s, " Inclination = "); s = to_ascii_2_decimals( RAD_2_DEGREES_10 * inclination_decimator.get_output(), s); - s=append_string( s, " MagAnomaly / % = "); + s=append_string( s, " MagAnomaly = "); s = to_ascii_2_decimals( output_data.magnetic_disturbance * 10000.0f, s); + *s++ = '%'; newline( s); if( output_data.c.sat_fix_type == (SAT_FIX | SAT_HEADING)) From c47a75b4202be5ef5a755bbd91f5458c4cd17247 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Tue, 9 Jul 2024 14:28:19 +0200 Subject: [PATCH 15/23] Tiny mod. --- sw_stm32/Communication/sensor_dump.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sw_stm32/Communication/sensor_dump.cpp b/sw_stm32/Communication/sensor_dump.cpp index 5ad7aa7..287f952 100644 --- a/sw_stm32/Communication/sensor_dump.cpp +++ b/sw_stm32/Communication/sensor_dump.cpp @@ -142,10 +142,10 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s=append_string( s, "Sats: "); s = format_2_digits( s, (float32_t)(output_data.c.SATS_number)); - s=append_string( s, " Speed-Accuracy: "); + s=append_string( s, " Speed-Accuracy = "); s = to_ascii_2_decimals( 100.0f * (float32_t)(output_data.c.speed_acc), s); - s=append_string( s, " GNSS time: "); + s=append_string( s, "m/s, GNSS time: "); s = format_2_digits( s, output_data.c.hour); *s ++ = ':'; s = format_2_digits( s, output_data.c.minute); @@ -215,7 +215,6 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA format_PLARV ( vario, 0.0f, 0.0f, 0.0f, s); } newline( s); - *s = 0; NMEA_buf.length = s - NMEA_buf.string; } From 7e9893ad07369631153d9d6534006a76dd5144d4 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Tue, 9 Jul 2024 14:57:29 +0200 Subject: [PATCH 16/23] Bugfix: Inclination output corrected. --- sw_stm32/Communication/sensor_dump.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sw_stm32/Communication/sensor_dump.cpp b/sw_stm32/Communication/sensor_dump.cpp index 287f952..469c078 100644 --- a/sw_stm32/Communication/sensor_dump.cpp +++ b/sw_stm32/Communication/sensor_dump.cpp @@ -171,7 +171,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s = to_ascii_1_decimal( RAD_2_DEGREES_10 * heading, s); s=append_string( s, " Inclination = "); - s = to_ascii_2_decimals( RAD_2_DEGREES_10 * inclination_decimator.get_output(), s); + s = to_ascii_1_decimal( RAD_2_DEGREES_10 * inclination_decimator.get_output(), s); s=append_string( s, " MagAnomaly = "); s = to_ascii_2_decimals( output_data.magnetic_disturbance * 10000.0f, s); @@ -189,7 +189,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA s = to_ascii_1_decimal( RAD_2_DEGREES_10 * output_data.c.relPosHeading, s); } else - s=append_string( s, "No D-GNSS"); + s=append_string( s, "No D-GNSS-fix"); newline( s); From 0f684aea8a50b05737882b968d105d74ce3a8fc0 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 09:56:46 +0200 Subject: [PATCH 17/23] Bugfix: Possible overflow of NMEA string buffer fixed. --- sw_stm32/Communication/NMEA_Output.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sw_stm32/Communication/NMEA_Output.cpp b/sw_stm32/Communication/NMEA_Output.cpp index 0f8a33c..9971acd 100644 --- a/sw_stm32/Communication/NMEA_Output.cpp +++ b/sw_stm32/Communication/NMEA_Output.cpp @@ -37,10 +37,10 @@ #include "sensor_dump.h" #include "uSD_handler.h" -COMMON string_buffer_t NMEA_buf; +COMMON string_buffer_t __ALIGNED( sizeof(string_buffer_t)) NMEA_buf; extern USBD_HandleTypeDef hUsbDeviceFS; // from usb_device.c -static void runnable (void* data) +static void NMEA_runnable (void* data) { delay( NMEA_START_DELAY); @@ -141,5 +141,19 @@ static void runnable (void* data) } } -COMMON RestrictedTask NMEA_task( runnable, "NMEA", 256, 0, (NMEA_USB_PRIORITY) | portPRIVILEGE_BIT); +static TaskParameters_t p = + { + NMEA_runnable, + "NMEA", + 256, 0, + NMEA_USB_PRIORITY | portPRIVILEGE_BIT, + 0, + { + { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, + { (void *)&NMEA_buf, sizeof(NMEA_buf), portMPU_REGION_READ_WRITE }, + { 0, 0, 0 } + } + }; + +COMMON RestrictedTask NMEA_task( p); From d8c79ac620c7a58b4a69278b00b3663f0045791b Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 10:05:11 +0200 Subject: [PATCH 18/23] Lib update --- sw_stm32/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_stm32/lib b/sw_stm32/lib index ec27bd2..5a27e17 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit ec27bd243675500c4b721a8622cc82a3e504f592 +Subproject commit 5a27e177459b93ee8fcd1e8dff9dc6931edc9254 From ebc4fef5a177c50ba6509e7f73b0eb6a8951cb62 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 10:33:30 +0200 Subject: [PATCH 19/23] Added git commit signature in boot block @ 0x08000000. --- sw_stm32/.cproject | 3 +++ sw_stm32/Core/Startup/startup_stm32f407vgtx.s | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/sw_stm32/.cproject b/sw_stm32/.cproject index 2e04eef..1dfd8d2 100644 --- a/sw_stm32/.cproject +++ b/sw_stm32/.cproject @@ -354,6 +354,9 @@ diff --git a/sw_stm32/Core/Startup/startup_stm32f407vgtx.s b/sw_stm32/Core/Startup/startup_stm32f407vgtx.s index a4da6e6..859971f 100644 --- a/sw_stm32/Core/Startup/startup_stm32f407vgtx.s +++ b/sw_stm32/Core/Startup/startup_stm32f407vgtx.s @@ -24,6 +24,7 @@ * ****************************************************************************** */ +#include "git-commit-version.h" .syntax unified .cpu cortex-m4 @@ -229,7 +230,8 @@ g_pfnVectors: .word 0 /* CRYP crypto */ .word HASH_RNG_IRQHandler /* Hash and Rng */ .word FPU_IRQHandler /* FPU */ - + + .ascii GIT_COMMIT_HASH /******************************************************************************* * From 15710ee0120bde2f2877d010df554c22532c48e2 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 10:58:37 +0200 Subject: [PATCH 20/23] NMEA reporting for position slowed down -> 1.5s --- sw_stm32/Core/Inc/system_configuration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index 47e18ac..6fca3b5 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -92,7 +92,7 @@ #define NMEA_REPORTING_PERIOD 250 // period in clock ticks for NMEA output #define NMEA_DECIMATION_RATIO 6 // slow-down factor for the slow properties -#define FAST_NMEA_POSITION_OUTPUT 1 +#define FAST_NMEA_POSITION_OUTPUT 0 #define ACTIVATE_FPU_EXCEPTION_TRAP 1 // I want to be SET ! #define SET_FPU_FLUSH_TO_ZERO 1 From 7b13d98babe71ffba13e7a2cc48a6fb226a22c90 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 13:08:38 +0200 Subject: [PATCH 21/23] Sound recording test: Successful. --- sw_stm32/Core/Inc/main.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_stm32/Core/Inc/main.h b/sw_stm32/Core/Inc/main.h index 8fa39ae..857be2f 100644 --- a/sw_stm32/Core/Inc/main.h +++ b/sw_stm32/Core/Inc/main.h @@ -73,7 +73,7 @@ void heartbeat(void); #define L3GD20_INT1_GPIO_Port GPIOB #define L3GD20_INT2_Pin GPIO_PIN_1 #define L3GD20_INT2_GPIO_Port GPIOB -#define SPI2_NSS_Pin GPIO_PIN_12 +#define SPI2_NSS_Pin GPIO_PIN_4 #define SPI2_NSS_GPIO_Port GPIOB #define MTi_1IMU_PSEL0_Pin GPIO_PIN_10 #define MTi_1IMU_PSEL0_GPIO_Port GPIOD From e796ddf2b4476c9880c0ca05653595ea4ce11aec Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Wed, 10 Jul 2024 14:24:30 +0200 Subject: [PATCH 22/23] Added sound intensity recording. Merge branch 'microphone' of git@github.com:larus-breeze/sw_sensor.git into microphone --- sw_stm32/Communication/uSD_handler.cpp | 22 +++------------------- sw_stm32/Core/Inc/system_configuration.h | 1 - sw_stm32/Drivers/Custom/microphone.cpp | 10 ++-------- sw_stm32/lib | 2 +- 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/sw_stm32/Communication/uSD_handler.cpp b/sw_stm32/Communication/uSD_handler.cpp index 0cb359f..f48d401 100644 --- a/sw_stm32/Communication/uSD_handler.cpp +++ b/sw_stm32/Communication/uSD_handler.cpp @@ -37,7 +37,6 @@ #include "uSD_handler.h" #include "watchdog_handler.h" #include "magnetic_induction_report.h" -#include "microphone.h" #include "SHA256.h" ROM uint8_t SHA_INITIALIZATION[] = "presently a well-known string"; @@ -634,9 +633,6 @@ void uSD_handler_runnable (void*) char out_filename[30]; -#if RECORD_AUDIO_SAMPLES - append_string( out_filename, "audio.byte"); -#else // wait until a GNSS timestamp is available. while (output_data.c.sat_fix_type == 0) { @@ -652,8 +648,7 @@ void uSD_handler_runnable (void*) *next++ = '.'; *next++ = 'f'; - next = format_2_digits( next, (sizeof(coordinates_t) + sizeof(measurement_data_t)) / sizeof(float)); -#endif + next = format_2_digits( next, sizeof(observations_type) / sizeof(float)); fresult = f_open (&the_file, out_filename, FA_CREATE_ALWAYS | FA_WRITE); if (fresult != FR_OK) @@ -670,20 +665,13 @@ void uSD_handler_runnable (void*) while( true) // logger loop synchronized by communicator { -#if RECORD_AUDIO_SAMPLES - uint8_t * audio_data_ptr; - mic_data_pointer_Q.receive( audio_data_ptr); - memcpy (buf_ptr, audio_data_ptr, SAMPLE_BUFSIZE); - buf_ptr += SAMPLE_BUFSIZE; -#else notify_take (true); // wait for synchronization by from communicator OR crash detection if( crashfile) write_crash_dump(); - memcpy (buf_ptr, (uint8_t*) &output_data.m, sizeof(measurement_data_t)+sizeof(coordinates_t)); - buf_ptr += sizeof(measurement_data_t)+sizeof(coordinates_t); -#endif + memcpy (buf_ptr, (uint8_t*) &output_data.m, sizeof(observations_type)); + buf_ptr += sizeof(observations_type); if (buf_ptr < mem_buffer + MEM_BUFSIZE) continue; // buffer only filled partially @@ -741,11 +729,7 @@ static TaskParameters_t p = { { COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE }, { (void *)0x80f8000, 0x8000, portMPU_REGION_READ_WRITE }, -#if RECORD_AUDIO_SAMPLES - { audio_samples, sizeof(audio_samples), portMPU_REGION_READ_ONLY} -#else { 0, 0, 0} -#endif } }; diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index c2e79b5..467d55b 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -48,7 +48,6 @@ #define RUN_PITOT_MODULE 1 #define RUN_MICROPHONE 1 -#define RECORD_AUDIO_SAMPLES 1 #define RUN_CAN_TESTER 0 #define TEST_EEPROM 0 diff --git a/sw_stm32/Drivers/Custom/microphone.cpp b/sw_stm32/Drivers/Custom/microphone.cpp index 29d1111..81dec75 100644 --- a/sw_stm32/Drivers/Custom/microphone.cpp +++ b/sw_stm32/Drivers/Custom/microphone.cpp @@ -10,13 +10,12 @@ #include "stm32f4xx_hal_cortex.h" #include "stm32f4xx_hal_spi.h" #include "spi.h" +#include "communicator.h" #include "microphone.h" #include "embedded_math.h" #if RUN_MICROPHONE -COMMON Queue mic_data_pointer_Q(1); - ROM int8_t bit_count_table[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, @@ -120,8 +119,6 @@ void resample( uint8_t * bytes, int8_t * samples, unsigned output_bytecount) uint64_t getTime_usec(void); -COMMON float ac_power; - static void runnable (void*) { configure_SPI_interface (); @@ -195,11 +192,8 @@ static void runnable (void*) // summa summarum ... if( samples_pointer >= audio_samples[double_buffer_index] + SAMPLE_BUFSIZE) { - bool success = mic_data_pointer_Q.send((uint8_t *)&audio_samples[double_buffer_index], 1); - ASSERT( success); - // this scaling delivers ac_power = 13.0 @ 94dB - ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); + output_data.sound_intensity = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE); sum = 0; qsum = 0; diff --git a/sw_stm32/lib b/sw_stm32/lib index 5a27e17..92e670d 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit 5a27e177459b93ee8fcd1e8dff9dc6931edc9254 +Subproject commit 92e670dca9c6c5280ac917e2dc717e5155077d7a From def44231ba85b175717d8226fada9f8ca4f3bcce Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 12 Jul 2024 12:25:06 +0200 Subject: [PATCH 23/23] +Compile time switch for microphone support. --- sw_stm32/Core/Inc/system_configuration.h | 2 +- sw_stm32/lib | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sw_stm32/Core/Inc/system_configuration.h b/sw_stm32/Core/Inc/system_configuration.h index 467d55b..1248d5a 100644 --- a/sw_stm32/Core/Inc/system_configuration.h +++ b/sw_stm32/Core/Inc/system_configuration.h @@ -47,7 +47,7 @@ #define RUN_FXOS8700 0 #define RUN_PITOT_MODULE 1 -#define RUN_MICROPHONE 1 +#define RUN_MICROPHONE 0 #define RUN_CAN_TESTER 0 #define TEST_EEPROM 0 diff --git a/sw_stm32/lib b/sw_stm32/lib index 92e670d..65a7d56 160000 --- a/sw_stm32/lib +++ b/sw_stm32/lib @@ -1 +1 @@ -Subproject commit 92e670dca9c6c5280ac917e2dc717e5155077d7a +Subproject commit 65a7d56e175b265f8f5d27a3ad5dfb7a82fb850b