Skip to content

Commit 3462d44

Browse files
fpistmMCUdude
andcommitted
Allow non contiguous analog pins definition
Signed-off-by: Frederic Pillon <[email protected]> Co-Authored-By: MCUdude <[email protected]>
1 parent 6d9f40e commit 3462d44

File tree

3 files changed

+90
-99
lines changed

3 files changed

+90
-99
lines changed

cores/arduino/pins_arduino.h

+39-7
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,30 @@ enum {
5151
};
5252

5353
// Arduino analog pins
54+
5455
#ifndef NUM_ANALOG_INPUTS
5556
#define NUM_ANALOG_INPUTS 0
5657
#endif
58+
59+
// If NUM_ANALOG_FIRST is not defined:
60+
// - Ax are not contiguous in the digitalPin array
61+
// - analogInPin array is available
5762
#ifndef NUM_ANALOG_FIRST
58-
#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS
63+
#define NUM_ANALOG_FIRST (NUM_DIGITAL_PINS + 1)
64+
#define NUM_ANALOG_LAST (NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS)
65+
#define NUM_ANALOG_INTERNAL_FIRST (NUM_ANALOG_LAST + 1)
66+
#else
67+
#define NUM_ANALOG_INTERNAL_FIRST (NUM_DIGITAL_PINS + 1)
5968
#endif
6069

70+
// If NUM_ANALOG_INPUTS is not defined there is no analog pins defined.
71+
// Anyway ADC internal channels are always avaialable.
72+
#if NUM_ANALOG_INPUTS > 0
6173
// Analog pins must be contiguous to be able to loop on each value
6274
#define MAX_ANALOG_INPUTS 24
6375
_Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS,
6476
"Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS");
77+
6578
_Static_assert(NUM_ANALOG_FIRST >= NUM_ANALOG_INPUTS,
6679
"First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS");
6780

@@ -161,6 +174,7 @@ static const uint8_t A22 = PIN_A22;
161174
#define PIN_A23 (PIN_A22 + 1)
162175
static const uint8_t A23 = PIN_A23;
163176
#endif
177+
#endif /* NUM_ANALOG_INPUTS > 0 */
164178

165179
// Default for Arduino connector compatibility
166180
// SPI Definitions
@@ -208,38 +222,56 @@ static const uint8_t SCL = PIN_WIRE_SCL;
208222
// ADC internal channels (not a pins)
209223
// Only used for analogRead()
210224
#if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1)
211-
#define ATEMP (NUM_DIGITAL_PINS + 1)
225+
#define ATEMP (NUM_ANALOG_INTERNAL_FIRST)
212226
#endif
213227
#ifdef ADC_CHANNEL_VREFINT
214-
#define AVREF (NUM_DIGITAL_PINS + 2)
228+
#define AVREF (NUM_ANALOG_INTERNAL_FIRST + 2)
215229
#endif
216230
#ifdef ADC_CHANNEL_VBAT
217-
#define AVBAT (NUM_DIGITAL_PINS + 3)
231+
#define AVBAT (NUM_ANALOG_INTERNAL_FIRST + 3)
218232
#endif
219233
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)
220-
#define ATEMP_ADC5 (NUM_DIGITAL_PINS + 4)
234+
#define ATEMP_ADC5 (NUM_ANALOG_INTERNAL_FIRST + 4)
221235
#endif
222236

223237
#ifdef __cplusplus
224238
extern "C" {
225239
#endif
226240
extern const PinName digitalPin[];
241+
extern const uint32_t analogInPin[];
227242

228243
#define NOT_AN_INTERRUPT NC // -1
229244

230245
// Convert a digital pin number Dxx to a PinName PX_n
231246
// Note: Analog pin is also a digital pin.
247+
#ifndef NUM_ANALOG_LAST
232248
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : NC)
249+
#else
250+
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : \
251+
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
252+
digitalPin[analogInPin[p-NUM_ANALOG_FIRST]] : NC)
253+
#endif
233254
// Convert a PinName PX_n to a digital pin number
234255
uint32_t pinNametoDigitalPin(PinName p);
235256

236257
// Convert an analog pin number to a digital pin number
237-
#if defined(NUM_ANALOG_INPUTS) && (NUM_ANALOG_INPUTS>0)
258+
#if NUM_ANALOG_INPUTS > 0
238259
// Used by analogRead api to have A0 == 0
260+
// For contiguous analog pins definition in digitalPin array
261+
#ifndef NUM_ANALOG_LAST
239262
#define analogInputToDigitalPin(p) (((uint32_t)p < NUM_ANALOG_INPUTS) ? (p+A0) : p)
240263
#else
264+
// For non contiguous analog pins definition in digitalPin array
265+
#define analogInputToDigitalPin(p) ( \
266+
((uint32_t)p < NUM_ANALOG_INPUTS) ? analogInPin[p] : \
267+
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
268+
analogInPin[p-NUM_ANALOG_FIRST] : p)
269+
#endif // !NUM_ANALOG_LAST
270+
#else
271+
// No analog pin defined
241272
#define analogInputToDigitalPin(p) (NUM_DIGITAL_PINS)
242-
#endif
273+
#endif // NUM_ANALOG_INPUTS > 0
274+
243275
// Convert an analog pin number Axx to a PinName PX_n
244276
PinName analogInputToPinName(uint32_t pin);
245277

variants/board_template/variant.cpp

+24-82
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
extern "C" {
1818
#endif
1919

20-
// Pin number
20+
// Digital PinName array
2121
// This array allows to wrap Arduino pin number(Dx or x)
2222
// to STM32 PinName (PX_n)
2323
const PinName digitalPin[] = {
@@ -38,89 +38,31 @@ const PinName digitalPin[] = {
3838
P, //D13
3939
P, //D14
4040
P, //D15
41-
P, //D16
42-
P, //D17
43-
P, //D18
44-
P, //D19
45-
P, //D20
46-
P, //D21
47-
P, //D22
48-
P, //D23
49-
P, //D24
50-
P, //D25
51-
P, //D26
52-
P, //D27
53-
P, //D28
54-
P, //D29
55-
P, //D30
56-
P, //D31
57-
P, //D32
58-
P, //D33
59-
P, //D34
60-
P, //D35
61-
P, //D36
62-
P, //D37
63-
P, //D38
64-
P, //D39
65-
P, //D40
66-
P, //D41
67-
P, //D42
68-
P, //D43
69-
P, //D44
70-
P, //D45
71-
P, //D46
72-
P, //D47
73-
P, //D48
74-
P, //D49
75-
P, //D50
76-
P, //D51
77-
P, //D52
78-
P, //D53
79-
P, //D54
80-
P, //D55
81-
P, //D56
82-
P, //D57
83-
P, //D58
84-
P, //D59
85-
P, //D60
86-
P, //D61
87-
P, //D62
88-
P, //D63
89-
P, //D64
90-
P, //D65
91-
P, //D66
92-
P, //D67
93-
P, //D68
94-
P, //D69
95-
P, //D70
96-
P, //D71
97-
P, //D72
98-
P, //D73
99-
P, //D74
100-
P, //D75
101-
P, //D76
102-
P, //D77
103-
P, //D78/A0
104-
P, //D79/A1
105-
P, //D80/A2
106-
P, //D81/A3
107-
P, //D82/A4
108-
P, //D83/A5
109-
P, //D84/A6
110-
P, //D85/A7
111-
P, //D86/A8
112-
P, //D87/A9
41+
P, //D16/A0
42+
P, //D17/A1
43+
P, //D18/A2
44+
P, //D19/A3
45+
P, //D20/A4
46+
P, //D21/A5
47+
// Required only if Ax pins are automaticaly defined using `NUM_ANALOG_FIRST`
48+
// and have to be contiguous in this array
11349
// Duplicated pins in order to be aligned with PinMap_ADC
114-
P, //D88/A10 = D
115-
P, //D89/A11 = D
116-
P, //D90/A12 = D
117-
P, //D91/A13 = D
118-
P, //D92/A14 = D
119-
P, //D93/A15 = D
120-
P, //D94/A16 = D
121-
P //D95/A17 = D
50+
P, //D22/A6 = D
51+
P, //D23/A7 = D
52+
P //D24/A8 = D
12253
};
12354

55+
// If analog pins are not contiguous in the digitalPin array:
56+
// Add the analogInPin array without defining NUM_ANALOG_FIRST
57+
// Analog (Ax) pin number array
58+
// where x is the index to retrieve the digital pin number
59+
const uint32_t analogInPin[] = {
60+
//PXn, //Ax = Dx
61+
2, //A0 = Dx
62+
8, //A1 = Dy
63+
3 //A2 = Dz
64+
}
65+
12466
#ifdef __cplusplus
12567
}
12668
#endif
@@ -143,7 +85,7 @@ WEAK void SystemClock_Config(void)
14385
// available in src/main.c
14486
// or
14587
// copied from a STM32CubeYY project examples
146-
// where 'YY' could be F0, F1, F2, F3, F4, F7, G0, G4, H7, L0, L1, L4, WB
88+
// where 'YY' could be F0, F1, F2, F3, F4, F7, G0, G4, H7, L0, L1, L4, MP1, WB
14789
}
14890

14991
#ifdef __cplusplus

variants/board_template/variant.h

+27-10
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ extern "C" {
3535
// !!! // x is PXn (y)
3636
// !!! Ex:
3737
// !!! ...
38-
// !!! #define PA4 20 // A14
38+
// !!! #define PA4 20 // A14 <-- if NUM_ANALOG_FIRST not defined
39+
// !!! or
40+
// !!! #define PA4 A14 // 20 <-- if NUM_ANALOG_FIRST defined
3941
// !!! #define PB4 21
4042
// !!! #define PB5 22
4143
// !!! #define PB3 23
4244
// !!! // 24 is PA4 (20)
4345
// !!! // 25 is PB4 (21)
44-
// !!! #define PA2 26 // A15
46+
// !!! #define PA2 26 // A15 <-- if NUM_ANALOG_FIRST not defined
47+
// !!! or
48+
// !!! #define PA2 A15 // 26 <-- if NUM_ANALOG_FIRST defined
4549
// !!! ...
4650
//#define PXn x
4751

@@ -51,22 +55,35 @@ extern "C" {
5155
#define NUM_DIGITAL_PINS 0
5256

5357
// Allow to define Arduino style alias for analog input pin number --> Ax
54-
// If no analog pin required then NUM_ANALOG_INPUTS and NUM_ANALOG_FIRST
55-
// could not be defined or set to respectively `0` and `NUM_DIGITAL_PINS`
56-
// All pins are digital, analog inputs are a subset of digital pins
57-
// and must be contiguous to be able to loop on each value
58-
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
59-
// defined in pin_arduino.h
58+
// If no analog pin required then NUM_ANALOG_INPUTS could not be defined
59+
// or set to `0`
60+
// All pins are digital, analog inputs are a subset of digital pins.
61+
// This must be a literal
6062
// It is used with preprocessor tests (e.g. #if NUM_ANALOG_INPUTS > 3)
6163
// so an enum will not work.
6264
// !!!
6365
// !!! It must be aligned with the number of analog PinName
6466
// !!! defined in digitalPin[] array in variant.cpp
6567
// !!!
6668
#define NUM_ANALOG_INPUTS 0
67-
// Define digital pin number of the first analog input (i.e. which digital pin is A0)
68-
// First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS
69+
70+
// They are 2 possibles way to define analog pins:
71+
//-------------------------------------------------------------------------------------------
72+
// - If they are contiguous in the digitalPin array:
73+
// Simply defined `NUM_ANALOG_FIRST` and all pins Ax will be automatically defined.
74+
// It define the digital pin number of the first analog input (i.e. which digital pin is A0)
75+
// First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS
76+
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
77+
// defined in pin_arduino.h
6978
#define NUM_ANALOG_FIRST 0
79+
//------------------------------------OR------------------------------------------------------
80+
// - If they are not contiguous in the digitalPin array:
81+
// Add an analogInPin array in the variant.cpp without defining NUM_ANALOG_FIRST
82+
// In that case the defined PYn for analog pin have to define the Ax definition instead of
83+
// index in digitalPin[] array:
84+
// #define PA4 A14
85+
//-------------------------------------------------------------------------------------------
86+
7087

7188
// Below ADC, DAC and PWM definitions already done in the core
7289
// Could be redefined here if needed

0 commit comments

Comments
 (0)