Skip to content

Commit 5c3e845

Browse files
committed
Use interrupt if no repetition counter
1 parent 1ae389a commit 5c3e845

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_hal.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
124124
HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected);
125125
}
126126

127-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
128-
// enable interrupt
129-
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
130-
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
131-
#endif
132-
133127
cs_params->adc_handle = &hadc;
134128

135129
return 0;
@@ -153,14 +147,11 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
153147

154148
}
155149

156-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
157150
extern "C" {
158151
void ADC1_2_IRQHandler(void)
159152
{
160153
HAL_ADC_IRQHandler(&hadc);
161154
}
162-
163155
}
164-
#endif
165156

166157
#endif

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_mcu.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ bool needs_downsample[3] = {1};
1919
// downsampling variable - per adc (3)
2020
uint8_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+
2228
int _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){
86104
float _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
102119
extern "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

Comments
 (0)