DMA-based averager works! Also it dumps avg values to USB CDC

This commit is contained in:
2025-12-18 20:19:39 +03:00
parent d0ef88b6b9
commit 7f5d47d422
19 changed files with 3988 additions and 3837 deletions

View File

@ -30,41 +30,59 @@ extern "C" {
#include "stm32f4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
#define LED_RED_Pin GPIO_PIN_14
#define LED_RED_GPIO_Port GPIOB
#define LED_BLUE_Pin GPIO_PIN_7
#define LED_BLUE_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#define LED_RED_Pin GPIO_PIN_14
#define LED_RED_GPIO_Port GPIOB
#define LED_BLUE_Pin GPIO_PIN_7
#define LED_BLUE_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
/* Shared ADC app types and declarations */
/* Size of circular DMA buffer for ADC1 */
#ifndef ADC_BUFF_SIZE
#define ADC_BUFF_SIZE 100
#endif
/* Structure describing simple accumulation state for ADC processing */
struct ADC_proc {
uint8_t status; /* 0 - stopped, 1 - collecting, 2 - filled */
uint32_t sum;
uint32_t avg;
uint32_t N;
};
/* Externs provided by main.c */
extern struct ADC_proc adc_process;
extern uint16_t ADC1_buff_circular[ADC_BUFF_SIZE];
/* USER CODE END Private defines */
#ifdef __cplusplus
}

View File

@ -59,16 +59,14 @@ static void MX_ADC1_Init(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
struct ADC_proc{
uint8_t status; // 0 - stopped, 1 - started-filling, 2 - filled
uint32_t sum;
uint32_t avg;
uint32_t N;
} adc_process;
/* adc_process definition is provided here; structure is declared in main.h */
struct ADC_proc adc_process;
#define ADC_BUFF_SIZE 100
/* ADC1 circular DMA buffer definition */
uint16_t ADC1_buff_circular[ADC_BUFF_SIZE];
char ADC_msg[] = "Received ADC value: ??????????\r\n";
#define ADC_msg_len 32
#define ADC_msg_val_pos 20
/* USER CODE END 0 */
/**
@ -118,31 +116,29 @@ int main(void)
while (1)
{
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
HAL_Delay(100);
//HAL_Delay(100);
if (adc_process.status == 2) {
adc_process.avg = adc_process.sum / adc_process.N;
adc_process.status = 1; // reset for next accumulation
adc_process.sum = 0;
adc_process.N = 0;
char digits[10] = {0};
digits[0] = (adc_process.avg / 1000000000) % 10 + '0';
digits[1] = (adc_process.avg / 100000000) % 10 + '0';
digits[2] = (adc_process.avg / 10000000) % 10 + '0';
digits[3] = (adc_process.avg / 1000000) % 10 + '0';
digits[4] = (adc_process.avg / 100000) % 10 + '0';
digits[5] = (adc_process.avg / 10000) % 10 + '0';
digits[6] = (adc_process.avg / 1000) % 10 + '0';
digits[7] = (adc_process.avg / 100) % 10 + '0';
digits[8] = (adc_process.avg / 10) % 10 + '0';
digits[9] = (adc_process.avg / 1) % 10 + '0';
CDC_Transmit_FS((uint8_t *)"ADC Average calculated: ", 24);
CDC_Transmit_FS((uint8_t *)digits, 10);
CDC_Transmit_FS((uint8_t *)"\r\n", 2);
ADC_msg[ADC_msg_val_pos + 0] = (adc_process.avg / 10000000000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 1] = (adc_process.avg / 1000000000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 2] = (adc_process.avg / 10000000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 3] = (adc_process.avg / 1000000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 4] = (adc_process.avg / 100000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 5] = (adc_process.avg / 10000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 6] = (adc_process.avg / 1000) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 7] = (adc_process.avg / 100) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 8] = (adc_process.avg / 10) % 10 + '0';
ADC_msg[ADC_msg_val_pos + 9] = (adc_process.avg / 1) % 10 + '0';
CDC_Transmit_FS((uint8_t *)ADC_msg, ADC_msg_len);
}
CDC_Transmit_FS((uint8_t *)"Hello from STM32!\r\n", 19);
//CDC_Transmit_FS((uint8_t *)"Hello from STM32!\r\n", 19);
/* USER CODE END WHILE */