summaryrefslogtreecommitdiff
path: root/supervisor
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-12-05 22:45:53 -0500
committerDan Halbert <halbert@halwitz.org>2019-12-05 22:45:53 -0500
commit40434d691934cf7f5792f91ce0288beb568bab85 (patch)
treee2f7d0191685f0b519e98c3e1551626fbbf49585 /supervisor
parent1505da784f228d43df41a773cfe504e7e4da330a (diff)
parent15886b1505d854d76e353b9c5261568c7065d2bf (diff)
wip
Diffstat (limited to 'supervisor')
-rw-r--r--supervisor/port.h6
-rw-r--r--supervisor/shared/autoreload.c8
-rw-r--r--supervisor/shared/autoreload.h2
-rw-r--r--supervisor/shared/bluetooth.c340
-rw-r--r--supervisor/shared/bluetooth.h34
-rw-r--r--supervisor/shared/external_flash/devices.h17
-rw-r--r--supervisor/shared/filesystem.c15
-rwxr-xr-xsupervisor/shared/memory.c7
-rw-r--r--supervisor/shared/rgb_led_colors.h24
-rw-r--r--supervisor/shared/rgb_led_status.c67
-rw-r--r--supervisor/shared/safe_mode.c5
-rw-r--r--supervisor/shared/safe_mode.h3
-rwxr-xr-xsupervisor/shared/stack.c3
-rw-r--r--supervisor/shared/tick.c94
-rw-r--r--supervisor/shared/tick.h67
-rw-r--r--supervisor/shared/translate.c20
-rw-r--r--supervisor/supervisor.mk29
17 files changed, 696 insertions, 45 deletions
diff --git a/supervisor/port.h b/supervisor/port.h
index 1430c6a50..c8a011978 100644
--- a/supervisor/port.h
+++ b/supervisor/port.h
@@ -54,6 +54,12 @@ void reset_board(void);
// Reset to the bootloader
void reset_to_bootloader(void);
+// Get stack limit address
+uint32_t *port_stack_get_limit(void);
+
+// Get stack top address
+uint32_t *port_stack_get_top(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);
diff --git a/supervisor/shared/autoreload.c b/supervisor/shared/autoreload.c
index 14b21902c..a6212c1a9 100644
--- a/supervisor/shared/autoreload.c
+++ b/supervisor/shared/autoreload.c
@@ -76,3 +76,11 @@ void autoreload_stop() {
autoreload_delay_ms = 0;
reload_requested = false;
}
+
+void autoreload_now() {
+ if (!autoreload_enabled || autoreload_suspended || reload_requested) {
+ return;
+ }
+ mp_raise_reload_exception();
+ reload_requested = true;
+}
diff --git a/supervisor/shared/autoreload.h b/supervisor/shared/autoreload.h
index bcb100151..fbd482c19 100644
--- a/supervisor/shared/autoreload.h
+++ b/supervisor/shared/autoreload.h
@@ -43,4 +43,6 @@ bool autoreload_is_enabled(void);
void autoreload_suspend(void);
void autoreload_resume(void);
+void autoreload_now(void);
+
#endif // MICROPY_INCLUDED_SUPERVISOR_AUTORELOAD_H
diff --git a/supervisor/shared/bluetooth.c b/supervisor/shared/bluetooth.c
new file mode 100644
index 000000000..02258de74
--- /dev/null
+++ b/supervisor/shared/bluetooth.c
@@ -0,0 +1,340 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 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 <string.h>
+
+#include "extmod/vfs.h"
+#include "extmod/vfs_fat.h"
+
+#include "shared-bindings/_bleio/__init__.h"
+#include "shared-bindings/_bleio/Adapter.h"
+#include "shared-bindings/_bleio/Characteristic.h"
+#include "shared-bindings/_bleio/Service.h"
+#include "shared-bindings/_bleio/UUID.h"
+
+#include "common-hal/_bleio/__init__.h"
+
+#include "supervisor/shared/autoreload.h"
+
+#include "py/mpstate.h"
+
+bleio_service_obj_t supervisor_ble_service;
+bleio_uuid_obj_t supervisor_ble_service_uuid;
+bleio_characteristic_obj_t supervisor_ble_version_characteristic;
+bleio_uuid_obj_t supervisor_ble_version_uuid;
+bleio_characteristic_obj_t supervisor_ble_filename_characteristic;
+bleio_uuid_obj_t supervisor_ble_filename_uuid;
+bleio_characteristic_obj_t supervisor_ble_length_characteristic;
+bleio_uuid_obj_t supervisor_ble_length_uuid;
+bleio_characteristic_obj_t supervisor_ble_contents_characteristic;
+bleio_uuid_obj_t supervisor_ble_contents_uuid;
+const uint8_t circuitpython_base_uuid[16] = {0x6e, 0x68, 0x74, 0x79, 0x50, 0x74, 0x69, 0x75, 0x63, 0x72, 0x69, 0x43, 0x00, 0x00, 0xaf, 0xad };
+uint8_t circuitpython_advertising_data[] = { 0x02, 0x01, 0x06, 0x02, 0x0a, 0x00, 0x11, 0x07, 0x6e, 0x68, 0x74, 0x79, 0x50, 0x74, 0x69, 0x75, 0x63, 0x72, 0x69, 0x43, 0x00, 0x01, 0xaf, 0xad, 0x06, 0x08, 0x43, 0x49, 0x52, 0x43, 0x55 };
+uint8_t circuitpython_scan_response_data[15] = {0x0e, 0x09, 0x43, 0x49, 0x52, 0x43, 0x55, 0x49, 0x54, 0x50, 0x59, 0x00, 0x00, 0x00, 0x00};
+mp_obj_list_t service_list;
+mp_obj_t service_list_items[1];
+mp_obj_list_t characteristic_list;
+mp_obj_t characteristic_list_items[4];
+
+void supervisor_bluetooth_start_advertising(void) {
+ bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
+ if (is_connected) {
+ return;
+ }
+ // TODO: switch to Adafruit short UUID for the advertisement and add manufacturing data to distinguish ourselves from arduino.
+ _common_hal_bleio_adapter_start_advertising(&common_hal_bleio_adapter_obj,
+ true,
+ 1.0,
+ circuitpython_advertising_data,
+ sizeof(circuitpython_advertising_data),
+ circuitpython_scan_response_data,
+ sizeof(circuitpython_scan_response_data));
+}
+
+void supervisor_start_bluetooth(void) {
+ common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, true);
+
+ supervisor_ble_service_uuid.base.type = &bleio_uuid_type;
+ common_hal_bleio_uuid_construct(&supervisor_ble_service_uuid, 0x0100, circuitpython_base_uuid);
+
+ // We know we'll only be 1 characteristic so we can statically allocate it.
+ characteristic_list.base.type = &mp_type_list;
+ characteristic_list.alloc = sizeof(characteristic_list_items) / sizeof(characteristic_list_items[0]);
+ characteristic_list.len = 0;
+ characteristic_list.items = characteristic_list_items;
+ mp_seq_clear(characteristic_list.items, 0, characteristic_list.alloc, sizeof(*characteristic_list.items));
+
+ _common_hal_bleio_service_construct(&supervisor_ble_service, &supervisor_ble_service_uuid, false /* is secondary */, &characteristic_list);
+
+ // File length
+ supervisor_ble_version_uuid.base.type = &bleio_uuid_type;
+ common_hal_bleio_uuid_construct(&supervisor_ble_version_uuid, 0x0203, circuitpython_base_uuid);
+ common_hal_bleio_characteristic_construct(&supervisor_ble_version_characteristic,
+ &supervisor_ble_service,
+ 0, // handle (for remote only)
+ &supervisor_ble_version_uuid,
+ CHAR_PROP_READ,
+ SECURITY_MODE_OPEN,
+ SECURITY_MODE_NO_ACCESS,
+ 4, // max length
+ true, // fixed length
+ NULL); // no initial value
+
+ uint32_t version = 1;
+ mp_buffer_info_t bufinfo;
+ bufinfo.buf = &version;
+ bufinfo.len = sizeof(version);
+ common_hal_bleio_characteristic_set_value(&supervisor_ble_version_characteristic, &bufinfo);
+
+ // Active filename.
+ supervisor_ble_filename_uuid.base.type = &bleio_uuid_type;
+ common_hal_bleio_uuid_construct(&supervisor_ble_filename_uuid, 0x0200, circuitpython_base_uuid);
+ common_hal_bleio_characteristic_construct(&supervisor_ble_filename_characteristic,
+ &supervisor_ble_service,
+ 0, // handle (for remote only)
+ &supervisor_ble_filename_uuid,
+ CHAR_PROP_READ | CHAR_PROP_WRITE,
+ SECURITY_MODE_OPEN,
+ SECURITY_MODE_OPEN,
+ 500, // max length
+ false, // fixed length
+ NULL); // no initial value
+
+ char code_py[] = "/code.py";
+ bufinfo.buf = code_py;
+ bufinfo.len = sizeof(code_py);
+ common_hal_bleio_characteristic_set_value(&supervisor_ble_filename_characteristic, &bufinfo);
+
+ // File length
+ supervisor_ble_length_uuid.base.type = &bleio_uuid_type;
+ common_hal_bleio_uuid_construct(&supervisor_ble_length_uuid, 0x0202, circuitpython_base_uuid);
+ common_hal_bleio_characteristic_construct(&supervisor_ble_length_characteristic,
+ &supervisor_ble_service,
+ 0, // handle (for remote only)
+ &supervisor_ble_length_uuid,
+ CHAR_PROP_NOTIFY | CHAR_PROP_READ,
+ SECURITY_MODE_OPEN,
+ SECURITY_MODE_NO_ACCESS,
+ 4, // max length
+ true, // fixed length
+ NULL); // no initial value
+
+ // File actions
+ supervisor_ble_contents_uuid.base.type = &bleio_uuid_type;
+ common_hal_bleio_uuid_construct(&supervisor_ble_contents_uuid, 0x0201, circuitpython_base_uuid);
+ common_hal_bleio_characteristic_construct(&supervisor_ble_contents_characteristic,
+ &supervisor_ble_service,
+ 0, // handle (for remote only)
+ &supervisor_ble_contents_uuid,
+ CHAR_PROP_NOTIFY | CHAR_PROP_WRITE_NO_RESPONSE | CHAR_PROP_WRITE,
+ SECURITY_MODE_OPEN,
+ SECURITY_MODE_OPEN,
+ 500, // max length
+ false, // fixed length
+ NULL); // no initial value
+
+ supervisor_bluetooth_start_advertising();
+ vm_used_ble = false;
+}
+
+FIL active_file;
+volatile bool new_filename;
+volatile bool run_ble_background;
+bool was_connected;
+
+void update_file_length(void) {
+ int32_t file_length = -1;
+ mp_buffer_info_t bufinfo;
+ bufinfo.buf = &file_length;
+ bufinfo.len = sizeof(file_length);
+ if (active_file.obj.fs != 0) {
+ file_length = (int32_t) f_size(&active_file);
+ }
+ common_hal_bleio_characteristic_set_value(&supervisor_ble_length_characteristic, &bufinfo);
+}
+
+void open_current_file(void) {
+ if (active_file.obj.fs != 0) {
+ return;
+ }
+ uint16_t max_len = supervisor_ble_filename_characteristic.max_length;
+ uint8_t path[max_len];
+ size_t length = common_hal_bleio_characteristic_get_value(&supervisor_ble_filename_characteristic, path, max_len - 1);
+ path[length] = '\0';
+
+ FATFS *fs = &((fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
+ f_open(fs, &active_file, (char*) path, FA_READ | FA_WRITE);
+
+ update_file_length();
+}
+
+void close_current_file(void) {
+ f_close(&active_file);
+}
+
+uint32_t current_command[1024 / sizeof(uint32_t)];
+volatile size_t current_offset;
+
+void supervisor_bluetooth_background(void) {
+ if (!run_ble_background) {
+ return;
+ }
+ bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
+ if (!was_connected && is_connected) {
+ open_current_file();
+ } else if (was_connected && !is_connected) {
+ close_current_file();
+ new_filename = false;
+ }
+ was_connected = is_connected;
+ run_ble_background = false;
+ if (!is_connected) {
+ supervisor_bluetooth_start_advertising();
+ return;
+ }
+ if (new_filename) {
+ close_current_file();
+ open_current_file();
+
+ new_filename = false;
+ // get length and set the characteristic for it
+ }
+ uint16_t current_length = ((uint16_t*) current_command)[0];
+ if (current_length > 0 && current_length == current_offset) {
+ uint16_t command = ((uint16_t *) current_command)[1];
+
+ if (command == 1) {
+ uint16_t max_len = 20; //supervisor_ble_contents_characteristic.max_length;
+ uint8_t buf[max_len];
+ mp_buffer_info_t bufinfo;
+ bufinfo.buf = buf;
+ f_lseek(&active_file, 0);
+ while (f_read(&active_file, buf, max_len, &bufinfo.len) == FR_OK) {
+ if (bufinfo.len == 0) {
+ break;
+ }
+ common_hal_bleio_characteristic_set_value(&supervisor_ble_contents_characteristic, &bufinfo);
+ }
+ } else if (command == 2) { // patch
+ uint32_t offset = current_command[1];
+ uint32_t remove_length = current_command[2];
+ uint32_t insert_length = current_command[3];
+ uint32_t file_length = (int32_t) f_size(&active_file);
+ //uint32_t data_shift_length = fileLength - offset - remove_length;
+ int32_t data_shift = insert_length - remove_length;
+ uint32_t new_length = file_length + data_shift;
+
+ // TODO: Make these loops smarter to read and write on sector boundaries.
+ if (data_shift < 0) {
+ for (uint32_t shift_offset = offset + insert_length; shift_offset < new_length; shift_offset++) {
+ uint8_t data;
+ UINT actual;
+ f_lseek(&active_file, shift_offset - data_shift);
+ f_read(&active_file, &data, 1, &actual);
+ f_lseek(&active_file, shift_offset);
+ f_write(&active_file, &data, 1, &actual);
+ }
+ f_truncate(&active_file);
+ } else if (data_shift > 0) {
+ f_lseek(&active_file, file_length);
+ // Fill end with 0xff so we don't need to erase.
+ uint8_t data = 0xff;
+ for (size_t i = 0; i < (size_t) data_shift; i++) {
+ UINT actual;
+ f_write(&active_file, &data, 1, &actual);
+ }
+ for (uint32_t shift_offset = new_length - 1; shift_offset >= offset + insert_length ; shift_offset--) {
+ UINT actual;
+ f_lseek(&active_file, shift_offset - data_shift);
+ f_read(&active_file, &data, 1, &actual);
+ f_lseek(&active_file, shift_offset);
+ f_write(&active_file, &data, 1, &actual);
+ }
+ }
+
+ f_lseek(&active_file, offset);
+ uint8_t* data = (uint8_t *) (current_command + 4);
+ UINT written;
+ f_write(&active_file, data, insert_length, &written);
+ f_sync(&active_file);
+ // Notify the new file length.
+ update_file_length();
+
+ // Trigger an autoreload
+ autoreload_now();
+ }
+ current_offset = 0;
+ }
+}
+
+// This happens in an interrupt so we need to be quick.
+bool supervisor_bluetooth_hook(ble_evt_t *ble_evt) {
+ // Catch writes to filename or contents. Length is read-only.
+
+ bool done = false;
+ switch (ble_evt->header.evt_id) {
+ case BLE_GAP_EVT_CONNECTED:
+ // We run our background task even if it wasn't us connected to because we may want to
+ // advertise if the user code stopped advertising.
+ run_ble_background = true;
+ break;
+ case BLE_GAP_EVT_DISCONNECTED:
+ run_ble_background = true;
+ break;
+ case BLE_GATTS_EVT_WRITE: {
+ // A client wrote to a characteristic.
+
+ ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write;
+ // Event handle must match the handle for my characteristic.
+ if (evt_write->handle == supervisor_ble_contents_characteristic.handle) {
+ // Handle events
+ //write_to_ringbuf(self, evt_write->data, evt_write->len);
+ // First packet includes a uint16_t le for length at the start.
+ uint16_t current_length = ((uint16_t*) current_command)[0];
+ memcpy(((uint8_t*) current_command) + current_offset, evt_write->data, evt_write->len);
+ current_offset += evt_write->len;
+ current_length = ((uint16_t*) current_command)[0];
+ if (current_offset == current_length) {
+ run_ble_background = true;
+ done = true;
+ }
+ } else if (evt_write->handle == supervisor_ble_filename_characteristic.handle) {
+ new_filename = true;
+ run_ble_background = true;
+ done = true;
+ } else {
+ return done;
+ }
+ break;
+ }
+
+ default:
+ // For debugging.
+ // mp_printf(&mp_plat_print, "Unhandled peripheral event: 0x%04x\n", ble_evt->header.evt_id);
+ break;
+ }
+ return done;
+}
diff --git a/supervisor/shared/bluetooth.h b/supervisor/shared/bluetooth.h
new file mode 100644
index 000000000..7ebcb953f
--- /dev/null
+++ b/supervisor/shared/bluetooth.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 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_SHARED_BLUETOOTH_H
+#define MICROPY_INCLUDED_SUPERVISOR_SHARED_BLUETOOTH_H
+
+void supervisor_start_bluetooth(void);
+bool supervisor_bluetooth_hook(ble_evt_t *ble_evt);
+void supervisor_bluetooth_background(void);
+
+#endif \ No newline at end of file
diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h
index 05091ad8c..08d0a78fd 100644
--- a/supervisor/shared/external_flash/devices.h
+++ b/supervisor/shared/external_flash/devices.h
@@ -300,6 +300,23 @@ typedef struct {
.write_status_register_split = false, \
}
+// Settings for the Winbond W25Q32JV-IQ 4MiB SPI flash.
+// Datasheet: https://www.mouser.com/datasheet/2/949/w25q32jv_revg_03272018_plus-1489806.pdf
+#define W25Q32JV_IQ {\
+ .total_size = (1 << 22), /* 4 MiB */ \
+ .start_up_time_us = 5000, \
+ .manufacturer_id = 0xef, \
+ .memory_type = 0x40, \
+ .capacity = 0x16, \
+ .max_clock_speed_mhz = 133, \
+ .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, \
+}
+
// 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 {\
diff --git a/supervisor/shared/filesystem.c b/supervisor/shared/filesystem.c
index c50d7f4ee..92727f6fb 100644
--- a/supervisor/shared/filesystem.c
+++ b/supervisor/shared/filesystem.c
@@ -69,6 +69,19 @@ static void make_empty_file(FATFS *fatfs, const char *path) {
f_close(&fp);
}
+
+static void make_sample_code_file(FATFS *fatfs) {
+ #if CIRCUITPY_FULL_BUILD
+ FIL fs;
+ UINT char_written = 0;
+ const byte buffer[] = "print('Hello World!')\n";
+ //Create or modify existing code.py file
+ f_open(fatfs, &fs, "/code.py", FA_WRITE | FA_CREATE_ALWAYS);
+ f_write(&fs, buffer, sizeof(buffer) - 1, &char_written);
+ f_close(&fs);
+ #endif
+}
+
// we don't make this function static because it needs a lot of stack and we
// want it to be executed without using stack within main() function
void filesystem_init(bool create_allowed, bool force_create) {
@@ -98,6 +111,8 @@ void filesystem_init(bool create_allowed, bool force_create) {
make_empty_file(&vfs_fat->fatfs, "/.metadata_never_index");
make_empty_file(&vfs_fat->fatfs, "/.Trashes");
make_empty_file(&vfs_fat->fatfs, "/.fseventsd/no_log");
+ // make a sample code.py file
+ make_sample_code_file(&vfs_fat->fatfs);
// create empty lib directory
f_mkdir(&vfs_fat->fatfs, "/lib");
diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c
index 11133415d..38040d11d 100755
--- a/supervisor/shared/memory.c
+++ b/supervisor/shared/memory.c
@@ -25,6 +25,7 @@
*/
#include "supervisor/memory.h"
+#include "supervisor/port.h"
#include <stddef.h>
@@ -36,12 +37,10 @@ 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;
-extern uint32_t _ebss;
-extern uint32_t _estack;
void memory_init(void) {
- low_address = &_ebss;
- high_address = &_estack;
+ low_address = port_stack_get_limit();
+ high_address = port_stack_get_top();
}
void free_memory(supervisor_allocation* allocation) {
diff --git a/supervisor/shared/rgb_led_colors.h b/supervisor/shared/rgb_led_colors.h
index 48994ad62..c723fbab3 100644
--- a/supervisor/shared/rgb_led_colors.h
+++ b/supervisor/shared/rgb_led_colors.h
@@ -1,12 +1,18 @@
-#define BLACK 0x000000
-#define GREEN 0x003000
-#define BLUE 0x000030
-#define CYAN 0x003030
-#define RED 0x300000
-#define ORANGE 0x302000
-#define YELLOW 0x303000
-#define PURPLE 0x300030
-#define WHITE 0x303030
+
+#define COLOR(r, g, b) (((r) << 16) | ((g) << 8) | (b))
+// For brightness == 255 (full). This will be adjusted downward for various different RGB indicators,
+// which vary in brightness.
+#define INTENSITY (0x30)
+
+#define BLACK COLOR(0, 0, 0)
+#define GREEN COLOR(0, INTENSITY, 0)
+#define BLUE COLOR(0, 0, INTENSITY)
+#define CYAN COLOR(0, INTENSITY, INTENSITY)
+#define RED COLOR(INTENSITY, 0, 0)
+#define ORANGE COLOR(INTENSITY, INTENSITY*2/3, 0)
+#define YELLOW COLOR(INTENSITY, INTENSITY, 0)
+#define PURPLE COLOR(INTENSITY, 0, INTENSITY)
+#define WHITE COLOR(INTENSITY, INTENSITY, INTENSITY)
#define BOOT_RUNNING BLUE
#define MAIN_RUNNING GREEN
diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c
index 4d9371516..f751a7ffd 100644
--- a/supervisor/shared/rgb_led_status.c
+++ b/supervisor/shared/rgb_led_status.c
@@ -25,8 +25,9 @@
*/
#include "mphalport.h"
-#include "common-hal/microcontroller/Pin.h"
+#include "shared-bindings/microcontroller/Pin.h"
#include "rgb_led_status.h"
+#include "supervisor/shared/tick.h"
#ifdef MICROPY_HW_NEOPIXEL
uint8_t rgb_status_brightness = 63;
@@ -38,7 +39,7 @@ static digitalio_digitalinout_obj_t status_neopixel;
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
-uint8_t rgb_status_brightness = 255;
+uint8_t rgb_status_brightness = 50;
#define APA102_BUFFER_LENGTH 12
static uint8_t status_apa102_color[APA102_BUFFER_LENGTH] = {0, 0, 0, 0, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff};
@@ -46,10 +47,18 @@ static uint8_t status_apa102_color[APA102_BUFFER_LENGTH] = {0, 0, 0, 0, 0xff, 0,
#if CIRCUITPY_BITBANG_APA102
#include "shared-bindings/bitbangio/SPI.h"
#include "shared-module/bitbangio/types.h"
-static bitbangio_spi_obj_t status_apa102;
+static bitbangio_spi_obj_t status_apa102 = {
+ .base = {
+ .type = &bitbangio_spi_type,
+ },
+};
#else
#include "shared-bindings/busio/SPI.h"
-busio_spi_obj_t status_apa102;
+busio_spi_obj_t status_apa102 = {
+ .base = {
+ .type = &busio_spi_type,
+ },
+};
#endif
#endif
@@ -59,9 +68,21 @@ busio_spi_obj_t status_apa102;
#include "shared-bindings/pulseio/PWMOut.h"
#include "shared-bindings/microcontroller/Pin.h"
-pulseio_pwmout_obj_t rgb_status_r;
-pulseio_pwmout_obj_t rgb_status_g;
-pulseio_pwmout_obj_t rgb_status_b;
+pulseio_pwmout_obj_t rgb_status_r = {
+ .base = {
+ .type = &pulseio_pwmout_type,
+ },
+};
+pulseio_pwmout_obj_t rgb_status_g = {
+ .base = {
+ .type = &pulseio_pwmout_type,
+ },
+};
+pulseio_pwmout_obj_t rgb_status_b = {
+ .base = {
+ .type = &pulseio_pwmout_type,
+ },
+};
uint8_t rgb_status_brightness = 0xFF;
@@ -74,8 +95,14 @@ uint16_t status_rgb_color[3] = {
static uint32_t current_status_color = 0;
#endif
-
+static bool rgb_led_status_init_in_progress = false;
void rgb_led_status_init() {
+ if (rgb_led_status_init_in_progress) {
+ // Avoid recursion.
+ return;
+ }
+ rgb_led_status_init_in_progress = true;
+
#ifdef MICROPY_HW_NEOPIXEL
common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL);
// Pretend we aren't using the pins. digitalio.DigitalInOut
@@ -91,15 +118,15 @@ void rgb_led_status_init() {
mp_const_none);
#else
if (!common_hal_busio_spi_deinited(&status_apa102)) {
- // Don't use spi_deinit because that leads to infinite
- // recursion because reset_pin_number may call
- // rgb_led_status_init.
- spi_m_sync_disable(&status_apa102.spi_desc);
+ // This may call us recursively if common_hal_reset_pin() is called,
+ // The rgb_led_status_init_in_progress guard will prevent further recursion.
+ common_hal_busio_spi_deinit(&status_apa102);
}
common_hal_busio_spi_construct(&status_apa102,
MICROPY_HW_APA102_SCK,
MICROPY_HW_APA102_MOSI,
mp_const_none);
+ common_hal_busio_spi_never_reset(&status_apa102);
#endif
// Pretend we aren't using the pins. bitbangio.SPI will
// mark them as used.
@@ -149,15 +176,17 @@ void rgb_led_status_init() {
current_status_color = 0x1000000; // Not a valid color
new_status_color(rgb);
#endif
+
+ rgb_led_status_init_in_progress = false;
}
void reset_status_led() {
#ifdef MICROPY_HW_NEOPIXEL
- reset_pin_number(MICROPY_HW_NEOPIXEL->number);
+ common_hal_reset_pin(MICROPY_HW_NEOPIXEL);
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
- reset_pin_number(MICROPY_HW_APA102_MOSI->number);
- reset_pin_number(MICROPY_HW_APA102_SCK->number);
+ common_hal_reset_pin(MICROPY_HW_APA102_MOSI);
+ common_hal_reset_pin(MICROPY_HW_APA102_SCK);
#endif
#if defined(CP_RGB_STATUS_LED)
// TODO: Support sharing status LED with user.
@@ -332,7 +361,7 @@ void prep_rgb_status_animation(const pyexec_result_t* result,
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))
new_status_color(ALL_DONE);
- status->pattern_start = ticks_ms;
+ status->pattern_start = supervisor_ticks_ms32();
status->safe_mode = safe_mode;
status->found_main = found_main;
status->total_exception_cycle = 0;
@@ -377,11 +406,11 @@ void prep_rgb_status_animation(const pyexec_result_t* result,
void 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 = ticks_ms - status->pattern_start;
+ 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 = ticks_ms;
+ status->pattern_start = supervisor_ticks_ms32();
tick_diff = 0;
}
@@ -396,7 +425,7 @@ void tick_rgb_status_animation(rgb_status_animation_t* status) {
}
} else {
if (tick_diff > status->total_exception_cycle) {
- status->pattern_start = ticks_ms;
+ status->pattern_start = supervisor_ticks_ms32();
tick_diff = 0;
}
// First flash the file color.
diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c
index d8d3ab379..363181da0 100644
--- a/supervisor/shared/safe_mode.c
+++ b/supervisor/shared/safe_mode.c
@@ -34,6 +34,7 @@
#include "supervisor/shared/rgb_led_colors.h"
#include "supervisor/shared/rgb_led_status.h"
#include "supervisor/shared/translate.h"
+#include "supervisor/shared/tick.h"
#define SAFE_MODE_DATA_GUARD 0xad0000af
#define SAFE_MODE_DATA_GUARD_MASK 0xff0000ff
@@ -59,14 +60,14 @@ safe_mode_t wait_for_safe_mode_reset(void) {
common_hal_digitalio_digitalinout_construct(&status_led, MICROPY_HW_LED_STATUS);
common_hal_digitalio_digitalinout_switch_to_output(&status_led, true, DRIVE_MODE_PUSH_PULL);
#endif
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
uint64_t diff = 0;
while (diff < 700) {
#ifdef MICROPY_HW_LED_STATUS
// Blink on for 100, off for 100, on for 100, off for 100 and on for 200
common_hal_digitalio_digitalinout_set_value(&status_led, diff > 100 && diff / 100 != 2 && diff / 100 != 4);
#endif
- diff = ticks_ms - start_ticks;
+ diff = supervisor_ticks_ms64() - start_ticks;
}
#ifdef MICROPY_HW_LED_STATUS
common_hal_digitalio_digitalinout_deinit(&status_led);
diff --git a/supervisor/shared/safe_mode.h b/supervisor/shared/safe_mode.h
index ee0723cff..8c5dcd9c5 100644
--- a/supervisor/shared/safe_mode.h
+++ b/supervisor/shared/safe_mode.h
@@ -37,7 +37,8 @@ typedef enum {
MICROPY_NLR_JUMP_FAIL,
MICROPY_FATAL_ERROR,
GC_ALLOC_OUTSIDE_VM,
- PROGRAMMATIC_SAFE_MODE
+ PROGRAMMATIC_SAFE_MODE,
+ NORDIC_SOFT_DEVICE_ASSERT
} safe_mode_t;
safe_mode_t wait_for_safe_mode_reset(void);
diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c
index 311fa31b2..dcecf2067 100755
--- a/supervisor/shared/stack.c
+++ b/supervisor/shared/stack.c
@@ -29,6 +29,7 @@
#include "py/mpconfig.h"
#include "py/runtime.h"
#include "supervisor/cpu.h"
+#include "supervisor/port.h"
#include "supervisor/shared/safe_mode.h"
extern uint32_t _estack;
@@ -43,7 +44,7 @@ void allocate_stack(void) {
mp_uint_t regs[10];
mp_uint_t sp = cpu_get_regs_and_sp(regs);
- mp_uint_t c_size = (uint32_t) &_estack - sp;
+ 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);
if (stack_alloc == NULL) {
diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c
new file mode 100644
index 000000000..69256081c
--- /dev/null
+++ b/supervisor/shared/tick.c
@@ -0,0 +1,94 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Jeff Epler 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/shared/tick.h"
+#include "supervisor/filesystem.h"
+#include "supervisor/shared/autoreload.h"
+
+static volatile uint64_t ticks_ms;
+static volatile uint32_t background_ticks_ms32;
+
+#if CIRCUITPY_GAMEPAD
+#include "shared-module/gamepad/__init__.h"
+#endif
+
+#if CIRCUITPY_GAMEPADSHIFT
+#include "shared-module/gamepadshift/__init__.h"
+#endif
+
+#include "shared-bindings/microcontroller/__init__.h"
+
+void supervisor_tick(void) {
+
+ ticks_ms ++;
+
+#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
+ filesystem_tick();
+#endif
+#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
+ autoreload_tick();
+#endif
+#ifdef CIRCUITPY_GAMEPAD_TICKS
+ if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
+ #if CIRCUITPY_GAMEPAD
+ gamepad_tick();
+ #endif
+ #if CIRCUITPY_GAMEPADSHIFT
+ gamepadshift_tick();
+ #endif
+ }
+#endif
+}
+
+uint64_t supervisor_ticks_ms64() {
+ uint64_t result;
+ common_hal_mcu_disable_interrupts();
+ result = ticks_ms;
+ common_hal_mcu_enable_interrupts();
+ return result;
+}
+
+uint32_t supervisor_ticks_ms32() {
+ return ticks_ms;
+}
+
+extern void run_background_tasks(void);
+
+void supervisor_run_background_tasks_if_tick() {
+ uint32_t now32 = ticks_ms;
+
+ if (now32 == background_ticks_ms32) {
+ return;
+ }
+ background_ticks_ms32 = now32;
+
+ run_background_tasks();
+}
+
+void supervisor_fake_tick() {
+ uint32_t now32 = ticks_ms;
+ background_ticks_ms32 = (now32 - 1);
+}
diff --git a/supervisor/shared/tick.h b/supervisor/shared/tick.h
new file mode 100644
index 000000000..7ce8281ba
--- /dev/null
+++ b/supervisor/shared/tick.h
@@ -0,0 +1,67 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Jeff Epler 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 __INCLUDED_SUPERVISOR_TICK_H
+#define __INCLUDED_SUPERVISOR_TICK_H
+
+#include <stdint.h>
+
+/** @brief To be called once every ms
+ *
+ * The port must call supervisor_tick once per millisecond to perform regular tasks.
+ * This is called from the SysTick interrupt or similar, and is safe to call in an
+ * interrupt context.
+ */
+extern void supervisor_tick(void);
+/** @brief Cause background tasks to be called soon
+ *
+ * Normally, background tasks are only run once per tick. For other cases where
+ * an event noticed from an interrupt context needs to be completed by a background
+ * task activity, the interrupt can call supervisor_fake_tick.
+ */
+extern void supervisor_fake_tick(void);
+/** @brief Get the lower 32 bits of the time in milliseconds
+ *
+ * This can be more efficient than supervisor_ticks_ms64, for sites where a wraparound
+ * of ~49.5 days is not harmful.
+ */
+extern uint32_t supervisor_ticks_ms32(void);
+/** @brief Get the full time in milliseconds
+ *
+ * Because common ARM mcus cannot atomically work with 64-bit quantities, this
+ * function must briefly disable interrupts in order to return the value. If
+ * only relative durations of less than about ~49.5 days need to be considered,
+ * then it may be possible to use supervisor_ticks_ms64 instead.
+ */
+extern uint64_t supervisor_ticks_ms64(void);
+/** @brief Run background ticks, but only about every millisecond.
+ *
+ * Normally, this is not called directly. Instead use the RUN_BACKGROUND_TASKS
+ * macro.
+ */
+extern void supervisor_run_background_if_tick(void);
+
+#endif
diff --git a/supervisor/shared/translate.c b/supervisor/shared/translate.c
index aa5e4517c..187d5ff8a 100644
--- a/supervisor/shared/translate.c
+++ b/supervisor/shared/translate.c
@@ -42,12 +42,28 @@ void serial_write_compressed(const compressed_string_t* compressed) {
serial_write(decompressed);
}
+STATIC int put_utf8(char *buf, int u) {
+ if(u <= 0x7f) {
+ *buf = u;
+ return 1;
+ } else if(u <= 0x07ff) {
+ *buf++ = 0b11000000 | (u >> 6);
+ *buf = 0b10000000 | (u & 0b00111111);
+ return 2;
+ } else { // u <= 0xffff)
+ *buf++ = 0b11000000 | (u >> 12);
+ *buf = 0b10000000 | ((u >> 6) & 0b00111111);
+ *buf = 0b10000000 | (u & 0b00111111);
+ return 3;
+ }
+}
+
char* decompress(const compressed_string_t* compressed, char* decompressed) {
uint8_t this_byte = 0;
uint8_t this_bit = 7;
uint8_t b = compressed->data[this_byte];
// Stop one early because the last byte is always NULL.
- for (uint16_t i = 0; i < compressed->length - 1; i++) {
+ for (uint16_t i = 0; i < compressed->length - 1;) {
uint32_t bits = 0;
uint8_t bit_length = 0;
uint32_t max_code = lengths[0];
@@ -72,7 +88,7 @@ char* decompress(const compressed_string_t* compressed, char* decompressed) {
max_code = (max_code << 1) + lengths[bit_length];
searched_length += lengths[bit_length];
}
- decompressed[i] = values[searched_length + bits - max_code];
+ i += put_utf8(decompressed + i, values[searched_length + bits - max_code]);
}
decompressed[compressed->length-1] = '\0';
diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk
index f576bc546..87d506b3b 100644
--- a/supervisor/supervisor.mk
+++ b/supervisor/supervisor.mk
@@ -10,26 +10,36 @@ SRC_SUPERVISOR = \
supervisor/shared/safe_mode.c \
supervisor/shared/stack.c \
supervisor/shared/status_leds.c \
+ supervisor/shared/tick.c \
supervisor/shared/translate.c
ifndef $(NO_USB)
NO_USB = $(wildcard supervisor/usb.c)
endif
-ifneq ($(INTERNAL_FLASH_FILESYSTEM),)
- CFLAGS += -DINTERNAL_FLASH_FILESYSTEM=$(INTERNAL_FLASH_FILESYSTEM)
+ifndef INTERNAL_FLASH_FILESYSTEM
+INTERNAL_FLASH_FILESYSTEM = 0
+endif
+CFLAGS += -DINTERNAL_FLASH_FILESYSTEM=$(INTERNAL_FLASH_FILESYSTEM)
+
+ifndef QSPI_FLASH_FILESYSTEM
+QSPI_FLASH_FILESYSTEM = 0
endif
-ifneq ($(QSPI_FLASH_FILESYSTEM),)
# EXPRESS_BOARD is obsolete and should be removed when samd-peripherals is updated.
- CFLAGS += -DQSPI_FLASH_FILESYSTEM=$(QSPI_FLASH_FILESYSTEM) -DEXPRESS_BOARD
+CFLAGS += -DQSPI_FLASH_FILESYSTEM=$(QSPI_FLASH_FILESYSTEM) -DEXPRESS_BOARD
+
+ifndef SPI_FLASH_FILESYSTEM
+SPI_FLASH_FILESYSTEM = 0
endif
-ifneq ($(SPI_FLASH_FILESYSTEM),)
# EXPRESS_BOARD is obsolete and should be removed when samd-peripherals is updated.
- CFLAGS += -DSPI_FLASH_FILESYSTEM=$(SPI_FLASH_FILESYSTEM) -DEXPRESS_BOARD
+CFLAGS += -DSPI_FLASH_FILESYSTEM=$(SPI_FLASH_FILESYSTEM) -DEXPRESS_BOARD
+
+ifeq ($(CIRCUITPY_BLEIO),1)
+ SRC_SUPERVISOR += supervisor/shared/bluetooth.c
endif
# Choose which flash filesystem impl to use.
-# (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive.
+# (Right now INTERNAL_FLASH_FILESYSTEM and (Q)SPI_FLASH_FILESYSTEM are mutually exclusive.
# But that might not be true in the future.)
ifdef EXTERNAL_FLASH_DEVICES
CFLAGS += -DEXTERNAL_FLASH_DEVICES=$(EXTERNAL_FLASH_DEVICES) \
@@ -87,6 +97,10 @@ else
CFLAGS += -DUSB_AVAILABLE
endif
+ifndef USB_INTERFACE_NAME
+USB_INTERFACE_NAME = "CircuitPython"
+endif
+
ifndef USB_DEVICES
USB_DEVICES = "CDC,MSC,AUDIO,HID"
endif
@@ -141,6 +155,7 @@ USB_DESCRIPTOR_ARGS = \
--vid $(USB_VID)\
--pid $(USB_PID)\
--serial_number_length $(USB_SERIAL_NUMBER_LENGTH)\
+ --interface_name $(USB_INTERFACE_NAME)\
--devices $(USB_DEVICES)\
--hid_devices $(USB_HID_DEVICES)\
--msc_max_packet_size $(USB_MSC_MAX_PACKET_SIZE)\