Skip to content

Commit 758a6e4

Browse files
committed
Nano ESP32: make GPIO matrix reset robust to pin mode choice
1 parent 61d867b commit 758a6e4

File tree

2 files changed

+67
-49
lines changed

2 files changed

+67
-49
lines changed

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))

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)