@@ -19,6 +19,12 @@ bool needs_downsample[3] = {1};
19
19
// downsampling variable - per adc (3)
20
20
uint8_t tim_downsample[3 ] = {0 };
21
21
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
+
22
28
int _adcToIndex (ADC_HandleTypeDef *AdcHandle){
23
29
if (AdcHandle->Instance == ADC1) return 0 ;
24
30
#ifdef ADC2 // if ADC2 exists
@@ -70,12 +76,24 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
70
76
// Start the adc calibration
71
77
HAL_ADCEx_Calibration_Start (cs_params->adc_handle );
72
78
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
+
73
87
// 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
+
79
97
80
98
// restart all the timers of the driver
81
99
_startTimers (driver_params->timers , 6 );
@@ -86,19 +104,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
86
104
float _readADCVoltageLowSide (const int pin, const void * cs_params){
87
105
for (int i=0 ; i < 3 ; i++){
88
106
if ( pin == ((Stm32CurrentSenseParams*)cs_params)->pins [i]){ // found in the buffer
89
- # ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
107
+ if (use_adc_interrupt){
90
108
return adc_val[_adcToIndex (((Stm32CurrentSenseParams*)cs_params)->adc_handle )][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv ;
91
- # else
109
+ } else {
92
110
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
93
111
uint32_t channel = (i == 0 ) ? ADC_INJECTED_RANK_1 : (i == 1 ) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;;
94
112
return HAL_ADCEx_InjectedGetValue (((Stm32CurrentSenseParams*)cs_params)->adc_handle , channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv ;
95
- # endif
113
+ }
96
114
}
97
115
}
98
116
return 0 ;
99
117
}
100
118
101
- #ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
102
119
extern " C" {
103
120
void HAL_ADCEx_InjectedConvCpltCallback (ADC_HandleTypeDef *AdcHandle){
104
121
// calculate the instance
@@ -115,6 +132,5 @@ extern "C" {
115
132
adc_val[adc_index][2 ]=HAL_ADCEx_InjectedGetValue (AdcHandle, ADC_INJECTED_RANK_3);
116
133
}
117
134
}
118
- #endif
119
135
120
136
#endif
0 commit comments