Skip to content

Commit

Permalink
Use interrupt if no repetition counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Candas1 committed Feb 6, 2024
1 parent 1ae389a commit 5c3e845
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected);
}

#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
// enable interrupt
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
#endif

cs_params->adc_handle = &hadc;

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

}

#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
extern "C" {
void ADC1_2_IRQHandler(void)
{
HAL_ADC_IRQHandler(&hadc);
}

}
#endif

#endif
36 changes: 26 additions & 10 deletions src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ bool needs_downsample[3] = {1};
// downsampling variable - per adc (3)
uint8_t tim_downsample[3] = {0};

#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
uint8_t use_adc_interrupt = 1;
#else
uint8_t use_adc_interrupt = 0;
#endif

int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
if(AdcHandle->Instance == ADC1) return 0;
#ifdef ADC2 // if ADC2 exists
Expand Down Expand Up @@ -70,12 +76,24 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
// Start the adc calibration
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);

if( !use_adc_interrupt && !IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance)){
// If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
use_adc_interrupt = 1;
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
#endif
}

// start the adc
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
#else
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
#endif
if(use_adc_interrupt){
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);

HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
}else{
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
}


// restart all the timers of the driver
_startTimers(driver_params->timers, 6);
Expand All @@ -86,19 +104,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
float _readADCVoltageLowSide(const int pin, const void* cs_params){
for(int i=0; i < 3; i++){
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
if (use_adc_interrupt){
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
#else
}else{
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
uint32_t channel = (i == 0) ? ADC_INJECTED_RANK_1 : (i == 1) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;;
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
#endif
}
}
}
return 0;
}

#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
extern "C" {
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
// calculate the instance
Expand All @@ -115,6 +132,5 @@ extern "C" {
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
}
}
#endif

#endif

0 comments on commit 5c3e845

Please sign in to comment.