Skip to content

Commit 7d26b07

Browse files
pillo79P-R-O-C-H-Yme-no-dev
authored
Add support for the Arduino Nano ESP32 on 3.x branch (espressif#8909)
* [pin_remap 1/3] platform: define ARDUINO_CORE_BUILD when building core files * [pin_remap 2/3] core,libs: add pin remap hooks * platform: remove previous build options if file is missing "touch" would create the file if not present, but not delete its contents if a previous run left the file in the build dir. * platform: make debug_custom.json file customizable by board * platform: fix default debug prefix "debug.toolchain.prefix" must end with a dash, since only the tool name is appended to this string. The reason this is not a major issue is that the "debug_custom.json" file (copied in the sketch directory when debugging is enabled) forces its own prefix. And to make things more interesting, the "toolchainPrefix" entry in that file should _not_ end with a dash. * [pin_remap 3/3]: add Arduino Nano ESP32 board * fix: periman: include it by default, add include guard * fix: io_pin_remap: adjust for new perimap APIs * fix: libraries: manually handled pin remapping files Previously all libraries invoked either high-level APIs (transparently remapped, like the user sketch) or low-level ESP-IDF calls (where the remap to GPIO numbers had to be added manually). Since 3.x, some of these are mixed (for example, periman* APIs are remapped, while soc* are not). This must be handled by disabling the automatic API remapping and making sure all calls use GPIO numbers. * feat: show remapped pins in chip debug reports --------- Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
1 parent 595dc01 commit 7d26b07

28 files changed

+1014
-45
lines changed

Diff for: boards.txt

+70-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ menu.MemoryType=Memory Type
1818
menu.EraseFlash=Erase All Flash Before Sketch Upload
1919
menu.JTAGAdapter=JTAG Adapter
2020
menu.ZigbeeMode=Zigbee Mode
21+
menu.PinNumbers=Pin Numbering
2122

2223
# Custom options
2324
menu.Revision=Board Revision
@@ -29688,4 +29689,72 @@ sensebox_mcu_esp32s2.menu.EraseFlash.none.upload.erase_cmd=
2968829689
sensebox_mcu_esp32s2.menu.EraseFlash.all=Enabled
2968929690
sensebox_mcu_esp32s2.menu.EraseFlash.all.upload.erase_cmd=-e
2969029691

29691-
##############################################################
29692+
##############################################################
29693+
29694+
nano_nora.name=Arduino Nano ESP32
29695+
nano_nora.vid.0=0x2341
29696+
nano_nora.pid.0=0x0070
29697+
nano_nora.upload_port.0.vid=0x2341
29698+
nano_nora.upload_port.0.pid=0x0070
29699+
29700+
nano_nora.bootloader.tool=esptool_py
29701+
nano_nora.bootloader.tool.default=esptool_py
29702+
29703+
nano_nora.upload.tool=dfu-util
29704+
nano_nora.upload.tool.default=dfu-util
29705+
nano_nora.upload.tool.network=esp_ota
29706+
nano_nora.upload.protocol=serial
29707+
nano_nora.upload.maximum_size=3145728
29708+
nano_nora.upload.maximum_data_size=327680
29709+
nano_nora.upload.use_1200bps_touch=false
29710+
nano_nora.upload.wait_for_upload_port=false
29711+
29712+
nano_nora.serial.disableDTR=false
29713+
nano_nora.serial.disableRTS=false
29714+
29715+
nano_nora.build.tarch=xtensa
29716+
nano_nora.build.bootloader_addr=0x0
29717+
nano_nora.build.target=esp32s3
29718+
nano_nora.build.mcu=esp32s3
29719+
nano_nora.build.core=esp32
29720+
nano_nora.build.variant=arduino_nano_nora
29721+
nano_nora.build.board=NANO_ESP32
29722+
nano_nora.build.code_debug=0
29723+
29724+
nano_nora.build.usb_mode=0
29725+
nano_nora.build.cdc_on_boot=1
29726+
nano_nora.build.msc_on_boot=0
29727+
nano_nora.build.dfu_on_boot=1
29728+
nano_nora.build.f_cpu=240000000L
29729+
nano_nora.build.flash_size=16MB
29730+
nano_nora.build.flash_freq=80m
29731+
nano_nora.build.flash_mode=dio
29732+
nano_nora.build.boot=qio
29733+
nano_nora.build.boot_freq=80m
29734+
nano_nora.build.partitions=app3M_fat9M_fact512k_16MB
29735+
nano_nora.build.defines=-DBOARD_HAS_PIN_REMAP {build.disable_pin_remap} -DBOARD_HAS_PSRAM '-DUSB_MANUFACTURER="Arduino"' '-DUSB_PRODUCT="Nano ESP32"'
29736+
nano_nora.build.loop_core=-DARDUINO_RUNNING_CORE=1
29737+
nano_nora.build.event_core=-DARDUINO_EVENT_RUNNING_CORE=1
29738+
nano_nora.build.psram_type=opi
29739+
nano_nora.build.memory_type={build.boot}_{build.psram_type}
29740+
nano_nora.build.disable_pin_remap=
29741+
29742+
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"
29743+
nano_nora.tools.esptool_py.erase.pattern_args=--chip {build.mcu} --port "{serial.port}" --before default_reset --after hard_reset erase_flash
29744+
29745+
nano_nora.menu.PartitionScheme.default=With FAT partition (default)
29746+
nano_nora.menu.PartitionScheme.spiffs=With SPIFFS partition (advanced)
29747+
nano_nora.menu.PartitionScheme.spiffs.build.partitions=app3M_spiffs9M_fact512k_16MB
29748+
29749+
nano_nora.menu.PinNumbers.default=By Arduino pin (default)
29750+
nano_nora.menu.PinNumbers.byGPIONumber=By GPIO number (legacy)
29751+
nano_nora.menu.PinNumbers.byGPIONumber.build.disable_pin_remap=-DBOARD_USES_HW_GPIO_NUMBERS
29752+
29753+
nano_nora.menu.USBMode.default=Normal mode (TinyUSB)
29754+
nano_nora.menu.USBMode.hwcdc=Debug mode (Hardware CDC)
29755+
nano_nora.menu.USBMode.hwcdc.build.usb_mode=1
29756+
nano_nora.menu.USBMode.hwcdc.build.copy_jtag_files=1
29757+
nano_nora.menu.USBMode.hwcdc.build.openocdscript=esp32s3-builtin.cfg
29758+
nano_nora.menu.USBMode.hwcdc.build.debugconfig=esp32s3-arduino.json
29759+
29760+
##############################################################

Diff for: cores/esp32/Arduino.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@
111111
#define analogInPinToBit(P) (P)
112112
#if SOC_GPIO_PIN_COUNT <= 32
113113
#define digitalPinToPort(pin) (0)
114-
#define digitalPinToBitMask(pin) (1UL << (pin))
114+
#define digitalPinToBitMask(pin) (1UL << digitalPinToGPIONumber(pin))
115115
#define portOutputRegister(port) ((volatile uint32_t*)GPIO_OUT_REG)
116116
#define portInputRegister(port) ((volatile uint32_t*)GPIO_IN_REG)
117117
#define portModeRegister(port) ((volatile uint32_t*)GPIO_ENABLE_REG)
118118
#elif SOC_GPIO_PIN_COUNT <= 64
119-
#define digitalPinToPort(pin) (((pin)>31)?1:0)
120-
#define digitalPinToBitMask(pin) (1UL << (((pin)>31)?((pin)-32):(pin)))
119+
#define digitalPinToPort(pin) ((digitalPinToGPIONumber(pin)>31)?1:0)
120+
#define digitalPinToBitMask(pin) (1UL << (digitalPinToGPIONumber(pin)&31))
121121
#define portOutputRegister(port) ((volatile uint32_t*)((port)?GPIO_OUT1_REG:GPIO_OUT_REG))
122122
#define portInputRegister(port) ((volatile uint32_t*)((port)?GPIO_IN1_REG:GPIO_IN_REG))
123123
#define portModeRegister(port) ((volatile uint32_t*)((port)?GPIO_ENABLE1_REG:GPIO_ENABLE_REG))
@@ -139,8 +139,8 @@
139139
#endif
140140
#define EXTERNAL_NUM_INTERRUPTS NUM_DIGITAL_PINS // All GPIOs
141141
#define analogInputToDigitalPin(p) (((p)<NUM_ANALOG_INPUTS)?(analogChannelToDigitalPin(p)):-1)
142-
#define digitalPinToInterrupt(p) (((p)<NUM_DIGITAL_PINS)?(p):NOT_AN_INTERRUPT)
143-
#define digitalPinHasPWM(p) ((p)<NUM_DIGITAL_PINS)
142+
#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p))<NUM_DIGITAL_PINS)?digitalPinToGPIONumber(p):NOT_AN_INTERRUPT)
143+
#define digitalPinHasPWM(p) (((uint8_t)digitalPinToGPIONumber(p))<NUM_DIGITAL_PINS)
144144

145145
typedef bool boolean;
146146
typedef uint8_t byte;
@@ -236,5 +236,6 @@ void noTone(uint8_t _pin);
236236
#endif /* __cplusplus */
237237

238238
#include "pins_arduino.h"
239+
#include "io_pin_remap.h"
239240

240241
#endif /* _ESP32_CORE_ARDUINO_H_ */

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/HardwareSerial.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ctime>
66

77
#include "pins_arduino.h"
8+
#include "io_pin_remap.h"
89
#include "HardwareSerial.h"
910
#include "soc/soc_caps.h"
1011
#include "driver/uart.h"
@@ -325,6 +326,10 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
325326
}
326327
}
327328

329+
// map logical pins to GPIO numbers
330+
rxPin = digitalPinToGPIONumber(rxPin);
331+
txPin = digitalPinToGPIONumber(txPin);
332+
328333
if(_uart) {
329334
// in this case it is a begin() over a previous begin() - maybe to change baud rate
330335
// thus do not disable debug output
@@ -500,6 +505,12 @@ void HardwareSerial::setRxInvert(bool invert)
500505
// can be called after or before begin()
501506
bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
502507
{
508+
// map logical pins to GPIO numbers
509+
rxPin = digitalPinToGPIONumber(rxPin);
510+
txPin = digitalPinToGPIONumber(txPin);
511+
ctsPin = digitalPinToGPIONumber(ctsPin);
512+
rtsPin = digitalPinToGPIONumber(rtsPin);
513+
503514
// uartSetPins() checks if pins are valid and, if necessary, detaches the previous ones
504515
return uartSetPins(_uart_nr, rxPin, txPin, ctsPin, rtsPin);
505516
}

Diff for: cores/esp32/USB.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
#define USB_WEBUSB_URL "https://espressif.github.io/arduino-esp32/webusb.html"
5252
#endif
5353

54-
#if CFG_TUD_DFU_RUNTIME
54+
#if CFG_TUD_DFU
55+
__attribute__((weak)) uint16_t load_dfu_ota_descriptor(uint8_t * dst, uint8_t * itf) {
56+
return 0;
57+
}
58+
#elif CFG_TUD_DFU_RUNTIME
5559
static uint16_t load_dfu_descriptor(uint8_t * dst, uint8_t * itf)
5660
{
5761
#define DFU_ATTRS (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_CAN_UPLOAD | DFU_ATTR_MANIFESTATION_TOLERANT)
@@ -65,6 +69,9 @@ static uint16_t load_dfu_descriptor(uint8_t * dst, uint8_t * itf)
6569
memcpy(dst, descriptor, TUD_DFU_RT_DESC_LEN);
6670
return TUD_DFU_RT_DESC_LEN;
6771
}
72+
#endif /* CFG_TUD_DFU_RUNTIME */
73+
74+
#if CFG_TUD_DFU_RUNTIME
6875
// Invoked on DFU_DETACH request to reboot to the bootloader
6976
void tud_dfu_runtime_reboot_to_dfu_cb(void)
7077
{
@@ -207,7 +214,9 @@ ESPUSB::operator bool() const
207214
}
208215

209216
bool ESPUSB::enableDFU(){
210-
#if CFG_TUD_DFU_RUNTIME
217+
#if CFG_TUD_DFU
218+
return tinyusb_enable_interface(USB_INTERFACE_DFU, TUD_DFU_DESC_LEN(1), load_dfu_ota_descriptor) == ESP_OK;
219+
#elif CFG_TUD_DFU_RUNTIME
211220
return tinyusb_enable_interface(USB_INTERFACE_DFU, TUD_DFU_RT_DESC_LEN, load_dfu_descriptor) == ESP_OK;
212221
#endif /* CFG_TUD_DFU_RUNTIME */
213222
return false;

Diff for: cores/esp32/chip-debug-report.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ static void printBoardInfo(void){
242242
static void printPerimanInfo(void){
243243
chip_report_printf("GPIO Info:\n");
244244
chip_report_printf("------------------------------------------\n");
245+
#if defined(BOARD_HAS_PIN_REMAP)
246+
chip_report_printf(" DPIN|GPIO : BUS_TYPE[bus/unit][chan]\n");
247+
#else
245248
chip_report_printf(" GPIO : BUS_TYPE[bus/unit][chan]\n");
249+
#endif
246250
chip_report_printf(" -------------------------------------- \n");
247251
for(uint8_t i = 0; i < SOC_GPIO_PIN_COUNT; i++){
248252
if(!perimanPinIsValid(i)){
@@ -252,8 +256,17 @@ static void printPerimanInfo(void){
252256
if(type == ESP32_BUS_TYPE_INIT){
253257
continue;//unused pin
254258
}
255-
const char* extra_type = perimanGetPinBusExtraType(i);
259+
#if defined(BOARD_HAS_PIN_REMAP)
260+
int dpin = gpioNumberToDigitalPin(i);
261+
if (dpin < 0) {
262+
continue;//pin is not exported
263+
} else {
264+
chip_report_printf(" D%-3d|%4u : ", dpin, i);
265+
}
266+
#else
256267
chip_report_printf(" %4u : ", i);
268+
#endif
269+
const char* extra_type = perimanGetPinBusExtraType(i);
257270
if(extra_type){
258271
chip_report_printf("%s", extra_type);
259272
}

Diff for: cores/esp32/esp32-hal-periman.h

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#pragma once
8+
79
#ifdef __cplusplus
810
extern "C"
911
{

Diff for: cores/esp32/esp32-hal.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void yield(void);
8686
#include "esp32-hal-psram.h"
8787
#include "esp32-hal-rgb-led.h"
8888
#include "esp32-hal-cpu.h"
89+
#include "esp32-hal-periman.h"
8990

9091
void analogWrite(uint8_t pin, int value);
9192
void analogWriteFrequency(uint8_t pin, uint32_t freq);

0 commit comments

Comments
 (0)