Synchronize ADC2 with PWM signal

For boards with STM32G431, the ADC2 is used for fast current measurement,
synchronized with the half bridge switching PWM.
With this change, the ADC2 is triggered in the center of high-side
MOSFET ON signal using a second PWM output of TIM1.
This commit is contained in:
Martin Jäger
2020-04-02 12:32:43 +02:00
parent 91d4ec4424
commit 2c2e59c072
3 changed files with 17 additions and 19 deletions
-16
View File
@@ -3,19 +3,3 @@
# include generic charge controller Kconfig
rsource "../../Kconfig"
if ADC_STM32
config ADC_1
prompt "ADC1"
default y
help
Enable ADC1
config ADC_2
prompt "ADC2"
default y
help
Enable ADC2
endif # ADC_STM32
+5 -3
View File
@@ -187,6 +187,10 @@ static void adc_init(ADC_TypeDef *adc)
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
#if defined(CONFIG_SOC_SERIES_STM32G4X)
if (adc == ADC2) {
LL_ADC_REG_SetTriggerEdge(adc, LL_ADC_REG_TRIG_EXT_RISING);
LL_ADC_REG_SetTriggerSource(adc, LL_ADC_REG_TRIG_EXT_TIM1_TRGO2);
}
LL_ADC_Enable(adc);
#endif
}
@@ -210,9 +214,7 @@ static inline void adc_trigger_conversion(struct k_timer *timer_id)
{
LL_ADC_REG_StartConversion(ADC1);
#if defined(CONFIG_SOC_SERIES_STM32G4X)
LL_ADC_REG_StartConversion(ADC2);
#endif
// ADC2 (if existing) is triggered by the PWM timer
}
static void DMA1_Channel1_IRQHandler(void *args)
+12
View File
@@ -159,6 +159,15 @@ class PWM_TIM1
// OIS1 = OIS1N = 0: Output Idle State is set to off
//TIM1->CR2 |= ;
#ifdef STM32G4
// Boards with STM32G4 MCU use ADC2 for synchronized ADC sampling. We use OC2
// for triggering, but but don't configure any pin for PWM mode
TIM1->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;
TIM1->CCER |= TIM_CCER_CC2E | TIM_CCER_CC2NE;
// Trigger ADC via TIM1_TRGO2 on OC2ref signal event
TIM1->CR2 |= TIM_CR2_MMS2_2 | TIM_CR2_MMS2_0;
#endif
// Force update generation (UG = 1)
TIM1->EGR |= TIM_EGR_UG;
@@ -199,6 +208,9 @@ class PWM_TIM1
static void set_ccr(uint32_t val)
{
TIM1->CCR1 = val;
// Trigger ADC for current measurement in the middle of the cycle.
TIM1->CCR2 = 1;
}
};