Skip to content

Commit 302719c

Browse files
committed
tested h7 low-side, added repetition counterhandling
1 parent 162322f commit 302719c

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_utils.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
2626
// timer to regular TRGO
2727
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
2828
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
29+
#ifdef TIM2 // if defined timer 2
2930
if(timer->Instance == TIM2)
3031
return ADC_EXTERNALTRIGCONV_T2_TRGO;
32+
#endif
3133
#ifdef TIM3 // if defined timer 3
32-
else if(timer->Instance == TIM3)
34+
if(timer->Instance == TIM3)
3335
return ADC_EXTERNALTRIGCONV_T3_TRGO;
3436
#endif
3537
#ifdef TIM8 // if defined timer 8
36-
else if(timer->Instance == TIM8)
38+
if(timer->Instance == TIM8)
3739
return ADC_EXTERNALTRIGCONV_T8_TRGO;
3840
#endif
39-
else
40-
return _TRGO_NOT_AVAILABLE;
41+
return _TRGO_NOT_AVAILABLE;
4142
}
4243

4344
#endif

src/current_sense/hardware_specific/stm32/stm32h7/stm32h7_hal.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8686
// if ADC1 or ADC2
8787
if(hadc.Instance == ADC1 || hadc.Instance == ADC2){
8888
// more info here: https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_adc.h#L658
89-
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_2CYCLE_5;
89+
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
9090
}else {
9191
// adc3
9292
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_adc.h#L673
@@ -196,13 +196,12 @@ int _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const int
196196
return 0;
197197
}
198198

199-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
199+
200200
extern "C" {
201201
void ADC_IRQHandler(void)
202202
{
203203
HAL_ADC_IRQHandler(&hadc);
204204
}
205205
}
206-
#endif
207206

208207
#endif

src/current_sense/hardware_specific/stm32/stm32h7/stm32h7_mcu.cpp

+48-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ bool needs_downsample[3] = {1};
2222
// downsampling variable - per adc (3)
2323
uint8_t tim_downsample[3] = {1};
2424

25+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
26+
uint8_t use_adc_interrupt = 1;
27+
#else
28+
uint8_t use_adc_interrupt = 0;
29+
#endif
30+
2531
void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
2632

2733
Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
@@ -56,19 +62,47 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
5662
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
5763
// remember that this timer has repetition counter - no need to downasmple
5864
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
65+
}else{
66+
if(!use_adc_interrupt){
67+
// If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
68+
use_adc_interrupt = 1;
69+
#ifdef SIMPLEFOC_STM32_DEBUG
70+
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
71+
#endif
72+
}
5973
}
74+
6075
// set the trigger output event
6176
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
6277

6378
// Start the adc calibration
64-
HAL_ADCEx_Calibration_Start(cs_params->adc_handle, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
79+
if(HAL_ADCEx_Calibration_Start(cs_params->adc_handle, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK){
80+
#ifdef SIMPLEFOC_STM32_DEBUG
81+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot calibrate ADC!");
82+
#endif
83+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
84+
}
6585

6686
// start the adc
67-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
68-
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
69-
#else
70-
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
71-
#endif
87+
if(use_adc_interrupt){
88+
// enable interrupt
89+
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
90+
HAL_NVIC_EnableIRQ(ADC_IRQn);
91+
92+
if(HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle) != HAL_OK){
93+
#ifdef SIMPLEFOC_STM32_DEBUG
94+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot start injected channels in interrupt mode!");
95+
#endif
96+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
97+
}
98+
}else{
99+
if(HAL_ADCEx_InjectedStart(cs_params->adc_handle) != HAL_OK){
100+
#ifdef SIMPLEFOC_STM32_DEBUG
101+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot start injected channels!");
102+
#endif
103+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
104+
}
105+
}
72106

73107

74108
// restart all the timers of the driver
@@ -83,32 +117,34 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
83117

84118
// function reading an ADC value and returning the read voltage
85119
float _readADCVoltageLowSide(const int pin, const void* cs_params){
120+
// print all values in the buffer
121+
// SIMPLEFOC_DEBUG("adc_a:", (int)HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, _getADCInjectedRank(0)));
122+
// SIMPLEFOC_DEBUG("adc_b:", (int)HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, _getADCInjectedRank(1)));
86123
uint8_t channel_no = 0;
87124
for(int i=0; i < 3; i++){
88125
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
89-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
126+
if (use_adc_interrupt){
90127
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][channel_no] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
91-
#else
128+
}else{
92129
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
93130
uint32_t channel = _getADCInjectedRank(channel_no);
94131
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
95-
#endif
132+
}
96133
}
97134
if(_isset(((Stm32CurrentSenseParams*)cs_params)->pins[i])) channel_no++;
98135
}
99136
return 0;
100137
}
101138

102-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
103139
extern "C" {
104140
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
105141

106142
// calculate the instance
107143
int adc_index = _adcToIndex(AdcHandle);
108144

109145
// if the timer han't repetition counter - downsample two times
110-
if( needs_downsample[adc_index] && tim_downsample[adc_index]++ > 0) {
111-
tim_downsample[adc_index] = 0;
146+
if( needs_downsample[adc_index] && tim_downsample[adc_index]++ > 1) {
147+
tim_downsample[adc_index] = 1;
112148
return;
113149
}
114150

@@ -117,6 +153,5 @@ extern "C" {
117153
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
118154
}
119155
}
120-
#endif
121156

122157
#endif

0 commit comments

Comments
 (0)