From d09f4289b5788d6a8b34e424841292e2b8529e56 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 1 Feb 2025 13:31:20 -0500 Subject: exec from ram; process all samples --- Core/Src/main.c | 78 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 17 deletions(-) (limited to 'Core/Src') diff --git a/Core/Src/main.c b/Core/Src/main.c index 45d16a6..ed49703 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -32,7 +32,7 @@ typedef uint32_t sample_t; /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ -#define SAMPLE_COUNT (256) // (Stereo!) Sample count per half-transfer +#define SAMPLE_COUNT (1024) // (Stereo!) Sample count per half-transfer #define SAMPLES_PER_REPORT (48000) // Report every second given our 48 kHz rate #define MIC_OFFSET_DB ( 0.f) // Linear offset #define MIC_SENSITIVITY (-26.f) // dBFS value expected at MIC_REF_DB @@ -79,7 +79,9 @@ static HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA_Mixed( uint8_t *pRxData, uint16_t TxSize, uint16_t RxSize); +__RAM_FUNC static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma); +__RAM_FUNC static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma); /* USER CODE END PFP */ @@ -91,6 +93,14 @@ int __io_putchar(int ch) HAL_UART_Transmit(&huart2, &buf, sizeof(buf), HAL_TIMEOUT); return buf; } + +__RAM_FUNC +void fvar_init(void) +{ + ln10 = qfp_fln(10.f); + MIC_REF_AMPL = qfp_fmul(qfp_int2float((1u << (MIC_BITS - 2)) - 1), + qfp_fpow(10.f, MIC_SENSITIVITY / 20.f)); +} /* USER CODE END 0 */ /** @@ -101,9 +111,7 @@ int main(void) { /* USER CODE BEGIN 1 */ - ln10 = qfp_fln(10.f); - MIC_REF_AMPL = qfp_fmul(qfp_int2float((1u << (MIC_BITS - 1)) - 1), - qfp_fpow(10.f, MIC_SENSITIVITY / 20.f)); + fvar_init(); /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ @@ -160,7 +168,7 @@ void SystemClock_Config(void) /** Configure the main internal regulator output voltage */ - HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2); + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. @@ -180,10 +188,10 @@ void SystemClock_Config(void) RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } @@ -323,7 +331,7 @@ static void MX_GPIO_Init(void) HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(IDLE_GPIO_Port, IDLE_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(IDLE_GPIO_Port, IDLE_Pin, GPIO_PIN_SET); /*Configure GPIO pin : LED_GREEN_Pin */ GPIO_InitStruct.Pin = LED_GREEN_Pin; @@ -443,6 +451,43 @@ error : #define BITO (10) +__RAM_FUNC +__attribute__((naked)) +int64_t lmul(int64_t a, int64_t b) +{ + asm( +" push {r4, lr}\n" +" mul r1, r2\n" +" mul r3, r0\n" +" add r1, r3\n" + +" lsr r3, r0, #16\n" +" lsr r4, r2, #16\n" +" mul r3, r4\n" +" add r1, r3\n" + +" lsr r3, r0, #16\n" +" uxth r0, r0\n" +" uxth r2, r2\n" +" mul r3, r2\n" +" mul r4, r0\n" +" mul r0, r2\n" + +" mov r2, #0\n" +" add r3, r4\n" +" adc r2, r2\n" +" lsl r2, #16\n" +" add r1, r2\n" + +" lsl r2, r3, #16\n" +" lsr r3, #16\n" +" add r0, r2\n" +" adc r1, r3\n" +" pop {r4, pc}\n" + ); +} + +__RAM_FUNC static inline void process(int64_t in_div4) { static int64_t z[4] = {0, 0, 0, 0}; @@ -458,25 +503,24 @@ static inline void process(int64_t in_div4) int64_t out3 = (out2 + z[3]); z[3] = ((out3 * 0x3f1 /*0.985f*/) >> BITO) - out2; - DB_Sum_Squares += (out3 * out3) >> BITO; + DB_Sum_Squares += lmul(out3, out3) >> BITO; } +__RAM_FUNC static void processSampleBlock(sample_t *sample) { - HAL_GPIO_WritePin(IDLE_GPIO_Port, IDLE_Pin, GPIO_PIN_RESET); - -#define STRIDE 2 + IDLE_GPIO_Port->ODR ^= IDLE_Pin; - for (int i = 0; i < SAMPLE_COUNT; i += 2 * STRIDE) { + for (int i = 0; i < SAMPLE_COUNT; i += 2) { // 18-bit sample comes in as big-endian with right padding. // Use REVSH to extract 18-bit reading divided by four for process(). int samp; asm("revsh %0, %1" : "=l" (samp) : "l" (sample[i])); process(samp); } - DB_Count += SAMPLE_COUNT / (2 * STRIDE); + DB_Count += SAMPLE_COUNT / 2; - if (DB_Count >= SAMPLES_PER_REPORT / STRIDE) { + if (DB_Count >= SAMPLES_PER_REPORT) { float rms = qfp_fsqrt(qfp_int2float((DB_Sum_Squares >> BITO) / DB_Count)); float db = qfp_fadd(qfp_fmul(qfp_flog10(qfp_fdiv(rms, MIC_REF_AMPL)), 20.f), MIC_OFFSET_DB + MIC_REF_DB); @@ -486,7 +530,7 @@ static void processSampleBlock(sample_t *sample) printf("%d dB\r\n", qfp_float2int(db)); } - HAL_GPIO_WritePin(IDLE_GPIO_Port, IDLE_Pin, GPIO_PIN_SET); + IDLE_GPIO_Port->ODR ^= IDLE_Pin; } void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma) -- cgit v1.2.3