Skip to content

Commit adbdb6a

Browse files
committed
added full support for stm32g4 boards + restructure of stm32f4 and testing of stm32f1/f4/g4
1 parent 4a8f8de commit adbdb6a

File tree

12 files changed

+691
-241
lines changed

12 files changed

+691
-241
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
3333
void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
3434

3535
Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
36-
.pins={0},
36+
.pins={(int)NOT_SET,(int)NOT_SET,(int)NOT_SET},
3737
.adc_voltage_conv = (_ADC_VOLTAGE_F1) / (_ADC_RESOLUTION_F1)
3838
};
3939
_adc_gpio_init(cs_params, pinA,pinB,pinC);

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

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,29 @@
22

33
#if defined(STM32F4xx)
44

5+
//#define SIMPLEFOC_STM32_DEBUG
6+
57
#include "../../../../communication/SimpleFOCDebug.h"
68
#define _TRGO_NOT_AVAILABLE 12345
79

8-
9-
// timer to injected TRGO
10-
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h#L179
11-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
12-
if(timer->getHandle()->Instance == TIM1)
13-
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
14-
#ifdef TIM2 // if defined timer 2
15-
else if(timer->getHandle()->Instance == TIM2)
16-
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
17-
#endif
18-
#ifdef TIM4 // if defined timer 4
19-
else if(timer->getHandle()->Instance == TIM4)
20-
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
21-
#endif
22-
#ifdef TIM5 // if defined timer 5
23-
else if(timer->getHandle()->Instance == TIM5)
24-
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
25-
#endif
26-
else
27-
return _TRGO_NOT_AVAILABLE;
28-
}
29-
30-
// timer to regular TRGO
31-
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
32-
uint32_t _timerToRegularTRGO(HardwareTimer* timer){
33-
if(timer->getHandle()->Instance == TIM2)
34-
return ADC_EXTERNALTRIGCONV_T2_TRGO;
35-
#ifdef TIM3 // if defined timer 3
36-
else if(timer->getHandle()->Instance == TIM3)
37-
return ADC_EXTERNALTRIGCONV_T3_TRGO;
38-
#endif
39-
#ifdef TIM8 // if defined timer 8
40-
else if(timer->getHandle()->Instance == TIM8)
41-
return ADC_EXTERNALTRIGCONV_T8_TRGO;
42-
#endif
43-
else
44-
return _TRGO_NOT_AVAILABLE;
45-
}
46-
4710
ADC_HandleTypeDef hadc;
4811

4912
int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params)
5013
{
5114
ADC_InjectionConfTypeDef sConfigInjected;
15+
16+
// check if all pins belong to the same ADC
17+
ADC_TypeDef* adc_pin1 = (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC);
18+
ADC_TypeDef* adc_pin2 = (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[1]), PinMap_ADC);
19+
ADC_TypeDef* adc_pin3 = _isset(cs_params->pins[2]) ? (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[2]), PinMap_ADC) : nullptr;
20+
if ( (adc_pin1 != adc_pin2) || ( (adc_pin3) && (adc_pin1 != adc_pin3) )){
21+
#ifdef SIMPLEFOC_STM32_DEBUG
22+
SIMPLEFOC_DEBUG("STM32-CS: ERR: Analog pins dont belong to the same ADC!");
23+
#endif
24+
return -1;
25+
}
26+
27+
5228
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
5329
*/
5430
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC);
@@ -67,6 +43,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
6743
return -1; // error not a valid ADC instance
6844
}
6945

46+
#ifdef SIMPLEFOC_STM32_DEBUG
47+
SIMPLEFOC_DEBUG("STM32-CS: Using ADC: ", _adcToIndex(&hadc)+1);
48+
#endif
49+
7050
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
7151
hadc.Init.Resolution = ADC_RESOLUTION_12B;
7252
hadc.Init.ScanConvMode = ENABLE;
@@ -79,13 +59,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
7959
hadc.Init.DMAContinuousRequests = DISABLE;
8060
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
8161
if ( HAL_ADC_Init(&hadc) != HAL_OK){
82-
#ifdef SIMPLEFOC_STM32_DEBUG
62+
#ifdef SIMPLEFOC_STM32_DEBUG
8363
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init ADC!");
84-
#endif
64+
#endif
8565
return -1;
8666
}
87-
/**Configure for the selected ADC regular channel to be converted.
88-
*/
8967

9068
/**Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
9169
*/
@@ -124,33 +102,35 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
124102
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->getHandle()->Instance) + 1);
125103
#endif
126104

105+
127106
// first channel
128-
sConfigInjected.InjectedRank = 1;
129-
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[0]), PinMap_ADC));
107+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1;
108+
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[0]));
130109
if (HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected) != HAL_OK){
131-
#ifdef SIMPLEFOC_STM32_DEBUG
132-
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[0]), PinMap_ADC)) );
133-
#endif
110+
#ifdef SIMPLEFOC_STM32_DEBUG
111+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[0])) );
112+
#endif
134113
return -1;
135114
}
115+
136116
// second channel
137-
sConfigInjected.InjectedRank = 2;
138-
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[1]), PinMap_ADC));
117+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_2;
118+
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[1]));
139119
if (HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected) != HAL_OK){
140-
#ifdef SIMPLEFOC_STM32_DEBUG
141-
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[1]), PinMap_ADC)) );
142-
#endif
120+
#ifdef SIMPLEFOC_STM32_DEBUG
121+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[1]))) ;
122+
#endif
143123
return -1;
144124
}
145125

146126
// third channel - if exists
147127
if(_isset(cs_params->pins[2])){
148-
sConfigInjected.InjectedRank = 3;
149-
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[2]), PinMap_ADC));
128+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_3;
129+
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[2]));
150130
if (HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected) != HAL_OK){
151-
#ifdef SIMPLEFOC_STM32_DEBUG
152-
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[2]), PinMap_ADC)) );
153-
#endif
131+
#ifdef SIMPLEFOC_STM32_DEBUG
132+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[2]))) ;
133+
#endif
154134
return -1;
155135
}
156136
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../../../../common/foc_utils.h"
99
#include "../../../../drivers/hardware_specific/stm32_mcu.h"
1010
#include "../stm32_mcu.h"
11+
#include "stm32f4_utils.h"
1112

1213
int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params);
1314
void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const int pinB, const int pinC);

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../../../hardware_api.h"
88
#include "../stm32_mcu.h"
99
#include "stm32f4_hal.h"
10+
#include "stm32f4_utils.h"
1011
#include "Arduino.h"
1112

1213

@@ -21,21 +22,10 @@ bool needs_downsample[3] = {1};
2122
// downsampling variable - per adc (3)
2223
uint8_t tim_downsample[3] = {0};
2324

24-
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
25-
if(AdcHandle->Instance == ADC1) return 0;
26-
#ifdef ADC2 // if ADC2 exists
27-
else if(AdcHandle->Instance == ADC2) return 1;
28-
#endif
29-
#ifdef ADC3 // if ADC3 exists
30-
else if(AdcHandle->Instance == ADC3) return 2;
31-
#endif
32-
return 0;
33-
}
34-
3525
void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
3626

3727
Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
38-
.pins={0},
28+
.pins={(int)NOT_SET,(int)NOT_SET,(int)NOT_SET},
3929
.adc_voltage_conv = (_ADC_VOLTAGE_F4) / (_ADC_RESOLUTION_F4)
4030
};
4131
_adc_gpio_init(cs_params, pinA,pinB,pinC);
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#include "stm32f4_utils.h"
2+
3+
#if defined(STM32F4xx)
4+
5+
/* Exported Functions */
6+
/**
7+
* @brief Return ADC HAL channel linked to a PinName
8+
* @param pin: PinName
9+
* @retval Valid HAL channel
10+
*/
11+
uint32_t _getADCChannel(PinName pin)
12+
{
13+
uint32_t function = pinmap_function(pin, PinMap_ADC);
14+
uint32_t channel = 0;
15+
switch (STM_PIN_CHANNEL(function)) {
16+
#ifdef ADC_CHANNEL_0
17+
case 0:
18+
channel = ADC_CHANNEL_0;
19+
break;
20+
#endif
21+
case 1:
22+
channel = ADC_CHANNEL_1;
23+
break;
24+
case 2:
25+
channel = ADC_CHANNEL_2;
26+
break;
27+
case 3:
28+
channel = ADC_CHANNEL_3;
29+
break;
30+
case 4:
31+
channel = ADC_CHANNEL_4;
32+
break;
33+
case 5:
34+
channel = ADC_CHANNEL_5;
35+
break;
36+
case 6:
37+
channel = ADC_CHANNEL_6;
38+
break;
39+
case 7:
40+
channel = ADC_CHANNEL_7;
41+
break;
42+
case 8:
43+
channel = ADC_CHANNEL_8;
44+
break;
45+
case 9:
46+
channel = ADC_CHANNEL_9;
47+
break;
48+
case 10:
49+
channel = ADC_CHANNEL_10;
50+
break;
51+
case 11:
52+
channel = ADC_CHANNEL_11;
53+
break;
54+
case 12:
55+
channel = ADC_CHANNEL_12;
56+
break;
57+
case 13:
58+
channel = ADC_CHANNEL_13;
59+
break;
60+
case 14:
61+
channel = ADC_CHANNEL_14;
62+
break;
63+
case 15:
64+
channel = ADC_CHANNEL_15;
65+
break;
66+
#ifdef ADC_CHANNEL_16
67+
case 16:
68+
channel = ADC_CHANNEL_16;
69+
break;
70+
#endif
71+
case 17:
72+
channel = ADC_CHANNEL_17;
73+
break;
74+
#ifdef ADC_CHANNEL_18
75+
case 18:
76+
channel = ADC_CHANNEL_18;
77+
break;
78+
#endif
79+
#ifdef ADC_CHANNEL_19
80+
case 19:
81+
channel = ADC_CHANNEL_19;
82+
break;
83+
#endif
84+
#ifdef ADC_CHANNEL_20
85+
case 20:
86+
channel = ADC_CHANNEL_20;
87+
break;
88+
case 21:
89+
channel = ADC_CHANNEL_21;
90+
break;
91+
case 22:
92+
channel = ADC_CHANNEL_22;
93+
break;
94+
case 23:
95+
channel = ADC_CHANNEL_23;
96+
break;
97+
#ifdef ADC_CHANNEL_24
98+
case 24:
99+
channel = ADC_CHANNEL_24;
100+
break;
101+
case 25:
102+
channel = ADC_CHANNEL_25;
103+
break;
104+
case 26:
105+
channel = ADC_CHANNEL_26;
106+
break;
107+
#ifdef ADC_CHANNEL_27
108+
case 27:
109+
channel = ADC_CHANNEL_27;
110+
break;
111+
case 28:
112+
channel = ADC_CHANNEL_28;
113+
break;
114+
case 29:
115+
channel = ADC_CHANNEL_29;
116+
break;
117+
case 30:
118+
channel = ADC_CHANNEL_30;
119+
break;
120+
case 31:
121+
channel = ADC_CHANNEL_31;
122+
break;
123+
#endif
124+
#endif
125+
#endif
126+
default:
127+
_Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function)));
128+
break;
129+
}
130+
return channel;
131+
}
132+
133+
134+
// timer to injected TRGO
135+
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h#L179
136+
uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
137+
if(timer->getHandle()->Instance == TIM1)
138+
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
139+
#ifdef TIM2 // if defined timer 2
140+
else if(timer->getHandle()->Instance == TIM2)
141+
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
142+
#endif
143+
#ifdef TIM4 // if defined timer 4
144+
else if(timer->getHandle()->Instance == TIM4)
145+
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
146+
#endif
147+
#ifdef TIM5 // if defined timer 5
148+
else if(timer->getHandle()->Instance == TIM5)
149+
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
150+
#endif
151+
else
152+
return _TRGO_NOT_AVAILABLE;
153+
}
154+
155+
// timer to regular TRGO
156+
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
157+
uint32_t _timerToRegularTRGO(HardwareTimer* timer){
158+
if(timer->getHandle()->Instance == TIM2)
159+
return ADC_EXTERNALTRIGCONV_T2_TRGO;
160+
#ifdef TIM3 // if defined timer 3
161+
else if(timer->getHandle()->Instance == TIM3)
162+
return ADC_EXTERNALTRIGCONV_T3_TRGO;
163+
#endif
164+
#ifdef TIM8 // if defined timer 8
165+
else if(timer->getHandle()->Instance == TIM8)
166+
return ADC_EXTERNALTRIGCONV_T8_TRGO;
167+
#endif
168+
else
169+
return _TRGO_NOT_AVAILABLE;
170+
}
171+
172+
173+
int _adcToIndex(ADC_TypeDef *AdcHandle){
174+
if(AdcHandle == ADC1) return 0;
175+
#ifdef ADC2 // if ADC2 exists
176+
else if(AdcHandle == ADC2) return 1;
177+
#endif
178+
#ifdef ADC3 // if ADC3 exists
179+
else if(AdcHandle == ADC3) return 2;
180+
#endif
181+
#ifdef ADC4 // if ADC4 exists
182+
else if(AdcHandle == ADC4) return 3;
183+
#endif
184+
#ifdef ADC5 // if ADC5 exists
185+
else if(AdcHandle == ADC5) return 4;
186+
#endif
187+
return 0;
188+
}
189+
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
190+
return _adcToIndex(AdcHandle->Instance);
191+
}
192+
193+
#endif

0 commit comments

Comments
 (0)