Skip to content

Commit 49e34e8

Browse files
committed
automatic adc selsction
1 parent 3383d57 commit 49e34e8

File tree

10 files changed

+262
-216
lines changed

10 files changed

+262
-216
lines changed

src/current_sense/hardware_specific/stm32/stm32_adc_utils.cpp

Lines changed: 203 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,173 @@
77
// here I've assumed the maximum number of ADCs is 5
88
#define ADC_COUNT 5
99

10+
11+
12+
int _adcToIndex(ADC_TypeDef *AdcHandle){
13+
if(AdcHandle == ADC1) return 0;
14+
#ifdef ADC2 // if ADC2 exists
15+
else if(AdcHandle == ADC2) return 1;
16+
#endif
17+
#ifdef ADC3 // if ADC3 exists
18+
else if(AdcHandle == ADC3) return 2;
19+
#endif
20+
#ifdef ADC4 // if ADC4 exists
21+
else if(AdcHandle == ADC4) return 3;
22+
#endif
23+
#ifdef ADC5 // if ADC5 exists
24+
else if(AdcHandle == ADC5) return 4;
25+
#endif
26+
return 0;
27+
}
28+
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
29+
return _adcToIndex(AdcHandle->Instance);
30+
}
31+
32+
33+
ADC_TypeDef* _indexToADC(uint8_t index){
34+
switch (index) {
35+
case 0:
36+
return ADC1;
37+
break;
38+
#ifdef ADC2 // if ADC2 exists
39+
case 1:
40+
return ADC2;
41+
break;
42+
#endif
43+
#ifdef ADC3 // if ADC3 exists
44+
case 2:
45+
return ADC3;
46+
break;
47+
#endif
48+
#ifdef ADC4 // if ADC4 exists
49+
case 3:
50+
return ADC4;
51+
break;
52+
#endif
53+
#ifdef ADC5 // if ADC5 exists
54+
case 4:
55+
return ADC5;
56+
break;
57+
#endif
58+
}
59+
return nullptr;
60+
}
61+
62+
int _findIndexOfEntry(PinName pin) {
63+
// remove the ALT if it is there
64+
PinName pinName = (PinName)(pinName & ~ALTX_MASK);
65+
int i = 0;
66+
SIMPLEFOC_DEBUG("STM32-CS: Looking for pin ");
67+
while (PinMap_ADC[i].pin !=NC) {
68+
if (pinName == PinMap_ADC[i].pin )
69+
return i;
70+
i++;
71+
SIMPLEFOC_DEBUG("STM32-CS: Looking for pin ", i);
72+
}
73+
return -1;
74+
}
75+
76+
int _findIndexOfLastEntry(PinName pin) {
77+
// remove the ALT if it is there
78+
PinName pinName = (PinName)(pin & ~ALTX_MASK);
79+
80+
int i = 0;
81+
while (PinMap_ADC[i].pin!=NC) {
82+
if ( pinName == (PinMap_ADC[i].pin & ~ALTX_MASK)
83+
&& pinName != (PinMap_ADC[i+1].pin & ~ALTX_MASK))
84+
return i;
85+
i++;
86+
}
87+
return -1;
88+
}
89+
int _findIndexOfFirstEntry(PinName pin) {
90+
// remove the ALT if it is there
91+
PinName pinName = (PinName)(pin & ~ALTX_MASK);
92+
int i = 0;
93+
while (PinMap_ADC[i].pin !=NC) {
94+
if (pinName == PinMap_ADC[i].pin )
95+
return i;
96+
i++;
97+
}
98+
return -1;
99+
}
100+
101+
// functions finding the index of the first pin entry in the PinMap_ADC
102+
// returns -1 if not found
103+
int _findIndexOfFirstPinMapADCEntry(int pin) {
104+
PinName pinName = digitalPinToPinName(pin);
105+
// remove the ALT if it is there
106+
return _findIndexOfFirstEntry(pinName);
107+
}
108+
109+
// functions finding the index of the last pin entry in the PinMap_ADC
110+
// returns -1 if not found
111+
int _findIndexOfLastPinMapADCEntry(int pin) {
112+
PinName pinName = digitalPinToPinName(pin);
113+
// remove the ALT if it is there
114+
return _findIndexOfLastEntry(pinName);
115+
}
116+
117+
// find the best ADC combination for the given pins
118+
// returns the index of the best ADC
119+
// each pin can be connected to multiple ADCs
120+
// the function will try to find a single ADC that can be used for all pins
121+
// if not possible it will return nullptr
122+
ADC_TypeDef* _findBestADCForPins(int numPins, int pins[]) {
123+
124+
// assuning that there is less than 8 ADCs
125+
uint8_t pins_at_adc[ADC_COUNT] = {0};
126+
127+
// check how many pins are there and are not set
128+
int no_pins = 0;
129+
for (int i = 0; i < numPins; i++) {
130+
if(_isset(pins[i])) no_pins++;
131+
}
132+
133+
// loop over all elements and count the pins connected to each ADC
134+
for (int i = 0; i < numPins; i++) {
135+
int pin = pins[i];
136+
if(!_isset(pin)) continue;
137+
138+
int index = _findIndexOfFirstPinMapADCEntry(pin);
139+
int last_index = _findIndexOfLastPinMapADCEntry(pin);
140+
if (index == -1) {
141+
return nullptr;
142+
}
143+
for (int j = index; j <= last_index; j++) {
144+
if (PinMap_ADC[j].pin == NC) {
145+
break;
146+
}
147+
int adcIndex = _adcToIndex((ADC_TypeDef*)PinMap_ADC[j].peripheral);
148+
pins_at_adc[adcIndex]++;
149+
}
150+
}
151+
152+
for (int i = 0; i < ADC_COUNT; i++) {
153+
if(!pins_at_adc[i]) continue;
154+
SimpleFOCDebug::print("STM32-CS: ADC");
155+
SimpleFOCDebug::print(i+1);
156+
SimpleFOCDebug::print(" pins: ");
157+
SimpleFOCDebug::println(pins_at_adc[i]);
158+
}
159+
160+
// now take the first ADC that has all pins connected
161+
for (int i = 0; i < ADC_COUNT; i++) {
162+
if (pins_at_adc[i] == no_pins) {
163+
return _indexToADC(i);
164+
}
165+
}
166+
return nullptr;
167+
}
168+
169+
170+
10171
/**
11172
* @brief Return ADC HAL channel linked to a PinName
12173
* @param pin: PinName
13174
* @retval Valid HAL channel
14175
*/
15-
uint32_t _getADCChannel(PinName pin)
176+
uint32_t _getADCChannelFromPinMap(PinName pin)
16177
{
17178
uint32_t function = pinmap_function(pin, PinMap_ADC);
18179
uint32_t channel = 0;
@@ -134,135 +295,60 @@ uint32_t _getADCChannel(PinName pin)
134295
return channel;
135296
}
136297

298+
/**
299+
* @brief Return ADC HAL channel linked to a PinName and the ADC handle
300+
* @param pin: PinName
301+
* @param AdcHandle: ADC_HandleTypeDef a pointer to the ADC handle
302+
* @retval Valid HAL channel
303+
*/
304+
uint32_t _getADCChannel(PinName pin, ADC_TypeDef *AdcHandle )
305+
{
306+
if (AdcHandle == NP) {
307+
return _getADCChannelFromPinMap(pin);
308+
}
309+
// find the PinName that corresponds to the ADC
310+
int first_ind = _findIndexOfFirstEntry(pin);
311+
int last_ind = _findIndexOfLastEntry(pin);
312+
if (first_ind == -1 || last_ind == -1) {
313+
_Error_Handler("ADC: Pin not found in PinMap_ADC", (int)pin);
314+
}
315+
// find the channel
316+
uint32_t channel = 0;
317+
for (int i = first_ind; i <= last_ind; i++) {
318+
if (PinMap_ADC[i].peripheral == AdcHandle) {
319+
channel =_getADCChannelFromPinMap(PinMap_ADC[i].pin);
137320

138-
int _adcToIndex(ADC_TypeDef *AdcHandle){
139-
if(AdcHandle == ADC1) return 0;
140-
#ifdef ADC2 // if ADC2 exists
141-
else if(AdcHandle == ADC2) return 1;
142-
#endif
143-
#ifdef ADC3 // if ADC3 exists
144-
else if(AdcHandle == ADC3) return 2;
145-
#endif
146-
#ifdef ADC4 // if ADC4 exists
147-
else if(AdcHandle == ADC4) return 3;
148-
#endif
149-
#ifdef ADC5 // if ADC5 exists
150-
else if(AdcHandle == ADC5) return 4;
151-
#endif
152-
return 0;
153-
}
154-
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
155-
return _adcToIndex(AdcHandle->Instance);
156-
}
321+
// #ifdef SIMPLEFOC_STM32_DEBUG
322+
SimpleFOCDebug::print("STM32-CS: Configuring ADC");
323+
SimpleFOCDebug::print(_adcToIndex(AdcHandle)+1);
324+
SimpleFOCDebug::print(" channel: ");
325+
SimpleFOCDebug::println((int)STM_PIN_CHANNEL(pinmap_function(PinMap_ADC[i].pin, PinMap_ADC)));
326+
// #endif
157327

328+
break;
329+
}
330+
}
331+
return channel;
332+
}
158333

159-
ADC_TypeDef* _indexToADC(uint8_t index){
160-
switch (index) {
334+
uint32_t _getADCInjectedRank(uint8_t ind){
335+
switch (ind) {
161336
case 0:
162-
return ADC1;
337+
return ADC_INJECTED_RANK_1;
163338
break;
164-
#ifdef ADC2 // if ADC2 exists
165339
case 1:
166-
return ADC2;
340+
return ADC_INJECTED_RANK_2;
167341
break;
168-
#endif
169-
#ifdef ADC3 // if ADC3 exists
170342
case 2:
171-
return ADC3;
343+
return ADC_INJECTED_RANK_3;
172344
break;
173-
#endif
174-
#ifdef ADC4 // if ADC4 exists
175345
case 3:
176-
return ADC4;
346+
return ADC_INJECTED_RANK_4;
177347
break;
178-
#endif
179-
#ifdef ADC5 // if ADC5 exists
180-
case 4:
181-
return ADC5;
348+
default:
349+
return 0;
182350
break;
183-
#endif
184-
}
185-
return nullptr;
186-
}
187-
188-
189-
// functions finding the index of the first pin entry in the PinMap_ADC
190-
// returns -1 if not found
191-
int _findIndexOfFirstPinMapADCEntry(int pin) {
192-
PinName pinName = digitalPinToPinName(pin);
193-
int i = 0;
194-
while ((PinMap_ADC[i].pin & ~ALTX_MASK) !=NC) {
195-
if (pinName == (PinMap_ADC[i].pin & ~ALTX_MASK))
196-
return i;
197-
i++;
198351
}
199-
return -1;
200-
}
201-
202-
// functions finding the index of the last pin entry in the PinMap_ADC
203-
// returns -1 if not found
204-
int _findIndexOfLastPinMapADCEntry(int pin) {
205-
PinName pinName = digitalPinToPinName(pin);
206-
int i = 0;
207-
while (PinMap_ADC[i].pin!=NC) {
208-
if ( pinName == (PinMap_ADC[i].pin & ~ALTX_MASK)
209-
&& pinName != (PinMap_ADC[i+1].pin & ~ALTX_MASK))
210-
return i;
211-
i++;
212-
}
213-
return -1;
214-
}
215-
216-
// find the best ADC combination for the given pins
217-
// returns the index of the best ADC
218-
// each pin can be connected to multiple ADCs
219-
// the function will try to find a single ADC that can be used for all pins
220-
// if not possible it will return nullptr
221-
ADC_TypeDef* _findBestADCForPins(int numPins, int pins[]) {
222-
223-
// assuning that there is less than 8 ADCs
224-
uint8_t pins_at_adc[ADC_COUNT] = {0};
225-
226-
// check how many pins are there and are not set
227-
int no_pins = 0;
228-
for (int i = 0; i < numPins; i++) {
229-
if(_isset(pins[i])) no_pins++;
230-
}
231-
232-
// loop over all elements and count the pins connected to each ADC
233-
for (int i = 0; i < numPins; i++) {
234-
int pin = pins[i];
235-
if(!_isset(pin)) continue;
236-
237-
int index = _findIndexOfFirstPinMapADCEntry(pin);
238-
int last_index = _findIndexOfLastPinMapADCEntry(pin);
239-
if (index == -1) {
240-
return nullptr;
241-
}
242-
for (int j = index; j <= last_index; j++) {
243-
if (PinMap_ADC[j].pin == NC) {
244-
break;
245-
}
246-
int adcIndex = _adcToIndex((ADC_TypeDef*)PinMap_ADC[j].peripheral);
247-
pins_at_adc[adcIndex]++;
248-
}
249-
}
250-
251-
for (int i = 0; i < ADC_COUNT; i++) {
252-
if(!pins_at_adc[i]) continue;
253-
SimpleFOCDebug::print("STM32-CS: ADC");
254-
SimpleFOCDebug::print(i+1);
255-
SimpleFOCDebug::print(" pins: ");
256-
SimpleFOCDebug::println(pins_at_adc[i]);
257-
}
258-
259-
// now take the first ADC that has all pins connected
260-
for (int i = 0; i < ADC_COUNT; i++) {
261-
if (pins_at_adc[i] == no_pins) {
262-
return _indexToADC(i);
263-
}
264-
}
265-
return nullptr;
266352
}
267353

268354
#endif

src/current_sense/hardware_specific/stm32/stm32_adc_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
/**
1515
* @brief Return ADC HAL channel linked to a PinName
1616
* @param pin: PinName
17+
* @param adc: ADC_TypeDef a pointer to the ADC handle
1718
* @retval Valid HAL channel
1819
*/
19-
uint32_t _getADCChannel(PinName pin);
20+
uint32_t _getADCChannel(PinName pin, ADC_TypeDef* adc = NP);
21+
uint32_t _getADCInjectedRank(uint8_t ind);
2022

2123
// timer to injected TRGO - architecure specific
2224
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer);
@@ -28,9 +30,6 @@ uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer);
2830
int _adcToIndex(ADC_HandleTypeDef *AdcHandle);
2931
int _adcToIndex(ADC_TypeDef *AdcHandle);
3032

31-
32-
33-
3433
// functions helping to find the best ADC channel
3534
int _findIndexOfFirstPinMapADCEntry(int pin);
3635
int _findIndexOfLastPinMapADCEntry(int pin);

0 commit comments

Comments
 (0)