Skip to content
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

Nano ESP32: add pin numbering option #8565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ menu.EventsCore=Events Run On
menu.MemoryType=Memory Type
menu.EraseFlash=Erase All Flash Before Sketch Upload
menu.JTAGAdapter=JTAG Adapter
menu.PinNumbers=Pin Numbering

# Custom options
menu.Revision=Board Revision
Expand Down Expand Up @@ -24154,12 +24155,17 @@ nano_nora.build.flash_mode=dio
nano_nora.build.boot=qio
nano_nora.build.boot_freq=80m
nano_nora.build.partitions=app3M_fat9M_fact512k_16MB
nano_nora.build.defines=-DBOARD_HAS_PIN_REMAP -DBOARD_HAS_PSRAM '-DUSB_MANUFACTURER="Arduino"' '-DUSB_PRODUCT="Nano ESP32"'
nano_nora.build.defines=-DBOARD_HAS_PIN_REMAP {build.disable_pin_remap} -DBOARD_HAS_PSRAM '-DUSB_MANUFACTURER="Arduino"' '-DUSB_PRODUCT="Nano ESP32"'
nano_nora.build.loop_core=-DARDUINO_RUNNING_CORE=1
nano_nora.build.event_core=-DARDUINO_EVENT_RUNNING_CORE=1
nano_nora.build.psram_type=opi
nano_nora.build.memory_type={build.boot}_{build.psram_type}
nano_nora.build.disable_pin_remap=

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"

nano_nora.menu.PinNumbers.default=By Arduino pin (default)
nano_nora.menu.PinNumbers.byGPIONumber=By GPIO number (legacy)
nano_nora.menu.PinNumbers.byGPIONumber.build.disable_pin_remap=-DBOARD_USES_HW_GPIO_NUMBERS

##############################################################
7 changes: 4 additions & 3 deletions cores/esp32/FunctionalInterrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#include <stdint.h>

struct InterruptArgStructure {
std::function<void(void)> interruptFunction;
std::function<void(void)> interruptFunction;
};

void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode);

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

#endif /* CORE_CORE_FUNCTIONALINTERRUPT_H_ */
10 changes: 5 additions & 5 deletions cores/esp32/io_pin_remap.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

// Pin remapping functions
int8_t digitalPinToGPIONumber(int8_t digitalPin);
int8_t digitalPinFromGPIONumber(int8_t gpioPin);
int8_t digitalPinFromGPIONumber(int8_t gpioNumber);

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

// Override APIs requiring pin remapping

// cores/esp32/Arduino.h
#define pulseInLong(pin, state, timeout) pulseInLong(digitalPinToGPIONumber(pin), state, timeout)
#define pulseIn(pin, state, timeout) pulseIn(digitalPinToGPIONumber(pin), state, timeout)
#define noTone(_pin) noTone(digitalPinToGPIONumber(_pin))
#define tone(_pin, args...) tone(digitalPinToGPIONumber(_pin), args)
#define pulseInLong(pin, args...) pulseInLong(digitalPinToGPIONumber(pin), args)
#define pulseIn(pin, args...) pulseIn(digitalPinToGPIONumber(pin), args)
#define noTone(_pin) noTone(digitalPinToGPIONumber(_pin))
#define tone(_pin, args...) tone(digitalPinToGPIONumber(_pin), args)

// cores/esp32/esp32-hal.h
#define analogGetChannel(pin) analogGetChannel(digitalPinToGPIONumber(pin))
Expand Down
91 changes: 63 additions & 28 deletions variants/arduino_nano_nora/io_pin_remap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,73 @@
#warning The Arduino API will use GPIO numbers for this build.
#endif

#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)

#include "Arduino.h"

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

void _nano_nora_reset_gpio_matrix(void)
{
// In this core file pin mapping is _not_ applied, so the API is
// always GPIO-based, but the constants can be either.
for (int pin = 0; pin < sizeof(TO_GPIO_NUMBER); ++pin) {
int gpio = TO_GPIO_NUMBER[pin];
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
// Pin remapping in effect, constants = indexes
switch (pin) {
#else
// Pin remapping disabled, constants = GPIO numbers
switch (gpio) {
#endif
case LED_RED:
case LED_GREEN:
case LED_BLUE:
// set RGB pins to dig outputs, HIGH=off
pinMode(gpio, OUTPUT);
digitalWrite(gpio, HIGH);
break;

case TX:
case RX:
// leave UART pins alone
break;

default:
// initialize other pins to dig inputs
pinMode(gpio, INPUT);
break;
}
}
}

#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)

int8_t digitalPinToGPIONumber(int8_t digitalPin)
{
if ((digitalPin < 0) || (digitalPin >= NUM_DIGITAL_PINS))
Expand Down
61 changes: 53 additions & 8 deletions variants/arduino_nano_nora/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
#define USB_VID 0x2341
#define USB_PID 0x0070

#define EXTERNAL_NUM_INTERRUPTS 46
#define NUM_DIGITAL_PINS 25
#define NUM_ANALOG_INPUTS 8

#define analogInputToDigitalPin(p) (p)
#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p)) < 48)? digitalPinToGPIONumber(p) : -1)
#define digitalPinHasPWM(p) (((uint8_t)digitalPinToGPIONumber(p)) < 46)

#ifndef __cplusplus
#define constexpr const
#endif

// primary pin names

#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)

// Arduino style definitions (API uses Dx)

#define analogInputToDigitalPin(p) (p)
#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p)) < 48)? digitalPinToGPIONumber(p) : -1)
#define digitalPinHasPWM(p) (((uint8_t)digitalPinToGPIONumber(p)) < 46)

#define EXTERNAL_NUM_INTERRUPTS 46
#define NUM_DIGITAL_PINS 25
#define NUM_ANALOG_INPUTS 8

static constexpr uint8_t D0 = 0; // also RX
static constexpr uint8_t D1 = 1; // also TX
static constexpr uint8_t D2 = 2;
Expand Down Expand Up @@ -48,6 +52,47 @@ static constexpr uint8_t A5 = 22; // also SCL
static constexpr uint8_t A6 = 23;
static constexpr uint8_t A7 = 24;

#else

// ESP32-style definitions (API uses GPIOx)

#define EXTERNAL_NUM_INTERRUPTS 46
#define NUM_DIGITAL_PINS 48
#define NUM_ANALOG_INPUTS 20

#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
#define digitalPinToInterrupt(p) (((p)<48)?(p):-1)
#define digitalPinHasPWM(p) (p < 46)

static constexpr uint8_t D0 = 44; // also RX
static constexpr uint8_t D1 = 43; // also TX
static constexpr uint8_t D2 = 5;
static constexpr uint8_t D3 = 6; // also CTS
static constexpr uint8_t D4 = 7; // also DSR
static constexpr uint8_t D5 = 8;
static constexpr uint8_t D6 = 9;
static constexpr uint8_t D7 = 10;
static constexpr uint8_t D8 = 17;
static constexpr uint8_t D9 = 18;
static constexpr uint8_t D10 = 21; // also SS
static constexpr uint8_t D11 = 38; // also MOSI
static constexpr uint8_t D12 = 47; // also MISO
static constexpr uint8_t D13 = 48; // also SCK, LED_BUILTIN
static constexpr uint8_t LED_RED = 46;
static constexpr uint8_t LED_GREEN = 0;
static constexpr uint8_t LED_BLUE = 45; // also RTS

static constexpr uint8_t A0 = 1; // also DTR
static constexpr uint8_t A1 = 2;
static constexpr uint8_t A2 = 3;
static constexpr uint8_t A3 = 4;
static constexpr uint8_t A4 = 11; // also SDA
static constexpr uint8_t A5 = 12; // also SCL
static constexpr uint8_t A6 = 13;
static constexpr uint8_t A7 = 14;

#endif

// alternate pin functions

static constexpr uint8_t LED_BUILTIN = D13;
Expand Down
25 changes: 4 additions & 21 deletions variants/arduino_nano_nora/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,13 @@
#include <esp_ota_ops.h>
#include <esp_partition.h>

// defined in io_pin_remap.cpp
extern void _nano_nora_reset_gpio_matrix(void);

extern "C" {
void initVariant() {
// FIXME: fix issues with GPIO matrix not being soft reset properly
for (int pin = 0; pin<NUM_DIGITAL_PINS; ++pin) {
switch (pin) {
case LED_RED:
case LED_GREEN:
case LED_BLUE:
// set RGB pins to dig outputs, HIGH=off
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
break;

case TX:
case RX:
// leave UART pins alone
break;

default:
// initialize other pins to dig inputs
pinMode(pin, INPUT);
break;
}
}
_nano_nora_reset_gpio_matrix();
}
}

Expand Down