Skip to content

Commit 2857d3e

Browse files
committed
h7 update for auto-search
1 parent 7cb5a17 commit 2857d3e

File tree

5 files changed

+21
-215
lines changed

5 files changed

+21
-215
lines changed

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//#define SIMPLEFOC_STM32_DEBUG
66

77
#include "../../../../communication/SimpleFOCDebug.h"
8-
#define _TRGO_NOT_AVAILABLE 12345
98

109
ADC_HandleTypeDef hadc;
1110

@@ -21,23 +20,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
2120
{
2221
ADC_InjectionConfTypeDef sConfigInjected;
2322

24-
// check if all pins belong to the same ADC
25-
ADC_TypeDef* adc_pin1 = _isset(cs_params->pins[0]) ? (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC) : nullptr;
26-
ADC_TypeDef* adc_pin2 = _isset(cs_params->pins[1]) ? (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[1]), PinMap_ADC) : nullptr;
27-
ADC_TypeDef* adc_pin3 = _isset(cs_params->pins[2]) ? (ADC_TypeDef*)pinmap_peripheral(analogInputToPinName(cs_params->pins[2]), PinMap_ADC) : nullptr;
28-
if ( ((adc_pin1 != adc_pin2) && (adc_pin1 && adc_pin2)) ||
29-
((adc_pin2 != adc_pin3) && (adc_pin2 && adc_pin3)) ||
30-
((adc_pin1 != adc_pin3) && (adc_pin1 && adc_pin3))
31-
){
32-
#ifdef SIMPLEFOC_STM32_DEBUG
33-
SIMPLEFOC_DEBUG("STM32-CS: ERR: Analog pins dont belong to the same ADC!");
34-
#endif
35-
return -1;
36-
}
3723

3824
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
3925
*/
40-
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC);
26+
hadc.Instance = _findBestADCForPins(3, cs_params->pins);
4127

4228
if(hadc.Instance == ADC1) __HAL_RCC_ADC12_CLK_ENABLE();
4329
#ifdef ADC2 // if defined ADC2
@@ -48,7 +34,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
4834
#endif
4935
else{
5036
#ifdef SIMPLEFOC_STM32_DEBUG
51-
SIMPLEFOC_DEBUG("STM32-CS: ERR: Pin does not belong to any ADC!");
37+
SIMPLEFOC_DEBUG("STM32-CS: ERR: Cannot find a common ADC for the pins!");
5238
#endif
5339
return -1; // error not a valid ADC instance
5440
}
@@ -91,7 +77,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
9177

9278
/**Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
9379
*/
94-
sConfigInjected.InjectedNbrOfConversion = _isset(cs_params->pins[2]) ? 3 : 2;
80+
sConfigInjected.InjectedNbrOfConversion = 0;
81+
for(int pin_no=0; pin_no<3; pin_no++){
82+
if(_isset(cs_params->pins[pin_no])){
83+
sConfigInjected.InjectedNbrOfConversion++;
84+
}
85+
}
9586
// if ADC1 or ADC2
9687
if(hadc.Instance == ADC1 || hadc.Instance == ADC2){
9788
// more info here: https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_adc.h#L658
@@ -147,21 +138,21 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
147138
#endif
148139
}
149140

150-
uint32_t ranks[4] = {ADC_INJECTED_RANK_1, ADC_INJECTED_RANK_2, ADC_INJECTED_RANK_3, ADC_INJECTED_RANK_4};
141+
uint8_t channel_no = 0;
151142
for(int i=0; i<3; i++){
152143
// skip if not set
153-
if (!_isset(cs_params->pins[i])) continue;
154-
sConfigInjected.InjectedRank = ranks[i];
155-
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[i]));
144+
if (!_isset(cs_params->pins[i])) continue;
145+
146+
sConfigInjected.InjectedRank = _getADCInjectedRank(channel_no++);
147+
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[i]), hadc.Instance);
156148
if (HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected) != HAL_OK){
157149
#ifdef SIMPLEFOC_STM32_DEBUG
158-
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[i])) );
150+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[i]) , hadc.Instance));
159151
#endif
160152
return -1;
161153
}
162154
}
163155

164-
165156
delay(1000);
166157
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
167158
// enable interrupt

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#if defined(STM32H7xx)
66
#include "stm32h7xx_hal.h"
7+
#include "../stm32_adc_utils.h"
78
#include "../stm32_mcu.h"
8-
#include "stm32h7_utils.h"
99

1010
int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params);
1111
int _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const int pinB, const int pinC);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
77
#include "../../../hardware_api.h"
88
#include "../stm32_mcu.h"
9+
#include "../stm32_adc_utils.h"
910
#include "stm32h7_hal.h"
10-
#include "stm32h7_utils.h"
1111
#include "Arduino.h"
1212

1313

@@ -83,16 +83,18 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
8383

8484
// function reading an ADC value and returning the read voltage
8585
float _readADCVoltageLowSide(const int pin, const void* cs_params){
86+
uint8_t channel_no = 0;
8687
for(int i=0; i < 3; i++){
8788
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
8889
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
89-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
90+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][channel_no] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
9091
#else
9192
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
92-
uint32_t channel = (i == 0) ? ADC_INJECTED_RANK_1 : (i == 1) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;
93+
uint32_t channel = _getADCInjectedRank(channel_no);
9394
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
9495
#endif
9596
}
97+
if(_isset(((Stm32CurrentSenseParams*)cs_params)->pins[i])) channel_no++;
9698
}
9799
return 0;
98100
}
Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,10 @@
1-
#include "stm32h7_utils.h"
1+
#include "../stm32_adc_utils.h"
22

33
#if defined(STM32H7xx)
44

55
/* Exported Functions */
66

77

8-
PinName analog_to_pin(uint32_t pin) {
9-
PinName pin_name = analogInputToPinName(pin);
10-
if (pin_name == NC) {
11-
return (PinName) pin;
12-
}
13-
return pin_name;
14-
}
15-
16-
17-
/**
18-
* @brief Return ADC HAL channel linked to a PinName
19-
* @param pin: PinName
20-
* @retval Valid HAL channel
21-
*/
22-
uint32_t _getADCChannel(PinName pin)
23-
{
24-
uint32_t function = pinmap_function(pin, PinMap_ADC);
25-
uint32_t channel = 0;
26-
switch (STM_PIN_CHANNEL(function)) {
27-
#ifdef ADC_CHANNEL_0
28-
case 0:
29-
channel = ADC_CHANNEL_0;
30-
break;
31-
#endif
32-
case 1:
33-
channel = ADC_CHANNEL_1;
34-
break;
35-
case 2:
36-
channel = ADC_CHANNEL_2;
37-
break;
38-
case 3:
39-
channel = ADC_CHANNEL_3;
40-
break;
41-
case 4:
42-
channel = ADC_CHANNEL_4;
43-
break;
44-
case 5:
45-
channel = ADC_CHANNEL_5;
46-
break;
47-
case 6:
48-
channel = ADC_CHANNEL_6;
49-
break;
50-
case 7:
51-
channel = ADC_CHANNEL_7;
52-
break;
53-
case 8:
54-
channel = ADC_CHANNEL_8;
55-
break;
56-
case 9:
57-
channel = ADC_CHANNEL_9;
58-
break;
59-
case 10:
60-
channel = ADC_CHANNEL_10;
61-
break;
62-
case 11:
63-
channel = ADC_CHANNEL_11;
64-
break;
65-
case 12:
66-
channel = ADC_CHANNEL_12;
67-
break;
68-
case 13:
69-
channel = ADC_CHANNEL_13;
70-
break;
71-
case 14:
72-
channel = ADC_CHANNEL_14;
73-
break;
74-
case 15:
75-
channel = ADC_CHANNEL_15;
76-
break;
77-
#ifdef ADC_CHANNEL_16
78-
case 16:
79-
channel = ADC_CHANNEL_16;
80-
break;
81-
#endif
82-
case 17:
83-
channel = ADC_CHANNEL_17;
84-
break;
85-
#ifdef ADC_CHANNEL_18
86-
case 18:
87-
channel = ADC_CHANNEL_18;
88-
break;
89-
#endif
90-
#ifdef ADC_CHANNEL_19
91-
case 19:
92-
channel = ADC_CHANNEL_19;
93-
break;
94-
#endif
95-
#ifdef ADC_CHANNEL_20
96-
case 20:
97-
channel = ADC_CHANNEL_20;
98-
break;
99-
case 21:
100-
channel = ADC_CHANNEL_21;
101-
break;
102-
case 22:
103-
channel = ADC_CHANNEL_22;
104-
break;
105-
case 23:
106-
channel = ADC_CHANNEL_23;
107-
break;
108-
#ifdef ADC_CHANNEL_24
109-
case 24:
110-
channel = ADC_CHANNEL_24;
111-
break;
112-
case 25:
113-
channel = ADC_CHANNEL_25;
114-
break;
115-
case 26:
116-
channel = ADC_CHANNEL_26;
117-
break;
118-
#ifdef ADC_CHANNEL_27
119-
case 27:
120-
channel = ADC_CHANNEL_27;
121-
break;
122-
case 28:
123-
channel = ADC_CHANNEL_28;
124-
break;
125-
case 29:
126-
channel = ADC_CHANNEL_29;
127-
break;
128-
case 30:
129-
channel = ADC_CHANNEL_30;
130-
break;
131-
case 31:
132-
channel = ADC_CHANNEL_31;
133-
break;
134-
#endif
135-
#endif
136-
#endif
137-
default:
138-
_Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function)));
139-
break;
140-
}
141-
return channel;
142-
}
143-
1448
// timer to injected TRGO
1459
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_adc_ex.h#L235
14610
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
@@ -216,25 +80,4 @@ uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
21680
return _TRGO_NOT_AVAILABLE;
21781
}
21882

219-
220-
int _adcToIndex(ADC_TypeDef *AdcHandle){
221-
if(AdcHandle == ADC1) return 0;
222-
#ifdef ADC2 // if ADC2 exists
223-
else if(AdcHandle == ADC2) return 1;
224-
#endif
225-
#ifdef ADC3 // if ADC3 exists
226-
else if(AdcHandle == ADC3) return 2;
227-
#endif
228-
#ifdef ADC4 // if ADC4 exists
229-
else if(AdcHandle == ADC4) return 3;
230-
#endif
231-
#ifdef ADC5 // if ADC5 exists
232-
else if(AdcHandle == ADC5) return 4;
233-
#endif
234-
return 0;
235-
}
236-
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
237-
return _adcToIndex(AdcHandle->Instance);
238-
}
239-
24083
#endif

src/current_sense/hardware_specific/stm32/stm32h7/stm32h7_utils.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)