summaryrefslogtreecommitdiff
path: root/supervisor
diff options
context:
space:
mode:
authorJeff Epler <jeff@adafruit.com>2020-10-27 11:47:44 -0500
committerSeon Rozenblum <seon@unexpectedmaker.com>2021-01-04 09:52:42 +1100
commit2a5f3de1cdec92d412148cabaf605a750d238234 (patch)
tree9c187db62ecbec0000b4533c95f4089901f3b5ae /supervisor
parentcaca0609bdbc8f5b1b406fad19878505bb5492a5 (diff)
parentd0e54993e9546249911c2c380c870e6a7f2b9917 (diff)
Merge pull request #3608 from adafruit/6.0.x
Update main with latest 6.0.x
Diffstat (limited to 'supervisor')
-rw-r--r--supervisor/board.h53
-rwxr-xr-xsupervisor/memory.h33
-rw-r--r--supervisor/port.h14
-rw-r--r--supervisor/serial.h1
-rw-r--r--supervisor/shared/board.c19
-rw-r--r--supervisor/shared/board.h14
-rw-r--r--supervisor/shared/display.c57
-rw-r--r--supervisor/shared/external_flash/devices.h78
-rw-r--r--supervisor/shared/external_flash/external_flash.c2
-rwxr-xr-xsupervisor/shared/memory.c319
-rw-r--r--supervisor/shared/rgb_led_status.c8
-rw-r--r--supervisor/shared/rgb_led_status.h2
-rw-r--r--supervisor/shared/safe_mode.c8
-rw-r--r--supervisor/shared/serial.c4
-rwxr-xr-xsupervisor/shared/stack.c38
-rwxr-xr-xsupervisor/shared/stack.h6
-rw-r--r--supervisor/shared/tick.c12
-rw-r--r--supervisor/shared/usb/usb.c4
-rw-r--r--supervisor/shared/workflow.c47
-rw-r--r--supervisor/shared/workflow.h32
-rw-r--r--supervisor/supervisor.mk1
-rwxr-xr-xsupervisor/workflow.h30
22 files changed, 606 insertions, 176 deletions
diff --git a/supervisor/board.h b/supervisor/board.h
new file mode 100644
index 000000000..939ee1c19
--- /dev/null
+++ b/supervisor/board.h
@@ -0,0 +1,53 @@
+/*
+ * This file is part of the MicroPython 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_SUPERVISOR_BOARD_H
+#define MICROPY_INCLUDED_SUPERVISOR_BOARD_H
+
+#include <stdbool.h>
+
+#include "supervisor/shared/safe_mode.h"
+
+// Returns true if the user initiates safe mode in a board specific way.
+// Also add BOARD_USER_SAFE_MODE in mpconfigboard.h to explain the board specific
+// way.
+bool board_requests_safe_mode(void);
+
+// Initializes board related state once on start up.
+void board_init(void);
+
+// Reset the state of off MCU components such as neopixels.
+void reset_board(void);
+
+#if CIRCUITPY_ALARM
+// Deinit the board. This should put the board in deep sleep durable, low power
+// state. It should not prevent the user access method from working (such as
+// disabling USB, BLE or flash) because CircuitPython may continue to run.
+void board_deinit(void);
+#endif
+
+
+#endif // MICROPY_INCLUDED_SUPERVISOR_BOARD_H
diff --git a/supervisor/memory.h b/supervisor/memory.h
index f4359ca46..0f820eac1 100755
--- a/supervisor/memory.h
+++ b/supervisor/memory.h
@@ -33,32 +33,45 @@
#include <stdbool.h>
#include <stdint.h>
+#include <stddef.h>
typedef struct {
uint32_t* ptr;
- uint32_t length; // in bytes
} supervisor_allocation;
-void memory_init(void);
void free_memory(supervisor_allocation* allocation);
+
+// Find the allocation with the given ptr, NULL if not found. When called from the context of a
+// supervisor_move_memory() callback, finds the allocation that had that ptr *before* the move, but
+// the returned allocation already contains the ptr after the move.
+// When called with NULL, may return either NULL or an unused allocation whose ptr is NULL (this is
+// a feature used internally in allocate_memory to save code size). Passing the return value to
+// free_memory() is a permissible no-op in either case.
supervisor_allocation* allocation_from_ptr(void *ptr);
+
supervisor_allocation* allocate_remaining_memory(void);
// Allocate a piece of a given length in bytes. If high_address is true then it should be allocated
// at a lower address from the top of the stack. Otherwise, addresses will increase starting after
-// statically allocated memory.
-supervisor_allocation* allocate_memory(uint32_t length, bool high_address);
+// statically allocated memory. If movable is false, memory will be taken from outside the GC heap
+// and will stay stationary until freed. While the VM is running, this will fail unless a previous
+// allocation of exactly matching length has recently been freed. If movable is true, memory will be
+// taken from either outside or inside the GC heap, and when the VM exits, will be moved outside.
+// The ptr of the returned supervisor_allocation will change at that point. If you need to be
+// notified of that, add your own callback function at the designated place near the end of
+// supervisor_move_memory().
+supervisor_allocation* allocate_memory(uint32_t length, bool high_address, bool movable);
-static inline uint16_t align32_size(uint16_t size) {
- if (size % 4 != 0) {
- return (size & 0xfffc) + 0x4;
- }
- return size;
+static inline size_t align32_size(size_t size) {
+ return (size + 3) & ~3;
}
-// Called after the heap is freed in case the supervisor wants to save some values.
+size_t get_allocation_length(supervisor_allocation* allocation);
+
+// Called after the GC heap is freed, transfers movable allocations from the GC heap to the
+// supervisor heap and compacts the supervisor heap.
void supervisor_move_memory(void);
#endif // MICROPY_INCLUDED_SUPERVISOR_MEMORY_H
diff --git a/supervisor/port.h b/supervisor/port.h
index f5b3c15d1..862400986 100644
--- a/supervisor/port.h
+++ b/supervisor/port.h
@@ -49,9 +49,6 @@ void reset_cpu(void) NORETURN;
// Reset the microcontroller state.
void reset_port(void);
-// Reset the rest of the board.
-void reset_board(void);
-
// Reset to the bootloader
void reset_to_bootloader(void) NORETURN;
@@ -61,7 +58,8 @@ uint32_t *port_stack_get_limit(void);
// Get stack top address
uint32_t *port_stack_get_top(void);
-supervisor_allocation* port_fixed_stack(void);
+// True if stack is not located inside heap (at the top)
+bool port_has_fixed_stack(void);
// Get heap bottom address
uint32_t *port_heap_get_bottom(void);
@@ -69,8 +67,6 @@ uint32_t *port_heap_get_bottom(void);
// Get heap top address
uint32_t *port_heap_get_top(void);
-supervisor_allocation* port_fixed_heap(void);
-
// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
void port_set_saved_word(uint32_t);
uint32_t port_get_saved_word(void);
@@ -90,8 +86,9 @@ void port_disable_tick(void);
// Only the common sleep routine should use it.
void port_interrupt_after_ticks(uint32_t ticks);
-// Sleep the CPU until an interrupt is received.
-void port_sleep_until_interrupt(void);
+// Sleep the CPU until an interrupt is received. We call this idle because it
+// may not be a system level sleep.
+void port_idle_until_interrupt(void);
// Execute port specific actions during background tasks.
void port_background_task(void);
@@ -101,4 +98,5 @@ void port_background_task(void);
// work" should be done in port_background_task() instead.
void port_start_background_task(void);
void port_finish_background_task(void);
+
#endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H
diff --git a/supervisor/serial.h b/supervisor/serial.h
index 9c2d44737..066886303 100644
--- a/supervisor/serial.h
+++ b/supervisor/serial.h
@@ -47,5 +47,4 @@ char serial_read(void);
bool serial_bytes_available(void);
bool serial_connected(void);
-extern volatile bool _serial_connected;
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H
diff --git a/supervisor/shared/board.c b/supervisor/shared/board.c
index e3eb8fd0d..30603aa66 100644
--- a/supervisor/shared/board.c
+++ b/supervisor/shared/board.c
@@ -26,23 +26,22 @@
#include "supervisor/shared/board.h"
-#include "shared-bindings/digitalio/DigitalInOut.h"
-#include "shared-bindings/neopixel_write/__init__.h"
+#if CIRCUITPY_DIGITALIO && CIRCUITPY_NEOPIXEL_WRITE
-#ifdef USER_NEOPIXELS_PIN
+#include <string.h>
-// The maximum number of user neopixels right now is 10, on Circuit Playgrounds.
-// PyBadge and PyGamer have max 5
-#define USER_NEOPIXELS_MAX_COUNT 10
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/neopixel_write/__init__.h"
-void board_reset_user_neopixels(void) {
+void board_reset_user_neopixels(const mcu_pin_obj_t* pin, size_t count) {
// Turn off on-board NeoPixel string
- uint8_t empty[USER_NEOPIXELS_MAX_COUNT * 3] = { 0 };
+ uint8_t empty[count * 3];
+ memset(empty, 0, count);
digitalio_digitalinout_obj_t neopixel_pin;
- common_hal_digitalio_digitalinout_construct(&neopixel_pin, USER_NEOPIXELS_PIN);
+ common_hal_digitalio_digitalinout_construct(&neopixel_pin, pin);
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
DRIVE_MODE_PUSH_PULL);
- common_hal_neopixel_write(&neopixel_pin, empty, USER_NEOPIXELS_MAX_COUNT * 3);
+ common_hal_neopixel_write(&neopixel_pin, empty, count * 3);
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
}
diff --git a/supervisor/shared/board.h b/supervisor/shared/board.h
index 0e4d73455..fe887a933 100644
--- a/supervisor/shared/board.h
+++ b/supervisor/shared/board.h
@@ -24,15 +24,13 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_SUPERVISOR_BOARD_H
-#define MICROPY_INCLUDED_SUPERVISOR_BOARD_H
+#ifndef MICROPY_INCLUDED_SUPERVISOR_SHARED_BOARD_H
+#define MICROPY_INCLUDED_SUPERVISOR_SHARED_BOARD_H
-#include "py/mpconfig.h"
+#include <stddef.h>
-#ifdef USER_NEOPIXELS_PIN
+#include "shared-bindings/microcontroller/Pin.h"
-void board_reset_user_neopixels(void);
+void board_reset_user_neopixels(const mcu_pin_obj_t* pin, size_t count);
-#endif
-
-#endif // MICROPY_INCLUDED_SUPERVISOR_BOARD_H
+#endif // MICROPY_INCLUDED_SUPERVISOR_SHARED_BOARD_H
diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c
index afb3f3a9a..9c9c66cd7 100644
--- a/supervisor/shared/display.c
+++ b/supervisor/shared/display.c
@@ -60,38 +60,44 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) {
#if CIRCUITPY_TERMINALIO
displayio_tilegrid_t* grid = &supervisor_terminal_text_grid;
- uint16_t width_in_tiles = (width_px - blinka_bitmap.width) / grid->tile_width;
+ bool tall = height_px > width_px;
+ uint16_t terminal_width_px = tall ? width_px : width_px - blinka_bitmap.width;
+ uint16_t terminal_height_px = tall ? height_px - blinka_bitmap.height : height_px ;
+ uint16_t width_in_tiles = terminal_width_px / grid->tile_width;
// determine scale based on h
if (width_in_tiles < 80) {
scale = 1;
}
- width_in_tiles = (width_px - blinka_bitmap.width * scale) / (grid->tile_width * scale);
+ width_in_tiles = terminal_width_px / (grid->tile_width * scale);
if (width_in_tiles < 1) {
width_in_tiles = 1;
}
- uint16_t height_in_tiles = height_px / (grid->tile_height * scale);
- uint16_t remaining_pixels = height_px % (grid->tile_height * scale);
+ uint16_t height_in_tiles = terminal_height_px / (grid->tile_height * scale);
+ uint16_t remaining_pixels = tall ? 0 : terminal_height_px % (grid->tile_height * scale);
if (height_in_tiles < 1 || remaining_pixels > 0) {
height_in_tiles += 1;
}
uint16_t total_tiles = width_in_tiles * height_in_tiles;
- // First try to allocate outside the heap. This will fail when the VM is running.
- tilegrid_tiles = allocate_memory(align32_size(total_tiles), false);
- uint8_t* tiles;
- if (tilegrid_tiles == NULL) {
- tiles = m_malloc(total_tiles, true);
- MP_STATE_VM(terminal_tilegrid_tiles) = tiles;
- } else {
- tiles = (uint8_t*) tilegrid_tiles->ptr;
+ // Reuse the previous allocation if possible
+ if (tilegrid_tiles) {
+ if (get_allocation_length(tilegrid_tiles) != align32_size(total_tiles)) {
+ free_memory(tilegrid_tiles);
+ tilegrid_tiles = NULL;
+ }
}
-
- if (tiles == NULL) {
- return;
+ if (!tilegrid_tiles) {
+ tilegrid_tiles = allocate_memory(align32_size(total_tiles), false, true);
+ if (!tilegrid_tiles) {
+ return;
+ }
}
- grid->y = 0;
+ uint8_t* tiles = (uint8_t*) tilegrid_tiles->ptr;
+
+ grid->y = tall ? blinka_bitmap.height : 0;
+ grid->x = tall ? 0 : blinka_bitmap.width;
grid->top_left_y = 0;
if (remaining_pixels > 0) {
grid->y -= (grid->tile_height - remaining_pixels);
@@ -116,7 +122,6 @@ void supervisor_stop_terminal(void) {
if (tilegrid_tiles != NULL) {
free_memory(tilegrid_tiles);
tilegrid_tiles = NULL;
- supervisor_terminal_text_grid.inline_tiles = false;
supervisor_terminal_text_grid.tiles = NULL;
}
#endif
@@ -124,20 +129,10 @@ void supervisor_stop_terminal(void) {
void supervisor_display_move_memory(void) {
#if CIRCUITPY_TERMINALIO
- displayio_tilegrid_t* grid = &supervisor_terminal_text_grid;
- if (MP_STATE_VM(terminal_tilegrid_tiles) != NULL &&
- grid->tiles == MP_STATE_VM(terminal_tilegrid_tiles)) {
- uint16_t total_tiles = grid->width_in_tiles * grid->height_in_tiles;
-
- tilegrid_tiles = allocate_memory(align32_size(total_tiles), false);
- if (tilegrid_tiles != NULL) {
- memcpy(tilegrid_tiles->ptr, grid->tiles, total_tiles);
- grid->tiles = (uint8_t*) tilegrid_tiles->ptr;
- } else {
- grid->tiles = NULL;
- grid->inline_tiles = false;
- }
- MP_STATE_VM(terminal_tilegrid_tiles) = NULL;
+ if (tilegrid_tiles != NULL) {
+ supervisor_terminal_text_grid.tiles = (uint8_t*) tilegrid_tiles->ptr;
+ } else {
+ supervisor_terminal_text_grid.tiles = NULL;
}
#endif
diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h
index a874dbd4f..c8c941adf 100644
--- a/supervisor/shared/external_flash/devices.h
+++ b/supervisor/shared/external_flash/devices.h
@@ -94,6 +94,26 @@ typedef struct {
.single_status_byte = false, \
}
+// Settings for the Adesto Tech AT25DF641-SSHD-T 8MiB SPI flash
+// for the Oak Dev Tech Icy Tree M0 (SAMD21) feather board.
+// Source: https://www.digikey.com/product-detail/en/adesto-technologies/AT25SF641-SDHD-T/1265-1180-1-ND/
+// Datasheet: https://www.adestotech.com/wp-content/uploads/doc8693.pdf
+#define AT25DF641A {\
+ .total_size = (1 << 23), /* 8 MiB */ \
+ .start_up_time_us = 10000, \
+ .manufacturer_id = 0x1f, \
+ .memory_type = 0x48, \
+ .capacity = 0x00, \
+ .max_clock_speed_mhz = 85, \
+ .quad_enable_bit_mask = 0x00, \
+ .has_sector_protection = true, \
+ .supports_fast_read = true, \
+ .supports_qspi = false, \
+ .supports_qspi_writes = false, \
+ .write_status_register_split = false, \
+ .single_status_byte = false, \
+}
+
// Settings for the Adesto Tech AT25SF161-SSHD-T 2MiB SPI flash
// for the StringCar M0 (SAMD21) Express board.
// Source: https://www.digikey.com/product-detail/en/adesto-technologies/AT25SF161-SDHD-T/1265-1230-1-ND/
@@ -114,6 +134,7 @@ typedef struct {
.single_status_byte = false, \
}
+
// Settings for the Adesto Tech AT25SF041 1MiB SPI flash. It's on the SparkFun
// SAMD51 Thing Plus board
// Datasheet: https://www.adestotech.com/wp-content/uploads/DS-AT25SF041_044.pdf
@@ -363,6 +384,24 @@ typedef struct {
.write_status_register_split = false, \
}
+// Settings for the Winbond W25Q64FV 8MiB SPI flash.
+// Datasheet: https://www.winbond.com/resource-files/w25q64fv%20revs%2007182017.pdf
+#define W25Q64FV {\
+ .total_size = (1 << 23), /* 8 MiB */ \
+ .start_up_time_us = 5000, \
+ .manufacturer_id = 0xef, \
+ .memory_type = 0x40, \
+ .capacity = 0x17, \
+ .max_clock_speed_mhz = 104, \
+ .quad_enable_bit_mask = 0x02, \
+ .has_sector_protection = false, \
+ .supports_fast_read = true, \
+ .supports_qspi = true, \
+ .supports_qspi_writes = true, \
+ .write_status_register_split = false, \
+ .single_status_byte = false, \
+}
+
// Settings for the Winbond W25Q64JV-IM 8MiB SPI flash. Note that JV-IQ has a different .memory_type (0x40)
// Datasheet: http://www.winbond.com/resource-files/w25q64jv%20revj%2003272018%20plus.pdf
#define W25Q64JV_IM {\
@@ -537,7 +576,7 @@ typedef struct {
.start_up_time_us = 800, \
.manufacturer_id = 0xc2, \
.memory_type = 0x28, \
- .capacity = 0x18, \
+ .capacity = 0x15, \
.max_clock_speed_mhz = 33, /* 8 mhz for dual/quad */ \
.quad_enable_bit_mask = 0x80, \
.has_sector_protection = false, \
@@ -566,6 +605,43 @@ typedef struct {
.single_status_byte = true, \
}
+// Settings for the Macronix MX25L51245G 64MiB SPI flash.
+// Datasheet: https://www.macronix.com/Lists/Datasheet/Attachments/7437/MX25L51245G,%203V,%20512Mb,%20v1.6.pdf
+#define MX25L25645G {\
+ .total_size = (1 << 25), /* 32 MiB */ \
+ .start_up_time_us = 5000, \
+ .manufacturer_id = 0x9f, \
+ .memory_type = 0xab, \
+ .capacity = 0x90, \
+ .max_clock_speed_mhz = 133, \
+ .quad_enable_bit_mask = 0xaf, \
+ .has_sector_protection = false, \
+ .supports_fast_read = true, \
+ .supports_qspi = true, \
+ .supports_qspi_writes = true, \
+ .write_status_register_split = false, \
+ .single_status_byte = true, \
+}
+
+// Settings for the Macronix MX25L12833F 16MiB SPI flash
+// Datasheet: https://www.macronix.com/Lists/Datasheet/Attachments/7447/MX25L12833F,%203V,%20128Mb,%20v1.0.pdf
+
+#define MX25L12833F {\
+ .total_size = (1UL << 24), /* 16 MiB */ \
+ .start_up_time_us = 5000, \
+ .manufacturer_id = 0xc2, \
+ .memory_type = 0x20, \
+ .capacity = 0x18, \
+ .max_clock_speed_mhz = 133, \
+ .quad_enable_bit_mask = 0x40, \
+ .has_sector_protection = true, \
+ .supports_fast_read = true, \
+ .supports_qspi = true, \
+ .supports_qspi_writes = true, \
+ .write_status_register_split = false, \
+ .single_status_byte = true, \
+ }
+
// Settings for the Winbond W25Q128JV-PM 16MiB SPI flash. Note that JV-IM has a different .memory_type (0x70)
// Datasheet: https://www.winbond.com/resource-files/w25q128jv%20revf%2003272018%20plus.pdf
#define W25Q128JV_PM {\
diff --git a/supervisor/shared/external_flash/external_flash.c b/supervisor/shared/external_flash/external_flash.c
index 5bde7fd48..e2d767235 100644
--- a/supervisor/shared/external_flash/external_flash.c
+++ b/supervisor/shared/external_flash/external_flash.c
@@ -338,7 +338,7 @@ static bool allocate_ram_cache(void) {
uint32_t table_size = blocks_per_sector * pages_per_block * sizeof(uint32_t);
// Attempt to allocate outside the heap first.
- supervisor_cache = allocate_memory(table_size + SPI_FLASH_ERASE_SIZE, false);
+ supervisor_cache = allocate_memory(table_size + SPI_FLASH_ERASE_SIZE, false, false);
if (supervisor_cache != NULL) {
MP_STATE_VM(flash_ram_cache) = (uint8_t **) supervisor_cache->ptr;
uint8_t* page_start = (uint8_t *) supervisor_cache->ptr + table_size;
diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c
index 0f96ae273..480c322b0 100755
--- a/supervisor/shared/memory.c
+++ b/supervisor/shared/memory.c
@@ -27,78 +27,111 @@
#include "supervisor/memory.h"
#include "supervisor/port.h"
-#include <stddef.h>
+#include <string.h>
+#include "py/gc.h"
#include "supervisor/shared/display.h"
-#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12)
-
-// Using a zero length to mark an unused allocation makes the code a bit shorter (but makes it
-// impossible to support zero-length allocations).
-#define FREE 0
+enum {
+ CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT =
+ // stack + heap
+ 2
+#ifdef EXTERNAL_FLASH_DEVICES
+ + 1
+#endif
+#if CIRCUITPY_USB_MIDI
+ + 1
+#endif
+ ,
+ CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT =
+ 0
+#if CIRCUITPY_DISPLAYIO
+ #if CIRCUITPY_TERMINALIO
+ + 1
+ #endif
+ + CIRCUITPY_DISPLAY_LIMIT * (
+ // Maximum needs of one display: max(4 if RGBMATRIX, 1 if SHARPDISPLAY, 0)
+ #if CIRCUITPY_RGBMATRIX
+ 4
+ #elif CIRCUITPY_SHARPDISPLAY
+ 1
+ #else
+ 0
+ #endif
+ )
+#endif
+ ,
+ CIRCUITPY_SUPERVISOR_ALLOC_COUNT = CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT + CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT
+};
// The lowest two bits of a valid length are always zero, so we can use them to mark an allocation
-// as freed by the client but not yet reclaimed into the FREE middle.
+// as a hole (freed by the client but not yet reclaimed into the free middle) and as movable.
+#define FLAGS 3
#define HOLE 1
+#define MOVABLE 2
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
-// We use uint32_t* to ensure word (4 byte) alignment.
-uint32_t* low_address;
-uint32_t* high_address;
+supervisor_allocation* old_allocations;
-void memory_init(void) {
- low_address = port_heap_get_bottom();
- high_address = port_heap_get_top();
-}
+typedef struct _supervisor_allocation_node {
+ struct _supervisor_allocation_node* next;
+ size_t length;
+ // We use uint32_t to ensure word (4 byte) alignment.
+ uint32_t data[];
+} supervisor_allocation_node;
+
+supervisor_allocation_node* low_head;
+supervisor_allocation_node* high_head;
+
+// Intermediate (void*) is to suppress -Wcast-align warning. Alignment will always be correct
+// because this only reverses how (alloc)->ptr was obtained as &(node->data[0]).
+#define ALLOCATION_NODE(alloc) ((supervisor_allocation_node*)(void*)((char*)((alloc)->ptr) - sizeof(supervisor_allocation_node)))
void free_memory(supervisor_allocation* allocation) {
- if (allocation == NULL) {
+ if (allocation == NULL || allocation->ptr == NULL) {
return;
}
- int32_t index = 0;
- bool found = false;
- for (index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
- found = allocation == &allocations[index];
- if (found) {
- break;
- }
+ supervisor_allocation_node* node = ALLOCATION_NODE(allocation);
+ if (node == low_head) {
+ do {
+ low_head = low_head->next;
+ } while (low_head != NULL && (low_head->length & HOLE));
}
- if (!found) {
- // Bad!
- // TODO(tannewt): Add a way to escape into safe mode on error.
+ else if (node == high_head) {
+ do {
+ high_head = high_head->next;
+ } while (high_head != NULL && (high_head->length & HOLE));
}
- if (allocation->ptr == high_address) {
- high_address += allocation->length / 4;
- allocation->length = FREE;
- for (index++; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
- if (!(allocations[index].length & HOLE)) {
- break;
- }
- // Division automatically shifts out the HOLE bit.
- high_address += allocations[index].length / 4;
- allocations[index].length = FREE;
+ else {
+ // Check if it's in the list of embedded allocations.
+ supervisor_allocation_node** emb = &MP_STATE_VM(first_embedded_allocation);
+ while (*emb != NULL && *emb != node) {
+ emb = &((*emb)->next);
}
- } else if (allocation->ptr + allocation->length / 4 == low_address) {
- low_address = allocation->ptr;
- allocation->length = FREE;
- for (index--; index >= 0; index--) {
- if (!(allocations[index].length & HOLE)) {
- break;
- }
- low_address -= allocations[index].length / 4;
- allocations[index].length = FREE;
+ if (*emb != NULL) {
+ // Found, remove it from the list.
+ *emb = node->next;
+ m_free(node
+#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
+ , sizeof(supervisor_allocation_node) + (node->length & ~FLAGS)
+#endif
+ );
+ }
+ else {
+ // Else it must be within the low or high ranges and becomes a hole.
+ node->length = ((node->length & ~FLAGS) | HOLE);
}
- } else {
- // Freed memory isn't in the middle so skip updating bounds. The memory will be added to the
- // middle when the memory to the inside is freed. We still need its length, but setting
- // only the lowest bit is nondestructive.
- allocation->length |= HOLE;
}
+ allocation->ptr = NULL;
}
supervisor_allocation* allocation_from_ptr(void *ptr) {
+ // When called from the context of supervisor_move_memory() (old_allocations != NULL), search
+ // by old pointer to give clients a way of mapping from old to new pointer. But not if
+ // ptr == NULL, then the caller wants an allocation whose current ptr is NULL.
+ supervisor_allocation* list = (old_allocations && ptr) ? old_allocations : &allocations[0];
for (size_t index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
- if (allocations[index].ptr == ptr) {
+ if (list[index].ptr == ptr) {
return &allocations[index];
}
}
@@ -106,50 +139,182 @@ supervisor_allocation* allocation_from_ptr(void *ptr) {
}
supervisor_allocation* allocate_remaining_memory(void) {
- if (low_address == high_address) {
- return NULL;
+ return allocate_memory((uint32_t)-1, false, false);
+}
+
+static supervisor_allocation_node* find_hole(supervisor_allocation_node* node, size_t length) {
+ for (; node != NULL; node = node->next) {
+ if (node->length == (length | HOLE)) {
+ break;
+ }
}
- return allocate_memory((high_address - low_address) * 4, false);
+ return node;
}
-supervisor_allocation* allocate_memory(uint32_t length, bool high) {
+static supervisor_allocation_node* allocate_memory_node(uint32_t length, bool high, bool movable) {
+ if (CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT == 0) {
+ assert(!movable);
+ }
+ // supervisor_move_memory() currently does not support movable allocations on the high side, it
+ // must be extended first if this is ever needed.
+ assert(!(high && movable));
+ uint32_t* low_address = low_head ? low_head->data + low_head->length / 4 : port_heap_get_bottom();
+ uint32_t* high_address = high_head ? (uint32_t*)high_head : port_heap_get_top();
+ // Special case for allocate_remaining_memory(), avoids computing low/high_address twice.
+ if (length == (uint32_t)-1) {
+ length = (high_address - low_address) * 4 - sizeof(supervisor_allocation_node);
+ }
if (length == 0 || length % 4 != 0) {
return NULL;
}
- uint8_t index = 0;
- int8_t direction = 1;
- if (high) {
- index = CIRCUITPY_SUPERVISOR_ALLOC_COUNT - 1;
- direction = -1;
- }
- supervisor_allocation* alloc;
- for (; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index += direction) {
- alloc = &allocations[index];
- if (alloc->length == FREE && (high_address - low_address) * 4 >= (int32_t) length) {
- break;
+ // 1. Matching hole on the requested side?
+ supervisor_allocation_node* node = find_hole(high ? high_head : low_head, length);
+ if (!node) {
+ // 2. Enough free space in the middle?
+ if ((high_address - low_address) * 4 >= (int32_t)(sizeof(supervisor_allocation_node) + length)) {
+ if (high) {
+ high_address -= (sizeof(supervisor_allocation_node) + length) / 4;
+ node = (supervisor_allocation_node*)high_address;
+ node->next = high_head;
+ high_head = node;
+ }
+ else {
+ node = (supervisor_allocation_node*)low_address;
+ node->next = low_head;
+ low_head = node;
+ }
}
- // If a hole matches in length exactly, we can reuse it.
- if (alloc->length == (length | HOLE)) {
- alloc->length = length;
- return alloc;
+ else {
+ // 3. Matching hole on the other side?
+ node = find_hole(high ? low_head : high_head, length);
+ if (!node) {
+ // 4. GC allocation?
+ if (movable && gc_alloc_possible()) {
+ node = m_malloc_maybe(sizeof(supervisor_allocation_node) + length, true);
+ if (node) {
+ node->next = MP_STATE_VM(first_embedded_allocation);
+ MP_STATE_VM(first_embedded_allocation) = node;
+ }
+ }
+ if (!node) {
+ // 5. Give up.
+ return NULL;
+ }
+ }
}
}
- if (index >= CIRCUITPY_SUPERVISOR_ALLOC_COUNT) {
+ node->length = length;
+ if (movable) {
+ node->length |= MOVABLE;
+ }
+ return node;
+}
+
+supervisor_allocation* allocate_memory(uint32_t length, bool high, bool movable) {
+ supervisor_allocation_node* node = allocate_memory_node(length, high, movable);
+ if (!node) {
return NULL;
}
- if (high) {
- high_address -= length / 4;
- alloc->ptr = high_address;
- } else {
- alloc->ptr = low_address;
- low_address += length / 4;
+ // Find the first free allocation.
+ supervisor_allocation* alloc = allocation_from_ptr(NULL);
+ if (!alloc) {
+ // We should free node again to avoid leaking, but something is wrong anyway if clients try
+ // to make more allocations than available, so don't bother.
+ return NULL;
}
- alloc->length = length;
+ alloc->ptr = &(node->data[0]);
return alloc;
}
+size_t get_allocation_length(supervisor_allocation* allocation) {
+ return ALLOCATION_NODE(allocation)->length & ~FLAGS;
+}
+
void supervisor_move_memory(void) {
+ // This whole function is not needed when there are no movable allocations, let it be optimized
+ // out.
+ if (CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT == 0) {
+ return;
+ }
+ // This must be called exactly after freeing the heap, so that the embedded allocations, if any,
+ // are now in the free region.
+ assert(MP_STATE_VM(first_embedded_allocation) == NULL || (low_head < MP_STATE_VM(first_embedded_allocation) && MP_STATE_VM(first_embedded_allocation) < high_head));
+
+ // Save the old pointers for allocation_from_ptr().
+ supervisor_allocation old_allocations_array[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
+ memcpy(old_allocations_array, allocations, sizeof(allocations));
+
+ // Compact the low side. Traverse the list repeatedly, finding movable allocations preceded by a
+ // hole and swapping them, until no more are found. This is not the most runtime-efficient way,
+ // but probably the shortest and simplest code.
+ bool acted;
+ do {
+ acted = false;
+ supervisor_allocation_node** nodep = &low_head;
+ while (*nodep != NULL && (*nodep)->next != NULL) {
+ if (((*nodep)->length & MOVABLE) && ((*nodep)->next->length & HOLE)) {
+ supervisor_allocation_node* oldnode = *nodep;
+ supervisor_allocation_node* start = oldnode->next;
+ supervisor_allocation* alloc = allocation_from_ptr(&(oldnode->data[0]));
+ assert(alloc != NULL);
+ alloc->ptr = &(start->data[0]);
+ oldnode->next = start->next;
+ size_t holelength = start->length;
+ size_t size = sizeof(supervisor_allocation_node) + (oldnode->length & ~FLAGS);
+ memmove(start, oldnode, size);
+ supervisor_allocation_node* newhole = (supervisor_allocation_node*)(void*)((char*)start + size);
+ newhole->next = start;
+ newhole->length = holelength;
+ *nodep = newhole;
+ acted = true;
+ }
+ nodep = &((*nodep)->next);
+ }
+ } while (acted);
+ // Any holes bubbled to the top can be absorbed into the free middle.
+ while (low_head != NULL && (low_head->length & HOLE)) {
+ low_head = low_head->next;
+ };
+
+ // Don't bother compacting the high side, there are no movable allocations and no holes there in
+ // current usage.
+
+ // Promote the embedded allocations to top-level ones, compacting them at the beginning of the
+ // now free region (or possibly in matching holes).
+ // The linked list is unordered, but allocations must be processed in order to avoid risking
+ // overwriting each other. To that end, repeatedly find the lowest element of the list, remove
+ // it from the list, and process it. This ad-hoc selection sort results in substantially shorter
+ // code than using the qsort() function from the C library.
+ while (MP_STATE_VM(first_embedded_allocation)) {
+ // First element is first candidate.
+ supervisor_allocation_node** pminnode = &MP_STATE_VM(first_embedded_allocation);
+ // Iterate from second element (if any) on.
+ for (supervisor_allocation_node** pnode = &(MP_STATE_VM(first_embedded_allocation)->next); *pnode != NULL; pnode = &(*pnode)->next) {
+ if (*pnode < *pminnode) {
+ pminnode = pnode;
+ }
+ }
+ // Remove from list.
+ supervisor_allocation_node* node = *pminnode;
+ *pminnode = node->next;
+ // Process.
+ size_t length = (node->length & ~FLAGS);
+ supervisor_allocation* alloc = allocation_from_ptr(&(node->data[0]));
+ assert(alloc != NULL);
+ // This may overwrite the header of node if it happened to be there already, but not the
+ // data.
+ supervisor_allocation_node* new_node = allocate_memory_node(length, false, true);
+ // There must be enough free space.
+ assert(new_node != NULL);
+ memmove(&(new_node->data[0]), &(node->data[0]), length);
+ alloc->ptr = &(new_node->data[0]);
+ }
+
+ // Notify clients that their movable allocations may have moved.
+ old_allocations = &old_allocations_array[0];
#if CIRCUITPY_DISPLAYIO
supervisor_display_move_memory();
#endif
+ // Add calls to further clients here.
+ old_allocations = NULL;
}
diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c
index 283b9da12..006bb1b34 100644
--- a/supervisor/shared/rgb_led_status.c
+++ b/supervisor/shared/rgb_led_status.c
@@ -411,14 +411,15 @@ void prep_rgb_status_animation(const pyexec_result_t* result,
#endif
}
-void tick_rgb_status_animation(rgb_status_animation_t* status) {
+bool tick_rgb_status_animation(rgb_status_animation_t* status) {
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
uint32_t tick_diff = supervisor_ticks_ms32() - status->pattern_start;
if (status->ok) {
// All is good. Ramp ALL_DONE up and down.
if (tick_diff > ALL_GOOD_CYCLE_MS) {
status->pattern_start = supervisor_ticks_ms32();
- tick_diff = 0;
+ new_status_color(BLACK);
+ return true;
}
uint16_t brightness = tick_diff * 255 / (ALL_GOOD_CYCLE_MS / 2);
@@ -433,7 +434,7 @@ void tick_rgb_status_animation(rgb_status_animation_t* status) {
} else {
if (tick_diff > status->total_exception_cycle) {
status->pattern_start = supervisor_ticks_ms32();
- tick_diff = 0;
+ return true;
}
// First flash the file color.
if (tick_diff < EXCEPTION_TYPE_LENGTH_MS) {
@@ -482,4 +483,5 @@ void tick_rgb_status_animation(rgb_status_animation_t* status) {
}
}
#endif
+ return false; // Animation is not finished.
}
diff --git a/supervisor/shared/rgb_led_status.h b/supervisor/shared/rgb_led_status.h
index e4e1981a2..84c97796a 100644
--- a/supervisor/shared/rgb_led_status.h
+++ b/supervisor/shared/rgb_led_status.h
@@ -76,6 +76,6 @@ void prep_rgb_status_animation(const pyexec_result_t* result,
bool found_main,
safe_mode_t safe_mode,
rgb_status_animation_t* status);
-void tick_rgb_status_animation(rgb_status_animation_t* status);
+bool tick_rgb_status_animation(rgb_status_animation_t* status);
#endif // MICROPY_INCLUDED_SUPERVISOR_RGB_LED_STATUS_H
diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c
index d59e754ed..9032e4045 100644
--- a/supervisor/shared/safe_mode.c
+++ b/supervisor/shared/safe_mode.c
@@ -29,6 +29,8 @@
#include "mphalport.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/microcontroller/Processor.h"
+#include "shared-bindings/microcontroller/ResetReason.h"
#include "supervisor/serial.h"
#include "supervisor/shared/rgb_led_colors.h"
@@ -52,6 +54,12 @@ safe_mode_t wait_for_safe_mode_reset(void) {
current_safe_mode = safe_mode;
return safe_mode;
}
+
+ const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason();
+ if (reset_reason != RESET_REASON_POWER_ON &&
+ reset_reason != RESET_REASON_RESET_PIN) {
+ return NO_SAFE_MODE;
+ }
port_set_saved_word(SAFE_MODE_DATA_GUARD | (MANUAL_SAFE_MODE << 8));
// Wait for a while to allow for reset.
temp_status_color(SAFE_MODE);
diff --git a/supervisor/shared/serial.c b/supervisor/shared/serial.c
index 91e90671d..303f89e75 100644
--- a/supervisor/shared/serial.c
+++ b/supervisor/shared/serial.c
@@ -47,8 +47,6 @@ busio_uart_obj_t debug_uart;
byte buf_array[64];
#endif
-volatile bool _serial_connected;
-
void serial_early_init(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
debug_uart.base.type = &busio_uart_type;
@@ -71,7 +69,7 @@ bool serial_connected(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
return true;
#else
- return _serial_connected;
+ return tud_cdc_connected();
#endif
}
diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c
index e7aa956b0..afea20401 100755
--- a/supervisor/shared/stack.c
+++ b/supervisor/shared/stack.c
@@ -34,36 +34,42 @@
extern uint32_t _estack;
+// Requested size.
static uint32_t next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
static uint32_t current_stack_size = 0;
-supervisor_allocation* stack_alloc = NULL;
+// Actual location and size, may be larger than requested.
+static uint32_t* stack_limit = NULL;
+static size_t stack_length = 0;
#define EXCEPTION_STACK_SIZE 1024
void allocate_stack(void) {
- if (port_fixed_stack() != NULL) {
- stack_alloc = port_fixed_stack();
- current_stack_size = stack_alloc->length;
+ if (port_has_fixed_stack()) {
+ stack_limit = port_stack_get_limit();
+ stack_length = (port_stack_get_top() - stack_limit)*sizeof(uint32_t);
+ current_stack_size = stack_length;
} else {
mp_uint_t regs[10];
mp_uint_t sp = cpu_get_regs_and_sp(regs);
mp_uint_t c_size = (uint32_t) port_stack_get_top() - sp;
- stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true);
+ supervisor_allocation* stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true, false);
if (stack_alloc == NULL) {
- stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true);
+ stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true, false);
current_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
} else {
current_stack_size = next_stack_size;
}
+ stack_limit = stack_alloc->ptr;
+ stack_length = get_allocation_length(stack_alloc);
}
- *stack_alloc->ptr = STACK_CANARY_VALUE;
+ *stack_limit = STACK_CANARY_VALUE;
}
inline bool stack_ok(void) {
- return stack_alloc == NULL || *stack_alloc->ptr == STACK_CANARY_VALUE;
+ return stack_limit == NULL || *stack_limit == STACK_CANARY_VALUE;
}
inline void assert_heap_ok(void) {
@@ -77,18 +83,26 @@ void stack_init(void) {
}
void stack_resize(void) {
- if (stack_alloc == NULL) {
+ if (stack_limit == NULL) {
return;
}
if (next_stack_size == current_stack_size) {
- *stack_alloc->ptr = STACK_CANARY_VALUE;
+ *stack_limit = STACK_CANARY_VALUE;
return;
}
- free_memory(stack_alloc);
- stack_alloc = NULL;
+ free_memory(allocation_from_ptr(stack_limit));
+ stack_limit = NULL;
allocate_stack();
}
+uint32_t* stack_get_bottom(void) {
+ return stack_limit;
+}
+
+size_t stack_get_length(void) {
+ return stack_length;
+}
+
void set_next_stack_size(uint32_t size) {
next_stack_size = size;
}
diff --git a/supervisor/shared/stack.h b/supervisor/shared/stack.h
index 7096f0b3e..1c75de5f7 100755
--- a/supervisor/shared/stack.h
+++ b/supervisor/shared/stack.h
@@ -31,10 +31,12 @@
#include "supervisor/memory.h"
-extern supervisor_allocation* stack_alloc;
-
void stack_init(void);
void stack_resize(void);
+// Actual stack location and size, may be larger than requested.
+uint32_t* stack_get_bottom(void);
+size_t stack_get_length(void);
+// Next/current requested stack size.
void set_next_stack_size(uint32_t size);
uint32_t get_current_stack_size(void);
bool stack_ok(void);
diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c
index a2855a570..e51c48c73 100644
--- a/supervisor/shared/tick.c
+++ b/supervisor/shared/tick.c
@@ -26,6 +26,7 @@
#include "supervisor/shared/tick.h"
+#include "lib/utils/interrupt_char.h"
#include "py/mpstate.h"
#include "py/runtime.h"
#include "supervisor/linker.h"
@@ -145,10 +146,11 @@ void mp_hal_delay_ms(mp_uint_t delay) {
delay = delay * 1024 / 1000;
uint64_t end_tick = start_tick + delay;
int64_t remaining = delay;
- while (remaining > 0) {
+
+ // Loop until we've waited long enough or we've been CTRL-Ced by autoreload
+ // or the user.
+ while (remaining > 0 && !mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
- // Check to see if we've been CTRL-Ced by autoreload or the user.
- mp_handle_pending();
remaining = end_tick - port_get_raw_ticks(NULL);
// We break a bit early so we don't risk setting the alarm before the time when we call
// sleep.
@@ -156,8 +158,8 @@ void mp_hal_delay_ms(mp_uint_t delay) {
break;
}
port_interrupt_after_ticks(remaining);
- // Sleep until an interrupt happens.
- port_sleep_until_interrupt();
+ // Idle until an interrupt happens.
+ port_idle_until_interrupt();
remaining = end_tick - port_get_raw_ticks(NULL);
}
}
diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c
index 89fbf56f3..ff08ade18 100644
--- a/supervisor/shared/usb/usb.c
+++ b/supervisor/shared/usb/usb.c
@@ -31,6 +31,7 @@
#include "supervisor/port.h"
#include "supervisor/serial.h"
#include "supervisor/usb.h"
+#include "supervisor/shared/workflow.h"
#include "lib/utils/interrupt_char.h"
#include "lib/mp-readline/readline.h"
@@ -116,7 +117,6 @@ void tud_umount_cb(void) {
// remote_wakeup_en : if host allows us to perform remote wakeup
// USB Specs: Within 7ms, device must draw an average current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en) {
- _serial_connected = false;
}
// Invoked when usb bus is resumed
@@ -128,8 +128,6 @@ void tud_resume_cb(void) {
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
(void) itf; // interface ID, not used
- _serial_connected = dtr;
-
// DTR = false is counted as disconnected
if ( !dtr )
{
diff --git a/supervisor/shared/workflow.c b/supervisor/shared/workflow.c
new file mode 100644
index 000000000..4986c0957
--- /dev/null
+++ b/supervisor/shared/workflow.c
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the MicroPython 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 <stdbool.h>
+#include "py/mpconfig.h"
+#include "tusb.h"
+
+void supervisor_workflow_reset(void) {
+}
+
+// Return true as soon as USB communication with host has started,
+// even before enumeration is done.
+// Not that some chips don't notice when USB is unplugged after first being plugged in,
+// so this is not perfect, but tud_suspended() check helps.
+bool supervisor_workflow_connecting(void) {
+ return tud_connected() && !tud_suspended();
+}
+
+// Return true if host has completed connection to us (such as USB enumeration).
+bool supervisor_workflow_active(void) {
+ // Eventually there might be other non-USB workflows, such as BLE.
+ // tud_ready() checks for usb mounted and not suspended.
+ return tud_ready();
+}
diff --git a/supervisor/shared/workflow.h b/supervisor/shared/workflow.h
new file mode 100644
index 000000000..22a9fa468
--- /dev/null
+++ b/supervisor/shared/workflow.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython 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.
+ */
+
+#pragma once
+
+extern void supervisor_workflow_reset(void);
+
+extern bool supervisor_workflow_connecting(void);
+extern bool supervisor_workflow_active(void);
diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk
index e81c51a88..a59e99e3d 100644
--- a/supervisor/supervisor.mk
+++ b/supervisor/supervisor.mk
@@ -75,6 +75,7 @@ else
lib/tinyusb/src/class/cdc/cdc_device.c \
lib/tinyusb/src/tusb.c \
supervisor/shared/serial.c \
+ supervisor/shared/workflow.c \
supervisor/usb.c \
supervisor/shared/usb/usb_desc.c \
supervisor/shared/usb/usb.c \
diff --git a/supervisor/workflow.h b/supervisor/workflow.h
new file mode 100755
index 000000000..4008b83a1
--- /dev/null
+++ b/supervisor/workflow.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of the MicroPython 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.
+ */
+
+#pragma once
+
+// True when the user could be actively iterating on their code.
+bool workflow_active(void);