Skip to content

Commit fccf573

Browse files
committed
Add option to select the core used for Arduino and it's events
1 parent c91105d commit fccf573

File tree

11 files changed

+109
-12
lines changed

11 files changed

+109
-12
lines changed

boards.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ menu.PSRAM=PSRAM
1111
menu.Revision=Board Revision
1212
menu.LORAWAN_REGION=LoRaWan Region
1313
menu.LoRaWanDebugLevel=LoRaWan Debug Level
14+
menu.LoopCore=Arduino Runs On
15+
menu.EventsCore=Events Run On
1416

1517
##############################################################
1618
### DO NOT PUT BOARDS ABOVE THE OFFICIAL ESPRESSIF BOARDS! ###
@@ -196,6 +198,8 @@ esp32.build.flash_mode=dio
196198
esp32.build.boot=dio
197199
esp32.build.partitions=default
198200
esp32.build.defines=
201+
esp32.build.loop_core=
202+
esp32.build.event_core=
199203

200204
esp32.menu.PSRAM.disabled=Disabled
201205
esp32.menu.PSRAM.disabled.build.defines=
@@ -299,6 +303,16 @@ esp32.menu.UploadSpeed.460800.upload.speed=460800
299303
esp32.menu.UploadSpeed.512000.windows=512000
300304
esp32.menu.UploadSpeed.512000.upload.speed=512000
301305

306+
esp32.menu.LoopCore.1=Core 1
307+
esp32.menu.LoopCore.1.build.loop_core=-DARDUINO_RUNNING_CORE=1
308+
esp32.menu.LoopCore.0=Core 0
309+
esp32.menu.LoopCore.0.build.loop_core=-DARDUINO_RUNNING_CORE=0
310+
311+
esp32.menu.EventsCore.1=Core 1
312+
esp32.menu.EventsCore.1.build.event_core=-DARDUINO_EVENT_RUNNING_CORE=1
313+
esp32.menu.EventsCore.0=Core 0
314+
esp32.menu.EventsCore.0.build.event_core=-DARDUINO_EVENT_RUNNING_CORE=0
315+
302316
esp32.menu.DebugLevel.none=None
303317
esp32.menu.DebugLevel.none.build.code_debug=0
304318
esp32.menu.DebugLevel.error=Error

cores/esp32/HardwareSerial.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class HardwareSerial: public Stream
113113
extern void serialEventRun(void) __attribute__((weak));
114114

115115
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
116+
#ifndef ARDUINO_SERIAL_PORT
117+
#define ARDUINO_SERIAL_PORT 0
118+
#endif
116119
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
117120
#include "USB.h"
118121
#include "USBCDC.h"

cores/esp32/USB.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "esp32-hal-tinyusb.h"
1616
#include "USB.h"
1717
#if CONFIG_USB_ENABLED
18-
#include "usb_persist.h"
1918

2019
#ifndef USB_VID
2120
#define USB_VID USB_ESPRESSIF_VID

cores/esp32/USBCDC.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "USB.h"
1717
#include "USBCDC.h"
1818
#if CONFIG_USB_ENABLED
19-
#include "usb_persist.h"
2019

2120
ESP_EVENT_DEFINE_BASE(ARDUINO_USB_CDC_EVENTS);
2221
esp_err_t arduino_usb_event_post(esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, TickType_t ticks_to_wait);

cores/esp32/esp32-hal-log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C"
2020
#endif
2121

2222
#include "sdkconfig.h"
23+
#include "esp_timer.h"
2324

2425
#define ARDUHAL_LOG_LEVEL_NONE (0)
2526
#define ARDUHAL_LOG_LEVEL_ERROR (1)

cores/esp32/esp32-hal-tinyusb.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "esp32-hal.h"
3030

3131
#include "esp32-hal-tinyusb.h"
32-
#include "usb_persist.h"
32+
#include "esp32s2/rom/usb/usb_persist.h"
3333

3434
typedef char tusb_str_t[127];
3535

@@ -309,6 +309,8 @@ __attribute__ ((weak)) int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_
309309
/*
310310
* Private API
311311
* */
312+
static bool usb_persist_enabled = false;
313+
static restart_type_t usb_persist_mode = RESTART_NO_PERSIST;
312314

313315
static bool tinyusb_reserve_in_endpoint(uint8_t endpoint){
314316
if(endpoint > 6 || (tinyusb_endpoints.in & BIT(endpoint)) != 0){
@@ -381,7 +383,7 @@ static bool tinyusb_load_enabled_interfaces(){
381383
};
382384
memcpy(tinyusb_config_descriptor, descriptor, TUD_CONFIG_DESC_LEN);
383385
if ((tinyusb_loaded_interfaces_mask == (BIT(USB_INTERFACE_CDC) | BIT(USB_INTERFACE_DFU))) || (tinyusb_loaded_interfaces_mask == BIT(USB_INTERFACE_CDC))) {
384-
usb_persist_set_enable(true);
386+
usb_persist_enabled = true;
385387
log_d("USB Persist enabled");
386388
}
387389
log_d("Load Done: if_num: %u, descr_len: %u, if_mask: 0x%x", tinyusb_loaded_interfaces_num, tinyusb_config_descriptor_len, tinyusb_loaded_interfaces_mask);
@@ -451,6 +453,34 @@ static void tinyusb_apply_device_config(tinyusb_device_config_t *config){
451453
tinyusb_device_descriptor.bDeviceProtocol = config->usb_protocol;
452454
}
453455

456+
static void IRAM_ATTR usb_persist_shutdown_handler(void)
457+
{
458+
if(usb_persist_mode != RESTART_NO_PERSIST){
459+
if (usb_persist_enabled) {
460+
REG_SET_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_IO_MUX_RESET_DISABLE);
461+
REG_SET_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE);
462+
}
463+
if (usb_persist_mode == RESTART_BOOTLOADER) {
464+
//USB CDC Download
465+
if (usb_persist_enabled) {
466+
USB_WRAP.date.val = USBDC_PERSIST_ENA;
467+
}
468+
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
469+
periph_module_disable(PERIPH_TIMG1_MODULE);
470+
} else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
471+
//DFU Download
472+
USB_WRAP.date.val = USBDC_BOOT_DFU;
473+
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
474+
periph_module_disable(PERIPH_TIMG0_MODULE);
475+
periph_module_disable(PERIPH_TIMG1_MODULE);
476+
} else if (usb_persist_enabled) {
477+
//USB Persist reboot
478+
USB_WRAP.date.val = USBDC_PERSIST_ENA;
479+
}
480+
SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_PROCPU_RST);
481+
}
482+
}
483+
454484
// USB Device Driver task
455485
// This top level thread processes all usb events and invokes callbacks
456486
static void usb_device_task(void *param) {
@@ -488,6 +518,24 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
488518
initialized = false;
489519
return ESP_FAIL;
490520
}
521+
522+
bool usb_did_persist = (USB_WRAP.date.val == USBDC_PERSIST_ENA);
523+
524+
if(usb_did_persist && usb_persist_enabled){
525+
// Enable USB/IO_MUX peripheral reset, if coming from persistent reboot
526+
REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_IO_MUX_RESET_DISABLE);
527+
REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE);
528+
} else {
529+
// Reset USB module
530+
periph_module_reset(PERIPH_USB_MODULE);
531+
periph_module_enable(PERIPH_USB_MODULE);
532+
}
533+
534+
if (usb_persist_enabled && esp_register_shutdown_handler(usb_persist_shutdown_handler) != ESP_OK) {
535+
initialized = false;
536+
return ESP_FAIL;
537+
}
538+
491539
tinyusb_config_t tusb_cfg = {
492540
.external_phy = false // In the most cases you need to use a `false` value
493541
};
@@ -500,6 +548,16 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
500548
return err;
501549
}
502550

551+
void usb_persist_restart(restart_type_t mode)
552+
{
553+
if (usb_persist_enabled && mode < RESTART_TYPE_MAX) {
554+
usb_persist_mode = mode;
555+
esp_restart();
556+
} else {
557+
log_e("Persistence is not enabled");
558+
}
559+
}
560+
503561
uint8_t tinyusb_add_string_descriptor(const char * str){
504562
if(str == NULL || tinyusb_string_descriptor_len >= MAX_STRING_DESCRIPTORS){
505563
return 0;

cores/esp32/esp32-hal-tinyusb.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ typedef struct {
6161

6262
esp_err_t tinyusb_init(tinyusb_device_config_t *config);
6363

64+
/*
65+
* USB Persistence API
66+
* */
67+
typedef enum {
68+
RESTART_NO_PERSIST,
69+
RESTART_PERSIST,
70+
RESTART_BOOTLOADER,
71+
RESTART_BOOTLOADER_DFU,
72+
RESTART_TYPE_MAX
73+
} restart_type_t;
74+
75+
void usb_persist_restart(restart_type_t mode);
76+
6477
// The following definitions and functions are to be used only by the drivers
6578
typedef enum {
6679
USB_INTERFACE_CDC,

cores/esp32/esp32-hal.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
#ifndef HAL_ESP32_HAL_H_
2121
#define HAL_ESP32_HAL_H_
2222

23-
#ifdef __cplusplus
24-
extern "C" {
25-
#endif
26-
2723
#include <stdint.h>
2824
#include <stdbool.h>
2925
#include <stdio.h>
@@ -36,6 +32,10 @@ extern "C" {
3632
#include "esp_system.h"
3733
#include "esp_sleep.h"
3834

35+
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
3939
#ifndef F_CPU
4040
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
4141
#define F_CPU (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000U)
@@ -52,6 +52,14 @@ extern "C" {
5252
#define ARDUINO_ISR_FLAG (0)
5353
#endif
5454

55+
#ifndef ARDUINO_RUNNING_CORE
56+
#define ARDUINO_RUNNING_CORE CONFIG_ARDUINO_RUNNING_CORE
57+
#endif
58+
59+
#ifndef ARDUINO_EVENT_RUNNING_CORE
60+
#define ARDUINO_EVENT_RUNNING_CORE CONFIG_ARDUINO_EVENT_RUNNING_CORE
61+
#endif
62+
5563
//forward declaration from freertos/portmacro.h
5664
void vPortYield(void);
5765
void yield(void);

cores/esp32/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern "C" void app_main()
4848
#endif
4949
loopTaskWDTEnabled = false;
5050
initArduino();
51-
xTaskCreateUniversal(loopTask, "loopTask", CONFIG_ARDUINO_LOOP_STACK_SIZE, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);
51+
xTaskCreateUniversal(loopTask, "loopTask", CONFIG_ARDUINO_LOOP_STACK_SIZE, NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE);
5252
}
5353

5454
#endif

libraries/WiFi/src/WiFiGeneric.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern "C" {
4545

4646
} //extern "C"
4747

48-
#include "esp32-hal-log.h"
48+
#include "esp32-hal.h"
4949
#include <vector>
5050
#include "sdkconfig.h"
5151

@@ -475,7 +475,7 @@ static bool _start_network_event_task(){
475475
}
476476

477477
if(!_arduino_event_task_handle){
478-
xTaskCreateUniversal(_arduino_event_task, "arduino_events", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_arduino_event_task_handle, CONFIG_ARDUINO_EVENT_RUNNING_CORE);
478+
xTaskCreateUniversal(_arduino_event_task, "arduino_events", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_arduino_event_task_handle, ARDUINO_EVENT_RUNNING_CORE);
479479
if(!_arduino_event_task_handle){
480480
log_e("Network Event Task Start Failed!");
481481
return false;

platform.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ build.flash_mode=dio
9090
build.boot=bootloader
9191
build.code_debug=0
9292
build.defines=
93-
build.extra_flags=-DESP32 -DCORE_DEBUG_LEVEL={build.code_debug} {build.defines} {build.extra_flags.{build.mcu}}
93+
build.loop_core=
94+
build.event_core=
95+
build.extra_flags=-DESP32 -DCORE_DEBUG_LEVEL={build.code_debug} {build.loop_core} {build.event_core} {build.defines} {build.extra_flags.{build.mcu}}
9496

9597
# Check if custom partitions exist
9698
recipe.hooks.prebuild.1.pattern=bash -c "[ ! -f "{build.source.path}"/partitions.csv ] || cp -f "{build.source.path}"/partitions.csv "{build.path}"/partitions.csv"

0 commit comments

Comments
 (0)