Skip to content

Commit 8c7841c

Browse files
authored
Nano ESP32: add pin numbering option (#8565)
* io_pin_remap: additional bugfixes - FunctionalInterrupt.h: prevent macro expansion in declaration - io_pin_remap.h: fix remaining pin-remapped functions whose API allow a variable number of parameters * Nano ESP32: make GPIO matrix reset robust to pin mode choice * Nano ESP32: add backwards-compatible pin definitions
1 parent 9e8b5ac commit 8c7841c

File tree

6 files changed

+136
-66
lines changed

6 files changed

+136
-66
lines changed

Diff for: boards.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ menu.EventsCore=Events Run On
1717
menu.MemoryType=Memory Type
1818
menu.EraseFlash=Erase All Flash Before Sketch Upload
1919
menu.JTAGAdapter=JTAG Adapter
20+
menu.PinNumbers=Pin Numbering
2021

2122
# Custom options
2223
menu.Revision=Board Revision
@@ -24154,12 +24155,17 @@ nano_nora.build.flash_mode=dio
2415424155
nano_nora.build.boot=qio
2415524156
nano_nora.build.boot_freq=80m
2415624157
nano_nora.build.partitions=app3M_fat9M_fact512k_16MB
24157-
nano_nora.build.defines=-DBOARD_HAS_PIN_REMAP -DBOARD_HAS_PSRAM '-DUSB_MANUFACTURER="Arduino"' '-DUSB_PRODUCT="Nano ESP32"'
24158+
nano_nora.build.defines=-DBOARD_HAS_PIN_REMAP {build.disable_pin_remap} -DBOARD_HAS_PSRAM '-DUSB_MANUFACTURER="Arduino"' '-DUSB_PRODUCT="Nano ESP32"'
2415824159
nano_nora.build.loop_core=-DARDUINO_RUNNING_CORE=1
2415924160
nano_nora.build.event_core=-DARDUINO_EVENT_RUNNING_CORE=1
2416024161
nano_nora.build.psram_type=opi
2416124162
nano_nora.build.memory_type={build.boot}_{build.psram_type}
24163+
nano_nora.build.disable_pin_remap=
2416224164

2416324165
nano_nora.tools.esptool_py.program.pattern_args=--chip {build.mcu} --port "{serial.port}" --before default_reset --after hard_reset write_flash -z --flash_mode {build.flash_mode} --flash_freq {build.flash_freq} --flash_size {build.flash_size} {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0xf70000 "{build.variant.path}/extra/nora_recovery/nora_recovery.ino.bin" 0x10000 "{build.path}/{build.project_name}.bin"
2416424166

24167+
nano_nora.menu.PinNumbers.default=By Arduino pin (default)
24168+
nano_nora.menu.PinNumbers.byGPIONumber=By GPIO number (legacy)
24169+
nano_nora.menu.PinNumbers.byGPIONumber.build.disable_pin_remap=-DBOARD_USES_HW_GPIO_NUMBERS
24170+
2416524171
##############################################################

Diff for: cores/esp32/FunctionalInterrupt.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include <stdint.h>
1313

1414
struct InterruptArgStructure {
15-
std::function<void(void)> interruptFunction;
15+
std::function<void(void)> interruptFunction;
1616
};
1717

18-
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode);
19-
18+
// The extra set of parentheses here prevents macros defined
19+
// in io_pin_remap.h from applying to this declaration.
20+
void (attachInterrupt)(uint8_t pin, std::function<void(void)> intRoutine, int mode);
2021

2122
#endif /* CORE_CORE_FUNCTIONALINTERRUPT_H_ */

Diff for: cores/esp32/io_pin_remap.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
// Pin remapping functions
99
int8_t digitalPinToGPIONumber(int8_t digitalPin);
10-
int8_t digitalPinFromGPIONumber(int8_t gpioPin);
10+
int8_t digitalPinFromGPIONumber(int8_t gpioNumber);
1111

1212
// Apply pin remapping to API only when building libraries and user sketch
1313
#ifndef ARDUINO_CORE_BUILD
1414

1515
// Override APIs requiring pin remapping
1616

1717
// cores/esp32/Arduino.h
18-
#define pulseInLong(pin, state, timeout) pulseInLong(digitalPinToGPIONumber(pin), state, timeout)
19-
#define pulseIn(pin, state, timeout) pulseIn(digitalPinToGPIONumber(pin), state, timeout)
20-
#define noTone(_pin) noTone(digitalPinToGPIONumber(_pin))
21-
#define tone(_pin, args...) tone(digitalPinToGPIONumber(_pin), args)
18+
#define pulseInLong(pin, args...) pulseInLong(digitalPinToGPIONumber(pin), args)
19+
#define pulseIn(pin, args...) pulseIn(digitalPinToGPIONumber(pin), args)
20+
#define noTone(_pin) noTone(digitalPinToGPIONumber(_pin))
21+
#define tone(_pin, args...) tone(digitalPinToGPIONumber(_pin), args)
2222

2323
// cores/esp32/esp32-hal.h
2424
#define analogGetChannel(pin) analogGetChannel(digitalPinToGPIONumber(pin))

Diff for: variants/arduino_nano_nora/io_pin_remap.cpp

+63-28
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,73 @@
1414
#warning The Arduino API will use GPIO numbers for this build.
1515
#endif
1616

17-
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
18-
1917
#include "Arduino.h"
2018

21-
static const int8_t TO_GPIO_NUMBER[NUM_DIGITAL_PINS] = {
22-
[D0] = 44, // RX
23-
[D1] = 43, // TX
24-
[D2] = 5,
25-
[D3] = 6, // CTS
26-
[D4] = 7, // DSR
27-
[D5] = 8,
28-
[D6] = 9,
29-
[D7] = 10,
30-
[D8] = 17,
31-
[D9] = 18,
32-
[D10] = 21, // SS
33-
[D11] = 38, // MOSI
34-
[D12] = 47, // MISO
35-
[D13] = 48, // SCK, LED_BUILTIN
36-
[LED_RED] = 46,
37-
[LED_GREEN] = 0,
38-
[LED_BLUE] = 45, // RTS
39-
[A0] = 1, // DTR
40-
[A1] = 2,
41-
[A2] = 3,
42-
[A3] = 4,
43-
[A4] = 11, // SDA
44-
[A5] = 12, // SCL
45-
[A6] = 13,
46-
[A7] = 14,
19+
// NOTE: This must match with the remapped pin sequence in pins_arduino.h
20+
static const int8_t TO_GPIO_NUMBER[] = {
21+
44, // [ 0] D0, RX
22+
43, // [ 1] D1, TX
23+
5, // [ 2] D2
24+
6, // [ 3] D3, CTS
25+
7, // [ 4] D4, DSR
26+
8, // [ 5] D5
27+
9, // [ 6] D6
28+
10, // [ 7] D7
29+
17, // [ 8] D8
30+
18, // [ 9] D9
31+
21, // [10] D10, SS
32+
38, // [11] D11, MOSI
33+
47, // [12] D12, MISO
34+
48, // [13] D13, SCK, LED_BUILTIN
35+
46, // [14] LED_RED
36+
0, // [15] LED_GREEN
37+
45, // [16] LED_BLUE, RTS
38+
1, // [17] A0, DTR
39+
2, // [18] A1
40+
3, // [19] A2
41+
4, // [20] A3
42+
11, // [21] A4, SDA
43+
12, // [22] A5, SCL
44+
13, // [23] A6
45+
14, // [24] A7
4746
};
4847

48+
void _nano_nora_reset_gpio_matrix(void)
49+
{
50+
// In this core file pin mapping is _not_ applied, so the API is
51+
// always GPIO-based, but the constants can be either.
52+
for (int pin = 0; pin < sizeof(TO_GPIO_NUMBER); ++pin) {
53+
int gpio = TO_GPIO_NUMBER[pin];
54+
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
55+
// Pin remapping in effect, constants = indexes
56+
switch (pin) {
57+
#else
58+
// Pin remapping disabled, constants = GPIO numbers
59+
switch (gpio) {
60+
#endif
61+
case LED_RED:
62+
case LED_GREEN:
63+
case LED_BLUE:
64+
// set RGB pins to dig outputs, HIGH=off
65+
pinMode(gpio, OUTPUT);
66+
digitalWrite(gpio, HIGH);
67+
break;
68+
69+
case TX:
70+
case RX:
71+
// leave UART pins alone
72+
break;
73+
74+
default:
75+
// initialize other pins to dig inputs
76+
pinMode(gpio, INPUT);
77+
break;
78+
}
79+
}
80+
}
81+
82+
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
83+
4984
int8_t digitalPinToGPIONumber(int8_t digitalPin)
5085
{
5186
if ((digitalPin < 0) || (digitalPin >= NUM_DIGITAL_PINS))

Diff for: variants/arduino_nano_nora/pins_arduino.h

+53-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
#define USB_VID 0x2341
88
#define USB_PID 0x0070
99

10-
#define EXTERNAL_NUM_INTERRUPTS 46
11-
#define NUM_DIGITAL_PINS 25
12-
#define NUM_ANALOG_INPUTS 8
13-
14-
#define analogInputToDigitalPin(p) (p)
15-
#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p)) < 48)? digitalPinToGPIONumber(p) : -1)
16-
#define digitalPinHasPWM(p) (((uint8_t)digitalPinToGPIONumber(p)) < 46)
17-
1810
#ifndef __cplusplus
1911
#define constexpr const
2012
#endif
2113

2214
// primary pin names
2315

16+
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
17+
18+
// Arduino style definitions (API uses Dx)
19+
20+
#define analogInputToDigitalPin(p) (p)
21+
#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p)) < 48)? digitalPinToGPIONumber(p) : -1)
22+
#define digitalPinHasPWM(p) (((uint8_t)digitalPinToGPIONumber(p)) < 46)
23+
24+
#define EXTERNAL_NUM_INTERRUPTS 46
25+
#define NUM_DIGITAL_PINS 25
26+
#define NUM_ANALOG_INPUTS 8
27+
2428
static constexpr uint8_t D0 = 0; // also RX
2529
static constexpr uint8_t D1 = 1; // also TX
2630
static constexpr uint8_t D2 = 2;
@@ -48,6 +52,47 @@ static constexpr uint8_t A5 = 22; // also SCL
4852
static constexpr uint8_t A6 = 23;
4953
static constexpr uint8_t A7 = 24;
5054

55+
#else
56+
57+
// ESP32-style definitions (API uses GPIOx)
58+
59+
#define EXTERNAL_NUM_INTERRUPTS 46
60+
#define NUM_DIGITAL_PINS 48
61+
#define NUM_ANALOG_INPUTS 20
62+
63+
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
64+
#define digitalPinToInterrupt(p) (((p)<48)?(p):-1)
65+
#define digitalPinHasPWM(p) (p < 46)
66+
67+
static constexpr uint8_t D0 = 44; // also RX
68+
static constexpr uint8_t D1 = 43; // also TX
69+
static constexpr uint8_t D2 = 5;
70+
static constexpr uint8_t D3 = 6; // also CTS
71+
static constexpr uint8_t D4 = 7; // also DSR
72+
static constexpr uint8_t D5 = 8;
73+
static constexpr uint8_t D6 = 9;
74+
static constexpr uint8_t D7 = 10;
75+
static constexpr uint8_t D8 = 17;
76+
static constexpr uint8_t D9 = 18;
77+
static constexpr uint8_t D10 = 21; // also SS
78+
static constexpr uint8_t D11 = 38; // also MOSI
79+
static constexpr uint8_t D12 = 47; // also MISO
80+
static constexpr uint8_t D13 = 48; // also SCK, LED_BUILTIN
81+
static constexpr uint8_t LED_RED = 46;
82+
static constexpr uint8_t LED_GREEN = 0;
83+
static constexpr uint8_t LED_BLUE = 45; // also RTS
84+
85+
static constexpr uint8_t A0 = 1; // also DTR
86+
static constexpr uint8_t A1 = 2;
87+
static constexpr uint8_t A2 = 3;
88+
static constexpr uint8_t A3 = 4;
89+
static constexpr uint8_t A4 = 11; // also SDA
90+
static constexpr uint8_t A5 = 12; // also SCL
91+
static constexpr uint8_t A6 = 13;
92+
static constexpr uint8_t A7 = 14;
93+
94+
#endif
95+
5196
// alternate pin functions
5297

5398
static constexpr uint8_t LED_BUILTIN = D13;

Diff for: variants/arduino_nano_nora/variant.cpp

+4-21
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,13 @@
99
#include <esp_ota_ops.h>
1010
#include <esp_partition.h>
1111

12+
// defined in io_pin_remap.cpp
13+
extern void _nano_nora_reset_gpio_matrix(void);
14+
1215
extern "C" {
1316
void initVariant() {
1417
// FIXME: fix issues with GPIO matrix not being soft reset properly
15-
for (int pin = 0; pin<NUM_DIGITAL_PINS; ++pin) {
16-
switch (pin) {
17-
case LED_RED:
18-
case LED_GREEN:
19-
case LED_BLUE:
20-
// set RGB pins to dig outputs, HIGH=off
21-
pinMode(pin, OUTPUT);
22-
digitalWrite(pin, HIGH);
23-
break;
24-
25-
case TX:
26-
case RX:
27-
// leave UART pins alone
28-
break;
29-
30-
default:
31-
// initialize other pins to dig inputs
32-
pinMode(pin, INPUT);
33-
break;
34-
}
35-
}
18+
_nano_nora_reset_gpio_matrix();
3619
}
3720
}
3821

0 commit comments

Comments
 (0)