diff options
Diffstat (limited to 'ports/esp32s2')
25 files changed, 330 insertions, 64 deletions
diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile index be4b2f00f..5ad5c2f1d 100644 --- a/ports/esp32s2/Makefile +++ b/ports/esp32s2/Makefile @@ -31,6 +31,9 @@ else endif endif +# If the flash PORT is not given, use the default /dev/tty.SLAB_USBtoUART. +PORT ?= /dev/tty.SLAB_USBtoUART + # If the build directory is not given, make it reflect the board name. BUILD ?= build-$(BOARD) @@ -107,7 +110,7 @@ endif CFLAGS += $(INC) -Werror -Wall -mlongcalls -std=gnu11 -Wl,--gc-sections $(BASE_CFLAGS) $(C_DEFS) $(CFLAGS_MOD) $(COPT) -LDFLAGS = $(CFLAGS) -fshort-enums -Wl,-nostdlib -Wl,-Map=$@.map -Wl,-cref +LDFLAGS = $(CFLAGS) -Wl,-nostdlib -Wl,-Map=$@.map -Wl,-cref LDFLAGS += -L$(BUILD)/esp-idf/esp-idf/esp32s2 \ -Tesp32s2_out.ld \ -L$(BUILD)/esp-idf/esp-idf/esp32s2/ld \ @@ -149,6 +152,7 @@ SRC_C += \ mphalport.c \ boards/$(BOARD)/board.c \ boards/$(BOARD)/pins.c \ + modules/$(CIRCUITPY_MODULE).c \ lib/libc/string0.c \ lib/mp-readline/readline.c \ lib/oofatfs/ff.c \ @@ -270,7 +274,7 @@ $(BUILD)/firmware.bin: $(BUILD)/esp-idf/partition_table/partition-table.bin $(BU $(Q)$(PYTHON) ../../tools/join_bins.py $@ 0x1000 $(BUILD)/esp-idf/bootloader/bootloader.bin 0x8000 $(BUILD)/esp-idf/partition_table/partition-table.bin 0x10000 $(BUILD)/circuitpython-firmware.bin flash: $(BUILD)/firmware.bin - esptool.py --chip esp32s2 -p /dev/tty.SLAB_USBtoUART -b 460800 --before=default_reset --after=hard_reset write_flash $(FLASH_FLAGS) 0x0000 $^ + esptool.py --chip esp32s2 -p $(PORT) -b 460800 --before=default_reset --after=hard_reset write_flash $(FLASH_FLAGS) 0x0000 $^ include $(TOP)/py/mkrules.mk diff --git a/ports/esp32s2/boards/espressif_saola_1_wroom/board.c b/ports/esp32s2/boards/espressif_saola_1_wroom/board.c index a3a351f27..b7b2c4ef5 100644 --- a/ports/esp32s2/boards/espressif_saola_1_wroom/board.c +++ b/ports/esp32s2/boards/espressif_saola_1_wroom/board.c @@ -26,8 +26,16 @@ #include "boards/board.h" #include "mpconfigboard.h" +#include "shared-bindings/microcontroller/Pin.h" void board_init(void) { + // USB + never_reset_pin(&pin_GPIO19); + never_reset_pin(&pin_GPIO20); + + // Debug UART + never_reset_pin(&pin_GPIO43); + never_reset_pin(&pin_GPIO44); } bool board_requests_safe_mode(void) { diff --git a/ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.mk b/ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.mk index aa82588a5..cd2735615 100644 --- a/ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.mk +++ b/ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.mk @@ -18,3 +18,5 @@ CIRCUITPY_MICROCONTROLLER = 0 CIRCUITPY_ESP_FLASH_MODE=dio CIRCUITPY_ESP_FLASH_FREQ=40m CIRCUITPY_ESP_FLASH_SIZE=4MB + +CIRCUITPY_MODULE=wroom diff --git a/ports/esp32s2/boards/espressif_saola_1_wrover/board.c b/ports/esp32s2/boards/espressif_saola_1_wrover/board.c index a3a351f27..b7b2c4ef5 100644 --- a/ports/esp32s2/boards/espressif_saola_1_wrover/board.c +++ b/ports/esp32s2/boards/espressif_saola_1_wrover/board.c @@ -26,8 +26,16 @@ #include "boards/board.h" #include "mpconfigboard.h" +#include "shared-bindings/microcontroller/Pin.h" void board_init(void) { + // USB + never_reset_pin(&pin_GPIO19); + never_reset_pin(&pin_GPIO20); + + // Debug UART + never_reset_pin(&pin_GPIO43); + never_reset_pin(&pin_GPIO44); } bool board_requests_safe_mode(void) { diff --git a/ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.mk b/ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.mk index ff7e6228a..0b847de94 100644 --- a/ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.mk +++ b/ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.mk @@ -18,3 +18,5 @@ CIRCUITPY_MICROCONTROLLER = 0 CIRCUITPY_ESP_FLASH_MODE=dio CIRCUITPY_ESP_FLASH_FREQ=40m CIRCUITPY_ESP_FLASH_SIZE=4MB + +CIRCUITPY_MODULE=wrover diff --git a/ports/esp32s2/common-hal/digitalio/DigitalInOut.c b/ports/esp32s2/common-hal/digitalio/DigitalInOut.c index 349da6bf4..7745a9c0a 100644 --- a/ports/esp32s2/common-hal/digitalio/DigitalInOut.c +++ b/ports/esp32s2/common-hal/digitalio/DigitalInOut.c @@ -30,24 +30,18 @@ #include "driver/gpio.h" - -#include "esp_log.h" - -static const char* TAG = "CircuitPython digitalio"; +#include "esp-idf/components/soc/include/hal/gpio_hal.h" void common_hal_digitalio_digitalinout_never_reset( digitalio_digitalinout_obj_t *self) { - (void)self; + never_reset_pin_number(self->pin->number); } digitalinout_result_t common_hal_digitalio_digitalinout_construct( digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { - - // claim_pin(pin); + claim_pin(pin); self->pin = pin; - ESP_EARLY_LOGW(TAG, "construct %d", pin->number); - return DIGITALINOUT_OK; } @@ -60,78 +54,86 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self return; } - // reset_pin_number(0, self->pin->number); + reset_pin_number(self->pin->number); self->pin = mp_const_none; } void common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - gpio_config_t config = { - .pin_bit_mask = 1 << self->pin->number, - .mode = GPIO_MODE_INPUT - }; - if (pull == PULL_UP) { - config.pull_up_en = GPIO_PULLUP_ENABLE; - } else if (pull == PULL_DOWN) { - config.pull_down_en = GPIO_PULLDOWN_ENABLE; - } - - gpio_config(&config); + common_hal_digitalio_digitalinout_set_pull(self, pull); + gpio_set_direction(self->pin->number, GPIO_MODE_DEF_INPUT); } -void common_hal_digitalio_digitalinout_switch_to_output( +digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { - gpio_config_t config = { - .pin_bit_mask = 1ULL << self->pin->number, - .mode = GPIO_MODE_OUTPUT - }; - if (drive_mode == DRIVE_MODE_OPEN_DRAIN) { - config.mode = GPIO_MODE_OUTPUT_OD; - } gpio_set_level(self->pin->number, value); - gpio_config(&config); + return common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode); } digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( digitalio_digitalinout_obj_t *self) { - + uint32_t iomux = READ_PERI_REG(GPIO_PIN_MUX_REG[self->pin->number]); + if ((iomux & FUN_IE) != 0) { + return DIRECTION_INPUT; + } return DIRECTION_OUTPUT; } void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t *self, bool value) { - ESP_EARLY_LOGW(TAG, "set %d %d", self->pin->number, value); gpio_set_level(self->pin->number, value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - return true; + return gpio_get_level(self->pin->number) == 1; } -void common_hal_digitalio_digitalinout_set_drive_mode( +digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { - (void)self; - (void)drive_mode; + gpio_num_t number = self->pin->number; + gpio_mode_t mode; + if (drive_mode == DRIVE_MODE_OPEN_DRAIN) { + mode = GPIO_MODE_DEF_OD; + } else { + mode = GPIO_MODE_DEF_OUTPUT; + } + esp_err_t result = gpio_set_direction(number, mode); + if (result != ESP_OK) { + return DIGITALINOUT_INPUT_ONLY; + } + return DIGITALINOUT_OK; } digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_digitalinout_obj_t *self) { - // if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) - // return DRIVE_MODE_PUSH_PULL; - // else - return DRIVE_MODE_OPEN_DRAIN; + if (GPIO_HAL_GET_HW(GPIO_PORT_0)->pin[self->pin->number].pad_driver == 1) { + return DRIVE_MODE_OPEN_DRAIN; + } + return DRIVE_MODE_PUSH_PULL; } void common_hal_digitalio_digitalinout_set_pull( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - (void)self; - (void)pull; + gpio_num_t number = self->pin->number; + gpio_pullup_dis(number); + gpio_pulldown_dis(number); + if (pull == PULL_UP) { + gpio_pullup_en(number); + } else if (pull == PULL_DOWN) { + gpio_pulldown_en(number); + } } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( digitalio_digitalinout_obj_t *self) { + gpio_num_t gpio_num = self->pin->number; + if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU) == 1) { + return PULL_UP; + } else if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD) == 1) { + return PULL_DOWN; + } return PULL_NONE; } diff --git a/ports/esp32s2/common-hal/microcontroller/Pin.c b/ports/esp32s2/common-hal/microcontroller/Pin.c index e142191fd..4c39eea16 100644 --- a/ports/esp32s2/common-hal/microcontroller/Pin.c +++ b/ports/esp32s2/common-hal/microcontroller/Pin.c @@ -29,18 +29,51 @@ #include "py/mphal.h" +#include "esp-idf/components/driver/include/driver/gpio.h" +#include "esp-idf/components/soc/include/hal/gpio_hal.h" + +STATIC uint32_t never_reset_pins[2]; +STATIC uint32_t in_use[2]; + +void never_reset_pin_number(gpio_num_t pin_number) { + never_reset_pins[pin_number / 32] |= 1 << pin_number % 32; +} + +void never_reset_pin(const mcu_pin_obj_t* pin) { + never_reset_pin_number(pin->number); +} + // Mark pin as free and return it to a quiescent state. -void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { +void reset_pin_number(gpio_num_t pin_number) { + never_reset_pins[pin_number / 32] &= ~(1 << pin_number % 32); + in_use[pin_number / 32] &= ~(1 << pin_number % 32); } +void reset_all_pins(void) { + for (uint8_t i = 0; i < GPIO_PIN_COUNT; i++) { + uint32_t iomux_address = GPIO_PIN_MUX_REG[i]; + if (iomux_address == 0 || + (never_reset_pins[i / 32] & (1 << i % 32)) != 0) { + continue; + } + gpio_set_direction(i, GPIO_MODE_DEF_INPUT); + gpio_pullup_dis(i); + gpio_pulldown_dis(i); + } + in_use[0] = 0; + in_use[1] = 0; +} void claim_pin(const mcu_pin_obj_t* pin) { + in_use[pin->number / 32] |= (1 << pin->number % 32); } -bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) { - return true; +bool pin_number_is_free(gpio_num_t pin_number) { + uint8_t offset = pin_number / 32; + uint8_t mask = 1 << pin_number % 32; + return (never_reset_pins[offset] & mask) == 0 && (in_use[offset] & mask) == 0; } bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { - return pin_number_is_free(0, pin->number); + return pin_number_is_free(pin->number); } diff --git a/ports/esp32s2/common-hal/microcontroller/Pin.h b/ports/esp32s2/common-hal/microcontroller/Pin.h index ab5574422..6f47b1ed3 100644 --- a/ports/esp32s2/common-hal/microcontroller/Pin.h +++ b/ports/esp32s2/common-hal/microcontroller/Pin.h @@ -34,11 +34,10 @@ void reset_all_pins(void); // reset_pin_number takes the pin number instead of the pointer so that objects don't // need to store a full pointer. -void reset_pin_number(uint8_t pin_port, uint8_t pin_number); +void reset_pin_number(gpio_num_t pin_number); void claim_pin(const mcu_pin_obj_t* pin); -bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number); -void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number); -// GPIO_TypeDef * pin_port(uint8_t pin_port); -uint16_t pin_mask(uint8_t pin_number); +bool pin_number_is_free(gpio_num_t pin_number); +void never_reset_pin_number(gpio_num_t pin_number); +void never_reset_pin(const mcu_pin_obj_t* pin); #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c index a36d0c440..6b2e18673 100644 --- a/ports/esp32s2/common-hal/microcontroller/__init__.c +++ b/ports/esp32s2/common-hal/microcontroller/__init__.c @@ -39,24 +39,31 @@ #include "supervisor/filesystem.h" #include "supervisor/shared/safe_mode.h" +#include "freertos/FreeRTOS.h" + void common_hal_mcu_delay_us(uint32_t delay) { } volatile uint32_t nesting_count = 0; +static portMUX_TYPE cp_mutex = portMUX_INITIALIZER_UNLOCKED; void common_hal_mcu_disable_interrupts(void) { + if (nesting_count == 0) { + portENTER_CRITICAL(&cp_mutex); + } nesting_count++; } void common_hal_mcu_enable_interrupts(void) { if (nesting_count == 0) { - + // Maybe log here because it's very bad. } nesting_count--; if (nesting_count > 0) { return; } + portEXIT_CRITICAL(&cp_mutex); } void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { diff --git a/ports/esp32s2/common-hal/supervisor/Runtime.c b/ports/esp32s2/common-hal/supervisor/Runtime.c index feab6987d..ea663f897 100644 --- a/ports/esp32s2/common-hal/supervisor/Runtime.c +++ b/ports/esp32s2/common-hal/supervisor/Runtime.c @@ -35,4 +35,3 @@ bool common_hal_get_serial_connected(void) { bool common_hal_get_serial_bytes_available(void) { return (bool) serial_bytes_available(); } - diff --git a/ports/esp32s2/common-hal/supervisor/__init__.c b/ports/esp32s2/common-hal/supervisor/__init__.c index ac88556b4..6dca35fb5 100644 --- a/ports/esp32s2/common-hal/supervisor/__init__.c +++ b/ports/esp32s2/common-hal/supervisor/__init__.c @@ -37,4 +37,4 @@ const super_runtime_obj_t common_hal_supervisor_runtime_obj = { .base = { .type = &supervisor_runtime_type, }, -};
\ No newline at end of file +}; diff --git a/ports/esp32s2/esp-idf b/ports/esp32s2/esp-idf -Subproject 0daf6e0e41f95d22d193d08941a00df9525bc40 +Subproject 7aae7f034bab68d2dd6aaa763924c91eb697d87 diff --git a/ports/esp32s2/modules/module.h b/ports/esp32s2/modules/module.h new file mode 100644 index 000000000..6c40e30e0 --- /dev/null +++ b/ports/esp32s2/modules/module.h @@ -0,0 +1,35 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#ifndef MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H +#define MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H + +#include "shared-bindings/microcontroller/Pin.h" + +void never_reset_module_internal_pins(void); + +#endif // MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H diff --git a/ports/esp32s2/modules/none.c b/ports/esp32s2/modules/none.c new file mode 100644 index 000000000..9b5433bd9 --- /dev/null +++ b/ports/esp32s2/modules/none.c @@ -0,0 +1,28 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +void never_reset_module_internal_pins(void) { +} diff --git a/ports/esp32s2/modules/wroom.c b/ports/esp32s2/modules/wroom.c new file mode 100644 index 000000000..16c586118 --- /dev/null +++ b/ports/esp32s2/modules/wroom.c @@ -0,0 +1,37 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "modules/module.h" + +void never_reset_module_internal_pins(void) { + // SPI Flash + never_reset_pin(&pin_GPIO27); + never_reset_pin(&pin_GPIO28); + never_reset_pin(&pin_GPIO29); + never_reset_pin(&pin_GPIO30); + never_reset_pin(&pin_GPIO31); + never_reset_pin(&pin_GPIO32); +} diff --git a/ports/esp32s2/modules/wrover.c b/ports/esp32s2/modules/wrover.c new file mode 100644 index 000000000..d589a8fd4 --- /dev/null +++ b/ports/esp32s2/modules/wrover.c @@ -0,0 +1,38 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "modules/module.h" + +void never_reset_module_internal_pins(void) { + // SPI Flash and RAM + never_reset_pin(&pin_GPIO26); + never_reset_pin(&pin_GPIO27); + never_reset_pin(&pin_GPIO28); + never_reset_pin(&pin_GPIO29); + never_reset_pin(&pin_GPIO30); + never_reset_pin(&pin_GPIO31); + never_reset_pin(&pin_GPIO32); +} diff --git a/ports/esp32s2/mpconfigport.h b/ports/esp32s2/mpconfigport.h index c307d4791..d340bb480 100644 --- a/ports/esp32s2/mpconfigport.h +++ b/ports/esp32s2/mpconfigport.h @@ -32,6 +32,7 @@ #define MICROPY_NLR_THUMB (0) #define MICROPY_PY_UJSON (0) +#define MICROPY_USE_INTERNAL_PRINTF (0) #include "py/circuitpy_mpconfig.h" diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk index 6d66d9057..c9df81db0 100644 --- a/ports/esp32s2/mpconfigport.mk +++ b/ports/esp32s2/mpconfigport.mk @@ -33,3 +33,5 @@ CIRCUITPY_TOUCHIO = 0 # Enable USB support CIRCUITPY_USB_HID = 1 CIRCUITPY_USB_MIDI = 1 + +CIRCUITPY_MODULE ?= none diff --git a/ports/esp32s2/mphalport.c b/ports/esp32s2/mphalport.c index 772b61c9e..da5258b0e 100644 --- a/ports/esp32s2/mphalport.c +++ b/ports/esp32s2/mphalport.c @@ -37,6 +37,24 @@ void mp_hal_delay_us(mp_uint_t delay) { mp_hal_delay_ms(delay / 1000); } -mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) { +// This is provided by the esp-idf/components/xtensa/esp32s2/libhal.a binary +// blob. +extern void xthal_window_spill(void); + +mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs, uint8_t reg_count) { + // xtensa has more registers than an instruction can address. The 16 that + // can be addressed are called the "window". When a function is called or + // returns the window rotates. This allows for more efficient function calls + // because ram doesn't need to be used. It's only used if the window wraps + // around onto itself. At that point values are "spilled" to empty spots in + // the stack that were set aside. When the window rotates back around (on + // function return), the values are restored into the register from ram. + + // So, in order to read the values in the stack scan we must make sure all + // of the register values we care about have been spilled to RAM. Luckily, + // there is a HAL call to do it. There is a bit of a race condition here + // because the register value could change after it's been restored but that + // is unlikely to happen with a heap pointer while we do a GC. + xthal_window_spill(); return (mp_uint_t) __builtin_frame_address(0); } diff --git a/ports/esp32s2/peripherals/pins.h b/ports/esp32s2/peripherals/pins.h index 1a74b0367..07d0b908e 100644 --- a/ports/esp32s2/peripherals/pins.h +++ b/ports/esp32s2/peripherals/pins.h @@ -33,10 +33,12 @@ #include <stdint.h> #include "esp32s2_peripherals_config.h" +#include "esp-idf/config/sdkconfig.h" +#include "esp-idf/components/soc/include/hal/gpio_types.h" typedef struct { PIN_PREFIX_FIELDS - uint8_t number; + gpio_num_t number; } mcu_pin_obj_t; extern const mcu_pin_obj_t pin_GPIO0; diff --git a/ports/esp32s2/sdkconfig.defaults b/ports/esp32s2/sdkconfig.defaults index f7deb53fb..958e1852e 100644 --- a/ports/esp32s2/sdkconfig.defaults +++ b/ports/esp32s2/sdkconfig.defaults @@ -259,7 +259,7 @@ CONFIG_ESP32S2_RTC_CLK_CAL_CYCLES=576 CONFIG_ESP_ERR_TO_NAME_LOOKUP=y CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 CONFIG_ESP_CONSOLE_UART_DEFAULT=y diff --git a/ports/esp32s2/supervisor/internal_flash.c b/ports/esp32s2/supervisor/internal_flash.c index aab7e587a..a8d3fa485 100644 --- a/ports/esp32s2/supervisor/internal_flash.c +++ b/ports/esp32s2/supervisor/internal_flash.c @@ -100,18 +100,20 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32 _cache_lba = sector_offset; } for (uint8_t b = block_offset; b < blocks_per_sector; b++) { + // Stop copying after the last block. + if (block >= num_blocks) { + break; + } memcpy(_cache + b * FILESYSTEM_BLOCK_SIZE, src + block * FILESYSTEM_BLOCK_SIZE, FILESYSTEM_BLOCK_SIZE); block++; } result = esp_partition_erase_range(_partition, sector_offset, SECTOR_SIZE); - ESP_EARLY_LOGW(TAG, "erase %d", result); result = esp_partition_write(_partition, sector_offset, _cache, SECTOR_SIZE); - ESP_EARLY_LOGW(TAG, "write %d", result); } return 0; // success @@ -119,4 +121,3 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32 void supervisor_flash_release_cache(void) { } - diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 4389bb6a8..4c42f11af 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -29,10 +29,12 @@ #include <sys/time.h> #include "supervisor/port.h" #include "boards/board.h" +#include "modules/module.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "common-hal/microcontroller/Pin.h" #include "supervisor/memory.h" #include "supervisor/shared/tick.h" @@ -56,12 +58,14 @@ safe_mode_t port_init(void) { if (result != ESP_OK) { ESP_EARLY_LOGE(TAG, "Unable to create tick timer."); } + never_reset_module_internal_pins(); ESP_EARLY_LOGW(TAG, "port init done"); return NO_SAFE_MODE; } void reset_port(void) { + reset_all_pins(); } void reset_to_bootloader(void) { @@ -148,6 +152,9 @@ void port_sleep_until_interrupt(void) { // FreeRTOS delay here maybe. // Light sleep shuts down BLE and wifi. // esp_light_sleep_start() + if (sleep_time_duration == 0) { + return; + } vTaskDelayUntil(&sleep_time_set, sleep_time_duration); } diff --git a/ports/esp32s2/supervisor/usb.c b/ports/esp32s2/supervisor/usb.c index 7c508485e..39eb8204f 100644 --- a/ports/esp32s2/supervisor/usb.c +++ b/ports/esp32s2/supervisor/usb.c @@ -29,6 +29,11 @@ #include "lib/utils/interrupt_char.h" #include "lib/mp-readline/readline.h" +#include "esp-idf/components/soc/soc/esp32s2/include/soc/usb_periph.h" +#include "esp-idf/components/driver/include/driver/periph_ctrl.h" +#include "esp-idf/components/driver/include/driver/gpio.h" +#include "esp-idf/components/esp_rom/include/esp32s2/rom/gpio.h" + #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -64,17 +69,22 @@ void usb_device_task(void* param) } void init_usb_hardware(void) { + periph_module_reset(PERIPH_USB_MODULE); + periph_module_enable(PERIPH_USB_MODULE); usb_hal_context_t hal = { .use_external_phy = false // use built-in PHY }; usb_hal_init(&hal); + // Initialize the pin drive strength. + gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3); + gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3); + (void) xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, - configMAX_PRIORITIES-1, + 5, usb_device_stack, &usb_device_taskdef); } - diff --git a/ports/esp32s2/tools/decode_backtrace.py b/ports/esp32s2/tools/decode_backtrace.py new file mode 100644 index 000000000..3f078895a --- /dev/null +++ b/ports/esp32s2/tools/decode_backtrace.py @@ -0,0 +1,23 @@ +"""Simple script that translates "Backtrace:" lines from the ESP output to files + and line numbers. + + Run with: python3 tools/decode_backtrace.py <board> + + Enter the backtrace line at the "? " prompt. CTRL-C to exit the script. + """ + +import subprocess +import sys + +board = sys.argv[1] +print(board) + +while True: + addresses = input("? ") + if addresses.startswith("Backtrace:"): + addresses = addresses[len("Backtrace:"):] + addresses = addresses.strip().split() + addresses = [address.split(":")[0] for address in addresses] + print('got', addresses) + subprocess.run(["xtensa-esp32s2-elf-addr2line", + "-e", "build-{}/firmware.elf".format(board)] + addresses) |
