Skip to content

Commit 370ad02

Browse files
authored
Merge pull request #800 from fpistm/Analog_pins_def
Analog pins definition clean up
2 parents 345a060 + 3c8e21d commit 370ad02

File tree

66 files changed

+1182
-959
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1182
-959
lines changed

Diff for: cores/arduino/pins_arduino.c

+36
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,42 @@ PinName analogInputToPinName(uint32_t pin)
6767
return pn;
6868
}
6969

70+
bool pinIsAnalogInput(uint32_t pin)
71+
{
72+
bool ret = false;
73+
#if NUM_ANALOG_INPUTS > 0
74+
#ifndef NUM_ANALOG_LAST
75+
ret = (pin >= A0) && (pin < (A0 + NUM_ANALOG_INPUTS));
76+
#else
77+
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
78+
if (analogInPin[i] == pin) {
79+
ret = true;
80+
break;
81+
}
82+
}
83+
#endif /* NUM_ANALOG_LAST */
84+
#endif /* NUM_ANALOG_INPUTS > 0 */
85+
return ret;
86+
}
87+
88+
uint32_t digitalPinToAnalogInput(uint32_t pin)
89+
{
90+
uint32_t ret = NUM_ANALOG_INPUTS;
91+
#if NUM_ANALOG_INPUTS > 0
92+
#ifndef NUM_ANALOG_LAST
93+
ret = pin - A0;
94+
#else
95+
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
96+
if (analogInPin[i] == pin) {
97+
ret = i;
98+
break;
99+
}
100+
}
101+
#endif /* NUM_ANALOG_LAST */
102+
#endif /* NUM_ANALOG_INPUTS > 0 */
103+
return ret;
104+
}
105+
70106
#ifdef __cplusplus
71107
}
72108
#endif

Diff for: cores/arduino/pins_arduino.h

+45-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#ifndef _PINS_ARDUINO_H_
1919
#define _PINS_ARDUINO_H_
20+
#include <stdbool.h>
2021
#include <stdlib.h> /* Required for static_assert */
2122
// Include board variant
2223
#include "variant.h"
@@ -51,23 +52,33 @@ enum {
5152
};
5253

5354
// Arduino analog pins
55+
5456
#ifndef NUM_ANALOG_INPUTS
5557
#define NUM_ANALOG_INPUTS 0
5658
#endif
59+
60+
// If NUM_ANALOG_FIRST is not defined:
61+
// - Ax are not contiguous in the digitalPin array
62+
// - analogInPin array is available
5763
#ifndef NUM_ANALOG_FIRST
58-
#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS
64+
#define NUM_ANALOG_FIRST (NUM_DIGITAL_PINS + 1)
65+
#define NUM_ANALOG_LAST (NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS)
66+
#define NUM_ANALOG_INTERNAL_FIRST (NUM_ANALOG_LAST + 1)
67+
#else
68+
#define NUM_ANALOG_INTERNAL_FIRST (NUM_DIGITAL_PINS + 1)
5969
#endif
6070

71+
// If NUM_ANALOG_INPUTS is not defined there is no analog pins defined.
72+
// Anyway ADC internal channels are always avaialable.
73+
#if NUM_ANALOG_INPUTS > 0
6174
// Analog pins must be contiguous to be able to loop on each value
6275
#define MAX_ANALOG_INPUTS 24
6376
_Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS,
6477
"Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS");
78+
6579
_Static_assert(NUM_ANALOG_FIRST >= NUM_ANALOG_INPUTS,
6680
"First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS");
6781

68-
// Defined for backward compatibility with Firmata which unfortunately use it
69-
#define AEND (NUM_ANALOG_FIRST+NUM_ANALOG_INPUTS)
70-
7182
#if NUM_ANALOG_INPUTS > 0
7283
#define PIN_A0 NUM_ANALOG_FIRST
7384
static const uint8_t A0 = PIN_A0;
@@ -164,6 +175,7 @@ static const uint8_t A22 = PIN_A22;
164175
#define PIN_A23 (PIN_A22 + 1)
165176
static const uint8_t A23 = PIN_A23;
166177
#endif
178+
#endif /* NUM_ANALOG_INPUTS > 0 */
167179

168180
// Default for Arduino connector compatibility
169181
// SPI Definitions
@@ -211,38 +223,56 @@ static const uint8_t SCL = PIN_WIRE_SCL;
211223
// ADC internal channels (not a pins)
212224
// Only used for analogRead()
213225
#if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1)
214-
#define ATEMP (NUM_DIGITAL_PINS + 1)
226+
#define ATEMP (NUM_ANALOG_INTERNAL_FIRST)
215227
#endif
216228
#ifdef ADC_CHANNEL_VREFINT
217-
#define AVREF (NUM_DIGITAL_PINS + 2)
229+
#define AVREF (NUM_ANALOG_INTERNAL_FIRST + 2)
218230
#endif
219231
#ifdef ADC_CHANNEL_VBAT
220-
#define AVBAT (NUM_DIGITAL_PINS + 3)
232+
#define AVBAT (NUM_ANALOG_INTERNAL_FIRST + 3)
221233
#endif
222234
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)
223-
#define ATEMP_ADC5 (NUM_DIGITAL_PINS + 4)
235+
#define ATEMP_ADC5 (NUM_ANALOG_INTERNAL_FIRST + 4)
224236
#endif
225237

226238
#ifdef __cplusplus
227239
extern "C" {
228240
#endif
229241
extern const PinName digitalPin[];
242+
extern const uint32_t analogInPin[];
230243

231244
#define NOT_AN_INTERRUPT NC // -1
232245

233246
// Convert a digital pin number Dxx to a PinName PX_n
234247
// Note: Analog pin is also a digital pin.
248+
#ifndef NUM_ANALOG_LAST
235249
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : NC)
250+
#else
251+
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : \
252+
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
253+
digitalPin[analogInPin[p-NUM_ANALOG_FIRST]] : NC)
254+
#endif
236255
// Convert a PinName PX_n to a digital pin number
237256
uint32_t pinNametoDigitalPin(PinName p);
238257

239258
// Convert an analog pin number to a digital pin number
240-
#if defined(NUM_ANALOG_INPUTS) && (NUM_ANALOG_INPUTS>0)
259+
#if NUM_ANALOG_INPUTS > 0
241260
// Used by analogRead api to have A0 == 0
261+
// For contiguous analog pins definition in digitalPin array
262+
#ifndef NUM_ANALOG_LAST
242263
#define analogInputToDigitalPin(p) (((uint32_t)p < NUM_ANALOG_INPUTS) ? (p+A0) : p)
243264
#else
265+
// For non contiguous analog pins definition in digitalPin array
266+
#define analogInputToDigitalPin(p) ( \
267+
((uint32_t)p < NUM_ANALOG_INPUTS) ? analogInPin[p] : \
268+
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
269+
analogInPin[p-NUM_ANALOG_FIRST] : p)
270+
#endif // !NUM_ANALOG_LAST
271+
#else
272+
// No analog pin defined
244273
#define analogInputToDigitalPin(p) (NUM_DIGITAL_PINS)
245-
#endif
274+
#endif // NUM_ANALOG_INPUTS > 0
275+
246276
// Convert an analog pin number Axx to a PinName PX_n
247277
PinName analogInputToPinName(uint32_t pin);
248278

@@ -294,12 +324,15 @@ PinName analogInputToPinName(uint32_t pin);
294324
// return first occurence of linked PinName (PYx)
295325
#define digitalPinFirstOccurence(p) (pinNametoDigitalPin(digitalPinToPinName(p)))
296326

297-
// Specific for Firmata. As some pins could be duplicated,
298-
// ensure 'p' is not one of the serial pins
327+
// Specific for Firmata.
328+
// Some pins could be duplicated, ensure 'p' is not one of the serial pins
299329
#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
300330
#define pinIsSerial(p) ((digitalPinFirstOccurence(p) == PIN_SERIAL_RX) ||\
301331
(digitalPinFirstOccurence(p) == PIN_SERIAL_TX))
302332
#endif
333+
// Convenient macro to handle Analog
334+
bool pinIsAnalogInput(uint32_t pin);
335+
uint32_t digitalPinToAnalogInput(uint32_t pin);
303336

304337
#ifdef __cplusplus
305338
}

Diff for: variants/BLUE_F407VE_Mini/variant.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,28 @@ const PinName digitalPin[] = {
128128
PB_3,
129129
PB_5,
130130
PB_7,
131-
PB_9, //D79 - LED
131+
PB_9 //D79 - LED
132132
//GND
133133
//3V3
134134
//GND
135-
// Analog pins
136-
PA_0, //D80
137-
PA_1,
138-
PA_2,
139-
PA_3,
140-
PA_4,
141-
PA_5,
142-
PB_0,
143-
PB_1,
144-
PC_0,
145-
PC_1,
146-
PC_2, //D90
147-
PC_3,
148-
PC_4,
149-
PC_5
135+
};
136+
137+
// Analog (Ax) pin number array
138+
const uint32_t analogInPin[] = {
139+
7, //A0
140+
8, //A1
141+
49, //A2
142+
50, //A3
143+
9, //A4
144+
51, //A5
145+
12, //A6
146+
54, //A7
147+
5, //A8
148+
47, //A9
149+
6, //A10
150+
48, //A11
151+
11, //A12
152+
53 //A13
150153
};
151154

152155
#ifdef __cplusplus

Diff for: variants/BLUE_F407VE_Mini/variant.h

+15-16
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ extern "C" {
4747
#define PE4 2
4848
#define PE6 3
4949
#define PC14 4 // OSC32_IN
50-
#define PC0 5 // A8
51-
#define PC2 6 // A10
52-
#define PA0 7 // A0
53-
#define PA1 8 // A1
54-
#define PA4 9 // A4
50+
#define PC0 A8
51+
#define PC2 A10
52+
#define PA0 A0
53+
#define PA1 A1
54+
#define PA4 A4
5555
#define PA6 10
56-
#define PC4 11 // A12
57-
#define PB0 12 // A6
56+
#define PC4 A12
57+
#define PB0 A6
5858
#define PB2 13
5959
#define PE8 14
6060
#define PE9 15
@@ -96,14 +96,14 @@ extern "C" {
9696
#define PE5 44
9797
#define PC13 45
9898
#define PC15 46 // OSC32_OUT
99-
#define PC1 47 // A9
100-
#define PC3 48 // A11
101-
#define PA2 49 // A2
102-
#define PA3 50 // A3
103-
#define PA5 51 // A5
99+
#define PC1 A9
100+
#define PC3 A11
101+
#define PA2 A2
102+
#define PA3 A3
103+
#define PA5 A5
104104
#define PA7 52
105-
#define PC5 53 // A13
106-
#define PB1 54 // A7
105+
#define PC5 A13
106+
#define PB1 A7
107107
#define PE7 55
108108
#define PE10 56
109109
#define PE12 57
@@ -135,10 +135,9 @@ extern "C" {
135135
// GND
136136

137137
// This must be a literal
138-
#define NUM_DIGITAL_PINS 94
138+
#define NUM_DIGITAL_PINS 80
139139
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
140140
#define NUM_ANALOG_INPUTS 14
141-
#define NUM_ANALOG_FIRST 80
142141

143142
// On-board LED pin number
144143
#define LED_BUILTIN PB9

Diff for: variants/DEMO_F030F4/variant.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ const PinName digitalPin[] = {
5757
// These two are only available on boards without a crystal:
5858
PF_0,
5959
PF_1,
60-
// Duplicated pins in order to be aligned with PinMap_ADC
61-
// A0 have to be greater than NUM_ANALOG_INPUTS
62-
PA_0, //D15/A0 ~ D0
63-
PA_1, //D16/A1 ~ D1
64-
PA_2, //D17/A2 ~ D2
65-
PA_3, //D18/A3 ~ D3
66-
PA_4, //D19/A4 ~ D4
67-
PA_5, //D20/A5 ~ D5
68-
PA_6, //D21/A6 ~ D6
69-
PA_7, //D22/A7 ~ D7
70-
PB_1 //D23/A8 ~ D8
60+
};
61+
62+
// Analog (Ax) pin number array
63+
const uint32_t analogInPin[] = {
64+
0, //A0
65+
1, //A1
66+
2, //A2
67+
3, //A3
68+
4, //A4
69+
5, //A5
70+
6, //A6
71+
7, //A7
72+
8 //A8
7173
};
7274

7375
#ifdef __cplusplus

Diff for: variants/DEMO_F030F4/variant.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ extern "C" {
4040

4141
// USB connector on the top, MCU side
4242
// Left Side
43-
#define PA0 0 //D0/A0
44-
#define PA1 1 //D1/A1
45-
#define PA2 2 //D2/A2 - TX
46-
#define PA3 3 //D3/A3 - RX
47-
#define PA4 4 //D4/A4 - LED
43+
#define PA0 A0 //D0/A0
44+
#define PA1 A1 //D1/A1
45+
#define PA2 A2 //D2/A2 - TX
46+
#define PA3 A3 //D3/A3 - RX
47+
#define PA4 A4 //D4/A4 - LED
4848
// Right side
49-
#define PA5 5 //D5/A5 - SCK
50-
#define PA6 6 //D6/A6 - MISO
51-
#define PA7 7 //D7/A7 - MOSI
52-
#define PB1 8 //D8/A8 - SS
49+
#define PA5 A5 //D5/A5 - SCK
50+
#define PA6 A6 //D6/A6 - MISO
51+
#define PA7 A7 //D7/A7 - MOSI
52+
#define PB1 A8 //D8/A8 - SS
5353
#define PA9 9 //D9 - SCL (TX UART header)
5454
#define PA10 10 //D10 - SDA (RX UART header)
5555
#define PA13 11 //D11 - SWDIO
@@ -59,10 +59,9 @@ extern "C" {
5959
#define PF1 14
6060

6161
// This must be a literal with the same value as PEND
62-
#define NUM_DIGITAL_PINS 24
62+
#define NUM_DIGITAL_PINS 15
6363
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
6464
#define NUM_ANALOG_INPUTS 9
65-
#define NUM_ANALOG_FIRST 15
6665

6766
// On-board LED pin number
6867
#define LED_BUILTIN PA4

0 commit comments

Comments
 (0)