From 627c4919989ad447f96f451d7d7d3e0c9e0ac4c0 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Mon, 28 Dec 2020 15:59:43 +1100 Subject: [PATCH] Fix broken ADC sampling was ignoring ADC2 readings --- workspace/TS100/Core/BSP/Miniware/Pins.h | 1 + workspace/TS100/Core/BSP/Miniware/Setup.c | 33 +++++++++++-------- .../Core/BSP/Miniware/stm32f1xx_hal_msp.c | 14 +++++--- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/workspace/TS100/Core/BSP/Miniware/Pins.h b/workspace/TS100/Core/BSP/Miniware/Pins.h index 59d26a07..b7d06711 100644 --- a/workspace/TS100/Core/BSP/Miniware/Pins.h +++ b/workspace/TS100/Core/BSP/Miniware/Pins.h @@ -16,6 +16,7 @@ #define TMP36_INPUT_Pin GPIO_PIN_7 #define TMP36_INPUT_GPIO_Port GPIOA #define TMP36_ADC1_CHANNEL ADC_CHANNEL_7 +#define TMP36_ADC2_CHANNEL ADC_CHANNEL_7 #define TIP_TEMP_Pin GPIO_PIN_0 #define TIP_TEMP_GPIO_Port GPIOB #define TIP_TEMP_ADC1_CHANNEL ADC_CHANNEL_8 diff --git a/workspace/TS100/Core/BSP/Miniware/Setup.c b/workspace/TS100/Core/BSP/Miniware/Setup.c index 1ecc25a5..d3d4bff8 100644 --- a/workspace/TS100/Core/BSP/Miniware/Setup.c +++ b/workspace/TS100/Core/BSP/Miniware/Setup.c @@ -17,8 +17,9 @@ DMA_HandleTypeDef hdma_i2c1_tx; IWDG_HandleTypeDef hiwdg; TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim3; - -uint16_t ADCReadings[64]; // room for 32 lots of the pair of readings +#define ADC_CHANNELS 2 +#define ADC_SAMPLES 16 +uint32_t ADCReadings[ADC_SAMPLES * ADC_CHANNELS]; // room for 32 lots of the pair of readings // Functions static void SystemClock_Config(void); @@ -30,7 +31,6 @@ static void MX_TIM2_Init(void); static void MX_DMA_Init(void); static void MX_GPIO_Init(void); static void MX_ADC2_Init(void); -#define SWD_ENABLE void Setup_HAL() { SystemClock_Config(); @@ -49,7 +49,7 @@ void Setup_HAL() { MX_TIM2_Init(); MX_IWDG_Init(); 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(&hadc2); // enable injected readings } @@ -57,8 +57,12 @@ void Setup_HAL() { // channel 0 -> temperature sensor, 1-> VIN uint16_t getADC(uint8_t channel) { uint32_t sum = 0; - for (uint8_t i = 0; i < 32; i++) - sum += ADCReadings[channel + (i * 2)]; + for (uint8_t i = 0; i < ADC_SAMPLES; i++) { + uint16_t adc1Sample = ADCReadings[channel + (i * ADC_CHANNELS)]; + uint16_t adc2Sample = ADCReadings[channel + (i * ADC_CHANNELS)] >> 16; + + sum += (adc1Sample + adc2Sample); + } return sum >> 2; } @@ -125,7 +129,7 @@ static void MX_ADC1_Init(void) { hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 2; + hadc1.Init.NbrOfConversion = ADC_CHANNELS; HAL_ADC_Init(&hadc1); /**Configure the ADC multi-mode @@ -136,14 +140,14 @@ static void MX_ADC1_Init(void) { /**Configure Regular Channel */ sConfig.Channel = TMP36_ADC1_CHANNEL; - sConfig.Rank = 1; + sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure Regular Channel */ sConfig.Channel = VIN_ADC1_CHANNEL; - sConfig.Rank = 2; + sConfig.Rank = ADC_REGULAR_RANK_2; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure Injected Channel @@ -164,8 +168,8 @@ static void MX_ADC1_Init(void) { sConfigInjected.InjectedOffset = 0; HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); - sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfigInjected.InjectedRank = 2; HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); sConfigInjected.InjectedRank = 3; @@ -191,15 +195,16 @@ static void MX_ADC2_Init(void) { hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc2.Init.NbrOfConversion = 2; + hadc2.Init.NbrOfConversion = ADC_CHANNELS; HAL_ADC_Init(&hadc2); /**Configure Regular Channel */ - sConfig.Channel = TIP_TEMP_ADC2_CHANNEL; + sConfig.Channel = TMP36_ADC2_CHANNEL; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; HAL_ADC_ConfigChannel(&hadc2, &sConfig); + sConfig.Channel = VIN_ADC2_CHANNEL; sConfig.Rank = ADC_REGULAR_RANK_2; HAL_ADC_ConfigChannel(&hadc2, &sConfig); @@ -279,7 +284,7 @@ static void MX_TIM3_Init(void) { HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); 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.OCFastMode = TIM_OCFAST_ENABLE; HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL); @@ -339,7 +344,7 @@ static void MX_TIM2_Init(void) { HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); 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 //255 is the largest time period of the drive signal, and then offset ADC sample to be a bit delayed after this /* diff --git a/workspace/TS100/Core/BSP/Miniware/stm32f1xx_hal_msp.c b/workspace/TS100/Core/BSP/Miniware/stm32f1xx_hal_msp.c index a9daf6ac..058fb241 100644 --- a/workspace/TS100/Core/BSP/Miniware/stm32f1xx_hal_msp.c +++ b/workspace/TS100/Core/BSP/Miniware/stm32f1xx_hal_msp.c @@ -41,10 +41,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; - hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; - hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; + hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; + hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; 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_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.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 */ HAL_NVIC_SetPriority(ADC1_2_IRQn, 15, 0);