Skip to content

Commit 83e5247

Browse files
authored
Merge pull request #383 from Candas1/use_interrupt_if_no_repetition_counter
Use interrupt if no repetition counter
2 parents 97a3978 + 8382603 commit 83e5247

File tree

8 files changed

+162
-135
lines changed

8 files changed

+162
-135
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: 27 additions & 11 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
@@ -63,19 +69,31 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6369
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
6470
// remember that this timer has repetition counter - no need to downasmple
6571
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
72+
}else{
73+
if(!use_adc_interrupt){
74+
// If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
75+
use_adc_interrupt = 1;
76+
#ifdef SIMPLEFOC_STM32_DEBUG
77+
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
78+
#endif
79+
}
6680
}
6781
// set the trigger output event
6882
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
6983

7084
// Start the adc calibration
7185
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);
72-
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

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

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

138-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
139-
// enable interrupt
140-
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
141-
HAL_NVIC_EnableIRQ(ADC_IRQn);
142-
#endif
143-
144138
cs_params->adc_handle = &hadc;
145139
return 0;
146140
}
@@ -162,13 +156,11 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
162156
}
163157
}
164158

165-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
166159
extern "C" {
167160
void ADC_IRQHandler(void)
168161
{
169162
HAL_ADC_IRQHandler(&hadc);
170163
}
171164
}
172-
#endif
173165

174166
#endif

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

Lines changed: 26 additions & 10 deletions
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] = {0};
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 {
@@ -55,16 +61,28 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
5561
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
5662
// remember that this timer has repetition counter - no need to downasmple
5763
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
64+
}else{
65+
if(!use_adc_interrupt){
66+
// If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
67+
use_adc_interrupt = 1;
68+
#ifdef SIMPLEFOC_STM32_DEBUG
69+
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
70+
#endif
71+
}
5872
}
5973
// set the trigger output event
6074
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
6175

6276
// start the adc
63-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
64-
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
65-
#else
66-
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
67-
#endif
77+
if (use_adc_interrupt){
78+
// enable interrupt
79+
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
80+
HAL_NVIC_EnableIRQ(ADC_IRQn);
81+
82+
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
83+
}else{
84+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
85+
}
6886

6987
// restart all the timers of the driver
7088
_startTimers(driver_params->timers, 6);
@@ -75,19 +93,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
7593
float _readADCVoltageLowSide(const int pin, const void* cs_params){
7694
for(int i=0; i < 3; i++){
7795
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
78-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
96+
if (use_adc_interrupt){
7997
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
80-
#else
98+
}else{
8199
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
82100
uint32_t channel = (i == 0) ? ADC_INJECTED_RANK_1 : (i == 1) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;
83101
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
84-
#endif
102+
}
85103
}
86104
}
87105
return 0;
88106
}
89107

90-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
91108
extern "C" {
92109
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
93110
// calculate the instance
@@ -104,6 +121,5 @@ extern "C" {
104121
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
105122
}
106123
}
107-
#endif
108124

109125
#endif

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,43 +180,6 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
180180
}
181181
}
182182

183-
184-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
185-
if(hadc.Instance == ADC1) {
186-
// enable interrupt
187-
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
188-
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
189-
}
190-
#ifdef ADC2
191-
else if (hadc.Instance == ADC2) {
192-
// enable interrupt
193-
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
194-
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
195-
}
196-
#endif
197-
#ifdef ADC3
198-
else if (hadc.Instance == ADC3) {
199-
// enable interrupt
200-
HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
201-
HAL_NVIC_EnableIRQ(ADC3_IRQn);
202-
}
203-
#endif
204-
#ifdef ADC4
205-
else if (hadc.Instance == ADC4) {
206-
// enable interrupt
207-
HAL_NVIC_SetPriority(ADC4_IRQn, 0, 0);
208-
HAL_NVIC_EnableIRQ(ADC4_IRQn);
209-
}
210-
#endif
211-
#ifdef ADC5
212-
else if (hadc.Instance == ADC5) {
213-
// enable interrupt
214-
HAL_NVIC_SetPriority(ADC5_IRQn, 0, 0);
215-
HAL_NVIC_EnableIRQ(ADC5_IRQn);
216-
}
217-
#endif
218-
#endif
219-
220183
cs_params->adc_handle = &hadc;
221184
return 0;
222185
}
@@ -238,7 +201,6 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
238201
}
239202
}
240203

241-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
242204
extern "C" {
243205
void ADC1_2_IRQHandler(void)
244206
{
@@ -265,6 +227,5 @@ extern "C" {
265227
}
266228
#endif
267229
}
268-
#endif
269230

270231
#endif

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ bool needs_downsample[5] = {1};
2424
// downsampling variable - per adc (5)
2525
uint8_t tim_downsample[5] = {0};
2626

27+
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
28+
uint8_t use_adc_interrupt = 1;
29+
#else
30+
uint8_t use_adc_interrupt = 0;
31+
#endif
2732

2833
void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
2934

@@ -58,6 +63,14 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
5863
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
5964
// remember that this timer has repetition counter - no need to downasmple
6065
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
66+
}else{
67+
if(!use_adc_interrupt){
68+
// If the timer has no repetition counter, it needs to use the interrupt to downsample for low side sensing
69+
use_adc_interrupt = 1;
70+
#ifdef SIMPLEFOC_STM32_DEBUG
71+
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
72+
#endif
73+
}
6174
}
6275

6376
// set the trigger output event
@@ -66,12 +79,47 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6679
// Start the adc calibration
6780
HAL_ADCEx_Calibration_Start(cs_params->adc_handle,ADC_SINGLE_ENDED);
6881

69-
// start the adc
70-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
71-
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
72-
#else
73-
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
74-
#endif
82+
// start the adc
83+
if (use_adc_interrupt){
84+
// enable interrupt
85+
if(cs_params->adc_handle->Instance == ADC1) {
86+
// enable interrupt
87+
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
88+
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
89+
}
90+
#ifdef ADC2
91+
else if (cs_params->adc_handle->Instance == ADC2) {
92+
// enable interrupt
93+
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
94+
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
95+
}
96+
#endif
97+
#ifdef ADC3
98+
else if (cs_params->adc_handle->Instance == ADC3) {
99+
// enable interrupt
100+
HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
101+
HAL_NVIC_EnableIRQ(ADC3_IRQn);
102+
}
103+
#endif
104+
#ifdef ADC4
105+
else if (cs_params->adc_handle->Instance == ADC4) {
106+
// enable interrupt
107+
HAL_NVIC_SetPriority(ADC4_IRQn, 0, 0);
108+
HAL_NVIC_EnableIRQ(ADC4_IRQn);
109+
}
110+
#endif
111+
#ifdef ADC5
112+
else if (cs_params->adc_handle->Instance == ADC5) {
113+
// enable interrupt
114+
HAL_NVIC_SetPriority(ADC5_IRQn, 0, 0);
115+
HAL_NVIC_EnableIRQ(ADC5_IRQn);
116+
}
117+
#endif
118+
119+
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
120+
}else{
121+
HAL_ADCEx_InjectedStart(cs_params->adc_handle);
122+
}
75123

76124
// restart all the timers of the driver
77125
_startTimers(driver_params->timers, 6);
@@ -82,19 +130,18 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
82130
float _readADCVoltageLowSide(const int pin, const void* cs_params){
83131
for(int i=0; i < 3; i++){
84132
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
85-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
133+
if (use_adc_interrupt){
86134
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
87-
#else
135+
}else{
88136
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
89137
uint32_t channel = (i == 0) ? ADC_INJECTED_RANK_1 : (i == 1) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;
90138
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
91-
#endif
139+
}
92140
}
93141
}
94142
return 0;
95143
}
96144

97-
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
98145
extern "C" {
99146
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
100147
// calculate the instance
@@ -111,6 +158,5 @@ extern "C" {
111158
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
112159
}
113160
}
114-
#endif
115161

116162
#endif

0 commit comments

Comments
 (0)