@@ -19,6 +19,12 @@ bool needs_downsample[3] = {1};
1919// downsampling variable - per adc (3)
2020uint8_t tim_downsample[3 ] = {0 };
2121
22+ #ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
23+ uint8_t use_adc_interrupt = 1 ;
24+ #else
25+ uint8_t use_adc_interrupt = 0 ;
26+ #endif
27+
2228int _adcToIndex (ADC_HandleTypeDef *AdcHandle){
2329 if (AdcHandle->Instance == ADC1) return 0 ;
2430#ifdef ADC2 // if ADC2 exists
@@ -70,12 +76,24 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
7076 // Start the adc calibration
7177 HAL_ADCEx_Calibration_Start (cs_params->adc_handle );
7278
79+ if ( !use_adc_interrupt && !IS_TIM_REPETITION_COUNTER_INSTANCE (cs_params->timer_handle ->getHandle ()->Instance )){
80+ // If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
81+ use_adc_interrupt = 1 ;
82+ #ifdef SIMPLEFOC_STM32_DEBUG
83+ SIMPLEFOC_DEBUG (" STM32-CS: timer has no repetition counter, ADC interrupt has to be used" );
84+ #endif
85+ }
86+
7387 // start the adc
74- #ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
75- HAL_ADCEx_InjectedStart_IT (cs_params->adc_handle );
76- #else
77- HAL_ADCEx_InjectedStart (cs_params->adc_handle );
78- #endif
88+ if (use_adc_interrupt){
89+ HAL_NVIC_SetPriority (ADC1_2_IRQn, 0 , 0 );
90+ HAL_NVIC_EnableIRQ (ADC1_2_IRQn);
91+
92+ HAL_ADCEx_InjectedStart_IT (cs_params->adc_handle );
93+ }else {
94+ HAL_ADCEx_InjectedStart (cs_params->adc_handle );
95+ }
96+
7997
8098 // restart all the timers of the driver
8199 _startTimers (driver_params->timers , 6 );
@@ -86,19 +104,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
86104float _readADCVoltageLowSide (const int pin, const void * cs_params){
87105 for (int i=0 ; i < 3 ; i++){
88106 if ( pin == ((Stm32CurrentSenseParams*)cs_params)->pins [i]){ // found in the buffer
89- # ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
107+ if (use_adc_interrupt){
90108 return adc_val[_adcToIndex (((Stm32CurrentSenseParams*)cs_params)->adc_handle )][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv ;
91- # else
109+ } else {
92110 // an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
93111 uint32_t channel = (i == 0 ) ? ADC_INJECTED_RANK_1 : (i == 1 ) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;;
94112 return HAL_ADCEx_InjectedGetValue (((Stm32CurrentSenseParams*)cs_params)->adc_handle , channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv ;
95- # endif
113+ }
96114 }
97115 }
98116 return 0 ;
99117}
100118
101- #ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
102119extern " C" {
103120 void HAL_ADCEx_InjectedConvCpltCallback (ADC_HandleTypeDef *AdcHandle){
104121 // calculate the instance
@@ -115,6 +132,5 @@ extern "C" {
115132 adc_val[adc_index][2 ]=HAL_ADCEx_InjectedGetValue (AdcHandle, ADC_INJECTED_RANK_3);
116133 }
117134}
118- #endif
119135
120136#endif
0 commit comments