Skip to content

Analog pins definition clean up #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
602e8b7
Remove AEND definition
fpistm Nov 28, 2019
3e24b89
Allow non contiguous analog pins definition
fpistm Nov 28, 2019
ed64ffc
Add macro helper for Firmata
fpistm Dec 3, 2019
e509cad
[DISCO_F030R8] Avoid duplicated analog pins
fpistm Dec 2, 2019
861a50c
[Nucleo_F429ZI] Avoid duplicated analog pins
fpistm Dec 2, 2019
d85d978
[Nucleo_L496ZG] Avoid duplicated analog pins
fpistm Dec 16, 2019
082e835
[Nucleo_F103RB] Avoid duplicated analog pins
fpistm Dec 16, 2019
e119a26
[BLUE_F407VE] Avoid duplicated analog pins
fpistm Dec 16, 2019
f34e43c
[DEMO_F030F4] Avoid duplicated analog pins
fpistm Dec 16, 2019
d09004c
[DISCO_F100RB] Avoid duplicated analog pins
fpistm Dec 16, 2019
4e8ffa2
[DISCO_F407VG] Avoid duplicated analog pins
fpistm Dec 16, 2019
1b02cb6
[DISCO_L475VG] Avoid duplicated analog pins
fpistm Dec 16, 2019
9b60a75
[DIYMORE_F407VGT] Avoid duplicated analog pins
fpistm Dec 18, 2019
95dd5e7
[FK407M1] Avoid duplicated analog pins
fpistm Dec 18, 2019
e57915e
[HY_TinySTM103T] Avoid duplicated analog pins
fpistm Dec 18, 2019
36ed309
[MAPLEMINI_F103CB] Avoid duplicated analog pins
fpistm Dec 18, 2019
24286ba
[NUCLEO_F030R8] Avoid duplicated analog pins
fpistm Dec 18, 2019
7bfe2cc
[NUCLEO_F091RC] Avoid duplicated analog pins
fpistm Dec 18, 2019
6fb9c0e
[NUCLEO_F207ZG] Avoid duplicated analog pins
fpistm Dec 19, 2019
c39bbf3
[NUCLEO_F302R8] Avoid duplicated analog pins
fpistm Dec 19, 2019
c6c3bec
[NUCLEO_F303RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
31c3428
[NUCLEO_F401RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
0fcb012
[NUCLEO_F411RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
3709639
[NUCLEO_F446RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
111ced7
[NUCLEO_F767ZI] Avoid duplicated analog pins
fpistm Dec 19, 2019
cbcb31c
[NUCLEO_G071RB] Avoid duplicated analog pins
fpistm Dec 19, 2019
6350b27
[NUCLEO_L4R5ZI] Avoid duplicated analog pins
fpistm Dec 19, 2019
e45fe44
[NUCLEO_L053R8] Avoid duplicated analog pins
fpistm Dec 19, 2019
be1cb00
[NUCLEO_L073RZ] Avoid duplicated analog pins
fpistm Dec 19, 2019
ece1381
[NUCLEO_L152RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
de36478
[NUCLEO_L452RE] Avoid duplicated analog pins
fpistm Dec 19, 2019
2eaa5da
[NUCLEO_L476RG] Avoid duplicated analog pins
fpistm Dec 19, 2019
337e0b7
[RUMBA32_F446VE] Avoid duplicated analog pins
fpistm Dec 19, 2019
3c8e21d
[VAKE_F446VE] Avoid duplicated analog pins
fpistm Dec 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cores/arduino/pins_arduino.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@ PinName analogInputToPinName(uint32_t pin)
return pn;
}

bool pinIsAnalogInput(uint32_t pin)
{
bool ret = false;
#if NUM_ANALOG_INPUTS > 0
#ifndef NUM_ANALOG_LAST
ret = (pin >= A0) && (pin < (A0 + NUM_ANALOG_INPUTS));
#else
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
if (analogInPin[i] == pin) {
ret = true;
break;
}
}
#endif /* NUM_ANALOG_LAST */
#endif /* NUM_ANALOG_INPUTS > 0 */
return ret;
}

uint32_t digitalPinToAnalogInput(uint32_t pin)
{
uint32_t ret = NUM_ANALOG_INPUTS;
#if NUM_ANALOG_INPUTS > 0
#ifndef NUM_ANALOG_LAST
ret = pin - A0;
#else
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
if (analogInPin[i] == pin) {
ret = i;
break;
}
}
#endif /* NUM_ANALOG_LAST */
#endif /* NUM_ANALOG_INPUTS > 0 */
return ret;
}

#ifdef __cplusplus
}
#endif
57 changes: 45 additions & 12 deletions cores/arduino/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#ifndef _PINS_ARDUINO_H_
#define _PINS_ARDUINO_H_
#include <stdbool.h>
#include <stdlib.h> /* Required for static_assert */
// Include board variant
#include "variant.h"
Expand Down Expand Up @@ -51,23 +52,33 @@ enum {
};

// Arduino analog pins

#ifndef NUM_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 0
#endif

// If NUM_ANALOG_FIRST is not defined:
// - Ax are not contiguous in the digitalPin array
// - analogInPin array is available
#ifndef NUM_ANALOG_FIRST
#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS
#define NUM_ANALOG_FIRST (NUM_DIGITAL_PINS + 1)
#define NUM_ANALOG_LAST (NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS)
#define NUM_ANALOG_INTERNAL_FIRST (NUM_ANALOG_LAST + 1)
#else
#define NUM_ANALOG_INTERNAL_FIRST (NUM_DIGITAL_PINS + 1)
#endif

// If NUM_ANALOG_INPUTS is not defined there is no analog pins defined.
// Anyway ADC internal channels are always avaialable.
#if NUM_ANALOG_INPUTS > 0
// Analog pins must be contiguous to be able to loop on each value
#define MAX_ANALOG_INPUTS 24
_Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS,
"Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS");

_Static_assert(NUM_ANALOG_FIRST >= NUM_ANALOG_INPUTS,
"First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS");

// Defined for backward compatibility with Firmata which unfortunately use it
#define AEND (NUM_ANALOG_FIRST+NUM_ANALOG_INPUTS)

#if NUM_ANALOG_INPUTS > 0
#define PIN_A0 NUM_ANALOG_FIRST
static const uint8_t A0 = PIN_A0;
Expand Down Expand Up @@ -164,6 +175,7 @@ static const uint8_t A22 = PIN_A22;
#define PIN_A23 (PIN_A22 + 1)
static const uint8_t A23 = PIN_A23;
#endif
#endif /* NUM_ANALOG_INPUTS > 0 */

// Default for Arduino connector compatibility
// SPI Definitions
Expand Down Expand Up @@ -211,38 +223,56 @@ static const uint8_t SCL = PIN_WIRE_SCL;
// ADC internal channels (not a pins)
// Only used for analogRead()
#if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1)
#define ATEMP (NUM_DIGITAL_PINS + 1)
#define ATEMP (NUM_ANALOG_INTERNAL_FIRST)
#endif
#ifdef ADC_CHANNEL_VREFINT
#define AVREF (NUM_DIGITAL_PINS + 2)
#define AVREF (NUM_ANALOG_INTERNAL_FIRST + 2)
#endif
#ifdef ADC_CHANNEL_VBAT
#define AVBAT (NUM_DIGITAL_PINS + 3)
#define AVBAT (NUM_ANALOG_INTERNAL_FIRST + 3)
#endif
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)
#define ATEMP_ADC5 (NUM_DIGITAL_PINS + 4)
#define ATEMP_ADC5 (NUM_ANALOG_INTERNAL_FIRST + 4)
#endif

#ifdef __cplusplus
extern "C" {
#endif
extern const PinName digitalPin[];
extern const uint32_t analogInPin[];

#define NOT_AN_INTERRUPT NC // -1

// Convert a digital pin number Dxx to a PinName PX_n
// Note: Analog pin is also a digital pin.
#ifndef NUM_ANALOG_LAST
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : NC)
#else
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : \
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
digitalPin[analogInPin[p-NUM_ANALOG_FIRST]] : NC)
#endif
// Convert a PinName PX_n to a digital pin number
uint32_t pinNametoDigitalPin(PinName p);

// Convert an analog pin number to a digital pin number
#if defined(NUM_ANALOG_INPUTS) && (NUM_ANALOG_INPUTS>0)
#if NUM_ANALOG_INPUTS > 0
// Used by analogRead api to have A0 == 0
// For contiguous analog pins definition in digitalPin array
#ifndef NUM_ANALOG_LAST
#define analogInputToDigitalPin(p) (((uint32_t)p < NUM_ANALOG_INPUTS) ? (p+A0) : p)
#else
// For non contiguous analog pins definition in digitalPin array
#define analogInputToDigitalPin(p) ( \
((uint32_t)p < NUM_ANALOG_INPUTS) ? analogInPin[p] : \
((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \
analogInPin[p-NUM_ANALOG_FIRST] : p)
#endif // !NUM_ANALOG_LAST
#else
// No analog pin defined
#define analogInputToDigitalPin(p) (NUM_DIGITAL_PINS)
#endif
#endif // NUM_ANALOG_INPUTS > 0

// Convert an analog pin number Axx to a PinName PX_n
PinName analogInputToPinName(uint32_t pin);

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

// Specific for Firmata. As some pins could be duplicated,
// ensure 'p' is not one of the serial pins
// Specific for Firmata.
// Some pins could be duplicated, ensure 'p' is not one of the serial pins
#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
#define pinIsSerial(p) ((digitalPinFirstOccurence(p) == PIN_SERIAL_RX) ||\
(digitalPinFirstOccurence(p) == PIN_SERIAL_TX))
#endif
// Convenient macro to handle Analog
bool pinIsAnalogInput(uint32_t pin);
uint32_t digitalPinToAnalogInput(uint32_t pin);

#ifdef __cplusplus
}
Expand Down
35 changes: 19 additions & 16 deletions variants/BLUE_F407VE_Mini/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,28 @@ const PinName digitalPin[] = {
PB_3,
PB_5,
PB_7,
PB_9, //D79 - LED
PB_9 //D79 - LED
//GND
//3V3
//GND
// Analog pins
PA_0, //D80
PA_1,
PA_2,
PA_3,
PA_4,
PA_5,
PB_0,
PB_1,
PC_0,
PC_1,
PC_2, //D90
PC_3,
PC_4,
PC_5
};

// Analog (Ax) pin number array
const uint32_t analogInPin[] = {
7, //A0
8, //A1
49, //A2
50, //A3
9, //A4
51, //A5
12, //A6
54, //A7
5, //A8
47, //A9
6, //A10
48, //A11
11, //A12
53 //A13
};

#ifdef __cplusplus
Expand Down
31 changes: 15 additions & 16 deletions variants/BLUE_F407VE_Mini/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ extern "C" {
#define PE4 2
#define PE6 3
#define PC14 4 // OSC32_IN
#define PC0 5 // A8
#define PC2 6 // A10
#define PA0 7 // A0
#define PA1 8 // A1
#define PA4 9 // A4
#define PC0 A8
#define PC2 A10
#define PA0 A0
#define PA1 A1
#define PA4 A4
#define PA6 10
#define PC4 11 // A12
#define PB0 12 // A6
#define PC4 A12
#define PB0 A6
#define PB2 13
#define PE8 14
#define PE9 15
Expand Down Expand Up @@ -96,14 +96,14 @@ extern "C" {
#define PE5 44
#define PC13 45
#define PC15 46 // OSC32_OUT
#define PC1 47 // A9
#define PC3 48 // A11
#define PA2 49 // A2
#define PA3 50 // A3
#define PA5 51 // A5
#define PC1 A9
#define PC3 A11
#define PA2 A2
#define PA3 A3
#define PA5 A5
#define PA7 52
#define PC5 53 // A13
#define PB1 54 // A7
#define PC5 A13
#define PB1 A7
#define PE7 55
#define PE10 56
#define PE12 57
Expand Down Expand Up @@ -135,10 +135,9 @@ extern "C" {
// GND

// This must be a literal
#define NUM_DIGITAL_PINS 94
#define NUM_DIGITAL_PINS 80
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 14
#define NUM_ANALOG_FIRST 80

// On-board LED pin number
#define LED_BUILTIN PB9
Expand Down
24 changes: 13 additions & 11 deletions variants/DEMO_F030F4/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ const PinName digitalPin[] = {
// These two are only available on boards without a crystal:
PF_0,
PF_1,
// Duplicated pins in order to be aligned with PinMap_ADC
// A0 have to be greater than NUM_ANALOG_INPUTS
PA_0, //D15/A0 ~ D0
PA_1, //D16/A1 ~ D1
PA_2, //D17/A2 ~ D2
PA_3, //D18/A3 ~ D3
PA_4, //D19/A4 ~ D4
PA_5, //D20/A5 ~ D5
PA_6, //D21/A6 ~ D6
PA_7, //D22/A7 ~ D7
PB_1 //D23/A8 ~ D8
};

// Analog (Ax) pin number array
const uint32_t analogInPin[] = {
0, //A0
1, //A1
2, //A2
3, //A3
4, //A4
5, //A5
6, //A6
7, //A7
8 //A8
};

#ifdef __cplusplus
Expand Down
21 changes: 10 additions & 11 deletions variants/DEMO_F030F4/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ extern "C" {

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

// This must be a literal with the same value as PEND
#define NUM_DIGITAL_PINS 24
#define NUM_DIGITAL_PINS 15
// This must be a literal with a value less than or equal to MAX_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 9
#define NUM_ANALOG_FIRST 15

// On-board LED pin number
#define LED_BUILTIN PA4
Expand Down
Loading