summaryrefslogtreecommitdiff
path: root/ports/esp32s2/supervisor
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2020-04-17 16:23:28 -0700
committerScott Shawcroft <scott@tannewt.org>2020-05-15 15:36:16 -0700
commit6aaab005c5f3a781fec229640c74bf44a601fc97 (patch)
treed81d0c4d0387a15f8ea764ea31062d47527a4390 /ports/esp32s2/supervisor
parent0d8bca92e208e705150097d26db6a5390dc9eb9b (diff)
Initial ESP32S2 port.
Basic blinky works but doesn't check pins.
Diffstat (limited to 'ports/esp32s2/supervisor')
-rw-r--r--ports/esp32s2/supervisor/internal_flash.c122
-rw-r--r--ports/esp32s2/supervisor/internal_flash.h38
-rw-r--r--ports/esp32s2/supervisor/internal_flash_root_pointers.h31
-rw-r--r--ports/esp32s2/supervisor/port.c162
-rw-r--r--ports/esp32s2/supervisor/usb.c80
5 files changed, 433 insertions, 0 deletions
diff --git a/ports/esp32s2/supervisor/internal_flash.c b/ports/esp32s2/supervisor/internal_flash.c
new file mode 100644
index 000000000..aab7e587a
--- /dev/null
+++ b/ports/esp32s2/supervisor/internal_flash.c
@@ -0,0 +1,122 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ * 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 "supervisor/internal_flash.h"
+
+#include <stdint.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "extmod/vfs.h"
+#include "extmod/vfs_fat.h"
+#include "py/mphal.h"
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "lib/oofatfs/ff.h"
+
+#include "esp-idf/components/spi_flash/include/esp_partition.h"
+
+#include "esp_log.h"
+
+
+static const char* TAG = "CircuitPython Internal Flash";
+
+#include "supervisor/usb.h"
+
+STATIC const esp_partition_t * _partition;
+
+void supervisor_flash_init(void) {
+ _partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
+ ESP_PARTITION_SUBTYPE_DATA_FAT,
+ NULL);
+
+ ESP_EARLY_LOGW(TAG, "fatfs partition %p", _partition);
+}
+
+uint32_t supervisor_flash_get_block_size(void) {
+ return FILESYSTEM_BLOCK_SIZE;
+}
+
+uint32_t supervisor_flash_get_block_count(void) {
+ return _partition->size / FILESYSTEM_BLOCK_SIZE;
+}
+
+void port_internal_flash_flush(void) {
+
+}
+
+// TODO: Split the caching out of supervisor/shared/external_flash so we can use it.
+#define SECTOR_SIZE 4096
+STATIC uint8_t _cache[SECTOR_SIZE];
+STATIC uint32_t _cache_lba;
+
+mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
+ esp_err_t ok = esp_partition_read(_partition,
+ block * FILESYSTEM_BLOCK_SIZE,
+ dest,
+ num_blocks * FILESYSTEM_BLOCK_SIZE);
+ ESP_EARLY_LOGW(TAG, "read %d", ok);
+ return 0;
+}
+
+mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) {
+ uint32_t blocks_per_sector = SECTOR_SIZE / FILESYSTEM_BLOCK_SIZE;
+ uint32_t block = 0;
+ while (block < num_blocks) {
+ uint32_t block_address = lba + block;
+ uint32_t sector_offset = block_address / blocks_per_sector * SECTOR_SIZE;
+ uint8_t block_offset = block_address % blocks_per_sector;
+
+ esp_err_t result;
+ if (_cache_lba != block_address) {
+ result = esp_partition_read(_partition,
+ sector_offset,
+ _cache,
+ SECTOR_SIZE);
+ ESP_EARLY_LOGW(TAG, "flash read before write %d", result);
+ _cache_lba = sector_offset;
+ }
+ for (uint8_t b = block_offset; b < blocks_per_sector; b++) {
+ 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
+}
+
+void supervisor_flash_release_cache(void) {
+}
+
diff --git a/ports/esp32s2/supervisor/internal_flash.h b/ports/esp32s2/supervisor/internal_flash.h
new file mode 100644
index 000000000..e06ef2d16
--- /dev/null
+++ b/ports/esp32s2/supervisor/internal_flash.h
@@ -0,0 +1,38 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ * 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.
+ */
+#ifndef MICROPY_INCLUDED_ESP32S2_INTERNAL_FLASH_H
+#define MICROPY_INCLUDED_ESP32S2_INTERNAL_FLASH_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "py/mpconfig.h"
+
+#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
+#define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2)
+
+#endif // MICROPY_INCLUDED_ESP32S2_INTERNAL_FLASH_H
diff --git a/ports/esp32s2/supervisor/internal_flash_root_pointers.h b/ports/esp32s2/supervisor/internal_flash_root_pointers.h
new file mode 100644
index 000000000..ae3e45e14
--- /dev/null
+++ b/ports/esp32s2/supervisor/internal_flash_root_pointers.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries LLC
+ *
+ * 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_LITEX_INTERNAL_FLASH_ROOT_POINTERS_H
+#define MICROPY_INCLUDED_LITEX_INTERNAL_FLASH_ROOT_POINTERS_H
+
+#define FLASH_ROOT_POINTERS
+
+#endif // MICROPY_INCLUDED_LITEX_INTERNAL_FLASH_ROOT_POINTERS_H
diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c
new file mode 100644
index 000000000..4389bb6a8
--- /dev/null
+++ b/ports/esp32s2/supervisor/port.c
@@ -0,0 +1,162 @@
+/*
+ * 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>
+#include "supervisor/port.h"
+#include "boards/board.h"
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+
+#include "supervisor/memory.h"
+#include "supervisor/shared/tick.h"
+
+#include "esp_log.h"
+
+static const char* TAG = "CircuitPython";
+
+STATIC esp_timer_handle_t _tick_timer;
+
+void tick_timer_cb(void* arg) {
+ supervisor_tick();
+}
+
+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";
+ esp_err_t result = esp_timer_create(&args, &_tick_timer);
+ if (result != ESP_OK) {
+ ESP_EARLY_LOGE(TAG, "Unable to create tick timer.");
+ }
+ ESP_EARLY_LOGW(TAG, "port init done");
+ return NO_SAFE_MODE;
+}
+
+void reset_port(void) {
+
+}
+
+void reset_to_bootloader(void) {
+}
+
+void reset_cpu(void) {
+}
+
+uint32_t heap[64 / sizeof(uint32_t) * 1024];
+
+uint32_t *port_heap_get_bottom(void) {
+ return heap;
+}
+
+uint32_t *port_heap_get_top(void) {
+ return heap + sizeof(heap) / sizeof(heap[0]);
+}
+
+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) {
+ return port_stack_get_limit() + CONFIG_ESP_MAIN_TASK_STACK_SIZE / (sizeof(uint32_t) / sizeof(StackType_t));
+}
+
+supervisor_allocation _fixed_stack;
+
+supervisor_allocation* port_fixed_stack(void) {
+
+ ESP_EARLY_LOGW(TAG, "port fixed stack");
+ _fixed_stack.ptr = port_stack_get_limit();
+ ESP_EARLY_LOGW(TAG, "got limit %p", _fixed_stack.ptr);
+ _fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
+ ESP_EARLY_LOGW(TAG, "got length %d", _fixed_stack.length);
+ return &_fixed_stack;
+}
+
+// Place the word to save just after our BSS section that gets blanked.
+void port_set_saved_word(uint32_t value) {
+}
+
+uint32_t port_get_saved_word(void) {
+ return 0;
+}
+
+uint64_t port_get_raw_ticks(uint8_t* subticks) {
+ struct timeval tv_now;
+ gettimeofday(&tv_now, NULL);
+ uint64_t all_subticks = (uint64_t)tv_now.tv_usec / 32768;
+ if (subticks != NULL) {
+ *subticks = all_subticks % 32;
+ }
+ return (uint64_t)tv_now.tv_sec * 1024L + all_subticks / 32;
+}
+
+// Enable 1/1024 second tick.
+void port_enable_tick(void) {
+ esp_err_t result = esp_timer_start_periodic(_tick_timer, 1000000 / 1024);
+ if (result != ESP_OK) {
+ ESP_EARLY_LOGE(TAG, "Unable to start tick timer.");
+ }
+}
+
+// Disable 1/1024 second tick.
+void port_disable_tick(void) {
+ esp_timer_stop(_tick_timer);
+}
+
+TickType_t sleep_time_set;
+TickType_t sleep_time_duration;
+void port_interrupt_after_ticks(uint32_t ticks) {
+ // ESP_EARLY_LOGW(TAG, "after ticks");
+ sleep_time_set = xTaskGetTickCount();
+ sleep_time_duration = ticks / portTICK_PERIOD_MS;
+ // esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
+}
+
+void port_sleep_until_interrupt(void) {
+ // ESP_EARLY_LOGW(TAG, "sleep until");
+ // FreeRTOS delay here maybe.
+ // Light sleep shuts down BLE and wifi.
+ // esp_light_sleep_start()
+ vTaskDelayUntil(&sleep_time_set, sleep_time_duration);
+}
+
+
+// Wrap main in app_main that the IDF expects.
+extern void main(void);
+void app_main(void) {
+ ESP_EARLY_LOGW(TAG, "Hello from CircuitPython");
+ // ESP_LOGW(TAG, "Hello from CircuitPython");
+
+ main();
+}
diff --git a/ports/esp32s2/supervisor/usb.c b/ports/esp32s2/supervisor/usb.c
new file mode 100644
index 000000000..7a24a90cb
--- /dev/null
+++ b/ports/esp32s2/supervisor/usb.c
@@ -0,0 +1,80 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 hathach 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 "supervisor/usb.h"
+#include "lib/utils/interrupt_char.h"
+#include "lib/mp-readline/readline.h"
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+
+#include "hal/usb_hal.h"
+
+#include "tusb.h"
+
+#ifdef CFG_TUSB_DEBUG
+ #define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE)
+#else
+ #define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2)
+#endif
+
+StackType_t usb_device_stack[USBD_STACK_SIZE];
+StaticTask_t usb_device_taskdef;
+
+// USB Device Driver task
+// This top level thread process all usb events and invoke callbacks
+void usb_device_task(void* param)
+{
+ (void) param;
+
+ // RTOS forever loop
+ while (1)
+ {
+ // tinyusb device task
+ if (tusb_inited()) {
+ tud_task();
+ tud_cdc_write_flush();
+ }
+ vTaskDelay(10);
+ }
+}
+
+void init_usb_hardware(void) {
+ usb_hal_context_t hal = {
+ .use_external_phy = false // use built-in PHY
+ };
+ usb_hal_init(&hal);
+
+ (void) xTaskCreateStatic(usb_device_task,
+ "usbd",
+ USBD_STACK_SIZE,
+ NULL,
+ configMAX_PRIORITIES-1,
+ usb_device_stack,
+ &usb_device_taskdef);
+}
+