Skip to content

Commit cb822dc

Browse files
Merge pull request #341 from Candas1/stm32_adc_no_interrupt
Stm32 adc no interrupt
2 parents 95335a6 + 5c724d9 commit cb822dc

File tree

8 files changed

+82
-17
lines changed

8 files changed

+82
-17
lines changed

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

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

127+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
127128
// enable interrupt
128129
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
129130
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
131+
#endif
130132

131133
cs_params->adc_handle = &hadc;
132134

@@ -151,12 +153,14 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
151153

152154
}
153155

156+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
154157
extern "C" {
155158
void ADC1_2_IRQHandler(void)
156159
{
157160
HAL_ADC_IRQHandler(&hadc);
158161
}
159162

160163
}
164+
#endif
161165

162166
#endif

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6969

7070
// Start the adc calibration
7171
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);
72-
72+
7373
// start the adc
74+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
7475
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
76+
#else
77+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
78+
#endif
7579

7680
// restart all the timers of the driver
7781
_startTimers(driver_params->timers, 6);
@@ -81,13 +85,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
8185
// function reading an ADC value and returning the read voltage
8286
float _readADCVoltageLowSide(const int pin, const void* cs_params){
8387
for(int i=0; i < 3; i++){
84-
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]) // found in the buffer
85-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
88+
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
89+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
90+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
91+
#else
92+
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle,i+1) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
93+
#endif
94+
}
8695
}
8796
return 0;
8897
}
8998

90-
99+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
91100
extern "C" {
92101
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
93102
// calculate the instance
@@ -104,5 +113,6 @@ extern "C" {
104113
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
105114
}
106115
}
116+
#endif
107117

108118
#endif

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
135135
}
136136
}
137137

138+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
138139
// enable interrupt
139140
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
140141
HAL_NVIC_EnableIRQ(ADC_IRQn);
142+
#endif
141143

142144
cs_params->adc_handle = &hadc;
143145
return 0;
@@ -160,11 +162,13 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
160162
}
161163
}
162164

165+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
163166
extern "C" {
164167
void ADC_IRQHandler(void)
165168
{
166169
HAL_ADC_IRQHandler(&hadc);
167170
}
168171
}
172+
#endif
169173

170174
#endif

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
5858
}
5959
// set the trigger output event
6060
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
61-
// start the adc
61+
62+
// start the adc
63+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
6264
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
65+
#else
66+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
67+
#endif
6368

6469
// restart all the timers of the driver
6570
_startTimers(driver_params->timers, 6);
@@ -69,13 +74,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6974
// function reading an ADC value and returning the read voltage
7075
float _readADCVoltageLowSide(const int pin, const void* cs_params){
7176
for(int i=0; i < 3; i++){
72-
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]) // found in the buffer
73-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
77+
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
78+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
79+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
80+
#else
81+
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle,i+1) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
82+
#endif
83+
}
7484
}
7585
return 0;
7686
}
7787

78-
88+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
7989
extern "C" {
8090
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
8191
// calculate the instance
@@ -92,5 +102,6 @@ extern "C" {
92102
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
93103
}
94104
}
105+
#endif
95106

96107
#endif

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_hal.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
181181
}
182182

183183

184-
184+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
185185
if(hadc.Instance == ADC1) {
186186
// enable interrupt
187187
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
@@ -214,6 +214,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
214214
HAL_NVIC_SetPriority(ADC5_IRQn, 0, 0);
215215
HAL_NVIC_EnableIRQ(ADC5_IRQn);
216216
}
217+
#endif
217218
#endif
218219

219220
cs_params->adc_handle = &hadc;
@@ -237,6 +238,7 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
237238
}
238239
}
239240

241+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
240242
extern "C" {
241243
void ADC1_2_IRQHandler(void)
242244
{
@@ -263,5 +265,6 @@ extern "C" {
263265
}
264266
#endif
265267
}
268+
#endif
266269

267270
#endif

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_mcu.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,17 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6161

6262
// set the trigger output event
6363
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
64+
65+
// Start the adc calibration
66+
HAL_ADCEx_Calibration_Start(cs_params->adc_handle,ADC_SINGLE_ENDED);
67+
6468
// start the adc
69+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
6570
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
71+
#else
72+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
73+
#endif
74+
6675
// restart all the timers of the driver
6776
_startTimers(driver_params->timers, 6);
6877
}
@@ -71,13 +80,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
7180
// function reading an ADC value and returning the read voltage
7281
float _readADCVoltageLowSide(const int pin, const void* cs_params){
7382
for(int i=0; i < 3; i++){
74-
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]) // found in the buffer
75-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
83+
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
84+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
85+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
86+
#else
87+
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle,i+1) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
88+
#endif
89+
}
7690
}
7791
return 0;
7892
}
7993

80-
94+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
8195
extern "C" {
8296
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
8397
// calculate the instance
@@ -94,5 +108,6 @@ extern "C" {
94108
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
95109
}
96110
}
111+
#endif
97112

98113
#endif

src/current_sense/hardware_specific/stm32/stm32l4/stm32l4_hal.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
180180
}
181181

182182

183-
183+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
184184
if(hadc.Instance == ADC1) {
185185
// enable interrupt
186186
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
@@ -213,6 +213,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
213213
HAL_NVIC_SetPriority(ADC5_IRQn, 0, 0);
214214
HAL_NVIC_EnableIRQ(ADC5_IRQn);
215215
}
216+
#endif
216217
#endif
217218

218219
cs_params->adc_handle = &hadc;
@@ -236,6 +237,7 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
236237
}
237238
}
238239

240+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
239241
extern "C" {
240242
void ADC1_2_IRQHandler(void)
241243
{
@@ -262,5 +264,6 @@ extern "C" {
262264
}
263265
#endif
264266
}
267+
#endif
265268

266269
#endif

src/current_sense/hardware_specific/stm32/stm32l4/stm32l4_mcu.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,17 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6161

6262
// set the trigger output event
6363
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
64-
// start the adc
64+
65+
// Start the adc calibration
66+
HAL_ADCEx_Calibration_Start(cs_params->adc_handle,ADC_SINGLE_ENDED);
67+
68+
// start the adc
69+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
6570
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
71+
#else
72+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
73+
#endif
74+
6675
// restart all the timers of the driver
6776
_startTimers(driver_params->timers, 6);
6877
}
@@ -71,13 +80,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
7180
// function reading an ADC value and returning the read voltage
7281
float _readADCVoltageLowSide(const int pin, const void* cs_params){
7382
for(int i=0; i < 3; i++){
74-
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]) // found in the buffer
75-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
83+
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
84+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
85+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
86+
#else
87+
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle,i+1) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
88+
#endif
89+
}
7690
}
7791
return 0;
7892
}
7993

80-
94+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
8195
extern "C" {
8296
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
8397
// calculate the instance
@@ -94,5 +108,6 @@ extern "C" {
94108
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
95109
}
96110
}
111+
#endif
97112

98113
#endif

0 commit comments

Comments
 (0)