2020-04-17 19:23:28 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2019 Lucian Copeland 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 <stdint.h>
|
|
|
|
#include <sys/time.h>
|
2020-12-02 21:05:29 -05:00
|
|
|
#include "supervisor/board.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
#include "supervisor/port.h"
|
2020-05-19 20:46:29 -04:00
|
|
|
#include "modules/module.h"
|
2020-11-05 22:27:21 -05:00
|
|
|
#include "py/runtime.h"
|
2020-11-10 13:45:39 -05:00
|
|
|
#include "supervisor/esp_port.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
2020-05-19 20:46:29 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2020-10-06 15:00:26 -04:00
|
|
|
#include "common-hal/analogio/AnalogOut.h"
|
2020-05-28 18:51:33 -04:00
|
|
|
#include "common-hal/busio/I2C.h"
|
|
|
|
#include "common-hal/busio/SPI.h"
|
|
|
|
#include "common-hal/busio/UART.h"
|
2020-12-17 14:04:56 -05:00
|
|
|
#include "common-hal/dualbank/__init__.h"
|
2020-11-19 01:14:22 -05:00
|
|
|
#include "common-hal/ps2io/Ps2.h"
|
2020-08-10 12:14:16 -04:00
|
|
|
#include "common-hal/pulseio/PulseIn.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "common-hal/pwmio/PWMOut.h"
|
2020-11-14 01:11:14 -05:00
|
|
|
#include "common-hal/watchdog/WatchDogTimer.h"
|
2021-01-13 19:05:07 -05:00
|
|
|
#include "common-hal/socketpool/Socket.h"
|
2020-07-30 20:58:12 -04:00
|
|
|
#include "common-hal/wifi/__init__.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
#include "supervisor/memory.h"
|
|
|
|
#include "supervisor/shared/tick.h"
|
2020-09-22 02:07:12 -04:00
|
|
|
#include "shared-bindings/rtc/__init__.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2020-08-21 19:20:58 -04:00
|
|
|
#include "peripherals/rmt.h"
|
2020-11-12 06:00:30 -05:00
|
|
|
#include "peripherals/pcnt.h"
|
2020-11-18 01:46:14 -05:00
|
|
|
#include "peripherals/timer.h"
|
2020-12-19 02:26:34 -05:00
|
|
|
#include "peripherals/touch.h"
|
2020-12-11 23:34:01 -05:00
|
|
|
#include "components/esp_rom/include/esp32s2/rom/ets_sys.h"
|
2020-09-28 18:59:29 -04:00
|
|
|
#include "components/heap/include/esp_heap_caps.h"
|
2020-12-07 19:39:54 -05:00
|
|
|
#include "components/xtensa/include/esp_debug_helpers.h"
|
2020-09-28 18:59:29 -04:00
|
|
|
#include "components/soc/soc/esp32s2/include/soc/cache_memory.h"
|
2020-12-07 19:39:54 -05:00
|
|
|
#include "components/soc/soc/esp32s2/include/soc/rtc_cntl_reg.h"
|
2020-07-30 20:58:12 -04:00
|
|
|
|
2020-10-30 11:05:16 -04:00
|
|
|
#if CIRCUITPY_AUDIOBUSIO
|
2021-01-09 14:48:36 -05:00
|
|
|
#include "common-hal/audiobusio/__init__.h"
|
2020-10-30 11:05:16 -04:00
|
|
|
#endif
|
|
|
|
|
2020-08-13 19:55:49 -04:00
|
|
|
#define HEAP_SIZE (48 * 1024)
|
2020-07-30 20:58:12 -04:00
|
|
|
|
2020-08-21 19:20:58 -04:00
|
|
|
uint32_t* heap;
|
|
|
|
uint32_t heap_size;
|
2020-08-04 10:59:05 -04:00
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
STATIC esp_timer_handle_t _tick_timer;
|
2020-12-08 20:13:00 -05:00
|
|
|
STATIC esp_timer_handle_t _sleep_timer;
|
|
|
|
|
|
|
|
TaskHandle_t circuitpython_task = NULL;
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2020-09-25 14:28:04 -04:00
|
|
|
extern void esp_restart(void) NORETURN;
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
void tick_timer_cb(void* arg) {
|
|
|
|
supervisor_tick();
|
2020-12-22 19:13:02 -05:00
|
|
|
|
|
|
|
// CircuitPython's VM is run in a separate FreeRTOS task from timer callbacks. So, we have to
|
|
|
|
// notify the main task every time in case it's waiting for us.
|
|
|
|
xTaskNotifyGive(circuitpython_task);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:13:00 -05:00
|
|
|
void sleep_timer_cb(void* arg);
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
safe_mode_t port_init(void) {
|
|
|
|
esp_timer_create_args_t args;
|
|
|
|
args.callback = &tick_timer_cb;
|
|
|
|
args.arg = NULL;
|
|
|
|
args.dispatch_method = ESP_TIMER_TASK;
|
|
|
|
args.name = "CircuitPython Tick";
|
2020-06-26 19:56:17 -04:00
|
|
|
esp_timer_create(&args, &_tick_timer);
|
2020-07-30 20:58:12 -04:00
|
|
|
|
2020-12-08 20:13:00 -05:00
|
|
|
args.callback = &sleep_timer_cb;
|
|
|
|
args.arg = NULL;
|
|
|
|
args.dispatch_method = ESP_TIMER_TASK;
|
|
|
|
args.name = "CircuitPython Sleep";
|
|
|
|
esp_timer_create(&args, &_sleep_timer);
|
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
circuitpython_task = xTaskGetCurrentTaskHandle();
|
|
|
|
|
2020-12-07 19:39:54 -05:00
|
|
|
// Send the ROM output out of the UART. This includes early logs.
|
2020-12-11 23:34:01 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
ets_install_uart_printf();
|
|
|
|
#endif
|
2020-12-07 19:39:54 -05:00
|
|
|
|
2020-09-08 20:06:09 -04:00
|
|
|
heap = NULL;
|
2020-05-19 20:46:29 -04:00
|
|
|
never_reset_module_internal_pins();
|
2020-08-21 19:20:58 -04:00
|
|
|
|
2021-01-09 14:59:54 -05:00
|
|
|
#if defined(DEBUG)
|
|
|
|
// debug UART
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO43);
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO44);
|
|
|
|
#endif
|
|
|
|
|
2021-01-07 17:23:21 -05:00
|
|
|
#if defined(DEBUG) || defined(ENABLE_JTAG)
|
|
|
|
// JTAG
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO39);
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO40);
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO41);
|
|
|
|
common_hal_never_reset_pin(&pin_GPIO42);
|
|
|
|
#endif
|
|
|
|
|
2020-08-21 19:20:58 -04:00
|
|
|
#ifdef CONFIG_SPIRAM
|
|
|
|
heap = (uint32_t*) (DRAM0_CACHE_ADDRESS_HIGH - CONFIG_SPIRAM_SIZE);
|
|
|
|
heap_size = CONFIG_SPIRAM_SIZE / sizeof(uint32_t);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (heap == NULL) {
|
|
|
|
heap = malloc(HEAP_SIZE);
|
|
|
|
heap_size = HEAP_SIZE / sizeof(uint32_t);
|
|
|
|
}
|
2020-09-08 20:06:09 -04:00
|
|
|
if (heap == NULL) {
|
|
|
|
return NO_HEAP;
|
|
|
|
}
|
|
|
|
|
2020-12-08 14:03:24 -05:00
|
|
|
esp_reset_reason_t reason = esp_reset_reason();
|
2020-12-22 19:13:02 -05:00
|
|
|
switch (reason) {
|
|
|
|
case ESP_RST_BROWNOUT:
|
|
|
|
return BROWNOUT;
|
|
|
|
case ESP_RST_PANIC:
|
2021-01-16 20:38:08 -05:00
|
|
|
return HARD_CRASH;
|
2020-12-22 19:13:02 -05:00
|
|
|
case ESP_RST_INT_WDT:
|
|
|
|
case ESP_RST_WDT:
|
|
|
|
default:
|
|
|
|
break;
|
2020-12-08 14:03:24 -05:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
return NO_SAFE_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_port(void) {
|
2020-05-19 20:46:29 -04:00
|
|
|
reset_all_pins();
|
2020-05-28 18:51:33 -04:00
|
|
|
|
2020-06-24 16:10:31 -04:00
|
|
|
// A larger delay so the idle task can run and do any IDF cleanup needed.
|
|
|
|
vTaskDelay(4);
|
|
|
|
|
2020-10-06 15:00:26 -04:00
|
|
|
#if CIRCUITPY_ANALOGIO
|
|
|
|
analogout_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-12-17 14:04:56 -05:00
|
|
|
#if CIRCUITPY_DUALBANK
|
|
|
|
dualbank_reset();
|
2020-12-10 06:18:58 -05:00
|
|
|
#endif
|
|
|
|
|
2020-11-19 01:14:22 -05:00
|
|
|
#if CIRCUITPY_PS2IO
|
|
|
|
ps2_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-10-30 11:05:16 -04:00
|
|
|
#if CIRCUITPY_AUDIOBUSIO
|
|
|
|
i2s_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-07-21 20:43:28 -04:00
|
|
|
#if CIRCUITPY_PULSEIO
|
2020-08-04 10:59:05 -04:00
|
|
|
esp32s2_peripherals_rmt_reset();
|
2020-08-10 12:14:16 -04:00
|
|
|
pulsein_reset();
|
2020-07-21 20:43:28 -04:00
|
|
|
#endif
|
2020-08-21 14:13:53 -04:00
|
|
|
|
2020-08-18 16:08:33 -04:00
|
|
|
#if CIRCUITPY_PWMIO
|
2020-07-21 20:43:28 -04:00
|
|
|
pwmout_reset();
|
|
|
|
#endif
|
2020-08-21 14:13:53 -04:00
|
|
|
|
2020-05-28 18:51:33 -04:00
|
|
|
#if CIRCUITPY_BUSIO
|
|
|
|
i2c_reset();
|
|
|
|
spi_reset();
|
|
|
|
uart_reset();
|
|
|
|
#endif
|
2020-09-22 02:07:12 -04:00
|
|
|
|
2020-11-12 06:00:30 -05:00
|
|
|
#if defined(CIRCUITPY_COUNTIO) || defined(CIRCUITPY_ROTARYIO)
|
|
|
|
peripherals_pcnt_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-11-18 01:46:14 -05:00
|
|
|
#if CIRCUITPY_FREQUENCYIO
|
|
|
|
peripherals_timer_reset();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_PULSEIO
|
|
|
|
esp32s2_peripherals_rmt_reset();
|
|
|
|
pulsein_reset();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_PWMIO
|
|
|
|
pwmout_reset();
|
2020-11-12 06:00:30 -05:00
|
|
|
#endif
|
|
|
|
|
2020-09-22 02:07:12 -04:00
|
|
|
#if CIRCUITPY_RTC
|
|
|
|
rtc_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-11-26 00:52:44 -05:00
|
|
|
#if CIRCUITPY_TOUCHIO_USE_NATIVE
|
2020-12-19 02:26:34 -05:00
|
|
|
peripherals_touch_reset();
|
2020-11-26 00:52:44 -05:00
|
|
|
#endif
|
|
|
|
|
2020-11-14 01:11:14 -05:00
|
|
|
#if CIRCUITPY_WATCHDOG
|
|
|
|
watchdog_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-07-30 20:58:12 -04:00
|
|
|
#if CIRCUITPY_WIFI
|
|
|
|
wifi_reset();
|
|
|
|
#endif
|
2021-01-13 19:05:07 -05:00
|
|
|
|
|
|
|
#if CIRCUITPY_SOCKETPOOL
|
|
|
|
socket_reset();
|
|
|
|
#endif
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_to_bootloader(void) {
|
2020-09-25 14:28:04 -04:00
|
|
|
esp_restart();
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_cpu(void) {
|
2020-12-07 19:39:54 -05:00
|
|
|
esp_backtrace_print(100);
|
2020-09-24 12:20:32 -04:00
|
|
|
esp_restart();
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_heap_get_bottom(void) {
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_heap_get_top(void) {
|
2020-08-21 19:20:58 -04:00
|
|
|
return heap + heap_size;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_stack_get_limit(void) {
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
return (uint32_t*) pxTaskGetStackStart(NULL);
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_stack_get_top(void) {
|
2020-11-10 11:41:10 -05:00
|
|
|
// The sizeof-arithmetic is so that the pointer arithmetic is done on units
|
|
|
|
// of uint32_t instead of units of StackType_t. StackType_t is an alias
|
|
|
|
// for a byte sized type.
|
|
|
|
//
|
|
|
|
// The main stack is bigger than CONFIG_ESP_MAIN_TASK_STACK_SIZE -- an
|
|
|
|
// "extra" size is added to it (TASK_EXTRA_STACK_SIZE). This total size is
|
|
|
|
// available as ESP_TASK_MAIN_STACK. Presumably TASK_EXTRA_STACK_SIZE is
|
|
|
|
// additional stack that can be used by the esp-idf runtime. But what's
|
|
|
|
// important for us is that some very outermost stack frames, such as
|
|
|
|
// pyexec_friendly_repl, could lie inside the "extra" area and be invisible
|
|
|
|
// to the garbage collector.
|
|
|
|
return port_stack_get_limit() + ESP_TASK_MAIN_STACK / (sizeof(uint32_t) / sizeof(StackType_t));
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
bool port_has_fixed_stack(void) {
|
|
|
|
return true;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place the word to save just after our BSS section that gets blanked.
|
|
|
|
void port_set_saved_word(uint32_t value) {
|
2020-12-07 19:39:54 -05:00
|
|
|
REG_WRITE(RTC_CNTL_STORE0_REG, value);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t port_get_saved_word(void) {
|
2020-12-07 19:39:54 -05:00
|
|
|
return REG_READ(RTC_CNTL_STORE0_REG);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t port_get_raw_ticks(uint8_t* subticks) {
|
2020-11-17 18:45:12 -05:00
|
|
|
// Convert microseconds to subticks of 1/32768 seconds
|
|
|
|
// 32768/1000000 = 64/15625 in lowest terms
|
|
|
|
// this arithmetic overflows after 570 years
|
|
|
|
int64_t all_subticks = esp_timer_get_time() * 512 / 15625;
|
2020-04-17 19:23:28 -04:00
|
|
|
if (subticks != NULL) {
|
|
|
|
*subticks = all_subticks % 32;
|
|
|
|
}
|
2020-11-17 18:45:12 -05:00
|
|
|
return all_subticks / 32;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable 1/1024 second tick.
|
|
|
|
void port_enable_tick(void) {
|
2020-06-26 19:56:17 -04:00
|
|
|
esp_timer_start_periodic(_tick_timer, 1000000 / 1024);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable 1/1024 second tick.
|
|
|
|
void port_disable_tick(void) {
|
|
|
|
esp_timer_stop(_tick_timer);
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:59:54 -05:00
|
|
|
void port_wake_main_task() {
|
2020-12-08 20:13:00 -05:00
|
|
|
xTaskNotifyGive(circuitpython_task);
|
|
|
|
}
|
2020-11-05 22:27:21 -05:00
|
|
|
|
2021-01-09 14:59:54 -05:00
|
|
|
void sleep_timer_cb(void* arg) {
|
|
|
|
port_wake_main_task();
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
void port_interrupt_after_ticks(uint32_t ticks) {
|
2020-12-08 20:13:00 -05:00
|
|
|
uint64_t timeout_us = ticks * 1000000ull / 1024;
|
|
|
|
if (esp_timer_start_once(_sleep_timer, timeout_us) != ESP_OK) {
|
|
|
|
esp_timer_stop(_sleep_timer);
|
|
|
|
esp_timer_start_once(_sleep_timer, timeout_us);
|
|
|
|
}
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:13:00 -05:00
|
|
|
// On the ESP we use FreeRTOS notifications instead of interrupts so this is a
|
|
|
|
// bit of a misnomer.
|
2020-12-02 21:05:29 -05:00
|
|
|
void port_idle_until_interrupt(void) {
|
2020-12-08 20:13:00 -05:00
|
|
|
xTaskNotifyWait(0x01, 0x01, NULL, portMAX_DELAY);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap main in app_main that the IDF expects.
|
|
|
|
extern void main(void);
|
|
|
|
void app_main(void) {
|
|
|
|
main();
|
|
|
|
}
|