77#include  " ../stm32_mcu.h" 
88#include  " ../../../../drivers/hardware_specific/stm32/stm32_mcu.h" 
99#include  " communication/SimpleFOCDebug.h" 
10+ #include  " ../stm32_adc_utils.h" 
1011
1112#define  _ADC_VOLTAGE  3 .3f 
1213#define  _ADC_RESOLUTION  4096 .0f 
@@ -25,6 +26,16 @@ static DMA_HandleTypeDef hdma_adc2;
2526volatile  uint16_t  adcBuffer1[ADC_BUF_LEN_1] = {0 }; //  Buffer for store the results of the ADC conversion
2627volatile  uint16_t  adcBuffer2[ADC_BUF_LEN_2] = {0 }; //  Buffer for store the results of the ADC conversion
2728
29+ 
30+ //  structure containing the configuration of the adc interrupt
31+ Stm32AdcInterruptConfig adc_interrupt_config[5 ] = {
32+   {0 , 0 , 0 }, //  ADC1
33+   {0 , 0 , 0 }, //  ADC2
34+   {0 , 0 , 0 }, //  ADC3
35+   {0 , 0 , 0 }, //  ADC4
36+   {0 , 0 , 0 }  //  ADC5
37+ };
38+ 
2839//  function reading an ADC value and returning the read voltage
2940//  As DMA is being used just return the DMA result
3041float  _readADCVoltageLowSide (const  int  pin, const  void * cs_params){
@@ -155,14 +166,16 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
155166  //  stop all the timers for the driver
156167  stm32_pause (driver_params);
157168
158-   //  if timer has repetition counter - it will downsample using it
159-   //  and it does not need the software downsample
160-   if ( IS_TIM_REPETITION_COUNTER_INSTANCE (cs_params->timer_handle ->Instance ) ){
161-     //  adjust the initial timer state such that the trigger for DMA transfer aligns with the pwm peaks instead of throughs.
162-     //  only necessary for the timers that have repetition counters
163-     cs_params->timer_handle ->Instance ->CR1  |= TIM_CR1_DIR;
164-     cs_params->timer_handle ->Instance ->CNT  =  cs_params->timer_handle ->Instance ->ARR ;
169+   //  get the index of the adc
170+   int  adc_index = _adcToIndex (cs_params->adc_handle );
171+ 
172+   bool  tim_interrupt = _initTimerInterruptDownsampling (cs_params, driver_params, adc_interrupt_config[adc_index]);
173+   if (tim_interrupt) {
174+   //  error in the timer interrupt initialization
175+     SIMPLEFOC_DEBUG (" STM32-CS: timer has no repetition counter, ADC interrupt not supported for B-G431"  );
176+     return  SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
165177  }
178+ 
166179  //  set the trigger output event
167180  LL_TIM_SetTriggerOutput (cs_params->timer_handle ->Instance , LL_TIM_TRGO_UPDATE);
168181
0 commit comments