Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

Commit 8b56fd3

Browse files
polldofacchinm
authored andcommitted
AnalogIn: added struct to handle associations between ADC objects and analog pins
1 parent ef532df commit 8b56fd3

File tree

4 files changed

+65
-42
lines changed

4 files changed

+65
-42
lines changed

cores/arduino/Arduino.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ typedef struct _PinDescription
9090
PinName name;
9191
mbed::InterruptIn* irq;
9292
mbed::PwmOut* pwm;
93-
mbed::AnalogIn* analog;
9493
gpio_t* gpio;
9594
} PinDescription ;
9695

96+
typedef struct _AnalogPinDescription
97+
{
98+
PinName name;
99+
mbed::AnalogIn* adc;
100+
} AnalogPinDescription ;
101+
97102
int PinNameToIndex(PinName P);
98103

99104
// Pins table to be instantiated into variant.cpp
100105
extern PinDescription g_APinDescription[];
106+
extern AnalogPinDescription g_AAnalogPinDescription[];
101107

102108
#include "Serial.h"
103109
#if defined(SERIAL_CDC)

cores/arduino/macros.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#ifdef USE_ARDUINO_PINOUT
2525

2626
#define analogPinToPinName(P) (P >= PINS_COUNT ? NC : P < A0 ? g_APinDescription[P+A0].name : g_APinDescription[P].name)
27+
#define analogPinToAdcObj(P) (P < A0 ? g_AAnalogPinDescription[P].adc : g_AAnalogPinDescription[P-A0].adc)
2728
#define digitalPinToPinName(P) (P >= PINS_COUNT ? NC : g_APinDescription[P].name)
2829
#define digitalPinToInterruptObj(P) (g_APinDescription[P].irq)
2930
#define digitalPinToPwm(P) (g_APinDescription[P].pwm)
3031
#define digitalPinToGpio(P) (g_APinDescription[P].gpio)
31-
#define analogPinToAnalogObj(P) (P < A0 ? g_APinDescription[P+A0].analog : g_APinDescription[P].analog)
3232

3333
// this is needed for backwards compatibility
3434
#define digitalPinToInterrupt(P) (P)

cores/arduino/wiring_analog.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ void analogWriteResolution(int bits)
6262

6363
int analogRead(PinName pin)
6464
{
65-
return (mbed::AnalogIn(pin).read_u16() >> (16 - read_resolution));
65+
for (pin_size_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
66+
if (analogPinToPinName(i) == pin) {
67+
return analogRead(i + A0);
68+
}
69+
}
70+
return -1;
6671
}
6772

6873
int analogRead(pin_size_t pin)
@@ -74,12 +79,12 @@ int analogRead(pin_size_t pin)
7479
if (name == NC) {
7580
return -1;
7681
}
77-
mbed::AnalogIn* obj = analogPinToAnalogObj(pin);
78-
if (obj == NULL) {
79-
obj = new mbed::AnalogIn(name);
80-
analogPinToAnalogObj(pin) = obj;
82+
mbed::AnalogIn* adc = analogPinToAdcObj(pin);
83+
if (adc == NULL) {
84+
adc = new mbed::AnalogIn(name);
85+
analogPinToAdcObj(pin) = adc;
8186
}
82-
return (obj->read_u16() >> (16 - read_resolution));
87+
return (adc->read_u16() >> (16 - read_resolution));
8388
}
8489

8590
void analogReadResolution(int bits)

variants/ARDUINO_NANO33BLE/variant.cpp

+46-34
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,66 @@
11
#include "Arduino.h"
22

3+
AnalogPinDescription g_AAnalogPinDescription[] = {
4+
// A0 - A7
5+
{ P0_4, NULL }, // A0
6+
{ P0_5, NULL }, // A1
7+
{ P0_30, NULL }, // A2
8+
{ P0_29, NULL }, // A3
9+
{ P0_31, NULL }, // A4/SDA
10+
{ P0_2, NULL }, // A5/SCL
11+
{ P0_28, NULL }, // A6
12+
{ P0_3, NULL } // A7
13+
};
14+
315
PinDescription g_APinDescription[] = {
416
// D0 - D7
5-
{ P1_3, NULL, NULL, NULL, NULL }, // D0/TX
6-
{ P1_10, NULL, NULL, NULL, NULL }, // D1/RX
7-
{ P1_11, NULL, NULL, NULL, NULL }, // D2
8-
{ P1_12, NULL, NULL, NULL, NULL }, // D3
9-
{ P1_15, NULL, NULL, NULL, NULL }, // D4
10-
{ P1_13, NULL, NULL, NULL, NULL }, // D5
11-
{ P1_14, NULL, NULL, NULL, NULL }, // D6
12-
{ P0_23, NULL, NULL, NULL, NULL }, // D7
17+
{ P1_3, NULL, NULL, NULL }, // D0/TX
18+
{ P1_10, NULL, NULL, NULL }, // D1/RX
19+
{ P1_11, NULL, NULL, NULL }, // D2
20+
{ P1_12, NULL, NULL, NULL }, // D3
21+
{ P1_15, NULL, NULL, NULL }, // D4
22+
{ P1_13, NULL, NULL, NULL }, // D5
23+
{ P1_14, NULL, NULL, NULL }, // D6
24+
{ P0_23, NULL, NULL, NULL }, // D7
1325

1426
// D8 - D13
15-
{ P0_21, NULL, NULL, NULL, NULL }, // D8
16-
{ P0_27, NULL, NULL, NULL, NULL }, // D9
17-
{ P1_2, NULL, NULL, NULL, NULL }, // D10
18-
{ P1_1, NULL, NULL, NULL, NULL }, // D11/MOSI
19-
{ P1_8, NULL, NULL, NULL, NULL }, // D12/MISO
20-
{ P0_13, NULL, NULL, NULL, NULL }, // D13/SCK/LED
27+
{ P0_21, NULL, NULL, NULL }, // D8
28+
{ P0_27, NULL, NULL, NULL }, // D9
29+
{ P1_2, NULL, NULL, NULL }, // D10
30+
{ P1_1, NULL, NULL, NULL }, // D11/MOSI
31+
{ P1_8, NULL, NULL, NULL }, // D12/MISO
32+
{ P0_13, NULL, NULL, NULL }, // D13/SCK/LED
2133

2234
// A0 - A7
23-
{ P0_4, NULL, NULL, NULL, NULL }, // A0
24-
{ P0_5, NULL, NULL, NULL, NULL }, // A1
25-
{ P0_30, NULL, NULL, NULL, NULL }, // A2
26-
{ P0_29, NULL, NULL, NULL, NULL }, // A3
27-
{ P0_31, NULL, NULL, NULL, NULL }, // A4/SDA
28-
{ P0_2, NULL, NULL, NULL, NULL }, // A5/SCL
29-
{ P0_28, NULL, NULL, NULL, NULL }, // A6
30-
{ P0_3, NULL, NULL, NULL, NULL }, // A7
35+
{ P0_4, NULL, NULL, NULL }, // A0
36+
{ P0_5, NULL, NULL, NULL }, // A1
37+
{ P0_30, NULL, NULL, NULL }, // A2
38+
{ P0_29, NULL, NULL, NULL }, // A3
39+
{ P0_31, NULL, NULL, NULL }, // A4/SDA
40+
{ P0_2, NULL, NULL, NULL }, // A5/SCL
41+
{ P0_28, NULL, NULL, NULL }, // A6
42+
{ P0_3, NULL, NULL, NULL }, // A7
3143

3244
// LEDs
33-
{ P0_24, NULL, NULL, NULL, NULL }, // LED R
34-
{ P0_16, NULL, NULL, NULL, NULL }, // LED G
35-
{ P0_6, NULL, NULL, NULL, NULL }, // LED B
36-
{ P1_9, NULL, NULL, NULL, NULL }, // LED PWR
45+
{ P0_24, NULL, NULL, NULL }, // LED R
46+
{ P0_16, NULL, NULL, NULL }, // LED G
47+
{ P0_6, NULL, NULL, NULL }, // LED B
48+
{ P1_9, NULL, NULL, NULL }, // LED PWR
3749

38-
{ P0_19, NULL, NULL, NULL, NULL }, // INT APDS
50+
{ P0_19, NULL, NULL, NULL }, // INT APDS
3951

4052
// PDM
41-
{ P0_17, NULL, NULL, NULL, NULL }, // PDM PWR
42-
{ P0_26, NULL, NULL, NULL, NULL }, // PDM CLK
43-
{ P0_25, NULL, NULL, NULL, NULL }, // PDM DIN
53+
{ P0_17, NULL, NULL, NULL }, // PDM PWR
54+
{ P0_26, NULL, NULL, NULL }, // PDM CLK
55+
{ P0_25, NULL, NULL, NULL }, // PDM DIN
4456

4557
// Internal I2C
46-
{ P0_14, NULL, NULL, NULL, NULL }, // SDA2
47-
{ P0_15, NULL, NULL, NULL, NULL }, // SCL2
58+
{ P0_14, NULL, NULL, NULL }, // SDA2
59+
{ P0_15, NULL, NULL, NULL }, // SCL2
4860

4961
// Internal I2C
50-
{ P1_0, NULL, NULL, NULL, NULL }, // I2C_PULL
51-
{ P0_22, NULL, NULL, NULL, NULL } // VDD_ENV_ENABLE
62+
{ P1_0, NULL, NULL, NULL }, // I2C_PULL
63+
{ P0_22, NULL, NULL, NULL } // VDD_ENV_ENABLE
5264
};
5365

5466
extern "C" {

0 commit comments

Comments
 (0)