implemented sweep and current steps processing. But not compiles
This commit is contained in:
101
Core/Inc/main.h
101
Core/Inc/main.h
@ -30,59 +30,74 @@ 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 */
|
||||
/* 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 */
|
||||
#define CURR_STEP_START_TRG_Pin GPIO_PIN_0
|
||||
#define CURR_STEP_START_TRG_GPIO_Port GPIOC
|
||||
#define CURR_STEP_START_TRG_EXTI_IRQn EXTI0_IRQn
|
||||
#define SWEEP_CYCLE_START_TRG_Pin GPIO_PIN_1
|
||||
#define SWEEP_CYCLE_START_TRG_GPIO_Port GPIOC
|
||||
#define SWEEP_CYCLE_START_TRG_EXTI_IRQn EXTI1_IRQn
|
||||
#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_typedef {
|
||||
uint8_t status; /* 0 - stopped, 1 - collecting, 2 - filled */
|
||||
uint32_t sum;
|
||||
uint32_t avg;
|
||||
uint32_t N;
|
||||
};
|
||||
|
||||
uint32_t curr_step_start_N = 0;
|
||||
|
||||
struct Sweep_state_typedef {
|
||||
uint32_t curr_step_N;
|
||||
uint8_t curr_step_started_flag; //0 -- not started or waiting for; 1 -- first 1/2 DMA buff; 2 -- second 1/2 DMA buff
|
||||
uint32_t curr_step_start_DMA_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
|
||||
}
|
||||
|
||||
@ -55,6 +55,8 @@ void SVC_Handler(void);
|
||||
void DebugMon_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
void EXTI0_IRQHandler(void);
|
||||
void EXTI3_IRQHandler(void);
|
||||
void DMA2_Stream0_IRQHandler(void);
|
||||
void OTG_FS_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
@ -59,8 +59,9 @@ static void MX_ADC1_Init(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
/* adc_process definition is provided here; structure is declared in main.h */
|
||||
struct ADC_proc adc_process;
|
||||
/* ADC_proc_shadow definition is provided here; structure is declared in main.h */
|
||||
struct ADC_proc_typedef ADC_proc, ADC_proc_shadow;
|
||||
struct Sweep_state_typedef Sweep_state;
|
||||
|
||||
/* ADC1 circular DMA buffer definition */
|
||||
uint16_t ADC1_buff_circular[ADC_BUFF_SIZE];
|
||||
@ -104,10 +105,16 @@ int main(void)
|
||||
/* USER CODE BEGIN 2 */
|
||||
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
||||
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC1_buff_circular, ADC_BUFF_SIZE);
|
||||
adc_process.status = 0; // ADC started
|
||||
adc_process.N = 0;
|
||||
adc_process.sum = 0;
|
||||
adc_process.avg = 0;
|
||||
|
||||
ADC_proc_shadow.status = 0; // ADC started
|
||||
ADC_proc_shadow.N = 0;
|
||||
ADC_proc_shadow.sum = 0;
|
||||
ADC_proc_shadow.avg = 0;
|
||||
|
||||
ADC_proc.status = 0; // ADC started
|
||||
ADC_proc.N = 0;
|
||||
ADC_proc.sum = 0;
|
||||
ADC_proc.avg = 0;
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@ -118,23 +125,23 @@ int main(void)
|
||||
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
|
||||
//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;
|
||||
if (ADC_proc_shadow.status == 2) {
|
||||
ADC_proc_shadow.avg = ADC_proc_shadow.sum / ADC_proc_shadow.N;
|
||||
ADC_proc_shadow.status = 1; // reset for next accumulation
|
||||
ADC_proc_shadow.sum = 0;
|
||||
ADC_proc_shadow.N = 0;
|
||||
|
||||
|
||||
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';
|
||||
ADC_msg[ADC_msg_val_pos + 0] = (ADC_proc_shadow.avg / 10000000000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 1] = (ADC_proc_shadow.avg / 1000000000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 2] = (ADC_proc_shadow.avg / 10000000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 3] = (ADC_proc_shadow.avg / 1000000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 4] = (ADC_proc_shadow.avg / 100000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 5] = (ADC_proc_shadow.avg / 10000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 6] = (ADC_proc_shadow.avg / 1000) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 7] = (ADC_proc_shadow.avg / 100) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 8] = (ADC_proc_shadow.avg / 10) % 10 + '0';
|
||||
ADC_msg[ADC_msg_val_pos + 9] = (ADC_proc_shadow.avg / 1) % 10 + '0';
|
||||
CDC_Transmit_FS((uint8_t *)ADC_msg, ADC_msg_len);
|
||||
|
||||
}
|
||||
@ -285,6 +292,18 @@ static void MX_GPIO_Init(void)
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
||||
|
||||
/*Configure GPIO pin : CURR_STEP_START_TRG_Pin */
|
||||
GPIO_InitStruct.Pin = CURR_STEP_START_TRG_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
HAL_GPIO_Init(CURR_STEP_START_TRG_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : SWEEP_CYCLE_START_TRG_Pin */
|
||||
GPIO_InitStruct.Pin = SWEEP_CYCLE_START_TRG_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
HAL_GPIO_Init(SWEEP_CYCLE_START_TRG_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PF11 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_11;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
@ -298,6 +317,13 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
|
||||
@ -95,17 +95,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**ADC1 GPIO Configuration
|
||||
PC0 ------> ADC1_IN10
|
||||
PA3 ------> ADC1_IN3
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_3;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
@ -155,11 +148,8 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
|
||||
__HAL_RCC_ADC1_CLK_DISABLE();
|
||||
|
||||
/**ADC1 GPIO Configuration
|
||||
PC0 ------> ADC1_IN10
|
||||
PA3 ------> ADC1_IN3
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3);
|
||||
|
||||
/* ADC1 DMA DeInit */
|
||||
|
||||
@ -58,7 +58,8 @@
|
||||
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
|
||||
extern DMA_HandleTypeDef hdma_adc1;
|
||||
/* USER CODE BEGIN EV */
|
||||
extern struct ADC_proc adc_process;
|
||||
extern struct ADC_proc_typedef ADC_proc, ADC_proc_shadow;
|
||||
extern struct Sweep_state_typedef Sweep_state;
|
||||
/* USER CODE END EV */
|
||||
|
||||
/******************************************************************************/
|
||||
@ -199,6 +200,39 @@ void SysTick_Handler(void)
|
||||
/* please refer to the startup file (startup_stm32f4xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line0 interrupt.
|
||||
*/
|
||||
void EXTI0_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI0_IRQn 0 */
|
||||
Sweep_state.curr_step_start_DMA_N = ADC_BUFF_SIZE - hdma_adc1.Instance->NDTR;
|
||||
if (Sweep_state.curr_step_start_DMA_N < ADC_BUFF_SIZE/2) {
|
||||
Sweep_state.curr_step_started_flag =1; // first half DMA buffer
|
||||
} else{
|
||||
Sweep_state.curr_step_started_flag =2; // second half DMA buffer
|
||||
}
|
||||
/* USER CODE END EXTI0_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(CURR_STEP_START_TRG_Pin);
|
||||
/* USER CODE BEGIN EXTI0_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI0_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line3 interrupt.
|
||||
*/
|
||||
void EXTI3_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI3_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(SWEEP_CYCLE_START_TRG_Pin);
|
||||
/* USER CODE BEGIN EXTI3_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles DMA2 stream0 global interrupt.
|
||||
*/
|
||||
@ -232,17 +266,94 @@ void OTG_FS_IRQHandler(void)
|
||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
||||
for (uint32_t i = ADC_BUFF_SIZE/2; i < ADC_BUFF_SIZE; i++) {
|
||||
adc_process.sum += ADC1_buff_circular[i];
|
||||
|
||||
if (Sweep_state.curr_step_started_flag == 2) {
|
||||
for (uint32_t i = ADC_BUFF_SIZE/2; i < Sweep_state.curr_step_start_DMA_N; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
|
||||
ADC_proc.N += Sweep_state.curr_step_start_DMA_N - ADC_BUFF_SIZE/2;
|
||||
|
||||
|
||||
ADC_proc_shadow.sum = ADC_proc.sum;
|
||||
ADC_proc_shadow.avg = ADC_proc.avg;
|
||||
ADC_proc_shadow.N = ADC_proc.N;
|
||||
ADC_proc_shadow.status = 2; // buffer filled
|
||||
|
||||
|
||||
ADC_proc.sum = 0;
|
||||
ADC_proc.N = 0;
|
||||
ADC_proc.avg = 0;
|
||||
ADC_proc.status = 1; // collecting data
|
||||
|
||||
for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
ADC_proc.N = ADC_BUFF_SIZE - Sweep_state.curr_step_start_DMA_N;
|
||||
|
||||
|
||||
}else{
|
||||
for (uint32_t i = ADC_BUFF_SIZE/2; i < ADC_BUFF_SIZE; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
ADC_proc.N += ADC_BUFF_SIZE - ADC_BUFF_SIZE/2;
|
||||
}
|
||||
if (ADC_proc.N >= ADC_BUFF_SIZE*100){
|
||||
ADC_proc_shadow.sum = ADC_proc.sum;
|
||||
ADC_proc_shadow.avg = ADC_proc.avg;
|
||||
ADC_proc_shadow.N = ADC_proc.N;
|
||||
ADC_proc_shadow.status = 2; // buffer filled
|
||||
|
||||
|
||||
ADC_proc.sum = 0;
|
||||
ADC_proc.N = 0;
|
||||
ADC_proc.avg = 0;
|
||||
ADC_proc.status = 1; // collecting data
|
||||
}
|
||||
adc_process.N += ADC_BUFF_SIZE - ADC_BUFF_SIZE/2;
|
||||
adc_process.status = 2; // buffer filled
|
||||
// This function is called when the first half of the ADC buffer is filled
|
||||
// You can process the first half of ADC1_buff_circular here
|
||||
}
|
||||
|
||||
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
//HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
|
||||
|
||||
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
|
||||
if (Sweep_state.curr_step_started_flag == 1) {
|
||||
for (uint32_t i = 0; i < Sweep_state.curr_step_start_DMA_N; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
|
||||
ADC_proc.N += Sweep_state.curr_step_start_DMA_N;
|
||||
|
||||
|
||||
ADC_proc_shadow.sum = ADC_proc.sum;
|
||||
ADC_proc_shadow.avg = ADC_proc.avg;
|
||||
ADC_proc_shadow.N = ADC_proc.N;
|
||||
ADC_proc_shadow.status = 2; // buffer filled
|
||||
|
||||
|
||||
ADC_proc.sum = 0;
|
||||
ADC_proc.N = 0;
|
||||
ADC_proc.avg = 0;
|
||||
ADC_proc.status = 1; // collecting data
|
||||
|
||||
for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE/2; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
ADC_proc.N = Sweep_state.curr_step_start_DMA_N;
|
||||
|
||||
}else{
|
||||
for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
|
||||
ADC_proc.sum += ADC1_buff_circular[i];
|
||||
}
|
||||
ADC_proc.N += ADC_BUFF_SIZE/2;
|
||||
}
|
||||
// This function is called when the first half of the ADC buffer is filled
|
||||
// You can process the first half of ADC1_buff_circular here
|
||||
}
|
||||
|
||||
/*
|
||||
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
//HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
|
||||
@ -256,4 +367,6 @@ void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
// This function is called when the first half of the ADC buffer is filled
|
||||
// You can process the first half of ADC1_buff_circular here
|
||||
}
|
||||
|
||||
*/
|
||||
/* USER CODE END 1 */
|
||||
|
||||
Reference in New Issue
Block a user