Fix broken ADC sampling was ignoring ADC2 readings

This commit is contained in:
Ben V. Brown
2020-12-28 15:59:43 +11:00
parent f20ce0a446
commit 627c491998
3 changed files with 30 additions and 18 deletions

View File

@@ -16,6 +16,7 @@
#define TMP36_INPUT_Pin GPIO_PIN_7 #define TMP36_INPUT_Pin GPIO_PIN_7
#define TMP36_INPUT_GPIO_Port GPIOA #define TMP36_INPUT_GPIO_Port GPIOA
#define TMP36_ADC1_CHANNEL ADC_CHANNEL_7 #define TMP36_ADC1_CHANNEL ADC_CHANNEL_7
#define TMP36_ADC2_CHANNEL ADC_CHANNEL_7
#define TIP_TEMP_Pin GPIO_PIN_0 #define TIP_TEMP_Pin GPIO_PIN_0
#define TIP_TEMP_GPIO_Port GPIOB #define TIP_TEMP_GPIO_Port GPIOB
#define TIP_TEMP_ADC1_CHANNEL ADC_CHANNEL_8 #define TIP_TEMP_ADC1_CHANNEL ADC_CHANNEL_8

View File

@@ -17,8 +17,9 @@ DMA_HandleTypeDef hdma_i2c1_tx;
IWDG_HandleTypeDef hiwdg; IWDG_HandleTypeDef hiwdg;
TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3; TIM_HandleTypeDef htim3;
#define ADC_CHANNELS 2
uint16_t ADCReadings[64]; // room for 32 lots of the pair of readings #define ADC_SAMPLES 16
uint32_t ADCReadings[ADC_SAMPLES * ADC_CHANNELS]; // room for 32 lots of the pair of readings
// Functions // Functions
static void SystemClock_Config(void); static void SystemClock_Config(void);
@@ -30,7 +31,6 @@ static void MX_TIM2_Init(void);
static void MX_DMA_Init(void); static void MX_DMA_Init(void);
static void MX_GPIO_Init(void); static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void); static void MX_ADC2_Init(void);
#define SWD_ENABLE
void Setup_HAL() { void Setup_HAL() {
SystemClock_Config(); SystemClock_Config();
@@ -49,7 +49,7 @@ void Setup_HAL() {
MX_TIM2_Init(); MX_TIM2_Init();
MX_IWDG_Init(); MX_IWDG_Init();
HAL_ADC_Start(&hadc2); HAL_ADC_Start(&hadc2);
HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t*) ADCReadings, 64); // start DMA of normal readings HAL_ADCEx_MultiModeStart_DMA(&hadc1, ADCReadings, (ADC_SAMPLES * ADC_CHANNELS)); // start DMA of normal readings
HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings
HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings
} }
@@ -57,8 +57,12 @@ void Setup_HAL() {
// channel 0 -> temperature sensor, 1-> VIN // channel 0 -> temperature sensor, 1-> VIN
uint16_t getADC(uint8_t channel) { uint16_t getADC(uint8_t channel) {
uint32_t sum = 0; uint32_t sum = 0;
for (uint8_t i = 0; i < 32; i++) for (uint8_t i = 0; i < ADC_SAMPLES; i++) {
sum += ADCReadings[channel + (i * 2)]; uint16_t adc1Sample = ADCReadings[channel + (i * ADC_CHANNELS)];
uint16_t adc2Sample = ADCReadings[channel + (i * ADC_CHANNELS)] >> 16;
sum += (adc1Sample + adc2Sample);
}
return sum >> 2; return sum >> 2;
} }
@@ -125,7 +129,7 @@ static void MX_ADC1_Init(void) {
hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 2; hadc1.Init.NbrOfConversion = ADC_CHANNELS;
HAL_ADC_Init(&hadc1); HAL_ADC_Init(&hadc1);
/**Configure the ADC multi-mode /**Configure the ADC multi-mode
@@ -136,14 +140,14 @@ static void MX_ADC1_Init(void) {
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = TMP36_ADC1_CHANNEL; sConfig.Channel = TMP36_ADC1_CHANNEL;
sConfig.Rank = 1; sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = VIN_ADC1_CHANNEL; sConfig.Channel = VIN_ADC1_CHANNEL;
sConfig.Rank = 2; sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure Injected Channel /**Configure Injected Channel
@@ -164,8 +168,8 @@ static void MX_ADC1_Init(void) {
sConfigInjected.InjectedOffset = 0; sConfigInjected.InjectedOffset = 0;
HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected);
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
sConfigInjected.InjectedRank = 2; sConfigInjected.InjectedRank = 2;
HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected);
sConfigInjected.InjectedRank = 3; sConfigInjected.InjectedRank = 3;
@@ -191,15 +195,16 @@ static void MX_ADC2_Init(void) {
hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.NbrOfConversion = 2; hadc2.Init.NbrOfConversion = ADC_CHANNELS;
HAL_ADC_Init(&hadc2); HAL_ADC_Init(&hadc2);
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = TIP_TEMP_ADC2_CHANNEL; sConfig.Channel = TMP36_ADC2_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
HAL_ADC_ConfigChannel(&hadc2, &sConfig); HAL_ADC_ConfigChannel(&hadc2, &sConfig);
sConfig.Channel = VIN_ADC2_CHANNEL; sConfig.Channel = VIN_ADC2_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_2; sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc2, &sConfig); HAL_ADC_ConfigChannel(&hadc2, &sConfig);
@@ -279,7 +284,7 @@ static void MX_TIM3_Init(void) {
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 80; //80% duty cycle, that is AC coupled through the cap sConfigOC.Pulse = 50; //50% duty cycle, that is AC coupled through the cap
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL);
@@ -339,7 +344,7 @@ static void MX_TIM2_Init(void) {
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.OCMode = TIM_OCMODE_PWM1;
// dummy value, will be reconfigured by BSPInit() // dummy value, will be reconfigured by BSPInit() in the BSP.cpp
sConfigOC.Pulse = 255 + 13 * 2; // 13 -> Delay of 7 ms sConfigOC.Pulse = 255 + 13 * 2; // 13 -> Delay of 7 ms
//255 is the largest time period of the drive signal, and then offset ADC sample to be a bit delayed after this //255 is the largest time period of the drive signal, and then offset ADC sample to be a bit delayed after this
/* /*

View File

@@ -41,10 +41,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) {
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR; hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_VERY_HIGH; hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
HAL_DMA_Init(&hdma_adc1); HAL_DMA_Init(&hdma_adc1);
__HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1); __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);
@@ -62,7 +62,13 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) {
*/ */
GPIO_InitStruct.Pin = TIP_TEMP_Pin; GPIO_InitStruct.Pin = TIP_TEMP_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(TIP_TEMP_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = TMP36_INPUT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(TMP36_INPUT_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = VIN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(VIN_GPIO_Port, &GPIO_InitStruct);
/* ADC2 interrupt Init */ /* ADC2 interrupt Init */
HAL_NVIC_SetPriority(ADC1_2_IRQn, 15, 0); HAL_NVIC_SetPriority(ADC1_2_IRQn, 15, 0);