summaryrefslogtreecommitdiffstats
path: root/Core/Src/main.c
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2025-02-02 11:26:53 -0500
committerClyne Sullivan <clyne@bitgloo.com>2025-02-02 11:26:53 -0500
commit9c59a184dba820975e5da6fcd5d248aee87f7e2f (patch)
tree6b30516adc2ba0f7b0a8f5fb5d2e6966c03108d8 /Core/Src/main.c
parentd09f4289b5788d6a8b34e424841292e2b8529e56 (diff)
add l476 implementationl476
Diffstat (limited to 'Core/Src/main.c')
-rw-r--r--Core/Src/main.c205
1 files changed, 81 insertions, 124 deletions
diff --git a/Core/Src/main.c b/Core/Src/main.c
index ed49703..3aec32e 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -22,7 +22,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
-#include <qfplib-port.h>
+#include <math.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -46,7 +46,6 @@ typedef uint32_t sample_t;
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
-
SPI_HandleTypeDef hspi1;
DMA_HandleTypeDef hdma_spi1_rx;
DMA_HandleTypeDef hdma_spi1_tx;
@@ -62,7 +61,7 @@ static uint8_t I2S_Receive_Buffer[SAMPLE_COUNT * 2 * sizeof(sample_t)];
float ln10;
float MIC_REF_AMPL;
-static int64_t DB_Sum_Squares = 0.f;
+static float DB_Sum_Squares = 0.f;
static int DB_Count = 0;
/* USER CODE END PV */
@@ -97,9 +96,9 @@ int __io_putchar(int ch)
__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));
+ ln10 = logf(10.f);
+ MIC_REF_AMPL = (float)((1u << (MIC_BITS - 2)) - 1) *
+ powf(10.f, MIC_SENSITIVITY / 20.f);
}
/* USER CODE END 0 */
@@ -168,16 +167,25 @@ void SystemClock_Config(void)
/** Configure the main internal regulator output voltage
*/
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
+ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK)
+ {
+ Error_Handler();
+ }
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
- RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_9;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
+ RCC_OscInitStruct.MSICalibrationValue = 0;
+ RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
+ RCC_OscInitStruct.PLL.PLLM = 1;
+ RCC_OscInitStruct.PLL.PLLN = 12;
+ RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
+ RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
+ RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
@@ -186,10 +194,11 @@ void SystemClock_Config(void)
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
+ |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
@@ -259,26 +268,13 @@ static void MX_USART2_UART_Init(void)
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_8;
+ huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
- if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
@@ -295,12 +291,12 @@ static void MX_DMA_Init(void)
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- /* DMA1_Channel2_3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
+ /* DMA1_Channel2_IRQn interrupt configuration */
+ HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
+ HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
+ /* DMA1_Channel3_IRQn interrupt configuration */
+ HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
+ HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
}
@@ -317,55 +313,56 @@ static void MX_GPIO_Init(void)
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
+ __HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pin Output Level */
- 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_SET);
-
- /*Configure GPIO pin : LED_GREEN_Pin */
- GPIO_InitStruct.Pin = LED_GREEN_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ /*Configure GPIO pins : PC13 PC14 PC15 PC0
+ PC1 PC2 PC3 PC4
+ PC5 PC6 PC7 PC8
+ PC9 PC10 PC11 PC12 */
+ GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0
+ |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
+ |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
+ |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
+ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_GREEN_GPIO_Port, &GPIO_InitStruct);
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pin : Joystick_Pin */
- GPIO_InitStruct.Pin = Joystick_Pin;
+ /*Configure GPIO pins : PH0 PH1 */
+ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(Joystick_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pin : LED_BLUE_Pin */
- GPIO_InitStruct.Pin = LED_BLUE_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
+
+ /*Configure GPIO pins : PA0 PA1 PA4 PA5
+ PA6 PA7 PA8 PA9
+ PA10 PA11 PA12 PA15 */
+ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
+ |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9
+ |GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_15;
+ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pin : LED_RED_Pin */
- GPIO_InitStruct.Pin = LED_RED_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+ /*Configure GPIO pins : PB0 PB1 PB2 PB10
+ PB11 PB12 PB13 PB14
+ PB15 PB6 PB7 PB8
+ PB9 */
+ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
+ |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
+ |GPIO_PIN_15|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
+ |GPIO_PIN_9;
+ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_RED_GPIO_Port, &GPIO_InitStruct);
+ HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pin : IDLE_Pin */
- GPIO_InitStruct.Pin = IDLE_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ /*Configure GPIO pin : PD2 */
+ GPIO_InitStruct.Pin = GPIO_PIN_2;
+ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
- HAL_GPIO_Init(IDLE_GPIO_Port, &GPIO_InitStruct);
+ HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
@@ -449,67 +446,28 @@ error :
return errorcode;
}
-#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 inline void process(float in_div4)
{
- static int64_t z[4] = {0, 0, 0, 0};
+ static float z[4] = {0.f, 0.f, 0.f, 0.f};
- in_div4 <<= BITO;
- int64_t out1 = (in_div4 + z[0]);
- z[0] = ((out1 * 0x43f /*1.062f*/) >> BITO) + z[1];
- z[1] = ((out1 * -0x8f /*-0.14f*/) >> BITO) - in_div4;
+ float out1 = in_div4 + z[0];
+ z[0] = out1 * 1.062f + z[1];
+ z[1] = out1 * -0.14f - in_div4;
- int64_t out2 = (out1 + z[2]);
+ float out2 = out1 + z[2];
z[2] = out1;
- int64_t out3 = (out2 + z[3]);
- z[3] = ((out3 * 0x3f1 /*0.985f*/) >> BITO) - out2;
+ float out3 = out2 + z[3];
+ z[3] = out3 * 0.985f - out2;
- DB_Sum_Squares += lmul(out3, out3) >> BITO;
+ DB_Sum_Squares += out3 * out3;
}
__RAM_FUNC
static void processSampleBlock(sample_t *sample)
{
- IDLE_GPIO_Port->ODR ^= IDLE_Pin;
+ //IDLE_GPIO_Port->ODR ^= IDLE_Pin;
for (int i = 0; i < SAMPLE_COUNT; i += 2) {
// 18-bit sample comes in as big-endian with right padding.
@@ -521,16 +479,15 @@ static void processSampleBlock(sample_t *sample)
DB_Count += SAMPLE_COUNT / 2;
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);
+ float rms = sqrtf(DB_Sum_Squares / DB_Count);
+ float db = 20.f * log10f(rms / MIC_REF_AMPL) + MIC_OFFSET_DB + MIC_REF_DB;
DB_Sum_Squares = 0.f;
DB_Count = 0;
- printf("%d dB\r\n", qfp_float2int(db));
+ printf("%d dB\r\n", (int)db);
}
- IDLE_GPIO_Port->ODR ^= IDLE_Pin;
+ //IDLE_GPIO_Port->ODR ^= IDLE_Pin;
}
void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)