From d6c26942a5fadeb95305c86a604233257a7858db Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 13 May 2018 21:54:44 -0400 Subject: [PATCH 001/103] add timeout keyword to I2C - for bitbangio - ignored for busio --- ports/atmel-samd/common-hal/busio/I2C.c | 2 +- ports/nrf/common-hal/busio/I2C.c | 2 +- py/objexcept.c | 4 +--- shared-bindings/bitbangio/I2C.c | 7 +++++-- shared-bindings/bitbangio/I2C.h | 3 ++- shared-bindings/busio/I2C.c | 6 ++++-- shared-bindings/busio/I2C.h | 3 ++- shared-module/bitbangio/I2C.c | 15 +++++++++++---- shared-module/bitbangio/types.h | 1 + shared-module/busio/I2C.c | 4 ++-- 10 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ports/atmel-samd/common-hal/busio/I2C.c b/ports/atmel-samd/common-hal/busio/I2C.c index 77676b5355..347a2e3490 100644 --- a/ports/atmel-samd/common-hal/busio/I2C.c +++ b/ports/atmel-samd/common-hal/busio/I2C.c @@ -41,7 +41,7 @@ #define ATTEMPTS 2 void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, - const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) { + const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) { Sercom* sercom = NULL; uint8_t sercom_index; uint32_t sda_pinmux = 0; diff --git a/ports/nrf/common-hal/busio/I2C.c b/ports/nrf/common-hal/busio/I2C.c index 07b067a2cb..7e92a84ffe 100644 --- a/ports/nrf/common-hal/busio/I2C.c +++ b/ports/nrf/common-hal/busio/I2C.c @@ -32,7 +32,7 @@ #include "pins.h" #include "nrf.h" -void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) { +void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout ) { if (scl->pin == sda->pin) { mp_raise_ValueError("Invalid pins"); } diff --git a/py/objexcept.c b/py/objexcept.c index 4844c7f92d..9ef2f2e34d 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -275,9 +275,7 @@ MP_DEFINE_EXCEPTION(Exception, BaseException) MP_DEFINE_EXCEPTION(UnboundLocalError, NameError) */ MP_DEFINE_EXCEPTION(OSError, Exception) -#if MICROPY_PY_BUILTINS_TIMEOUTERROR - MP_DEFINE_EXCEPTION(TimeoutError, OSError) -#endif + MP_DEFINE_EXCEPTION(TimeoutError, OSError) /* MP_DEFINE_EXCEPTION(BlockingIOError, OSError) MP_DEFINE_EXCEPTION(ChildProcessError, OSError) diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c index f8f60210e6..a5c000ae5a 100644 --- a/shared-bindings/bitbangio/I2C.c +++ b/shared-bindings/bitbangio/I2C.c @@ -35,6 +35,7 @@ #include "lib/utils/context_manager_helpers.h" #include "py/mperrno.h" #include "py/runtime.h" + //| .. currentmodule:: bitbangio //| //| :class:`I2C` --- Two wire serial protocol @@ -49,6 +50,7 @@ //| :param ~microcontroller.Pin scl: The clock pin //| :param ~microcontroller.Pin sda: The data pin //| :param int frequency: The clock frequency of the bus +//| :param int timeout: The maximum clock stretching timeout in microseconds //| STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); @@ -57,11 +59,12 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, self->base.type = &bitbangio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_scl, ARG_sda, ARG_frequency }; + enum { ARG_scl, ARG_sda, ARG_frequency, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -69,7 +72,7 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin(args[ARG_sda].u_obj, false); const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj); const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); - shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); + shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int, args[ARG_timeout].u_int); return (mp_obj_t)self; } diff --git a/shared-bindings/bitbangio/I2C.h b/shared-bindings/bitbangio/I2C.h index 9d393ee124..1ce4d21e91 100644 --- a/shared-bindings/bitbangio/I2C.h +++ b/shared-bindings/bitbangio/I2C.h @@ -39,7 +39,8 @@ extern const mp_obj_type_t bitbangio_i2c_type; extern void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, - uint32_t frequency); + uint32_t frequency, + uint32_t us_timeout); extern void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self); extern bool shared_module_bitbangio_i2c_deinited(bitbangio_i2c_obj_t *self); diff --git a/shared-bindings/busio/I2C.c b/shared-bindings/busio/I2C.c index b0954d6c8a..0d21d30f00 100644 --- a/shared-bindings/busio/I2C.c +++ b/shared-bindings/busio/I2C.c @@ -56,6 +56,7 @@ //| :param ~microcontroller.Pin scl: The clock pin //| :param ~microcontroller.Pin sda: The data pin //| :param int frequency: The clock frequency in Hertz +//| :param int timeout: The maximum clock stretching timeut - only for bitbang //| STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); @@ -63,11 +64,12 @@ STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz self->base.type = &busio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_scl, ARG_sda, ARG_frequency }; + enum { ARG_scl, ARG_sda, ARG_frequency, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -77,7 +79,7 @@ STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz assert_pin_free(scl); const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); assert_pin_free(sda); - common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); + common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int, args[ARG_timeout].u_int); return (mp_obj_t)self; } diff --git a/shared-bindings/busio/I2C.h b/shared-bindings/busio/I2C.h index 7185d6075c..739732e97b 100644 --- a/shared-bindings/busio/I2C.h +++ b/shared-bindings/busio/I2C.h @@ -46,7 +46,8 @@ extern const mp_obj_type_t busio_i2c_type; extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, - uint32_t frequency); + uint32_t frequency, + uint32_t timeout); extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self); extern bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self); diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index 7eeda0e00e..ac2f9aead5 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -25,8 +25,8 @@ */ #include "shared-bindings/bitbangio/I2C.h" - #include "py/mperrno.h" +#include "py/nlr.h" #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" @@ -34,7 +34,6 @@ #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-module/bitbangio/types.h" -#define I2C_STRETCH_LIMIT 255 STATIC void delay(bitbangio_i2c_obj_t *self) { // We need to use an accurate delay to get acceptable I2C @@ -48,11 +47,16 @@ STATIC void scl_low(bitbangio_i2c_obj_t *self) { STATIC void scl_release(bitbangio_i2c_obj_t *self) { common_hal_digitalio_digitalinout_set_value(&self->scl, true); + uint32_t count = self->us_timeout; delay(self); // For clock stretching, wait for the SCL pin to be released, with timeout. - for (int count = I2C_STRETCH_LIMIT; !common_hal_digitalio_digitalinout_get_value(&self->scl) && count; --count) { + for (; !common_hal_digitalio_digitalinout_get_value(&self->scl) && count; --count) { common_hal_mcu_delay_us(1); } + if(count==0) { /// raise exception on timeout + nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, + "Clock Stretching Timeout.")); + } } STATIC void sda_low(bitbangio_i2c_obj_t *self) { @@ -142,7 +146,10 @@ STATIC bool read_byte(bitbangio_i2c_obj_t *self, uint8_t *val, bool ack) { void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, - uint32_t frequency) { + uint32_t frequency, + uint32_t us_timeout) { + + self->us_timeout = us_timeout; self->us_delay = 500000 / frequency; if (self->us_delay == 0) { self->us_delay = 1; diff --git a/shared-module/bitbangio/types.h b/shared-module/bitbangio/types.h index dfe50f0499..6d17234741 100644 --- a/shared-module/bitbangio/types.h +++ b/shared-module/bitbangio/types.h @@ -36,6 +36,7 @@ typedef struct { digitalio_digitalinout_obj_t scl; digitalio_digitalinout_obj_t sda; uint32_t us_delay; + uint32_t us_timeout; volatile bool locked; } bitbangio_i2c_obj_t; diff --git a/shared-module/busio/I2C.c b/shared-module/busio/I2C.c index 68d6ea22ab..06e1af10a1 100644 --- a/shared-module/busio/I2C.c +++ b/shared-module/busio/I2C.c @@ -30,8 +30,8 @@ #include "py/nlr.h" void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, - const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t freq) { - shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq); + const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t freq, uint32_t timeout) { + shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq, timeout); } bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { From aa9309704bb174c351034a866bb6b5b7073463db Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 13 May 2018 22:21:05 -0400 Subject: [PATCH 002/103] revise to use mp_raise_msg insead of nlr_raise.... --- shared-module/bitbangio/I2C.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index ac2f9aead5..8381f051af 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -28,6 +28,7 @@ #include "py/mperrno.h" #include "py/nlr.h" #include "py/obj.h" +#include "py/runtime.h" #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" @@ -54,8 +55,8 @@ STATIC void scl_release(bitbangio_i2c_obj_t *self) { common_hal_mcu_delay_us(1); } if(count==0) { /// raise exception on timeout - nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, - "Clock Stretching Timeout.")); + mp_raise_msg(&mp_type_TimeoutError, + "Clock Stretching Timeout."); } } From d9f1b1f5b786a1e11e41da10bddb404e0285987c Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 13 May 2018 22:32:32 -0400 Subject: [PATCH 003/103] remove #include py/nlr.h from I2C.c --- shared-module/bitbangio/I2C.c | 1 - 1 file changed, 1 deletion(-) diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index 8381f051af..7bf8292db2 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -26,7 +26,6 @@ #include "shared-bindings/bitbangio/I2C.h" #include "py/mperrno.h" -#include "py/nlr.h" #include "py/obj.h" #include "py/runtime.h" From 0518cc1d94a3c54e767496b38287333102b9d52a Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Mon, 14 May 2018 14:54:16 -0400 Subject: [PATCH 004/103] Include uerrno for boards with external SPI flash Signed-off-by: Matt Wozniski --- ports/atmel-samd/mpconfigport.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index e6c3a201fe..27c2480013 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -188,6 +188,8 @@ extern const struct _mp_obj_module_t usb_hid_module; #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_STR_SPLITLINES (1) #define MICROPY_PY_BUILTINS_REVERSED (1) + #define MICROPY_PY_UERRNO (1) + #define MICROPY_PY_UERRNO_ERRORCODE (0) #define MICROPY_PY_URE (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_FRAMEBUF (1) From 5833cbe6dec64a980054ed0b408033cff9d2f22e Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Mon, 14 May 2018 14:54:39 -0400 Subject: [PATCH 005/103] Display human readable strings for common OSErrors Signed-off-by: Matt Wozniski --- py/moduerrno.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/py/moduerrno.c b/py/moduerrno.c index de66c941b0..ce8a6bf89e 100644 --- a/py/moduerrno.c +++ b/py/moduerrno.c @@ -100,6 +100,20 @@ const mp_obj_module_t mp_module_uerrno = { }; qstr mp_errno_to_str(mp_obj_t errno_val) { + // For commonly encountered errors, return human readable strings + if (MP_OBJ_IS_SMALL_INT(errno_val)) { + switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) { + case EPERM: return MP_QSTR_Permission_space_denied; + case ENOENT: return MP_QSTR_No_space_such_space_file_slash_directory; + case EIO: return MP_QSTR_Input_slash_output_space_error; + case EACCES: return MP_QSTR_Permission_space_denied; + case EEXIST: return MP_QSTR_File_space_exists; + case ENODEV: return MP_QSTR_Unsupported_space_operation; + case EINVAL: return MP_QSTR_Invalid_space_argument; + } + } + + // Otherwise, return the Exxxx string for that error code #if MICROPY_PY_UERRNO_ERRORCODE // We have the errorcode dict so can do a lookup using the hash map mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP); From 567f3e30a77f286713b697a0f8a85e8b31a4a27a Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Mon, 14 May 2018 16:57:50 -0400 Subject: [PATCH 006/103] Initial implementation of supervisor.reload() --- docs/library/builtins.rst | 4 ++++ py/modbuiltins.c | 1 + py/mpstate.h | 3 +++ py/obj.h | 1 + py/objexcept.c | 1 + py/runtime.c | 6 ++++++ shared-bindings/supervisor/__init__.c | 23 +++++++++++++++++++++++ 7 files changed, 39 insertions(+) diff --git a/docs/library/builtins.rst b/docs/library/builtins.rst index b45b6fe380..83246afc92 100644 --- a/docs/library/builtins.rst +++ b/docs/library/builtins.rst @@ -188,6 +188,10 @@ Exceptions .. exception:: RuntimeError +.. exception:: ReloadException + + `ReloadException` is used internally to deal with soft restarts. + .. exception:: StopIteration .. exception:: SyntaxError diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 5e11b8d18c..cea87b1813 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -700,6 +700,7 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_IndentationError), MP_ROM_PTR(&mp_type_IndentationError) }, { MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) }, { MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) }, + { MP_ROM_QSTR(MP_QSTR_ReloadException), MP_ROM_PTR(&mp_type_ReloadException) }, { MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) }, { MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) }, { MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) }, diff --git a/py/mpstate.h b/py/mpstate.h index 19a5d711ed..c509470662 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -136,6 +136,9 @@ typedef struct _mp_state_vm_t { mp_obj_exception_t mp_kbd_exception; #endif + // exception object of type ReloadException + mp_obj_exception_t mp_reload_exception; + // dictionary with loaded modules (may be exposed as sys.modules) mp_obj_dict_t mp_loaded_modules_dict; diff --git a/py/obj.h b/py/obj.h index e4d950b97c..d086d3c149 100644 --- a/py/obj.h +++ b/py/obj.h @@ -590,6 +590,7 @@ extern const mp_obj_type_t mp_type_ImportError; extern const mp_obj_type_t mp_type_IndentationError; extern const mp_obj_type_t mp_type_IndexError; extern const mp_obj_type_t mp_type_KeyboardInterrupt; +extern const mp_obj_type_t mp_type_ReloadException; extern const mp_obj_type_t mp_type_KeyError; extern const mp_obj_type_t mp_type_LookupError; extern const mp_obj_type_t mp_type_MemoryError; diff --git a/py/objexcept.c b/py/objexcept.c index 4844c7f92d..bcf8493c4e 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -249,6 +249,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \ // http://docs.python.org/3/library/exceptions.html MP_DEFINE_EXCEPTION(SystemExit, BaseException) MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) +MP_DEFINE_EXCEPTION(ReloadException, BaseException) MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) MP_DEFINE_EXCEPTION(Exception, BaseException) #if MICROPY_PY_ASYNC_AWAIT diff --git a/py/runtime.c b/py/runtime.c index cbec82f17c..a7ff57a47f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -84,6 +84,12 @@ void mp_init(void) { MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; #endif + MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException; + MP_STATE_VM(mp_reload_exception).traceback_alloc = 0; + MP_STATE_VM(mp_reload_exception).traceback_len = 0; + MP_STATE_VM(mp_reload_exception).traceback_data = NULL; + MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; + // call port specific initialization if any #ifdef MICROPY_PORT_INIT_FUNC MICROPY_PORT_INIT_FUNC; diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c index a82459ad78..9a3fa855af 100644 --- a/shared-bindings/supervisor/__init__.c +++ b/shared-bindings/supervisor/__init__.c @@ -25,11 +25,15 @@ */ #include "py/obj.h" #include "py/runtime.h" + + #include "lib/utils/interrupt_char.h" #include "supervisor/shared/autoreload.h" + #include "supervisor/shared/rgb_led_status.h" #include "shared-bindings/supervisor/__init__.h" #include "shared-bindings/supervisor/Runtime.h" + //| :mod:`supervisor` --- Supervisor settings //| ================================================= //| @@ -91,12 +95,31 @@ STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl){ } MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness); +//| .. method:: reload() +//| +//| Reload the microcontroller (equivalent to hitting Ctrl-D at the REPL). +//| +STATIC mp_obj_t supervisor_reload(void) { + reload_next_character = true; + + MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)); + #if MICROPY_ENABLE_SCHEDULER + if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { + MP_STATE_VM(sched_state) = MP_SCHED_PENDING; + } + #endif + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload); + + STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) }, { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) }, { MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) }, }; STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table); From 698912633fab70b7a0fa19a84c730face41b10ff Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Mon, 14 May 2018 17:00:38 -0400 Subject: [PATCH 007/103] Rename reload_next_character to reload_requested to make it match it's intended use --- main.c | 4 ++-- ports/atmel-samd/mphalport.c | 2 +- shared-bindings/supervisor/__init__.c | 2 +- supervisor/shared/autoreload.c | 10 +++++----- supervisor/shared/autoreload.h | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index bc37fa8711..24f27438c9 100644 --- a/main.c +++ b/main.c @@ -168,7 +168,7 @@ bool start_mp(safe_mode_t safe_mode) { reset_status_led(); if (result.return_code & PYEXEC_FORCED_EXIT) { - return reload_next_character; + return reload_requested; } } @@ -180,7 +180,7 @@ bool start_mp(safe_mode_t safe_mode) { #ifdef MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_LOOP #endif - if (reload_next_character) { + if (reload_requested) { return true; } diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index cdf27fe028..c1b8ce5504 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -28,7 +28,7 @@ int mp_hal_stdin_rx_chr(void) { #ifdef MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_LOOP #endif - // if (reload_next_character) { + // if (reload_requested) { // return CHAR_CTRL_D; // } if (usb_bytes_available()) { diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c index 9a3fa855af..4eae404060 100644 --- a/shared-bindings/supervisor/__init__.c +++ b/shared-bindings/supervisor/__init__.c @@ -100,7 +100,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s //| Reload the microcontroller (equivalent to hitting Ctrl-D at the REPL). //| STATIC mp_obj_t supervisor_reload(void) { - reload_next_character = true; + reload_requested = true; MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)); #if MICROPY_ENABLE_SCHEDULER diff --git a/supervisor/shared/autoreload.c b/supervisor/shared/autoreload.c index 39c33c05ea..fc70991fb1 100644 --- a/supervisor/shared/autoreload.c +++ b/supervisor/shared/autoreload.c @@ -32,23 +32,23 @@ volatile uint32_t autoreload_delay_ms = 0; bool autoreload_enabled = false; static bool autoreload_suspended = false; -volatile bool reload_next_character = false; +volatile bool reload_requested = false; inline void autoreload_tick() { if (autoreload_delay_ms == 0) { return; } if (autoreload_delay_ms == 1 && autoreload_enabled && - !autoreload_suspended && !reload_next_character) { + !autoreload_suspended && !reload_requested) { mp_keyboard_interrupt(); - reload_next_character = true; + reload_requested = true; } autoreload_delay_ms--; } void autoreload_enable() { autoreload_enabled = true; - reload_next_character = false; + reload_requested = false; } void autoreload_disable() { @@ -73,5 +73,5 @@ void autoreload_start() { void autoreload_stop() { autoreload_delay_ms = 0; - reload_next_character = false; + reload_requested = false; } diff --git a/supervisor/shared/autoreload.h b/supervisor/shared/autoreload.h index 62f9fe0738..bcb1001513 100644 --- a/supervisor/shared/autoreload.h +++ b/supervisor/shared/autoreload.h @@ -29,7 +29,7 @@ #include -extern volatile bool reload_next_character; +extern volatile bool reload_requested; void autoreload_tick(void); From 92b1cb574320578abe022c021c4b1891cfafbae6 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Mon, 14 May 2018 17:41:17 -0400 Subject: [PATCH 008/103] move reload exception to reload.c --- py/py.mk | 1 + py/reload.c | 16 ++++++++++++++++ py/reload.h | 10 ++++++++++ shared-bindings/supervisor/__init__.c | 9 ++------- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 py/reload.c create mode 100644 py/reload.h diff --git a/py/py.mk b/py/py.mk index 21f81c35d5..e934c3a83f 100644 --- a/py/py.mk +++ b/py/py.mk @@ -188,6 +188,7 @@ PY_O_BASENAME = \ objtype.o \ objzip.o \ opmethods.o \ + reload.o \ sequence.o \ stream.o \ binary.o \ diff --git a/py/reload.c b/py/reload.c new file mode 100644 index 0000000000..95305f2c9c --- /dev/null +++ b/py/reload.c @@ -0,0 +1,16 @@ +// +// Created by Roy Hooper on 2018-05-14. +// + +#include "reload.h" +#include "py/mpstate.h" + +void mp_raise_reload_exception(void) { + MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)); +#if MICROPY_ENABLE_SCHEDULER + if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { + MP_STATE_VM(sched_state) = MP_SCHED_PENDING; + } +#endif + +} diff --git a/py/reload.h b/py/reload.h new file mode 100644 index 0000000000..72e84e5ca6 --- /dev/null +++ b/py/reload.h @@ -0,0 +1,10 @@ +// +// Created by Roy Hooper on 2018-05-14. +// + +#ifndef CIRCUITPYTHON_RELOAD_H +#define CIRCUITPYTHON_RELOAD_H + +void mp_raise_reload_exception(void); + +#endif //CIRCUITPYTHON_RELOAD_H diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c index 4eae404060..b2b0c80d96 100644 --- a/shared-bindings/supervisor/__init__.c +++ b/shared-bindings/supervisor/__init__.c @@ -25,6 +25,7 @@ */ #include "py/obj.h" #include "py/runtime.h" + #include "py/reload.h" #include "lib/utils/interrupt_char.h" #include "supervisor/shared/autoreload.h" @@ -101,13 +102,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s //| STATIC mp_obj_t supervisor_reload(void) { reload_requested = true; - - MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)); - #if MICROPY_ENABLE_SCHEDULER - if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { - MP_STATE_VM(sched_state) = MP_SCHED_PENDING; - } - #endif + mp_raise_reload_exception(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload); From 06b6a10a5e54b7ba7421e2fc7ca42a2172773398 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Mon, 14 May 2018 17:44:50 -0400 Subject: [PATCH 009/103] switch autoreload to reloadexception --- supervisor/shared/autoreload.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervisor/shared/autoreload.c b/supervisor/shared/autoreload.c index fc70991fb1..2a7fd1e9d8 100644 --- a/supervisor/shared/autoreload.c +++ b/supervisor/shared/autoreload.c @@ -26,8 +26,8 @@ #include "autoreload.h" -#include "lib/utils/interrupt_char.h" #include "py/mphal.h" +#include "py/reload.h" volatile uint32_t autoreload_delay_ms = 0; bool autoreload_enabled = false; @@ -40,7 +40,7 @@ inline void autoreload_tick() { } if (autoreload_delay_ms == 1 && autoreload_enabled && !autoreload_suspended && !reload_requested) { - mp_keyboard_interrupt(); + mp_raise_reload_exception(); reload_requested = true; } autoreload_delay_ms--; From 086bffb5940eb0450b4213934c6c1ed12d124e7c Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Mon, 14 May 2018 22:55:21 -0400 Subject: [PATCH 010/103] Update errno tests to match new expected behavior Signed-off-by: Matt Wozniski --- tests/basics/errno1.py | 5 ++++- tests/basics/errno1.py.exp | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/basics/errno1.py b/tests/basics/errno1.py index 63930b767f..bbbfac10ca 100644 --- a/tests/basics/errno1.py +++ b/tests/basics/errno1.py @@ -11,7 +11,10 @@ print(type(uerrno.EIO)) # check that errors are rendered in a nice way msg = str(OSError(uerrno.EIO)) -print(msg[:7], msg[-5:]) +print(msg[:7], msg[msg.find(']'):]) + +msg = str(OSError(uerrno.ENOBUFS)) +print(msg[:7], msg[msg.find(']'):]) # check that unknown errno is still rendered print(str(OSError(9999))) diff --git a/tests/basics/errno1.py.exp b/tests/basics/errno1.py.exp index c3703df4a2..b5dd529e46 100644 --- a/tests/basics/errno1.py.exp +++ b/tests/basics/errno1.py.exp @@ -1,3 +1,4 @@ -[Errno ] EIO +[Errno ] Input/output error +[Errno ] ENOBUFS 9999 From 3fd9900ea75227aeab8505f1bc149eeeb1a21b56 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Tue, 15 May 2018 13:13:59 -0400 Subject: [PATCH 011/103] disable pins on trinket_m0 so that we can free a fair bit of flash - Issue #840 --- .../boards/trinket_m0/mpconfigboard.h | 42 +++++++ .../common-hal/microcontroller/__init__.c | 104 +++++++++--------- ports/atmel-samd/samd21_pins.c | 104 +++++++++--------- 3 files changed, 146 insertions(+), 104 deletions(-) diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h index 05033ad38b..92a14ba15a 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h @@ -14,3 +14,45 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define IGNORE_PIN_PA03 1 +#define IGNORE_PIN_PA04 1 +#define IGNORE_PIN_PA11 1 +#define IGNORE_PIN_PA12 1 +#define IGNORE_PIN_PA13 1 +#define IGNORE_PIN_PA14 1 +#define IGNORE_PIN_PA15 1 +#define IGNORE_PIN_PA16 1 +#define IGNORE_PIN_PA17 1 +#define IGNORE_PIN_PA18 1 +#define IGNORE_PIN_PA19 1 +#define IGNORE_PIN_PA20 1 +#define IGNORE_PIN_PA21 1 +#define IGNORE_PIN_PA22 1 +#define IGNORE_PIN_PA23 1 +#define IGNORE_PIN_PA27 1 +#define IGNORE_PIN_PA28 1 +#define IGNORE_PIN_PA30 1 +#define IGNORE_PIN_PA31 1 +#define IGNORE_PIN_PB01 1 +#define IGNORE_PIN_PB02 1 +#define IGNORE_PIN_PB03 1 +#define IGNORE_PIN_PB04 1 +#define IGNORE_PIN_PB05 1 +#define IGNORE_PIN_PB06 1 +#define IGNORE_PIN_PB07 1 +#define IGNORE_PIN_PB08 1 +#define IGNORE_PIN_PB09 1 +#define IGNORE_PIN_PB10 1 +#define IGNORE_PIN_PB11 1 +#define IGNORE_PIN_PB12 1 +#define IGNORE_PIN_PB13 1 +#define IGNORE_PIN_PB14 1 +#define IGNORE_PIN_PB15 1 +#define IGNORE_PIN_PB16 1 +#define IGNORE_PIN_PB17 1 +#define IGNORE_PIN_PB22 1 +#define IGNORE_PIN_PB23 1 +#define IGNORE_PIN_PB30 1 +#define IGNORE_PIN_PB31 1 +#define IGNORE_PIN_PB00 1 diff --git a/ports/atmel-samd/common-hal/microcontroller/__init__.c b/ports/atmel-samd/common-hal/microcontroller/__init__.c index 4b8c33182f..a9c83646fd 100644 --- a/ports/atmel-samd/common-hal/microcontroller/__init__.c +++ b/ports/atmel-samd/common-hal/microcontroller/__init__.c @@ -97,162 +97,162 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { // This maps MCU pin names to pin objects. STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = { // Pins in datasheet order. -#ifdef PIN_PA00 +#if defined(PIN_PA00) && !defined(IGNORE_PIN_PA00) { MP_ROM_QSTR(MP_QSTR_PA00), MP_ROM_PTR(&pin_PA00) }, #endif -#ifdef PIN_PA01 +#if defined(PIN_PA01) && !defined(IGNORE_PIN_PA01) { MP_ROM_QSTR(MP_QSTR_PA01), MP_ROM_PTR(&pin_PA01) }, #endif -#ifdef PIN_PA02 +#if defined(PIN_PA02) && !defined(IGNORE_PIN_PA02) { MP_ROM_QSTR(MP_QSTR_PA02), MP_ROM_PTR(&pin_PA02) }, #endif -#ifdef PIN_PA03 +#if defined(PIN_PA03) && !defined(IGNORE_PIN_PA03) { MP_ROM_QSTR(MP_QSTR_PA03), MP_ROM_PTR(&pin_PA03) }, #endif -#ifdef PIN_PB04 +#if defined(PIN_PB04) && !defined(IGNORE_PIN_PB04) { MP_ROM_QSTR(MP_QSTR_PB04), MP_ROM_PTR(&pin_PB04) }, #endif -#ifdef PIN_PB05 +#if defined(PIN_PB05) && !defined(IGNORE_PIN_PB05) { MP_ROM_QSTR(MP_QSTR_PB05), MP_ROM_PTR(&pin_PB05) }, #endif -#ifdef PIN_PB06 +#if defined(PIN_PB06) && !defined(IGNORE_PIN_PB06) { MP_ROM_QSTR(MP_QSTR_PB06), MP_ROM_PTR(&pin_PB06) }, #endif -#ifdef PIN_PB07 +#if defined(PIN_PB07) && !defined(IGNORE_PIN_PB07) { MP_ROM_QSTR(MP_QSTR_PB07), MP_ROM_PTR(&pin_PB07) }, #endif -#ifdef PIN_PB08 +#if defined(PIN_PB08) && !defined(IGNORE_PIN_PB08) { MP_ROM_QSTR(MP_QSTR_PB08), MP_ROM_PTR(&pin_PB08) }, #endif -#ifdef PIN_PB09 +#if defined(PIN_PB09) && !defined(IGNORE_PIN_PB09) { MP_ROM_QSTR(MP_QSTR_PB09), MP_ROM_PTR(&pin_PB09) }, #endif -#ifdef PIN_PA04 +#if defined(PIN_PA04) && !defined(IGNORE_PIN_PA04) { MP_ROM_QSTR(MP_QSTR_PA04), MP_ROM_PTR(&pin_PA04) }, #endif -#ifdef PIN_PA05 +#if defined(PIN_PA05) && !defined(IGNORE_PIN_PA05) { MP_ROM_QSTR(MP_QSTR_PA05), MP_ROM_PTR(&pin_PA05) }, #endif -#ifdef PIN_PA06 +#if defined(PIN_PA06) && !defined(IGNORE_PIN_PA06) { MP_ROM_QSTR(MP_QSTR_PA06), MP_ROM_PTR(&pin_PA06) }, #endif -#ifdef PIN_PA07 +#if defined(PIN_PA07) && !defined(IGNORE_PIN_PA07) { MP_ROM_QSTR(MP_QSTR_PA07), MP_ROM_PTR(&pin_PA07) }, #endif -#ifdef PIN_PA08 +#if defined(PIN_PA08) && !defined(IGNORE_PIN_PA08) { MP_ROM_QSTR(MP_QSTR_PA08), MP_ROM_PTR(&pin_PA08) }, #endif -#ifdef PIN_PA09 +#if defined(PIN_PA09) && !defined(IGNORE_PIN_PA09) { MP_ROM_QSTR(MP_QSTR_PA09), MP_ROM_PTR(&pin_PA09) }, #endif -#ifdef PIN_PA10 +#if defined(PIN_PA10) && !defined(IGNORE_PIN_PA10) { MP_ROM_QSTR(MP_QSTR_PA10), MP_ROM_PTR(&pin_PA10) }, #endif -#ifdef PIN_PA11 +#if defined(PIN_PA11) && !defined(IGNORE_PIN_PA11) { MP_ROM_QSTR(MP_QSTR_PA11), MP_ROM_PTR(&pin_PA11) }, #endif -#ifdef PIN_PB10 +#if defined(PIN_PB10) && !defined(IGNORE_PIN_PB10) { MP_ROM_QSTR(MP_QSTR_PB10), MP_ROM_PTR(&pin_PB10) }, #endif -#ifdef PIN_PB11 +#if defined(PIN_PB11) && !defined(IGNORE_PIN_PB11) { MP_ROM_QSTR(MP_QSTR_PB11), MP_ROM_PTR(&pin_PB11) }, #endif -#ifdef PIN_PB12 +#if defined(PIN_PB12) && !defined(IGNORE_PIN_PB12) { MP_ROM_QSTR(MP_QSTR_PB12), MP_ROM_PTR(&pin_PB12) }, #endif -#ifdef PIN_PB13 +#if defined(PIN_PB13) && !defined(IGNORE_PIN_PB13) { MP_ROM_QSTR(MP_QSTR_PB13), MP_ROM_PTR(&pin_PB13) }, #endif -#ifdef PIN_PB14 +#if defined(PIN_PB14) && !defined(IGNORE_PIN_PB14) { MP_ROM_QSTR(MP_QSTR_PB14), MP_ROM_PTR(&pin_PB14) }, #endif // Second page. -#ifdef PIN_PB15 +#if defined(PIN_PB15) && !defined(IGNORE_PIN_PB15) { MP_ROM_QSTR(MP_QSTR_PB15), MP_ROM_PTR(&pin_PB15) }, #endif -#ifdef PIN_PA12 +#if defined(PIN_PA12) && !defined(IGNORE_PIN_PA12) { MP_ROM_QSTR(MP_QSTR_PA12), MP_ROM_PTR(&pin_PA12) }, #endif -#ifdef PIN_PA13 +#if defined(PIN_PA13) && !defined(IGNORE_PIN_PA13) { MP_ROM_QSTR(MP_QSTR_PA13), MP_ROM_PTR(&pin_PA13) }, #endif -#ifdef PIN_PA14 +#if defined(PIN_PA14) && !defined(IGNORE_PIN_PA14) { MP_ROM_QSTR(MP_QSTR_PA14), MP_ROM_PTR(&pin_PA14) }, #endif -#ifdef PIN_PA15 +#if defined(PIN_PA15) && !defined(IGNORE_PIN_PA15) { MP_ROM_QSTR(MP_QSTR_PA15), MP_ROM_PTR(&pin_PA15) }, #endif -#ifdef PIN_PA16 +#if defined(PIN_PA16) && !defined(IGNORE_PIN_PA16) { MP_ROM_QSTR(MP_QSTR_PA16), MP_ROM_PTR(&pin_PA16) }, #endif -#ifdef PIN_PA17 +#if defined(PIN_PA17) && !defined(IGNORE_PIN_PA17) { MP_ROM_QSTR(MP_QSTR_PA17), MP_ROM_PTR(&pin_PA17) }, #endif -#ifdef PIN_PA18 +#if defined(PIN_PA18) && !defined(IGNORE_PIN_PA18) { MP_ROM_QSTR(MP_QSTR_PA18), MP_ROM_PTR(&pin_PA18) }, #endif -#ifdef PIN_PA19 +#if defined(PIN_PA19) && !defined(IGNORE_PIN_PA19) { MP_ROM_QSTR(MP_QSTR_PA19), MP_ROM_PTR(&pin_PA19) }, #endif -#ifdef PIN_PB16 +#if defined(PIN_PB16) && !defined(IGNORE_PIN_PB16) { MP_ROM_QSTR(MP_QSTR_PB16), MP_ROM_PTR(&pin_PB16) }, #endif -#ifdef PIN_PB17 +#if defined(PIN_PB17) && !defined(IGNORE_PIN_PB17) { MP_ROM_QSTR(MP_QSTR_PB17), MP_ROM_PTR(&pin_PB17) }, #endif -#ifdef PIN_PA20 +#if defined(PIN_PA20) && !defined(IGNORE_PIN_PA20) { MP_ROM_QSTR(MP_QSTR_PA20), MP_ROM_PTR(&pin_PA20) }, #endif -#ifdef PIN_PA21 +#if defined(PIN_PA21) && !defined(IGNORE_PIN_PA21) { MP_ROM_QSTR(MP_QSTR_PA21), MP_ROM_PTR(&pin_PA21) }, #endif -#ifdef PIN_PA22 +#if defined(PIN_PA22) && !defined(IGNORE_PIN_PA22) { MP_ROM_QSTR(MP_QSTR_PA22), MP_ROM_PTR(&pin_PA22) }, #endif -#ifdef PIN_PA23 +#if defined(PIN_PA23) && !defined(IGNORE_PIN_PA23) { MP_ROM_QSTR(MP_QSTR_PA23), MP_ROM_PTR(&pin_PA23) }, #endif -#ifdef PIN_PA24 +#if defined(PIN_PA24) && !defined(IGNORE_PIN_PA24) { MP_ROM_QSTR(MP_QSTR_PA24), MP_ROM_PTR(&pin_PA24) }, #endif -#ifdef PIN_PA25 +#if defined(PIN_PA25) && !defined(IGNORE_PIN_PA25) { MP_ROM_QSTR(MP_QSTR_PA25), MP_ROM_PTR(&pin_PA25) }, #endif -#ifdef PIN_PB22 +#if defined(PIN_PB22) && !defined(IGNORE_PIN_PB22) { MP_ROM_QSTR(MP_QSTR_PB22), MP_ROM_PTR(&pin_PB22) }, #endif -#ifdef PIN_PB23 +#if defined(PIN_PB23) && !defined(IGNORE_PIN_PB23) { MP_ROM_QSTR(MP_QSTR_PB23), MP_ROM_PTR(&pin_PB23) }, #endif -#ifdef PIN_PA27 +#if defined(PIN_PA27) && !defined(IGNORE_PIN_PA27) { MP_ROM_QSTR(MP_QSTR_PA27), MP_ROM_PTR(&pin_PA27) }, #endif -#ifdef PIN_PA28 +#if defined(PIN_PA28) && !defined(IGNORE_PIN_PA28) { MP_ROM_QSTR(MP_QSTR_PA28), MP_ROM_PTR(&pin_PA28) }, #endif -#ifdef PIN_PA30 +#if defined(PIN_PA30) && !defined(IGNORE_PIN_PA30) { MP_ROM_QSTR(MP_QSTR_PA30), MP_ROM_PTR(&pin_PA30) }, #endif -#ifdef PIN_PA31 +#if defined(PIN_PA31) && !defined(IGNORE_PIN_PA31) { MP_ROM_QSTR(MP_QSTR_PA31), MP_ROM_PTR(&pin_PA31) }, #endif -#ifdef PIN_PB30 +#if defined(PIN_PB30) && !defined(IGNORE_PIN_PB30) { MP_ROM_QSTR(MP_QSTR_PB30), MP_ROM_PTR(&pin_PB30) }, #endif -#ifdef PIN_PB31 +#if defined(PIN_PB31) && !defined(IGNORE_PIN_PB31) { MP_ROM_QSTR(MP_QSTR_PB31), MP_ROM_PTR(&pin_PB31) }, #endif -#ifdef PIN_PB00 +#if defined(PIN_PB00) && !defined(IGNORE_PIN_PB00) { MP_ROM_QSTR(MP_QSTR_PB00), MP_ROM_PTR(&pin_PB00) }, #endif -#ifdef PIN_PB01 +#if defined(PIN_PB01) && !defined(IGNORE_PIN_PB01) { MP_ROM_QSTR(MP_QSTR_PB01), MP_ROM_PTR(&pin_PB01) }, #endif -#ifdef PIN_PB02 +#if defined(PIN_PB02) && !defined(IGNORE_PIN_PB02) { MP_ROM_QSTR(MP_QSTR_PB02), MP_ROM_PTR(&pin_PB02) }, #endif -#ifdef PIN_PB03 +#if defined(PIN_PB03) && !defined(IGNORE_PIN_PB03) { MP_ROM_QSTR(MP_QSTR_PB03), MP_ROM_PTR(&pin_PB03) } #endif }; diff --git a/ports/atmel-samd/samd21_pins.c b/ports/atmel-samd/samd21_pins.c index eafc4b5fb6..319870b9a3 100644 --- a/ports/atmel-samd/samd21_pins.c +++ b/ports/atmel-samd/samd21_pins.c @@ -93,21 +93,21 @@ const mcu_pin_obj_t pin_## p_name = { \ // Pins in datasheet order. // NOTE(tannewt): TC wave out 0 is commented out because the first channel is // used to vary the 16 bit timer's frequency. -#ifdef PIN_PA00 +#if defined(PIN_PA00) && !defined(IGNORE_PIN_PA00) PIN(PA00, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(1, 0), TCC(2, 0), NO_TIMER); #endif -#ifdef PIN_PA01 +#if defined(PIN_PA01) && !defined(IGNORE_PIN_PA01) PIN(PA01, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(1, 1), TCC(2, 1), NO_TIMER); #endif -#ifdef PIN_PA02 +#if defined(PIN_PA02) && !defined(IGNORE_PIN_PA02) // Touch is not allowed on A0 (PA02) on Circuit Playground Express. PIN(PA02, EXTINT_CHANNEL(2), ADC_INPUT(0), #ifdef PA02_NO_TOUCH @@ -120,140 +120,140 @@ PIN(PA02, EXTINT_CHANNEL(2), ADC_INPUT(0), NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PA03 +#if defined(PIN_PA03) && !defined(IGNORE_PIN_PA03) PIN(PA03, EXTINT_CHANNEL(3), ADC_INPUT(1), TOUCH(1), NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PB04 +#if defined(PIN_PB04) && !defined(IGNORE_PIN_PB04) PIN(PB04, EXTINT_CHANNEL(4), ADC_INPUT(12), TOUCH(10), NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PB05 +#if defined(PIN_PB05) && !defined(IGNORE_PIN_PB05) PIN(PB05, EXTINT_CHANNEL(5), ADC_INPUT(13), TOUCH(11), NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PB06 +#if defined(PIN_PB06) && !defined(IGNORE_PIN_PB06) PIN(PB06, EXTINT_CHANNEL(6), ADC_INPUT(14), TOUCH(12), NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PB07 +#if defined(PIN_PB07) && !defined(IGNORE_PIN_PB07) PIN(PB07, EXTINT_CHANNEL(7), ADC_INPUT(15), TOUCH(13), NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PB08 +#if defined(PIN_PB08) && !defined(IGNORE_PIN_PB08) PIN(PB08, EXTINT_CHANNEL(8), ADC_INPUT(2), TOUCH(14), NO_SERCOM, SERCOM(4, 0), TC(4, 0), NO_TIMER); #endif -#ifdef PIN_PB09 +#if defined(PIN_PB09) && !defined(IGNORE_PIN_PB09) PIN(PB09, EXTINT_CHANNEL(9), ADC_INPUT(3), TOUCH(15), NO_SERCOM, SERCOM(4, 1), TC(4, 1), NO_TIMER); #endif -#ifdef PIN_PA04 +#if defined(PIN_PA04) && !defined(IGNORE_PIN_PA04) PIN(PA04, EXTINT_CHANNEL(4), ADC_INPUT(4), TOUCH(2), NO_SERCOM, SERCOM(0, 0), TCC(0, 0), NO_TIMER); #endif -#ifdef PIN_PA05 +#if defined(PIN_PA05) && !defined(IGNORE_PIN_PA05) PIN(PA05, EXTINT_CHANNEL(5), ADC_INPUT(5), TOUCH(3), NO_SERCOM, SERCOM(0, 1), TCC(0, 1), NO_TIMER); #endif -#ifdef PIN_PA06 +#if defined(PIN_PA06) && !defined(IGNORE_PIN_PA06) PIN(PA06, EXTINT_CHANNEL(6), ADC_INPUT(6), TOUCH(4), NO_SERCOM, SERCOM(0, 2), TCC(1, 0), NO_TIMER); #endif -#ifdef PIN_PA07 +#if defined(PIN_PA07) && !defined(IGNORE_PIN_PA07) PIN(PA07, EXTINT_CHANNEL(7), ADC_INPUT(7), TOUCH(5), NO_SERCOM, SERCOM(0, 3), TCC(1, 1), NO_TIMER); #endif -#ifdef PIN_PA08 +#if defined(PIN_PA08) && !defined(IGNORE_PIN_PA08) PIN(PA08, NO_EXTINT, ADC_INPUT(16), NO_TOUCH, SERCOM(0, 0), SERCOM(2, 0), TCC(0, 0), TCC(1, 2)); #endif -#ifdef PIN_PA09 +#if defined(PIN_PA09) && !defined(IGNORE_PIN_PA09) PIN(PA09, EXTINT_CHANNEL(9), ADC_INPUT(17), NO_TOUCH, SERCOM(0, 1), SERCOM(2, 1), TCC(0, 1), TCC(1, 3)); #endif -#ifdef PIN_PA10 +#if defined(PIN_PA10) && !defined(IGNORE_PIN_PA10) PIN(PA10, EXTINT_CHANNEL(10), ADC_INPUT(18), NO_TOUCH, SERCOM(0, 2), SERCOM(2, 2), TCC(1, 0), TCC(0, 2)); #endif -#ifdef PIN_PA11 +#if defined(PIN_PA11) && !defined(IGNORE_PIN_PA11) PIN(PA11, EXTINT_CHANNEL(11), ADC_INPUT(19), NO_TOUCH, SERCOM(0, 3), SERCOM(2, 3), TCC(1, 1), TCC(0, 3)); #endif -#ifdef PIN_PB10 +#if defined(PIN_PB10) && !defined(IGNORE_PIN_PB10) PIN(PB10, EXTINT_CHANNEL(10), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(4, 2), TC(5, 0), TCC(0, 4)); #endif -#ifdef PIN_PB11 +#if defined(PIN_PB11) && !defined(IGNORE_PIN_PB11) PIN(PB11, EXTINT_CHANNEL(11), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(4, 3), TC(5, 1), TCC(0, 5)); #endif -#ifdef PIN_PB12 +#if defined(PIN_PB12) && !defined(IGNORE_PIN_PB12) PIN(PB12, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, SERCOM(4, 0), NO_SERCOM, TC(4, 0), TCC(0, 6)); #endif -#ifdef PIN_PB13 +#if defined(PIN_PB13) && !defined(IGNORE_PIN_PB13) PIN(PB13, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, SERCOM(4, 1), NO_SERCOM, TC(4, 1), TCC(0, 7)); #endif -#ifdef PIN_PB14 +#if defined(PIN_PB14) && !defined(IGNORE_PIN_PB14) PIN(PB14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, SERCOM(4, 2), NO_SERCOM, @@ -262,28 +262,28 @@ PIN(PB14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, #endif // Second page. -#ifdef PIN_PB15 +#if defined(PIN_PB15) && !defined(IGNORE_PIN_PB15) PIN(PB15, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, SERCOM(4, 3), NO_SERCOM, TC(5, 1), NO_TIMER); #endif -#ifdef PIN_PA12 +#if defined(PIN_PA12) && !defined(IGNORE_PIN_PA12) PIN(PA12, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, SERCOM(2, 0), SERCOM(4, 0), TCC(2, 0), TCC(0, 6)); #endif -#ifdef PIN_PA13 +#if defined(PIN_PA13) && !defined(IGNORE_PIN_PA13) PIN(PA13, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, SERCOM(2, 1), SERCOM(4, 1), TCC(2, 1), TCC(0, 7)); #endif -#ifdef PIN_PA14 +#if defined(PIN_PA14) && !defined(IGNORE_PIN_PA14) PIN(PA14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, SERCOM(2, 2), #ifdef SERCOM4 @@ -294,7 +294,7 @@ PIN(PA14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, TC(3, 0), TCC(0, 4)); #endif -#ifdef PIN_PA15 +#if defined(PIN_PA15) && !defined(IGNORE_PIN_PA15) PIN(PA15, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, SERCOM(2, 3), #ifdef SERCOM4 @@ -305,35 +305,35 @@ PIN(PA15, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, TC(3, 1), TCC(0, 5)); #endif -#ifdef PIN_PA16 +#if defined(PIN_PA16) && !defined(IGNORE_PIN_PA16) PIN(PA16, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, SERCOM(1, 0), SERCOM(3, 0), TCC(2, 0), TCC(0, 6)); #endif -#ifdef PIN_PA17 +#if defined(PIN_PA17) && !defined(IGNORE_PIN_PA17) PIN(PA17, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, SERCOM(1, 1), SERCOM(3, 1), TCC(2, 1), TCC(0, 7)); #endif -#ifdef PIN_PA18 +#if defined(PIN_PA18) && !defined(IGNORE_PIN_PA18) PIN(PA18, EXTINT_CHANNEL(2), NO_ADC, NO_TOUCH, SERCOM(1, 2), SERCOM(3, 2), TC(3, 0), TCC(0, 2)); #endif -#ifdef PIN_PA19 +#if defined(PIN_PA19) && !defined(IGNORE_PIN_PA19) PIN(PA19, EXTINT_CHANNEL(3), NO_ADC, NO_TOUCH, SERCOM(1, 3), SERCOM(3, 3), TC(3, 1), TCC(0, 3)); #endif -#ifdef PIN_PB16 +#if defined(PIN_PB16) && !defined(IGNORE_PIN_PB16) PIN(PB16, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, SERCOM(5, 0), NO_SERCOM, @@ -344,7 +344,7 @@ PIN(PB16, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, #endif TCC(0, 4)); #endif -#ifdef PIN_PB17 +#if defined(PIN_PB17) && !defined(IGNORE_PIN_PB17) PIN(PB17, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, SERCOM(5, 1), NO_SERCOM, @@ -355,7 +355,7 @@ PIN(PB17, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, #endif TCC(0, 5)); #endif -#ifdef PIN_PA20 +#if defined(PIN_PA20) && !defined(IGNORE_PIN_PA20) PIN(PA20, EXTINT_CHANNEL(4), NO_ADC, NO_TOUCH, SERCOM(5, 2), SERCOM(3, 2), @@ -366,7 +366,7 @@ PIN(PA20, EXTINT_CHANNEL(4), NO_ADC, NO_TOUCH, #endif TCC(0, 6)); #endif -#ifdef PIN_PA21 +#if defined(PIN_PA21) && !defined(IGNORE_PIN_PA21) PIN(PA21, EXTINT_CHANNEL(5), NO_ADC, NO_TOUCH, SERCOM(5, 3), SERCOM(3, 3), @@ -377,7 +377,7 @@ PIN(PA21, EXTINT_CHANNEL(5), NO_ADC, NO_TOUCH, #endif TCC(0, 7)); #endif -#ifdef PIN_PA22 +#if defined(PIN_PA22) && !defined(IGNORE_PIN_PA22) PIN(PA22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, SERCOM(3, 0), #ifdef SERCOM5 @@ -388,7 +388,7 @@ PIN(PA22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, TC(4, 0), TCC(0, 4)); #endif -#ifdef PIN_PA23 +#if defined(PIN_PA23) && !defined(IGNORE_PIN_PA23) PIN(PA23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, SERCOM(3, 1), #ifdef SERCOM5 @@ -399,7 +399,7 @@ PIN(PA23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, TC(4, 1), TCC(0, 5)); #endif -#ifdef PIN_PA24 +#if defined(PIN_PA24) && !defined(IGNORE_PIN_PA24) PIN(PA24, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, SERCOM(3, 2), #ifdef SERCOM5 @@ -410,7 +410,7 @@ PIN(PA24, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, TC(5, 0), TCC(0, 2)); #endif -#ifdef PIN_PA25 +#if defined(PIN_PA25) && !defined(IGNORE_PIN_PA25) PIN(PA25, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, SERCOM(3, 3), #ifdef SERCOM5 @@ -421,7 +421,7 @@ PIN(PA25, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, TC(5, 1), TCC(1, 3)); #endif -#ifdef PIN_PB22 +#if defined(PIN_PB22) && !defined(IGNORE_PIN_PB22) PIN(PB22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(5, 2), @@ -432,7 +432,7 @@ PIN(PB22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, #endif NO_TIMER); #endif -#ifdef PIN_PB23 +#if defined(PIN_PB23) && !defined(IGNORE_PIN_PB23) PIN(PB23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(5, 3), @@ -443,49 +443,49 @@ PIN(PB23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, #endif NO_TIMER); #endif -#ifdef PIN_PA27 +#if defined(PIN_PA27) && !defined(IGNORE_PIN_PA27) PIN(PA27, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PA28 +#if defined(PIN_PA28) && !defined(IGNORE_PIN_PA28) PIN(PA28, EXTINT_CHANNEL(8), NO_ADC, NO_TOUCH, NO_SERCOM, NO_SERCOM, NO_TIMER, NO_TIMER); #endif -#ifdef PIN_PA30 +#if defined(PIN_PA30) && !defined(IGNORE_PIN_PA30) PIN(PA30, EXTINT_CHANNEL(10), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(1, 2), TCC(1, 0), NO_TIMER); #endif -#ifdef PIN_PA31 +#if defined(PIN_PA31) && !defined(IGNORE_PIN_PA31) PIN(PA31, EXTINT_CHANNEL(11), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(1, 3), TCC(1, 1), NO_TIMER); #endif -#ifdef PIN_PB30 +#if defined(PIN_PB30) && !defined(IGNORE_PIN_PB30) PIN(PB30, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(5, 0), TCC(0, 0), TCC(1, 2)); #endif -#ifdef PIN_PB31 +#if defined(PIN_PB31) && !defined(IGNORE_PIN_PB31) PIN(PB31, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, NO_SERCOM, SERCOM(5, 1), TCC(0, 1), TCC(1, 3)); #endif -#ifdef PIN_PB00 +#if defined(PIN_PB00) && !defined(IGNORE_PIN_PB00) PIN(PB00, EXTINT_CHANNEL(0), ADC_INPUT(8), TOUCH(6), NO_SERCOM, SERCOM(5, 2), @@ -496,7 +496,7 @@ PIN(PB00, EXTINT_CHANNEL(0), ADC_INPUT(8), TOUCH(6), #endif NO_TIMER); #endif -#ifdef PIN_PB01 +#if defined(PIN_PB01) && !defined(IGNORE_PIN_PB01) PIN(PB01, EXTINT_CHANNEL(1), ADC_INPUT(9), TOUCH(7), NO_SERCOM, SERCOM(5, 3)), @@ -507,7 +507,7 @@ PIN(PB01, EXTINT_CHANNEL(1), ADC_INPUT(9), TOUCH(7), #endif NO_TIMER; #endif -#ifdef PIN_PB02 +#if defined(PIN_PB02) && !defined(IGNORE_PIN_PB02) PIN(PB02, EXTINT_CHANNEL(2), ADC_INPUT(10), TOUCH(8), NO_SERCOM, SERCOM(5, 0), @@ -518,7 +518,7 @@ PIN(PB02, EXTINT_CHANNEL(2), ADC_INPUT(10), TOUCH(8), #endif NO_TIMER); #endif -#ifdef PIN_PB03 +#if defined(PIN_PB03) && !defined(IGNORE_PIN_PB03) PIN(PB03, EXTINT_CHANNEL(3), ADC_INPUT(11), TOUCH(9), NO_SERCOM, SERCOM(5, 1), From b0bacd9b0b0cbe8a633ab00f8174c509a2b1d243 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 13:15:24 -0400 Subject: [PATCH 012/103] review code --- ports/atmel-samd/board_busses.c | 48 +++++++++++++++++++ ports/atmel-samd/board_busses.h | 33 +++++++++++++ .../boards/circuitplayground_express/pins.c | 1 + .../boards/gemma_m0/mpconfigboard.h | 3 ++ ports/atmel-samd/boards/gemma_m0/pins.c | 11 +++++ 5 files changed, 96 insertions(+) create mode 100644 ports/atmel-samd/board_busses.c create mode 100644 ports/atmel-samd/board_busses.h diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c new file mode 100644 index 0000000000..5aa33b924f --- /dev/null +++ b/ports/atmel-samd/board_busses.c @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "shared-bindings/busio/I2C.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "mpconfigboard.h" + +STATIC mp_obj_t i2c_singleton = NULL; + +STATIC mp_obj_t board_i2c(void) { + #if !defined(DEFAULT_I2C_BUS_SCA) || !defined(DEFAULT_I2C_BUS_SCL) + mp_raise_NotImplementedError('no default bus'); + #endif + if (i2c_singleton == NULL) { + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); + self->base.type = &busio_i2c_type; + + assert_pin_free(DEFAULT_I2C_BUS_SCA); + assert_pin_free(DEFAULT_I2C_BUS_SCL); + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCA, DEFAULT_I2C_BUS_SCL, 400000); + i2c_singleton = (mp_obj_t)self; + } + return i2c_singleton; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); diff --git a/ports/atmel-samd/board_busses.h b/ports/atmel-samd/board_busses.h new file mode 100644 index 0000000000..fdddd452bf --- /dev/null +++ b/ports/atmel-samd/board_busses.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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_ATMEL_SAMD_BOARD_BUSSES_H +#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H + +void board_i2c(void); +mp_obj_t board_i2c_obj; + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H diff --git a/ports/atmel-samd/boards/circuitplayground_express/pins.c b/ports/atmel-samd/boards/circuitplayground_express/pins.c index aecd7f416a..9541262665 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express/pins.c @@ -56,5 +56,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA05) }, { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA07) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index 6457d481f3..14f6e0814c 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -12,6 +12,9 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 +#define DEFAULT_I2C_BUS_SCL (&pin_PA04) +#define DEFAULT_I2C_BUS_SCA (&pin_PA05) + #include "internal_flash.h" #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index 5d3bedca0e..f17c8c7b23 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PA05) }, // pad 1 @@ -19,5 +20,15 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); +// https://learn.adafruit.com/building-circuitpython/build-circuitpython +// make BOARD=gemma_m0 +// screen /dev/tty.usbmodem1411 +// enger +// control AK to kill screen +// import board +// board.I2C() +// two long clicks +// /ports/amtel-sand/build-gemma_m0/firmware.uf2 \ No newline at end of file From a10f04ad6ec516ff86a40159f9b09f22a21b653f Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 13:53:53 -0400 Subject: [PATCH 013/103] support for 2 --- ports/atmel-samd/Makefile | 1 + ports/atmel-samd/board_busses.c | 9 +++++---- ports/atmel-samd/board_busses.h | 2 +- .../boards/circuitplayground_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/circuitplayground_express/pins.c | 1 + ports/atmel-samd/boards/gemma_m0/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/gemma_m0/pins.c | 9 --------- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index e85a11b570..eb25c1aa30 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -227,6 +227,7 @@ SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF)) SRC_C = \ audio_dma.c \ + board_busses.c \ background.c \ clocks.c \ $(CHIP_FAMILY)_clocks.c \ diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 5aa33b924f..4f5b03d036 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -27,22 +27,23 @@ #include "shared-bindings/busio/I2C.h" #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" +#include "pins.h" STATIC mp_obj_t i2c_singleton = NULL; STATIC mp_obj_t board_i2c(void) { - #if !defined(DEFAULT_I2C_BUS_SCA) || !defined(DEFAULT_I2C_BUS_SCL) + #if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) mp_raise_NotImplementedError('no default bus'); #endif if (i2c_singleton == NULL) { busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); self->base.type = &busio_i2c_type; - assert_pin_free(DEFAULT_I2C_BUS_SCA); + assert_pin_free(DEFAULT_I2C_BUS_SDA); assert_pin_free(DEFAULT_I2C_BUS_SCL); - common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCA, DEFAULT_I2C_BUS_SCL, 400000); + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000); i2c_singleton = (mp_obj_t)self; } return i2c_singleton; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); +MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); diff --git a/ports/atmel-samd/board_busses.h b/ports/atmel-samd/board_busses.h index fdddd452bf..e872e4902b 100644 --- a/ports/atmel-samd/board_busses.h +++ b/ports/atmel-samd/board_busses.h @@ -28,6 +28,6 @@ #define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H void board_i2c(void); -mp_obj_t board_i2c_obj; +extern mp_obj_fun_builtin_fixed_t board_i2c_obj; #endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 73d45f2cf8..c307d50689 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -58,3 +58,6 @@ // Explanation of how a user got into safe mode. #define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up" + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) \ No newline at end of file diff --git a/ports/atmel-samd/boards/circuitplayground_express/pins.c b/ports/atmel-samd/boards/circuitplayground_express/pins.c index 9541262665..c0545fd7c0 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index 14f6e0814c..f98119e11d 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -12,8 +12,8 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 -#define DEFAULT_I2C_BUS_SCL (&pin_PA04) -#define DEFAULT_I2C_BUS_SCA (&pin_PA05) +#define DEFAULT_I2C_BUS_SCL (&pin_PA05) +#define DEFAULT_I2C_BUS_SDA (&pin_PA04) #include "internal_flash.h" diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index f17c8c7b23..6d9d2ad759 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -23,12 +23,3 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); -// https://learn.adafruit.com/building-circuitpython/build-circuitpython -// make BOARD=gemma_m0 -// screen /dev/tty.usbmodem1411 -// enger -// control AK to kill screen -// import board -// board.I2C() -// two long clicks -// /ports/amtel-sand/build-gemma_m0/firmware.uf2 \ No newline at end of file From 7d443c87b39903f08e5f888117ee2f406acd6b01 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Tue, 15 May 2018 14:03:39 -0400 Subject: [PATCH 014/103] Add gemma_m0 pin exclusions --- .../boards/gemma_m0/mpconfigboard.h | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index 6457d481f3..45a6a694e9 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -15,3 +15,48 @@ #include "internal_flash.h" #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define IGNORE_PIN_PA03 1 +#define IGNORE_PIN_PA06 1 +#define IGNORE_PIN_PA07 1 +#define IGNORE_PIN_PA08 1 +#define IGNORE_PIN_PA09 1 +#define IGNORE_PIN_PA10 1 +#define IGNORE_PIN_PA11 1 +#define IGNORE_PIN_PA12 1 +#define IGNORE_PIN_PA13 1 +#define IGNORE_PIN_PA14 1 +#define IGNORE_PIN_PA15 1 +#define IGNORE_PIN_PA16 1 +#define IGNORE_PIN_PA17 1 +#define IGNORE_PIN_PA18 1 +#define IGNORE_PIN_PA19 1 +#define IGNORE_PIN_PA20 1 +#define IGNORE_PIN_PA21 1 +#define IGNORE_PIN_PA22 1 +#define IGNORE_PIN_PA27 1 +#define IGNORE_PIN_PA28 1 +#define IGNORE_PIN_PA30 1 +#define IGNORE_PIN_PA31 1 +#define IGNORE_PIN_PB01 1 +#define IGNORE_PIN_PB02 1 +#define IGNORE_PIN_PB03 1 +#define IGNORE_PIN_PB04 1 +#define IGNORE_PIN_PB05 1 +#define IGNORE_PIN_PB06 1 +#define IGNORE_PIN_PB07 1 +#define IGNORE_PIN_PB08 1 +#define IGNORE_PIN_PB09 1 +#define IGNORE_PIN_PB10 1 +#define IGNORE_PIN_PB11 1 +#define IGNORE_PIN_PB12 1 +#define IGNORE_PIN_PB13 1 +#define IGNORE_PIN_PB14 1 +#define IGNORE_PIN_PB15 1 +#define IGNORE_PIN_PB16 1 +#define IGNORE_PIN_PB17 1 +#define IGNORE_PIN_PB22 1 +#define IGNORE_PIN_PB23 1 +#define IGNORE_PIN_PB30 1 +#define IGNORE_PIN_PB31 1 +#define IGNORE_PIN_PB00 1 From bd7f603748cac9946e384c676921b40baa36a10d Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 14:06:01 -0400 Subject: [PATCH 015/103] zero + feather logger --- ports/atmel-samd/boards/arduino_zero/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/arduino_zero/pins.c | 4 ++++ .../boards/circuitplayground_express/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_adalogger/pins.c | 4 ++++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h index 6eeddb36c7..12f7dbe40b 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h @@ -14,3 +14,6 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index 8b28c0fc22..dc873d9e67 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -1,6 +1,8 @@ #include "shared-bindings/board/__init__.h" #include "samd21_pins.h" +#include "board_busses.h" + STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -30,5 +32,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PB11) }, { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index c307d50689..6ea0d93602 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -60,4 +60,4 @@ #define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up" #define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index 8802d39d99..6e23ebc61b 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -12,3 +12,6 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c index a229b6bab1..fdcb691fcf 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c +++ b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c @@ -1,4 +1,6 @@ #include "samd21_pins.h" +#include "board_busses.h" + STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -30,5 +32,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_RED_LED), MP_ROM_PTR(&pin_PA17) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From cd1f6627e411d71eec8d94f32c7530d415a0cab7 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 14:09:00 -0400 Subject: [PATCH 016/103] formatting --- ports/atmel-samd/boards/arduino_zero/mpconfigboard.h | 2 +- ports/atmel-samd/boards/arduino_zero/pins.c | 1 - ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h index 12f7dbe40b..11e01b6c60 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h @@ -16,4 +16,4 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) #define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index dc873d9e67..74cff5ae86 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -33,6 +33,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, - }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index 6e23ebc61b..38df41b94c 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -14,4 +14,4 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) #define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) From cdeb7ddff8b3f82e3cf303c04682d041fe51db31 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 14:27:05 -0400 Subject: [PATCH 017/103] fix non- builds --- ports/atmel-samd/board_busses.c | 41 ++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 4f5b03d036..5c539e3f5c 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -28,22 +28,35 @@ #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" #include "pins.h" +#include "py/runtime.h" -STATIC mp_obj_t i2c_singleton = NULL; -STATIC mp_obj_t board_i2c(void) { - #if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) - mp_raise_NotImplementedError('no default bus'); - #endif - if (i2c_singleton == NULL) { - busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); - self->base.type = &busio_i2c_type; - assert_pin_free(DEFAULT_I2C_BUS_SDA); - assert_pin_free(DEFAULT_I2C_BUS_SCL); - common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000); - i2c_singleton = (mp_obj_t)self; +#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) + + STATIC mp_obj_t board_i2c(void) { + //board_i2c_obj = NULL; + mp_raise_NotImplementedError("no default bus"); + return NULL; } - return i2c_singleton; -} + +#else + STATIC mp_obj_t i2c_singleton = NULL; + + STATIC mp_obj_t board_i2c(void) { + + if (i2c_singleton == NULL) { + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); + self->base.type = &busio_i2c_type; + + assert_pin_free(DEFAULT_I2C_BUS_SDA); + assert_pin_free(DEFAULT_I2C_BUS_SCL); + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000); + i2c_singleton = (mp_obj_t)self; + } + return i2c_singleton; + + } +#endif + MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); From 9f2026158b8a2d99ac41d1ee6a894db40d508718 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Tue, 15 May 2018 14:54:12 -0400 Subject: [PATCH 018/103] add ugly workaround for apt failing - retry --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1e18640c81..4749416e0f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,11 @@ notifications: before_script: - sudo dpkg --add-architecture i386 - - sudo apt-get install -y python3 gcc-multilib pkg-config libffi-dev libffi-dev:i386 qemu-system + - | + sudo apt-get install -y python3 gcc-multilib pkg-config libffi-dev libffi-dev:i386 qemu-system || + sleep 30 || + sudo apt-get install -y python3 gcc-multilib pkg-config libffi-dev libffi-dev:i386 qemu-system + - ([[ -z "$TRAVIS_TEST" ]] || sudo apt-get install -y qemu-system) - ([[ -z "$TRAVIS_BOARD" ]] || (wget https://s3.amazonaws.com/adafruit-circuit-python/gcc-arm-embedded_7-2017q4-1~trusty3_amd64.deb && sudo dpkg -i gcc-arm-embedded*_amd64.deb)) - ([[ $TRAVIS_TEST != "qemu" ]] || (wget https://s3.amazonaws.com/adafruit-circuit-python/gcc-arm-embedded_7-2017q4-1~trusty3_amd64.deb && sudo dpkg -i gcc-arm-embedded*_amd64.deb)) From 4e65752d6e88f5d81ef9180dfab90760ff32bd63 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 15:28:23 -0400 Subject: [PATCH 019/103] feather m0 boards --- ports/atmel-samd/boards/feather_m0_adalogger/pins.c | 2 -- ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_basic/pins.c | 2 ++ ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_express/pins.c | 2 ++ ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 5 +++++ ports/atmel-samd/boards/feather_m0_rfm69/pins.c | 2 ++ ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h | 6 ++++++ ports/atmel-samd/boards/feather_m0_rfm9x/pins.c | 2 ++ .../atmel-samd/boards/feather_m0_supersized/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_supersized/pins.c | 2 ++ 11 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c index fdcb691fcf..cf4160d723 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c +++ b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c @@ -1,7 +1,6 @@ #include "samd21_pins.h" #include "board_busses.h" - STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PB08) }, @@ -33,6 +32,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_RED_LED), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, - }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index 04cbc4ff8a..d949244972 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -13,3 +13,6 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/feather_m0_basic/pins.c b/ports/atmel-samd/boards/feather_m0_basic/pins.c index c9119e77bc..2916df7a1c 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/pins.c +++ b/ports/atmel-samd/boards/feather_m0_basic/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -23,5 +24,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_PA16) }, { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index c85f553ba9..c34869a572 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -48,3 +48,6 @@ #include "external_flash/external_flash.h" #define BOARD_HAS_CRYSTAL 1 + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/feather_m0_express/pins.c b/ports/atmel-samd/boards/feather_m0_express/pins.c index c5c70ee5dc..740be2cbd6 100644 --- a/ports/atmel-samd/boards/feather_m0_express/pins.c +++ b/ports/atmel-samd/boards/feather_m0_express/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -24,5 +25,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index 33445940e6..ff5512d814 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -13,3 +13,8 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +// https://learn.adafruit.com/assets/46254 +// https://learn.adafruit.com/assets/46255 +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c index eb5f531e36..6a35c33d49 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -26,5 +27,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM69_D0), MP_ROM_PTR(&pin_PA09) }, { MP_ROM_QSTR(MP_QSTR_RFM69_RST), MP_ROM_PTR(&pin_PA08) }, { MP_ROM_QSTR(MP_QSTR_RFM69_CS), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h index a2563cc9f1..cac39ee846 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h @@ -13,3 +13,9 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +// https://learn.adafruit.com/assets/46254 +// https://learn.adafruit.com/assets/46255 +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) + diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c index 2e5d0a8e68..bf4b3b347d 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -26,5 +27,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM9X_D0), MP_ROM_PTR(&pin_PA09) }, { MP_ROM_QSTR(MP_QSTR_RFM9X_RST), MP_ROM_PTR(&pin_PA08) }, { MP_ROM_QSTR(MP_QSTR_RFM9X_CS), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index 5bc74d5a74..7525c01624 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -47,3 +47,6 @@ #define EXTERNAL_FLASH_DEVICES S25FL064L #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/feather_m0_supersized/pins.c b/ports/atmel-samd/boards/feather_m0_supersized/pins.c index c5c70ee5dc..740be2cbd6 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/pins.c +++ b/ports/atmel-samd/boards/feather_m0_supersized/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -24,5 +25,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From ff6e6b51557ff1d6d69e279f39ffba6dd113e300 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 15:33:27 -0400 Subject: [PATCH 020/103] best effort --- ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m0_express/pins.c | 2 ++ ports/atmel-samd/boards/trinket_m0/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/trinket_m0/pins.c | 2 ++ ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/trinket_m0_haxpress/pins.c | 2 ++ ports/atmel-samd/boards/ugame10/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/ugame10/pins.c | 2 ++ 8 files changed, 20 insertions(+) diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index e635acd593..9b8b0ab88c 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -49,3 +49,6 @@ #include "external_flash/external_flash.h" #define BOARD_HAS_CRYSTAL 1 + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/metro_m0_express/pins.c b/ports/atmel-samd/boards/metro_m0_express/pins.c index 58510faabb..32f3f875c4 100644 --- a/ports/atmel-samd/boards/metro_m0_express/pins.c +++ b/ports/atmel-samd/boards/metro_m0_express/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -29,5 +30,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PB11) }, { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h index 05033ad38b..a0c4db3d6c 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h @@ -14,3 +14,6 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define DEFAULT_I2C_BUS_SCL (&pin_PA09) +#define DEFAULT_I2C_BUS_SDA (&pin_PA08) diff --git a/ports/atmel-samd/boards/trinket_m0/pins.c b/ports/atmel-samd/boards/trinket_m0/pins.c index ac58986009..74d03def29 100644 --- a/ports/atmel-samd/boards/trinket_m0/pins.c +++ b/ports/atmel-samd/boards/trinket_m0/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA08) }, @@ -27,5 +28,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index ea45c1f8b8..3b1eab4b20 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -54,3 +54,6 @@ #define EXTERNAL_FLASH_DEVICES W25Q32BV #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c index ac58986009..74d03def29 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA08) }, @@ -27,5 +28,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.h b/ports/atmel-samd/boards/ugame10/mpconfigboard.h index 1652314ed8..57caefb882 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.h +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.h @@ -47,3 +47,6 @@ #define EXTERNAL_FLASH_DEVICES S25FL216K #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index af516a401d..2af55f6da7 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA00) }, @@ -23,5 +24,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_B), MP_ROM_PTR(&pin_PA14) }, { MP_ROM_QSTR(MP_QSTR_C), MP_ROM_PTR(&pin_PA15) }, { MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From fbb57f902d83a8be83b25155adb9d4190eff050e Mon Sep 17 00:00:00 2001 From: Matt Land Date: Tue, 15 May 2018 15:37:48 -0400 Subject: [PATCH 021/103] better error message --- ports/atmel-samd/board_busses.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 5c539e3f5c..fa38972064 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -36,7 +36,7 @@ STATIC mp_obj_t board_i2c(void) { //board_i2c_obj = NULL; - mp_raise_NotImplementedError("no default bus"); + mp_raise_NotImplementedError("No default I2C bus"); return NULL; } From 9a2cda7fd3917c3e4f57b4a8510bc7690945774a Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 16 May 2018 13:19:51 -0500 Subject: [PATCH 022/103] Reformat the error message --- shared-module/bitbangio/I2C.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index 7bf8292db2..47529748a5 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -53,9 +53,9 @@ STATIC void scl_release(bitbangio_i2c_obj_t *self) { for (; !common_hal_digitalio_digitalinout_get_value(&self->scl) && count; --count) { common_hal_mcu_delay_us(1); } - if(count==0) { /// raise exception on timeout - mp_raise_msg(&mp_type_TimeoutError, - "Clock Stretching Timeout."); + // raise exception on timeout + if (count == 0) { + mp_raise_msg(&mp_type_TimeoutError, "Clock stretch too long"); } } From 8d1d821876b77a3f7213f3bf5468f38af2a4fc9f Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 14:58:57 -0400 Subject: [PATCH 023/103] pin issue --- ports/atmel-samd/board_busses.c | 28 +++++++++++++++++++ ports/atmel-samd/board_busses.h | 3 ++ .../circuitplayground_express/mpconfigboard.h | 4 +++ .../boards/circuitplayground_express/pins.c | 1 + 4 files changed, 36 insertions(+) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index fa38972064..8f007aa754 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -25,6 +25,7 @@ */ #include "shared-bindings/busio/I2C.h" +#include "shared-bindings/busio/SPI.h" #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" #include "pins.h" @@ -60,3 +61,30 @@ #endif MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); + +#if !defined(DEFAULT_SPI_BUS_CLK) || !defined(DEFAULT_SPI_BUS_MISO) || !defined(DEFAULT_SPI_BUS_MOSI) + STATIC mp_obj_t board_spi(void) { + mp_raise_NotImplementedError("No default SPI bus"); + return NULL; + } +#else + STATIC mp_obj_t spi_singleton = NULL; + + STATIC mp_obj_t board_spi(void) { + + if (spi_singleton == NULL) { + busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t); + self->base.type = &busio_spi_type; + assert_pin_free(DEFAULT_SPI_BUS_CLK); + assert_pin_free(DEFAULT_SPI_BUS_MOSI); + assert_pin_free(DEFAULT_SPI_BUS_MISO); + const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_CLK); + const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MOSI); + const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MISO); + common_hal_busio_spi_construct(self, clock, mosi, miso); + spi_singleton = (mp_obj_t)self; + } + return spi_singleton; + } +#endif +MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); diff --git a/ports/atmel-samd/board_busses.h b/ports/atmel-samd/board_busses.h index e872e4902b..d0cd5dbf78 100644 --- a/ports/atmel-samd/board_busses.h +++ b/ports/atmel-samd/board_busses.h @@ -30,4 +30,7 @@ void board_i2c(void); extern mp_obj_fun_builtin_fixed_t board_i2c_obj; +void board_spi(void); +extern mp_obj_fun_builtin_fixed_t board_spi_obj; + #endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 6ea0d93602..34b0a01fed 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -61,3 +61,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_CLK (&pin_PA21) +#define DEFAULT_SPI_BUS_MISO (&pin_PA16) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA20) diff --git a/ports/atmel-samd/boards/circuitplayground_express/pins.c b/ports/atmel-samd/boards/circuitplayground_express/pins.c index c0545fd7c0..4f32ecafab 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express/pins.c @@ -58,5 +58,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA07) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 10888be8e8a756f4c505c534d5a952c688fcebf3 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 15:07:12 -0400 Subject: [PATCH 024/103] fixed pins --- .../boards/circuitplayground_express/mpconfigboard.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 34b0a01fed..442b101a82 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -62,6 +62,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PA21) -#define DEFAULT_SPI_BUS_MISO (&pin_PA16) -#define DEFAULT_SPI_BUS_MOSI (&pin_PA20) +#define DEFAULT_SPI_BUS_CLK (&pin_PA05) +#define DEFAULT_SPI_BUS_MISO (&pin_PA06) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA07) From 244866c2c32eb8de7beefacf731eafe7726044d0 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 15:25:24 -0400 Subject: [PATCH 025/103] spi on much --- ports/atmel-samd/boards/arduino_zero/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/arduino_zero/pins.c | 2 ++ .../boards/circuitplayground_express/mpconfigboard.h | 2 +- .../atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/feather_m0_adalogger/pins.c | 1 + ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/feather_m0_basic/pins.c | 2 ++ ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/feather_m0_express/pins.c | 2 ++ ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 6 +++++- ports/atmel-samd/boards/feather_m0_rfm69/pins.c | 1 + ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_rfm9x/pins.c | 1 + .../atmel-samd/boards/feather_m0_supersized/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/feather_m0_supersized/pins.c | 1 + ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/feather_m4_express/pins.c | 1 + .../atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/itsybitsy_m0_express/pins.c | 2 ++ .../atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/itsybitsy_m4_express/pins.c | 2 ++ 21 files changed, 56 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h index 11e01b6c60..761a9689bc 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h @@ -17,3 +17,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index 74cff5ae86..7869b24ccf 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -33,5 +33,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 442b101a82..bc7a6ed5df 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -63,5 +63,5 @@ #define DEFAULT_I2C_BUS_SDA (&pin_PB02) #define DEFAULT_SPI_BUS_CLK (&pin_PA05) -#define DEFAULT_SPI_BUS_MISO (&pin_PA06) #define DEFAULT_SPI_BUS_MOSI (&pin_PA07) +#define DEFAULT_SPI_BUS_MISO (&pin_PA06) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index 38df41b94c..4ba9511325 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -15,3 +15,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c index cf4160d723..1ae2d958a4 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c +++ b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c @@ -32,5 +32,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_RED_LED), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index d949244972..3a9231a2d9 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -16,3 +16,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_basic/pins.c b/ports/atmel-samd/boards/feather_m0_basic/pins.c index 2916df7a1c..9e0b4f7d67 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/pins.c +++ b/ports/atmel-samd/boards/feather_m0_basic/pins.c @@ -25,5 +25,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index c34869a572..752308739d 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -51,3 +51,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/feather_m0_express/pins.c b/ports/atmel-samd/boards/feather_m0_express/pins.c index 740be2cbd6..f3a3e0acda 100644 --- a/ports/atmel-samd/boards/feather_m0_express/pins.c +++ b/ports/atmel-samd/boards/feather_m0_express/pins.c @@ -26,5 +26,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index ff5512d814..c8b461ac53 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -17,4 +17,8 @@ // https://learn.adafruit.com/assets/46254 // https://learn.adafruit.com/assets/46255 #define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c index 6a35c33d49..1c6051a5c4 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c @@ -28,5 +28,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM69_RST), MP_ROM_PTR(&pin_PA08) }, { MP_ROM_QSTR(MP_QSTR_RFM69_CS), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h index cac39ee846..71f95af1d4 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h @@ -19,3 +19,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c index bf4b3b347d..addb700e42 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c @@ -28,5 +28,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM9X_RST), MP_ROM_PTR(&pin_PA08) }, { MP_ROM_QSTR(MP_QSTR_RFM9X_CS), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index 7525c01624..6345ddfde6 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -50,3 +50,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_supersized/pins.c b/ports/atmel-samd/boards/feather_m0_supersized/pins.c index 740be2cbd6..395a84d551 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/pins.c +++ b/ports/atmel-samd/boards/feather_m0_supersized/pins.c @@ -26,5 +26,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 6e10222562..962558200e 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -31,3 +31,7 @@ #define EXTERNAL_FLASH_QSPI_DUAL #include "external_flash/external_flash.h" + +#define DEFAULT_SPI_BUS_CLK (&pin_PA17) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA19) +#define DEFAULT_SPI_BUS_MISO (&pin_PA18) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index c161e736ed..8548cd9ac8 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -26,5 +26,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA22) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA23) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB23) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index a7ecd27ea8..4fbf3189f5 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -46,3 +46,7 @@ GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c index b70bb6a76c..54ea381edf 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c @@ -38,5 +38,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA00) }, + + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h index 95c542a43e..dcdd3f43ef 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h @@ -31,3 +31,7 @@ #define EXTERNAL_FLASH_DEVICES GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_SPI_BUS_CLK (&pin_PA01) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA00) +#define DEFAULT_SPI_BUS_MISO (&pin_PB23) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c index 11aeda9c67..54b06452a8 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c @@ -35,5 +35,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PB03) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PB02) }, + + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 3144654854a357b82858c0435afd6e635f0a9031 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 15:36:36 -0400 Subject: [PATCH 026/103] SPI for all boards --- ports/atmel-samd/board_busses.c | 6 +++--- ports/atmel-samd/boards/arduino_zero/mpconfigboard.h | 2 +- .../boards/circuitplayground_express/mpconfigboard.h | 2 +- .../atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h | 2 +- .../atmel-samd/boards/feather_m0_supersized/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h | 2 +- .../atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 2 +- .../atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h | 2 +- ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/metro_m0_express/pins.c | 1 + ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/metro_m4_express/pins.c | 2 ++ .../atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/metro_m4_express_revb/pins.c | 2 ++ ports/atmel-samd/boards/trinket_m0/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/trinket_m0/pins.c | 1 + ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h | 4 ++++ ports/atmel-samd/boards/trinket_m0_haxpress/pins.c | 1 + 22 files changed, 41 insertions(+), 14 deletions(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 8f007aa754..65fa0047d1 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -62,7 +62,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); -#if !defined(DEFAULT_SPI_BUS_CLK) || !defined(DEFAULT_SPI_BUS_MISO) || !defined(DEFAULT_SPI_BUS_MOSI) +#if !defined(DEFAULT_SPI_BUS_SCK) || !defined(DEFAULT_SPI_BUS_MISO) || !defined(DEFAULT_SPI_BUS_MOSI) STATIC mp_obj_t board_spi(void) { mp_raise_NotImplementedError("No default SPI bus"); return NULL; @@ -75,10 +75,10 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); if (spi_singleton == NULL) { busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t); self->base.type = &busio_spi_type; - assert_pin_free(DEFAULT_SPI_BUS_CLK); + assert_pin_free(DEFAULT_SPI_BUS_SCK); assert_pin_free(DEFAULT_SPI_BUS_MOSI); assert_pin_free(DEFAULT_SPI_BUS_MISO); - const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_CLK); + const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_SCK); const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MOSI); const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MISO); common_hal_busio_spi_construct(self, clock, mosi, miso); diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h index 761a9689bc..0f930767ad 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h @@ -18,6 +18,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index bc7a6ed5df..320374cbbf 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -62,6 +62,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PA05) +#define DEFAULT_SPI_BUS_SCK (&pin_PA05) #define DEFAULT_SPI_BUS_MOSI (&pin_PA07) #define DEFAULT_SPI_BUS_MISO (&pin_PA06) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index 4ba9511325..7ffba821bf 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -16,6 +16,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index 3a9231a2d9..2201a57724 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -17,6 +17,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index 752308739d..1e7947696f 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -52,6 +52,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index c8b461ac53..424fbb95f4 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -19,6 +19,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h index 71f95af1d4..6c29bb5512 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h @@ -19,6 +19,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index 6345ddfde6..8126eb2646 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -51,6 +51,6 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 962558200e..e18cac45f6 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -32,6 +32,6 @@ #include "external_flash/external_flash.h" -#define DEFAULT_SPI_BUS_CLK (&pin_PA17) +#define DEFAULT_SPI_BUS_SCK (&pin_PA17) #define DEFAULT_SPI_BUS_MOSI (&pin_PA19) #define DEFAULT_SPI_BUS_MISO (&pin_PA18) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index 4fbf3189f5..ce96f8e385 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -47,6 +47,6 @@ #include "external_flash/external_flash.h" -#define DEFAULT_SPI_BUS_CLK (&pin_PB11) +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h index dcdd3f43ef..ec3ce51b2d 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h @@ -32,6 +32,6 @@ #include "external_flash/external_flash.h" -#define DEFAULT_SPI_BUS_CLK (&pin_PA01) +#define DEFAULT_SPI_BUS_SCK (&pin_PA01) #define DEFAULT_SPI_BUS_MOSI (&pin_PA00) #define DEFAULT_SPI_BUS_MISO (&pin_PB23) \ No newline at end of file diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index 9b8b0ab88c..f1c278023b 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -52,3 +52,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) + +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/metro_m0_express/pins.c b/ports/atmel-samd/boards/metro_m0_express/pins.c index 32f3f875c4..458b01bb5b 100644 --- a/ports/atmel-samd/boards/metro_m0_express/pins.c +++ b/ports/atmel-samd/boards/metro_m0_express/pins.c @@ -31,5 +31,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index 7012037717..9cb41a28e5 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -32,3 +32,7 @@ #define EXTERNAL_FLASH_DEVICES S25FL116K, S25FL216K, GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_SPI_BUS_SCK (&pin_PA13) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA12) +#define DEFAULT_SPI_BUS_MISO (&pin_PA14) \ No newline at end of file diff --git a/ports/atmel-samd/boards/metro_m4_express/pins.c b/ports/atmel-samd/boards/metro_m4_express/pins.c index 32ecdf4923..35e34845b6 100644 --- a/ports/atmel-samd/boards/metro_m4_express/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express/pins.c @@ -41,5 +41,7 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_RX), (mp_obj_t)&pin_PB06 }, { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, + + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h index 206d85465e..939d7615f9 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h @@ -60,3 +60,7 @@ GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_SPI_BUS_SCK (&pin_PA13) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA12) +#define DEFAULT_SPI_BUS_MISO (&pin_PA15) diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c index a18906f73d..d0c1080130 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c @@ -40,5 +40,7 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_RX), (mp_obj_t)&pin_PB06 }, { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, + + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h index a0c4db3d6c..3c2b41fd13 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h @@ -17,3 +17,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA09) #define DEFAULT_I2C_BUS_SDA (&pin_PA08) + +#define DEFAULT_SPI_BUS_SCK (&pin_PA07) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA06) +#define DEFAULT_SPI_BUS_MISO (&pin_PA09) \ No newline at end of file diff --git a/ports/atmel-samd/boards/trinket_m0/pins.c b/ports/atmel-samd/boards/trinket_m0/pins.c index 74d03def29..424b8f75aa 100644 --- a/ports/atmel-samd/boards/trinket_m0/pins.c +++ b/ports/atmel-samd/boards/trinket_m0/pins.c @@ -29,5 +29,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index 3b1eab4b20..f1e12a5c62 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -57,3 +57,7 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) + +#define DEFAULT_SPI_BUS_SCK (&pin_PA07) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA06) +#define DEFAULT_SPI_BUS_MISO (&pin_PA09) \ No newline at end of file diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c index 74d03def29..424b8f75aa 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c @@ -29,5 +29,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 2c067edf540fcc1f1f4aa68f0992ce8480a954bb Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 15:54:01 -0400 Subject: [PATCH 027/103] used pins.c instead of README.rst --- ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m4_express/pins.c | 2 ++ ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/itsybitsy_m0_express/pins.c | 2 ++ ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/itsybitsy_m4_express/pins.c | 2 ++ ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m4_express/pins.c | 2 ++ ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m4_express_revb/pins.c | 2 ++ ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h | 4 ++-- ports/atmel-samd/boards/ugame10/mpconfigboard.h | 3 --- ports/atmel-samd/boards/ugame10/pins.c | 2 -- 17 files changed, 35 insertions(+), 15 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index 38df41b94c..dc9aabf17c 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -13,5 +13,5 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) -#define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) +#define DEFAULT_I2C_BUS_SCL (&pin_PA22) +#define DEFAULT_I2C_BUS_SDA (&pin_PA23) diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index d949244972..dff82912ec 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -14,5 +14,5 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) -#define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index c34869a572..4435412dfa 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -49,5 +49,5 @@ #define BOARD_HAS_CRYSTAL 1 -#define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index 7525c01624..037d9b9922 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -48,5 +48,5 @@ #include "external_flash/external_flash.h" -#define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 6e10222562..930dc9ee28 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -31,3 +31,6 @@ #define EXTERNAL_FLASH_QSPI_DUAL #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PA13) +#define DEFAULT_I2C_BUS_SDA (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index c161e736ed..2fa5fa1b56 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, @@ -26,5 +27,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA22) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA23) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB23) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index a7ecd27ea8..4058a9bcd2 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -46,3 +46,6 @@ GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c index b70bb6a76c..8eec4ca224 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA11) }, @@ -38,5 +39,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA00) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h index 95c542a43e..0ff0ee2c34 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h @@ -31,3 +31,6 @@ #define EXTERNAL_FLASH_DEVICES GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PA13) +#define DEFAULT_I2C_BUS_SDA (&pin_PA12) \ No newline at end of file diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c index 11aeda9c67..490922a46a 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c @@ -1,4 +1,5 @@ #include "samd51_pins.h" +#include "board_busses.h" // This mapping only includes functional names because pins broken // out on connectors are labeled with their MCU name available from @@ -35,5 +36,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PB03) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PB02) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index 7012037717..0d93ed700e 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -32,3 +32,6 @@ #define EXTERNAL_FLASH_DEVICES S25FL116K, S25FL216K, GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/metro_m4_express/pins.c b/ports/atmel-samd/boards/metro_m4_express/pins.c index 32ecdf4923..1d24a0f2f2 100644 --- a/ports/atmel-samd/boards/metro_m4_express/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express/pins.c @@ -1,4 +1,5 @@ #include "samd51_pins.h" +#include "board_busses.h" // This mapping only includes functional names because pins broken // out on connectors are labeled with their MCU name available from @@ -41,5 +42,6 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_RX), (mp_obj_t)&pin_PB06 }, { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h index 206d85465e..8f3c0cd0e0 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h @@ -60,3 +60,6 @@ GD25Q16C #include "external_flash/external_flash.h" + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c index a18906f73d..5e6e719ace 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c @@ -1,4 +1,5 @@ #include "samd51_pins.h" +#include "board_busses.h" // This mapping only includes functional names because pins broken // out on connectors are labeled with their MCU name available from @@ -40,5 +41,6 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_RX), (mp_obj_t)&pin_PB06 }, { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index 3b1eab4b20..9142a7d7a7 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -55,5 +55,5 @@ #include "external_flash/external_flash.h" -#define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) +#define DEFAULT_I2C_BUS_SCL (&pin_PA09) +#define DEFAULT_I2C_BUS_SDA (&pin_PA08) diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.h b/ports/atmel-samd/boards/ugame10/mpconfigboard.h index 57caefb882..1652314ed8 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.h +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.h @@ -47,6 +47,3 @@ #define EXTERNAL_FLASH_DEVICES S25FL216K #include "external_flash/external_flash.h" - -#define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index 2af55f6da7..af516a401d 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -1,5 +1,4 @@ #include "samd21_pins.h" -#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA00) }, @@ -24,6 +23,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_B), MP_ROM_PTR(&pin_PA14) }, { MP_ROM_QSTR(MP_QSTR_C), MP_ROM_PTR(&pin_PA15) }, { MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 8a351595ef6df62c2c223e2bfe7b35f29db3bf84 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 15:57:25 -0400 Subject: [PATCH 028/103] formatting, trailing newlines --- ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h | 2 +- ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 2 +- ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index ff5512d814..1a3fa72f21 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -17,4 +17,4 @@ // https://learn.adafruit.com/assets/46254 // https://learn.adafruit.com/assets/46255 #define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 930dc9ee28..48c63454e5 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -33,4 +33,4 @@ #include "external_flash/external_flash.h" #define DEFAULT_I2C_BUS_SCL (&pin_PA13) -#define DEFAULT_I2C_BUS_SDA (&pin_PA12) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA12) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index 4058a9bcd2..7b0280acb3 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -48,4 +48,4 @@ #include "external_flash/external_flash.h" #define DEFAULT_I2C_BUS_SCL (&pin_PA23) -#define DEFAULT_I2C_BUS_SDA (&pin_PA22) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h index 0ff0ee2c34..e8335ef3f8 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h @@ -33,4 +33,4 @@ #include "external_flash/external_flash.h" #define DEFAULT_I2C_BUS_SCL (&pin_PA13) -#define DEFAULT_I2C_BUS_SDA (&pin_PA12) \ No newline at end of file +#define DEFAULT_I2C_BUS_SDA (&pin_PA12) From 4f93467841fff419f1736a78d0a1dacac6911a7a Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Wed, 16 May 2018 16:10:32 -0400 Subject: [PATCH 029/103] set #define MICROPY_USE_INTERNAL_ERRNO (0) in mpconfigport for ESP8266 to fix compile error --- ports/esp8266/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 616e9875c1..907a4cbdfd 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -28,7 +28,7 @@ #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) -#define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_USE_INTERNAL_ERRNO (0) #define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_BUILTINS_COMPLEX (0) From e8e75c056a884ce87d1a1e2eeea1ee266ba0fcd5 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 17:02:41 -0400 Subject: [PATCH 030/103] For the two boards without SCL and SDA can you still add the board global entry for I2C? That way they'll raise the NotImplementedError with the better message instead of a NameError. --- ports/atmel-samd/board_busses.c | 2 +- ports/atmel-samd/boards/pirkey_m0/pins.c | 2 ++ ports/atmel-samd/boards/ugame10/pins.c | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index fa38972064..0dc95a32c7 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -51,7 +51,7 @@ assert_pin_free(DEFAULT_I2C_BUS_SDA); assert_pin_free(DEFAULT_I2C_BUS_SCL); - common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000); + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000, 0); i2c_singleton = (mp_obj_t)self; } return i2c_singleton; diff --git a/ports/atmel-samd/boards/pirkey_m0/pins.c b/ports/atmel-samd/boards/pirkey_m0/pins.c index a1b7db85c8..dac6687955 100644 --- a/ports/atmel-samd/boards/pirkey_m0/pins.c +++ b/ports/atmel-samd/boards/pirkey_m0/pins.c @@ -1,9 +1,11 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_REMOTEIN), MP_ROM_PTR(&pin_PA28) }, { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index af516a401d..2af55f6da7 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -1,4 +1,5 @@ #include "samd21_pins.h" +#include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA00) }, @@ -23,5 +24,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_B), MP_ROM_PTR(&pin_PA14) }, { MP_ROM_QSTR(MP_QSTR_C), MP_ROM_PTR(&pin_PA15) }, { MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 918d30cb97c8d15ad27c6a59f68c55dc211b425a Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 16 May 2018 16:28:43 -0500 Subject: [PATCH 031/103] Rephrase the doc --- shared-bindings/supervisor/__init__.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c index b2b0c80d96..e6429be320 100644 --- a/shared-bindings/supervisor/__init__.c +++ b/shared-bindings/supervisor/__init__.c @@ -98,7 +98,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s //| .. method:: reload() //| -//| Reload the microcontroller (equivalent to hitting Ctrl-D at the REPL). +//| Reload the main Python code and run it (equivalent to hitting Ctrl-D at the REPL). //| STATIC mp_obj_t supervisor_reload(void) { reload_requested = true; From c2fa892a0273dc9bb45b395daea46fda4a412692 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 17:38:44 -0400 Subject: [PATCH 032/103] fix formatting --- ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 2 +- ports/atmel-samd/boards/feather_m0_basic/pins.c | 1 - ports/atmel-samd/boards/feather_m0_express/pins.c | 1 - ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 2 -- ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h | 4 +--- ports/atmel-samd/boards/trinket_m0/mpconfigboard.h | 2 +- ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h | 2 +- 7 files changed, 4 insertions(+), 10 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index e736977c8f..0e66fb25e1 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -19,4 +19,4 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) -#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/feather_m0_basic/pins.c b/ports/atmel-samd/boards/feather_m0_basic/pins.c index 9e0b4f7d67..1bd283bab3 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/pins.c +++ b/ports/atmel-samd/boards/feather_m0_basic/pins.c @@ -26,6 +26,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_express/pins.c b/ports/atmel-samd/boards/feather_m0_express/pins.c index f3a3e0acda..395a84d551 100644 --- a/ports/atmel-samd/boards/feather_m0_express/pins.c +++ b/ports/atmel-samd/boards/feather_m0_express/pins.c @@ -27,6 +27,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index ca98543e25..fd5e026953 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -14,8 +14,6 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) -// https://learn.adafruit.com/assets/46254 -// https://learn.adafruit.com/assets/46255 #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h index 6c29bb5512..511c9c5e18 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h @@ -14,11 +14,9 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) -// https://learn.adafruit.com/assets/46254 -// https://learn.adafruit.com/assets/46255 #define DEFAULT_I2C_BUS_SCL (&pin_PA23) #define DEFAULT_I2C_BUS_SDA (&pin_PA22) #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) -#define DEFAULT_SPI_BUS_MISO (&pin_PA12) \ No newline at end of file +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h index 3c2b41fd13..5ae83db9ac 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h @@ -20,4 +20,4 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA07) #define DEFAULT_SPI_BUS_MOSI (&pin_PA06) -#define DEFAULT_SPI_BUS_MISO (&pin_PA09) \ No newline at end of file +#define DEFAULT_SPI_BUS_MISO (&pin_PA09) diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index de0e386dd2..4a635a6df3 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -60,4 +60,4 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA07) #define DEFAULT_SPI_BUS_MOSI (&pin_PA06) -#define DEFAULT_SPI_BUS_MISO (&pin_PA09) \ No newline at end of file +#define DEFAULT_SPI_BUS_MISO (&pin_PA09) From b9b742cc69a789008078735dcae5368a4f73e676 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Wed, 16 May 2018 17:43:14 -0400 Subject: [PATCH 033/103] add spi def to all boards pins file --- ports/atmel-samd/boards/gemma_m0/pins.c | 1 + ports/atmel-samd/boards/pirkey_m0/pins.c | 1 + ports/atmel-samd/boards/ugame10/pins.c | 1 + 3 files changed, 3 insertions(+) diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index 6d9d2ad759..9fe2bbd6f2 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -21,5 +21,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/pirkey_m0/pins.c b/ports/atmel-samd/boards/pirkey_m0/pins.c index dac6687955..b8e9371966 100644 --- a/ports/atmel-samd/boards/pirkey_m0/pins.c +++ b/ports/atmel-samd/boards/pirkey_m0/pins.c @@ -7,5 +7,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index 2af55f6da7..80051b550b 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -25,5 +25,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_C), MP_ROM_PTR(&pin_PA15) }, { MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 4fd4adf974a3ac2f4c66bd04923cd9b4dde433bb Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 12:14:51 -0400 Subject: [PATCH 034/103] not working --- ports/atmel-samd/board_busses.c | 31 ++++++++++++++++--- ports/atmel-samd/board_busses.h | 3 ++ .../boards/gemma_m0/mpconfigboard.h | 4 +++ ports/atmel-samd/boards/gemma_m0/pins.c | 1 + 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 52aa5a366e..59f9b80641 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -26,6 +26,8 @@ #include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/SPI.h" +//#include "shared-bindings/busio/UART.h" + #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" #include "pins.h" @@ -34,13 +36,10 @@ #if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) - STATIC mp_obj_t board_i2c(void) { - //board_i2c_obj = NULL; mp_raise_NotImplementedError("No default I2C bus"); return NULL; } - #else STATIC mp_obj_t i2c_singleton = NULL; @@ -59,7 +58,6 @@ } #endif - MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); #if !defined(DEFAULT_SPI_BUS_SCK) || !defined(DEFAULT_SPI_BUS_MISO) || !defined(DEFAULT_SPI_BUS_MOSI) @@ -88,3 +86,28 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); } #endif MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); + +#if !defined(DEFAULT_SERIAL_BUS_RX) || !defined(DEFAULT_SERIAL_BUS_TX) + STATIC mp_obj_t board_serial(void) { + mp_raise_NotImplementedError("No default serial bus"); + return NULL; + } +#else + STATIC mp_obj_t serial_singleton = NULL; + + STATIC mp_obj_t board_serial(void) { + if (serial_singleton == NULL) { + //busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); + //self->base.type = &busio_uart_type; + + assert_pin_free(DEFAULT_SERIAL_BUS_RX); + assert_pin_free(DEFAULT_SERIAL_BUS_TX); + + //common_hal_busio_uart_construct(self, DEFAULT_SERIAL_BUS_TX, DEFAULT_SERIAL_BUS_RX, 9600, 8, PARITY_NONE, 1, 1000, 64); + //serial_singleton = (mp_obj_t)self; + serial_singleton = NULL; + } + return serial_singleton; + } +#endif +MP_DEFINE_CONST_FUN_OBJ_0(board_serial_obj, board_serial); \ No newline at end of file diff --git a/ports/atmel-samd/board_busses.h b/ports/atmel-samd/board_busses.h index d0cd5dbf78..64e937a69b 100644 --- a/ports/atmel-samd/board_busses.h +++ b/ports/atmel-samd/board_busses.h @@ -33,4 +33,7 @@ extern mp_obj_fun_builtin_fixed_t board_i2c_obj; void board_spi(void); extern mp_obj_fun_builtin_fixed_t board_spi_obj; +void board_serial(void); +extern mp_obj_fun_builtin_fixed_t board_serial_obj; + #endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index 2527b9d069..46866fdb44 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -15,6 +15,10 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA05) #define DEFAULT_I2C_BUS_SDA (&pin_PA04) +#define DEFAULT_SERIAL_BUS_RX (&pin_PA05) +#define DEFAULT_SERIAL_BUS_TX (&pin_PA04) + + #include "internal_flash.h" #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index 9fe2bbd6f2..3cb3b795f0 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -22,5 +22,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_SERIAL), MP_ROM_PTR(&board_i2c_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From c846f4bdae77c04958ee22114ad9c730e3171641 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 12:17:06 -0400 Subject: [PATCH 035/103] remove newline --- ports/atmel-samd/boards/arduino_zero/pins.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index 7869b24ccf..8a4fe09017 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -34,6 +34,5 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From e1cccd3dac4ef6ead064540d93e282d8fbe4fc54 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 13:21:15 -0400 Subject: [PATCH 036/103] rename to UART --- ports/atmel-samd/board_busses.c | 31 +++++++++---------- ports/atmel-samd/board_busses.h | 4 +-- .../boards/gemma_m0/mpconfigboard.h | 4 +-- ports/atmel-samd/boards/gemma_m0/pins.c | 2 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 59f9b80641..97602f8745 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -26,7 +26,7 @@ #include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/SPI.h" -//#include "shared-bindings/busio/UART.h" +#include "shared-bindings/busio/UART.h" #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" @@ -87,27 +87,26 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); #endif MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); -#if !defined(DEFAULT_SERIAL_BUS_RX) || !defined(DEFAULT_SERIAL_BUS_TX) - STATIC mp_obj_t board_serial(void) { - mp_raise_NotImplementedError("No default serial bus"); +#if !defined(DEFAULT_UART_BUS_RX) || !defined(DEFAULT_UART_BUS_TX) + STATIC mp_obj_t board_uart(void) { + mp_raise_NotImplementedError("No default UART bus"); return NULL; } #else - STATIC mp_obj_t serial_singleton = NULL; + STATIC mp_obj_t uart_singleton = NULL; - STATIC mp_obj_t board_serial(void) { - if (serial_singleton == NULL) { - //busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); - //self->base.type = &busio_uart_type; + STATIC mp_obj_t board_uart(void) { + if (uart_singleton == NULL) { + busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); + self->base.type = &busio_uart_type; - assert_pin_free(DEFAULT_SERIAL_BUS_RX); - assert_pin_free(DEFAULT_SERIAL_BUS_TX); + assert_pin_free(DEFAULT_UART_BUS_RX); + assert_pin_free(DEFAULT_UART_BUS_TX); - //common_hal_busio_uart_construct(self, DEFAULT_SERIAL_BUS_TX, DEFAULT_SERIAL_BUS_RX, 9600, 8, PARITY_NONE, 1, 1000, 64); - //serial_singleton = (mp_obj_t)self; - serial_singleton = NULL; + common_hal_busio_uart_construct(self, DEFAULT_UART_BUS_TX, DEFAULT_UART_BUS_RX, 9600, 8, PARITY_NONE, 1, 1000, 64); + uart_singleton = (mp_obj_t)self; } - return serial_singleton; + return uart_singleton; } #endif -MP_DEFINE_CONST_FUN_OBJ_0(board_serial_obj, board_serial); \ No newline at end of file +MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); \ No newline at end of file diff --git a/ports/atmel-samd/board_busses.h b/ports/atmel-samd/board_busses.h index 64e937a69b..a368885a58 100644 --- a/ports/atmel-samd/board_busses.h +++ b/ports/atmel-samd/board_busses.h @@ -33,7 +33,7 @@ extern mp_obj_fun_builtin_fixed_t board_i2c_obj; void board_spi(void); extern mp_obj_fun_builtin_fixed_t board_spi_obj; -void board_serial(void); -extern mp_obj_fun_builtin_fixed_t board_serial_obj; +void board_uart(void); +extern mp_obj_fun_builtin_fixed_t board_uart_obj; #endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index 46866fdb44..b9aa9c1747 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -15,8 +15,8 @@ #define DEFAULT_I2C_BUS_SCL (&pin_PA05) #define DEFAULT_I2C_BUS_SDA (&pin_PA04) -#define DEFAULT_SERIAL_BUS_RX (&pin_PA05) -#define DEFAULT_SERIAL_BUS_TX (&pin_PA04) +#define DEFAULT_UART_BUS_RX (&pin_PA05) +#define DEFAULT_UART_BUS_TX (&pin_PA04) #include "internal_flash.h" diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index 3cb3b795f0..6d031c6854 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -22,6 +22,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - { MP_ROM_QSTR(MP_QSTR_SERIAL), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From fc2b4526d351053992b945ddddf9ee3c5895e05d Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 13:33:26 -0400 Subject: [PATCH 037/103] worked --- ports/atmel-samd/board_busses.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 97602f8745..02d08133c7 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -103,7 +103,10 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); assert_pin_free(DEFAULT_UART_BUS_RX); assert_pin_free(DEFAULT_UART_BUS_TX); - common_hal_busio_uart_construct(self, DEFAULT_UART_BUS_TX, DEFAULT_UART_BUS_RX, 9600, 8, PARITY_NONE, 1, 1000, 64); + const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RX); + const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_TX); + + common_hal_busio_uart_construct(self, tx, rx, 9600, 8, PARITY_NONE, 1, 1000, 64); uart_singleton = (mp_obj_t)self; } return uart_singleton; From 173a0a5faa68023c997fc55f46ed4bc1cf1ab7cd Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 13:42:08 -0400 Subject: [PATCH 038/103] arduino, circuitplayground, feather boards --- ports/atmel-samd/boards/arduino_zero/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/arduino_zero/pins.c | 1 + .../boards/circuitplayground_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/circuitplayground_express/pins.c | 1 + ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_adalogger/pins.c | 1 + ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_basic/pins.c | 1 + ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_express/pins.c | 1 + ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_rfm69/pins.c | 1 + ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_rfm9x/pins.c | 1 + ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_supersized/pins.c | 1 + ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m4_express/pins.c | 1 + 18 files changed, 36 insertions(+) diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h index 0f930767ad..a32f128f52 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.h @@ -21,3 +21,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index 8a4fe09017..4fa417b373 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -34,5 +34,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 320374cbbf..3d97e2cb19 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -65,3 +65,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA05) #define DEFAULT_SPI_BUS_MOSI (&pin_PA07) #define DEFAULT_SPI_BUS_MISO (&pin_PA06) + +#define DEFAULT_UART_BUS_RX (&pin_PB09) +#define DEFAULT_UART_BUS_TX (&pin_PB08) diff --git a/ports/atmel-samd/boards/circuitplayground_express/pins.c b/ports/atmel-samd/boards/circuitplayground_express/pins.c index 4f32ecafab..f4540573a8 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express/pins.c @@ -59,5 +59,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h index d38bfcdc87..94caa7d040 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h @@ -19,3 +19,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c index 1ae2d958a4..7eff106e82 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c +++ b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c @@ -33,5 +33,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RED_LED), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h index 0e66fb25e1..34de8e1660 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h @@ -20,3 +20,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_basic/pins.c b/ports/atmel-samd/boards/feather_m0_basic/pins.c index 1bd283bab3..b3edabe812 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/pins.c +++ b/ports/atmel-samd/boards/feather_m0_basic/pins.c @@ -26,5 +26,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index bc4a9d825c..bfa8c642a2 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -55,3 +55,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_express/pins.c b/ports/atmel-samd/boards/feather_m0_express/pins.c index 395a84d551..6bd74f3729 100644 --- a/ports/atmel-samd/boards/feather_m0_express/pins.c +++ b/ports/atmel-samd/boards/feather_m0_express/pins.c @@ -27,5 +27,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h index fd5e026953..4f4ea2ffea 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h @@ -20,3 +20,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c index 1c6051a5c4..e6cede3dd9 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c @@ -29,5 +29,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM69_CS), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h index 511c9c5e18..a9d586c2e3 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h @@ -20,3 +20,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c index addb700e42..8f52a5767c 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c @@ -29,5 +29,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_RFM9X_CS), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index cbbe8b8f2c..c8118e48b4 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -54,3 +54,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_supersized/pins.c b/ports/atmel-samd/boards/feather_m0_supersized/pins.c index 395a84d551..6bd74f3729 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/pins.c +++ b/ports/atmel-samd/boards/feather_m0_supersized/pins.c @@ -27,5 +27,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index f3dc24feef..3c1895dd24 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -38,3 +38,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA17) #define DEFAULT_SPI_BUS_MOSI (&pin_PA19) #define DEFAULT_SPI_BUS_MISO (&pin_PA18) + +#define DEFAULT_UART_BUS_RX (&pin_PA07) +#define DEFAULT_UART_BUS_TX (&pin_PA04) diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index 99c344638e..6807a901d5 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -29,5 +29,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB23) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From a84845c0ae09739d732264634fc3b84b7624f9c4 Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 13:48:33 -0400 Subject: [PATCH 039/103] itsbitsy, metro --- ports/atmel-samd/boards/gemma_m0/mpconfigboard.h | 1 - ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/itsybitsy_m0_express/pins.c | 1 + ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/itsybitsy_m4_express/pins.c | 1 + ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m0_express/pins.c | 1 + ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m4_express/pins.c | 1 + ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m4_express_revb/pins.c | 1 + 11 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h index b9aa9c1747..53e802f172 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.h @@ -18,7 +18,6 @@ #define DEFAULT_UART_BUS_RX (&pin_PA05) #define DEFAULT_UART_BUS_TX (&pin_PA04) - #include "internal_flash.h" #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index 14efd46022..be59bc5d4a 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -53,3 +53,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c index a2f19501d4..2b35c91aad 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c @@ -41,5 +41,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA00) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h index db6ea09b31..faa0d6e754 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h @@ -38,3 +38,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA01) #define DEFAULT_SPI_BUS_MOSI (&pin_PA00) #define DEFAULT_SPI_BUS_MISO (&pin_PB23) + +#define DEFAULT_UART_BUS_RX (&pin_PA16) +#define DEFAULT_UART_BUS_TX (&pin_PA17) diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c index bb235aed1d..084250b311 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c @@ -38,5 +38,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PB02) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index f1c278023b..f798c5d365 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -56,3 +56,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PB11) #define DEFAULT_SPI_BUS_MOSI (&pin_PB10) #define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/metro_m0_express/pins.c b/ports/atmel-samd/boards/metro_m0_express/pins.c index 458b01bb5b..4cd8752bea 100644 --- a/ports/atmel-samd/boards/metro_m0_express/pins.c +++ b/ports/atmel-samd/boards/metro_m0_express/pins.c @@ -32,5 +32,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index 9bc8c8b006..cda43d95e6 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -39,3 +39,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA13) #define DEFAULT_SPI_BUS_MOSI (&pin_PA12) #define DEFAULT_SPI_BUS_MISO (&pin_PA14) + +#define DEFAULT_UART_BUS_RX (&pin_PA23) +#define DEFAULT_UART_BUS_TX (&pin_PA22) diff --git a/ports/atmel-samd/boards/metro_m4_express/pins.c b/ports/atmel-samd/boards/metro_m4_express/pins.c index 3d509fa53f..95a97d825f 100644 --- a/ports/atmel-samd/boards/metro_m4_express/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express/pins.c @@ -44,5 +44,6 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h index ee85138a7b..db51140d66 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h @@ -67,3 +67,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA13) #define DEFAULT_SPI_BUS_MOSI (&pin_PA12) #define DEFAULT_SPI_BUS_MISO (&pin_PA15) + +#define DEFAULT_UART_BUS_RX (&pin_PA23) +#define DEFAULT_UART_BUS_TX (&pin_PA22) diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c index a7b4d25b20..23e662f1c3 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c @@ -43,5 +43,6 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 06b293af29da8c5ffa48ac5499e26c2eed529a7d Mon Sep 17 00:00:00 2001 From: Matt Land Date: Thu, 17 May 2018 13:56:10 -0400 Subject: [PATCH 040/103] pirkey, trinket, ugame --- ports/atmel-samd/boards/pirkey_m0/pins.c | 1 + ports/atmel-samd/boards/trinket_m0/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/trinket_m0/pins.c | 1 + ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/trinket_m0_haxpress/pins.c | 1 + ports/atmel-samd/boards/ugame10/pins.c | 1 + 6 files changed, 10 insertions(+) diff --git a/ports/atmel-samd/boards/pirkey_m0/pins.c b/ports/atmel-samd/boards/pirkey_m0/pins.c index b8e9371966..7efc8eb33f 100644 --- a/ports/atmel-samd/boards/pirkey_m0/pins.c +++ b/ports/atmel-samd/boards/pirkey_m0/pins.c @@ -8,5 +8,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h index 0ce7962191..d561947e39 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.h @@ -63,3 +63,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA07) #define DEFAULT_SPI_BUS_MOSI (&pin_PA06) #define DEFAULT_SPI_BUS_MISO (&pin_PA09) + +#define DEFAULT_UART_BUS_RX (&pin_PA07) +#define DEFAULT_UART_BUS_TX (&pin_PA06) diff --git a/ports/atmel-samd/boards/trinket_m0/pins.c b/ports/atmel-samd/boards/trinket_m0/pins.c index 424b8f75aa..0fdcbc2790 100644 --- a/ports/atmel-samd/boards/trinket_m0/pins.c +++ b/ports/atmel-samd/boards/trinket_m0/pins.c @@ -30,5 +30,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index 4a635a6df3..3377d1b7ea 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -61,3 +61,6 @@ #define DEFAULT_SPI_BUS_SCK (&pin_PA07) #define DEFAULT_SPI_BUS_MOSI (&pin_PA06) #define DEFAULT_SPI_BUS_MISO (&pin_PA09) + +#define DEFAULT_UART_BUS_RX (&pin_PA07) +#define DEFAULT_UART_BUS_TX (&pin_PA06) diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c index 424b8f75aa..0fdcbc2790 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c @@ -30,5 +30,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index 80051b550b..acf73c9ffc 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -26,5 +26,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From cf33ad9a548d5d481a34b46d6d0f6c5bd3d1e720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Fri, 18 May 2018 12:31:23 +0200 Subject: [PATCH 041/103] atmel-samd/samd21: Enable long int on Express boards This is necessary for using 1970 epoch in the time module. --- .../boards/circuitplayground_express/mpconfigboard.h | 3 +++ .../boards/circuitplayground_express/mpconfigboard.mk | 2 ++ ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk | 2 ++ ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk | 1 + ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h | 3 +++ ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk | 2 ++ ports/atmel-samd/mpconfigport.h | 2 ++ ports/atmel-samd/mpconfigport.mk | 2 ++ 10 files changed, 23 insertions(+) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 3d97e2cb19..44da28eae4 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit CircuitPlayground Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + // Don't allow touch on A0 (PA02), because it's connected to the speaker. #define PA02_NO_TOUCH (true) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk index 4d6e32eb7b..3b341c32c9 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk @@ -9,6 +9,8 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz + # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index bfa8c642a2..44141773da 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Feather M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define MICROPY_HW_NEOPIXEL (&pin_PA06) // Clock rates are off: Salae reads 12MHz which is the limit even though we set it to the safer 8MHz. diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk index 909922dbe4..202d8a6264 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk @@ -8,3 +8,5 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 + +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index be59bc5d4a..fea2d290b5 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Itsy Bitsy M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define CIRCUITPY_BITBANG_APA102 #define MICROPY_HW_APA102_MOSI (&pin_PA01) #define MICROPY_HW_APA102_SCK (&pin_PA00) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk index 0fb0e38ed4..b3e5a636d0 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk @@ -9,3 +9,4 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index f798c5d365..4825b66631 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Metro M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define MICROPY_HW_LED_TX PIN_PA27 //#define MICROPY_HW_LED_RX PIN_PA31 diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index 79d60380de..ee1335c9a0 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -8,3 +8,5 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 + +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 27c2480013..8e726f9b74 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -141,8 +141,10 @@ typedef long mp_off_t; #define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21" #define PORT_HEAP_SIZE (16384 + 4096) // If you change MICROPY_LONGINT_IMPL, also change MPY_TOOL_LONGINT_IMPL in mpconfigport.mk. +#ifndef MICROPY_LONGINT_IMPL #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) #endif +#endif #ifdef SAMD51 #define CIRCUITPY_MCU_FAMILY samd51 diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 0f3f399e22..80ec9d9787 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -2,8 +2,10 @@ # $(MPY-TOOL) needs to know what kind of longint to use (if any) to freeze long integers. # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. ifeq ($(CHIP_FAMILY), samd21) +ifndef MPY_TOOL_LONGINT_IMPL MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none endif +endif ifeq ($(CHIP_FAMILY), samd51) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz From 95e70cd0ea6d91a235633832b03a7a2137f4b3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Fri, 18 May 2018 10:13:25 +0200 Subject: [PATCH 042/103] time: Use 1970 epoch Use UNIX epoch to match CPython. This overflows small int so time.{time,localtime,mktime} is only supported with long int. Also remove some comment cruft in time_time(). --- shared-bindings/time/__init__.c | 35 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index b4137706ee..a105941c09 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -34,6 +34,8 @@ #include "shared-bindings/rtc/__init__.h" #include "shared-bindings/time/__init__.h" +#define EPOCH1970_EPOCH2000_DIFF_SECS 946684800 + //| :mod:`time` --- time and timing related functions //| ======================================================== //| @@ -183,13 +185,14 @@ void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm) { // elems[8] tm_isdst is not supported } +#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE mp_obj_t MP_WEAK rtc_get_time_source_time(void) { mp_raise_RuntimeError("RTC is not supported on this board"); } //| .. method:: time() //| -//| Return the current time in seconds since since Jan 1, 2000. +//| Return the current time in seconds since since Jan 1, 1970. //| //| :return: the current time //| :rtype: int @@ -197,17 +200,18 @@ mp_obj_t MP_WEAK rtc_get_time_source_time(void) { STATIC mp_obj_t time_time(void) { timeutils_struct_time_t tm; struct_time_to_tm(rtc_get_time_source_time(), &tm); - mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, // mp_uint_t date + mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return mp_obj_new_int_from_uint(secs); + return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS); } MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); //| .. method:: localtime([secs]) //| -//| Convert a time expressed in seconds since Jan 1, 2000 to a struct_time in +//| Convert a time expressed in seconds since Jan 1, 1970 to a struct_time in //| local time. If secs is not provided or None, the current time as returned //| by time() is used. +//| The earliest date for which it can generate a time is Jan 1, 2000. //| //| :return: the current time //| :rtype: time.struct_time @@ -217,8 +221,12 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { return rtc_get_time_source_time(); } + mp_int_t secs = mp_obj_int_get_checked(args[0]); + if (secs < EPOCH1970_EPOCH2000_DIFF_SECS) + mp_raise_msg(&mp_type_OverflowError, "timestamp out of range for platform time_t"); + timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(mp_obj_get_int(args[0]), &tm); + timeutils_seconds_since_2000_to_struct_time(secs - EPOCH1970_EPOCH2000_DIFF_SECS, &tm); return struct_time_from_tm(&tm); } @@ -247,11 +255,16 @@ STATIC mp_obj_t time_mktime(mp_obj_t t) { mp_raise_TypeError("function takes exactly 9 arguments"); } - return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), - mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); + if (mp_obj_get_int(elem[0]) < 2000) + mp_raise_msg(&mp_type_OverflowError, "timestamp out of range for platform time_t"); + + mp_uint_t secs = timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), + mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])); + return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS); } MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); -#endif +#endif // MICROPY_LONGINT_IMPL +#endif // MICROPY_PY_COLLECTIONS STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, @@ -260,10 +273,14 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) }, #if MICROPY_PY_COLLECTIONS { MP_ROM_QSTR(MP_QSTR_struct_time), MP_ROM_PTR(&struct_time_type_obj) }, + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, - #endif + #endif // MICROPY_LONGINT_IMPL + #endif // MICROPY_PY_COLLECTIONS + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); From 990da6d3637da3eafb0f9a71bc9e26e006e82195 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 19 May 2018 01:01:37 -0400 Subject: [PATCH 043/103] wasn't always clearing boot_output_file --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 24f27438c9..4402ebe328 100644 --- a/main.c +++ b/main.c @@ -329,8 +329,8 @@ int __attribute__((used)) main(void) { if (!skip_boot_output) { f_close(boot_output_file); filesystem_flush(); - boot_output_file = NULL; } + boot_output_file = NULL; #endif // Reset to remove any state that boot.py setup. It should only be used to From 1d9bcc5ddf20af29e5a053a885bce446ff39b4c3 Mon Sep 17 00:00:00 2001 From: jerryneedell Date: Sat, 19 May 2018 10:41:36 -0400 Subject: [PATCH 044/103] fix doc error in audioOut.c fixes #851 --- shared-bindings/audioio/AudioOut.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared-bindings/audioio/AudioOut.c b/shared-bindings/audioio/AudioOut.c index 3450b39712..36de117ac8 100644 --- a/shared-bindings/audioio/AudioOut.c +++ b/shared-bindings/audioio/AudioOut.c @@ -80,7 +80,8 @@ //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) //| speaker_enable.switch_to_output(value=True) //| -//| wav = audioio.WaveFile("cplay-5.1-16bit-16khz.wav") +//| data = open("cplay-5.1-16bit-16khz.wav", "rb") +//| wav = audioio.WaveFile(data) //| a = audioio.AudioOut(board.A0) //| //| print("playing") From 6a6ab2b0ec6419285849335c81587695cf9618e6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sun, 20 May 2018 22:06:47 +0200 Subject: [PATCH 045/103] Allow overriding EXTRA_BUILTIN_MODULES in mpconfigboard.h Some boards (like the uGame10) may want to have their own set of extra modules. This change lets them override EXTRA_BUILTIN_MODULES in their mpconfigboard.h and makes the ugame10 board do that. --- ports/atmel-samd/boards/ugame10/mpconfigboard.h | 5 +++++ ports/atmel-samd/boards/ugame10/mpconfigboard.mk | 3 +++ ports/atmel-samd/mpconfigport.h | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.h b/ports/atmel-samd/boards/ugame10/mpconfigboard.h index 1652314ed8..4e5e885694 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.h +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.h @@ -47,3 +47,8 @@ #define EXTERNAL_FLASH_DEVICES S25FL216K #include "external_flash/external_flash.h" + +#define EXTRA_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module } diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk index 343115fbb4..346dcc6580 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk @@ -9,3 +9,6 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 + + +FROZEN_MPY_DIRS += $(TOP)/frozen/ugame10 diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 8e726f9b74..959d969866 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -207,18 +207,22 @@ extern const struct _mp_obj_module_t usb_hid_module; #define AUDIOBUSIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_audiobusio), (mp_obj_t)&audiobusio_module }, #endif + #ifndef EXTRA_BUILTIN_MODULES #define EXTRA_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \ AUDIOBUSIO_MODULE \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module } + #endif #define EXPRESS_BOARD #else #define MICROPY_PY_BUILTINS_REVERSED (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (0) #define MICROPY_PY_FRAMEBUF (0) + #ifndef EXTRA_BUILTIN_MODULES #define EXTRA_BUILTIN_MODULES + #endif #define MICROPY_PY_BUILTINS_COMPLEX (0) From 5680933483b937c34c317ffa93c367aab7723e9d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 21 May 2018 23:58:03 -0400 Subject: [PATCH 046/103] refactor longint settings; make crickit cpx build --- .gitmodules | 6 ++ frozen/Adafruit_CircuitPython_Motor | 1 + frozen/Adafruit_CircuitPython_seesaw | 1 + ports/atmel-samd/Makefile | 13 ++++ .../boards/arduino_zero/mpconfigboard.mk | 1 + .../circuitplayground_express/mpconfigboard.h | 3 - .../mpconfigboard.mk | 1 + .../circuitplayground_express_crickit/board.c | 66 +++++++++++++++++ .../mpconfigboard.h | 73 +++++++++++++++++++ .../mpconfigboard.mk | 22 ++++++ .../circuitplayground_express_crickit/pins.c | 64 ++++++++++++++++ .../feather_m0_adalogger/mpconfigboard.mk | 1 + .../boards/feather_m0_basic/mpconfigboard.mk | 1 + .../boards/feather_m0_express/mpconfigboard.h | 3 - .../feather_m0_express/mpconfigboard.mk | 1 + .../boards/feather_m0_rfm69/mpconfigboard.mk | 1 + .../boards/feather_m0_rfm9x/mpconfigboard.mk | 1 + .../feather_m0_supersized/mpconfigboard.mk | 2 +- .../feather_m4_express/mpconfigboard.mk | 1 + .../boards/gemma_m0/mpconfigboard.mk | 1 + .../itsybitsy_m0_express/mpconfigboard.h | 3 - .../itsybitsy_m0_express/mpconfigboard.mk | 1 + .../itsybitsy_m4_express/mpconfigboard.mk | 1 + .../boards/metro_m0_express/mpconfigboard.h | 3 - .../boards/metro_m0_express/mpconfigboard.mk | 1 + .../boards/metro_m4_express/mpconfigboard.mk | 1 + .../metro_m4_express_revb/mpconfigboard.mk | 1 + .../boards/pirkey_m0/mpconfigboard.mk | 1 + .../boards/trinket_m0/mpconfigboard.mk | 1 + .../trinket_m0_haxpress/mpconfigboard.mk | 1 + .../boards/ugame10/mpconfigboard.mk | 1 + ports/atmel-samd/mpconfigport.h | 22 ++++-- ports/atmel-samd/mpconfigport.mk | 15 +++- tools/build_adafruit_bins.sh | 2 +- 34 files changed, 292 insertions(+), 25 deletions(-) create mode 160000 frozen/Adafruit_CircuitPython_Motor create mode 160000 frozen/Adafruit_CircuitPython_seesaw create mode 100644 ports/atmel-samd/boards/circuitplayground_express_crickit/board.c create mode 100644 ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h create mode 100644 ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk create mode 100644 ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c diff --git a/.gitmodules b/.gitmodules index 59048d6c03..9025eb7d66 100644 --- a/.gitmodules +++ b/.gitmodules @@ -55,3 +55,9 @@ [submodule "frozen/Adafruit_CircuitPython_HID"] path = frozen/Adafruit_CircuitPython_HID url = https://github.com/adafruit/Adafruit_CircuitPython_HID.git +[submodule "ports/atmel-samd/Adafruit_CircuitPython_Motor"] + path = frozen/Adafruit_CircuitPython_Motor + url = https://github.com/adafruit/Adafruit_CircuitPython_Motor.git +[submodule "ports/atmel-samd/Adafruit_CircuitPython_seesaw"] + path = frozen/Adafruit_CircuitPython_seesaw + url = https://github.com/adafruit/Adafruit_CircuitPython_seesaw.git diff --git a/frozen/Adafruit_CircuitPython_Motor b/frozen/Adafruit_CircuitPython_Motor new file mode 160000 index 0000000000..6da5d14b98 --- /dev/null +++ b/frozen/Adafruit_CircuitPython_Motor @@ -0,0 +1 @@ +Subproject commit 6da5d14b98ac3d929662731f73ce03c0c958b520 diff --git a/frozen/Adafruit_CircuitPython_seesaw b/frozen/Adafruit_CircuitPython_seesaw new file mode 160000 index 0000000000..71f3947ff9 --- /dev/null +++ b/frozen/Adafruit_CircuitPython_seesaw @@ -0,0 +1 @@ +Subproject commit 71f3947ff94ca070eb01c5f872b0400031be8086 diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index eb25c1aa30..1fdeab5f5e 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -165,6 +165,19 @@ ifndef INTERNAL_LIBM LIBS += -lm endif +# Propagate longint choice from .mk to C. +ifdef LONGINT_IMPL_NONE +CFLAGS += -DLONGINT_IMPL_NONE +endif + +ifdef LONGINT_IMPL_MPZ +CFLAGS += -DLONGINT_IMPL_MPZ +endif + +ifdef LONGINT_IMPL_LONGLONG +CFLAGS += -DLONGINT_IMPL_LONGLONG +endif + ifeq ($(CHIP_FAMILY), samd21) LDFLAGS += -mthumb -mcpu=cortex-m0plus -Lasf/thirdparty/CMSIS/Lib/GCC/ BOOTLOADER_SIZE := 0x2000 diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk index b24cc9c647..638c76e1f0 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Arduino Zero" USB_MANUFACTURER = "Arduino" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 44da28eae4..3d97e2cb19 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -1,9 +1,6 @@ #define MICROPY_HW_BOARD_NAME "Adafruit CircuitPlayground Express" #define MICROPY_HW_MCU_NAME "samd21g18" -#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ -#define MP_SSIZE_MAX 0x7fffffff - // Don't allow touch on A0 (PA02), because it's connected to the speaker. #define PA02_NO_TOUCH (true) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk index 3b341c32c9..a3580923c2 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "CircuitPlayground Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c b/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c new file mode 100644 index 0000000000..eb7ffab4a2 --- /dev/null +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c @@ -0,0 +1,66 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 + +#include "boards/board.h" +#include "common-hal/microcontroller/Pin.h" +#include "hal/include/hal_gpio.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/neopixel_write/__init__.h" +#include "samd21_pins.h" + +void board_init(void) +{ +} + +// Check the status of the two buttons on CircuitPlayground Express. If both are +// pressed, then boot into user safe mode. +bool board_requests_safe_mode(void) { + gpio_set_pin_function(PIN_PA14, GPIO_PIN_FUNCTION_OFF); + gpio_set_pin_direction(PIN_PA14, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(PIN_PA14, GPIO_PULL_DOWN); + + gpio_set_pin_function(PIN_PA28, GPIO_PIN_FUNCTION_OFF); + gpio_set_pin_direction(PIN_PA28, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN); + bool safe_mode = gpio_get_pin_level(PIN_PA14) && + gpio_get_pin_level(PIN_PA28); + reset_pin(PIN_PA14); + reset_pin(PIN_PA28); + return safe_mode; +} + +void reset_board(void) { + uint8_t empty[30]; + memset(empty, 0, 30); + digitalio_digitalinout_obj_t neopixel_pin; + common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23); + common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false, + DRIVE_MODE_PUSH_PULL); + common_hal_neopixel_write(&neopixel_pin, empty, 30); + common_hal_digitalio_digitalinout_deinit(&neopixel_pin); +} diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h new file mode 100644 index 0000000000..83cc19d9f3 --- /dev/null +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h @@ -0,0 +1,73 @@ +#define MICROPY_HW_BOARD_NAME "Adafruit CircuitPlayground Express with Crickit libraries" +#define MICROPY_HW_MCU_NAME "samd21g18" + +// No framebuf on CRICKit version to save space. +#define MICROPY_PY_FRAMEBUF (0) + +// Don't allow touch on A0 (PA02), because it's connected to the speaker. +#define PA02_NO_TOUCH (true) + +// Salae reads 12mhz which is the limit even though we set it to the safer 8mhz. +#define SPI_FLASH_BAUDRATE (8000000) + +// On-board flash +#define SPI_FLASH_MOSI_PIN PIN_PA20 +#define SPI_FLASH_MISO_PIN PIN_PA16 +#define SPI_FLASH_SCK_PIN PIN_PA21 +#define SPI_FLASH_CS_PIN PIN_PB22 + +#define SPI_FLASH_MOSI_PIN_FUNCTION PINMUX_PA20D_SERCOM3_PAD2 +#define SPI_FLASH_MISO_PIN_FUNCTION PINMUX_PA16D_SERCOM3_PAD0 +#define SPI_FLASH_SCK_PIN_FUNCTION PINMUX_PA21D_SERCOM3_PAD3 +#define SPI_FLASH_SERCOM SERCOM3 +#define SPI_FLASH_SERCOM_INDEX 3 +#define SPI_FLASH_MOSI_PAD 2 +#define SPI_FLASH_MISO_PAD 0 +#define SPI_FLASH_SCK_PAD 3 + +// Transmit Data Pinout +// <0x0=>PAD[0,1]_DO_SCK +// <0x1=>PAD[2,3]_DO_SCK +// <0x2=>PAD[3,1]_DO_SCK +// <0x3=>PAD[0,3]_DO_SCK +#define SPI_FLASH_DOPO 1 +#define SPI_FLASH_DIPO 0 // same as MISO PAD + +// These are pins not to reset. +// PA24 and PA25 are USB. +#define MICROPY_PORT_A (PORT_PA16 | PORT_PA20 | PORT_PA21 | PORT_PA24 | PORT_PA25) +#define MICROPY_PORT_B (PORT_PB22) +#define MICROPY_PORT_C (0) + +#define SPEAKER_ENABLE_PIN (&pin_PA30) + +#include "external_flash/devices.h" + +// If you change this, then make sure to update the linker scripts as well to +// make sure you don't overwrite code. +#define CIRCUITPY_INTERNAL_NVM_SIZE 256 + +#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) + +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ + GD25Q16C + +#include "external_flash/external_flash.h" + +#define CALIBRATE_CRYSTALLESS 1 + +// Explanation of how a user got into safe mode. +#define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up" + +#define DEFAULT_I2C_BUS_SCL (&pin_PB03) +#define DEFAULT_I2C_BUS_SDA (&pin_PB02) + +#define DEFAULT_SPI_BUS_SCK (&pin_PA05) +#define DEFAULT_SPI_BUS_MOSI (&pin_PA07) +#define DEFAULT_SPI_BUS_MISO (&pin_PA06) + +#define DEFAULT_UART_BUS_RX (&pin_PB09) +#define DEFAULT_UART_BUS_TX (&pin_PB08) diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk new file mode 100644 index 0000000000..47be11ed48 --- /dev/null +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk @@ -0,0 +1,22 @@ +LD_FILE = boards/samd21x18-bootloader-external-flash-crystalless.ld +USB_VID = 0x239A +USB_PID = 0x8019 +USB_PRODUCT = "CircuitPlayground Express with Crickit libraries" +USB_MANUFACTURER = "Adafruit Industries LLC" + +SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 + +CHIP_VARIANT = SAMD21G18A +CHIP_FAMILY = samd21 + +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz + +# Include these Python libraries in firmware. +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_seesaw +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Thermistor diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c b/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c new file mode 100644 index 0000000000..f4540573a8 --- /dev/null +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c @@ -0,0 +1,64 @@ +#include "samd21_pins.h" +#include "board_busses.h" + +STATIC const mp_rom_map_elem_t board_global_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA02) }, + { MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_PA02) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_PA07) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_PA07) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PB03) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_PB03) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PB03) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_PB02) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_PB02) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PB02) }, + { MP_ROM_QSTR(MP_QSTR_A6), MP_ROM_PTR(&pin_PB09) }, + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PB09) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PB09) }, + { MP_ROM_QSTR(MP_QSTR_A7), MP_ROM_PTR(&pin_PB08) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_PB08) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PB08) }, + { MP_ROM_QSTR(MP_QSTR_LIGHT), MP_ROM_PTR(&pin_PA11) }, + { MP_ROM_QSTR(MP_QSTR_A8), MP_ROM_PTR(&pin_PA11) }, + { MP_ROM_QSTR(MP_QSTR_TEMPERATURE), MP_ROM_PTR(&pin_PA09) }, + { MP_ROM_QSTR(MP_QSTR_A9), MP_ROM_PTR(&pin_PA09) }, + + { MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_PTR(&pin_PA28) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_PA28) }, + { MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_PTR(&pin_PA14) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PA14) }, + { MP_ROM_QSTR(MP_QSTR_SLIDE_SWITCH), MP_ROM_PTR(&pin_PA15) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_PA15) }, + { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB23) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_PB23) }, + + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, + + { MP_ROM_QSTR(MP_QSTR_REMOTEIN), MP_ROM_PTR(&pin_PA12) }, + { MP_ROM_QSTR(MP_QSTR_IR_RX), MP_ROM_PTR(&pin_PA12) }, + { MP_ROM_QSTR(MP_QSTR_REMOTEOUT), MP_ROM_PTR(&pin_PA23) }, + { MP_ROM_QSTR(MP_QSTR_IR_TX), MP_ROM_PTR(&pin_PA23) }, + { MP_ROM_QSTR(MP_QSTR_IR_PROXIMITY), MP_ROM_PTR(&pin_PA04) }, + + { MP_ROM_QSTR(MP_QSTR_MICROPHONE_CLOCK), MP_ROM_PTR(&pin_PA10) }, + { MP_ROM_QSTR(MP_QSTR_MICROPHONE_DATA), MP_ROM_PTR(&pin_PA08) }, + + { MP_ROM_QSTR(MP_QSTR_ACCELEROMETER_INTERRUPT), MP_ROM_PTR(&pin_PA13) }, + { MP_ROM_QSTR(MP_QSTR_ACCELEROMETER_SDA), MP_ROM_PTR(&pin_PA00) }, + { MP_ROM_QSTR(MP_QSTR_ACCELEROMETER_SCL), MP_ROM_PTR(&pin_PA01) }, + + { MP_ROM_QSTR(MP_QSTR_SPEAKER_ENABLE), MP_ROM_PTR(&pin_PA30) }, + + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA07) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk index debd3d775c..4971ad2342 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M0 Adalogger" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk index 641611176f..2dfd8dba99 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index 44141773da..bfa8c642a2 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -1,9 +1,6 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Feather M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" -#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ -#define MP_SSIZE_MAX 0x7fffffff - #define MICROPY_HW_NEOPIXEL (&pin_PA06) // Clock rates are off: Salae reads 12MHz which is the limit even though we set it to the safer 8MHz. diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk index 202d8a6264..c24eb06734 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk index bf4f567af1..5c1cea8e0e 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M0 RFM69" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk index fbc422eef2..b0fc395649 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M0 RFM9x" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk index 340421f500..e974b921b6 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0 Supersized" USB_MANUFACTURER = "Dave Astels" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 - diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk index 2bfd0f3a09..061767c712 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Feather M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD51G19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk index a406e8f58f..01329bc32b 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Gemma M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index fea2d290b5..be59bc5d4a 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -1,9 +1,6 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Itsy Bitsy M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" -#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ -#define MP_SSIZE_MAX 0x7fffffff - #define CIRCUITPY_BITBANG_APA102 #define MICROPY_HW_APA102_MOSI (&pin_PA01) #define MICROPY_HW_APA102_SCK (&pin_PA00) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk index b3e5a636d0..d1ef58049a 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Itsy Bitsy M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk index f96661327d..00f4976a3e 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "ItsyBitsy M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD51G19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index 4825b66631..f798c5d365 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -1,9 +1,6 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Metro M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" -#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ -#define MP_SSIZE_MAX 0x7fffffff - #define MICROPY_HW_LED_TX PIN_PA27 //#define MICROPY_HW_LED_RX PIN_PA31 diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index ee1335c9a0..d449b65e19 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Metro M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk index 1d59ed9664..473690cd7c 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Metro M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk index a3d6e25221..a225067ea2 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Metro M4 Express Rev B (Black)" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk index 52dfc14309..fbfa2c9ecd 100644 --- a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "pIRKey M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk index 43d07eb5ff..65ecec0e2a 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT = "Trinket M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_NONE = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk index a904b2837c..95996085f5 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk @@ -5,6 +5,7 @@ USB_PRODUCT="Trinket M0 Haxpress" USB_MANUFACTURER="Radomir Dopieralski" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk index 343115fbb4..fee23b94d8 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk @@ -6,6 +6,7 @@ USB_PRODUCT = "uGame10" USB_MANUFACTURER = "Radomir Dopieralski" SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL_MPZ = 1 CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 8e726f9b74..626519cc10 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -140,21 +140,27 @@ typedef long mp_off_t; #define CIRCUITPY_MCU_FAMILY samd21 #define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21" #define PORT_HEAP_SIZE (16384 + 4096) -// If you change MICROPY_LONGINT_IMPL, also change MPY_TOOL_LONGINT_IMPL in mpconfigport.mk. -#ifndef MICROPY_LONGINT_IMPL -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) -#endif #endif #ifdef SAMD51 #define CIRCUITPY_MCU_FAMILY samd51 #define MICROPY_PY_SYS_PLATFORM "MicroChip SAMD51" #define PORT_HEAP_SIZE (0x20000) // 128KiB -// If you change MICROPY_LONGINT_IMPL, also change MPY_TOOL_LONGINT_IMPL in mpconfigport.mk. -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +#endif + +#ifdef LONGINT_IMPL_NONE +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) +#endif + +#ifdef LONGINT_IMPL_MPZ +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MP_SSIZE_MAX (0x7fffffff) #endif +#ifdef LONGINT_IMPL_LONGLONG +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG) +#endif + // extra built in modules to add to the list of known ones extern const struct _mp_obj_module_t microcontroller_module; extern const struct _mp_obj_module_t bitbangio_module; @@ -194,7 +200,9 @@ extern const struct _mp_obj_module_t usb_hid_module; #define MICROPY_PY_UERRNO_ERRORCODE (0) #define MICROPY_PY_URE (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) - #define MICROPY_PY_FRAMEBUF (1) + #ifndef MICROPY_PY_FRAMEBUF + #define MICROPY_PY_FRAMEBUF (1) + #endif #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 80ec9d9787..25e40af377 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -1,14 +1,21 @@ # Define an equivalent for MICROPY_LONGINT_IMPL, to pass to $(MPY-TOOL) in py/mkrules.mk # $(MPY-TOOL) needs to know what kind of longint to use (if any) to freeze long integers. # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. -ifeq ($(CHIP_FAMILY), samd21) -ifndef MPY_TOOL_LONGINT_IMPL + +ifdef LONGINT_IMPL_NONE MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none endif -endif -ifeq ($(CHIP_FAMILY), samd51) +ifdef LONGINT_IMPL_MPZ MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz endif +ifdef LONGINT_IMPL_LONGLONG +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=longlong +endif + +ifndef MPY_TOOL_LONGINT_IMPL +$(error one of LONGINT_IMPL_NONE, LONGINT_IMPL_MPZ, or LONGINT_IMPL_LONGLONG must be defined) +endif + INTERNAL_LIBM = 1 diff --git a/tools/build_adafruit_bins.sh b/tools/build_adafruit_bins.sh index 004877f8ee..4d0854a109 100755 --- a/tools/build_adafruit_bins.sh +++ b/tools/build_adafruit_bins.sh @@ -2,7 +2,7 @@ rm -rf ports/atmel-samd/build* rm -rf ports/esp8266/build* rm -rf ports/nrf/build* -ATMEL_BOARDS="arduino_zero circuitplayground_express feather_m0_basic feather_m0_adalogger itsybitsy_m0_express itsybitsy_m4_express feather_m0_rfm69 feather_m0_rfm9x feather_m0_express feather_m4_express metro_m0_express metro_m4_express pirkey_m0 trinket_m0 gemma_m0 feather52" +ATMEL_BOARDS="arduino_zero circuitplayground_express circuitplayground_express_crickit feather_m0_basic feather_m0_adalogger itsybitsy_m0_express itsybitsy_m4_express feather_m0_rfm69 feather_m0_rfm9x feather_m0_express feather_m4_express metro_m0_express metro_m4_express pirkey_m0 trinket_m0 gemma_m0 feather52" ROSIE_SETUPS="rosie-ci" PARALLEL="-j 5" From 45db48bbaae5d32c9064e12fdabc688fe79b362c Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 08:04:14 -0400 Subject: [PATCH 047/103] make longint choice mechanism more readable --- ports/atmel-samd/Makefile | 9 +++++---- ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk | 2 +- .../boards/circuitplayground_express/mpconfigboard.mk | 4 +--- .../circuitplayground_express_crickit/mpconfigboard.mk | 4 +--- .../boards/feather_m0_adalogger/mpconfigboard.mk | 2 +- .../atmel-samd/boards/feather_m0_basic/mpconfigboard.mk | 2 +- .../boards/feather_m0_express/mpconfigboard.mk | 4 +--- .../atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk | 2 +- .../atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk | 2 +- .../boards/feather_m0_supersized/mpconfigboard.mk | 2 +- .../boards/feather_m4_express/mpconfigboard.mk | 2 +- ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk | 2 +- .../boards/itsybitsy_m0_express/mpconfigboard.mk | 4 +--- .../boards/itsybitsy_m4_express/mpconfigboard.mk | 2 +- .../atmel-samd/boards/metro_m0_express/mpconfigboard.mk | 4 +--- .../atmel-samd/boards/metro_m4_express/mpconfigboard.mk | 2 +- .../boards/metro_m4_express_revb/mpconfigboard.mk | 2 +- ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk | 2 +- ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk | 2 +- .../boards/trinket_m0_haxpress/mpconfigboard.mk | 2 +- ports/atmel-samd/boards/ugame10/mpconfigboard.mk | 2 +- ports/atmel-samd/mpconfigport.mk | 9 +++------ 22 files changed, 28 insertions(+), 40 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 1fdeab5f5e..86f656a42e 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -165,16 +165,17 @@ ifndef INTERNAL_LIBM LIBS += -lm endif -# Propagate longint choice from .mk to C. -ifdef LONGINT_IMPL_NONE +# Propagate longint choice from .mk to C. There's no easy string comparison +# in cpp coniditionals, so we #define separate names for each. +ifeq ($(LONGINT_IMPL),NONE) CFLAGS += -DLONGINT_IMPL_NONE endif -ifdef LONGINT_IMPL_MPZ +ifeq ($(LONGINT_IMPL),MPZ) CFLAGS += -DLONGINT_IMPL_MPZ endif -ifdef LONGINT_IMPL_LONGLONG +ifeq ($(LONGINT_IMPL),LONGLONG) CFLAGS += -DLONGINT_IMPL_LONGLONG endif diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk index 638c76e1f0..017e66ddff 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Arduino Zero" USB_MANUFACTURER = "Arduino" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk index a3580923c2..831106adc8 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk @@ -5,13 +5,11 @@ USB_PRODUCT = "CircuitPlayground Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 -MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz - # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk index 47be11ed48..d852b4d47c 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk @@ -5,13 +5,11 @@ USB_PRODUCT = "CircuitPlayground Express with Crickit libraries" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 -MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz - # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk index 4971ad2342..4f2855d732 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0 Adalogger" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk index 2dfd8dba99..a77da91f2c 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk index c24eb06734..d4725fdf45 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk @@ -5,9 +5,7 @@ USB_PRODUCT = "Feather M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 - -MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk index 5c1cea8e0e..bd1a96479e 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0 RFM69" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk index b0fc395649..fedd8d0720 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0 RFM9x" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk index e974b921b6..79bf2d34a2 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M0 Supersized" USB_MANUFACTURER = "Dave Astels" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk index 061767c712..01385bdc6c 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Feather M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD51G19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk index 01329bc32b..ddc4287c3f 100644 --- a/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Gemma M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk index d1ef58049a..66586b6111 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk @@ -5,9 +5,7 @@ USB_PRODUCT = "Itsy Bitsy M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 - -MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk index 00f4976a3e..27f5ea2b88 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "ItsyBitsy M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD51G19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index d449b65e19..0388a408bc 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -5,9 +5,7 @@ USB_PRODUCT = "Metro M0 Express" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 - -MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk index 473690cd7c..38e8a12d72 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Metro M4 Express" USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk index a225067ea2..9e6965063c 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Metro M4 Express Rev B (Black)" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk index fbfa2c9ecd..87b46c642f 100644 --- a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "pIRKey M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk index 65ecec0e2a..c9c45152be 100644 --- a/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT = "Trinket M0" USB_MANUFACTURER = "Adafruit Industries LLC" INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_NONE = 1 +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk index 95996085f5..d97aa08ab4 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk @@ -5,7 +5,7 @@ USB_PRODUCT="Trinket M0 Haxpress" USB_MANUFACTURER="Radomir Dopieralski" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk index fee23b94d8..f30434025d 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.mk @@ -6,7 +6,7 @@ USB_PRODUCT = "uGame10" USB_MANUFACTURER = "Radomir Dopieralski" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL_MPZ = 1 +LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 25e40af377..e7aeb08058 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -2,20 +2,17 @@ # $(MPY-TOOL) needs to know what kind of longint to use (if any) to freeze long integers. # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. -ifdef LONGINT_IMPL_NONE +ifeq ($(LONGINT_IMPL),NONE) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none endif -ifdef LONGINT_IMPL_MPZ +ifeq ($(LONGINT_IMPL),MPZ) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz endif -ifdef LONGINT_IMPL_LONGLONG +ifeq ($(LONGINT_IMPL),LONGONG) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=longlong endif -ifndef MPY_TOOL_LONGINT_IMPL -$(error one of LONGINT_IMPL_NONE, LONGINT_IMPL_MPZ, or LONGINT_IMPL_LONGLONG must be defined) -endif INTERNAL_LIBM = 1 From 49a81d5c4bd432e5a62049f77f6278a79d9cb3ac Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 09:35:29 -0400 Subject: [PATCH 048/103] LONGLONG typo --- ports/atmel-samd/mpconfigport.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index e7aeb08058..c2eedfdc31 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -10,7 +10,7 @@ ifeq ($(LONGINT_IMPL),MPZ) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz endif -ifeq ($(LONGINT_IMPL),LONGONG) +ifeq ($(LONGINT_IMPL),LONGLONG) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=longlong endif From 0d7db296307d35a0b5b862b2117ea65056250441 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 13:03:06 -0400 Subject: [PATCH 049/103] turn off longint on cpx crickit; update frozen libs --- frozen/Adafruit_CircuitPython_BusDevice | 2 +- frozen/Adafruit_CircuitPython_HID | 2 +- frozen/Adafruit_CircuitPython_LIS3DH | 2 +- .../boards/circuitplayground_express_crickit/mpconfigboard.mk | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/frozen/Adafruit_CircuitPython_BusDevice b/frozen/Adafruit_CircuitPython_BusDevice index de2d0d33ca..376a80e62a 160000 --- a/frozen/Adafruit_CircuitPython_BusDevice +++ b/frozen/Adafruit_CircuitPython_BusDevice @@ -1 +1 @@ -Subproject commit de2d0d33cace54532467e411128521dfc8d253c3 +Subproject commit 376a80e62acb3ba8b9a1e5c11bb75d36352f2190 diff --git a/frozen/Adafruit_CircuitPython_HID b/frozen/Adafruit_CircuitPython_HID index 90e4ca931a..058c94513e 160000 --- a/frozen/Adafruit_CircuitPython_HID +++ b/frozen/Adafruit_CircuitPython_HID @@ -1 +1 @@ -Subproject commit 90e4ca931a991718985e655bdcd527c1b0543f55 +Subproject commit 058c94513e41249eac72deb3abc7af4f7d96a4f6 diff --git a/frozen/Adafruit_CircuitPython_LIS3DH b/frozen/Adafruit_CircuitPython_LIS3DH index d5491cded0..bdd1478330 160000 --- a/frozen/Adafruit_CircuitPython_LIS3DH +++ b/frozen/Adafruit_CircuitPython_LIS3DH @@ -1 +1 @@ -Subproject commit d5491cded0d12716ceb1111ca4c4431111a22df6 +Subproject commit bdd1478330b41017b09de58fca213218c36f2821 diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk index d852b4d47c..0145bf7850 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk @@ -5,7 +5,8 @@ USB_PRODUCT = "CircuitPlayground Express with Crickit libraries" USB_MANUFACTURER = "Adafruit Industries LLC" SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL = MPZ +# Turn off longints for Crickit build to make room for additional frozen libs. +LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 From 99123a8621d2f3931ee38bdb84b39594a11a09e1 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 2 May 2018 10:18:14 -0700 Subject: [PATCH 050/103] Turn on touchio for M0 boards. M4 will come later once its supported by FreeTouch. Fixes #670 --- ports/atmel-samd/Makefile | 4 +-- ports/atmel-samd/common-hal/touchio/TouchIn.c | 32 +++++++++++++++---- ports/atmel-samd/common-hal/touchio/TouchIn.h | 1 + ports/atmel-samd/freetouch | 2 +- ports/atmel-samd/mpconfigport.h | 8 ++++- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 86f656a42e..ecc8a28a40 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -274,7 +274,7 @@ SRC_C = \ lib/libc/string0.c \ lib/mp-readline/readline.c \ $(BUILD)/autogen_usb_descriptor.c \ - # freetouch/adafruit_ptc.c + freetouch/adafruit_ptc.c # Choose which flash filesystem impl to use. # (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive. @@ -321,7 +321,7 @@ SRC_COMMON_HAL = \ usb_hid/Device.c \ audioio/__init__.c \ audioio/AudioOut.c \ -# touchio/__init__.c \ + touchio/__init__.c \ touchio/TouchIn.c \ ifeq ($(INTERNAL_LIBM),1) diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.c b/ports/atmel-samd/common-hal/touchio/TouchIn.c index 6bb96ff00e..c116bb3eb3 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.c +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.c @@ -33,11 +33,18 @@ #include "py/mphal.h" #include "shared-bindings/touchio/TouchIn.h" +#ifdef SAMD21 +#include "hpl/pm/hpl_pm_base.h" +#endif + +#include "clocks.h" #include "samd21_pins.h" #include "tick.h" #include "adafruit_ptc.h" +bool touch_enabled = false; + static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { adafruit_ptc_start_conversion(PTC, &self->config); @@ -58,13 +65,21 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, } claim_pin(pin); - /* Setup and enable generic clock source for PTC module. */ - struct system_gclk_chan_config gclk_chan_conf; - system_gclk_chan_get_config_defaults(&gclk_chan_conf); - gclk_chan_conf.source_generator = GCLK_GENERATOR_3; - system_gclk_chan_set_config(PTC_GCLK_ID, &gclk_chan_conf); - system_gclk_chan_enable(PTC_GCLK_ID); - system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, PM_APBCMASK_PTC); + // Turn on the PTC if its not in use. We won't turn it off until reset. + #ifdef SAMD21 + if ((( Ptc *) PTC)->CTRLA.bit.ENABLE == 0) { + // We run the PTC at 8mhz so divide the 48mhz clock by 6. + uint8_t gclk = find_free_gclk(6); + if (gclk > GCLK_GEN_NUM) { + mp_raise_RuntimeError("No free GCLKs"); + } + enable_clock_generator(self->gclk, CLOCK_48MHZ, 6); + + /* Setup and enable generic clock source for PTC module. */ + connect_gclk_to_peripheral(self->gclk, PTC_GCLK_ID); + + _pm_enable_bus_clock(PM_BUS_APBC, PTC); + } adafruit_ptc_get_config_default(&self->config); self->config.pin = pin->pin; @@ -80,6 +95,7 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, // but for touches using fruit or other objects, the difference is much less. self->threshold = get_raw_reading(self) + 100; + #endif } bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) { @@ -91,6 +107,8 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { if (common_hal_touchio_touchin_deinited(self)) { return; } + // We leave the clocks running because they may be in use by others.s + reset_pin(self->config.pin); self->config.pin = NO_PIN; } diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.h b/ports/atmel-samd/common-hal/touchio/TouchIn.h index ffe8a6c017..17b3116a37 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.h +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.h @@ -38,6 +38,7 @@ typedef struct { mp_obj_base_t base; struct adafruit_ptc_config config; uint16_t threshold; + uint8_t gclk; } touchio_touchin_obj_t; void touchin_reset(void); diff --git a/ports/atmel-samd/freetouch b/ports/atmel-samd/freetouch index c3deba11eb..5d8c33e084 160000 --- a/ports/atmel-samd/freetouch +++ b/ports/atmel-samd/freetouch @@ -1 +1 @@ -Subproject commit c3deba11eb4be397ec719cfbfba1abcaecda2c08 +Subproject commit 5d8c33e084996d48134eaf7d4b200d1a806cb9ee diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index e1683f234e..0d07e0009e 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -238,9 +238,14 @@ extern const struct _mp_obj_module_t usb_hid_module; #endif // Disabled for now. -// { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module }, +#ifdef SAMD21 +#define TOUCHIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, +#endif +#ifdef SAMD51 +#define TOUCHIO_MODULE +#endif #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \ @@ -260,6 +265,7 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \ + TOUCHIO_MODULE \ EXTRA_BUILTIN_MODULES #define MICROPY_PORT_BUILTIN_DEBUG_MODULES \ From 31bcd1c45c811a6b2fe7b8ba31e1701a7705bfe1 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 22 May 2018 13:56:12 -0700 Subject: [PATCH 051/103] Fixup gclk init --- ports/atmel-samd/common-hal/touchio/TouchIn.c | 6 +++--- ports/atmel-samd/common-hal/touchio/TouchIn.h | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.c b/ports/atmel-samd/common-hal/touchio/TouchIn.c index c116bb3eb3..0677d5b3a1 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.c +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.c @@ -73,10 +73,10 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, if (gclk > GCLK_GEN_NUM) { mp_raise_RuntimeError("No free GCLKs"); } - enable_clock_generator(self->gclk, CLOCK_48MHZ, 6); + enable_clock_generator(gclk, CLOCK_48MHZ, 6); /* Setup and enable generic clock source for PTC module. */ - connect_gclk_to_peripheral(self->gclk, PTC_GCLK_ID); + connect_gclk_to_peripheral(gclk, PTC_GCLK_ID); _pm_enable_bus_clock(PM_BUS_APBC, PTC); } @@ -107,7 +107,7 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { if (common_hal_touchio_touchin_deinited(self)) { return; } - // We leave the clocks running because they may be in use by others.s + // We leave the clocks running because they may be in use by others. reset_pin(self->config.pin); self->config.pin = NO_PIN; diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.h b/ports/atmel-samd/common-hal/touchio/TouchIn.h index 17b3116a37..ffe8a6c017 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.h +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.h @@ -38,7 +38,6 @@ typedef struct { mp_obj_base_t base; struct adafruit_ptc_config config; uint16_t threshold; - uint8_t gclk; } touchio_touchin_obj_t; void touchin_reset(void); From 3607d3ba2c3646653e48302d0aa51b30408b40ee Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 22 May 2018 14:20:35 -0700 Subject: [PATCH 052/103] Correctly reset the PTC --- ports/atmel-samd/common-hal/touchio/TouchIn.c | 9 ++++++++- ports/atmel-samd/supervisor/port.c | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.c b/ports/atmel-samd/common-hal/touchio/TouchIn.c index 0677d5b3a1..eda1b72c43 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.c +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.c @@ -114,7 +114,14 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { } void touchin_reset() { - // TODO(tannewt): Reset the PTC. + Ptc* ptc = ((Ptc *) PTC); + if (ptc->CTRLA.bit.ENABLE == 1) { + ptc->CTRLA.bit.ENABLE = 0; + while (ptc->CTRLA.bit.ENABLE == 1) {} + + ptc->CTRLA.bit.SWRESET = 1; + while (ptc->CTRLA.bit.SWRESET == 1) {} + } } bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 8e6f2b1857..cd122843de 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -53,6 +53,7 @@ #include "common-hal/pulseio/PulseOut.h" #include "common-hal/pulseio/PWMOut.h" #include "common-hal/rtc/RTC.h" +#include "common-hal/touchio/TouchIn.h" #include "common-hal/usb_hid/Device.h" #include "shared-bindings/rtc/__init__.h" #include "clocks.h" @@ -240,8 +241,10 @@ void reset_port(void) { i2sout_reset(); #endif audio_dma_reset(); -// touchin_reset(); //pdmin_reset(); +#endif +#ifdef SAMD21 + touchin_reset(); #endif pulsein_reset(); pulseout_reset(); From 783ffe6824cef7ed98454bc125de9aba276f642d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 22 May 2018 14:32:27 -0700 Subject: [PATCH 053/103] Update CONTRIBUTING.md to include new learn guides and Dan and Kattni's info. Fixes #858 --- CONTRIBUTING.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2eb0214da4..6feed085c5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,12 +5,6 @@ By participating in this project you agree to abide by its terms. Participation covers any forum used to converse about CircuitPython including unofficial and official spaces. Failure to do so will result in corrective actions such as time out or ban from the project. -## Developer contact -[@tannewt](https://github.com/tannewt) is the main developer of CircuitPython -and is sponsored by [Adafruit Industries LLC](https://adafruit.com). He is -reachable on [Discord](https://adafru.it/discord) as tannewt during US West Coast -working hours. He also checks GitHub issues and the [Adafruit support forum](https://forums.adafruit.com/viewforum.php?f=60). - ## Licensing By contributing to this repository you are certifying that you have all necessary permissions to license the code under an MIT License. You still retain the @@ -20,6 +14,23 @@ If you have an employment contract with your employer please make sure that they don't automatically own your work product. Make sure to get any necessary approvals before contributing. Another term for this contribution off-hours is moonlighting. +## Getting started +CircuitPython developer Dan Halbert (@dhalbert) has written up build instructions using native build +tools [here](https://learn.adafruit.com/building-circuitpython). + +For SAMD21 debugging workflow tips check out [this learn guide](https://learn.adafruit.com/debugging-the-samd21-with-gdb) from Scott (@tannewt). + +## Developer contacts +Scott Shawcroft ([@tannewt](https://github.com/tannewt)) is the lead developer of CircuitPython +and is sponsored by [Adafruit Industries LLC](https://adafruit.com). Scott is usually available +during US West Coast working hours. Dan Halbert ([@dhalbert](https://github.com/dhalbert)) and +Kattni Rembor ([@kattni](https://github.com/kattni)) are also sponsored by [Adafruit Industries + LLC](https://adafruit.com) and are usually available during US East Coast daytime hours including +some weekends. + +They are all reachable on [Discord](https://adafru.it/discord), GitHub issues and the [Adafruit +support forum](https://forums.adafruit.com/viewforum.php?f=60). + ## Code guidelines We aim to keep our code and commit style compatible with MicroPython upstream. Please review their From e65cc077640881baedae1dcea0b8fcc1886d0f88 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 19:52:01 -0400 Subject: [PATCH 054/103] RTD documentation updates --- README.rst | 10 +++-- docs/library/array.rst | 2 - docs/library/builtins.rst | 18 ++++----- docs/library/gc.rst | 8 ++-- docs/library/index.rst | 50 +++++++++++++++++------- docs/library/{ustruct.rst => struct.rst} | 6 +-- docs/library/sys.rst | 20 +++++----- 7 files changed, 66 insertions(+), 48 deletions(-) rename docs/library/{ustruct.rst => struct.rst} (90%) diff --git a/README.rst b/README.rst index 6017be207c..ace4ac1889 100644 --- a/README.rst +++ b/README.rst @@ -103,10 +103,14 @@ Differences from `MicroPython `__ CircuitPython: -- includes a port for Atmel SAMD21 (Commonly known as M0 in Adafruit - product names.) -- supports only Atmel SAMD21 and ESP8266 ports. +- includes a ports for MicroChip SAMD21 (Commonly known as M0 in Adafruit + product names and SAMD51 (M4). +- supports only SAMD21, SAMD51, and ESP8266 ports. An nRF port is under + development. - tracks MicroPython's releases (not master). +- Longints (arbitrary-length integers) are turned on for most Express builds + (those boards with SPI flash chips external to the microcontroller), + but not for boards without SPI flash, for space reasons. Behavior ~~~~~~~~ diff --git a/docs/library/array.rst b/docs/library/array.rst index dfaef0ff6c..0f1310b687 100644 --- a/docs/library/array.rst +++ b/docs/library/array.rst @@ -1,8 +1,6 @@ :mod:`array` -- arrays of numeric data ====================================== -.. include:: ../templates/unsupported_in_circuitpython.inc - .. module:: array :synopsis: efficient arrays of numeric data diff --git a/docs/library/builtins.rst b/docs/library/builtins.rst index 83246afc92..c60ade0406 100644 --- a/docs/library/builtins.rst +++ b/docs/library/builtins.rst @@ -1,18 +1,14 @@ Builtin functions and exceptions ================================ -.. warning:: - - These builtins are inherited from MicroPython and may not work in CircuitPython - as documented or at all! If work differently from CPython, then their behavior - may change. - All builtin functions and exceptions are described here. They are also available via ``builtins`` module. Functions and types ------------------- +Not all of these functions and types are turned on in all CircuitPython ports, for space reasons. + .. function:: abs() .. function:: all() @@ -62,6 +58,8 @@ Functions and types .. class:: frozenset() +`frozenset() is not enabled on non-Express CircuitPython boards.` + .. function:: getattr() .. function:: globals() @@ -80,12 +78,12 @@ Functions and types .. classmethod:: from_bytes(bytes, byteorder) - In MicroPython, `byteorder` parameter must be positional (this is + In CircuitPython, `byteorder` parameter must be positional (this is compatible with CPython). .. method:: to_bytes(size, byteorder) - In MicroPython, `byteorder` parameter must be positional (this is + In CircuitPython, `byteorder` parameter must be positional (this is compatible with CPython). .. function:: isinstance() @@ -130,6 +128,8 @@ Functions and types .. function:: reversed() +`reversed() is not enabled on non-Express CircuitPython boards.` + .. function:: round() .. class:: set() @@ -182,7 +182,7 @@ Exceptions .. exception:: OSError - |see_cpython| `OSError`. MicroPython doesn't implement ``errno`` + |see_cpython| `OSError`. CircuitPython doesn't implement the ``errno`` attribute, instead use the standard way to access exception arguments: ``exc.args[0]``. diff --git a/docs/library/gc.rst b/docs/library/gc.rst index 01bd925e99..ba25d788f2 100644 --- a/docs/library/gc.rst +++ b/docs/library/gc.rst @@ -1,8 +1,6 @@ :mod:`gc` -- control the garbage collector ========================================== -.. include:: ../templates/unsupported_in_circuitpython.inc - .. module:: gc :synopsis: control the garbage collector @@ -31,7 +29,7 @@ Functions .. admonition:: Difference to CPython :class: attention - This function is MicroPython extension. + This function is a MicroPython extension. .. function:: mem_free() @@ -41,7 +39,7 @@ Functions .. admonition:: Difference to CPython :class: attention - This function is MicroPython extension. + This function is a MicroPython extension. .. function:: threshold([amount]) @@ -63,6 +61,6 @@ Functions .. admonition:: Difference to CPython :class: attention - This function is a MicroPython extension. CPython has a similar + This function is a a MicroPython extension. CPython has a similar function - ``set_threshold()``, but due to different GC implementations, its signature and semantics are different. diff --git a/docs/library/index.rst b/docs/library/index.rst index c7b6879aa4..3d2703c0a9 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -1,44 +1,66 @@ .. _micropython_lib: -MicroPython libraries +CircuitPython libraries ===================== -.. warning:: - - These modules are inherited from MicroPython and may not work in CircuitPython - as documented or at all! If they do work, they may change at any time. - - Python standard libraries and micro-libraries --------------------------------------------- +These libraries are the same or are subsets or slight variants of the standard Python libraries. + +MicroPython prefixed many of these libraries with ``u``. In CircuitPython, those +that are subsets or the same as the standard Python libraries have been or will be renamed +to their original names. +Our aspiration is that code written in CircuitPython +that uses Python standard libraries will be runnable on CPython without changes. +But we may fall short of this goal in some cases. + +Some of the libraries below are not enabled on CircuitPython builds with +limited flash memory, usually on non-Express builds: +``uerrno``, ``ure``. + +Some libraries are not currently enabled in any CircuitPython build, but may be in the future: +``uio``, ``ujson``, ``uzlib``. + +Some libraries are only enabled only WiFi-capable ports (ESP8266, nRF) +because they are typically used for network software: +``binascii``, ``hashlib``, ``uheapq``, ``uselect``, ``ussl``. +Not all of these are enabled on all WiFi-capable ports. .. toctree:: :maxdepth: 1 builtins.rst + uheapq.rst array.rst - gc.rst - sys.rst binascii.rst collections.rst - uerrno.rst + gc.rst hashlib.rst - uheapq.rst + struct.rst + sys.rst + uerrno.rst uio.rst ujson.rst ure.rst uselect.rst usocket.rst ussl.rst - ustruct.rst uzlib.rst +Omitted functions in the ``string`` library +------------------------------------------- -MicroPython-specific libraries +A few string operations are not enabled on CircuitPython builds with +limited flash memory, usually on non-Express builds: +``string.center()``, ``string.partition()``, ``string.splitlines()``, +``string.reversed()``. + + +CircuitPython/MicroPython-specific libraries ------------------------------ -Functionality specific to the MicroPython implementation is available in +Functionality specific to the CircuitPython (MicroPython) implementation is available in the following libraries. .. toctree:: diff --git a/docs/library/ustruct.rst b/docs/library/struct.rst similarity index 90% rename from docs/library/ustruct.rst rename to docs/library/struct.rst index c378a94bbe..fd953473af 100644 --- a/docs/library/ustruct.rst +++ b/docs/library/struct.rst @@ -1,9 +1,7 @@ -:mod:`ustruct` -- pack and unpack primitive data types +:mod:`struct` -- pack and unpack primitive data types ====================================================== -.. include:: ../templates/unsupported_in_circuitpython.inc - -.. module:: ustruct +.. module:: struct :synopsis: pack and unpack primitive data types |see_cpython_module| :mod:`cpython:struct`. diff --git a/docs/library/sys.rst b/docs/library/sys.rst index de2ec2dcd2..eeda578e6b 100644 --- a/docs/library/sys.rst +++ b/docs/library/sys.rst @@ -1,8 +1,6 @@ :mod:`sys` -- system specific functions ======================================= -.. include:: ../templates/unsupported_in_circuitpython.inc - .. module:: sys :synopsis: system specific functions @@ -45,12 +43,12 @@ Constants .. data:: implementation Object with information about the current Python implementation. For - MicroPython, it has following attributes: + CircuitPython, it has following attributes: - * *name* - string "micropython" + * *name* - string "circuitpython" * *version* - tuple (major, minor, micro), e.g. (1, 7, 0) - This object is the recommended way to distinguish MicroPython from other + This object is the recommended way to distinguish CircuitPython from other Python implementations (note that it still may not exist in the very minimal ports). @@ -58,13 +56,13 @@ Constants :class: attention CPython mandates more attributes for this object, but the actual useful - bare minimum is implemented in MicroPython. + bare minimum is implemented in CircuitPython. .. data:: maxsize Maximum value which a native integer type can hold on the current platform, - or maximum value representable by MicroPython integer type, if it's smaller - than platform max value (that is the case for MicroPython ports without + or maximum value representable by CircuitPython integer type, if it's smaller + than platform max value (that is the case for CircuitPython ports without long int support). This attribute is useful for detecting "bitness" of a platform (32-bit vs @@ -96,10 +94,10 @@ Constants .. data:: platform - The platform that MicroPython is running on. For OS/RTOS ports, this is + The platform that CircuitPython is running on. For OS/RTOS ports, this is usually an identifier of the OS, e.g. ``"linux"``. For baremetal ports it - is an identifier of a board, e.g. ``"pyboard"`` for the original MicroPython - reference board. It thus can be used to distinguish one board from another. + is an identifier of the chip on a board, e.g. ``"MicroChip SAMD51"``. + It thus can be used to distinguish one board from another. If you need to check whether your program runs on MicroPython (vs other Python implementation), use `sys.implementation` instead. From 59615fc9938e0e7c7cf8141069f5eaaba05b18eb Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 20:49:08 -0400 Subject: [PATCH 055/103] add caveat language re MicroPython libraries; other slight changes --- README.rst | 7 ++-- docs/library/gc.rst | 2 ++ docs/library/index.rst | 36 ++++++++++++------- docs/library/struct.rst | 2 ++ docs/library/sys.rst | 4 ++- .../unsupported_in_circuitpython.inc | 9 +++-- 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/README.rst b/README.rst index ace4ac1889..a1b90fa02a 100644 --- a/README.rst +++ b/README.rst @@ -108,9 +108,10 @@ CircuitPython: - supports only SAMD21, SAMD51, and ESP8266 ports. An nRF port is under development. - tracks MicroPython's releases (not master). -- Longints (arbitrary-length integers) are turned on for most Express builds - (those boards with SPI flash chips external to the microcontroller), - but not for boards without SPI flash, for space reasons. +- Longints (arbitrary-length integers) are enabled for most M0 + Express boards (those boards with SPI flash chips external + to the microcontroller), and for all M4 builds. + Longints are disabled on other boards due to lack of flash space. Behavior ~~~~~~~~ diff --git a/docs/library/gc.rst b/docs/library/gc.rst index ba25d788f2..1a6c3d68c0 100644 --- a/docs/library/gc.rst +++ b/docs/library/gc.rst @@ -1,6 +1,8 @@ :mod:`gc` -- control the garbage collector ========================================== +.. include:: ../templates/unsupported_in_circuitpython.inc + .. module:: gc :synopsis: control the garbage collector diff --git a/docs/library/index.rst b/docs/library/index.rst index 3d2703c0a9..3156f9352f 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -1,21 +1,30 @@ .. _micropython_lib: -CircuitPython libraries +MicroPython libraries ===================== Python standard libraries and micro-libraries --------------------------------------------- -These libraries are the same or are subsets or slight variants of the standard Python libraries. +These libraries are inherited from MicroPython. +They are similar to the standard Python libraries with the same name +or with the "u" prefix dropped. +They implement a subset of or a variant of the corresponding +standard Python library. -MicroPython prefixed many of these libraries with ``u``. In CircuitPython, those -that are subsets or the same as the standard Python libraries have been or will be renamed -to their original names. -Our aspiration is that code written in CircuitPython -that uses Python standard libraries will be runnable on CPython without changes. -But we may fall short of this goal in some cases. +.. warning:: -Some of the libraries below are not enabled on CircuitPython builds with + Though these MicroPython-based libraries are available in CircuitPython, + their functionality may change in the future, perhaps significantly. + As CircuitPython continues to develop, new versions of these libraries will + be created that are more compliant with the standard Python libraries. + You may need to change your code later if you rely + on any non-standard functionality they currently provide. + +CircuitPython's goal long-term goalis that code written in CircuitPython +using Python standard libraries will be runnable on CPython without changes. + +Some libraries below are not enabled on CircuitPython builds with limited flash memory, usually on non-Express builds: ``uerrno``, ``ure``. @@ -51,8 +60,8 @@ Not all of these are enabled on all WiFi-capable ports. Omitted functions in the ``string`` library ------------------------------------------- -A few string operations are not enabled on CircuitPython builds with -limited flash memory, usually on non-Express builds: +A few string operations are not enabled on CircuitPython +M0 non-Express builds, due to limited flash memory: ``string.center()``, ``string.partition()``, ``string.splitlines()``, ``string.reversed()``. @@ -60,8 +69,9 @@ limited flash memory, usually on non-Express builds: CircuitPython/MicroPython-specific libraries ------------------------------ -Functionality specific to the CircuitPython (MicroPython) implementation is available in -the following libraries. +Functionality specific to the CircuitPython/MicroPython implementation is available in +the following libraries. These libraries may change signficantly or be removed in future +versions of CircuitPtyon. .. toctree:: :maxdepth: 1 diff --git a/docs/library/struct.rst b/docs/library/struct.rst index fd953473af..bee5a612bc 100644 --- a/docs/library/struct.rst +++ b/docs/library/struct.rst @@ -1,6 +1,8 @@ :mod:`struct` -- pack and unpack primitive data types ====================================================== +.. include:: ../templates/unsupported_in_circuitpython.inc + .. module:: struct :synopsis: pack and unpack primitive data types diff --git a/docs/library/sys.rst b/docs/library/sys.rst index eeda578e6b..bb92850b89 100644 --- a/docs/library/sys.rst +++ b/docs/library/sys.rst @@ -1,6 +1,8 @@ :mod:`sys` -- system specific functions ======================================= +.. include:: ../templates/unsupported_in_circuitpython.inc + .. module:: sys :synopsis: system specific functions @@ -98,7 +100,7 @@ Constants usually an identifier of the OS, e.g. ``"linux"``. For baremetal ports it is an identifier of the chip on a board, e.g. ``"MicroChip SAMD51"``. It thus can be used to distinguish one board from another. - If you need to check whether your program runs on MicroPython (vs other + If you need to check whether your program runs on CircuitPython (vs other Python implementation), use `sys.implementation` instead. .. data:: stderr diff --git a/docs/templates/unsupported_in_circuitpython.inc b/docs/templates/unsupported_in_circuitpython.inc index 18c9215a84..1ed605d503 100644 --- a/docs/templates/unsupported_in_circuitpython.inc +++ b/docs/templates/unsupported_in_circuitpython.inc @@ -1,5 +1,8 @@ .. warning:: - This module is inherited from MicroPython and may not work in CircuitPython - as documented or at all! If they do work, they may change at any time. It is - unsupported. + Though this MicroPython-based library is available for use in CircuitPython, + its functionality may change in the future, perhaps significantly. + As CircuitPython continues to develop, it may be changed + to comply more closely with the corresponding standard Python library. + You may need to change your code later if you rely + on any non-standard functionality it currently provides. From 20a13fa9fb1ed7b9e23aa9272132b97d7082cbd1 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 22 May 2018 21:21:19 -0400 Subject: [PATCH 056/103] fix title underline --- docs/library/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/index.rst b/docs/library/index.rst index 3156f9352f..b14d7d2f80 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -67,7 +67,7 @@ M0 non-Express builds, due to limited flash memory: CircuitPython/MicroPython-specific libraries ------------------------------- +-------------------------------------------- Functionality specific to the CircuitPython/MicroPython implementation is available in the following libraries. These libraries may change signficantly or be removed in future From 52d05bbc37d063a1fdc359b18b9b112665f92276 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 23 May 2018 11:26:16 +0200 Subject: [PATCH 057/103] Cache pullup state in gamepad Don't check the pin's pull direction on every tick, instead cache it at the beginning. Also avoid a "can't get pull of output pin" error when one of the pins passed is in output mode. --- shared-bindings/gamepad/GamePad.c | 2 +- shared-module/gamepad/GamePad.c | 11 +++++++++-- shared-module/gamepad/GamePad.h | 3 ++- shared-module/gamepad/__init__.c | 11 ++++++----- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index d80d1fcf65..43efc3b186 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -31,7 +31,7 @@ #include "GamePad.h" -gamepad_obj_t* gamepad_singleton = NULL; +gamepad_obj_t* volatile gamepad_singleton = NULL; //| .. currentmodule:: gamepad //| diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index cd1df9b574..540c882dac 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -38,14 +38,21 @@ void gamepad_init(size_t n_pins, const mp_obj_t* pins) { for (size_t i=0; i<8; ++i) { gamepad_singleton->pins[i] = NULL; } + gamepad_singleton->pulls = 0; for (size_t i=0; ipulls |= 1 << i; + } gamepad_singleton->pins[i] = pin; } gamepad_singleton->last = 0; diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index e5f709134d..77fdb8b0af 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -36,9 +36,10 @@ typedef struct { digitalio_digitalinout_obj_t* pins[8]; volatile uint8_t last; volatile uint8_t pressed; + uint8_t pulls; } gamepad_obj_t; -extern gamepad_obj_t* gamepad_singleton; +extern gamepad_obj_t* volatile gamepad_singleton; void gamepad_init(size_t n_pins, const mp_obj_t* pins); diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 591a2fa0a0..b1cdac2a70 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -37,17 +37,18 @@ void gamepad_tick(void) { return; } uint8_t gamepad_current = 0; - for (int i=0; i<8; ++i) { + uint8_t bit = 1; + for (int i = 0; i < 8; ++i) { digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i]; if (!pin) { break; } - digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin); - bool value = common_hal_digitalio_digitalinout_get_value(pin); - if ((pull == PULL_UP && !value) || (pull == PULL_DOWN && value)) { - gamepad_current |= 1 << i; + if (common_hal_digitalio_digitalinout_get_value(pin)) { + gamepad_current |= bit; } + bit <<= 1; } + gamepad_current ^= gamepad_singleton->pulls; gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current; gamepad_singleton->last = gamepad_current; } From edf2935ab166d3db765eec600ffccaedee79e5a5 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 23 May 2018 12:44:22 +0200 Subject: [PATCH 058/103] Make gamepad.get_pressed work when gamepad was created from frozen code For some reason, when the GamePad is created from frozen code, the get_pressed method would always return 0. This fixes it, and makes it work properly no matter how the object was created. --- shared-bindings/gamepad/GamePad.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 43efc3b186..2c982c7d55 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -119,9 +119,8 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, //| held down) can be recorded for the next call. //| STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { - gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); - self->pressed = 0; + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed); + gamepad_singleton->pressed = 0; return gamepad; } MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); From 641caaa6dd4d600be22219d4f5105ef84a96794b Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 23 May 2018 10:36:59 -0700 Subject: [PATCH 059/103] Gain space back in non-Express builds by using -finline-limit --- ports/atmel-samd/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index ecc8a28a40..d627e76410 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -112,8 +112,7 @@ else # -finline-limit=80 or so is similar to not having it on. # There is no simple default value, though. ifdef INTERNAL_FLASH_FILESYSTEM - ## Not currently needed - ## CFLAGS += -finline-limit=50 + CFLAGS += -finline-limit=60 endif CFLAGS += -flto endif From 079c20e58e2bb1ce8859cea99b951f6a3605e80b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 23 May 2018 14:24:02 -0400 Subject: [PATCH 060/103] use shared-bindings struct doc; remove docs/library one --- docs/library/index.rst | 1 - docs/library/struct.rst | 44 ----------------------------------------- 2 files changed, 45 deletions(-) delete mode 100644 docs/library/struct.rst diff --git a/docs/library/index.rst b/docs/library/index.rst index b14d7d2f80..6c2e576e7d 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -46,7 +46,6 @@ Not all of these are enabled on all WiFi-capable ports. collections.rst gc.rst hashlib.rst - struct.rst sys.rst uerrno.rst uio.rst diff --git a/docs/library/struct.rst b/docs/library/struct.rst deleted file mode 100644 index bee5a612bc..0000000000 --- a/docs/library/struct.rst +++ /dev/null @@ -1,44 +0,0 @@ -:mod:`struct` -- pack and unpack primitive data types -====================================================== - -.. include:: ../templates/unsupported_in_circuitpython.inc - -.. module:: struct - :synopsis: pack and unpack primitive data types - -|see_cpython_module| :mod:`cpython:struct`. - -Supported size/byte order prefixes: ``@``, ``<``, ``>``, ``!``. - -Supported format codes: ``b``, ``B``, ``h``, ``H``, ``i``, ``I``, ``l``, -``L``, ``q``, ``Q``, ``s``, ``P``, ``f``, ``d`` (the latter 2 depending -on the floating-point support). - -Functions ---------- - -.. function:: calcsize(fmt) - - Return the number of bytes needed to store the given *fmt*. - -.. function:: pack(fmt, v1, v2, ...) - - Pack the values *v1*, *v2*, ... according to the format string *fmt*. - The return value is a bytes object encoding the values. - -.. function:: pack_into(fmt, buffer, offset, v1, v2, ...) - - Pack the values *v1*, *v2*, ... according to the format string *fmt* - into a *buffer* starting at *offset*. *offset* may be negative to count - from the end of *buffer*. - -.. function:: unpack(fmt, data) - - Unpack from the *data* according to the format string *fmt*. - The return value is a tuple of the unpacked values. - -.. function:: unpack_from(fmt, data, offset=0) - - Unpack from the *data* starting at *offset* according to the format string - *fmt*. *offset* may be negative to count from the end of *buffer*. The return - value is a tuple of the unpacked values. From 27551cbb4d8df81e8516b21b1bd85ee56b92b1ea Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 23 May 2018 11:53:00 -0700 Subject: [PATCH 061/103] Fix ReadTheDocs build due to c2rst. --- docs/c2rst.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/c2rst.py b/docs/c2rst.py index 9ca8bc1bb3..904fb74084 100644 --- a/docs/c2rst.py +++ b/docs/c2rst.py @@ -7,6 +7,7 @@ class CStrip(sphinx.parsers.Parser): def parse(self, inputstring, document): # This setting is missing starting with Sphinx 1.7.1 so we set it ourself. document.settings.tab_width = 4 + document.settings.character_level_inline_markup = False stripped = [] for line in inputstring.split("\n"): line = line.strip() From aa86a1457ff1fba204432de05745be609287b4a5 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 23 May 2018 11:55:38 -0700 Subject: [PATCH 062/103] Use merged in FreeTouch --- ports/atmel-samd/freetouch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/freetouch b/ports/atmel-samd/freetouch index 5d8c33e084..b6859a349e 160000 --- a/ports/atmel-samd/freetouch +++ b/ports/atmel-samd/freetouch @@ -1 +1 @@ -Subproject commit 5d8c33e084996d48134eaf7d4b200d1a806cb9ee +Subproject commit b6859a349efb79e2008d6f6a34e406e93a8e19c0 From 6a63ff096a553dd3b6de29a8cc89667fc434f36e Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 23 May 2018 15:05:38 -0400 Subject: [PATCH 063/103] ustruct -> struct in design_guide.rst --- docs/design_guide.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/design_guide.rst b/docs/design_guide.rst index a1524eaa2b..e7b3832bcb 100644 --- a/docs/design_guide.rst +++ b/docs/design_guide.rst @@ -441,10 +441,10 @@ buffers. Examples ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -ustruct.pack +struct.pack ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Use `ustruct.pack_into` instead of `ustruct.pack`. +Use `struct.pack_into` instead of `struct.pack`. Sensor properties and units -------------------------------------------------------------------------------- From bbdca2f3529f23d76c8da7cf0a5d881d6acead36 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 23 May 2018 15:07:32 -0400 Subject: [PATCH 064/103] typos in builtins.rst --- docs/library/builtins.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/library/builtins.rst b/docs/library/builtins.rst index c60ade0406..36a84bc0c5 100644 --- a/docs/library/builtins.rst +++ b/docs/library/builtins.rst @@ -58,7 +58,7 @@ Not all of these functions and types are turned on in all CircuitPython ports, f .. class:: frozenset() -`frozenset() is not enabled on non-Express CircuitPython boards.` +`frozenset()` is not enabled on non-Express CircuitPython boards. .. function:: getattr() @@ -128,7 +128,7 @@ Not all of these functions and types are turned on in all CircuitPython ports, f .. function:: reversed() -`reversed() is not enabled on non-Express CircuitPython boards.` +`reversed()` is not enabled on non-Express CircuitPython boards. .. function:: round() From 240678e71403f54ab31452a7138dfd8ddcfc7f0c Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 23 May 2018 21:39:06 +0200 Subject: [PATCH 065/103] Avoid uninitialized gamepad on exception Raise exceptions before the gamepad_singleton is created. Also, use mp_raise for creating the exceptions. --- shared-bindings/gamepad/GamePad.c | 15 +++++++++------ shared-module/gamepad/GamePad.c | 5 ++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 2c982c7d55..aa645dc662 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -28,6 +28,7 @@ #include "py/mphal.h" #include "shared-module/gamepad/GamePad.h" #include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/util.h" #include "GamePad.h" @@ -93,16 +94,18 @@ gamepad_obj_t* volatile gamepad_singleton = NULL; //| STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + for (size_t i = 0; i < n_args; ++i) { + if (!MP_OBJ_IS_TYPE(args[i], &digitalio_digitalinout_type)) { + mp_raise_TypeError("Expected a DigitalInOut"); + } + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(args[i]); + raise_error_if_deinited( + common_hal_digitalio_digitalinout_deinited(pin)); + } if (!gamepad_singleton) { gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_singleton->base.type = &gamepad_type; } - for (size_t i = 0; i < n_args; ++i) { - if (!MP_OBJ_IS_TYPE(args[i], &digitalio_digitalinout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, - "Expected a %q", digitalio_digitalinout_type.name)); - } - } gamepad_init(n_args, args); return MP_OBJ_FROM_PTR(gamepad_singleton); } diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index 540c882dac..88ab23fc1e 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -35,13 +35,12 @@ void gamepad_init(size_t n_pins, const mp_obj_t* pins) { - for (size_t i=0; i<8; ++i) { + for (size_t i = 0; i < 8; ++i) { gamepad_singleton->pins[i] = NULL; } gamepad_singleton->pulls = 0; - for (size_t i=0; i Date: Wed, 23 May 2018 21:41:56 +0200 Subject: [PATCH 066/103] Raise an error if more than 8 buttons passed to gamepad --- shared-bindings/gamepad/GamePad.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index aa645dc662..80916d7f43 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -94,9 +94,12 @@ gamepad_obj_t* volatile gamepad_singleton = NULL; //| STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + if (n_args > 8) { + mp_raise_TypeError("too many arguments"); + } for (size_t i = 0; i < n_args; ++i) { if (!MP_OBJ_IS_TYPE(args[i], &digitalio_digitalinout_type)) { - mp_raise_TypeError("Expected a DigitalInOut"); + mp_raise_TypeError("expected a DigitalInOut"); } digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(args[i]); raise_error_if_deinited( From 42e36a883b4145e9f8b70022d6f13ceaab4fca2c Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 23 May 2018 21:49:20 +0200 Subject: [PATCH 067/103] Remove volatile from the gamepad struct --- shared-bindings/gamepad/GamePad.c | 2 +- shared-module/gamepad/GamePad.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 80916d7f43..99fdbb64a2 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -32,7 +32,7 @@ #include "GamePad.h" -gamepad_obj_t* volatile gamepad_singleton = NULL; +gamepad_obj_t* gamepad_singleton = NULL; //| .. currentmodule:: gamepad //| diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index 77fdb8b0af..7ce55cf984 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -39,7 +39,7 @@ typedef struct { uint8_t pulls; } gamepad_obj_t; -extern gamepad_obj_t* volatile gamepad_singleton; +extern gamepad_obj_t* gamepad_singleton; void gamepad_init(size_t n_pins, const mp_obj_t* pins); From 083f91363aee04f06cab1f4cc895a4aca185d04c Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 23 May 2018 18:21:07 -0400 Subject: [PATCH 068/103] pirkey APA102 defs were commented out in mpconfigboard.h --- ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h index a26fd7fdd0..fd4f92289c 100644 --- a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h @@ -1,8 +1,8 @@ #define MICROPY_HW_BOARD_NAME "Adafruit pIRKey M0" #define MICROPY_HW_MCU_NAME "samd21e18" -// #define MICROPY_HW_APA102_MOSI (&pin_PA00) -// #define MICROPY_HW_APA102_SCK (&pin_PA01) +#define MICROPY_HW_APA102_MOSI (&pin_PA00) +#define MICROPY_HW_APA102_SCK (&pin_PA01) // #define CIRCUITPY_BITBANG_APA102 From b9f36184f57264926230e31096ea0321c6de9382 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 24 May 2018 13:28:03 -0400 Subject: [PATCH 069/103] refine pirkey build --- .gitmodules | 6 +++ .../Adafruit_CircuitPython_CircuitPlayground | 2 +- frozen/Adafruit_CircuitPython_DotStar | 1 + frozen/Adafruit_CircuitPython_IRRemote | 1 + frozen/Adafruit_CircuitPython_seesaw | 2 +- .../boards/pirkey_m0/mpconfigboard.h | 52 +++++++++++++++++++ .../boards/pirkey_m0/mpconfigboard.mk | 5 ++ ports/atmel-samd/mpconfigport.h | 18 +++++++ shared-bindings/busio/__init__.c | 5 +- 9 files changed, 89 insertions(+), 3 deletions(-) create mode 160000 frozen/Adafruit_CircuitPython_DotStar create mode 160000 frozen/Adafruit_CircuitPython_IRRemote diff --git a/.gitmodules b/.gitmodules index 9025eb7d66..d0f3fd5656 100644 --- a/.gitmodules +++ b/.gitmodules @@ -61,3 +61,9 @@ [submodule "ports/atmel-samd/Adafruit_CircuitPython_seesaw"] path = frozen/Adafruit_CircuitPython_seesaw url = https://github.com/adafruit/Adafruit_CircuitPython_seesaw.git +[submodule "frozen/Adafruit_CircuitPython_IRRemote"] + path = frozen/Adafruit_CircuitPython_IRRemote + url = https://github.com/adafruit/Adafruit_CircuitPython_IRRemote.git +[submodule "frozen/Adafruit_CircuitPython_DotStar"] + path = frozen/Adafruit_CircuitPython_DotStar + url = https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git diff --git a/frozen/Adafruit_CircuitPython_CircuitPlayground b/frozen/Adafruit_CircuitPython_CircuitPlayground index a669915237..d82fb2399b 160000 --- a/frozen/Adafruit_CircuitPython_CircuitPlayground +++ b/frozen/Adafruit_CircuitPython_CircuitPlayground @@ -1 +1 @@ -Subproject commit a669915237545638c64f89400f368a91c408cd5d +Subproject commit d82fb2399b304e4cac10f0001ac63b76dd6b2ba0 diff --git a/frozen/Adafruit_CircuitPython_DotStar b/frozen/Adafruit_CircuitPython_DotStar new file mode 160000 index 0000000000..af25424ee7 --- /dev/null +++ b/frozen/Adafruit_CircuitPython_DotStar @@ -0,0 +1 @@ +Subproject commit af25424ee7dbebea3e5d77390c017018ffa52d36 diff --git a/frozen/Adafruit_CircuitPython_IRRemote b/frozen/Adafruit_CircuitPython_IRRemote new file mode 160000 index 0000000000..c29e10b590 --- /dev/null +++ b/frozen/Adafruit_CircuitPython_IRRemote @@ -0,0 +1 @@ +Subproject commit c29e10b590efbdf06163897b49cd0c2bea82ad6e diff --git a/frozen/Adafruit_CircuitPython_seesaw b/frozen/Adafruit_CircuitPython_seesaw index 71f3947ff9..498f59bf92 160000 --- a/frozen/Adafruit_CircuitPython_seesaw +++ b/frozen/Adafruit_CircuitPython_seesaw @@ -1 +1 @@ -Subproject commit 71f3947ff94ca070eb01c5f872b0400031be8086 +Subproject commit 498f59bf926477b3a8fb8eb157ca05eb12c3e298 diff --git a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h index fd4f92289c..15979ae778 100644 --- a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h +++ b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.h @@ -12,6 +12,58 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 +// A number of modules are removed for pIRKey to make room for frozen libraries. +#define PIRKEY_M0 (1) + #include "internal_flash.h" #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) + +#define IGNORE_PIN_PA02 1 +#define IGNORE_PIN_PA03 1 +#define IGNORE_PIN_PA04 1 +#define IGNORE_PIN_PA05 1 +#define IGNORE_PIN_PA06 1 +#define IGNORE_PIN_PA07 1 +#define IGNORE_PIN_PA08 1 +#define IGNORE_PIN_PA09 1 +#define IGNORE_PIN_PA10 1 +#define IGNORE_PIN_PA11 1 +#define IGNORE_PIN_PA12 1 +#define IGNORE_PIN_PA13 1 +#define IGNORE_PIN_PA14 1 +#define IGNORE_PIN_PA15 1 +#define IGNORE_PIN_PA16 1 +#define IGNORE_PIN_PA17 1 +#define IGNORE_PIN_PA18 1 +#define IGNORE_PIN_PA19 1 +#define IGNORE_PIN_PA20 1 +#define IGNORE_PIN_PA21 1 +#define IGNORE_PIN_PA22 1 +#define IGNORE_PIN_PA24 1 +#define IGNORE_PIN_PA25 1 +#define IGNORE_PIN_PA27 1 +#define IGNORE_PIN_PA30 1 +#define IGNORE_PIN_PA31 1 +#define IGNORE_PIN_PB00 1 +#define IGNORE_PIN_PB01 1 +#define IGNORE_PIN_PB02 1 +#define IGNORE_PIN_PB03 1 +#define IGNORE_PIN_PB04 1 +#define IGNORE_PIN_PB05 1 +#define IGNORE_PIN_PB06 1 +#define IGNORE_PIN_PB07 1 +#define IGNORE_PIN_PB08 1 +#define IGNORE_PIN_PB09 1 +#define IGNORE_PIN_PB10 1 +#define IGNORE_PIN_PB11 1 +#define IGNORE_PIN_PB12 1 +#define IGNORE_PIN_PB13 1 +#define IGNORE_PIN_PB14 1 +#define IGNORE_PIN_PB15 1 +#define IGNORE_PIN_PB16 1 +#define IGNORE_PIN_PB17 1 +#define IGNORE_PIN_PB22 1 +#define IGNORE_PIN_PB23 1 +#define IGNORE_PIN_PB30 1 +#define IGNORE_PIN_PB31 1 diff --git a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk index 87b46c642f..0f8d0f9ca4 100644 --- a/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk @@ -9,3 +9,8 @@ LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 + +# Include these Python libraries in firmware. +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_DotStar +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_IRRemote diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 0d07e0009e..15cb514049 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -247,6 +247,23 @@ extern const struct _mp_obj_module_t usb_hid_module; #define TOUCHIO_MODULE #endif +// A pIRKey has minimal I/O needs. Remove unneeded modules to make room +// for frozen modules. math is very large and is also removed. +#ifdef PIRKEY_M0 +#define MICROPY_PORT_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, +#else #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ @@ -267,6 +284,7 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \ TOUCHIO_MODULE \ EXTRA_BUILTIN_MODULES +#endif #define MICROPY_PORT_BUILTIN_DEBUG_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_uheap),(mp_obj_t)&uheap_module }, \ diff --git a/shared-bindings/busio/__init__.c b/shared-bindings/busio/__init__.c index 958ee062af..0ed0bf22db 100644 --- a/shared-bindings/busio/__init__.c +++ b/shared-bindings/busio/__init__.c @@ -87,10 +87,13 @@ STATIC const mp_rom_map_elem_t busio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_busio) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) }, +// a pIRKey does not need these. Remove them to make room for frozen modules. +#ifndef PIRKEY_M0 + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&busio_onewire_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&busio_uart_type) }, +#endif }; STATIC MP_DEFINE_CONST_DICT(busio_module_globals, busio_module_globals_table); From 42bf3a130620437b4c2a284e0d603377b7c42080 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 24 May 2018 13:14:40 -0700 Subject: [PATCH 070/103] Remove asf3 nvm code. --- ports/atmel-samd/supervisor/port.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index cd122843de..026a895620 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -192,12 +192,6 @@ safe_mode_t port_init(void) { tick_init(); rtc_init(); - // Init the nvm controller. - // struct nvm_config config_nvm; - // nvm_get_config_defaults(&config_nvm); - // config_nvm.manual_page_write = false; - // nvm_set_config(&config_nvm); - init_shared_dma(); #ifdef CIRCUITPY_CANARY_WORD // Run in safe mode if the canary is corrupt. From ee896cdd41f9c3f3ca4fdc7c08b8e24caabe950b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 24 May 2018 20:20:18 -0400 Subject: [PATCH 071/103] restore I2C and UART. Don't do port.c resets and inits for modules that are not available. --- ports/atmel-samd/supervisor/port.c | 12 ++++++++---- shared-bindings/busio/__init__.c | 5 +---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index cd122843de..8571348fbb 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -190,7 +190,10 @@ safe_mode_t port_init(void) { // Configure millisecond timer initialization. tick_init(); + +#ifndef PIRKEY_M0 rtc_init(); +#endif // Init the nvm controller. // struct nvm_config config_nvm; @@ -249,18 +252,19 @@ void reset_port(void) { pulsein_reset(); pulseout_reset(); pwmout_reset(); + +#ifndef PIRKEY_M0 + analogin_reset(); + analogout_reset(); rtc_reset(); +#endif reset_gclks(); - analogin_reset(); - #ifdef CIRCUITPY_GAMEPAD_TICKS gamepad_reset(); #endif - analogout_reset(); - reset_event_system(); reset_all_pins(); diff --git a/shared-bindings/busio/__init__.c b/shared-bindings/busio/__init__.c index 0ed0bf22db..958ee062af 100644 --- a/shared-bindings/busio/__init__.c +++ b/shared-bindings/busio/__init__.c @@ -87,13 +87,10 @@ STATIC const mp_rom_map_elem_t busio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_busio) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) }, -// a pIRKey does not need these. Remove them to make room for frozen modules. -#ifndef PIRKEY_M0 { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) }, { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&busio_onewire_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&busio_uart_type) }, -#endif }; STATIC MP_DEFINE_CONST_DICT(busio_module_globals, busio_module_globals_table); From a5e1174926f9b8d9371f2b67c1fb42e75c456559 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 24 May 2018 21:18:56 -0400 Subject: [PATCH 072/103] add cpx-crickit build to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4749416e0f..c7a45ea805 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ compiler: env: - TRAVIS_BOARD=arduino_zero - TRAVIS_BOARD=circuitplayground_express + - TRAVIS_BOARD=circuitplayground_express_crickit - TRAVIS_BOARD=feather_m0_basic - TRAVIS_BOARD=feather_m0_adalogger - TRAVIS_BOARD=feather_m0_rfm69 From 052a9ff3782ecad55ddece286af00cbd0a7fe57b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 24 May 2018 21:40:49 -0400 Subject: [PATCH 073/103] update cpx lib --- frozen/Adafruit_CircuitPython_CircuitPlayground | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozen/Adafruit_CircuitPython_CircuitPlayground b/frozen/Adafruit_CircuitPython_CircuitPlayground index d82fb2399b..d0022de9d5 160000 --- a/frozen/Adafruit_CircuitPython_CircuitPlayground +++ b/frozen/Adafruit_CircuitPython_CircuitPlayground @@ -1 +1 @@ -Subproject commit d82fb2399b304e4cac10f0001ac63b76dd6b2ba0 +Subproject commit d0022de9d5e9b4c3d4998a5d708e59ee147f0a21 From eab8e4318823ce8873a9a4f282f834dabac2d0f0 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 29 May 2018 20:25:28 -0400 Subject: [PATCH 074/103] rev E feather M4 express; remove named AREF pins --- .../boards/feather_m4_express/mpconfigboard.h | 24 +++++++-------- .../feather_m4_express/mpconfigboard.mk | 2 +- .../boards/feather_m4_express/pins.c | 29 +++++++++---------- .../boards/itsybitsy_m4_express/pins.c | 2 -- .../atmel-samd/boards/metro_m4_express/pins.c | 2 -- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 3c1895dd24..64b22acd57 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -3,15 +3,15 @@ #define CIRCUITPY_MCU_FAMILY samd51 -// This is for Rev C which is green +// Rev E -#define MICROPY_HW_NEOPIXEL (&pin_PB23) +#define MICROPY_HW_NEOPIXEL (&pin_PB03) // These are pins not to reset. -// QSPI Data pins and TX LED -#define MICROPY_PORT_A (PORT_PA08 | PORT_PA09) -// RX LED, QSPI CS, QSPI SCK and NeoPixel pin -#define MICROPY_PORT_B (PORT_PB10 | PORT_PB11 | PORT_PB23 ) +// QSPI Data pins +#define MICROPY_PORT_A (PORT_PA08 | PORT_PA09 | PORT_PA10 | PORT_PA11) +// QSPI CS, QSPI SCK and NeoPixel pin +#define MICROPY_PORT_B (PORT_PB03 | PORT_PB10 | PORT_PB11) #define MICROPY_PORT_C (0) #define MICROPY_PORT_D (0) @@ -25,8 +25,8 @@ #include "external_flash/devices.h" -#define EXTERNAL_FLASH_DEVICE_COUNT 2 -#define EXTERNAL_FLASH_DEVICES W25Q16FW, GD25Q16C +#define EXTERNAL_FLASH_DEVICE_COUNT 1 +#define EXTERNAL_FLASH_DEVICES GD25Q16C #define EXTERNAL_FLASH_QSPI_DUAL @@ -36,8 +36,8 @@ #define DEFAULT_I2C_BUS_SDA (&pin_PA12) #define DEFAULT_SPI_BUS_SCK (&pin_PA17) -#define DEFAULT_SPI_BUS_MOSI (&pin_PA19) -#define DEFAULT_SPI_BUS_MISO (&pin_PA18) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB23) +#define DEFAULT_SPI_BUS_MISO (&pin_PB22) -#define DEFAULT_UART_BUS_RX (&pin_PA07) -#define DEFAULT_UART_BUS_TX (&pin_PA04) +#define DEFAULT_UART_BUS_RX (&pin_PB17) +#define DEFAULT_UART_BUS_TX (&pin_PB16) diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk index 01385bdc6c..e19a27b88f 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk @@ -7,5 +7,5 @@ USB_MANUFACTURER = "Adafruit Industries LLC" QSPI_FLASH_FILESYSTEM = 1 LONGINT_IMPL = MPZ -CHIP_VARIANT = SAMD51G19A +CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index 6807a901d5..bc3cb1f477 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -1,4 +1,4 @@ -#include "samd21_pins.h" +#include "samd51_pins.h" #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { @@ -6,27 +6,26 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PA05) }, { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_PB08) }, { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_PB09) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PA06) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_PA10) }, - { MP_ROM_QSTR(MP_QSTR_AREF), MP_ROM_PTR(&pin_PA03) }, - + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PA04) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_PA06) }, { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA17) }, - { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA19) }, - { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA18) }, - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA07) }, - { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PA07) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_PA04) }, - { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PA04) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB23) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PB22) }, + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PB17) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PB17) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_PB16) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PB16) }, { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PA13) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PA14) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PA15) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PA16) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PA16) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PA18) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PA19) }, { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_PA20) }, { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_PA21) }, { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA22) }, { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA23) }, - { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB23) }, + { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PB03) }, + { MP_ROM_QSTR(MP_QSTR_VOLTAGE_MONITOR), MP_ROM_PTR(&pin_PB01) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c index 084250b311..fdcf039e64 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c @@ -28,8 +28,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PA12) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PA13) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_AREF), MP_ROM_PTR(&pin_PA03) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA01) }, { MP_OBJ_NEW_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA00) }, { MP_OBJ_NEW_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PB23) }, diff --git a/ports/atmel-samd/boards/metro_m4_express/pins.c b/ports/atmel-samd/boards/metro_m4_express/pins.c index 95a97d825f..8dc0f25d6b 100644 --- a/ports/atmel-samd/boards/metro_m4_express/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express/pins.c @@ -32,8 +32,6 @@ STATIC const mp_map_elem_t board_global_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), (mp_obj_t)&pin_PB02 }, { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), (mp_obj_t)&pin_PB03 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_AREF), (mp_obj_t)&pin_PA03 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_NEOPIXEL), (mp_obj_t)&pin_PB22 }, { MP_OBJ_NEW_QSTR(MP_QSTR_SCK), (mp_obj_t)&pin_PA13 }, From c37b69e1a5316956ce966cce6841f3d960e581b0 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 24 May 2018 11:16:30 +0200 Subject: [PATCH 076/103] Make the gamepad singleton long-lived So that it is not later moved. --- shared-bindings/gamepad/GamePad.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 99fdbb64a2..b103c59b4e 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -26,6 +26,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mphal.h" +#include "py/gc.h" #include "shared-module/gamepad/GamePad.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" @@ -108,6 +109,7 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, if (!gamepad_singleton) { gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_singleton->base.type = &gamepad_type; + gamepad_singleton = gc_make_long_lived(gamepad_singleton); } gamepad_init(n_args, args); return MP_OBJ_FROM_PTR(gamepad_singleton); From b219ce6d377b6e00e1967c9b98966d4229f566ed Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 24 May 2018 11:25:21 +0200 Subject: [PATCH 077/103] Add gamepad_singleton to root pointers --- ports/atmel-samd/mpconfigport.h | 1 + shared-bindings/gamepad/GamePad.c | 17 ++++++++--------- shared-module/gamepad/GamePad.c | 2 ++ shared-module/gamepad/GamePad.h | 2 -- shared-module/gamepad/__init__.c | 4 +++- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 15cb514049..cd05927b08 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -312,6 +312,7 @@ extern const struct _mp_obj_module_t usb_hid_module; mp_obj_t playing_audio[AUDIO_DMA_CHANNEL_COUNT]; \ mp_obj_t rtc_time_source; \ FLASH_ROOT_POINTERS \ + mp_obj_t gamepad_singleton; \ void run_background_tasks(void); #define MICROPY_VM_HOOK_LOOP run_background_tasks(); diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index b103c59b4e..1e95776d70 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -27,14 +27,14 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/gc.h" +#include "py/mpstate.h" +#include "shared-module/gamepad/__init__.h" #include "shared-module/gamepad/GamePad.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" #include "GamePad.h" -gamepad_obj_t* gamepad_singleton = NULL; - //| .. currentmodule:: gamepad //| //| :class:`GamePad` -- Scan buttons for presses @@ -106,13 +106,13 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, raise_error_if_deinited( common_hal_digitalio_digitalinout_deinited(pin)); } - if (!gamepad_singleton) { - gamepad_singleton = m_new_obj(gamepad_obj_t); + if (!MP_STATE_VM(gamepad_singleton)) { + gamepad_obj_t* gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_singleton->base.type = &gamepad_type; - gamepad_singleton = gc_make_long_lived(gamepad_singleton); + MP_STATE_VM(gamepad_singleton) = gc_make_long_lived(gamepad_singleton); } gamepad_init(n_args, args); - return MP_OBJ_FROM_PTR(gamepad_singleton); + return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); } @@ -127,6 +127,7 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, //| held down) can be recorded for the next call. //| STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { + gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed); gamepad_singleton->pressed = 0; return gamepad; @@ -139,14 +140,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); //| Disable button scanning. //| STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { - gamepad_singleton = NULL; + gamepad_reset(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); -STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *args); STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index 88ab23fc1e..2c14fa02a4 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -26,6 +26,7 @@ #include +#include "py/mpstate.h" #include "__init__.h" #include "GamePad.h" @@ -35,6 +36,7 @@ void gamepad_init(size_t n_pins, const mp_obj_t* pins) { + gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); for (size_t i = 0; i < 8; ++i) { gamepad_singleton->pins[i] = NULL; } diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index 7ce55cf984..9fd5c9626e 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -39,8 +39,6 @@ typedef struct { uint8_t pulls; } gamepad_obj_t; -extern gamepad_obj_t* gamepad_singleton; - void gamepad_init(size_t n_pins, const mp_obj_t* pins); #endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index b1cdac2a70..8414ddfbea 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -26,6 +26,7 @@ #include +#include "py/mpstate.h" #include "__init__.h" #include "GamePad.h" @@ -33,6 +34,7 @@ void gamepad_tick(void) { + gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); if (!gamepad_singleton) { return; } @@ -54,5 +56,5 @@ void gamepad_tick(void) { } void gamepad_reset(void) { - gamepad_singleton = NULL; + MP_STATE_VM(gamepad_singleton) = NULL; } From d02899f822c57979047c6a3fdea4e20b5f60e016 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 30 May 2018 23:04:29 +0200 Subject: [PATCH 078/103] Add gamepad_singleto to root pointers for the nrf port --- ports/nrf/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index a463108b87..442e98de53 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -323,6 +323,7 @@ extern const struct _mp_obj_module_t ble_module; struct _music_data_t *music_data; \ const struct _pwm_events *pwm_active_events; \ const struct _pwm_events *pwm_pending_events; \ + mp_obj_t gamepad_singleton; \ #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) From 5c6aea9fd8a26cb4c1f9b41a93114f0f8ab529a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Fri, 1 Jun 2018 14:06:43 +0200 Subject: [PATCH 079/103] atmel-samd/samd51: Implement samd.clock Fill out the dummy implementation. --- ports/atmel-samd/samd51_clocks.c | 323 +++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) diff --git a/ports/atmel-samd/samd51_clocks.c b/ports/atmel-samd/samd51_clocks.c index cc3dd7917a..d5cb8aed03 100644 --- a/ports/atmel-samd/samd51_clocks.c +++ b/ports/atmel-samd/samd51_clocks.c @@ -28,6 +28,7 @@ #include "hpl_gclk_config.h" +#include "bindings/samd/Clock.h" #include "shared-bindings/microcontroller/__init__.h" #include "py/runtime.h" @@ -61,26 +62,348 @@ void disable_clock_generator(uint8_t gclk) { while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {} } +static bool clk_enabled(uint8_t clk) { + return GCLK->PCHCTRL[clk].bit.CHEN; +} + +static uint8_t clk_get_generator(uint8_t clk) { + return GCLK->PCHCTRL[clk].bit.GEN; +} + +static uint8_t generator_get_source(uint8_t gen) { + return GCLK->GENCTRL[gen].bit.SRC; +} + +static bool osc_enabled(uint8_t index) { + switch (index) { + case GCLK_SOURCE_XOSC0: + return OSCCTRL->XOSCCTRL[0].bit.ENABLE; + case GCLK_SOURCE_XOSC1: + return OSCCTRL->XOSCCTRL[1].bit.ENABLE; + case GCLK_SOURCE_OSCULP32K: + return true; + case GCLK_SOURCE_XOSC32K: + return OSC32KCTRL->XOSC32K.bit.ENABLE; + case GCLK_SOURCE_DFLL: + return OSCCTRL->DFLLCTRLA.bit.ENABLE; + case GCLK_SOURCE_DPLL0: + return OSCCTRL->Dpll[0].DPLLCTRLA.bit.ENABLE; + case GCLK_SOURCE_DPLL1: + return OSCCTRL->Dpll[1].DPLLCTRLA.bit.ENABLE; + }; + return false; +} + +static uint32_t osc_get_source(uint8_t index) { + uint8_t dpll_index = index - GCLK_SOURCE_DPLL0; + uint32_t refclk = OSCCTRL->Dpll[dpll_index].DPLLCTRLB.bit.REFCLK; + switch (refclk) { + case 0x0: + return generator_get_source(GCLK->PCHCTRL[OSCCTRL_GCLK_ID_FDPLL0 + dpll_index].bit.GEN); + case 0x1: + return GCLK_SOURCE_XOSC32K; + case 0x2: + return GCLK_SOURCE_XOSC0; + case 0x3: + return GCLK_SOURCE_XOSC1; + } + return 0; +} + +static uint32_t osc_get_frequency(uint8_t index); + +static uint32_t generator_get_frequency(uint8_t gen) { + uint8_t src = GCLK->GENCTRL[gen].bit.SRC; + uint32_t div; + if (GCLK->GENCTRL[gen].bit.DIVSEL) { + div = 1 << (GCLK->GENCTRL[gen].bit.DIV + 1); + } else { + div = GCLK->GENCTRL[gen].bit.DIV; + if (!div) + div = 1; + } + + return osc_get_frequency(src) / div; +} + +static uint32_t dpll_get_frequency(uint8_t index) { + uint8_t dpll_index = index - GCLK_SOURCE_DPLL0; + uint32_t refclk = OSCCTRL->Dpll[dpll_index].DPLLCTRLB.bit.REFCLK; + uint32_t freq; + + switch (refclk) { + case 0x0: // GCLK + freq = generator_get_frequency(GCLK->PCHCTRL[OSCCTRL_GCLK_ID_FDPLL0 + dpll_index].bit.GEN); + break; + case 0x1: // XOSC32 + freq = 32768; + break; + case 0x2: // XOSC0 + case 0x3: // XOSC1 + return 0; // unknown + } + + return (freq * (OSCCTRL->Dpll[dpll_index].DPLLRATIO.bit.LDR + 1)) + + (freq * OSCCTRL->Dpll[dpll_index].DPLLRATIO.bit.LDRFRAC / 32); +} + +static uint32_t osc_get_frequency(uint8_t index) { + switch (index) { + case GCLK_SOURCE_XOSC0: + case GCLK_SOURCE_XOSC1: + return 0; // unknown + case GCLK_SOURCE_OSCULP32K: + case GCLK_SOURCE_XOSC32K: + return 32768; + case GCLK_SOURCE_DFLL: + return 48000000; + case GCLK_SOURCE_DPLL0: + case GCLK_SOURCE_DPLL1: + return dpll_get_frequency(index); + } + return 0; +} + bool clock_get_enabled(uint8_t type, uint8_t index) { + if (type == 0) + return osc_enabled(index); + if (type == 1) + return clk_enabled(index); + if (type == 2) + return SysTick->CTRL & SysTick_CTRL_ENABLE_Msk; return false; } bool clock_get_parent(uint8_t type, uint8_t index, uint8_t *p_type, uint8_t *p_index) { + if (type == 0 && osc_enabled(index)) { + if (index == GCLK_SOURCE_DPLL0 || index == GCLK_SOURCE_DPLL1) { + *p_type = 0; + *p_index = osc_get_source(index); + return true; + } + return false; + } + if (type == 1 && index <= 47 && clk_enabled(index)) { + *p_type = 0; + *p_index = generator_get_source(clk_get_generator(index)); + return true; + } + if (type == 2) { + switch (index) { + case 0: + case 1: + *p_type = 0; + *p_index = generator_get_source(0); + return true; + case 2: + *p_type = 0; + switch (OSC32KCTRL->RTCCTRL.bit.RTCSEL) { + case 0: + case 1: + *p_index = GCLK_SOURCE_OSCULP32K; + return true; + case 4: + case 5: + *p_index = GCLK_SOURCE_XOSC32K; + return true; + } + return false; + } + } return false; } uint32_t clock_get_frequency(uint8_t type, uint8_t index) { + if (type == 0) { + return osc_get_frequency(index); + } + if (type == 1 && index <= 47 && clk_enabled(index)) { + return generator_get_frequency(clk_get_generator(index)); + } + if (type == 2) { + switch (index) { + case 0: + return clock_get_frequency(0, generator_get_source(0)) / SysTick->LOAD; + case 1: + return clock_get_frequency(0, generator_get_source(0)) / MCLK->CPUDIV.bit.DIV; + case 2: + switch (OSC32KCTRL->RTCCTRL.bit.RTCSEL) { + case 0: + case 4: + return 1024; + case 1: + case 5: + return 32768; + } + } + } return 0; } uint32_t clock_get_calibration(uint8_t type, uint8_t index) { + if (type == 0) { + switch (index) { + case GCLK_SOURCE_OSCULP32K: + return OSC32KCTRL->OSCULP32K.bit.CALIB; + }; + } + if (type == 2 && index == 0) { + return SysTick->LOAD + 1; + } return 0; } int clock_set_calibration(uint8_t type, uint8_t index, uint32_t val) { + if (type == 0) { + switch (index) { + case GCLK_SOURCE_OSCULP32K: + if (val > 0x3f) + return -1; + OSC32KCTRL->OSCULP32K.bit.CALIB = val; + return 0; + }; + } + if (type == 2 && index == 0) { + if (val < 0x1000 || val > 0x1000000) + return -1; + SysTick->LOAD = val - 1; + return 0; + } return -2; } +#include +#include +#include +#include +#include +#include +#include + +CLOCK_SOURCE(XOSC0); +CLOCK_SOURCE(XOSC1); +CLOCK_SOURCE(GCLKIN); +CLOCK_SOURCE(GCLKGEN1); +CLOCK_SOURCE(OSCULP32K); +CLOCK_SOURCE(XOSC32K); +CLOCK_SOURCE(DFLL); +CLOCK_SOURCE(DPLL0); +CLOCK_SOURCE(DPLL1); + +CLOCK_GCLK_(OSCCTRL, DFLL48); +CLOCK_GCLK_(OSCCTRL, FDPLL0); +CLOCK_GCLK_(OSCCTRL, FDPLL1); +CLOCK_GCLK_(OSCCTRL, FDPLL032K); // GCLK_OSCCTRL_FDPLL1_32K, GCLK_SDHC0_SLOW, GCLK_SDHC1_SLOW, GCLK_SERCOM[0..7]_SLOW +CLOCK_GCLK(EIC); +CLOCK_GCLK_(FREQM, MSR); +// 6: GCLK_FREQM_REF +CLOCK_GCLK_(SERCOM0, CORE); +CLOCK_GCLK_(SERCOM1, CORE); +CLOCK(TC0_TC1, 1, 9); +CLOCK_GCLK(USB); +CLOCK_GCLK_(EVSYS, 0); +CLOCK_GCLK_(EVSYS, 1); +CLOCK_GCLK_(EVSYS, 2); +CLOCK_GCLK_(EVSYS, 3); +CLOCK_GCLK_(EVSYS, 4); +CLOCK_GCLK_(EVSYS, 5); +CLOCK_GCLK_(EVSYS, 6); +CLOCK_GCLK_(EVSYS, 7); +CLOCK_GCLK_(EVSYS, 8); +CLOCK_GCLK_(EVSYS, 9); +CLOCK_GCLK_(EVSYS, 10); +CLOCK_GCLK_(EVSYS, 11); +CLOCK_GCLK_(SERCOM2, CORE); +CLOCK_GCLK_(SERCOM3, CORE); +CLOCK(TCC0_TCC1, 1, 25); +CLOCK(TC2_TC3, 1, 26); +CLOCK_GCLK(CAN0); +CLOCK_GCLK(CAN1); +CLOCK(TCC2_TCC3, 1, 29); +CLOCK(TC4_TC5, 1, 30); +CLOCK_GCLK(PDEC); +CLOCK_GCLK(AC); +CLOCK_GCLK(CCL); +CLOCK_GCLK_(SERCOM4, CORE); +CLOCK_GCLK_(SERCOM5, CORE); +CLOCK_GCLK_(SERCOM6, CORE); +CLOCK_GCLK_(SERCOM7, CORE); +CLOCK_GCLK(TCC4); +CLOCK(TC6_TC7, 1, 39); +CLOCK_GCLK(ADC0); +CLOCK_GCLK(ADC1); +CLOCK_GCLK(DAC); +CLOCK_GCLK_(I2S, 0); +CLOCK_GCLK_(I2S, 1); +CLOCK_GCLK(SDHC0); +CLOCK_GCLK(SDHC1); +// 47: GCLK_CM4_TRACE + +CLOCK(SYSTICK, 2, 0); +CLOCK(CPU, 2, 1); +CLOCK(RTC, 2, 2); + + STATIC const mp_rom_map_elem_t samd_clock_global_dict_table[] = { + CLOCK_ENTRY(XOSC0), + CLOCK_ENTRY(XOSC1), + CLOCK_ENTRY(GCLKIN), + CLOCK_ENTRY(GCLKGEN1), + CLOCK_ENTRY(OSCULP32K), + CLOCK_ENTRY(XOSC32K), + CLOCK_ENTRY(DFLL), + CLOCK_ENTRY(DPLL0), + CLOCK_ENTRY(DPLL1), + + CLOCK_ENTRY_(OSCCTRL, DFLL48), + CLOCK_ENTRY_(OSCCTRL, FDPLL0), + CLOCK_ENTRY_(OSCCTRL, FDPLL1), + CLOCK_ENTRY_(OSCCTRL, FDPLL032K), + CLOCK_ENTRY(EIC), + CLOCK_ENTRY_(FREQM, MSR), + CLOCK_ENTRY_(SERCOM0, CORE), + CLOCK_ENTRY_(SERCOM1, CORE), + CLOCK_ENTRY(TC0_TC1), + CLOCK_ENTRY(USB), + CLOCK_ENTRY_(EVSYS, 0), + CLOCK_ENTRY_(EVSYS, 1), + CLOCK_ENTRY_(EVSYS, 2), + CLOCK_ENTRY_(EVSYS, 3), + CLOCK_ENTRY_(EVSYS, 4), + CLOCK_ENTRY_(EVSYS, 5), + CLOCK_ENTRY_(EVSYS, 6), + CLOCK_ENTRY_(EVSYS, 7), + CLOCK_ENTRY_(EVSYS, 8), + CLOCK_ENTRY_(EVSYS, 9), + CLOCK_ENTRY_(EVSYS, 10), + CLOCK_ENTRY_(EVSYS, 11), + CLOCK_ENTRY_(SERCOM2, CORE), + CLOCK_ENTRY_(SERCOM3, CORE), + CLOCK_ENTRY(TCC0_TCC1), + CLOCK_ENTRY(TC2_TC3), + CLOCK_ENTRY(CAN0), + CLOCK_ENTRY(CAN1), + CLOCK_ENTRY(TCC2_TCC3), + CLOCK_ENTRY(TC4_TC5), + CLOCK_ENTRY(PDEC), + CLOCK_ENTRY(AC), + CLOCK_ENTRY(CCL), + CLOCK_ENTRY_(SERCOM4, CORE), + CLOCK_ENTRY_(SERCOM5, CORE), + CLOCK_ENTRY_(SERCOM6, CORE), + CLOCK_ENTRY_(SERCOM7, CORE), + CLOCK_ENTRY(TCC4), + CLOCK_ENTRY(TC6_TC7), + CLOCK_ENTRY(ADC0), + CLOCK_ENTRY(ADC1), + CLOCK_ENTRY(DAC), + CLOCK_ENTRY_(I2S, 0), + CLOCK_ENTRY_(I2S, 1), + CLOCK_ENTRY(SDHC0), + CLOCK_ENTRY(SDHC1), + + CLOCK_ENTRY(SYSTICK), + CLOCK_ENTRY(CPU), + CLOCK_ENTRY(RTC), }; MP_DEFINE_CONST_DICT(samd_clock_globals, samd_clock_global_dict_table); From ab7ddfddd53173e03c25176d7d1665504a8a8f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Fri, 1 Jun 2018 15:33:25 +0200 Subject: [PATCH 080/103] atmel-samd/samd51: Refactor clock setup Refactor the convoluted asf4 clock setup into something more readable. enable_clock_generator() has 2 changes: - Set 'Output enabled' to match the current clock setup - Handle divisors above 511 Add an enable_clock_generator_sync() version which makes it possible to setup clocks without waiting for syncing. The bootup would hang without this. I have checked these registers: NVMCTRL->CTRLA = 0x00000004 Peripheral clocks (only non-zero shown): PCHCTRL[1]=0x00000045 PCHCTRL[10]=0x00000041 Generator clocks (only non-zero shown): GENCTRL[0] = 0x00010907 GENCTRL[1] = 0x00010906 -GENCTRL[2] = 0x00041104 +GENCTRL[2] = 0x00200904 GENCTRL[4] = 0x00010907 GENCTRL[5] = 0x00180906 DFLL clock: OSCCTRL->DFLLCTRLA = 0x00000082 OSCCTRL->DFLLCTRLB = 0x00000000 OSCCTRL->DFLLVAL = 0x00008082 OSCCTRL->DFLLMUL = 0x00000000 DPLL clocks: OSCCTRL->Dpll[0].DPLLCTRLA=0x00000002 OSCCTRL->Dpll[0].DPLLCTRLB=0x00000000 OSCCTRL->Dpll[0].DPLLRATIO=0x0000003b OSCCTRL->Dpll[1].DPLLCTRLA=0x00000080 OSCCTRL->Dpll[1].DPLLCTRLB=0x00000020 OSCCTRL->Dpll[1].DPLLRATIO=0x00000000 OSC32KCTRL clock: OSC32KCTRL->RTCCTRL = 0x00000000 OSC32KCTRL->XOSC32K = 0x00002082 OSC32KCTRL->CFDCTRL = 0x00000000 OSC32KCTRL->EVCTRL = 0x00000000 OSC32KCTRL->OSCULP32K = 0x00002300 Only gen2 changed which is due to samd51 having more bits in the simple division register so DIVSEL wasn't necessary, and it didn't have OE set. --- ports/atmel-samd/samd51_clocks.c | 62 +++++++++++++++++++++++++++++- ports/atmel-samd/supervisor/port.c | 6 +-- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/ports/atmel-samd/samd51_clocks.c b/ports/atmel-samd/samd51_clocks.c index d5cb8aed03..7424636a40 100644 --- a/ports/atmel-samd/samd51_clocks.c +++ b/ports/atmel-samd/samd51_clocks.c @@ -52,9 +52,26 @@ void disconnect_gclk_from_peripheral(uint8_t gclk, uint8_t peripheral) { GCLK->PCHCTRL[peripheral].reg = 0; } +static void enable_clock_generator_sync(uint8_t gclk, uint32_t source, uint16_t divisor, bool sync) { + uint32_t divsel = 0; + // The datasheet says 8 bits and max value of 512, how is that possible? + if (divisor > 255) { // Generator 1 has 16 bits + divsel = GCLK_GENCTRL_DIVSEL; + for (int i = 15; i > 0; i--) { + if (divisor & (1 << i)) { + divisor = i - 1; + break; + } + } + } + + GCLK->GENCTRL[gclk].reg = GCLK_GENCTRL_SRC(source) | GCLK_GENCTRL_DIV(divisor) | divsel | GCLK_GENCTRL_OE | GCLK_GENCTRL_GENEN; + if (sync) + while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {} +} + void enable_clock_generator(uint8_t gclk, uint32_t source, uint16_t divisor) { - GCLK->GENCTRL[gclk].reg = GCLK_GENCTRL_SRC(source) | GCLK_GENCTRL_DIV(divisor) | GCLK_GENCTRL_GENEN; - while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {} + enable_clock_generator_sync(gclk, source, divisor, true); } void disable_clock_generator(uint8_t gclk) { @@ -62,6 +79,47 @@ void disable_clock_generator(uint8_t gclk) { while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {} } +static void init_clock_source_osculp32k(void) { + // Calibration value is loaded at startup + OSC32KCTRL->OSCULP32K.bit.EN1K = 0; + OSC32KCTRL->OSCULP32K.bit.EN32K = 0; +} + +static void init_clock_source_xosc32k(void) { + OSC32KCTRL->XOSC32K.reg = OSC32KCTRL_XOSC32K_ONDEMAND | + OSC32KCTRL_XOSC32K_ENABLE | + OSC32KCTRL_XOSC32K_CGM(1); +} + +static void init_clock_source_dpll0(void) +{ + GCLK->PCHCTRL[OSCCTRL_GCLK_ID_FDPLL0].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(5); + OSCCTRL->Dpll[0].DPLLRATIO.reg = OSCCTRL_DPLLRATIO_LDRFRAC(0) | OSCCTRL_DPLLRATIO_LDR(59); + OSCCTRL->Dpll[0].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_REFCLK(0); + OSCCTRL->Dpll[0].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_ENABLE; + + while (!(OSCCTRL->Dpll[0].DPLLSTATUS.bit.LOCK || OSCCTRL->Dpll[0].DPLLSTATUS.bit.CLKRDY)) {} +} + +void clock_init(void) { + // DFLL48M is enabled by default + + init_clock_source_osculp32k(); + init_clock_source_xosc32k(); + + OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val; + + MCLK->CPUDIV.reg = MCLK_CPUDIV_DIV(1); + + enable_clock_generator_sync(0, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false); + enable_clock_generator_sync(1, GCLK_GENCTRL_SRC_DFLL_Val, 1, false); + enable_clock_generator_sync(2, GCLK_GENCTRL_SRC_OSCULP32K_Val, 32, false); + enable_clock_generator_sync(4, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false); + enable_clock_generator_sync(5, GCLK_GENCTRL_SRC_DFLL_Val, 24, false); + + init_clock_source_dpll0(); +} + static bool clk_enabled(uint8_t clk) { return GCLK->PCHCTRL[clk].bit.CHEN; } diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index a92ab2913f..55304e8bab 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -181,11 +181,9 @@ safe_mode_t port_init(void) { #ifdef SAMD21 hri_nvmctrl_set_CTRLB_RWS_bf(NVMCTRL, 2); _pm_init(); +#endif clock_init(); -#endif -#ifdef SAMD51 - init_mcu(); -#endif + board_init(); // Configure millisecond timer initialization. From e158702a68637a975c97c34cc0a564c4d693faa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Fri, 1 Jun 2018 16:12:48 +0200 Subject: [PATCH 081/103] atmel-samd/samd51: Use crystal for RTC This uses the crystal to clock the RTC on boards which have a crystal. Disable clock generator 2 which was enabled in commit 8e2080411f65 ("atmel-samd: Add rtc module support"). samd51 differs from samd21 when it comes to the RTC clock. samd51 doesn't have an explicit clock peripheral so no need for a clock generator. The same commit didn't even setup XOSC32K correctly, it missed EN1K and XTALEN. The RTC uses the 1k clock output, so enable it on the OSCULP32K even if it works without it. --- .../boards/feather_m4_express/mpconfigboard.h | 2 ++ .../boards/metro_m4_express/mpconfigboard.h | 2 ++ ports/atmel-samd/samd51_clocks.c | 13 +++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h index 64b22acd57..b5af61ba6f 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h @@ -32,6 +32,8 @@ #include "external_flash/external_flash.h" +#define BOARD_HAS_CRYSTAL 1 + #define DEFAULT_I2C_BUS_SCL (&pin_PA13) #define DEFAULT_I2C_BUS_SDA (&pin_PA12) diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index cda43d95e6..7aef929f7c 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -33,6 +33,8 @@ #include "external_flash/external_flash.h" +#define BOARD_HAS_CRYSTAL 1 + #define DEFAULT_I2C_BUS_SCL (&pin_PB03) #define DEFAULT_I2C_BUS_SDA (&pin_PB02) diff --git a/ports/atmel-samd/samd51_clocks.c b/ports/atmel-samd/samd51_clocks.c index 7424636a40..6f8b1f0102 100644 --- a/ports/atmel-samd/samd51_clocks.c +++ b/ports/atmel-samd/samd51_clocks.c @@ -81,12 +81,14 @@ void disable_clock_generator(uint8_t gclk) { static void init_clock_source_osculp32k(void) { // Calibration value is loaded at startup - OSC32KCTRL->OSCULP32K.bit.EN1K = 0; + OSC32KCTRL->OSCULP32K.bit.EN1K = 1; OSC32KCTRL->OSCULP32K.bit.EN32K = 0; } static void init_clock_source_xosc32k(void) { OSC32KCTRL->XOSC32K.reg = OSC32KCTRL_XOSC32K_ONDEMAND | + OSC32KCTRL_XOSC32K_EN1K | + OSC32KCTRL_XOSC32K_XTALEN | OSC32KCTRL_XOSC32K_ENABLE | OSC32KCTRL_XOSC32K_CGM(1); } @@ -105,15 +107,18 @@ void clock_init(void) { // DFLL48M is enabled by default init_clock_source_osculp32k(); - init_clock_source_xosc32k(); - OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val; + if (board_has_crystal()) { + init_clock_source_xosc32k(); + OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val; + } else { + OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val; + } MCLK->CPUDIV.reg = MCLK_CPUDIV_DIV(1); enable_clock_generator_sync(0, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false); enable_clock_generator_sync(1, GCLK_GENCTRL_SRC_DFLL_Val, 1, false); - enable_clock_generator_sync(2, GCLK_GENCTRL_SRC_OSCULP32K_Val, 32, false); enable_clock_generator_sync(4, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false); enable_clock_generator_sync(5, GCLK_GENCTRL_SRC_DFLL_Val, 24, false); From bf1f0b3d117dd772ae47b6899f49d8caf1e66947 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 1 Jun 2018 13:32:45 -0700 Subject: [PATCH 082/103] Turn off timer on AudioOut deinit. Thanks to @sommersoft for spotting the error. Fixes #850 --- ports/atmel-samd/common-hal/audioio/AudioOut.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/atmel-samd/common-hal/audioio/AudioOut.c b/ports/atmel-samd/common-hal/audioio/AudioOut.c index 6971c020b4..331ae9c9b5 100644 --- a/ports/atmel-samd/common-hal/audioio/AudioOut.c +++ b/ports/atmel-samd/common-hal/audioio/AudioOut.c @@ -240,6 +240,8 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) { disable_event_channel(self->tc_to_dac_event_channel); + tc_set_enable(tc_insts[self->tc_index], false); + reset_pin(self->left_channel->pin); self->left_channel = mp_const_none; #ifdef SAMD51 From f38ce1060ca231e4542619d5401e46d22a92c009 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 1 Jun 2018 13:45:28 -0700 Subject: [PATCH 083/103] Support brownout to safe mode. Fixes #870 --- ports/atmel-samd/supervisor/port.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index a92ab2913f..63c143c2a8 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -203,9 +203,16 @@ safe_mode_t port_init(void) { } #endif - // if (PM->RCAUSE.bit.BOD33 == 1 || PM->RCAUSE.bit.BOD12 == 1) { - // return BROWNOUT; - // } + #ifdef SAMD21 + if (PM->RCAUSE.bit.BOD33 == 1 || PM->RCAUSE.bit.BOD12 == 1) { + return BROWNOUT; + } + #endif + #ifdef SAMD51 + if (RSTC->RCAUSE.bit.BODVDD == 1 || RSTC->RCAUSE.bit.BODCORE == 1) { + return BROWNOUT; + } + #endif if (board_requests_safe_mode()) { return USER_SAFE_MODE; From fd71e56891749837a823a4b555b84150b6fbc1e8 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 25 May 2018 18:39:16 -0700 Subject: [PATCH 084/103] atmel-samd: Re-org helper peripheral files into their own subdirectory. Ideally in the future they won't depend on ASF4 or MicroPython. --- conf.py | 1 + ports/atmel-samd/Makefile | 25 ++- ports/atmel-samd/audio_dma.c | 6 +- ports/atmel-samd/bindings/samd/Clock.c | 3 +- ports/atmel-samd/board_busses.c | 6 +- ports/atmel-samd/boards/arduino_zero/pins.c | 2 - .../boards/circuitplayground_express/board.c | 1 - .../boards/circuitplayground_express/pins.c | 3 +- .../circuitplayground_express_crickit/board.c | 1 - .../circuitplayground_express_crickit/pins.c | 3 +- .../boards/feather_m0_adalogger/pins.c | 3 +- .../atmel-samd/boards/feather_m0_basic/pins.c | 3 +- .../boards/feather_m0_express/pins.c | 3 +- .../atmel-samd/boards/feather_m0_rfm69/pins.c | 3 +- .../atmel-samd/boards/feather_m0_rfm9x/pins.c | 3 +- .../boards/feather_m0_supersized/pins.c | 3 +- .../boards/feather_m4_express/pins.c | 3 +- ports/atmel-samd/boards/gemma_m0/pins.c | 3 +- .../boards/itsybitsy_m0_express/pins.c | 3 +- .../boards/itsybitsy_m4_express/pins.c | 3 +- .../atmel-samd/boards/metro_m0_express/pins.c | 3 +- .../atmel-samd/boards/metro_m4_express/pins.c | 3 +- .../metro_m4_express_revb/mpconfigboard.h | 72 -------- .../metro_m4_express_revb/mpconfigboard.mk | 11 -- .../boards/metro_m4_express_revb/pins.c | 48 ----- ports/atmel-samd/boards/pirkey_m0/pins.c | 3 +- ports/atmel-samd/boards/trinket_m0/pins.c | 3 +- .../boards/trinket_m0_haxpress/pins.c | 3 +- ports/atmel-samd/boards/ugame10/pins.c | 3 +- .../atmel-samd/common-hal/analogio/AnalogIn.c | 2 +- .../common-hal/analogio/AnalogOut.c | 6 +- .../atmel-samd/common-hal/audiobusio/I2SOut.c | 13 +- .../atmel-samd/common-hal/audiobusio/PDMIn.c | 11 +- .../atmel-samd/common-hal/audioio/AudioOut.c | 9 +- ports/atmel-samd/common-hal/busio/I2C.c | 3 +- ports/atmel-samd/common-hal/busio/SPI.c | 6 +- ports/atmel-samd/common-hal/busio/UART.c | 3 +- .../common-hal/microcontroller/Pin.c | 7 +- .../common-hal/microcontroller/Pin.h | 2 + .../common-hal/microcontroller/Processor.c | 2 +- .../common-hal/microcontroller/__init__.c | 2 +- ports/atmel-samd/common-hal/pulseio/PWMOut.c | 4 +- ports/atmel-samd/common-hal/pulseio/PulseIn.c | 4 +- .../atmel-samd/common-hal/pulseio/PulseOut.c | 4 +- .../common-hal/rotaryio/IncrementalEncoder.c | 158 +++++++++++++++++ .../common-hal/rotaryio/IncrementalEncoder.h | 40 +++++ .../atmel-samd/common-hal/rotaryio/__init__.c | 1 + ports/atmel-samd/common-hal/touchio/TouchIn.c | 6 +- .../external_flash/external_flash.c | 4 +- ports/atmel-samd/external_flash/qspi_flash.c | 4 +- ports/atmel-samd/external_flash/spi_flash.c | 4 +- ports/atmel-samd/mpconfigport.h | 4 +- .../adc.h} | 9 +- .../cache.h} | 8 - ports/atmel-samd/{ => peripherals}/clocks.c | 0 ports/atmel-samd/{ => peripherals}/clocks.h | 0 .../{shared_dma.c => peripherals/dma.c} | 164 +---------------- .../{shared_dma.h => peripherals/dma.h} | 9 +- ports/atmel-samd/peripherals/events.c | 62 +++++++ ports/atmel-samd/{ => peripherals}/events.h | 2 + ports/atmel-samd/peripherals/i2s.c | 46 +++++ ports/atmel-samd/{ => peripherals}/i2s.h | 0 ports/atmel-samd/peripherals/pins.h | 42 +++++ ports/atmel-samd/peripherals/samd21/adc.c | 47 +++++ .../{pins.h => peripherals/samd21/cache.c} | 19 +- .../samd21/clocks.c} | 2 +- ports/atmel-samd/peripherals/samd21/dma.c | 118 +++++++++++++ .../{ => peripherals/samd21}/events.c | 107 +---------- .../board.c => peripherals/samd21/i2s.c} | 32 ++-- .../samd21/pins.c} | 2 - .../samd21/pins.h} | 11 +- .../samd21/sercom.c} | 20 --- ports/atmel-samd/peripherals/samd21/timers.c | 75 ++++++++ ports/atmel-samd/peripherals/samd51/adc.c | 61 +++++++ ports/atmel-samd/peripherals/samd51/cache.c | 39 ++++ .../samd51/clocks.c} | 2 +- ports/atmel-samd/peripherals/samd51/dma.c | 94 ++++++++++ ports/atmel-samd/peripherals/samd51/events.c | 88 ++++++++++ .../atmel-samd/{ => peripherals/samd51}/i2s.c | 36 +--- .../samd51/pins.c} | 2 - .../samd51/pins.h} | 5 +- .../samd51/sercom.c} | 46 +---- .../{ => peripherals/samd51}/timers.c | 166 +----------------- .../{peripherals.c => peripherals/sercom.c} | 2 +- .../{peripherals.h => peripherals/sercom.h} | 17 +- ports/atmel-samd/peripherals/timers.c | 147 ++++++++++++++++ ports/atmel-samd/{ => peripherals}/timers.h | 0 ports/atmel-samd/supervisor/port.c | 8 +- shared-bindings/board/__init__.h | 2 + shared-bindings/rotaryio/IncrementalEncoder.c | 159 +++++++++++++++++ shared-bindings/rotaryio/IncrementalEncoder.h | 41 +++++ shared-bindings/rotaryio/__init__.c | 75 ++++++++ shared-bindings/rotaryio/__init__.h | 34 ++++ supervisor/shared/rgb_led_status.c | 1 - 94 files changed, 1505 insertions(+), 824 deletions(-) delete mode 100644 ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h delete mode 100644 ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk delete mode 100644 ports/atmel-samd/boards/metro_m4_express_revb/pins.c create mode 100644 ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c create mode 100644 ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h create mode 100644 ports/atmel-samd/common-hal/rotaryio/__init__.c rename ports/atmel-samd/{samd21_peripherals.h => peripherals/adc.h} (78%) rename ports/atmel-samd/{samd51_peripherals.h => peripherals/cache.h} (81%) rename ports/atmel-samd/{ => peripherals}/clocks.c (100%) rename ports/atmel-samd/{ => peripherals}/clocks.h (100%) rename ports/atmel-samd/{shared_dma.c => peripherals/dma.c} (62%) rename ports/atmel-samd/{shared_dma.h => peripherals/dma.h} (91%) create mode 100644 ports/atmel-samd/peripherals/events.c rename ports/atmel-samd/{ => peripherals}/events.h (97%) create mode 100644 ports/atmel-samd/peripherals/i2s.c rename ports/atmel-samd/{ => peripherals}/i2s.h (100%) create mode 100644 ports/atmel-samd/peripherals/pins.h create mode 100644 ports/atmel-samd/peripherals/samd21/adc.c rename ports/atmel-samd/{pins.h => peripherals/samd21/cache.c} (79%) rename ports/atmel-samd/{samd21_clocks.c => peripherals/samd21/clocks.c} (99%) create mode 100644 ports/atmel-samd/peripherals/samd21/dma.c rename ports/atmel-samd/{ => peripherals/samd21}/events.c (60%) rename ports/atmel-samd/{boards/metro_m4_express_revb/board.c => peripherals/samd21/i2s.c} (65%) rename ports/atmel-samd/{samd21_pins.c => peripherals/samd21/pins.c} (99%) rename ports/atmel-samd/{samd21_pins.h => peripherals/samd21/pins.h} (93%) rename ports/atmel-samd/{samd21_peripherals.c => peripherals/samd21/sercom.c} (76%) create mode 100644 ports/atmel-samd/peripherals/samd21/timers.c create mode 100644 ports/atmel-samd/peripherals/samd51/adc.c create mode 100644 ports/atmel-samd/peripherals/samd51/cache.c rename ports/atmel-samd/{samd51_clocks.c => peripherals/samd51/clocks.c} (99%) create mode 100644 ports/atmel-samd/peripherals/samd51/dma.c create mode 100644 ports/atmel-samd/peripherals/samd51/events.c rename ports/atmel-samd/{ => peripherals/samd51}/i2s.c (68%) rename ports/atmel-samd/{samd51_pins.c => peripherals/samd51/pins.c} (99%) rename ports/atmel-samd/{samd51_pins.h => peripherals/samd51/pins.h} (98%) rename ports/atmel-samd/{samd51_peripherals.c => peripherals/samd51/sercom.c} (67%) rename ports/atmel-samd/{ => peripherals/samd51}/timers.c (56%) rename ports/atmel-samd/{peripherals.c => peripherals/sercom.c} (98%) rename ports/atmel-samd/{peripherals.h => peripherals/sercom.h} (79%) create mode 100644 ports/atmel-samd/peripherals/timers.c rename ports/atmel-samd/{ => peripherals}/timers.h (100%) create mode 100644 shared-bindings/rotaryio/IncrementalEncoder.c create mode 100644 shared-bindings/rotaryio/IncrementalEncoder.h create mode 100644 shared-bindings/rotaryio/__init__.c create mode 100644 shared-bindings/rotaryio/__init__.h diff --git a/conf.py b/conf.py index eb4b1490f7..03d100f121 100644 --- a/conf.py +++ b/conf.py @@ -101,6 +101,7 @@ exclude_patterns = ["**/build*", "ports/atmel-samd/asf4_conf", "ports/atmel-samd/external_flash", "ports/atmel-samd/freetouch", + "ports/atmel-samd/peripherals", "ports/atmel-samd/QTouch", "ports/atmel-samd/tools", "ports/bare-arm", diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index d627e76410..162e6b3e61 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -242,19 +242,24 @@ SRC_C = \ audio_dma.c \ board_busses.c \ background.c \ - clocks.c \ - $(CHIP_FAMILY)_clocks.c \ - events.c \ fatfs_port.c \ flash_api.c \ mphalport.c \ reset.c \ - $(CHIP_FAMILY)_peripherals.c \ - peripherals.c \ - $(CHIP_FAMILY)_pins.c \ - shared_dma.c \ + peripherals/clocks.c \ + peripherals/dma.c \ + peripherals/events.c \ + peripherals/sercom.c \ + peripherals/timers.c \ + peripherals/$(CHIP_FAMILY)/adc.c \ + peripherals/$(CHIP_FAMILY)/cache.c \ + peripherals/$(CHIP_FAMILY)/clocks.c \ + peripherals/$(CHIP_FAMILY)/dma.c \ + peripherals/$(CHIP_FAMILY)/events.c \ + peripherals/$(CHIP_FAMILY)/pins.c \ + peripherals/$(CHIP_FAMILY)/sercom.c \ + peripherals/$(CHIP_FAMILY)/timers.c \ tick.c \ - timers.c \ usb.c \ usb_mass_storage.c \ bindings/samd/__init__.c \ @@ -301,6 +306,8 @@ SRC_COMMON_HAL = \ microcontroller/Processor.c \ neopixel_write/__init__.c \ os/__init__.c \ + rotaryio/__init__.c \ + rotaryio/IncrementalEncoder.c \ rtc/__init__.c \ rtc/RTC.c \ storage/__init__.c \ @@ -391,7 +398,7 @@ ifneq ($(CHIP_VARIANT),SAMD51G18A) audiobusio/__init__.c \ audiobusio/I2SOut.c \ audiobusio/PDMIn.c - SRC_C += i2s.c + SRC_C += peripherals/i2s.c peripherals/$(CHIP_FAMILY)/i2s.c endif endif diff --git a/ports/atmel-samd/audio_dma.c b/ports/atmel-samd/audio_dma.c index e8dbdc61d6..5a572e9d1f 100644 --- a/ports/atmel-samd/audio_dma.c +++ b/ports/atmel-samd/audio_dma.c @@ -25,9 +25,9 @@ */ #include "audio_dma.h" -#include "clocks.h" -#include "events.h" -#include "shared_dma.h" +#include "peripherals/clocks.h" +#include "peripherals/events.h" +#include "peripherals/dma.h" #include "shared-bindings/audioio/RawSample.h" #include "shared-bindings/audioio/WaveFile.h" diff --git a/ports/atmel-samd/bindings/samd/Clock.c b/ports/atmel-samd/bindings/samd/Clock.c index 81e8fb1cec..3c87177a46 100644 --- a/ports/atmel-samd/bindings/samd/Clock.c +++ b/ports/atmel-samd/bindings/samd/Clock.c @@ -24,9 +24,8 @@ * THE SOFTWARE. */ -#include "clocks.h" #include "bindings/samd/Clock.h" - +#include "peripherals/clocks.h" #include "py/obj.h" #include "py/objproperty.h" #include "py/runtime.h" diff --git a/ports/atmel-samd/board_busses.c b/ports/atmel-samd/board_busses.c index 02d08133c7..008391bf0a 100644 --- a/ports/atmel-samd/board_busses.c +++ b/ports/atmel-samd/board_busses.c @@ -30,11 +30,9 @@ #include "shared-bindings/microcontroller/Pin.h" #include "mpconfigboard.h" -#include "pins.h" +#include "peripherals/pins.h" #include "py/runtime.h" - - #if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) STATIC mp_obj_t board_i2c(void) { mp_raise_NotImplementedError("No default I2C bus"); @@ -112,4 +110,4 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); return uart_singleton; } #endif -MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); \ No newline at end of file +MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); diff --git a/ports/atmel-samd/boards/arduino_zero/pins.c b/ports/atmel-samd/boards/arduino_zero/pins.c index 4fa417b373..8a7ff70e6d 100644 --- a/ports/atmel-samd/boards/arduino_zero/pins.c +++ b/ports/atmel-samd/boards/arduino_zero/pins.c @@ -1,9 +1,7 @@ #include "shared-bindings/board/__init__.h" -#include "samd21_pins.h" #include "board_busses.h" - STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PB08) }, diff --git a/ports/atmel-samd/boards/circuitplayground_express/board.c b/ports/atmel-samd/boards/circuitplayground_express/board.c index eb7ffab4a2..5c5e9f7faf 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/board.c +++ b/ports/atmel-samd/boards/circuitplayground_express/board.c @@ -31,7 +31,6 @@ #include "hal/include/hal_gpio.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/neopixel_write/__init__.h" -#include "samd21_pins.h" void board_init(void) { diff --git a/ports/atmel-samd/boards/circuitplayground_express/pins.c b/ports/atmel-samd/boards/circuitplayground_express/pins.c index f4540573a8..14ae89c55e 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c b/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c index eb7ffab4a2..5c5e9f7faf 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/board.c @@ -31,7 +31,6 @@ #include "hal/include/hal_gpio.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/neopixel_write/__init__.h" -#include "samd21_pins.h" void board_init(void) { diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c b/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c index f4540573a8..14ae89c55e 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c index 7eff106e82..0754617ddd 100644 --- a/ports/atmel-samd/boards/feather_m0_adalogger/pins.c +++ b/ports/atmel-samd/boards/feather_m0_adalogger/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_basic/pins.c b/ports/atmel-samd/boards/feather_m0_basic/pins.c index b3edabe812..4486d27ab4 100644 --- a/ports/atmel-samd/boards/feather_m0_basic/pins.c +++ b/ports/atmel-samd/boards/feather_m0_basic/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_express/pins.c b/ports/atmel-samd/boards/feather_m0_express/pins.c index 6bd74f3729..68666c273e 100644 --- a/ports/atmel-samd/boards/feather_m0_express/pins.c +++ b/ports/atmel-samd/boards/feather_m0_express/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c index e6cede3dd9..6055f8a1b2 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm69/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm69/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c index 8f52a5767c..7395c6e2f1 100644 --- a/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c +++ b/ports/atmel-samd/boards/feather_m0_rfm9x/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m0_supersized/pins.c b/ports/atmel-samd/boards/feather_m0_supersized/pins.c index 6bd74f3729..68666c273e 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/pins.c +++ b/ports/atmel-samd/boards/feather_m0_supersized/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index bc3cb1f477..5767f63fad 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -1,4 +1,5 @@ -#include "samd51_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/gemma_m0/pins.c b/ports/atmel-samd/boards/gemma_m0/pins.c index 6d031c6854..196e438e0b 100644 --- a/ports/atmel-samd/boards/gemma_m0/pins.c +++ b/ports/atmel-samd/boards/gemma_m0/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c index 2b35c91aad..25407bc147 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c index fdcf039e64..3997ecda42 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/pins.c @@ -1,4 +1,5 @@ -#include "samd51_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" // This mapping only includes functional names because pins broken diff --git a/ports/atmel-samd/boards/metro_m0_express/pins.c b/ports/atmel-samd/boards/metro_m0_express/pins.c index 4cd8752bea..13f0eb0ba2 100644 --- a/ports/atmel-samd/boards/metro_m0_express/pins.c +++ b/ports/atmel-samd/boards/metro_m0_express/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/metro_m4_express/pins.c b/ports/atmel-samd/boards/metro_m4_express/pins.c index 8dc0f25d6b..0e66817f7c 100644 --- a/ports/atmel-samd/boards/metro_m4_express/pins.c +++ b/ports/atmel-samd/boards/metro_m4_express/pins.c @@ -1,4 +1,5 @@ -#include "samd51_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" // This mapping only includes functional names because pins broken diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h deleted file mode 100644 index db51140d66..0000000000 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h +++ /dev/null @@ -1,72 +0,0 @@ -// This is for Rev B which a larger run was done and sent to Adafruit community -// members. - -#define MICROPY_HW_BOARD_NAME "Metro M4 Express Rev B (Black)" -#define MICROPY_HW_MCU_NAME "samd51j19" - -#define CIRCUITPY_MCU_FAMILY samd51 - -#define MICROPY_HW_LED_TX PIN_PA27 -#define MICROPY_HW_LED_RX PIN_PB06 - -#define MICROPY_HW_NEOPIXEL (&pin_PB17) - -#define SPI_FLASH_BAUDRATE (60000000) - -// Rev B: single channel SPI -// Rev C will be QSPI -#define SPI_FLASH_MOSI_PIN PIN_PB08 -#define SPI_FLASH_MISO_PIN PIN_PB11 -#define SPI_FLASH_SCK_PIN PIN_PB09 -#define SPI_FLASH_CS_PIN PIN_PB10 -#define SPI_FLASH_MOSI_PIN_FUNCTION PINMUX_PB08D_SERCOM4_PAD0 -#define SPI_FLASH_MISO_PIN_FUNCTION PINMUX_PB11D_SERCOM4_PAD3 -#define SPI_FLASH_SCK_PIN_FUNCTION PINMUX_PB09D_SERCOM4_PAD1 -#define SPI_FLASH_SERCOM SERCOM4 -#define SPI_FLASH_SERCOM_INDEX 4 -#define SPI_FLASH_MOSI_PAD 0 -#define SPI_FLASH_MISO_PAD 3 -#define SPI_FLASH_SCK_PAD 1 -// Transmit Data Pinout -// <0x0=>PAD[0,1]_DO_SCK -// <0x1=>PAD[2,3]_DO_SCK -// <0x2=>PAD[3,1]_DO_SCK -// <0x3=>PAD[0,3]_DO_SCK -#define SPI_FLASH_DOPO 0 -#define SPI_FLASH_DIPO 3 // same as MISO pad - -// These are pins not to reset. -// Pin for TX LED -#define MICROPY_PORT_A (PORT_PA27) -// Pins for RX LED, SPI flash and neopixel -#define MICROPY_PORT_B (PORT_PB06 | PORT_PB08 | PORT_PB09 | PORT_PB10 | PORT_PB11 | PORT_PB17) -#define MICROPY_PORT_C (0) -#define MICROPY_PORT_D (0) - -#define AUTORESET_DELAY_MS 500 - -#include "external_flash/external_flash.h" - -// If you change this, then make sure to update the linker scripts as well to -// make sure you don't overwrite code -#define CIRCUITPY_INTERNAL_NVM_SIZE 8192 - -#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) - -#include "external_flash/devices.h" - -#define EXTERNAL_FLASH_DEVICE_COUNT 2 -#define EXTERNAL_FLASH_DEVICES S25FL216K, \ - GD25Q16C - -#include "external_flash/external_flash.h" - -#define DEFAULT_I2C_BUS_SCL (&pin_PB03) -#define DEFAULT_I2C_BUS_SDA (&pin_PB02) - -#define DEFAULT_SPI_BUS_SCK (&pin_PA13) -#define DEFAULT_SPI_BUS_MOSI (&pin_PA12) -#define DEFAULT_SPI_BUS_MISO (&pin_PA15) - -#define DEFAULT_UART_BUS_RX (&pin_PA23) -#define DEFAULT_UART_BUS_TX (&pin_PA22) diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk deleted file mode 100644 index 9e6965063c..0000000000 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.mk +++ /dev/null @@ -1,11 +0,0 @@ -LD_FILE = boards/samd51x19-bootloader-external-flash.ld -USB_VID = 0x239A -USB_PID = 0x8021 -USB_PRODUCT = "Metro M4 Express Rev B (Black)" -USB_MANUFACTURER = "Adafruit Industries LLC" - -SPI_FLASH_FILESYSTEM = 1 -LONGINT_IMPL = MPZ - -CHIP_VARIANT = SAMD51J19A -CHIP_FAMILY = samd51 diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c b/ports/atmel-samd/boards/metro_m4_express_revb/pins.c deleted file mode 100644 index 23e662f1c3..0000000000 --- a/ports/atmel-samd/boards/metro_m4_express_revb/pins.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "samd51_pins.h" -#include "board_busses.h" - -// This mapping only includes functional names because pins broken -// out on connectors are labeled with their MCU name available from -// microcontroller.pin. -STATIC const mp_map_elem_t board_global_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_A0), (mp_obj_t)&pin_PA02 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_A1), (mp_obj_t)&pin_PA05 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_A2), (mp_obj_t)&pin_PA06 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_A3), (mp_obj_t)&pin_PA04 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_A4), (mp_obj_t)&pin_PA11 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_A5), (mp_obj_t)&pin_PA07 }, - - - { MP_OBJ_NEW_QSTR(MP_QSTR_D0), (mp_obj_t)&pin_PA23 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_RX), (mp_obj_t)&pin_PA23 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D1), (mp_obj_t)&pin_PA22 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_TX), (mp_obj_t)&pin_PA22 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D2), (mp_obj_t)&pin_PA08 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D3), (mp_obj_t)&pin_PA10 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D4), (mp_obj_t)&pin_PB12 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D5), (mp_obj_t)&pin_PB14 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D6), (mp_obj_t)&pin_PB15 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D7), (mp_obj_t)&pin_PA14 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D8), (mp_obj_t)&pin_PA16 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D9), (mp_obj_t)&pin_PA17 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D10), (mp_obj_t)&pin_PA18 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D11), (mp_obj_t)&pin_PA19 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D12), (mp_obj_t)&pin_PA20 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_D13), (mp_obj_t)&pin_PA21 }, - - { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), (mp_obj_t)&pin_PB02 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), (mp_obj_t)&pin_PB03 }, - - { MP_OBJ_NEW_QSTR(MP_QSTR_NEOPIXEL), (mp_obj_t)&pin_PB17 }, - - { MP_OBJ_NEW_QSTR(MP_QSTR_SCK), (mp_obj_t)&pin_PA13 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_MOSI), (mp_obj_t)&pin_PA12 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_MISO), (mp_obj_t)&pin_PA15 }, - - { MP_OBJ_NEW_QSTR(MP_QSTR_LED_RX), (mp_obj_t)&pin_PB06 }, - { MP_OBJ_NEW_QSTR(MP_QSTR_LED_TX), (mp_obj_t)&pin_PA27 }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, -}; -MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/boards/pirkey_m0/pins.c b/ports/atmel-samd/boards/pirkey_m0/pins.c index 7efc8eb33f..e08302f140 100644 --- a/ports/atmel-samd/boards/pirkey_m0/pins.c +++ b/ports/atmel-samd/boards/pirkey_m0/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/trinket_m0/pins.c b/ports/atmel-samd/boards/trinket_m0/pins.c index 0fdcbc2790..6330170249 100644 --- a/ports/atmel-samd/boards/trinket_m0/pins.c +++ b/ports/atmel-samd/boards/trinket_m0/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c index 0fdcbc2790..6330170249 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/boards/ugame10/pins.c b/ports/atmel-samd/boards/ugame10/pins.c index acf73c9ffc..87ace5e96f 100644 --- a/ports/atmel-samd/boards/ugame10/pins.c +++ b/ports/atmel-samd/boards/ugame10/pins.c @@ -1,4 +1,5 @@ -#include "samd21_pins.h" +#include "shared-bindings/board/__init__.h" + #include "board_busses.h" STATIC const mp_rom_map_elem_t board_global_dict_table[] = { diff --git a/ports/atmel-samd/common-hal/analogio/AnalogIn.c b/ports/atmel-samd/common-hal/analogio/AnalogIn.c index 02613ae078..e452eb0305 100644 --- a/ports/atmel-samd/common-hal/analogio/AnalogIn.c +++ b/ports/atmel-samd/common-hal/analogio/AnalogIn.c @@ -34,7 +34,7 @@ #include "py/binary.h" #include "py/mphal.h" -#include "peripherals.h" +#include "peripherals/adc.h" #include "shared-bindings/analogio/AnalogIn.h" #include "atmel_start_pins.h" diff --git a/ports/atmel-samd/common-hal/analogio/AnalogOut.c b/ports/atmel-samd/common-hal/analogio/AnalogOut.c index 97299a8634..9ceca38683 100644 --- a/ports/atmel-samd/common-hal/analogio/AnalogOut.c +++ b/ports/atmel-samd/common-hal/analogio/AnalogOut.c @@ -42,10 +42,6 @@ #include "hpl/pm/hpl_pm_base.h" #endif -#ifdef SAMD51 -#include "samd51_pins.h" -#endif - void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { if (pin->pin != PIN_PA02 @@ -73,7 +69,7 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, #endif // SAMD21: This clock should be <= 12 MHz, per datasheet section 47.6.3. - // SAMD51: This clock should be <= 350kHz, per datasheet table 37-6. + // SAMD51: This clock should be <= 350kHz, per datasheet table 37-6. _gclk_enable_channel(DAC_GCLK_ID, CONF_GCLK_DAC_SRC); // Don't double init the DAC on the SAMD51 when both outputs are in use. We use the free state diff --git a/ports/atmel-samd/common-hal/audiobusio/I2SOut.c b/ports/atmel-samd/common-hal/audiobusio/I2SOut.c index f4a8eb52e8..27a7b9ee04 100644 --- a/ports/atmel-samd/common-hal/audiobusio/I2SOut.c +++ b/ports/atmel-samd/common-hal/audiobusio/I2SOut.c @@ -45,13 +45,14 @@ #include "hpl/pm/hpl_pm_base.h" #endif +#include "peripherals/clocks.h" +#include "peripherals/dma.h" +#include "peripherals/events.h" +#include "peripherals/i2s.h" +#include "peripherals/pins.h" +#include "peripherals/timers.h" + #include "audio_dma.h" -#include "clocks.h" -#include "events.h" -#include "i2s.h" -#include "pins.h" -#include "shared_dma.h" -#include "timers.h" #ifdef SAMD21 #define SERCTRL(name) I2S_SERCTRL_ ## name diff --git a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c index 44129eed12..5502fcd812 100644 --- a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c +++ b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c @@ -41,12 +41,13 @@ #include "hal/include/hal_gpio.h" #include "hal/utils/include/utils.h" +#include "peripherals/clocks.h" +#include "peripherals/events.h" +#include "peripherals/i2s.h" +#include "peripherals/pins.h" +#include "peripherals/dma.h" + #include "audio_dma.h" -#include "clocks.h" -#include "events.h" -#include "i2s.h" -#include "pins.h" -#include "shared_dma.h" #include "tick.h" #define OVERSAMPLING 64 diff --git a/ports/atmel-samd/common-hal/audioio/AudioOut.c b/ports/atmel-samd/common-hal/audioio/AudioOut.c index 331ae9c9b5..fc5c756731 100644 --- a/ports/atmel-samd/common-hal/audioio/AudioOut.c +++ b/ports/atmel-samd/common-hal/audioio/AudioOut.c @@ -45,10 +45,11 @@ #endif #include "audio_dma.h" -#include "events.h" -#include "samd21_pins.h" -#include "shared_dma.h" -#include "timers.h" + +#include "peripherals/dma.h" +#include "peripherals/events.h" +#include "peripherals/pins.h" +#include "peripherals/timers.h" void audioout_reset(void) { } diff --git a/ports/atmel-samd/common-hal/busio/I2C.c b/ports/atmel-samd/common-hal/busio/I2C.c index 347a2e3490..66929beff5 100644 --- a/ports/atmel-samd/common-hal/busio/I2C.c +++ b/ports/atmel-samd/common-hal/busio/I2C.c @@ -32,8 +32,7 @@ #include "hal/include/hal_i2c_m_sync.h" #include "hal/include/hpl_i2c_m_sync.h" -#include "peripherals.h" -#include "pins.h" +#include "peripherals/sercom.h" #include "shared-bindings/microcontroller/__init__.h" diff --git a/ports/atmel-samd/common-hal/busio/SPI.c b/ports/atmel-samd/common-hal/busio/SPI.c index a2bf30f054..a2ee93c6db 100644 --- a/ports/atmel-samd/common-hal/busio/SPI.c +++ b/ports/atmel-samd/common-hal/busio/SPI.c @@ -36,9 +36,9 @@ #include "hal/include/hpl_spi_m_sync.h" #include "supervisor/shared/rgb_led_status.h" -#include "peripherals.h" -#include "pins.h" -#include "shared_dma.h" +#include "peripherals/dma.h" +//#include "peripherals/pins.h" +#include "peripherals/sercom.h" void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, diff --git a/ports/atmel-samd/common-hal/busio/UART.c b/ports/atmel-samd/common-hal/busio/UART.c index 5376775206..a95ccf3acc 100644 --- a/ports/atmel-samd/common-hal/busio/UART.c +++ b/ports/atmel-samd/common-hal/busio/UART.c @@ -42,8 +42,7 @@ #include "hal/include/hal_usart_async.h" #include "hal/include/hpl_usart_async.h" -#include "peripherals.h" -#include "pins.h" +#include "peripherals/sercom.h" // Do-nothing callback needed so that usart_async code will enable rx interrupts. // See comment below re usart_async_register_callback() diff --git a/ports/atmel-samd/common-hal/microcontroller/Pin.c b/ports/atmel-samd/common-hal/microcontroller/Pin.c index e1ca5d6223..ad97581e14 100644 --- a/ports/atmel-samd/common-hal/microcontroller/Pin.c +++ b/ports/atmel-samd/common-hal/microcontroller/Pin.c @@ -29,13 +29,8 @@ #include "atmel_start_pins.h" #include "hal/include/hal_gpio.h" +#include "peripherals/pins.h" #include "supervisor/shared/rgb_led_status.h" -#ifdef SAMD21 -#include "samd21_pins.h" -#endif -#ifdef SAMD51 -#include "samd51_pins.h" -#endif #ifdef MICROPY_HW_NEOPIXEL bool neopixel_in_use; diff --git a/ports/atmel-samd/common-hal/microcontroller/Pin.h b/ports/atmel-samd/common-hal/microcontroller/Pin.h index 4e21a14cd8..4230246a5a 100644 --- a/ports/atmel-samd/common-hal/microcontroller/Pin.h +++ b/ports/atmel-samd/common-hal/microcontroller/Pin.h @@ -81,4 +81,6 @@ void reset_all_pins(void); void reset_pin(uint8_t pin); void claim_pin(const mcu_pin_obj_t* pin); +#include "peripherals/pins.h" + #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/atmel-samd/common-hal/microcontroller/Processor.c b/ports/atmel-samd/common-hal/microcontroller/Processor.c index e7f758cd10..a76ee2e78c 100644 --- a/ports/atmel-samd/common-hal/microcontroller/Processor.c +++ b/ports/atmel-samd/common-hal/microcontroller/Processor.c @@ -63,7 +63,7 @@ #include "common-hal/microcontroller/Processor.h" -#include "peripherals.h" +#include "peripherals/adc.h" #include "peripheral_clk_config.h" diff --git a/ports/atmel-samd/common-hal/microcontroller/__init__.c b/ports/atmel-samd/common-hal/microcontroller/__init__.c index a9c83646fd..fa057facac 100644 --- a/ports/atmel-samd/common-hal/microcontroller/__init__.c +++ b/ports/atmel-samd/common-hal/microcontroller/__init__.c @@ -30,10 +30,10 @@ #include "py/runtime.h" #include "reset.h" -#include "samd21_pins.h" #include "shared-bindings/nvm/ByteArray.h" #include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Processor.h" void common_hal_mcu_delay_us(uint32_t delay) { diff --git a/ports/atmel-samd/common-hal/pulseio/PWMOut.c b/ports/atmel-samd/common-hal/pulseio/PWMOut.c index 24cd154c2a..18d896686a 100644 --- a/ports/atmel-samd/common-hal/pulseio/PWMOut.c +++ b/ports/atmel-samd/common-hal/pulseio/PWMOut.c @@ -34,9 +34,9 @@ #include "atmel_start_pins.h" #include "hal/utils/include/utils_repeat_macro.h" -#include "timers.h" +#include "peripherals/timers.h" -#include "samd21_pins.h" +#include "peripherals/pins.h" #undef ENABLE diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index 6e3aa3abb3..266a9251e9 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2017-2018 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 @@ -34,7 +34,7 @@ #include "mpconfigport.h" #include "py/gc.h" #include "py/runtime.h" -#include "samd21_pins.h" +#include "peripherals/pins.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/pulseio/PulseIn.h" diff --git a/ports/atmel-samd/common-hal/pulseio/PulseOut.c b/ports/atmel-samd/common-hal/pulseio/PulseOut.c index 95b0bbee07..8e715d4109 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseOut.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseOut.c @@ -31,11 +31,11 @@ #include "hal/include/hal_gpio.h" #include "mpconfigport.h" +#include "peripherals/pins.h" +#include "peripherals/timers.h" #include "py/gc.h" #include "py/runtime.h" -#include "samd21_pins.h" #include "shared-bindings/pulseio/PulseOut.h" -#include "timers.h" // This timer is shared amongst all PulseOut objects under the assumption that // the code is single threaded. diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c new file mode 100644 index 0000000000..58119fa7e9 --- /dev/null +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c @@ -0,0 +1,158 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "common-hal/rotaryio/IncrementalEncoder.h" + +#include + +#include "atmel_start_pins.h" +#include "hal/include/hal_gpio.h" + +#include "mpconfigport.h" +#include "peripherals/pins.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" + +#ifdef SAMD21 +#include "hpl/gclk/hpl_gclk_base.h" +#endif + +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { +// if (!pin->has_extint) { +// mp_raise_RuntimeError("No hardware support on pin"); +// } +// uint32_t mask = 1 << pin->extint_channel; +// if (active_incrementalencoders[pin->extint_channel] != NULL || +// (eic_get_enable() == 1 && +// #ifdef SAMD51 +// ((EIC->INTENSET.bit.EXTINT & mask) != 0 || +// (EIC->EVCTRL.bit.EXTINTEO & mask) != 0))) { +// #endif +// #ifdef SAMD21 +// ((EIC->INTENSET.vec.EXTINT & mask) != 0 || +// (EIC->EVCTRL.vec.EXTINTEO & mask) != 0))) { +// #endif +// mp_raise_RuntimeError("EXTINT channel already in use"); +// } +// +// self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); +// if (self->buffer == NULL) { +// mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t)); +// } +// self->channel = pin->extint_channel; +// self->pin = pin->pin; +// self->maxlen = maxlen; +// self->idle_state = idle_state; +// self->start = 0; +// self->len = 0; +// self->first_edge = true; +// +// active_incrementalencoders[pin->extint_channel] = self; +// +// // Check to see if the EIC is enabled and start it up if its not.' +// // SAMD51 EIC can only be clocked up to 100mhz so we use the 48mhz clock. +// if (eic_get_enable() == 0) { +// #ifdef SAMD51 +// MCLK->APBAMASK.bit.EIC_ = true; +// hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, +// GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); +// #endif +// +// #ifdef SAMD21 +// PM->APBAMASK.bit.EIC_ = true; +// _gclk_enable_channel(EIC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); +// #endif +// +// +// #ifdef SAMD21 +// NVIC_DisableIRQ(EIC_IRQn); +// NVIC_ClearPendingIRQ(EIC_IRQn); +// NVIC_EnableIRQ(EIC_IRQn); +// #endif +// } +// +// gpio_set_pin_function(pin->pin, GPIO_PIN_FUNCTION_A); +// +// #ifdef SAMD51 +// NVIC_DisableIRQ(EIC_0_IRQn + self->channel); +// NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); +// NVIC_EnableIRQ(EIC_0_IRQn + self->channel); +// #endif +// +// // Set config will enable the EIC. +// incrementalencoder_set_config(self, true); +// EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; +} + +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { + //return self->pin == NO_PIN; + return true; +} + +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { + // if (common_hal_rotaryio_incrementalencoder_deinited(self)) { + // return; + // } + // uint32_t mask = 1 << self->channel; + // EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; + // #ifdef SAMD51 + // NVIC_DisableIRQ(EIC_0_IRQn + self->channel); + // NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); + // #endif + // active_incrementalencoders[self->channel] = NULL; + // reset_pin(self->pin); + // self->pin = NO_PIN; + // + // bool all_null = true; + // for (uint8_t i = 0; all_null && i < 16; i++) { + // all_null = all_null && active_incrementalencoders[i] == NULL; + // } + // #ifdef SAMD21 + // if (all_null && EIC->INTENSET.reg == 0) { + // NVIC_DisableIRQ(EIC_IRQn); + // NVIC_ClearPendingIRQ(EIC_IRQn); + // } + // #endif + // // Test if all channels are null and deinit everything if they are. + // if (all_null && EIC->EVCTRL.reg == 0 && EIC->INTENSET.reg == 0) { + // eic_set_enable(false); + // #ifdef SAMD51 + // MCLK->APBAMASK.bit.EIC_ = false; + // hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, 0); + // #endif + // + // #ifdef SAMD21 + // PM->APBAMASK.bit.EIC_ = false; + // hri_gclk_write_CLKCTRL_reg(GCLK, GCLK_CLKCTRL_ID(EIC_GCLK_ID)); + // #endif + // } +} + +mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { + return 0; +} diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h new file mode 100644 index 0000000000..c878239bcf --- /dev/null +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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_ATMEL_SAMD_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin_a; + uint8_t pin_b; +} rotaryio_incrementalencoder_obj_t; + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H diff --git a/ports/atmel-samd/common-hal/rotaryio/__init__.c b/ports/atmel-samd/common-hal/rotaryio/__init__.c new file mode 100644 index 0000000000..2bee925bc7 --- /dev/null +++ b/ports/atmel-samd/common-hal/rotaryio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.c b/ports/atmel-samd/common-hal/touchio/TouchIn.c index eda1b72c43..e39a091d79 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.c +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.c @@ -37,10 +37,10 @@ #include "hpl/pm/hpl_pm_base.h" #endif -#include "clocks.h" -#include "samd21_pins.h" -#include "tick.h" +#include "peripherals/clocks.h" +#include "peripherals/pins.h" +#include "tick.h" #include "adafruit_ptc.h" bool touch_enabled = false; diff --git a/ports/atmel-samd/external_flash/external_flash.c b/ports/atmel-samd/external_flash/external_flash.c index ccc47e5b14..b0e40fc705 100644 --- a/ports/atmel-samd/external_flash/external_flash.c +++ b/ports/atmel-samd/external_flash/external_flash.c @@ -36,12 +36,10 @@ #include "py/obj.h" #include "py/runtime.h" #include "lib/oofatfs/ff.h" -#include "peripherals.h" +//#include "peripherals.h" #include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/rgb_led_status.h" -//#include "shared_dma.h" - #include "hal_gpio.h" #include "hal_spi_m_sync.h" diff --git a/ports/atmel-samd/external_flash/qspi_flash.c b/ports/atmel-samd/external_flash/qspi_flash.c index 3fc9d31be4..ec91c21e2d 100644 --- a/ports/atmel-samd/external_flash/qspi_flash.c +++ b/ports/atmel-samd/external_flash/qspi_flash.c @@ -32,8 +32,8 @@ #include "mpconfigboard.h" // for EXTERNAL_FLASH_QSPI_DUAL #include "external_flash/common_commands.h" -#include "peripherals.h" -#include "shared_dma.h" +#include "peripherals/cache.h" +#include "peripherals/dma.h" #include "atmel_start_pins.h" #include "hal_gpio.h" diff --git a/ports/atmel-samd/external_flash/spi_flash.c b/ports/atmel-samd/external_flash/spi_flash.c index 8a8cbaef81..faad8d9e0f 100644 --- a/ports/atmel-samd/external_flash/spi_flash.c +++ b/ports/atmel-samd/external_flash/spi_flash.c @@ -29,8 +29,8 @@ #include #include "external_flash/common_commands.h" -#include "peripherals.h" -#include "shared_dma.h" +#include "peripherals/sercom.h" +#include "py/mpconfig.h" #include "hal_gpio.h" #include "hal_spi_m_sync.h" diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index cd05927b08..ee75a26c5b 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -174,6 +174,7 @@ extern const struct _mp_obj_module_t board_module; extern const struct _mp_obj_module_t math_module; extern const struct _mp_obj_module_t os_module; extern const struct _mp_obj_module_t random_module; +extern const struct _mp_obj_module_t rotaryio_module; extern const struct _mp_obj_module_t rtc_module; extern const struct _mp_obj_module_t samd_module; extern const struct _mp_obj_module_t storage_module; @@ -220,6 +221,7 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \ AUDIOBUSIO_MODULE \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_rotaryio), (mp_obj_t)&rotaryio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module } #endif #define EXPRESS_BOARD @@ -304,7 +306,7 @@ extern const struct _mp_obj_module_t usb_hid_module; #define MP_STATE_PORT MP_STATE_VM -#include "shared_dma.h" +#include "peripherals/dma.h" #define MICROPY_PORT_ROOT_POINTERS \ const char *readline_hist[8]; \ diff --git a/ports/atmel-samd/samd21_peripherals.h b/ports/atmel-samd/peripherals/adc.h similarity index 78% rename from ports/atmel-samd/samd21_peripherals.h rename to ports/atmel-samd/peripherals/adc.h index 805717695a..6530b1dcdc 100644 --- a/ports/atmel-samd/samd21_peripherals.h +++ b/ports/atmel-samd/peripherals/adc.h @@ -24,15 +24,12 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SAMD21_PERIPHERALS_H -#define MICROPY_INCLUDED_ATMEL_SAMD_SAMD21_PERIPHERALS_H +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_ADC_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_ADC_H #include "include/sam.h" #include "hal/include/hal_adc_sync.h" -void samd_peripherals_sercom_clock_init(Sercom* sercom, uint8_t sercom_index); -uint8_t samd_peripherals_get_spi_dopo(uint8_t clock_pad, uint8_t mosi_pad); -bool samd_peripherals_valid_spi_clock_pad(uint8_t clock_pad); void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance); -#endif // MICROPY_INCLUDED_ATMEL_SAMD_SAMD21_PERIPHERALS_H +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_ADC_H diff --git a/ports/atmel-samd/samd51_peripherals.h b/ports/atmel-samd/peripherals/cache.h similarity index 81% rename from ports/atmel-samd/samd51_peripherals.h rename to ports/atmel-samd/peripherals/cache.h index 6643379a5c..b260568a3b 100644 --- a/ports/atmel-samd/samd51_peripherals.h +++ b/ports/atmel-samd/peripherals/cache.h @@ -27,14 +27,6 @@ #ifndef MICROPY_INCLUDED_ATMEL_SAMD_SAMD51_PERIPHERALS_H #define MICROPY_INCLUDED_ATMEL_SAMD_SAMD51_PERIPHERALS_H -#include "sam.h" -#include "hal/include/hal_adc_sync.h" - -void samd_peripherals_sercom_clock_init(Sercom* sercom, uint8_t sercom_index); -uint8_t samd_peripherals_get_spi_dopo(uint8_t clock_pad, uint8_t mosi_pad); -bool samd_peripherals_valid_spi_clock_pad(uint8_t clock_pad); -void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance); - void samd_peripherals_disable_and_clear_cache(void); void samd_peripherals_enable_cache(void); diff --git a/ports/atmel-samd/clocks.c b/ports/atmel-samd/peripherals/clocks.c similarity index 100% rename from ports/atmel-samd/clocks.c rename to ports/atmel-samd/peripherals/clocks.c diff --git a/ports/atmel-samd/clocks.h b/ports/atmel-samd/peripherals/clocks.h similarity index 100% rename from ports/atmel-samd/clocks.h rename to ports/atmel-samd/peripherals/clocks.h diff --git a/ports/atmel-samd/shared_dma.c b/ports/atmel-samd/peripherals/dma.c similarity index 62% rename from ports/atmel-samd/shared_dma.c rename to ports/atmel-samd/peripherals/dma.c index edc2d0f1a0..664c6aea8f 100644 --- a/ports/atmel-samd/shared_dma.c +++ b/ports/atmel-samd/peripherals/dma.c @@ -23,7 +23,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "shared_dma.h" +#include "peripherals/dma.h" #include @@ -48,164 +48,6 @@ COMPILER_ALIGNED(16) static DmacDescriptor write_back_descriptors[DMA_CHANNEL_CO #define FIRST_SERCOM_TX_TRIGSRC 0x05 #endif -static uint8_t sercom_index(Sercom* sercom) { - #ifdef SAMD21 - return ((uint32_t) sercom - (uint32_t) SERCOM0) / 0x400; - #else - const Sercom* sercoms[SERCOM_INST_NUM] = SERCOM_INSTS; - for (uint8_t i = 0; i < SERCOM_INST_NUM; i++) { - if (sercoms[i] == sercom) { - return i; - } - } - return 0; - #endif -} - -void dma_configure(uint8_t channel_number, uint8_t trigsrc, bool output_event) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - DMAC->CHCTRLA.reg &= ~DMAC_CHCTRLA_ENABLE; - DMAC->CHCTRLA.reg = DMAC_CHCTRLA_SWRST; - DMAC->SWTRIGCTRL.reg &= (uint32_t)(~(1 << channel_number)); - uint32_t event_output_enable = 0; - if (output_event) { - event_output_enable = DMAC_CHCTRLB_EVOE; - } - DMAC->CHCTRLB.reg = DMAC_CHCTRLB_LVL_LVL0 | - DMAC_CHCTRLB_TRIGSRC(trigsrc) | - DMAC_CHCTRLB_TRIGACT_BEAT | - event_output_enable; - common_hal_mcu_enable_interrupts(); - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - channel->CHCTRLA.reg &= ~DMAC_CHCTRLA_ENABLE; - channel->CHCTRLA.reg = DMAC_CHCTRLA_SWRST; - if (output_event) { - channel->CHEVCTRL.reg = DMAC_CHEVCTRL_EVOE; - } - channel->CHCTRLA.reg = DMAC_CHCTRLA_TRIGSRC(trigsrc) | - DMAC_CHCTRLA_TRIGACT_BURST | - DMAC_CHCTRLA_BURSTLEN_SINGLE; - #endif -} - -void dma_enable_channel(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - // Clear any previous interrupts. - DMAC->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; - DMAC->CHCTRLA.bit.ENABLE = true; - common_hal_mcu_enable_interrupts(); - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - channel->CHCTRLA.bit.ENABLE = true; - // Clear any previous interrupts. - channel->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; - #endif -} - -void dma_disable_channel(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - DMAC->CHCTRLA.bit.ENABLE = false; - common_hal_mcu_enable_interrupts(); - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - channel->CHCTRLA.bit.ENABLE = false; - #endif -} - -void dma_suspend_channel(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - DMAC->CHCTRLB.bit.CMD = DMAC_CHCTRLB_CMD_SUSPEND_Val; - common_hal_mcu_enable_interrupts(); - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - channel->CHCTRLB.reg = DMAC_CHCTRLB_CMD_SUSPEND; - #endif -} - -void dma_resume_channel(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - DMAC->CHCTRLB.bit.CMD = DMAC_CHCTRLB_CMD_RESUME_Val; - DMAC->CHINTFLAG.reg = DMAC_CHINTFLAG_SUSP; - common_hal_mcu_enable_interrupts(); - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - channel->CHCTRLB.reg = DMAC_CHCTRLB_CMD_RESUME; - #endif -} - -bool dma_channel_enabled(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - bool enabled = DMAC->CHCTRLA.bit.ENABLE; - common_hal_mcu_enable_interrupts(); - return enabled; - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - return channel->CHCTRLA.bit.ENABLE; - #endif -} - -uint8_t dma_transfer_status(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - /** Select the DMA channel and clear software trigger */ - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - uint8_t status = DMAC->CHINTFLAG.reg; - common_hal_mcu_enable_interrupts(); - return status; - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - return channel->CHINTFLAG.reg; - #endif -} - -static bool channel_free(uint8_t channel_number) { - #ifdef SAMD21 - common_hal_mcu_disable_interrupts(); - DMAC->CHID.reg = DMAC_CHID_ID(channel_number); - bool channel_free = DMAC->CHSTATUS.reg == 0; - common_hal_mcu_enable_interrupts(); - return channel_free; - #endif - - #ifdef SAMD51 - DmacChannel* channel = &DMAC->Channel[channel_number]; - return channel->CHSTATUS.reg == 0; - #endif -} - void init_shared_dma(void) { // Turn on the clocks #ifdef SAMD51 @@ -237,8 +79,8 @@ static int32_t shared_dma_transfer(void* peripheral, const uint8_t* buffer_out, volatile uint32_t* dest, volatile uint32_t* src, uint8_t* buffer_in, uint32_t length, uint8_t tx) { - if (!channel_free(SHARED_TX_CHANNEL) || - (buffer_in != NULL && !channel_free(SHARED_RX_CHANNEL))) { + if (!dma_channel_free(SHARED_TX_CHANNEL) || + (buffer_in != NULL && !dma_channel_free(SHARED_RX_CHANNEL))) { return -1; } diff --git a/ports/atmel-samd/shared_dma.h b/ports/atmel-samd/peripherals/dma.h similarity index 91% rename from ports/atmel-samd/shared_dma.h rename to ports/atmel-samd/peripherals/dma.h index 03e5b276c8..cf256b962c 100644 --- a/ports/atmel-samd/shared_dma.h +++ b/ports/atmel-samd/peripherals/dma.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_DMA_H -#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_DMA_H +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_DMA_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_DMA_H #include #include @@ -49,6 +49,8 @@ int32_t qspi_dma_write(uint32_t address, const uint8_t* buffer, uint32_t length) int32_t qspi_dma_read(uint32_t address, uint8_t* buffer, uint32_t length); #endif +uint8_t sercom_index(Sercom* sercom); + int32_t sercom_dma_write(Sercom* sercom, const uint8_t* buffer, uint32_t length); int32_t sercom_dma_read(Sercom* sercom, uint8_t* buffer, uint32_t length, uint8_t tx); int32_t sercom_dma_transfer(Sercom* sercom, const uint8_t* buffer_out, uint8_t* buffer_in, uint32_t length); @@ -58,8 +60,9 @@ void dma_enable_channel(uint8_t channel_number); void dma_disable_channel(uint8_t channel_number); void dma_suspend_channel(uint8_t channel_number); void dma_resume_channel(uint8_t channel_number); +bool dma_channel_free(uint8_t channel_number); bool dma_channel_enabled(uint8_t channel_number); uint8_t dma_transfer_status(uint8_t channel_number); DmacDescriptor* dma_descriptor(uint8_t channel_number); -#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_DMA_H +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_DMA_H diff --git a/ports/atmel-samd/peripherals/events.c b/ports/atmel-samd/peripherals/events.c new file mode 100644 index 0000000000..9a4ab3b0ca --- /dev/null +++ b/ports/atmel-samd/peripherals/events.c @@ -0,0 +1,62 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 + +#include "peripherals/events.h" +// +// #include "clocks.h" +// +#include "py/runtime.h" + +uint8_t find_async_event_channel(void) { + int8_t channel; + for (channel = EVSYS_CHANNELS - 1; channel > 0; channel--) { + if (event_channel_free(channel)) { + break; + } + } + if (channel < 0) { + mp_raise_RuntimeError("All event channels in use"); + } + return channel; +} + +#ifdef SAMD21 +#define EVSYS_SYNCH_NUM EVSYS_CHANNELS +#endif +uint8_t find_sync_event_channel(void) { + uint8_t channel; + for (channel = 0; channel < EVSYS_SYNCH_NUM; channel++) { + if (event_channel_free(channel)) { + break; + } + } + if (channel >= EVSYS_SYNCH_NUM) { + mp_raise_RuntimeError("All sync event channels in use"); + } + return channel; +} diff --git a/ports/atmel-samd/events.h b/ports/atmel-samd/peripherals/events.h similarity index 97% rename from ports/atmel-samd/events.h rename to ports/atmel-samd/peripherals/events.h index 64093c0ab3..0073142f11 100644 --- a/ports/atmel-samd/events.h +++ b/ports/atmel-samd/peripherals/events.h @@ -44,4 +44,6 @@ void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generat bool event_interrupt_active(uint8_t channel); bool event_interrupt_overflow(uint8_t channel); +bool event_channel_free(uint8_t channel); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_EVENTS_H diff --git a/ports/atmel-samd/peripherals/i2s.c b/ports/atmel-samd/peripherals/i2s.c new file mode 100644 index 0000000000..4d99e7b5b9 --- /dev/null +++ b/ports/atmel-samd/peripherals/i2s.c @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "i2s.h" + +#include "clocks.h" + +#include "hpl/gclk/hpl_gclk_base.h" +#ifdef SAMD21 +#include "hpl/pm/hpl_pm_base.h" +#endif + +void i2s_set_enable(bool enable) { + while (I2S->SYNCBUSY.bit.ENABLE == 1) {} + I2S->CTRLA.bit.ENABLE = enable; + while (I2S->SYNCBUSY.bit.ENABLE == 1) {} +} + +void i2s_set_clock_unit_enable(uint8_t clock_unit, bool enable) { + while ((I2S->SYNCBUSY.vec.CKEN & (1 << clock_unit)) != 0) {} + I2S->CTRLA.vec.CKEN = 1 << clock_unit; + while ((I2S->SYNCBUSY.vec.CKEN & (1 << clock_unit)) != 0) {} +} diff --git a/ports/atmel-samd/i2s.h b/ports/atmel-samd/peripherals/i2s.h similarity index 100% rename from ports/atmel-samd/i2s.h rename to ports/atmel-samd/peripherals/i2s.h diff --git a/ports/atmel-samd/peripherals/pins.h b/ports/atmel-samd/peripherals/pins.h new file mode 100644 index 0000000000..1ddbf3329e --- /dev/null +++ b/ports/atmel-samd/peripherals/pins.h @@ -0,0 +1,42 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ + +// DO NOT include this file directly. Use shared-bindings/microcontroller/Pin.h instead to ensure +// that all necessary includes are already included. + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_PINS_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_PINS_H + +#include "mpconfigport.h" + +#ifdef SAMD21 +#include "peripherals/samd21/pins.h" +#endif +#ifdef SAMD51 +#include "peripherals/samd51/pins.h" +#endif + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_PINS_H diff --git a/ports/atmel-samd/peripherals/samd21/adc.c b/ports/atmel-samd/peripherals/samd21/adc.c new file mode 100644 index 0000000000..3a7f1e8116 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd21/adc.c @@ -0,0 +1,47 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Dan Halbert 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 "hal/include/hal_adc_sync.h" +#include "hpl/gclk/hpl_gclk_base.h" +#include "hpl/pm/hpl_pm_base.h" + +// Do initialization and calibration setup needed for any use of the ADC. +// The reference and resolution should be set by the caller. +void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance) { + // Turn the clocks on. + _pm_enable_bus_clock(PM_BUS_APBC, ADC); + _gclk_enable_channel(ADC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); + + adc_sync_init(adc, instance, (void *)NULL); + + // Load the factory calibration + hri_adc_write_CALIB_BIAS_CAL_bf(ADC, (*((uint32_t*) ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos); + // Bits 7:5 + uint16_t linearity = ((*((uint32_t*) ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5; + // Bits 4:0 + linearity |= (*((uint32_t*) ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; + hri_adc_write_CALIB_LINEARITY_CAL_bf(ADC, linearity); +} diff --git a/ports/atmel-samd/pins.h b/ports/atmel-samd/peripherals/samd21/cache.c similarity index 79% rename from ports/atmel-samd/pins.h rename to ports/atmel-samd/peripherals/samd21/cache.c index 1842efbc06..641b5e1155 100644 --- a/ports/atmel-samd/pins.h +++ b/ports/atmel-samd/peripherals/samd21/cache.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2017 Dan Halbert 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 @@ -24,16 +24,9 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PINS_H -#define MICROPY_INCLUDED_ATMEL_SAMD_PINS_H +// The SAMD21 doesn't have a cache so we have nothing to do. +void samd_peripherals_disable_and_clear_cache(void) { +} -#include "mpconfigport.h" - -#ifdef SAMD21 -#include "samd21_pins.h" -#endif -#ifdef SAMD51 -#include "samd51_pins.h" -#endif - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_PINS_H +void samd_peripherals_enable_cache(void) { +} diff --git a/ports/atmel-samd/samd21_clocks.c b/ports/atmel-samd/peripherals/samd21/clocks.c similarity index 99% rename from ports/atmel-samd/samd21_clocks.c rename to ports/atmel-samd/peripherals/samd21/clocks.c index 0fc4d63e50..94b5158146 100644 --- a/ports/atmel-samd/samd21_clocks.c +++ b/ports/atmel-samd/peripherals/samd21/clocks.c @@ -24,7 +24,7 @@ * THE SOFTWARE. */ -#include "clocks.h" +#include "peripherals/clocks.h" #include "hpl_gclk_config.h" diff --git a/ports/atmel-samd/peripherals/samd21/dma.c b/ports/atmel-samd/peripherals/samd21/dma.c new file mode 100644 index 0000000000..3a399745ac --- /dev/null +++ b/ports/atmel-samd/peripherals/samd21/dma.c @@ -0,0 +1,118 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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 "peripherals/dma.h" + +#include + +#include "py/gc.h" +#include "py/mpstate.h" + +#include "hal/utils/include/utils.h" + +#include "shared-bindings/microcontroller/__init__.h" + +uint8_t sercom_index(Sercom* sercom) { + return ((uint32_t) sercom - (uint32_t) SERCOM0) / 0x400; +} + +void dma_configure(uint8_t channel_number, uint8_t trigsrc, bool output_event) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + DMAC->CHCTRLA.reg &= ~DMAC_CHCTRLA_ENABLE; + DMAC->CHCTRLA.reg = DMAC_CHCTRLA_SWRST; + DMAC->SWTRIGCTRL.reg &= (uint32_t)(~(1 << channel_number)); + uint32_t event_output_enable = 0; + if (output_event) { + event_output_enable = DMAC_CHCTRLB_EVOE; + } + DMAC->CHCTRLB.reg = DMAC_CHCTRLB_LVL_LVL0 | + DMAC_CHCTRLB_TRIGSRC(trigsrc) | + DMAC_CHCTRLB_TRIGACT_BEAT | + event_output_enable; + common_hal_mcu_enable_interrupts(); +} + +void dma_enable_channel(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + // Clear any previous interrupts. + DMAC->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; + DMAC->CHCTRLA.bit.ENABLE = true; + common_hal_mcu_enable_interrupts(); +} + +void dma_disable_channel(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + DMAC->CHCTRLA.bit.ENABLE = false; + common_hal_mcu_enable_interrupts(); +} + +void dma_suspend_channel(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + DMAC->CHCTRLB.bit.CMD = DMAC_CHCTRLB_CMD_SUSPEND_Val; + common_hal_mcu_enable_interrupts(); +} + +void dma_resume_channel(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + DMAC->CHCTRLB.bit.CMD = DMAC_CHCTRLB_CMD_RESUME_Val; + DMAC->CHINTFLAG.reg = DMAC_CHINTFLAG_SUSP; + common_hal_mcu_enable_interrupts(); +} + +bool dma_channel_enabled(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + bool enabled = DMAC->CHCTRLA.bit.ENABLE; + common_hal_mcu_enable_interrupts(); + return enabled; +} + +uint8_t dma_transfer_status(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + /** Select the DMA channel and clear software trigger */ + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + uint8_t status = DMAC->CHINTFLAG.reg; + common_hal_mcu_enable_interrupts(); + return status; +} + +bool dma_channel_free(uint8_t channel_number) { + common_hal_mcu_disable_interrupts(); + DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + bool channel_free = DMAC->CHSTATUS.reg == 0; + common_hal_mcu_enable_interrupts(); + return channel_free; +} diff --git a/ports/atmel-samd/events.c b/ports/atmel-samd/peripherals/samd21/events.c similarity index 60% rename from ports/atmel-samd/events.c rename to ports/atmel-samd/peripherals/samd21/events.c index 05a2ae8a73..70dfa249dd 100644 --- a/ports/atmel-samd/events.c +++ b/ports/atmel-samd/peripherals/samd21/events.c @@ -24,131 +24,52 @@ * THE SOFTWARE. */ -#include "events.h" +#include "peripherals/events.h" -#include "clocks.h" +#include "peripherals/clocks.h" #include "py/runtime.h" -#ifdef SAMD21 #include "hpl/pm/hpl_pm_base.h" -#endif - -#ifdef SAMD51 -#include "hri/hri_mclk_d51.h" -#endif void turn_on_event_system(void) { - #ifdef SAMD51 - hri_mclk_set_APBBMASK_EVSYS_bit(MCLK); - #endif - - #ifdef SAMD21 _pm_enable_bus_clock(PM_BUS_APBC, EVSYS); - #endif } void reset_event_system(void) { - #ifdef SAMD51 - EVSYS->CTRLA.bit.SWRST = true; - hri_mclk_clear_APBBMASK_EVSYS_bit(MCLK); - #endif - - #ifdef SAMD21 EVSYS->CTRL.bit.SWRST = true; _pm_disable_bus_clock(PM_BUS_APBC, EVSYS); - #endif } -static bool channel_free(int8_t channel) { +bool event_channel_free(uint8_t channel) { uint8_t generator; - #ifdef SAMD51 - generator = EVSYS->Channel[channel].CHANNEL.bit.EVGEN; - #endif - #ifdef SAMD21 // Explicitly do a byte write so the peripheral knows we're just wanting to read the channel // rather than write to it. *((uint8_t*) &EVSYS->CHANNEL.reg) = channel; generator = (EVSYS->CHANNEL.reg & EVSYS_CHANNEL_EVGEN_Msk) >> EVSYS_CHANNEL_EVGEN_Pos; - #endif return generator == 0; } -uint8_t find_async_event_channel(void) { - int8_t channel; - for (channel = EVSYS_CHANNELS - 1; channel > 0; channel--) { - if (channel_free(channel)) { - break; - } - } - if (channel < 0) { - mp_raise_RuntimeError("All event channels in use"); - } - return channel; -} - -#ifdef SAMD21 -#define EVSYS_SYNCH_NUM EVSYS_CHANNELS -#endif -uint8_t find_sync_event_channel(void) { - uint8_t channel; - for (channel = 0; channel < EVSYS_SYNCH_NUM; channel++) { - if (channel_free(channel)) { - break; - } - } - if (channel >= EVSYS_SYNCH_NUM) { - mp_raise_RuntimeError("All sync event channels in use"); - } - return channel; -} - void disable_event_channel(uint8_t channel_number) { - #ifdef SAMD51 - EVSYS->Channel[channel_number].CHANNEL.reg = 0; - #endif - #ifdef SAMD21 EVSYS->CHANNEL.reg = EVSYS_CHANNEL_CHANNEL(channel_number); - #endif } void disable_event_user(uint8_t user_number) { - #ifdef SAMD51 - EVSYS->USER[user_number].reg = 0; - #endif - #ifdef SAMD21 EVSYS->USER.reg = EVSYS_USER_USER(user_number); - #endif } void connect_event_user_to_channel(uint8_t user, uint8_t channel) { - #ifdef SAMD51 - EVSYS->USER[user].reg = channel + 1; - #endif - #ifdef SAMD21 EVSYS->USER.reg = EVSYS_USER_USER(user) | EVSYS_USER_CHANNEL(channel + 1); - #endif } void init_async_event_channel(uint8_t channel, uint8_t generator) { - #ifdef SAMD51 - EVSYS->Channel[channel].CHANNEL.reg = EVSYS_CHANNEL_EVGEN(generator) | EVSYS_CHANNEL_PATH_ASYNCHRONOUS; - #endif - #ifdef SAMD21 - EVSYS->CHANNEL.reg = EVSYS_CHANNEL_CHANNEL(channel) | EVSYS_CHANNEL_EVGEN(generator) | EVSYS_CHANNEL_PATH_ASYNCHRONOUS; - #endif + EVSYS->CHANNEL.reg = EVSYS_CHANNEL_CHANNEL(channel) | + EVSYS_CHANNEL_EVGEN(generator) | + EVSYS_CHANNEL_PATH_ASYNCHRONOUS; } void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generator) { connect_gclk_to_peripheral(gclk, EVSYS_GCLK_ID_0 + channel); - #ifdef SAMD51 - EVSYS->Channel[channel].CHANNEL.reg = EVSYS_CHANNEL_EVGEN(generator) | - EVSYS_CHANNEL_PATH_SYNCHRONOUS | - EVSYS_CHANNEL_EDGSEL_RISING_EDGE; - EVSYS->Channel[channel].CHINTFLAG.reg = EVSYS_CHINTFLAG_EVD | EVSYS_CHINTFLAG_OVR; - EVSYS->Channel[channel].CHINTENSET.reg = EVSYS_CHINTENSET_EVD | EVSYS_CHINTENSET_OVR; - #endif - #ifdef SAMD21 EVSYS->CHANNEL.reg = EVSYS_CHANNEL_CHANNEL(channel) | EVSYS_CHANNEL_EVGEN(generator) | EVSYS_CHANNEL_PATH_RESYNCHRONIZED | @@ -162,12 +83,10 @@ void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generat EVSYS->INTFLAG.reg = EVSYS_INTFLAG_EVD(value) | EVSYS_INTFLAG_OVR(value); EVSYS->INTENSET.reg = EVSYS_INTENSET_EVD(value) | EVSYS_INTENSET_OVR(value); } - #endif } bool event_interrupt_active(uint8_t channel) { bool active = false; - #ifdef SAMD21 if (channel > 7) { uint8_t value = 1 << (channel - 7); active = (EVSYS->INTFLAG.reg & EVSYS_INTFLAG_EVDp8(value)) != 0; @@ -183,21 +102,11 @@ bool event_interrupt_active(uint8_t channel) { EVSYS->INTFLAG.reg = EVSYS_INTFLAG_EVD(value) | EVSYS_INTFLAG_OVR(value); } } - #endif - #ifdef SAMD51 - active = EVSYS->Channel[channel].CHINTFLAG.bit.EVD; - // Only clear if we know its active, otherwise there is the possibility it becomes after we - // check but before we clear. - if (active) { - EVSYS->Channel[channel].CHINTFLAG.reg = EVSYS_CHINTFLAG_EVD | EVSYS_CHINTFLAG_OVR; - } - #endif return active; } bool event_interrupt_overflow(uint8_t channel) { bool overflow = false; - #ifdef SAMD21 if (channel > 7) { uint8_t value = 1 << (channel - 7); overflow = (EVSYS->INTFLAG.reg & EVSYS_INTFLAG_OVRp8(value)) != 0; @@ -205,9 +114,5 @@ bool event_interrupt_overflow(uint8_t channel) { uint8_t value = 1 << channel; overflow = (EVSYS->INTFLAG.reg & EVSYS_INTFLAG_OVR(value)) != 0; } - #endif - #ifdef SAMD51 - overflow = EVSYS->Channel[channel].CHINTFLAG.bit.OVR; - #endif return overflow; } diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/board.c b/ports/atmel-samd/peripherals/samd21/i2s.c similarity index 65% rename from ports/atmel-samd/boards/metro_m4_express_revb/board.c rename to ports/atmel-samd/peripherals/samd21/i2s.c index a98385d295..7df401601d 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/board.c +++ b/ports/atmel-samd/peripherals/samd21/i2s.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2018 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 @@ -24,23 +24,23 @@ * THE SOFTWARE. */ -#include "boards/board.h" -#include "mpconfigboard.h" -#include "hal/include/hal_gpio.h" +#include "peripherals/i2s.h" -void board_init(void) { - gpio_set_pin_function(MICROPY_HW_LED_TX, GPIO_PIN_FUNCTION_OFF); - gpio_set_pin_direction(MICROPY_HW_LED_TX, GPIO_DIRECTION_OUT); - gpio_set_pin_level(MICROPY_HW_LED_TX, true); +#include "peripherals/clocks.h" - gpio_set_pin_function(MICROPY_HW_LED_RX, GPIO_PIN_FUNCTION_OFF); - gpio_set_pin_direction(MICROPY_HW_LED_RX, GPIO_DIRECTION_OUT); - gpio_set_pin_level(MICROPY_HW_LED_RX, true); +#include "hpl/gclk/hpl_gclk_base.h" +#include "hpl/pm/hpl_pm_base.h" + +void turn_on_i2s(void) { + _pm_enable_bus_clock(PM_BUS_APBC, I2S); } -bool board_requests_safe_mode(void) { - return false; -} - -void reset_board(void) { +void i2s_set_serializer_enable(uint8_t serializer, bool enable) { + while ((I2S->SYNCBUSY.vec.SEREN & (1 << serializer)) != 0) {} + if (enable) { + I2S->CTRLA.vec.SEREN = 1 << serializer; + } else { + I2S->CTRLA.vec.SEREN &= ~(1 << serializer); + } + while ((I2S->SYNCBUSY.vec.SEREN & (1 << serializer)) != 0) {} } diff --git a/ports/atmel-samd/samd21_pins.c b/ports/atmel-samd/peripherals/samd21/pins.c similarity index 99% rename from ports/atmel-samd/samd21_pins.c rename to ports/atmel-samd/peripherals/samd21/pins.c index 319870b9a3..bbb023a54f 100644 --- a/ports/atmel-samd/samd21_pins.c +++ b/ports/atmel-samd/peripherals/samd21/pins.c @@ -26,8 +26,6 @@ #include "shared-bindings/microcontroller/Pin.h" -#include "samd21_pins.h" - #define SERCOM(sercom_index, p_pad) \ { \ .index = sercom_index, \ diff --git a/ports/atmel-samd/samd21_pins.h b/ports/atmel-samd/peripherals/samd21/pins.h similarity index 93% rename from ports/atmel-samd/samd21_pins.h rename to ports/atmel-samd/peripherals/samd21/pins.h index 522563ef11..0b99ce9b36 100644 --- a/ports/atmel-samd/samd21_pins.h +++ b/ports/atmel-samd/peripherals/samd21/pins.h @@ -24,13 +24,14 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H +// DO NOT include this file directly. Use shared-bindings/microcontroller/Pin.h instead to ensure +// that all necessary includes are already included. + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SAMD21_PINS_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SAMD21_PINS_H #include "include/sam.h" -#include "common-hal/microcontroller/Pin.h" - void reset_pin(uint8_t pin); #define MUX_C 2 @@ -201,4 +202,4 @@ extern const mcu_pin_obj_t pin_PB02; extern const mcu_pin_obj_t pin_PB03; #endif -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SAMD21_PINS_H diff --git a/ports/atmel-samd/samd21_peripherals.c b/ports/atmel-samd/peripherals/samd21/sercom.c similarity index 76% rename from ports/atmel-samd/samd21_peripherals.c rename to ports/atmel-samd/peripherals/samd21/sercom.c index 5bbac29655..a27e49e0e5 100644 --- a/ports/atmel-samd/samd21_peripherals.c +++ b/ports/atmel-samd/peripherals/samd21/sercom.c @@ -24,11 +24,9 @@ * THE SOFTWARE. */ -#include "hal/include/hal_adc_sync.h" #include "hpl/gclk/hpl_gclk_base.h" #include "hpl/pm/hpl_pm_base.h" - // The clock initializer values are rather random, so we need to put them in // tables for lookup. We can't compute them. @@ -93,21 +91,3 @@ uint8_t samd_peripherals_get_spi_dopo(uint8_t clock_pad, uint8_t mosi_pad) { bool samd_peripherals_valid_spi_clock_pad(uint8_t clock_pad) { return clock_pad == 1 || clock_pad == 3; } - -// Do initialization and calibration setup needed for any use of the ADC. -// The reference and resolution should be set by the caller. -void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance) { - // Turn the clocks on. - _pm_enable_bus_clock(PM_BUS_APBC, ADC); - _gclk_enable_channel(ADC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); - - adc_sync_init(adc, instance, (void *)NULL); - - // Load the factory calibration - hri_adc_write_CALIB_BIAS_CAL_bf(ADC, (*((uint32_t*) ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos); - // Bits 7:5 - uint16_t linearity = ((*((uint32_t*) ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5; - // Bits 4:0 - linearity |= (*((uint32_t*) ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; - hri_adc_write_CALIB_LINEARITY_CAL_bf(ADC, linearity); -} diff --git a/ports/atmel-samd/peripherals/samd21/timers.c b/ports/atmel-samd/peripherals/samd21/timers.c new file mode 100644 index 0000000000..c93a710867 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd21/timers.c @@ -0,0 +1,75 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 +#include + +#include "peripherals/timers.h" + +//#include "common-hal/pulseio/PulseOut.h" + +#include "hpl/gclk/hpl_gclk_base.h" + +const uint8_t tcc_cc_num[3] = {4, 2, 2}; +const uint8_t tc_gclk_ids[TC_INST_NUM] = {TC3_GCLK_ID, + TC4_GCLK_ID, + TC5_GCLK_ID, +#ifdef TC6_GCLK_ID + TC6_GCLK_ID, +#endif +#ifdef TC7_GCLK_ID + TC7_GCLK_ID, +#endif + }; +const uint8_t tcc_gclk_ids[3] = {TCC0_GCLK_ID, TCC1_GCLK_ID, TCC2_GCLK_ID}; + +void turn_on_clocks(bool is_tc, uint8_t index, uint32_t gclk_index) { + uint8_t gclk_id; + if (is_tc) { + gclk_id = tc_gclk_ids[index]; + } else { + gclk_id = tcc_gclk_ids[index]; + } + // Determine the clock slot on the APBC bus. TCC0 is the first and 8 slots in. + uint8_t clock_slot = 8 + index; + // We index TCs starting at zero but in memory they begin at three so we have to add three. + if (is_tc) { + clock_slot += 3; + } + PM->APBCMASK.reg |= 1 << clock_slot; + _gclk_enable_channel(gclk_id, gclk_index); +} + +void tc_set_enable(Tc* tc, bool enable) { + tc->COUNT16.CTRLA.bit.ENABLE = enable; + while (tc->COUNT16.STATUS.bit.SYNCBUSY != 0) { + /* Wait for sync */ + } +} + +void tc_wait_for_sync(Tc* tc) { + while (tc->COUNT16.STATUS.bit.SYNCBUSY != 0) {} +} diff --git a/ports/atmel-samd/peripherals/samd51/adc.c b/ports/atmel-samd/peripherals/samd51/adc.c new file mode 100644 index 0000000000..af6333a949 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd51/adc.c @@ -0,0 +1,61 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Dan Halbert 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 "hal/include/hal_adc_sync.h" +#include "hpl/gclk/hpl_gclk_base.h" +#include "hri/hri_mclk_d51.h" + +// Do initialization and calibration setup needed for any use of the ADC. +// The reference and resolution should be set by the caller. +void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance) { + // Turn the clocks on. + if (instance == ADC0) { + hri_mclk_set_APBDMASK_ADC0_bit(MCLK); + hri_gclk_write_PCHCTRL_reg(GCLK, ADC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); + } else if (instance == ADC1) { + hri_mclk_set_APBDMASK_ADC1_bit(MCLK); + hri_gclk_write_PCHCTRL_reg(GCLK, ADC1_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); + } + + adc_sync_init(adc, instance, (void *)NULL); + + // SAMD51 has a CALIB register but doesn't have documented fuses for them. + uint8_t biasrefbuf; + uint8_t biasr2r; + uint8_t biascomp; + if (instance == ADC0) { + biasrefbuf = ((*(uint32_t*) ADC0_FUSES_BIASREFBUF_ADDR) & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; + biasr2r = ((*(uint32_t*) ADC0_FUSES_BIASR2R_ADDR) & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; + biascomp = ((*(uint32_t*) ADC0_FUSES_BIASCOMP_ADDR) & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; + } else { + biasrefbuf = ((*(uint32_t*) ADC1_FUSES_BIASREFBUF_ADDR) & ADC1_FUSES_BIASREFBUF_Msk) >> ADC1_FUSES_BIASREFBUF_Pos; + biasr2r = ((*(uint32_t*) ADC1_FUSES_BIASR2R_ADDR) & ADC1_FUSES_BIASR2R_Msk) >> ADC1_FUSES_BIASR2R_Pos; + biascomp = ((*(uint32_t*) ADC1_FUSES_BIASCOMP_ADDR) & ADC1_FUSES_BIASCOMP_Msk) >> ADC1_FUSES_BIASCOMP_Pos; + } + hri_adc_write_CALIB_BIASREFBUF_bf(instance, biasrefbuf); + hri_adc_write_CALIB_BIASR2R_bf(instance, biasr2r); + hri_adc_write_CALIB_BIASCOMP_bf(instance, biascomp); +} diff --git a/ports/atmel-samd/peripherals/samd51/cache.c b/ports/atmel-samd/peripherals/samd51/cache.c new file mode 100644 index 0000000000..f94dd830c0 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd51/cache.c @@ -0,0 +1,39 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Dan Halbert 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 "sam.h" + +// Turn off cache and invalidate all data in it. +void samd_peripherals_disable_and_clear_cache(void) { + CMCC->CTRL.bit.CEN = 0; + while (CMCC->SR.bit.CSTS) {} + CMCC->MAINT0.bit.INVALL = 1; +} + +// Enable cache +void samd_peripherals_enable_cache(void) { + CMCC->CTRL.bit.CEN = 1; +} diff --git a/ports/atmel-samd/samd51_clocks.c b/ports/atmel-samd/peripherals/samd51/clocks.c similarity index 99% rename from ports/atmel-samd/samd51_clocks.c rename to ports/atmel-samd/peripherals/samd51/clocks.c index 6f8b1f0102..29f9cb99cb 100644 --- a/ports/atmel-samd/samd51_clocks.c +++ b/ports/atmel-samd/peripherals/samd51/clocks.c @@ -24,7 +24,7 @@ * THE SOFTWARE. */ -#include "clocks.h" +#include "peripherals/clocks.h" #include "hpl_gclk_config.h" diff --git a/ports/atmel-samd/peripherals/samd51/dma.c b/ports/atmel-samd/peripherals/samd51/dma.c new file mode 100644 index 0000000000..ccb35aaf65 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd51/dma.c @@ -0,0 +1,94 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 "peripherals/dma.h" + +#include + +#include "py/gc.h" +#include "py/mpstate.h" + +#include "hal/utils/include/utils.h" + +#include "shared-bindings/microcontroller/__init__.h" + +uint8_t sercom_index(Sercom* sercom) { + const Sercom* sercoms[SERCOM_INST_NUM] = SERCOM_INSTS; + for (uint8_t i = 0; i < SERCOM_INST_NUM; i++) { + if (sercoms[i] == sercom) { + return i; + } + } + return 0; +} + +void dma_configure(uint8_t channel_number, uint8_t trigsrc, bool output_event) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + channel->CHCTRLA.reg &= ~DMAC_CHCTRLA_ENABLE; + channel->CHCTRLA.reg = DMAC_CHCTRLA_SWRST; + if (output_event) { + channel->CHEVCTRL.reg = DMAC_CHEVCTRL_EVOE; + } + channel->CHCTRLA.reg = DMAC_CHCTRLA_TRIGSRC(trigsrc) | + DMAC_CHCTRLA_TRIGACT_BURST | + DMAC_CHCTRLA_BURSTLEN_SINGLE; +} + +void dma_enable_channel(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + channel->CHCTRLA.bit.ENABLE = true; + // Clear any previous interrupts. + channel->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; +} + +void dma_disable_channel(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + channel->CHCTRLA.bit.ENABLE = false; +} + +void dma_suspend_channel(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + channel->CHCTRLB.reg = DMAC_CHCTRLB_CMD_SUSPEND; +} + +void dma_resume_channel(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + channel->CHCTRLB.reg = DMAC_CHCTRLB_CMD_RESUME; +} + +bool dma_channel_enabled(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + return channel->CHCTRLA.bit.ENABLE; +} + +uint8_t dma_transfer_status(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + return channel->CHINTFLAG.reg; +} + +bool dma_channel_free(uint8_t channel_number) { + DmacChannel* channel = &DMAC->Channel[channel_number]; + return channel->CHSTATUS.reg == 0; +} diff --git a/ports/atmel-samd/peripherals/samd51/events.c b/ports/atmel-samd/peripherals/samd51/events.c new file mode 100644 index 0000000000..5ea2c87e69 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd51/events.c @@ -0,0 +1,88 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "peripherals/events.h" + +#include "peripherals/clocks.h" + +#include "py/runtime.h" + +#include "hri/hri_mclk_d51.h" + +void turn_on_event_system(void) { + hri_mclk_set_APBBMASK_EVSYS_bit(MCLK); +} + +void reset_event_system(void) { + EVSYS->CTRLA.bit.SWRST = true; + hri_mclk_clear_APBBMASK_EVSYS_bit(MCLK); +} + +bool event_channel_free(uint8_t channel) { + uint8_t generator; + generator = EVSYS->Channel[channel].CHANNEL.bit.EVGEN; + return generator == 0; +} + +void disable_event_channel(uint8_t channel_number) { + EVSYS->Channel[channel_number].CHANNEL.reg = 0; +} + +void disable_event_user(uint8_t user_number) { + EVSYS->USER[user_number].reg = 0; +} + +void connect_event_user_to_channel(uint8_t user, uint8_t channel) { + EVSYS->USER[user].reg = channel + 1; +} + +void init_async_event_channel(uint8_t channel, uint8_t generator) { + EVSYS->Channel[channel].CHANNEL.reg = EVSYS_CHANNEL_EVGEN(generator) | EVSYS_CHANNEL_PATH_ASYNCHRONOUS; +} + +void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generator) { + connect_gclk_to_peripheral(gclk, EVSYS_GCLK_ID_0 + channel); + EVSYS->Channel[channel].CHANNEL.reg = EVSYS_CHANNEL_EVGEN(generator) | + EVSYS_CHANNEL_PATH_SYNCHRONOUS | + EVSYS_CHANNEL_EDGSEL_RISING_EDGE; + EVSYS->Channel[channel].CHINTFLAG.reg = EVSYS_CHINTFLAG_EVD | EVSYS_CHINTFLAG_OVR; + EVSYS->Channel[channel].CHINTENSET.reg = EVSYS_CHINTENSET_EVD | EVSYS_CHINTENSET_OVR; +} + +bool event_interrupt_active(uint8_t channel) { + bool active = false; + active = EVSYS->Channel[channel].CHINTFLAG.bit.EVD; + // Only clear if we know its active, otherwise there is the possibility it becomes after we + // check but before we clear. + if (active) { + EVSYS->Channel[channel].CHINTFLAG.reg = EVSYS_CHINTFLAG_EVD | EVSYS_CHINTFLAG_OVR; + } + return active; +} + +bool event_interrupt_overflow(uint8_t channel) { + return EVSYS->Channel[channel].CHINTFLAG.bit.OVR; +} diff --git a/ports/atmel-samd/i2s.c b/ports/atmel-samd/peripherals/samd51/i2s.c similarity index 68% rename from ports/atmel-samd/i2s.c rename to ports/atmel-samd/peripherals/samd51/i2s.c index 663ecdd1dc..97ce13f7d9 100644 --- a/ports/atmel-samd/i2s.c +++ b/ports/atmel-samd/peripherals/samd51/i2s.c @@ -24,53 +24,22 @@ * THE SOFTWARE. */ -#include "i2s.h" +#include "peripherals/i2s.h" -#include "clocks.h" +#include "peripherals/clocks.h" #include "hpl/gclk/hpl_gclk_base.h" -#ifdef SAMD21 -#include "hpl/pm/hpl_pm_base.h" -#endif void turn_on_i2s(void) { // Make sure the I2S peripheral is running so we can see if the resources we need are free. - #ifdef SAMD51 hri_mclk_set_APBDMASK_I2S_bit(MCLK); // Connect the clock units to the 2mhz clock by default. They can't reset without it. connect_gclk_to_peripheral(5, I2S_GCLK_ID_0); connect_gclk_to_peripheral(5, I2S_GCLK_ID_1); - #endif - - #ifdef SAMD21 - _pm_enable_bus_clock(PM_BUS_APBC, I2S); - #endif -} - -void i2s_set_enable(bool enable) { - while (I2S->SYNCBUSY.bit.ENABLE == 1) {} - I2S->CTRLA.bit.ENABLE = enable; - while (I2S->SYNCBUSY.bit.ENABLE == 1) {} -} - -void i2s_set_clock_unit_enable(uint8_t clock_unit, bool enable) { - while ((I2S->SYNCBUSY.vec.CKEN & (1 << clock_unit)) != 0) {} - I2S->CTRLA.vec.CKEN = 1 << clock_unit; - while ((I2S->SYNCBUSY.vec.CKEN & (1 << clock_unit)) != 0) {} } void i2s_set_serializer_enable(uint8_t serializer, bool enable) { - #ifdef SAMD21 - while ((I2S->SYNCBUSY.vec.SEREN & (1 << serializer)) != 0) {} - if (enable) { - I2S->CTRLA.vec.SEREN = 1 << serializer; - } else { - I2S->CTRLA.vec.SEREN &= ~(1 << serializer); - } - while ((I2S->SYNCBUSY.vec.SEREN & (1 << serializer)) != 0) {} - #endif - #ifdef SAMD51 if (serializer == 0) { while (I2S->SYNCBUSY.bit.TXEN == 1) {} I2S->CTRLA.bit.TXEN = enable; @@ -80,5 +49,4 @@ void i2s_set_serializer_enable(uint8_t serializer, bool enable) { I2S->CTRLA.bit.RXEN = enable; while (I2S->SYNCBUSY.bit.RXEN == 1) {} } - #endif } diff --git a/ports/atmel-samd/samd51_pins.c b/ports/atmel-samd/peripherals/samd51/pins.c similarity index 99% rename from ports/atmel-samd/samd51_pins.c rename to ports/atmel-samd/peripherals/samd51/pins.c index 6d597b6e1a..1c52d35a24 100644 --- a/ports/atmel-samd/samd51_pins.c +++ b/ports/atmel-samd/peripherals/samd51/pins.c @@ -26,8 +26,6 @@ #include "shared-bindings/microcontroller/Pin.h" -#include "samd51_pins.h" - #define SERCOM(sercom_index, p_pad) \ { \ .index = sercom_index, \ diff --git a/ports/atmel-samd/samd51_pins.h b/ports/atmel-samd/peripherals/samd51/pins.h similarity index 98% rename from ports/atmel-samd/samd51_pins.h rename to ports/atmel-samd/peripherals/samd51/pins.h index c9ffacc24b..b9a05a3459 100644 --- a/ports/atmel-samd/samd51_pins.h +++ b/ports/atmel-samd/peripherals/samd51/pins.h @@ -24,13 +24,14 @@ * THE SOFTWARE. */ +// DO NOT include this file directly. Use shared-bindings/microcontroller/Pin.h instead to ensure +// that all necessary includes are already included. + #ifndef MICROPY_INCLUDED_ATMEL_SAMD_SAMD51_PINS_H #define MICROPY_INCLUDED_ATMEL_SAMD_SAMD51_PINS_H #include "include/sam.h" -#include "common-hal/microcontroller/Pin.h" - void reset_pin(uint8_t pin); #define MUX_C 2 diff --git a/ports/atmel-samd/samd51_peripherals.c b/ports/atmel-samd/peripherals/samd51/sercom.c similarity index 67% rename from ports/atmel-samd/samd51_peripherals.c rename to ports/atmel-samd/peripherals/samd51/sercom.c index 5772b8e0ab..fc44214220 100644 --- a/ports/atmel-samd/samd51_peripherals.c +++ b/ports/atmel-samd/peripherals/samd51/sercom.c @@ -28,6 +28,8 @@ #include "hpl/gclk/hpl_gclk_base.h" #include "hri/hri_mclk_d51.h" +// FIXME(tannewt): Should this be called sercom.c? + // The clock initializer values are rather random, so we need to put them in // tables for lookup. We can't compute them. @@ -131,47 +133,3 @@ uint8_t samd_peripherals_get_spi_dopo(uint8_t clock_pad, uint8_t mosi_pad) { bool samd_peripherals_valid_spi_clock_pad(uint8_t clock_pad) { return clock_pad == 1; } - -// Do initialization and calibration setup needed for any use of the ADC. -// The reference and resolution should be set by the caller. -void samd_peripherals_adc_setup(struct adc_sync_descriptor *adc, Adc *instance) { - // Turn the clocks on. - if (instance == ADC0) { - hri_mclk_set_APBDMASK_ADC0_bit(MCLK); - hri_gclk_write_PCHCTRL_reg(GCLK, ADC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); - } else if (instance == ADC1) { - hri_mclk_set_APBDMASK_ADC1_bit(MCLK); - hri_gclk_write_PCHCTRL_reg(GCLK, ADC1_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); - } - - adc_sync_init(adc, instance, (void *)NULL); - - // SAMD51 has a CALIB register but doesn't have documented fuses for them. - uint8_t biasrefbuf; - uint8_t biasr2r; - uint8_t biascomp; - if (instance == ADC0) { - biasrefbuf = ((*(uint32_t*) ADC0_FUSES_BIASREFBUF_ADDR) & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; - biasr2r = ((*(uint32_t*) ADC0_FUSES_BIASR2R_ADDR) & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; - biascomp = ((*(uint32_t*) ADC0_FUSES_BIASCOMP_ADDR) & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; - } else { - biasrefbuf = ((*(uint32_t*) ADC1_FUSES_BIASREFBUF_ADDR) & ADC1_FUSES_BIASREFBUF_Msk) >> ADC1_FUSES_BIASREFBUF_Pos; - biasr2r = ((*(uint32_t*) ADC1_FUSES_BIASR2R_ADDR) & ADC1_FUSES_BIASR2R_Msk) >> ADC1_FUSES_BIASR2R_Pos; - biascomp = ((*(uint32_t*) ADC1_FUSES_BIASCOMP_ADDR) & ADC1_FUSES_BIASCOMP_Msk) >> ADC1_FUSES_BIASCOMP_Pos; - } - hri_adc_write_CALIB_BIASREFBUF_bf(instance, biasrefbuf); - hri_adc_write_CALIB_BIASR2R_bf(instance, biasr2r); - hri_adc_write_CALIB_BIASCOMP_bf(instance, biascomp); -} - -// Turn off cache and invalidate all data in it. -void samd_peripherals_disable_and_clear_cache(void) { - CMCC->CTRL.bit.CEN = 0; - while (CMCC->SR.bit.CSTS) {} - CMCC->MAINT0.bit.INVALL = 1; -} - -// Enable cache -void samd_peripherals_enable_cache(void) { - CMCC->CTRL.bit.CEN = 1; -} diff --git a/ports/atmel-samd/timers.c b/ports/atmel-samd/peripherals/samd51/timers.c similarity index 56% rename from ports/atmel-samd/timers.c rename to ports/atmel-samd/peripherals/samd51/timers.c index 33518b782f..305acce381 100644 --- a/ports/atmel-samd/timers.c +++ b/ports/atmel-samd/peripherals/samd51/timers.c @@ -27,36 +27,12 @@ #include #include -#include "timers.h" +#include "peripherals/timers.h" -#include "common-hal/pulseio/PulseOut.h" +//#include "common-hal/pulseio/PulseOut.h" -#ifdef SAMD21 -#include "hpl/gclk/hpl_gclk_base.h" -#endif - -#ifdef SAMD51 #include "hri/hri_gclk_d51.h" -#endif -const uint16_t prescaler[8] = {1, 2, 4, 8, 16, 64, 256, 1024}; - -// This bitmask keeps track of which channels of a TCC are currently claimed. -#ifdef SAMD21 -const uint8_t tcc_cc_num[3] = {4, 2, 2}; -const uint8_t tc_gclk_ids[TC_INST_NUM] = {TC3_GCLK_ID, - TC4_GCLK_ID, - TC5_GCLK_ID, -#ifdef TC6_GCLK_ID - TC6_GCLK_ID, -#endif -#ifdef TC7_GCLK_ID - TC7_GCLK_ID, -#endif - }; -const uint8_t tcc_gclk_ids[3] = {TCC0_GCLK_ID, TCC1_GCLK_ID, TCC2_GCLK_ID}; -#endif -#ifdef SAMD51 const uint8_t tcc_cc_num[5] = {6, 4, 3, 2, 2}; const uint8_t tc_gclk_ids[TC_INST_NUM] = {TC0_GCLK_ID, TC1_GCLK_ID, @@ -85,36 +61,6 @@ const uint8_t tcc_gclk_ids[TCC_INST_NUM] = {TCC0_GCLK_ID, TCC4_GCLK_ID #endif }; -#endif -Tc* const tc_insts[TC_INST_NUM] = TC_INSTS; -Tcc* const tcc_insts[TCC_INST_NUM] = TCC_INSTS; - -IRQn_Type const tc_irq[TC_INST_NUM] = { -#ifdef TC0 - TC0_IRQn, -#endif -#ifdef TC1 - TC1_IRQn, -#endif -#ifdef TC2 - TC2_IRQn, -#endif -#ifdef TC3 - TC3_IRQn, -#endif -#ifdef TC4 - TC4_IRQn, -#endif -#ifdef TC5 - TC5_IRQn, -#endif -#ifdef TC6 - TC6_IRQn, -#endif -#ifdef TC7 - TC7_IRQn, -#endif -}; void turn_on_clocks(bool is_tc, uint8_t index, uint32_t gclk_index) { uint8_t gclk_id; @@ -124,7 +70,6 @@ void turn_on_clocks(bool is_tc, uint8_t index, uint32_t gclk_index) { gclk_id = tcc_gclk_ids[index]; } // Turn on the clocks for the peripherals. - #ifdef SAMD51 if (is_tc) { switch (index) { case 0: @@ -180,122 +125,15 @@ void turn_on_clocks(bool is_tc, uint8_t index, uint32_t gclk_index) { hri_gclk_write_PCHCTRL_reg(GCLK, gclk_id, gclk_index | (1 << GCLK_PCHCTRL_CHEN_Pos)); - #endif - - #ifdef SAMD21 - // Determine the clock slot on the APBC bus. TCC0 is the first and 8 slots in. - uint8_t clock_slot = 8 + index; - // We index TCs starting at zero but in memory they begin at three so we have to add three. - if (is_tc) { - clock_slot += 3; - } - PM->APBCMASK.reg |= 1 << clock_slot; - _gclk_enable_channel(gclk_id, gclk_index); - #endif } void tc_set_enable(Tc* tc, bool enable) { tc->COUNT16.CTRLA.bit.ENABLE = enable; - #ifdef SAMD21 - while (tc->COUNT16.STATUS.bit.SYNCBUSY != 0) { - /* Wait for sync */ - } - #endif - #ifdef SAMD51 while (tc->COUNT16.SYNCBUSY.bit.ENABLE != 0) { /* Wait for sync */ } - #endif -} - -void tc_enable_interrupts(uint8_t tc_index) { - NVIC_DisableIRQ(tc_irq[tc_index]); - NVIC_ClearPendingIRQ(tc_irq[tc_index]); - NVIC_EnableIRQ(tc_irq[tc_index]); -} - -void tc_disable_interrupts(uint8_t tc_index) { - NVIC_DisableIRQ(tc_irq[tc_index]); - NVIC_ClearPendingIRQ(tc_irq[tc_index]); -} - -void tcc_set_enable(Tcc* tcc, bool enable) { - tcc->CTRLA.bit.ENABLE = enable; - while (tcc->SYNCBUSY.bit.ENABLE != 0) { - /* Wait for sync */ - } } void tc_wait_for_sync(Tc* tc) { - #ifdef SAMD21 - while (tc->COUNT16.STATUS.bit.SYNCBUSY != 0) {} - #endif - #ifdef SAMD51 while (tc->COUNT16.SYNCBUSY.reg != 0) {} - #endif } - -void tc_reset(Tc* tc) { - tc->COUNT16.CTRLA.bit.SWRST = 1; - while (tc->COUNT16.CTRLA.bit.SWRST == 1) { - } -} - -void shared_timer_handler(bool is_tc, uint8_t index) { - // Add calls to interrupt handlers for specific functionality here. - if (is_tc) { - pulseout_interrupt_handler(index); - } -} - -#ifdef SAMD51 -#define TC_OFFSET 0 -#endif -#ifdef SAMD21 -#define TC_OFFSET 3 -#endif - -void TCC0_Handler(void) { - shared_timer_handler(false, 0); -} -void TCC1_Handler(void) { - shared_timer_handler(false, 1); -} -void TCC2_Handler(void) { - shared_timer_handler(false, 2); -} -// TC0 - TC2 only exist on the SAMD51 -#ifdef TC0 -void TC0_Handler(void) { - shared_timer_handler(true, 0); -} -#endif -#ifdef TC1 -void TC1_Handler(void) { - shared_timer_handler(true, 1); -} -#endif -#ifdef TC2 -void TC2_Handler(void) { - shared_timer_handler(true, 2); -} -#endif -void TC3_Handler(void) { - shared_timer_handler(true, 3 - TC_OFFSET); -} -void TC4_Handler(void) { - shared_timer_handler(true, 4 - TC_OFFSET); -} -void TC5_Handler(void) { - shared_timer_handler(true, 5 - TC_OFFSET); -} -#ifdef TC6 -void TC6_Handler(void) { - shared_timer_handler(true, 6 - TC_OFFSET); -} -#endif -#ifdef TC7 -void TC7_Handler(void) { - shared_timer_handler(true, 7 - TC_OFFSET); -} -#endif diff --git a/ports/atmel-samd/peripherals.c b/ports/atmel-samd/peripherals/sercom.c similarity index 98% rename from ports/atmel-samd/peripherals.c rename to ports/atmel-samd/peripherals/sercom.c index c75a1f43b5..04c1fd008d 100644 --- a/ports/atmel-samd/peripherals.c +++ b/ports/atmel-samd/peripherals/sercom.c @@ -24,7 +24,7 @@ * THE SOFTWARE. */ -#include "peripherals.h" +#include "peripherals/sercom.h" #include "hpl_sercom_config.h" diff --git a/ports/atmel-samd/peripherals.h b/ports/atmel-samd/peripherals/sercom.h similarity index 79% rename from ports/atmel-samd/peripherals.h rename to ports/atmel-samd/peripherals/sercom.h index 8a07c00457..66ad1a403f 100644 --- a/ports/atmel-samd/peripherals.h +++ b/ports/atmel-samd/peripherals/sercom.h @@ -24,24 +24,19 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_H -#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_H +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SPI_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SPI_H #include #include "mpconfigport.h" -// Routines common across chip families. +void samd_peripherals_sercom_clock_init(Sercom* sercom, uint8_t sercom_index); +uint8_t samd_peripherals_get_spi_dopo(uint8_t clock_pad, uint8_t mosi_pad); uint8_t samd_peripherals_spi_baudrate_to_baud_reg_value(const uint32_t baudrate); uint32_t samd_peripherals_spi_baud_reg_value_to_baudrate(const uint8_t baud_reg_value); +bool samd_peripherals_valid_spi_clock_pad(uint8_t clock_pad); Sercom* sercom_insts[SERCOM_INST_NUM]; -#ifdef SAMD21 -#include "samd21_peripherals.h" -#endif -#ifdef SAMD51 -#include "samd51_peripherals.h" -#endif - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_H +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_SPI_H diff --git a/ports/atmel-samd/peripherals/timers.c b/ports/atmel-samd/peripherals/timers.c new file mode 100644 index 0000000000..cdcb92ac82 --- /dev/null +++ b/ports/atmel-samd/peripherals/timers.c @@ -0,0 +1,147 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 +#include + +#include "timers.h" + +#include "common-hal/pulseio/PulseOut.h" + +const uint16_t prescaler[8] = {1, 2, 4, 8, 16, 64, 256, 1024}; + +Tc* const tc_insts[TC_INST_NUM] = TC_INSTS; +Tcc* const tcc_insts[TCC_INST_NUM] = TCC_INSTS; + +IRQn_Type const tc_irq[TC_INST_NUM] = { +#ifdef TC0 + TC0_IRQn, +#endif +#ifdef TC1 + TC1_IRQn, +#endif +#ifdef TC2 + TC2_IRQn, +#endif +#ifdef TC3 + TC3_IRQn, +#endif +#ifdef TC4 + TC4_IRQn, +#endif +#ifdef TC5 + TC5_IRQn, +#endif +#ifdef TC6 + TC6_IRQn, +#endif +#ifdef TC7 + TC7_IRQn, +#endif +}; + +void tc_enable_interrupts(uint8_t tc_index) { + NVIC_DisableIRQ(tc_irq[tc_index]); + NVIC_ClearPendingIRQ(tc_irq[tc_index]); + NVIC_EnableIRQ(tc_irq[tc_index]); +} + +void tc_disable_interrupts(uint8_t tc_index) { + NVIC_DisableIRQ(tc_irq[tc_index]); + NVIC_ClearPendingIRQ(tc_irq[tc_index]); +} + +void tcc_set_enable(Tcc* tcc, bool enable) { + tcc->CTRLA.bit.ENABLE = enable; + while (tcc->SYNCBUSY.bit.ENABLE != 0) { + /* Wait for sync */ + } +} + +void tc_reset(Tc* tc) { + tc->COUNT16.CTRLA.bit.SWRST = 1; + while (tc->COUNT16.CTRLA.bit.SWRST == 1) { + } +} + +void shared_timer_handler(bool is_tc, uint8_t index) { + // Add calls to interrupt handlers for specific functionality here. + if (is_tc) { + pulseout_interrupt_handler(index); + } +} + +#ifdef SAMD51 +#define TC_OFFSET 0 +#endif +#ifdef SAMD21 +#define TC_OFFSET 3 +#endif + +void TCC0_Handler(void) { + shared_timer_handler(false, 0); +} +void TCC1_Handler(void) { + shared_timer_handler(false, 1); +} +void TCC2_Handler(void) { + shared_timer_handler(false, 2); +} +// TC0 - TC2 only exist on the SAMD51 +#ifdef TC0 +void TC0_Handler(void) { + shared_timer_handler(true, 0); +} +#endif +#ifdef TC1 +void TC1_Handler(void) { + shared_timer_handler(true, 1); +} +#endif +#ifdef TC2 +void TC2_Handler(void) { + shared_timer_handler(true, 2); +} +#endif +void TC3_Handler(void) { + shared_timer_handler(true, 3 - TC_OFFSET); +} +void TC4_Handler(void) { + shared_timer_handler(true, 4 - TC_OFFSET); +} +void TC5_Handler(void) { + shared_timer_handler(true, 5 - TC_OFFSET); +} +#ifdef TC6 +void TC6_Handler(void) { + shared_timer_handler(true, 6 - TC_OFFSET); +} +#endif +#ifdef TC7 +void TC7_Handler(void) { + shared_timer_handler(true, 7 - TC_OFFSET); +} +#endif diff --git a/ports/atmel-samd/timers.h b/ports/atmel-samd/peripherals/timers.h similarity index 100% rename from ports/atmel-samd/timers.h rename to ports/atmel-samd/peripherals/timers.h diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 7c682d1206..55e0a7ecfc 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -55,11 +55,11 @@ #include "common-hal/rtc/RTC.h" #include "common-hal/touchio/TouchIn.h" #include "common-hal/usb_hid/Device.h" +#include "peripherals/cache.h" +#include "peripherals/clocks.h" +#include "peripherals/events.h" +#include "peripherals/dma.h" #include "shared-bindings/rtc/__init__.h" -#include "clocks.h" -#include "events.h" -#include "peripherals.h" -#include "shared_dma.h" #include "tick.h" #ifdef CIRCUITPY_GAMEPAD_TICKS diff --git a/shared-bindings/board/__init__.h b/shared-bindings/board/__init__.h index 720239f280..2730e5f51b 100644 --- a/shared-bindings/board/__init__.h +++ b/shared-bindings/board/__init__.h @@ -29,6 +29,8 @@ #include "py/obj.h" +#include "shared-bindings/microcontroller/Pin.h" // for the pin definitions + extern const mp_obj_dict_t board_module_globals; #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BOARD___INIT___H diff --git a/shared-bindings/rotaryio/IncrementalEncoder.c b/shared-bindings/rotaryio/IncrementalEncoder.c new file mode 100644 index 0000000000..635826c60e --- /dev/null +++ b/shared-bindings/rotaryio/IncrementalEncoder.c @@ -0,0 +1,159 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 + +#include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/rotaryio/IncrementalEncoder.h" +#include "shared-bindings/util.h" + +//| .. currentmodule:: rotaryio +//| +//| :class:`IncrementalEncoder` -- Read the position of the incremental encoder +//| ============================================================================ +//| +//| IncrementalEncoder determines the relative rotational position based on two series of pulses. +//| +//| .. class:: IncrementalEncoder(pin_a, pin_b) +//| +//| Create an IncrementalEncoder object associated with the given pins. It tracks the positional +//| state of an incremental rotary encoder (also known as a quadrature encoder.) Position is +//| relative to the position when the object is contructed. +//| +//| :param ~microcontroller.Pin pin: First pin to read pulses from. +//| :param ~microcontroller.Pin pin: Second pin to read pulses from. +//| +//| For example:: +//| +//| import rotaryio +//| import time +//| from board import * +//| +//| enc = rotaryio.IncrementalEncoder(D1, D2) +//| last_position = None +//| while True; +//| position = enc.position +//| if last_position == None or position != last_position: +//| print(position) +//| last_position = position +//| +STATIC mp_obj_t rotaryio_incrementalencoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 2, 2, true); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_pin_a, ARG_pin_b }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin_a, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_pin_b, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + assert_pin(args[ARG_pin_a].u_obj, false); + const mcu_pin_obj_t* pin_a = MP_OBJ_TO_PTR(args[ARG_pin_a].u_obj); + assert_pin_free(pin_a); + + assert_pin(args[ARG_pin_b].u_obj, false); + const mcu_pin_obj_t* pin_b = MP_OBJ_TO_PTR(args[ARG_pin_b].u_obj); + assert_pin_free(pin_b); + + rotaryio_incrementalencoder_obj_t *self = m_new_obj(rotaryio_incrementalencoder_obj_t); + self->base.type = &rotaryio_incrementalencoder_type; + + common_hal_rotaryio_incrementalencoder_construct(self, pin_a, pin_b); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialises the IncrementalEncoder and releases any hardware resources for reuse. +//| +STATIC mp_obj_t rotaryio_incrementalencoder_deinit(mp_obj_t self_in) { + rotaryio_incrementalencoder_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_rotaryio_incrementalencoder_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rotaryio_incrementalencoder_deinit_obj, rotaryio_incrementalencoder_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| +STATIC mp_obj_t rotaryio_incrementalencoder_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_rotaryio_incrementalencoder_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rotaryio_incrementalencoder___exit___obj, 4, 4, rotaryio_incrementalencoder_obj___exit__); + + +//| .. attribute:: position +//| +//| The current position in terms of pulses. The number of pulses per rotation is defined by the +//| specific hardware. +//| +STATIC mp_obj_t rotaryio_incrementalencoder_obj_get_position(mp_obj_t self_in) { + rotaryio_incrementalencoder_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_rotaryio_incrementalencoder_deinited(self)); + + return mp_obj_new_int(common_hal_rotaryio_incrementalencoder_get_position(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(rotaryio_incrementalencoder_get_position_obj, rotaryio_incrementalencoder_obj_get_position); + +const mp_obj_property_t rotaryio_incrementalencoder_position_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&rotaryio_incrementalencoder_get_position_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t rotaryio_incrementalencoder_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rotaryio_incrementalencoder_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&rotaryio_incrementalencoder___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_position), MP_ROM_PTR(&rotaryio_incrementalencoder_position_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rotaryio_incrementalencoder_locals_dict, rotaryio_incrementalencoder_locals_dict_table); + +const mp_obj_type_t rotaryio_incrementalencoder_type = { + { &mp_type_type }, + .name = MP_QSTR_IncrementalEncoder, + .make_new = rotaryio_incrementalencoder_make_new, + .locals_dict = (mp_obj_dict_t*)&rotaryio_incrementalencoder_locals_dict, +}; diff --git a/shared-bindings/rotaryio/IncrementalEncoder.h b/shared-bindings/rotaryio/IncrementalEncoder.h new file mode 100644 index 0000000000..29e89e2089 --- /dev/null +++ b/shared-bindings/rotaryio/IncrementalEncoder.h @@ -0,0 +1,41 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_SHARED_BINDINGS_ROTARYIO_INCREMENTALENCODER_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ROTARYIO_INCREMENTALENCODER_H + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/rotaryio/IncrementalEncoder.h" + +extern const mp_obj_type_t rotaryio_incrementalencoder_type; + +extern void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b); +extern void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self); +extern bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self); +extern mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ROTARYIO_INCREMENTALENCODER_H diff --git a/shared-bindings/rotaryio/__init__.c b/shared-bindings/rotaryio/__init__.c new file mode 100644 index 0000000000..db1ff3d4c6 --- /dev/null +++ b/shared-bindings/rotaryio/__init__.c @@ -0,0 +1,75 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/rotaryio/__init__.h" +#include "shared-bindings/rotaryio/IncrementalEncoder.h" + +//| :mod:`rotaryio` --- Support for pulse based protocols +//| ===================================================== +//| +//| .. module:: rotaryio +//| :synopsis: Support for reading rotation sensors +//| :platform: SAMD +//| +//| The `rotaryio` module contains classes to read different rotation encoding schemes. See +//| `Wikipedia's Rotary Encoder page `_ for more +//| background. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| IncrementalEncoder +//| + +//| .. warning:: This module is not available in some SAMD21 (aka M0) builds. See the +//| :ref:`module-support-matrix` for more info. +//| + +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed if the program continues after use. To do so, either +//| call :py:meth:`!deinit` or use a context manager. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| + +STATIC const mp_rom_map_elem_t rotaryio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rotaryio) }, + { MP_ROM_QSTR(MP_QSTR_IncrementalEncoder), MP_ROM_PTR(&rotaryio_incrementalencoder_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(rotaryio_module_globals, rotaryio_module_globals_table); + +const mp_obj_module_t rotaryio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&rotaryio_module_globals, +}; diff --git a/shared-bindings/rotaryio/__init__.h b/shared-bindings/rotaryio/__init__.h new file mode 100644 index 0000000000..5d051d5a1a --- /dev/null +++ b/shared-bindings/rotaryio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft + * + * 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_SHARED_BINDINGS_ROTARYIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ROTARYIO___INIT___H + +#include "py/obj.h" + +// Nothing now. + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ROTARYIO___INIT___H diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 8f1b506fea..584d021b0e 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -27,7 +27,6 @@ #include "mphalport.h" #include "common-hal/microcontroller/Pin.h" #include "rgb_led_status.h" -#include "pins.h" #ifdef MICROPY_HW_NEOPIXEL uint8_t rgb_status_brightness = 63; From d0fb6e7a2fd6b60ecc0fc8a332a9de5ed32f88af Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 29 May 2018 18:21:19 -0700 Subject: [PATCH 085/103] atmel-samd: Add rotary encoder support. Fixes #283 --- ports/atmel-samd/Makefile | 2 + ports/atmel-samd/common-hal/busio/SPI.c | 1 - ports/atmel-samd/common-hal/pulseio/PulseIn.c | 238 ++---------------- ports/atmel-samd/common-hal/pulseio/PulseIn.h | 4 + .../common-hal/rotaryio/IncrementalEncoder.c | 201 +++++++-------- .../common-hal/rotaryio/IncrementalEncoder.h | 7 + .../atmel-samd/common-hal/rotaryio/__init__.c | 2 +- .../external_flash/external_flash.c | 1 - ports/atmel-samd/peripherals/clocks.c | 53 ++-- ports/atmel-samd/peripherals/clocks.h | 1 + ports/atmel-samd/peripherals/events.c | 3 - .../peripherals/external_interrupts.c | 105 ++++++++ .../peripherals/external_interrupts.h | 54 ++++ ports/atmel-samd/peripherals/samd21/clocks.c | 14 +- .../peripherals/samd21/external_interrupts.c | 86 +++++++ .../peripherals/samd51/external_interrupts.c | 132 ++++++++++ ports/atmel-samd/peripherals/samd51/sercom.c | 2 - ports/atmel-samd/peripherals/samd51/timers.c | 2 - ports/atmel-samd/supervisor/port.c | 4 +- shared-bindings/index.rst | 4 + shared-bindings/rotaryio/__init__.c | 4 +- 21 files changed, 538 insertions(+), 382 deletions(-) create mode 100644 ports/atmel-samd/peripherals/external_interrupts.c create mode 100644 ports/atmel-samd/peripherals/external_interrupts.h create mode 100644 ports/atmel-samd/peripherals/samd21/external_interrupts.c create mode 100644 ports/atmel-samd/peripherals/samd51/external_interrupts.c diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 162e6b3e61..1a406c1d01 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -249,6 +249,7 @@ SRC_C = \ peripherals/clocks.c \ peripherals/dma.c \ peripherals/events.c \ + peripherals/external_interrupts.c \ peripherals/sercom.c \ peripherals/timers.c \ peripherals/$(CHIP_FAMILY)/adc.c \ @@ -256,6 +257,7 @@ SRC_C = \ peripherals/$(CHIP_FAMILY)/clocks.c \ peripherals/$(CHIP_FAMILY)/dma.c \ peripherals/$(CHIP_FAMILY)/events.c \ + peripherals/$(CHIP_FAMILY)/external_interrupts.c \ peripherals/$(CHIP_FAMILY)/pins.c \ peripherals/$(CHIP_FAMILY)/sercom.c \ peripherals/$(CHIP_FAMILY)/timers.c \ diff --git a/ports/atmel-samd/common-hal/busio/SPI.c b/ports/atmel-samd/common-hal/busio/SPI.c index a2ee93c6db..a9bd559039 100644 --- a/ports/atmel-samd/common-hal/busio/SPI.c +++ b/ports/atmel-samd/common-hal/busio/SPI.c @@ -37,7 +37,6 @@ #include "supervisor/shared/rgb_led_status.h" #include "peripherals/dma.h" -//#include "peripherals/pins.h" #include "peripherals/sercom.h" void common_hal_busio_spi_construct(busio_spi_obj_t *self, diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index 266a9251e9..99a60a8297 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -34,90 +34,28 @@ #include "mpconfigport.h" #include "py/gc.h" #include "py/runtime.h" +#include "peripherals/external_interrupts.h" #include "peripherals/pins.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/pulseio/PulseIn.h" -#ifdef SAMD21 -#include "hpl/gclk/hpl_gclk_base.h" -#endif - #include "tick.h" -static pulseio_pulsein_obj_t *active_pulseins[EIC_EXTINT_NUM]; -static uint64_t last_ms[EIC_EXTINT_NUM]; -static uint16_t last_us[EIC_EXTINT_NUM]; - -bool eic_get_enable(void) { - #ifdef SAMD51 - return EIC->CTRLA.bit.ENABLE; - #endif - #ifdef SAMD21 - return EIC->CTRL.bit.ENABLE; - #endif -} - -void eic_set_enable(bool value) { - #ifdef SAMD51 - EIC->CTRLA.bit.ENABLE = value; - while (EIC->SYNCBUSY.bit.ENABLE != 0) {} - // This won't actually block long enough in Rev A of SAMD51 and will miss edges in the first - // three cycles of the peripheral clock. See the errata for details. It shouldn't impact us. - #endif - #ifdef SAMD21 - EIC->CTRL.bit.ENABLE = value; - while (EIC->STATUS.bit.SYNCBUSY != 0) {} - #endif -} - -void eic_reset(void) { - #ifdef SAMD51 - EIC->CTRLA.bit.SWRST = true; - while (EIC->SYNCBUSY.bit.SWRST != 0) {} - // This won't actually block long enough in Rev A of SAMD51 and will miss edges in the first - // three cycles of the peripheral clock. See the errata for details. It shouldn't impact us. - #endif - #ifdef SAMD21 - EIC->CTRL.bit.SWRST = true; - while (EIC->STATUS.bit.SYNCBUSY != 0) {} - #endif -} - -void pulsein_reset(void) { - for (int i = 0; i < EIC_EXTINT_NUM; i++) { - active_pulseins[i] = NULL; - last_ms[i] = 0; - last_us[i] = 0; - #ifdef SAMD51 - NVIC_DisableIRQ(EIC_0_IRQn + i); - NVIC_ClearPendingIRQ(EIC_0_IRQn + i); - #endif - } - eic_reset(); - #ifdef SAMD21 - NVIC_DisableIRQ(EIC_IRQn); - NVIC_ClearPendingIRQ(EIC_IRQn); - #endif -} - static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) { - uint8_t sense_setting = EIC_CONFIG_FILTEN0; + uint32_t sense_setting; if (!first_edge) { - sense_setting |= EIC_CONFIG_SENSE0_BOTH_Val; + sense_setting = EIC_CONFIG_SENSE0_BOTH_Val; + configure_eic_channel(self->channel, sense_setting); + return; } else if (self->idle_state) { - sense_setting |= EIC_CONFIG_SENSE0_FALL_Val; + sense_setting = EIC_CONFIG_SENSE0_FALL_Val; } else { - sense_setting |= EIC_CONFIG_SENSE0_RISE_Val; + sense_setting = EIC_CONFIG_SENSE0_RISE_Val; } - eic_set_enable(false); - uint8_t config_index = self->channel / 8; - uint8_t position = (self->channel % 8) * 4; - uint32_t masked_value = EIC->CONFIG[config_index].reg & ~(0xf << position); - EIC->CONFIG[config_index].reg = masked_value | (sense_setting << position); - eic_set_enable(true); + turn_on_eic_channel(self->channel, sense_setting, EIC_HANDLER_PULSEIN); } -static void pulsein_interrupt_handler(uint8_t channel) { +void pulsein_interrupt_handler(uint8_t channel) { // Grab the current time first. uint32_t current_us; uint64_t current_ms; @@ -125,16 +63,16 @@ static void pulsein_interrupt_handler(uint8_t channel) { // current_tick gives us the remaining us until the next tick but we want the number since the // last ms. current_us = 1000 - current_us; - pulseio_pulsein_obj_t* self = active_pulseins[channel]; + pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); if (self->first_edge) { self->first_edge = false; pulsein_set_config(self, false); } else { - uint32_t ms_diff = current_ms - last_ms[self->channel]; - uint16_t us_diff = current_us - last_us[self->channel]; + uint32_t ms_diff = current_ms - self->last_ms; + uint16_t us_diff = current_us - self->last_us; uint32_t total_diff = us_diff; - if (last_us[self->channel] > current_us) { - total_diff = 1000 + current_us - last_us[self->channel]; + if (self->last_us > current_us) { + total_diff = 1000 + current_us - self->last_us; if (ms_diff > 1) { total_diff += (ms_diff - 1) * 1000; } @@ -154,8 +92,8 @@ static void pulsein_interrupt_handler(uint8_t channel) { self->start++; } } - last_ms[self->channel] = current_ms; - last_us[self->channel] = current_us; + self->last_ms = current_ms; + self->last_us = current_us; } void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -163,17 +101,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, if (!pin->has_extint) { mp_raise_RuntimeError("No hardware support on pin"); } - uint32_t mask = 1 << pin->extint_channel; - if (active_pulseins[pin->extint_channel] != NULL || - (eic_get_enable() == 1 && -#ifdef SAMD51 - ((EIC->INTENSET.bit.EXTINT & mask) != 0 || - (EIC->EVCTRL.bit.EXTINTEO & mask) != 0))) { -#endif -#ifdef SAMD21 - ((EIC->INTENSET.vec.EXTINT & mask) != 0 || - (EIC->EVCTRL.vec.EXTINTEO & mask) != 0))) { -#endif + if (eic_get_enable() && !eic_channel_free(pin->extint_channel)) { mp_raise_RuntimeError("EXTINT channel already in use"); } @@ -188,42 +116,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, self->start = 0; self->len = 0; self->first_edge = true; + self->last_us = 0; + self->last_ms = 0; - active_pulseins[pin->extint_channel] = self; + set_eic_channel_data(pin->extint_channel, (void*) self); // Check to see if the EIC is enabled and start it up if its not.' - // SAMD51 EIC can only be clocked up to 100mhz so we use the 48mhz clock. if (eic_get_enable() == 0) { - #ifdef SAMD51 - MCLK->APBAMASK.bit.EIC_ = true; - hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, - GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); - #endif - - #ifdef SAMD21 - PM->APBAMASK.bit.EIC_ = true; - _gclk_enable_channel(EIC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); - #endif - - - #ifdef SAMD21 - NVIC_DisableIRQ(EIC_IRQn); - NVIC_ClearPendingIRQ(EIC_IRQn); - NVIC_EnableIRQ(EIC_IRQn); - #endif + turn_on_external_interrupt_controller(); } gpio_set_pin_function(pin->pin, GPIO_PIN_FUNCTION_A); - #ifdef SAMD51 - NVIC_DisableIRQ(EIC_0_IRQn + self->channel); - NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); - NVIC_EnableIRQ(EIC_0_IRQn + self->channel); - #endif + turn_on_cpu_interrupt(self->channel); // Set config will enable the EIC. pulsein_set_config(self, true); - EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { @@ -234,39 +142,9 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { if (common_hal_pulseio_pulsein_deinited(self)) { return; } - uint32_t mask = 1 << self->channel; - EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; - #ifdef SAMD51 - NVIC_DisableIRQ(EIC_0_IRQn + self->channel); - NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); - #endif - active_pulseins[self->channel] = NULL; + turn_off_eic_channel(self->channel); reset_pin(self->pin); self->pin = NO_PIN; - - bool all_null = true; - for (uint8_t i = 0; all_null && i < 16; i++) { - all_null = all_null && active_pulseins[i] == NULL; - } - #ifdef SAMD21 - if (all_null && EIC->INTENSET.reg == 0) { - NVIC_DisableIRQ(EIC_IRQn); - NVIC_ClearPendingIRQ(EIC_IRQn); - } - #endif - // Test if all channels are null and deinit everything if they are. - if (all_null && EIC->EVCTRL.reg == 0 && EIC->INTENSET.reg == 0) { - eic_set_enable(false); - #ifdef SAMD51 - MCLK->APBAMASK.bit.EIC_ = false; - hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, 0); - #endif - - #ifdef SAMD21 - PM->APBAMASK.bit.EIC_ = false; - hri_gclk_write_CLKCTRL_reg(GCLK, GCLK_CLKCTRL_ID(EIC_GCLK_ID)); - #endif - } } void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { @@ -289,9 +167,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, } // Reconfigure the pin and make sure its set to detect the first edge. - last_ms[self->channel] = 0; - last_us[self->channel] = 0; self->first_edge = true; + self->last_ms = 0; + self->last_us = 0; gpio_set_pin_function(self->pin, GPIO_PIN_FUNCTION_A); uint32_t mask = 1 << self->channel; // Clear previous interrupt state and re-enable it. @@ -343,69 +221,3 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, common_hal_mcu_enable_interrupts(); return value; } - -void external_interrupt_handler(uint8_t channel) { - pulsein_interrupt_handler(channel); - EIC->INTFLAG.reg = (1 << channel) << EIC_INTFLAG_EXTINT_Pos; -} - -#ifdef SAMD21 -void EIC_Handler(void) { - for (uint8_t i = 0; i < 16; i++) { - if ((EIC->INTFLAG.vec.EXTINT & (1 << i)) != 0) { - external_interrupt_handler(i); - } - } -} -#endif - -#ifdef SAMD51 -void EIC_0_Handler(void) { - external_interrupt_handler(0); -} -void EIC_1_Handler(void) { - external_interrupt_handler(1); -} -void EIC_2_Handler(void) { - external_interrupt_handler(2); -} -void EIC_3_Handler(void) { - external_interrupt_handler(3); -} -void EIC_4_Handler(void) { - external_interrupt_handler(4); -} -void EIC_5_Handler(void) { - external_interrupt_handler(5); -} -void EIC_6_Handler(void) { - external_interrupt_handler(6); -} -void EIC_7_Handler(void) { - external_interrupt_handler(7); -} -void EIC_8_Handler(void) { - external_interrupt_handler(8); -} -void EIC_9_Handler(void) { - external_interrupt_handler(9); -} -void EIC_10_Handler(void) { - external_interrupt_handler(10); -} -void EIC_11_Handler(void) { - external_interrupt_handler(11); -} -void EIC_12_Handler(void) { - external_interrupt_handler(12); -} -void EIC_13_Handler(void) { - external_interrupt_handler(13); -} -void EIC_14_Handler(void) { - external_interrupt_handler(14); -} -void EIC_15_Handler(void) { - external_interrupt_handler(15); -} -#endif diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.h b/ports/atmel-samd/common-hal/pulseio/PulseIn.h index e3af42e0a3..3d79b50c3c 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.h +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.h @@ -41,8 +41,12 @@ typedef struct { volatile uint16_t start; volatile uint16_t len; volatile bool first_edge; + volatile uint64_t last_ms; + volatile uint16_t last_us; } pulseio_pulsein_obj_t; void pulsein_reset(void); +void pulsein_interrupt_handler(uint8_t channel); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c index 58119fa7e9..d86f4763db 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c @@ -26,133 +26,104 @@ #include "common-hal/rotaryio/IncrementalEncoder.h" -#include - #include "atmel_start_pins.h" -#include "hal/include/hal_gpio.h" -#include "mpconfigport.h" -#include "peripherals/pins.h" -#include "py/gc.h" +#include "peripherals/external_interrupts.h" #include "py/runtime.h" -#include "shared-bindings/microcontroller/__init__.h" - -#ifdef SAMD21 -#include "hpl/gclk/hpl_gclk_base.h" -#endif void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, - const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { -// if (!pin->has_extint) { -// mp_raise_RuntimeError("No hardware support on pin"); -// } -// uint32_t mask = 1 << pin->extint_channel; -// if (active_incrementalencoders[pin->extint_channel] != NULL || -// (eic_get_enable() == 1 && -// #ifdef SAMD51 -// ((EIC->INTENSET.bit.EXTINT & mask) != 0 || -// (EIC->EVCTRL.bit.EXTINTEO & mask) != 0))) { -// #endif -// #ifdef SAMD21 -// ((EIC->INTENSET.vec.EXTINT & mask) != 0 || -// (EIC->EVCTRL.vec.EXTINTEO & mask) != 0))) { -// #endif -// mp_raise_RuntimeError("EXTINT channel already in use"); -// } -// -// self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); -// if (self->buffer == NULL) { -// mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t)); -// } -// self->channel = pin->extint_channel; -// self->pin = pin->pin; -// self->maxlen = maxlen; -// self->idle_state = idle_state; -// self->start = 0; -// self->len = 0; -// self->first_edge = true; -// -// active_incrementalencoders[pin->extint_channel] = self; -// -// // Check to see if the EIC is enabled and start it up if its not.' -// // SAMD51 EIC can only be clocked up to 100mhz so we use the 48mhz clock. -// if (eic_get_enable() == 0) { -// #ifdef SAMD51 -// MCLK->APBAMASK.bit.EIC_ = true; -// hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, -// GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos)); -// #endif -// -// #ifdef SAMD21 -// PM->APBAMASK.bit.EIC_ = true; -// _gclk_enable_channel(EIC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); -// #endif -// -// -// #ifdef SAMD21 -// NVIC_DisableIRQ(EIC_IRQn); -// NVIC_ClearPendingIRQ(EIC_IRQn); -// NVIC_EnableIRQ(EIC_IRQn); -// #endif -// } -// -// gpio_set_pin_function(pin->pin, GPIO_PIN_FUNCTION_A); -// -// #ifdef SAMD51 -// NVIC_DisableIRQ(EIC_0_IRQn + self->channel); -// NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); -// NVIC_EnableIRQ(EIC_0_IRQn + self->channel); -// #endif -// -// // Set config will enable the EIC. -// incrementalencoder_set_config(self, true); -// EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { + if (!pin_a->has_extint || !pin_a->has_extint) { + mp_raise_RuntimeError("Both pins must support hardware interrupts"); + } + + // TODO: The SAMD51 has a peripheral dedicated to quadrature encoder debugging. Use it instead + // of the external interrupt. + + if (eic_get_enable()) { + if (!eic_channel_free(pin_a->extint_channel) || !eic_channel_free(pin_b->extint_channel)) { + mp_raise_RuntimeError("A hardware interrupt channel is already in use"); + } + } else { + turn_on_external_interrupt_controller(); + } + + // These default settings apply when the EIC isn't yet enabled. + self->eic_channel_a = pin_a->extint_channel; + self->eic_channel_b = pin_b->extint_channel; + self->pin_a = pin_a->pin; + self->pin_b = pin_b->pin; + + gpio_set_pin_function(self->pin_a, GPIO_PIN_FUNCTION_A); + gpio_set_pin_pull_mode(self->pin_a, GPIO_PULL_UP); + + gpio_set_pin_function(self->pin_b, GPIO_PIN_FUNCTION_A); + gpio_set_pin_pull_mode(self->pin_b, GPIO_PULL_UP); + + set_eic_channel_data(self->eic_channel_a, (void*) self); + set_eic_channel_data(self->eic_channel_b, (void*) self); + + bool pin_a_level = gpio_get_pin_level(self->pin_a); + bool pin_b_level = gpio_get_pin_level(self->pin_b); + if (!pin_a_level && !pin_b_level) { + self->last_state = 1; + } else if (!pin_a_level && pin_b_level) { + self->last_state = 2; + } else if (pin_a_level && pin_b_level) { + self->last_state = 3; + } else { + self->last_state = 4; + } + + turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER); + turn_on_eic_channel(self->eic_channel_b, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER); } bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { - //return self->pin == NO_PIN; - return true; + return self->pin_a == NO_PIN; } void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { - // if (common_hal_rotaryio_incrementalencoder_deinited(self)) { - // return; - // } - // uint32_t mask = 1 << self->channel; - // EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; - // #ifdef SAMD51 - // NVIC_DisableIRQ(EIC_0_IRQn + self->channel); - // NVIC_ClearPendingIRQ(EIC_0_IRQn + self->channel); - // #endif - // active_incrementalencoders[self->channel] = NULL; - // reset_pin(self->pin); - // self->pin = NO_PIN; - // - // bool all_null = true; - // for (uint8_t i = 0; all_null && i < 16; i++) { - // all_null = all_null && active_incrementalencoders[i] == NULL; - // } - // #ifdef SAMD21 - // if (all_null && EIC->INTENSET.reg == 0) { - // NVIC_DisableIRQ(EIC_IRQn); - // NVIC_ClearPendingIRQ(EIC_IRQn); - // } - // #endif - // // Test if all channels are null and deinit everything if they are. - // if (all_null && EIC->EVCTRL.reg == 0 && EIC->INTENSET.reg == 0) { - // eic_set_enable(false); - // #ifdef SAMD51 - // MCLK->APBAMASK.bit.EIC_ = false; - // hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, 0); - // #endif - // - // #ifdef SAMD21 - // PM->APBAMASK.bit.EIC_ = false; - // hri_gclk_write_CLKCTRL_reg(GCLK, GCLK_CLKCTRL_ID(EIC_GCLK_ID)); - // #endif - // } + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { + return; + } + turn_off_eic_channel(self->eic_channel_a); + turn_off_eic_channel(self->eic_channel_b); + reset_pin(self->pin_a); + self->pin_a = NO_PIN; + reset_pin(self->pin_b); + self->pin_b = NO_PIN; } mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { - return 0; + return self->position; +} + +void incrementalencoder_interrupt_handler(uint8_t channel) { + rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel); + // TODO(tannewt): If we need more speed then read the pin directly. gpio_get_pin_level has + // smarts to compensate for pin direction we don't need. + bool pin_a = gpio_get_pin_level(self->pin_a); + bool pin_b = gpio_get_pin_level(self->pin_b); + + uint8_t this_state; + if (!pin_a && !pin_b) { + this_state = 1; + } else if (!pin_a && pin_b) { + this_state = 2; + } else if (pin_a && pin_b) { + this_state = 3; + } else { + this_state = 4; + } + + // Handle wrap around explicitly. + if (this_state == 4 && self->last_state == 1) { + self->position -= 1; + } else if (this_state == 1 && self->last_state == 4) { + self->position += 1; + } else { + self->position += (this_state - self->last_state); + } + self->last_state = this_state; } diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h index c878239bcf..ffdf7da174 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h @@ -35,6 +35,13 @@ typedef struct { mp_obj_base_t base; uint8_t pin_a; uint8_t pin_b; + uint8_t eic_channel_a:4; + uint8_t eic_channel_b:4; + uint8_t last_state; + mp_int_t position; } rotaryio_incrementalencoder_obj_t; + +void incrementalencoder_interrupt_handler(uint8_t channel); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H diff --git a/ports/atmel-samd/common-hal/rotaryio/__init__.c b/ports/atmel-samd/common-hal/rotaryio/__init__.c index 2bee925bc7..0aae79c26a 100644 --- a/ports/atmel-samd/common-hal/rotaryio/__init__.c +++ b/ports/atmel-samd/common-hal/rotaryio/__init__.c @@ -1 +1 @@ -// No pulseio module functions. +// No rotaryio module functions. diff --git a/ports/atmel-samd/external_flash/external_flash.c b/ports/atmel-samd/external_flash/external_flash.c index b0e40fc705..e6c407e73e 100644 --- a/ports/atmel-samd/external_flash/external_flash.c +++ b/ports/atmel-samd/external_flash/external_flash.c @@ -36,7 +36,6 @@ #include "py/obj.h" #include "py/runtime.h" #include "lib/oofatfs/ff.h" -//#include "peripherals.h" #include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/rgb_led_status.h" diff --git a/ports/atmel-samd/peripherals/clocks.c b/ports/atmel-samd/peripherals/clocks.c index a890210bf4..bfcca45475 100644 --- a/ports/atmel-samd/peripherals/clocks.c +++ b/ports/atmel-samd/peripherals/clocks.c @@ -56,41 +56,20 @@ uint8_t find_free_gclk(uint16_t divisor) { return 0xff; } -void reset_gclks(void) { - // Never reset GCLK0 because its used for the core - #if CONF_GCLK_GEN_1_GENEN == 0 - disable_gclk(1); - #endif - #if CONF_GCLK_GEN_2_GENEN == 0 - disable_gclk(2); - #endif - #if CONF_GCLK_GEN_3_GENEN == 0 - disable_gclk(3); - #endif - #if CONF_GCLK_GEN_4_GENEN == 0 - disable_gclk(4); - #endif - #if CONF_GCLK_GEN_5_GENEN == 0 - disable_gclk(5); - #endif - #if CONF_GCLK_GEN_6_GENEN == 0 - disable_gclk(6); - #endif - #if CONF_GCLK_GEN_7_GENEN == 0 - disable_gclk(7); - #endif - #ifdef SAMD51 - #if CONF_GCLK_GEN_8_GENEN == 0 - disable_gclk(8); - #endif - #if CONF_GCLK_GEN_9_GENEN == 0 - disable_gclk(9); - #endif - #if CONF_GCLK_GEN_10_GENEN == 0 - disable_gclk(10); - #endif - #if CONF_GCLK_GEN_11_GENEN == 0 - disable_gclk(11); - #endif - #endif +static uint8_t last_static_clock = 0; + +void init_dynamic_clocks(void) { + // Find the last statically initialized clock and save it. Everything after will be reset with + // the VM via reset_gclks. + for (uint8_t i = 0; i < GCLK_GEN_NUM; i++) { + if (gclk_enabled(i)) { + last_static_clock = i; + } + } +} + +void reset_gclks(void) { + for (uint8_t i = last_static_clock + 1; i < GCLK_GEN_NUM; i++) { + disable_gclk(i); + } } diff --git a/ports/atmel-samd/peripherals/clocks.h b/ports/atmel-samd/peripherals/clocks.h index 6b7cfedce0..78b2e2e3d5 100644 --- a/ports/atmel-samd/peripherals/clocks.h +++ b/ports/atmel-samd/peripherals/clocks.h @@ -63,6 +63,7 @@ static inline bool board_has_crystal(void) { } void clock_init(void); +void init_dynamic_clocks(void); bool clock_get_enabled(uint8_t type, uint8_t index); bool clock_get_parent(uint8_t type, uint8_t index, uint8_t *p_type, uint8_t *p_index); diff --git a/ports/atmel-samd/peripherals/events.c b/ports/atmel-samd/peripherals/events.c index 9a4ab3b0ca..2f6aea7166 100644 --- a/ports/atmel-samd/peripherals/events.c +++ b/ports/atmel-samd/peripherals/events.c @@ -27,9 +27,6 @@ #include #include "peripherals/events.h" -// -// #include "clocks.h" -// #include "py/runtime.h" uint8_t find_async_event_channel(void) { diff --git a/ports/atmel-samd/peripherals/external_interrupts.c b/ports/atmel-samd/peripherals/external_interrupts.c new file mode 100644 index 0000000000..dfec6c9987 --- /dev/null +++ b/ports/atmel-samd/peripherals/external_interrupts.c @@ -0,0 +1,105 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "common-hal/pulseio/PulseIn.h" +#include "common-hal/rotaryio/IncrementalEncoder.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "peripherals/external_interrupts.h" + +#include "sam.h" + +// This structure is used to share per-channel storage amongst all users of external interrupts. +// Without this there would be multiple arrays even though they are disjoint because each channel +// has one user. +static void *channel_data[EIC_EXTINT_NUM]; +static uint8_t channel_handler[EIC_EXTINT_NUM]; + +void external_interrupt_handler(uint8_t channel) { + uint8_t handler = channel_handler[channel]; + if (handler == EIC_HANDLER_PULSEIN) { + pulsein_interrupt_handler(channel); + } else if (handler == EIC_HANDLER_INCREMENTAL_ENCODER) { + incrementalencoder_interrupt_handler(channel); + } + EIC->INTFLAG.reg = (1 << channel) << EIC_INTFLAG_EXTINT_Pos; +} + +void configure_eic_channel(uint8_t eic_channel, uint32_t sense_setting) { + uint8_t config_index = eic_channel / 8; + uint8_t position = (eic_channel % 8) * 4; + #ifdef SAMD51 + eic_set_enable(false); + #endif + common_hal_mcu_disable_interrupts(); + uint32_t masked_value = EIC->CONFIG[config_index].reg & ~(0xf << position); + EIC->CONFIG[config_index].reg = masked_value | (sense_setting << position); + common_hal_mcu_enable_interrupts(); + #ifdef SAMD51 + eic_set_enable(true); + #endif +} + +void turn_on_eic_channel(uint8_t eic_channel, uint32_t sense_setting, + uint8_t channel_interrupt_handler) { + // We do very light filtering using majority voting. + sense_setting |= EIC_CONFIG_FILTEN0; + configure_eic_channel(eic_channel, sense_setting); + uint32_t mask = 1 << eic_channel; + EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; + if (channel_interrupt_handler != EIC_HANDLER_NO_INTERRUPT) { + channel_handler[eic_channel] = channel_interrupt_handler; + turn_on_cpu_interrupt(eic_channel); + } +} + +void turn_off_eic_channel(uint8_t eic_channel) { + uint32_t mask = 1 << eic_channel; + EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; + #ifdef SAMD51 + NVIC_DisableIRQ(EIC_0_IRQn + eic_channel); + NVIC_ClearPendingIRQ(EIC_0_IRQn + eic_channel); + #endif + channel_data[eic_channel] = NULL; + + #ifdef SAMD21 + if (EIC->INTENSET.reg == 0) { + NVIC_DisableIRQ(EIC_IRQn); + NVIC_ClearPendingIRQ(EIC_IRQn); + } + #endif + // Test if all channels are null and deinit everything if they are. + if (EIC->EVCTRL.reg == 0 && EIC->INTENSET.reg == 0) { + turn_off_external_interrupt_controller(); + } +} + +void* get_eic_channel_data(uint8_t eic_channel) { + return channel_data[eic_channel]; +} + +void set_eic_channel_data(uint8_t eic_channel, void* data) { + channel_data[eic_channel] = data; +} diff --git a/ports/atmel-samd/peripherals/external_interrupts.h b/ports/atmel-samd/peripherals/external_interrupts.h new file mode 100644 index 0000000000..12d8206770 --- /dev/null +++ b/ports/atmel-samd/peripherals/external_interrupts.h @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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_ATMEL_SAMD_PERIPHERALS_EXTERNAL_INTERRUPTS_H +#define MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_EXTERNAL_INTERRUPTS_H + +#include +#include + +#define EIC_HANDLER_NO_INTERRUPT 0x0 +#define EIC_HANDLER_PULSEIN 0x1 +#define EIC_HANDLER_INCREMENTAL_ENCODER 0x2 + +void turn_on_external_interrupt_controller(void); +void turn_off_external_interrupt_controller(void); +void turn_on_cpu_interrupt(uint8_t eic_channel); +void turn_on_eic_channel(uint8_t eic_channel, uint32_t sense_setting, + uint8_t channel_interrupt_handler); +void configure_eic_channel(uint8_t eic_channel, uint32_t sense_setting); +void turn_off_eic_channel(uint8_t eic_channel); +bool eic_channel_free(uint8_t eic_channel); +bool eic_get_enable(void); +void eic_set_enable(bool value); +void eic_reset(void); + +void* get_eic_channel_data(uint8_t eic_channel); +void set_eic_channel_data(uint8_t eic_channel, void* data); + +void external_interrupt_handler(uint8_t channel); + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_PERIPHERALS_EXTERNAL_INTERRUPTS_H diff --git a/ports/atmel-samd/peripherals/samd21/clocks.c b/ports/atmel-samd/peripherals/samd21/clocks.c index 94b5158146..aad251a84c 100644 --- a/ports/atmel-samd/peripherals/samd21/clocks.c +++ b/ports/atmel-samd/peripherals/samd21/clocks.c @@ -124,17 +124,23 @@ static void init_clock_source_dfll48m(void) { void clock_init(void) { init_clock_source_osc8m(); - if (board_has_crystal()) + if (board_has_crystal()) { init_clock_source_xosc32k(); - else + } else { init_clock_source_osc32k(); + } + enable_clock_generator(0, GCLK_GENCTRL_SRC_DFLL48M_Val, 1); enable_clock_generator(1, GCLK_GENCTRL_SRC_DFLL48M_Val, 150); init_clock_source_dfll48m(); - if (board_has_crystal()) + if (board_has_crystal()) { enable_clock_generator(2, GCLK_GENCTRL_SRC_XOSC32K_Val, 32); - else + } else { enable_clock_generator(2, GCLK_GENCTRL_SRC_OSC32K_Val, 32); + } + + // Do this after all static clock init so that they aren't used dynamically. + init_dynamic_clocks(); } static bool clk_enabled(uint8_t clk) { diff --git a/ports/atmel-samd/peripherals/samd21/external_interrupts.c b/ports/atmel-samd/peripherals/samd21/external_interrupts.c new file mode 100644 index 0000000000..209c439c0a --- /dev/null +++ b/ports/atmel-samd/peripherals/samd21/external_interrupts.c @@ -0,0 +1,86 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "peripherals/external_interrupts.h" + +#include "hpl/gclk/hpl_gclk_base.h" +#include "peripherals/clocks.h" +#include "sam.h" + +void turn_on_external_interrupt_controller(void) { + PM->APBAMASK.bit.EIC_ = true; + _gclk_enable_channel(EIC_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); + eic_set_enable(true); +} + +void turn_off_external_interrupt_controller(void) { + eic_set_enable(false); + PM->APBAMASK.bit.EIC_ = false; + hri_gclk_write_CLKCTRL_reg(GCLK, GCLK_CLKCTRL_ID(EIC_GCLK_ID)); +} + +void turn_on_cpu_interrupt(uint8_t eic_channel) { + // Ignore the channel since the CPU interrupt line is shared. + (void) eic_channel; + + NVIC_DisableIRQ(EIC_IRQn); + NVIC_ClearPendingIRQ(EIC_IRQn); + NVIC_EnableIRQ(EIC_IRQn); +} + +bool eic_get_enable(void) { + return EIC->CTRL.bit.ENABLE; +} + +void eic_set_enable(bool value) { + EIC->CTRL.bit.ENABLE = value; + while (EIC->STATUS.bit.SYNCBUSY != 0) {} +} + +void eic_reset(void) { + EIC->CTRL.bit.SWRST = true; + while (EIC->STATUS.bit.SYNCBUSY != 0) {} + for (int i = 0; i < EIC_EXTINT_NUM; i++) { + set_eic_channel_data(i, NULL); + } + NVIC_DisableIRQ(EIC_IRQn); + NVIC_ClearPendingIRQ(EIC_IRQn); +} + +bool eic_channel_free(uint8_t eic_channel) { + uint32_t mask = 1 << eic_channel; + return get_eic_channel_data(eic_channel) == NULL && + (EIC->INTENSET.vec.EXTINT & mask) == 0 && + (EIC->EVCTRL.vec.EXTINTEO & mask) == 0; +} + +void EIC_Handler(void) { + for (uint8_t i = 0; i < 16; i++) { + if ((EIC->INTFLAG.vec.EXTINT & (1 << i)) != 0) { + external_interrupt_handler(i); + } + } +} diff --git a/ports/atmel-samd/peripherals/samd51/external_interrupts.c b/ports/atmel-samd/peripherals/samd51/external_interrupts.c new file mode 100644 index 0000000000..6006d480e6 --- /dev/null +++ b/ports/atmel-samd/peripherals/samd51/external_interrupts.c @@ -0,0 +1,132 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "peripherals/external_interrupts.h" + +#include "peripherals/clocks.h" +#include "sam.h" + +void turn_on_external_interrupt_controller(void) { + MCLK->APBAMASK.bit.EIC_ = true; + + // We use the 48mhz clock to lightly filter the incoming pulse to reduce spurious interrupts. + connect_gclk_to_peripheral(GCLK_PCHCTRL_GEN_GCLK1_Val, EIC_GCLK_ID); + eic_set_enable(true); +} + +void turn_off_external_interrupt_controller(void) { + eic_set_enable(false); + MCLK->APBAMASK.bit.EIC_ = false; + disconnect_gclk_from_peripheral(GCLK_PCHCTRL_GEN_GCLK1_Val, EIC_GCLK_ID); +} + +void turn_on_cpu_interrupt(uint8_t eic_channel) { + // Ignore the channel since the CPU interrupt line is shared. + (void) eic_channel; + + NVIC_DisableIRQ(EIC_0_IRQn + eic_channel); + NVIC_ClearPendingIRQ(EIC_0_IRQn + eic_channel); + NVIC_EnableIRQ(EIC_0_IRQn + eic_channel); +} + +bool eic_get_enable(void) { + return EIC->CTRLA.bit.ENABLE; +} + +void eic_set_enable(bool value) { + EIC->CTRLA.bit.ENABLE = value; + while (EIC->SYNCBUSY.bit.ENABLE != 0) {} + // This won't actually block long enough in Rev A of SAMD51 and will miss edges in the first + // three cycles of the peripheral clock. See the errata for details. It shouldn't impact us. +} + +void eic_reset(void) { + EIC->CTRLA.bit.SWRST = true; + while (EIC->SYNCBUSY.bit.SWRST != 0) {} + // This won't actually block long enough in Rev A of SAMD51 and will miss edges in the first + // three cycles of the peripheral clock. See the errata for details. It shouldn't impact us. + for (int i = 0; i < EIC_EXTINT_NUM; i++) { + set_eic_channel_data(i, NULL); + NVIC_DisableIRQ(EIC_0_IRQn + i); + NVIC_ClearPendingIRQ(EIC_0_IRQn + i); + } +} + +bool eic_channel_free(uint8_t eic_channel) { + uint32_t mask = 1 << eic_channel; + return get_eic_channel_data(eic_channel) == NULL && + (EIC->INTENSET.bit.EXTINT & mask) == 0 && + (EIC->EVCTRL.bit.EXTINTEO & mask) == 0; +} + +void EIC_0_Handler(void) { + external_interrupt_handler(0); +} +void EIC_1_Handler(void) { + external_interrupt_handler(1); +} +void EIC_2_Handler(void) { + external_interrupt_handler(2); +} +void EIC_3_Handler(void) { + external_interrupt_handler(3); +} +void EIC_4_Handler(void) { + external_interrupt_handler(4); +} +void EIC_5_Handler(void) { + external_interrupt_handler(5); +} +void EIC_6_Handler(void) { + external_interrupt_handler(6); +} +void EIC_7_Handler(void) { + external_interrupt_handler(7); +} +void EIC_8_Handler(void) { + external_interrupt_handler(8); +} +void EIC_9_Handler(void) { + external_interrupt_handler(9); +} +void EIC_10_Handler(void) { + external_interrupt_handler(10); +} +void EIC_11_Handler(void) { + external_interrupt_handler(11); +} +void EIC_12_Handler(void) { + external_interrupt_handler(12); +} +void EIC_13_Handler(void) { + external_interrupt_handler(13); +} +void EIC_14_Handler(void) { + external_interrupt_handler(14); +} +void EIC_15_Handler(void) { + external_interrupt_handler(15); +} diff --git a/ports/atmel-samd/peripherals/samd51/sercom.c b/ports/atmel-samd/peripherals/samd51/sercom.c index fc44214220..6d0a02fbd7 100644 --- a/ports/atmel-samd/peripherals/samd51/sercom.c +++ b/ports/atmel-samd/peripherals/samd51/sercom.c @@ -28,8 +28,6 @@ #include "hpl/gclk/hpl_gclk_base.h" #include "hri/hri_mclk_d51.h" -// FIXME(tannewt): Should this be called sercom.c? - // The clock initializer values are rather random, so we need to put them in // tables for lookup. We can't compute them. diff --git a/ports/atmel-samd/peripherals/samd51/timers.c b/ports/atmel-samd/peripherals/samd51/timers.c index 305acce381..f25dbc0e41 100644 --- a/ports/atmel-samd/peripherals/samd51/timers.c +++ b/ports/atmel-samd/peripherals/samd51/timers.c @@ -29,8 +29,6 @@ #include "peripherals/timers.h" -//#include "common-hal/pulseio/PulseOut.h" - #include "hri/hri_gclk_d51.h" const uint8_t tcc_cc_num[5] = {6, 4, 3, 2, 2}; diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 55e0a7ecfc..e815d0f1ec 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -58,6 +58,7 @@ #include "peripherals/cache.h" #include "peripherals/clocks.h" #include "peripherals/events.h" +#include "peripherals/external_interrupts.h" #include "peripherals/dma.h" #include "shared-bindings/rtc/__init__.h" #include "tick.h" @@ -183,6 +184,7 @@ safe_mode_t port_init(void) { _pm_init(); #endif clock_init(); + init_dynamic_clocks(); board_init(); @@ -248,7 +250,7 @@ void reset_port(void) { #ifdef SAMD21 touchin_reset(); #endif - pulsein_reset(); + eic_reset(); pulseout_reset(); pwmout_reset(); diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 76b5e340b8..d985878809 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -17,6 +17,9 @@ SAMD51, SAMD51 Express, and ESP8266. NOTE 2: **SAMD** and/or **SAMD Express** without additional numbers, means both SAMD21 & SAMD51 versions are supported. +NOTE 3: The `pIRkey SAMD21 board `_ is specialized and may not +have modules as listed below. + ================= ============================== Module Supported Ports ================= ============================== @@ -38,6 +41,7 @@ Module Supported Ports `os` **All Supported** `pulseio` **SAMD/SAMD Express** `random` **All Supported** +`rotaryio` **SAMD51, SAMD Express** `storage` **All Supported** `struct` **All Supported** `supervisor` **SAMD/SAMD Express** diff --git a/shared-bindings/rotaryio/__init__.c b/shared-bindings/rotaryio/__init__.c index db1ff3d4c6..a0166771ab 100644 --- a/shared-bindings/rotaryio/__init__.c +++ b/shared-bindings/rotaryio/__init__.c @@ -33,8 +33,8 @@ #include "shared-bindings/rotaryio/__init__.h" #include "shared-bindings/rotaryio/IncrementalEncoder.h" -//| :mod:`rotaryio` --- Support for pulse based protocols -//| ===================================================== +//| :mod:`rotaryio` --- Support for reading rotation sensors +//| ======================================================== //| //| .. module:: rotaryio //| :synopsis: Support for reading rotation sensors From 9920f0a5de11b894b6255888475e6272e5e83f94 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 31 May 2018 16:47:18 -0700 Subject: [PATCH 086/103] atmel-samd: Make ticks more atomic. Always use current_tick when sub millisecond precision is required. Otherwise getting the ms/us to correspond is tricky. --- .../common-hal/microcontroller/__init__.c | 24 ++++++++++++++-- ports/atmel-samd/tick.c | 28 ++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/ports/atmel-samd/common-hal/microcontroller/__init__.c b/ports/atmel-samd/common-hal/microcontroller/__init__.c index fa057facac..c57b6939ce 100644 --- a/ports/atmel-samd/common-hal/microcontroller/__init__.c +++ b/ports/atmel-samd/common-hal/microcontroller/__init__.c @@ -42,13 +42,31 @@ void common_hal_mcu_delay_us(uint32_t delay) { // Interrupt flags that will be saved and restored during disable/Enable // interrupt functions below. -volatile hal_atomic_t flags; + +// ASF4's interrupt disable doesn't handle duplicate calls +volatile uint32_t interrupt_flags; +volatile uint32_t nesting_count = 0; void common_hal_mcu_disable_interrupts(void) { - atomic_enter_critical(&flags); + if (nesting_count == 0) { + interrupt_flags = __get_PRIMASK(); + __disable_irq(); + __DMB(); + } + nesting_count++; } void common_hal_mcu_enable_interrupts(void) { - atomic_leave_critical(&flags); + if (nesting_count == 0) { + // This is very very bad because it means there was mismatched disable/enables so we + // "HardFault". + HardFault_Handler(); + } + nesting_count--; + if (nesting_count > 0) { + return; + } + __DMB(); + __set_PRIMASK(interrupt_flags); } extern uint32_t _ezero; diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 27b5f05b44..6cd7c7833f 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -30,6 +30,7 @@ #include "supervisor/shared/autoreload.h" #include "shared-module/gamepad/__init__.h" +#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Processor.h" // Global millisecond tick count @@ -38,8 +39,13 @@ volatile uint64_t ticks_ms = 0; void SysTick_Handler(void) { // SysTick interrupt handler called when the SysTick timer reaches zero // (every millisecond). + common_hal_mcu_disable_interrupts(); ticks_ms += 1; + // Read the control register to reset the COUNTFLAG. + (void) SysTick->CTRL; + common_hal_mcu_enable_interrupts(); + #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS autoreload_tick(); #endif @@ -61,7 +67,7 @@ void tick_delay(uint32_t us) { uint32_t us_until_next_tick = SysTick->VAL / ticks_per_us; uint32_t start_tick; while (us >= us_until_next_tick) { - start_tick=SysTick->VAL; // wait for SysTick->VAL to RESET + start_tick = SysTick->VAL; // wait for SysTick->VAL to RESET while (SysTick->VAL < start_tick) {} us -= us_until_next_tick; us_until_next_tick = 1000; @@ -72,11 +78,25 @@ void tick_delay(uint32_t us) { // us counts down! void current_tick(uint64_t* ms, uint32_t* us_until_ms) { uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000; - *ms = ticks_ms; - *us_until_ms = SysTick->VAL / ticks_per_us; + + // We disable interrupts to prevent ticks_ms from changing while we grab it. + common_hal_mcu_disable_interrupts(); + uint32_t tick_status = SysTick->CTRL; + uint32_t current_us = SysTick->VAL; + uint32_t tick_status2 = SysTick->CTRL; + uint64_t current_ms = ticks_ms; + // The second clause ensures our value actually rolled over. Its possible it hit zero between + // the VAL read and CTRL read. + if ((tick_status & SysTick_CTRL_COUNTFLAG_Msk) != 0 || + ((tick_status2 & SysTick_CTRL_COUNTFLAG_Msk) != 0 && current_us > ticks_per_us)) { + current_ms++; + } + common_hal_mcu_enable_interrupts(); + *ms = current_ms; + *us_until_ms = current_us / ticks_per_us; } void wait_until(uint64_t ms, uint32_t us_until_ms) { uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000; - while(ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {} + while (ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {} } From f386144428b8ad4fab29e0df6657856141e7f96b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 1 Jun 2018 11:00:40 -0400 Subject: [PATCH 087/103] redo state algorithm --- .../common-hal/rotaryio/IncrementalEncoder.c | 81 ++++++++++++------- .../common-hal/rotaryio/IncrementalEncoder.h | 3 +- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c index d86f4763db..9f378f169e 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c @@ -63,17 +63,14 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode set_eic_channel_data(self->eic_channel_a, (void*) self); set_eic_channel_data(self->eic_channel_b, (void*) self); - bool pin_a_level = gpio_get_pin_level(self->pin_a); - bool pin_b_level = gpio_get_pin_level(self->pin_b); - if (!pin_a_level && !pin_b_level) { - self->last_state = 1; - } else if (!pin_a_level && pin_b_level) { - self->last_state = 2; - } else if (pin_a_level && pin_b_level) { - self->last_state = 3; - } else { - self->last_state = 4; - } + self->position = 0; + self->quarter_count = 0; + + // Top two bits of self->last_state don't matter, because they'll be gone as soon as + // interrupt handler is called. + self->last_state = + ((uint8_t) gpio_get_pin_level(self->pin_a) << 1) | + (uint8_t) gpio_get_pin_level(self->pin_b); turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER); turn_on_eic_channel(self->eic_channel_b, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER); @@ -101,29 +98,55 @@ mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementa void incrementalencoder_interrupt_handler(uint8_t channel) { rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel); + + // This table also works for detent both at 11 and 00 + // For 11 at detent: + // Turning cw: 11->01->00->10->11 + // Turning ccw: 11->10->00->01->11 + // For 00 at detent: + // Turning cw: 00->10->11->10->00 + // Turning ccw: 00->01->11->10->00 + + // index table by state + #define BAD 7 + static const int8_t transitions[16] = { + 0, // 00 -> 00 no movement + -1, // 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent) + +1, // 00 -> 10 3/4 cw or 1/4 cw + BAD, // 00 -> 11 non-Gray-code transition + +1, // 01 -> 00 2/4 or 4/4 cw + 0, // 01 -> 01 no movement + BAD, // 01 -> 10 non-Gray-code transition + -1, // 01 -> 11 4/4 or 2/4 ccw + -1, // 10 -> 00 2/4 or 4/4 ccw + BAD, // 10 -> 01 non-Gray-code transition + 0, // 10 -> 10 no movement + +1, // 10 -> 11 4/4 or 2/4 cw + BAD, // 11 -> 00 non-Gray-code transition + +1, // 11 -> 01 1/4 or 3/4 cw + -1, // 11 -> 10 1/4 or 3/4 ccw + 0, // 11 -> 11 no movement + }; + + // Shift the old AB bits to the "old" position, and set the new AB bits. // TODO(tannewt): If we need more speed then read the pin directly. gpio_get_pin_level has // smarts to compensate for pin direction we don't need. - bool pin_a = gpio_get_pin_level(self->pin_a); - bool pin_b = gpio_get_pin_level(self->pin_b); + self->last_state = (self->last_state & 0x3) << 2 | + ((uint8_t) gpio_get_pin_level(self->pin_a) << 1) | + (uint8_t) gpio_get_pin_level(self->pin_b); - uint8_t this_state; - if (!pin_a && !pin_b) { - this_state = 1; - } else if (!pin_a && pin_b) { - this_state = 2; - } else if (pin_a && pin_b) { - this_state = 3; - } else { - this_state = 4; + int8_t quarter_incr = transitions[self->last_state]; + if (quarter_incr == BAD) { + // Missed a transition. We don't know which way we're going, so do nothing. + return; } - // Handle wrap around explicitly. - if (this_state == 4 && self->last_state == 1) { - self->position -= 1; - } else if (this_state == 1 && self->last_state == 4) { + self->quarter_count += quarter_incr; + if (self->quarter_count >= 4) { self->position += 1; - } else { - self->position += (this_state - self->last_state); + self->quarter_count = 0; + } else if (self->quarter_count <= -4) { + self->position -= 1; + self->quarter_count = 0; } - self->last_state = this_state; } diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h index ffdf7da174..e07cc84d5d 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h @@ -37,7 +37,8 @@ typedef struct { uint8_t pin_b; uint8_t eic_channel_a:4; uint8_t eic_channel_b:4; - uint8_t last_state; + uint8_t last_state:4; // + int8_t quarter_count:4; // count intermediate transitions between detents mp_int_t position; } rotaryio_incrementalencoder_obj_t; From 717199018bcb43dd50af9011e2259f3caf3290f8 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 1 Jun 2018 13:11:40 -0700 Subject: [PATCH 088/103] Adapt for feedback and hack around pIRkey size constraint. --- ports/atmel-samd/common-hal/busio/I2C.c | 4 ++++ .../common-hal/rotaryio/IncrementalEncoder.c | 5 +++++ ports/atmel-samd/peripherals/samd21/events.c | 12 +++++------ shared-bindings/rotaryio/IncrementalEncoder.c | 21 +++++++++++++------ shared-bindings/rotaryio/IncrementalEncoder.h | 2 ++ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/ports/atmel-samd/common-hal/busio/I2C.c b/ports/atmel-samd/common-hal/busio/I2C.c index 66929beff5..c14b2d68f0 100644 --- a/ports/atmel-samd/common-hal/busio/I2C.c +++ b/ports/atmel-samd/common-hal/busio/I2C.c @@ -41,6 +41,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) { + #ifdef PIRKEY_M0 + mp_raise_NotImplementedError("Not enough pins available"); + return; + #endif Sercom* sercom = NULL; uint8_t sercom_index; uint32_t sda_pinmux = 0; diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c index 9f378f169e..ddc5452950 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c @@ -96,6 +96,11 @@ mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementa return self->position; } +void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, + mp_int_t new_position) { + self->position = new_position; +} + void incrementalencoder_interrupt_handler(uint8_t channel) { rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel); diff --git a/ports/atmel-samd/peripherals/samd21/events.c b/ports/atmel-samd/peripherals/samd21/events.c index 70dfa249dd..08b82c91fc 100644 --- a/ports/atmel-samd/peripherals/samd21/events.c +++ b/ports/atmel-samd/peripherals/samd21/events.c @@ -74,8 +74,8 @@ void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generat EVSYS_CHANNEL_EVGEN(generator) | EVSYS_CHANNEL_PATH_RESYNCHRONIZED | EVSYS_CHANNEL_EDGSEL_RISING_EDGE; - if (channel > 7) { - uint8_t value = 1 << (channel - 7); + if (channel >= 8) { + uint8_t value = 1 << (channel - 8); EVSYS->INTFLAG.reg = EVSYS_INTFLAG_EVDp8(value) | EVSYS_INTFLAG_OVRp8(value); EVSYS->INTENSET.reg = EVSYS_INTENSET_EVDp8(value) | EVSYS_INTENSET_OVRp8(value); } else { @@ -87,8 +87,8 @@ void init_event_channel_interrupt(uint8_t channel, uint8_t gclk, uint8_t generat bool event_interrupt_active(uint8_t channel) { bool active = false; - if (channel > 7) { - uint8_t value = 1 << (channel - 7); + if (channel >= 8) { + uint8_t value = 1 << (channel - 8); active = (EVSYS->INTFLAG.reg & EVSYS_INTFLAG_EVDp8(value)) != 0; // Only clear if we know its active, otherwise there is the possibility it becomes active // after we check but before we clear. @@ -107,8 +107,8 @@ bool event_interrupt_active(uint8_t channel) { bool event_interrupt_overflow(uint8_t channel) { bool overflow = false; - if (channel > 7) { - uint8_t value = 1 << (channel - 7); + if (channel >= 8) { + uint8_t value = 1 << (channel - 8); overflow = (EVSYS->INTFLAG.reg & EVSYS_INTFLAG_OVRp8(value)) != 0; } else { uint8_t value = 1 << channel; diff --git a/shared-bindings/rotaryio/IncrementalEncoder.c b/shared-bindings/rotaryio/IncrementalEncoder.c index 635826c60e..b2609f4a84 100644 --- a/shared-bindings/rotaryio/IncrementalEncoder.c +++ b/shared-bindings/rotaryio/IncrementalEncoder.c @@ -36,8 +36,8 @@ //| .. currentmodule:: rotaryio //| -//| :class:`IncrementalEncoder` -- Read the position of the incremental encoder -//| ============================================================================ +//| :class:`IncrementalEncoder` -- Track the relative position of an incremental encoder +//| ==================================================================================== //| //| IncrementalEncoder determines the relative rotational position based on two series of pulses. //| @@ -47,8 +47,8 @@ //| state of an incremental rotary encoder (also known as a quadrature encoder.) Position is //| relative to the position when the object is contructed. //| -//| :param ~microcontroller.Pin pin: First pin to read pulses from. -//| :param ~microcontroller.Pin pin: Second pin to read pulses from. +//| :param ~microcontroller.Pin pin_a: First pin to read pulses from. +//| :param ~microcontroller.Pin pin_b: Second pin to read pulses from. //| //| For example:: //| @@ -94,7 +94,7 @@ STATIC mp_obj_t rotaryio_incrementalencoder_make_new(const mp_obj_type_t *type, //| .. method:: deinit() //| -//| Deinitialises the IncrementalEncoder and releases any hardware resources for reuse. +//| Deinitializes the IncrementalEncoder and releases any hardware resources for reuse. //| STATIC mp_obj_t rotaryio_incrementalencoder_deinit(mp_obj_t self_in) { rotaryio_incrementalencoder_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -135,10 +135,19 @@ STATIC mp_obj_t rotaryio_incrementalencoder_obj_get_position(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(rotaryio_incrementalencoder_get_position_obj, rotaryio_incrementalencoder_obj_get_position); +STATIC mp_obj_t rotaryio_incrementalencoder_obj_set_position(mp_obj_t self_in, mp_obj_t new_position) { + rotaryio_incrementalencoder_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_rotaryio_incrementalencoder_deinited(self)); + + common_hal_rotaryio_incrementalencoder_set_position(self, mp_obj_get_int(new_position)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(rotaryio_incrementalencoder_set_position_obj, rotaryio_incrementalencoder_obj_set_position); + const mp_obj_property_t rotaryio_incrementalencoder_position_obj = { .base.type = &mp_type_property, .proxy = {(mp_obj_t)&rotaryio_incrementalencoder_get_position_obj, - (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&rotaryio_incrementalencoder_set_position_obj, (mp_obj_t)&mp_const_none_obj}, }; diff --git a/shared-bindings/rotaryio/IncrementalEncoder.h b/shared-bindings/rotaryio/IncrementalEncoder.h index 29e89e2089..f70632aefb 100644 --- a/shared-bindings/rotaryio/IncrementalEncoder.h +++ b/shared-bindings/rotaryio/IncrementalEncoder.h @@ -37,5 +37,7 @@ extern void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementa extern void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self); extern bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self); extern mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self); +extern void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, + mp_int_t new_position); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_ROTARYIO_INCREMENTALENCODER_H From 1c35dbfb5d43ed94108475e8cd17bcb81cda646f Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Fri, 1 Jun 2018 13:49:04 -0400 Subject: [PATCH 089/103] Provide mp_errno_to_str even without uerrno We can provide a basic version of mp_errno_to_str even if the uerrno module won't be provided. Rather than looking errno names up in the uerrno module's globals dict, we'll just rely on a simple mapping in the function itself. --- py/moduerrno.c | 15 +++++++++++++-- py/mperrno.h | 6 ------ py/objexcept.c | 2 -- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/py/moduerrno.c b/py/moduerrno.c index ce8a6bf89e..660fca07a2 100644 --- a/py/moduerrno.c +++ b/py/moduerrno.c @@ -30,8 +30,6 @@ #include "py/obj.h" #include "py/mperrno.h" -#if MICROPY_PY_UERRNO - // This list can be defined per port in mpconfigport.h to tailor it to a // specific port's needs. If it's not defined then we provide a default. #ifndef MICROPY_PY_UERRNO_LIST @@ -61,6 +59,8 @@ #endif +#if MICROPY_PY_UERRNO + #if MICROPY_PY_UERRNO_ERRORCODE STATIC const mp_rom_map_elem_t errorcode_table[] = { #define X(e) { MP_ROM_INT(MP_ ## e), MP_ROM_QSTR(MP_QSTR_## e) }, @@ -133,4 +133,15 @@ qstr mp_errno_to_str(mp_obj_t errno_val) { #endif } +#else //MICROPY_PY_UERRNO + +qstr mp_errno_to_str(mp_obj_t errno_val) { + int v = MP_OBJ_SMALL_INT_VALUE(errno_val); + #define X(e) if (v == e) return (MP_QSTR_ ## e); + MICROPY_PY_UERRNO_LIST + #undef X + + return MP_QSTR_; +} + #endif //MICROPY_PY_UERRNO diff --git a/py/mperrno.h b/py/mperrno.h index caac116ab6..a028157ba7 100644 --- a/py/mperrno.h +++ b/py/mperrno.h @@ -142,12 +142,6 @@ #endif -#if MICROPY_PY_UERRNO - -#include "py/obj.h" - qstr mp_errno_to_str(mp_obj_t errno_val); -#endif - #endif // MICROPY_INCLUDED_PY_MPERRNO_H diff --git a/py/objexcept.c b/py/objexcept.c index a800a13fb1..f0874df773 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -113,7 +113,6 @@ STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_pr mp_print_str(print, ""); return; } else if (o->args->len == 1) { - #if MICROPY_PY_UERRNO // try to provide a nice OSError error message if (o->base.type == &mp_type_OSError && MP_OBJ_IS_SMALL_INT(o->args->items[0])) { qstr qst = mp_errno_to_str(o->args->items[0]); @@ -122,7 +121,6 @@ STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_pr return; } } - #endif mp_obj_print_helper(print, o->args->items[0], PRINT_STR); return; } From e798b67ca2be756c07558d18887698f2c9c06c2b Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Fri, 1 Jun 2018 14:01:09 -0400 Subject: [PATCH 090/103] Begin a custom list of errnos for atmel-samd The uerrno module was written to allow boards to customize the list of errnos they can raise. Start by copying the default list. --- ports/atmel-samd/mpconfigport.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index cd05927b08..b338fb6bbf 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -290,6 +290,30 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_uheap),(mp_obj_t)&uheap_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_ustack),(mp_obj_t)&ustack_module } +#define MICROPY_PY_UERRNO_LIST \ + X(EPERM) \ + X(ENOENT) \ + X(EIO) \ + X(EBADF) \ + X(EAGAIN) \ + X(ENOMEM) \ + X(EACCES) \ + X(EEXIST) \ + X(ENODEV) \ + X(EISDIR) \ + X(EINVAL) \ + X(EOPNOTSUPP) \ + X(EADDRINUSE) \ + X(ECONNABORTED) \ + X(ECONNRESET) \ + X(ENOBUFS) \ + X(ENOTCONN) \ + X(ETIMEDOUT) \ + X(ECONNREFUSED) \ + X(EHOSTUNREACH) \ + X(EALREADY) \ + X(EINPROGRESS) \ + // We need to provide a declaration/definition of alloca() #include From 22f443878788ac5ed50a713897a7a3d4471c68da Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Fri, 1 Jun 2018 14:06:10 -0400 Subject: [PATCH 091/103] Prune atmel-samd errno list Remove errnos that are only raised by modules that aren't linked into the atmel-samd port. --- ports/atmel-samd/mpconfigport.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index b338fb6bbf..eefc119574 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -294,7 +294,6 @@ extern const struct _mp_obj_module_t usb_hid_module; X(EPERM) \ X(ENOENT) \ X(EIO) \ - X(EBADF) \ X(EAGAIN) \ X(ENOMEM) \ X(EACCES) \ @@ -302,17 +301,6 @@ extern const struct _mp_obj_module_t usb_hid_module; X(ENODEV) \ X(EISDIR) \ X(EINVAL) \ - X(EOPNOTSUPP) \ - X(EADDRINUSE) \ - X(ECONNABORTED) \ - X(ECONNRESET) \ - X(ENOBUFS) \ - X(ENOTCONN) \ - X(ETIMEDOUT) \ - X(ECONNREFUSED) \ - X(EHOSTUNREACH) \ - X(EALREADY) \ - X(EINPROGRESS) \ // We need to provide a declaration/definition of alloca() #include From e580d22f4a63cb56834495620589e038e93e791a Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 1 Jun 2018 18:01:42 -0700 Subject: [PATCH 092/103] Use the external crystal on SAMD21 again. Also, re-enable calibration storage for CircuitPlayground Express. Tested with a 500hz PWMOut on Metro M0 with Saleae: * with crystal 500hz * with usb 500hz +- 0.1hz * without either 487hz += 0.1hz SAMD51 is skipped due to DFLL errata and the fact it defaults to a factory calibrated 48mhz that works fine for USB. Fixes #648 --- ports/atmel-samd/peripherals/clocks.h | 1 + ports/atmel-samd/peripherals/samd21/clocks.c | 92 ++++++++++++++++++-- ports/atmel-samd/peripherals/samd51/clocks.c | 8 ++ ports/atmel-samd/supervisor/filesystem.c | 7 -- ports/atmel-samd/supervisor/port.c | 61 +------------ 5 files changed, 100 insertions(+), 69 deletions(-) diff --git a/ports/atmel-samd/peripherals/clocks.h b/ports/atmel-samd/peripherals/clocks.h index 78b2e2e3d5..22c166d3e2 100644 --- a/ports/atmel-samd/peripherals/clocks.h +++ b/ports/atmel-samd/peripherals/clocks.h @@ -70,5 +70,6 @@ bool clock_get_parent(uint8_t type, uint8_t index, uint8_t *p_type, uint8_t *p_i uint32_t clock_get_frequency(uint8_t type, uint8_t index); uint32_t clock_get_calibration(uint8_t type, uint8_t index); int clock_set_calibration(uint8_t type, uint8_t index, uint32_t val); +void save_usb_clock_calibration(void); #endif // MICROPY_INCLUDED_ATMEL_SAMD_CLOCKS_H diff --git a/ports/atmel-samd/peripherals/samd21/clocks.c b/ports/atmel-samd/peripherals/samd21/clocks.c index aad251a84c..51b7ad993d 100644 --- a/ports/atmel-samd/peripherals/samd21/clocks.c +++ b/ports/atmel-samd/peripherals/samd21/clocks.c @@ -24,15 +24,24 @@ * THE SOFTWARE. */ +#include +#include + #include "peripherals/clocks.h" -#include "hpl_gclk_config.h" +#include "hal/include/hal_flash.h" #include "bindings/samd/Clock.h" #include "shared-bindings/microcontroller/__init__.h" #include "py/runtime.h" +#ifdef EXPRESS_BOARD +#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - NVMCTRL_ROW_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) +#else +#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x010000 - NVMCTRL_ROW_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) +#endif + bool gclk_enabled(uint8_t gclk) { common_hal_mcu_disable_interrupts(); // Explicitly do a byte write so the peripheral knows we're just wanting to read the channel @@ -102,17 +111,48 @@ static void init_clock_source_xosc32k(void) { while (!SYSCTRL->PCLKSR.bit.XOSC32KRDY) {} } -static void init_clock_source_dfll48m(void) { +static void init_clock_source_dfll48m_xosc(void) { + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {} + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(0x1f / 4) | + SYSCTRL_DFLLMUL_FSTEP(0xff / 4) | + SYSCTRL_DFLLMUL_MUL(48000000 / 32768); + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) >> FUSES_DFLL48M_COARSE_CAL_Pos; + if (coarse == 0x3f) { + coarse = 0x1f; + } + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | + SYSCTRL_DFLLVAL_FINE(512); + + SYSCTRL->DFLLCTRL.reg = 0; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {} + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_MODE | + SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {} + while (GCLK->STATUS.bit.SYNCBUSY) {} +} + +static void init_clock_source_dfll48m_usb(void) { SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {} SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) | SYSCTRL_DFLLMUL_MUL(48000); uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) >> FUSES_DFLL48M_COARSE_CAL_Pos; - if (coarse == 0x3f) + if (coarse == 0x3f) { coarse = 0x1f; + } + uint32_t fine = 512; + #ifdef CALIBRATE_CRYSTALLESS + // This is stored in an NVM page after the text and data storage but before + // the optional file system. The first 16 bytes are the identifier for the + // section. + if (strcmp((char*) INTERNAL_CIRCUITPY_CONFIG_START_ADDR, "CIRCUITPYTHON1") == 0) { + fine = ((uint16_t *) INTERNAL_CIRCUITPY_CONFIG_START_ADDR)[8]; + } + #endif SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | - SYSCTRL_DFLLVAL_FINE(512); + SYSCTRL_DFLLVAL_FINE(fine); SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM | SYSCTRL_DFLLCTRL_MODE | @@ -130,9 +170,16 @@ void clock_init(void) init_clock_source_osc32k(); } + if (board_has_crystal()) { + enable_clock_generator(3, GCLK_GENCTRL_SRC_XOSC32K_Val, 1); + connect_gclk_to_peripheral(3, GCLK_CLKCTRL_ID_DFLL48_Val); + init_clock_source_dfll48m_xosc(); + } else { + init_clock_source_dfll48m_usb(); + } + enable_clock_generator(0, GCLK_GENCTRL_SRC_DFLL48M_Val, 1); enable_clock_generator(1, GCLK_GENCTRL_SRC_DFLL48M_Val, 150); - init_clock_source_dfll48m(); if (board_has_crystal()) { enable_clock_generator(2, GCLK_GENCTRL_SRC_XOSC32K_Val, 32); } else { @@ -316,6 +363,41 @@ int clock_set_calibration(uint8_t type, uint8_t index, uint32_t val) { return -2; // calibration is read only } +void save_usb_clock_calibration(void) { + #ifndef CALIBRATE_CRYSTALLESS + return; + #endif + // If we are on USB lets double check our fine calibration for the clock and + // save the new value if its different enough. + SYSCTRL->DFLLSYNC.bit.READREQ = 1; + uint16_t saved_calibration = 0x1ff; + if (strcmp((char*) INTERNAL_CIRCUITPY_CONFIG_START_ADDR, "CIRCUITPYTHON1") == 0) { + saved_calibration = ((uint16_t *) INTERNAL_CIRCUITPY_CONFIG_START_ADDR)[8]; + } + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + // TODO(tannewt): Run the mass storage stuff if this takes a while. + } + int16_t current_calibration = SYSCTRL->DFLLVAL.bit.FINE; + if (abs(current_calibration - saved_calibration) > 10) { + // Copy the full internal config page to memory. + uint8_t page_buffer[NVMCTRL_ROW_SIZE]; + memcpy(page_buffer, (uint8_t*) INTERNAL_CIRCUITPY_CONFIG_START_ADDR, NVMCTRL_ROW_SIZE); + + // Modify it. + memcpy(page_buffer, "CIRCUITPYTHON1", 15); + // First 16 bytes (0-15) are ID. Little endian! + page_buffer[16] = current_calibration & 0xff; + page_buffer[17] = current_calibration >> 8; + + // Write it back. + // We don't use features that use any advanced NVMCTRL features so we can fake the descriptor + // whenever we need it instead of storing it long term. + struct flash_descriptor desc; + desc.dev.hw = NVMCTRL; + flash_write(&desc, (uint32_t) INTERNAL_CIRCUITPY_CONFIG_START_ADDR, page_buffer, NVMCTRL_ROW_SIZE); + } +} + #ifdef SAMD21_EXPOSE_ALL_CLOCKS CLOCK_SOURCE(XOSC); CLOCK_SOURCE(GCLKIN); diff --git a/ports/atmel-samd/peripherals/samd51/clocks.c b/ports/atmel-samd/peripherals/samd51/clocks.c index 29f9cb99cb..37468a3f7b 100644 --- a/ports/atmel-samd/peripherals/samd51/clocks.c +++ b/ports/atmel-samd/peripherals/samd51/clocks.c @@ -123,6 +123,9 @@ void clock_init(void) { enable_clock_generator_sync(5, GCLK_GENCTRL_SRC_DFLL_Val, 24, false); init_clock_source_dpll0(); + + // Do this after all static clock init so that they aren't used dynamically. + init_dynamic_clocks(); } static bool clk_enabled(uint8_t clk) { @@ -203,6 +206,7 @@ static uint32_t dpll_get_frequency(uint8_t index) { break; case 0x2: // XOSC0 case 0x3: // XOSC1 + default: return 0; // unknown } @@ -335,6 +339,10 @@ int clock_set_calibration(uint8_t type, uint8_t index, uint32_t val) { return -2; } + +void save_usb_clock_calibration(void) { +} + #include #include #include diff --git a/ports/atmel-samd/supervisor/filesystem.c b/ports/atmel-samd/supervisor/filesystem.c index 2ba29f279f..8a6010951f 100644 --- a/ports/atmel-samd/supervisor/filesystem.c +++ b/ports/atmel-samd/supervisor/filesystem.c @@ -32,13 +32,6 @@ #include "flash_api.h" -#ifdef EXPRESS_BOARD -// #include "common-hal/touchio/TouchIn.h" -#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x100 - CIRCUITPY_INTERNAL_NVM_SIZE) -#else -#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x010000 - 0x100 - CIRCUITPY_INTERNAL_NVM_SIZE) -#endif - fs_user_mount_t fs_user_mount_flash; mp_vfs_mount_t mp_vfs_mount_flash; diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index e815d0f1ec..3e8d5bcc76 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -62,6 +62,7 @@ #include "peripherals/dma.h" #include "shared-bindings/rtc/__init__.h" #include "tick.h" +#include "usb.h" #ifdef CIRCUITPY_GAMEPAD_TICKS #include "shared-module/gamepad/__init__.h" @@ -184,7 +185,6 @@ safe_mode_t port_init(void) { _pm_init(); #endif clock_init(); - init_dynamic_clocks(); board_init(); @@ -270,16 +270,6 @@ void reset_port(void) { reset_all_pins(); - // Set up debugging pins after reset_all_pins(). - - // Uncomment to init PIN_PA17 for debugging. - // struct port_config pin_conf; - // port_get_config_defaults(&pin_conf); - // - // pin_conf.direction = PORT_PIN_DIR_OUTPUT; - // port_pin_set_config(MICROPY_HW_LED1, &pin_conf); - // port_pin_set_output_level(MICROPY_HW_LED1, false); - // Output clocks for debugging. // not supported by SAMD51G; uncomment for SAMD51J or update for 51G // #ifdef SAMD51 @@ -291,52 +281,9 @@ void reset_port(void) { usb_hid_reset(); -// #ifdef CALIBRATE_CRYSTALLESS -// // If we are on USB lets double check our fine calibration for the clock and -// // save the new value if its different enough. -// if (mp_msc_enabled) { -// SYSCTRL->DFLLSYNC.bit.READREQ = 1; -// uint16_t saved_calibration = 0x1ff; -// if (strcmp((char*) INTERNAL_CIRCUITPY_CONFIG_START_ADDR, "CIRCUITPYTHON1") == 0) { -// saved_calibration = ((uint16_t *) INTERNAL_CIRCUITPY_CONFIG_START_ADDR)[8]; -// } -// while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { -// // TODO(tannewt): Run the mass storage stuff if this takes a while. -// } -// int16_t current_calibration = SYSCTRL->DFLLVAL.bit.FINE; -// if (abs(current_calibration - saved_calibration) > 10) { -// enum status_code error_code; -// uint8_t page_buffer[NVMCTRL_ROW_SIZE]; -// for (int i = 0; i < NVMCTRL_ROW_PAGES; i++) { -// do -// { -// error_code = nvm_read_buffer(INTERNAL_CIRCUITPY_CONFIG_START_ADDR + i * NVMCTRL_PAGE_SIZE, -// page_buffer + i * NVMCTRL_PAGE_SIZE, -// NVMCTRL_PAGE_SIZE); -// } while (error_code == STATUS_BUSY); -// } -// // If this is the first write, include the header. -// if (strcmp((char*) page_buffer, "CIRCUITPYTHON1") != 0) { -// memcpy(page_buffer, "CIRCUITPYTHON1", 15); -// } -// // First 16 bytes (0-15) are ID. Little endian! -// page_buffer[16] = current_calibration & 0xff; -// page_buffer[17] = current_calibration >> 8; -// do -// { -// error_code = nvm_erase_row(INTERNAL_CIRCUITPY_CONFIG_START_ADDR); -// } while (error_code == STATUS_BUSY); -// for (int i = 0; i < NVMCTRL_ROW_PAGES; i++) { -// do -// { -// error_code = nvm_write_buffer(INTERNAL_CIRCUITPY_CONFIG_START_ADDR + i * NVMCTRL_PAGE_SIZE, -// page_buffer + i * NVMCTRL_PAGE_SIZE, -// NVMCTRL_PAGE_SIZE); -// } while (error_code == STATUS_BUSY); -// } -// } -// } -// #endif + if (usb_connected()) { + save_usb_clock_calibration(); + } } /** From d0e6bb269fa425995a31083f02753203afccd253 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Sat, 2 Jun 2018 12:55:07 -0400 Subject: [PATCH 093/103] Use the system errno's on nrf --- ports/nrf/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 442e98de53..e379469c08 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -82,7 +82,7 @@ #define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) -#define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_USE_INTERNAL_ERRNO (0) #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (0) From e37d5622fe6f21ef0fc757a4b3298f485ef7e443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Sat, 2 Jun 2018 18:39:15 +0200 Subject: [PATCH 094/103] tools: Add cpboard.py This is a variant of pyboard.py aimed at Circuit Python boards. It can emulate pyboard.py enough so it works with tests/run-tests. --- tools/cpboard.py | 684 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 684 insertions(+) create mode 100644 tools/cpboard.py diff --git a/tools/cpboard.py b/tools/cpboard.py new file mode 100644 index 0000000000..0b9c43c614 --- /dev/null +++ b/tools/cpboard.py @@ -0,0 +1,684 @@ +#!/usr/bin/env python3 +# +# 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) 2018 Noralf Trønnes +# +# Parts taken from pyboard.py: +# Copyright (c) 2014-2016 Damien P. George +# Copyright (c) 2017 Paul Sokolovsky +# +# 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. + +import os +import re +import serial +import sys +import time + +import serial.tools.list_ports +import sh +import shutil + +class CPboardError(BaseException): + pass + +# supervisor/messages/default.h: +MSG_NEWLINE = b"\r\n" +MSG_SAFE_MODE_CRASH = b"Looks like our core CircuitPython code crashed hard. Whoops!" +MSG_SAFE_MODE_BROWN_OUT_LINE_1 = b"The microcontroller's power dipped. Please make sure your power supply provides" +MSG_SAFE_MODE_BROWN_OUT_LINE_2 = b"enough power for the whole circuit and press reset (after ejecting CIRCUITPY)." +MSG_WAIT_BEFORE_REPL = b"Press any key to enter the REPL. Use CTRL-D to reload." + +class REPL: + CHAR_CTRL_A = b'\x01' + CHAR_CTRL_B = b'\x02' + CHAR_CTRL_C = b'\x03' + CHAR_CTRL_D = b'\x04' + + def __init__(self, board): + self.board = board + self.write_chunk_size = 32 + self.safe_mode = False + self.session = b'' + + def __enter__(self): + self.reset() + return self + + def __exit__(self, exception_type, exception_value, traceback): + pass + + @property + def serial(self): + return self.board.serial + + def read(self): + if self.serial.inWaiting(): + data = self.serial.read(self.serial.inWaiting()) + else: + data = b'' + self.session += data + return data + + def read_until(self, ending, timeout=10): + data = b'' + timeout_count = 0 + while True: + if data.endswith(ending): + break + elif self.serial.inWaiting() > 0: + new_data = self.serial.read(1) + data += new_data + self.session += new_data + timeout_count = 0 + else: + timeout_count += 1 + if timeout is not None and timeout_count >= 100 * timeout: + raise TimeoutError(110, "timeout waiting for", ending) + time.sleep(0.01) + return data + + def write(self, data, chunk_size=None): + if chunk_size is None: + chunk_size = self.write_chunk_size + if not isinstance(data, bytes): + data = bytes(data, encoding='utf8') + + for i in range(0, len(data), chunk_size): + chunk = data[i:min(i + chunk_size, len(data))] + self.session += chunk + self.serial.write(chunk) + time.sleep(0.01) + + def reset(self): + # Use read() since serial.reset_input_buffer() fails with termios.error now and then + self.read() + self.session = b'' + self.write(b'\r' + REPL.CHAR_CTRL_C + REPL.CHAR_CTRL_C) # interrupt any running program + self.write(b'\r' + REPL.CHAR_CTRL_B) # enter or reset friendly repl + data = self.read_until(b'>>> ') + + def execute(self, code, timeout=10, async=False): + self.read() # Throw away + + self.write(REPL.CHAR_CTRL_A) + self.read_until(b'\r\n>') + + self.write(code) + + self.write(REPL.CHAR_CTRL_D) + if async: + return b'', b'' + self.read_until(b'OK') + + output = self.read_until(b'\x04', timeout=timeout) + output = output[:-1] + + error = self.read_until(b'\x04') + error = error[:-1] + + return output, error + + def run(self): + if self.safe_mode: + raise RuntimeError("Can't run in safe mode") + + self.reset() + + self.write(REPL.CHAR_CTRL_D) + data = self.read_until(b' output:\r\n') + if b'Running in safe mode' in data: + self.safe_mode = True + raise RuntimeError("Can't run in safe mode") + + # TODO: MSG_SAFE_MODE_CRASH + # TODO: BROWNOUT + + marker = MSG_NEWLINE + MSG_WAIT_BEFORE_REPL + MSG_NEWLINE + data = self.read_until(marker) + data = data.split(marker)[0] + + # Haven't found out why we have to strip off this... + if data.endswith(b'\r\n\r\n'): + data = data[:-4] + return data + + +class Disk: + def __init__(self, dev): + self.dev = os.path.realpath(dev) + self.mountpoint = None + with open('/etc/mtab', 'r') as f: + mtab = f.read() + mount = [mount.split(' ') for mount in mtab.splitlines() if mount.startswith(self.dev)] + if mount: + self._path = mount[0][1] + else: + name = os.path.basename(dev) + sh.pmount("-tvfat", dev, name, _timeout=10) + self.mountpoint = "/media/" + name + self._path = self.mountpoint + + def __enter__(self): + return self + + def __exit__(self, exception_type, exception_value, traceback): + try: + self.close() + except: + pass + + @property + def path(self): + return self._path + + def close(self): + if not self.mountpoint: + return + mountpoint = self.mountpoint + self.mountpoint = None + + start_time = time.monotonic() + unmounted = False + while not unmounted and start_time - time.monotonic() < 30: + try: + sh.pumount(mountpoint) + unmounted = True + except sh.ErrorReturnCode_5: + time.sleep(0.1) + + def sync(self): + disk_device = os.path.basename(self.dev)[:-1] + os.sync() + # Monitor the block device so we know when the sync request is actually finished. + with open("/sys/block/" + disk_device + "/stat", "r") as f: + disk_inflight = 1 + last_wait_time = 0 + wait_time = 1 + while disk_inflight > 0 or wait_time > last_wait_time: + f.seek(0) + stats = f.read() + block_stats = stats.split() + disk_inflight = int(block_stats[8]) + last_wait_time = wait_time + wait_time = int(block_stats[9]) + + def copy(self, src, dst=None, sync=True): + if dst is None: + dst = os.path.basename(src) + shutil.copy(src, os.path.join(self.path, dst)) + if sync: + self.sync() + + +class Firmware: + def __init__(self, board): + self.board = board + + @property + def disk(self): + disks = self.board.get_disks() + if len(disks) != 1: + raise RuntimeError("Boot disk not found for: " + self.board.device) + return Disk(disks[0]) + + @property + def info(self): + with self.disk as disk: + fname = os.path.join(disk.path, 'INFO_UF2.TXT') + with open(fname, 'r') as f: + info = f.read() + lines = info.splitlines() + res = {} + res['header'] = lines[0] + for line in lines[1:]: + k, _, v = line.partition(':') + res[k.replace(':', '')] = v.strip() + return res + + def upload(self, fw): + with open(fw, 'rb') as f: + header = f.read(32) + if header[0:4] != b'UF2\n': + raise ValueError('Only UF2 files are supported') + self.board.close() + with self.disk as disk: + disk.copy(fw, sync=False) + + +class CPboard: + @classmethod + def from_try_all(cls, name, **kwargs): + try: + return CPboard.from_build_name(name, **kwargs) + except ValueError: + pass + + vendor, _, product = name.partition(':') + if vendor and product: + return CPboard.from_usb(**kwargs, idVendor=int(vendor, 16), idProduct=int(product, 16)) + + return CPboard(name, **kwargs) + + @classmethod + def from_build_name(cls, name, **kwargs): + boards = { + #'arduino_zero' + 'circuitplayground_express' : (0x239a, 0x8019), + #'feather_m0_adalogger' : (0x239a, ), + #'feather_m0_basic' : (0x239a, ), + 'feather_m0_express' : (0x239a, 0x8023), + #'feather_m0_rfm69' : (0x239a, ), + #'feather_m0_rfm9x' : (0x239a, ), + #'feather_m0_supersized' : (0x239a, ), + #'feather_m4_express' : (0x239a, ), + #'gemma_m0' : (0x239a, ), + #'itsybitsy_m0_express' : (0x239a, ), + #'itsybitsy_m4_express' : (0x239a, ), + 'metro_m0_express' : (0x239a, 0x8014), + 'metro_m4_express' : (0x239a, 0x8021), + #'metro_m4_express_revb' : (0x239a, ), + #'pirkey_m0' : (0x239a, ), + #'trinket_m0' : (0x239a, ), + #'trinket_m0_haxpress' : (0x239a, ), + #'ugame10' + } + + try: + vendor, product = boards[name] + except KeyError: + raise ValueError("Unknown build name: " + name) + + return CPboard.from_usb(**kwargs, idVendor=vendor, idProduct=product) + + @classmethod + def from_build_name_bootloader(cls, name, **kwargs): + boards = { + #'arduino_zero' + #'circuitplayground_express' : (0x239a, ), + #'feather_m0_adalogger' : (0x239a, ), + #'feather_m0_basic' : (0x239a, ), + 'feather_m0_express' : (0x239a, 0x001b), + #'feather_m0_rfm69' : (0x239a, ), + #'feather_m0_rfm9x' : (0x239a, ), + #'feather_m0_supersized' : (0x239a, ), + #'feather_m4_express' : (0x239a, ), + #'gemma_m0' : (0x239a, ), + #'itsybitsy_m0_express' : (0x239a, ), + #'itsybitsy_m4_express' : (0x239a, ), + #'metro_m0_express' : (0x239a, 0x8014), + 'metro_m4_express' : (0x239a, 0x0021), + #'metro_m4_express_revb' : (0x239a, ), + #'pirkey_m0' : (0x239a, ), + #'trinket_m0' : (0x239a, ), + #'trinket_m0_haxpress' : (0x239a, ), + #'ugame10' + } + + try: + vendor, product = boards[name] + except KeyError: + raise ValueError("Unknown build name: " + name) + + board = CPboard.from_usb(**kwargs, idVendor=vendor, idProduct=product) + board.bootloader = True + return board + + @classmethod + def from_usb(cls, baudrate=115200, wait=0, timeout=10, **kwargs): + import usb.core + dev = usb.core.find(**kwargs) + if not dev: + s = "Can't find USB device: " + args = [] + for x in kwargs.items(): + try: + args.append('%s=0x%x' % x) + except: + args.append('%s = %s' % x) + raise RuntimeError("Can't find USB device: " + ', '.join(args)) + return cls(dev, baudrate=baudrate, wait=wait, timeout=timeout) + + def __init__(self, device, baudrate=115200, wait=0, timeout=10): + self.device = device + self.usb_dev = None + try: + # Is it a usb.core.Device? + portstr = ':' + '.'.join(map(str, device.port_numbers)) + ':' + except: + pass + else: + serials = [serial for serial in os.listdir("/dev/serial/by-path") if portstr in serial] + if len(serials) != 1: + raise RuntimeError("Can't find excatly one matching usb serial device") + self.device = os.path.realpath("/dev/serial/by-path/" + serials[0]) + self.usb_dev = device + + self.baudrate = baudrate + self.wait = wait + self.timeout = timeout + self.debug = True + self.mount = None + self.serial = None + self.bootloader = False + self.repl = REPL(self) + + def __enter__(self): + self.open() + return self + + def __exit__(self, exception_type, exception_value, traceback): + self.close() + + def open(self, baudrate=None, wait=None): + if self.serial: + return + if baudrate is None: + baudrate = self.baudrate + if wait is None: + wait = self.wait + + delayed = False + for attempt in range(wait + 1): + try: + self.serial = serial.Serial(self.device, baudrate=self.baudrate, timeout=self.timeout, write_timeout=self.timeout, interCharTimeout=1) + break + except (OSError, IOError): # Py2 and Py3 have different errors + if wait == 0: + continue + if attempt == 0: + sys.stdout.write('Waiting {} seconds for board '.format(wait)) + delayed = True + time.sleep(1) + sys.stdout.write('.') + sys.stdout.flush() + else: + if delayed: + print('') + raise CPboardError('failed to access ' + self.device) + if delayed: + print('') + + def close(self): + if self.serial: + self.serial.close() + self.serial = None + + def exec(self, command, timeout=10, async=False): + with self.repl as repl: + try: + output, error = repl.execute(command, timeout=timeout, async=async) + except OSError as e: + if self.debug: + print('exec: session: ', self.repl.session) + raise CPboardError('timeout', e) + if error: + raise CPboardError('exception', output, error) + return output + + def eval(self, expression, timeout=10): + command = 'print({})'.format(expression) + with self.repl as repl: + output, error = repl.execute(command, timeout=timeout) + if error: + raise CPboardError('exception', output, error) + try: + res = eval(str(output, encoding='utf8')) + except: + raise CPboardError('failed to eval: %s' % output) + return res + + def _reset(self, mode='NORMAL'): + self.exec("import microcontroller;microcontroller.on_next_reset(microcontroller.RunMode.%s)" % mode) + try: + self.exec("import microcontroller;microcontroller.reset()", async=True) + except OSError: + pass + + def reset(self, safe_mode=False, delay=5, wait=10): + self._reset('SAFE_MODE' if safe_mode else 'NORMAL') + self.close() + time.sleep(delay) + self.open(wait) + time.sleep(delay) + + def reset_to_bootloader(self, repl=False): + if repl: + self._reset('BOOTLOADER') + self.close() + else: + self.close() + s = serial.Serial(self.device, 1200, write_timeout=4, timeout=4) + s.close() + + def get_port_info(self): + portinfo = None + for port_iter in serial.tools.list_ports.comports(): + if port_iter.device == self.device: + portinfo = port_iter + break + return portinfo + + @property + def serial_number(self): + try: # Permissions are needed to read the value + return self.usb_dev.serial_number + except: + pass + p = self.get_port_info() + return p.serial_number if p else None + + def get_disks(self): + if self.usb_dev: + portstr = ':' + '.'.join(map(str, self.usb_dev.port_numbers)) + ':' + return ["/dev/disk/by-path/" + disk for disk in os.listdir("/dev/disk/by-path") if portstr in disk] + serial = self.serial_number + if not serial: + raise RuntimeError("Serial number not found for: " + self.device) + return ["/dev/disk/by-id/" + disk for disk in os.listdir("/dev/disk/by-id") if serial in disk] + + @property + def disk(self): + disks = self.get_disks() + + part = [part for part in disks if 'part1' in part] + if not part: + raise RuntimeError("Disk not found for: " + self.device) + + return Disk(part[0]) + + @property + def firmware(self): + return Firmware(self) + + def execfile_disk(self, filename): + with self.disk as disk: + disk.copy(filename, 'code.py') + + with self.repl as repl: + try: + output = repl.run() + except OSError as e: + raise CPboardError('timeout', e) + except RuntimeError: + if self.repl.safe_mode: + raise PyboardError("Can't run in safe mode") + else: + raise + + return output + + def execfile(self, filename, timeout=10): + if os.environ.get('CPBOARD_EXEC_MODE') == 'disk': + return self.execfile_disk(filename) + else: + with open(filename, 'rb') as f: + pyfile = f.read() + return self.exec(pyfile, timeout=timeout) + + +# Implement just enough to make tests/run-tests work +PyboardError = CPboardError + +class Pyboard: + def __init__(self, device, baudrate=115200, user='micro', password='python', wait=0): + self.board = CPboard.from_try_all(device, baudrate=baudrate, wait=wait) + with self.board.disk as disk: + disk.copy('skip_if.py') + + def close(self): + self.board.close() + + def enter_raw_repl(self): + self.board.open() + + def execfile(self, filename): + return self.board.execfile(filename) + + +def eval_namedtuple(board, command): + from collections import namedtuple + s = board.exec("print(%s)" % command) + s = s.decode().strip() + items = [key.split('=') for key in s[1:-1].split(', ')] + keys = [item[0] for item in items] + vals = [item[1] for item in items] + nt = namedtuple('eval', keys) + res = nt(*[eval(val) for val in vals]) + return res + + +def os_uname(board): + return eval_namedtuple(board, "__import__('os').uname()") + +def print_verbose(cargs, *args, **kwargs): + if cargs.verbose: + print(*args, flush=True, **kwargs) + +def upload(args): + try: + board = CPboard.from_build_name_bootloader(args.board) + print_verbose(args, 'Board is already in the bootloader') + except (ValueError, RuntimeError): + board = CPboard.from_try_all(args.board) + + print_verbose(args, "Serial number :", board.serial_number) + + if not (args.quiet or board.bootloader): + board.open() + print('Current version:', os_uname(board).version, flush=True) + + if not board.bootloader: + print_verbose(args, 'Reset to bootloader...', end='') + board.reset_to_bootloader(repl=True) # Feather M0 Express doesn't respond to 1200 baud + time.sleep(5) + print_verbose(args, 'done') + + print_verbose(args, 'Bootloader:', board.firmware.info) + + print_verbose(args, 'Upload firmware...', end='') + board.firmware.upload(args.firmware) + print_verbose(args, 'done') + + print_verbose(args, 'Wait for board...', end='') + time.sleep(5) + print_verbose(args, 'done') + + if not args.quiet: + if board.bootloader: + board = CPboard.from_try_all(args.board) + board.open(wait=10) + print('New version:', os_uname(board).version, flush=True) + +def print_error_exit(args, e): + if args.debug: + return False + if not args.quiet: + print(e, file=sys.stderr) + sys.exit(1) + +def main(): + import argparse + cmd_parser = argparse.ArgumentParser(description='Circuit Python Board Tool') + cmd_parser.add_argument('board', help='build_name, vid:pid or /dev/tty') + cmd_parser.add_argument('-f', '--firmware', help='upload UF2 firmware file') + cmd_parser.add_argument('-c', '--command', help='program passed in as string') + cmd_parser.add_argument('--tty', action='store_true', help='print tty') + cmd_parser.add_argument('--verbose', '-v', action='count', default=0, help='be verbose') + cmd_parser.add_argument('-q', '--quiet', action='store_true', help='be quiet') + cmd_parser.add_argument('--debug', action='store_true', help='raise exceptions') + args = cmd_parser.parse_args() + + if args.quiet: + args.verbose = 0 + args.debug = False + + if args.firmware: + try: + upload(args) + except BaseException as e: + if not print_error_exit(args, e): + raise + sys.exit(0) + + try: + board = CPboard.from_try_all(args.board) + except BaseException as e: + if not print_error_exit(args, e): + raise + + if args.verbose: + exec_mode = os.environ.get('CPBOARD_EXEC_MODE') + if exec_mode: + print('CPBOARD_EXEC_MODE =', exec_mode) + + # Make sure we can open serial + try: + with board: + pass + except BaseException as e: + if not print_error_exit(args, e): + raise + + if args.tty: + print(board.device) + elif args.command: + with board as b: + print(b.eval(args.command)) + else: + with board as b: + print('Device: ', end='') + if b.usb_dev: + print('%04x:%04x on ' % (b.usb_dev.idVendor, b.usb_dev.idProduct), end='') + print(b.device) + print('Serial number:', b.serial_number) + uname = os_uname(b) + print('os.uname:') + print(' sysname:', uname.sysname) + print(' nodename:', uname.nodename) + print(' release:', uname.release) + print(' version:', uname.version) + print(' machine:', uname.machine) + +if __name__ == "__main__": + main() From d08cc2ab171c062246fa44a21f54a577552ac647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= Date: Sat, 2 Jun 2018 18:39:04 +0200 Subject: [PATCH 095/103] tests: Point pyboard.py to tools/cpboard.py This makes it possible to run-tests on the board itself. --- tests/pyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pyboard.py b/tests/pyboard.py index 3a82f6a6a3..616773a313 120000 --- a/tests/pyboard.py +++ b/tests/pyboard.py @@ -1 +1 @@ -../tools/pyboard.py \ No newline at end of file +../tools/cpboard.py \ No newline at end of file From 6e6bfc4d57df59eed11f64f04024f10c1b2799e1 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 2 Jun 2018 17:00:23 -0400 Subject: [PATCH 096/103] Update frozen LIS3DH to 4.2.1 to save space --- frozen/Adafruit_CircuitPython_LIS3DH | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozen/Adafruit_CircuitPython_LIS3DH b/frozen/Adafruit_CircuitPython_LIS3DH index bdd1478330..c4152a0d87 160000 --- a/frozen/Adafruit_CircuitPython_LIS3DH +++ b/frozen/Adafruit_CircuitPython_LIS3DH @@ -1 +1 @@ -Subproject commit bdd1478330b41017b09de58fca213218c36f2821 +Subproject commit c4152a0d87a04903ae0e612eb381af440c9e28b3 From 27eba65dc77fe0a1d751881a3bb94ef8489f8d57 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 2 Jun 2018 17:43:08 -0400 Subject: [PATCH 097/103] update HID, and update seesaw version --- frozen/Adafruit_CircuitPython_HID | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozen/Adafruit_CircuitPython_HID b/frozen/Adafruit_CircuitPython_HID index 058c94513e..1da880cee3 160000 --- a/frozen/Adafruit_CircuitPython_HID +++ b/frozen/Adafruit_CircuitPython_HID @@ -1 +1 @@ -Subproject commit 058c94513e41249eac72deb3abc7af4f7d96a4f6 +Subproject commit 1da880cee3c2d64e2e7e4ca5082f3a74c4a37a5a From 7e7e33e460aba17c932e90ef3f4d93ed188bfc67 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 5 Jun 2018 08:15:34 -0400 Subject: [PATCH 098/103] add PA14 as D4 to Feather M4 Exprses --- ports/atmel-samd/boards/feather_m4_express/pins.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/atmel-samd/boards/feather_m4_express/pins.c b/ports/atmel-samd/boards/feather_m4_express/pins.c index 5767f63fad..b9b04e0acf 100644 --- a/ports/atmel-samd/boards/feather_m4_express/pins.c +++ b/ports/atmel-samd/boards/feather_m4_express/pins.c @@ -18,6 +18,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PB16) }, { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PA12) }, { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PA13) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_PA14) }, { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PA16) }, { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PA18) }, { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PA19) }, From 9ba6990228438aad958934ea6f996debfb21334b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 5 Jun 2018 18:21:47 -0400 Subject: [PATCH 099/103] Feather M0 Express build with Crickit libraries --- .../boards/feather_m0_express_crickit/board.c | 38 ++++++++++++ .../mpconfigboard.h | 60 +++++++++++++++++++ .../mpconfigboard.mk | 17 ++++++ .../boards/feather_m0_express_crickit/pins.c | 33 ++++++++++ tools/build_adafruit_bins.sh | 2 +- 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 ports/atmel-samd/boards/feather_m0_express_crickit/board.c create mode 100644 ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.h create mode 100644 ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk create mode 100644 ports/atmel-samd/boards/feather_m0_express_crickit/pins.c diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/board.c b/ports/atmel-samd/boards/feather_m0_express_crickit/board.c new file mode 100644 index 0000000000..c8e20206a1 --- /dev/null +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/board.c @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 "boards/board.h" + +void board_init(void) +{ +} + +bool board_requests_safe_mode(void) { + return false; +} + +void reset_board(void) { +} diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.h new file mode 100644 index 0000000000..7564620136 --- /dev/null +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.h @@ -0,0 +1,60 @@ +#define MICROPY_HW_BOARD_NAME "Adafruit Feather M0 Express with Crickit libraries" +#define MICROPY_HW_MCU_NAME "samd21g18" + +#define MICROPY_HW_NEOPIXEL (&pin_PA06) + +// Clock rates are off: Salae reads 12MHz which is the limit even though we set it to the safer 8MHz. +#define SPI_FLASH_BAUDRATE (8000000) + +#define SPI_FLASH_MOSI_PIN PIN_PA08 +#define SPI_FLASH_MISO_PIN PIN_PA14 +#define SPI_FLASH_SCK_PIN PIN_PA09 +#define SPI_FLASH_CS_PIN PIN_PA13 +#define SPI_FLASH_MOSI_PIN_FUNCTION PINMUX_PA08D_SERCOM2_PAD0 +#define SPI_FLASH_MISO_PIN_FUNCTION PINMUX_PA14C_SERCOM2_PAD2 +#define SPI_FLASH_SCK_PIN_FUNCTION PINMUX_PA09D_SERCOM2_PAD1 +#define SPI_FLASH_SERCOM SERCOM2 +#define SPI_FLASH_SERCOM_INDEX 2 +#define SPI_FLASH_MOSI_PAD 0 +#define SPI_FLASH_MISO_PAD 2 +#define SPI_FLASH_SCK_PAD 1 +// Transmit Data Pinout +// <0x0=>PAD[0,1]_DO_SCK +// <0x1=>PAD[2,3]_DO_SCK +// <0x2=>PAD[3,1]_DO_SCK +// <0x3=>PAD[0,3]_DO_SCK +#define SPI_FLASH_DOPO 0 +#define SPI_FLASH_DIPO 2 // same as MISO pad + +// These are pins not to reset. +#define MICROPY_PORT_A (PORT_PA06 | PORT_PA08 | PORT_PA09 | PORT_PA13 | PORT_PA14 | PORT_PA24 | PORT_PA25) +#define MICROPY_PORT_B ( 0 ) +#define MICROPY_PORT_C ( 0 ) + +#include "external_flash/external_flash.h" + +// If you change this, then make sure to update the linker scripts as well to +// make sure you don't overwrite code. +#define CIRCUITPY_INTERNAL_NVM_SIZE 256 + +#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) + +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ + GD25Q16C + +#include "external_flash/external_flash.h" + +#define BOARD_HAS_CRYSTAL 1 + +#define DEFAULT_I2C_BUS_SCL (&pin_PA23) +#define DEFAULT_I2C_BUS_SDA (&pin_PA22) + +#define DEFAULT_SPI_BUS_SCK (&pin_PB11) +#define DEFAULT_SPI_BUS_MOSI (&pin_PB10) +#define DEFAULT_SPI_BUS_MISO (&pin_PA12) + +#define DEFAULT_UART_BUS_RX (&pin_PA11) +#define DEFAULT_UART_BUS_TX (&pin_PA10) diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk new file mode 100644 index 0000000000..4096560c22 --- /dev/null +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk @@ -0,0 +1,17 @@ +LD_FILE = boards/samd21x18-bootloader-external-flash.ld +USB_VID = 0x239A +USB_PID = 0x8023 +USB_PRODUCT = "Feather M0 Express" +USB_MANUFACTURER = "Adafruit Industries LLC" + +SPI_FLASH_FILESYSTEM = 1 +LONGINT_IMPL = MPZ + +CHIP_VARIANT = SAMD21G18A +CHIP_FAMILY = samd21 + +# Include these Python libraries in firmware. +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_seesaw diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/pins.c b/ports/atmel-samd/boards/feather_m0_express_crickit/pins.c new file mode 100644 index 0000000000..68666c273e --- /dev/null +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/pins.c @@ -0,0 +1,33 @@ +#include "shared-bindings/board/__init__.h" + +#include "board_busses.h" + +STATIC const mp_rom_map_elem_t board_global_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PB08) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_PB09) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_PA04) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_PB02) }, + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PB11) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) }, + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA11) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PA11) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_PA10) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PA10) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PA22) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PA23) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PA15) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PA20) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PA07) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_PA18) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_PA16) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) }, + { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA06) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/tools/build_adafruit_bins.sh b/tools/build_adafruit_bins.sh index 4d0854a109..6ad0858272 100755 --- a/tools/build_adafruit_bins.sh +++ b/tools/build_adafruit_bins.sh @@ -2,7 +2,7 @@ rm -rf ports/atmel-samd/build* rm -rf ports/esp8266/build* rm -rf ports/nrf/build* -ATMEL_BOARDS="arduino_zero circuitplayground_express circuitplayground_express_crickit feather_m0_basic feather_m0_adalogger itsybitsy_m0_express itsybitsy_m4_express feather_m0_rfm69 feather_m0_rfm9x feather_m0_express feather_m4_express metro_m0_express metro_m4_express pirkey_m0 trinket_m0 gemma_m0 feather52" +ATMEL_BOARDS="arduino_zero circuitplayground_express circuitplayground_express_crickit feather_m0_basic feather_m0_adalogger itsybitsy_m0_express itsybitsy_m4_express feather_m0_rfm69 feather_m0_rfm9x feather_m0_express feather_m0_express_crickit feather_m4_express metro_m0_express metro_m4_express pirkey_m0 trinket_m0 gemma_m0 feather52" ROSIE_SETUPS="rosie-ci" PARALLEL="-j 5" From 36a7ebe88e03bafa7e8163607a93135580249fff Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 5 Jun 2018 18:24:55 -0400 Subject: [PATCH 100/103] add feather_m0_express_crickit to .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c7a45ea805..986e32cc43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ env: - TRAVIS_BOARD=feather_m0_rfm69 - TRAVIS_BOARD=feather_m0_rfm9x - TRAVIS_BOARD=feather_m0_express + - TRAVIS_BOARD=feather_m0_express_crickit - TRAVIS_BOARD=feather_m4_express - TRAVIS_BOARD=itsybitsy_m0_express - TRAVIS_BOARD=itsybitsy_m4_express From a4e06bd998a1fb2d2dd209524d8ccd69b18712e6 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 5 Jun 2018 22:04:30 -0500 Subject: [PATCH 101/103] main: Explicitly set the stack top based on _estack .. setting it based on the ad-hoc stack pointer calculation of mp_stack_ctrl_init() meant that the stack used above main() counts against the 1KiB safety factor that the mp_stack_set_limit call tries to establish. It turns out, at least on M4, that over half of the safety factor is used up by stack-above-main()! In the case of the basics/gen_stack_overflow.py test, which blows the stack on purpose, it turns out that gc would be called while handling the "maximum recursion depth exceeded" error, and this needed more stack than was left. Closes: #900 --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 4402ebe328..4ed4f5d7cb 100644 --- a/main.c +++ b/main.c @@ -241,7 +241,7 @@ int __attribute__((used)) main(void) { // Stack limit should be less than real stack size, so we have a chance // to recover from limit hit. (Limit is measured in bytes.) - mp_stack_ctrl_init(); + mp_stack_set_top((char*)&_estack); mp_stack_set_limit((char*)&_estack - (char*)&_ebss - 1024); #if MICROPY_MAX_STACK_USAGE From 654591e11f45075b0a7534fd52c98fef6082c703 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 6 Jun 2018 08:01:09 -0500 Subject: [PATCH 102/103] main: move code pertaining to boot.py out of line .. this reduces stack usage in main() substantially, because the 512 byte stack allocation will only exist during the new run_boot_py function's duration. Closes: #904 --- main.c | 72 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/main.c b/main.c index 4ed4f5d7cb..fb1a9e90fb 100644 --- a/main.c +++ b/main.c @@ -233,40 +233,7 @@ bool start_mp(safe_mode_t safe_mode) { } } -int __attribute__((used)) main(void) { - // initialise the cpu and peripherals - safe_mode_t safe_mode = port_init(); - - rgb_led_status_init(); - - // Stack limit should be less than real stack size, so we have a chance - // to recover from limit hit. (Limit is measured in bytes.) - mp_stack_set_top((char*)&_estack); - mp_stack_set_limit((char*)&_estack - (char*)&_ebss - 1024); - -#if MICROPY_MAX_STACK_USAGE - // _ezero (same as _ebss) is an int, so start 4 bytes above it. - mp_stack_set_bottom(&_ezero + 1); - mp_stack_fill_with_sentinel(); -#endif - - // Create a new filesystem only if we're not in a safe mode. - // A power brownout here could make it appear as if there's - // no SPI flash filesystem, and we might erase the existing one. - filesystem_init(safe_mode == NO_SAFE_MODE, false); - - // Reset everything and prep MicroPython to run boot.py. - reset_port(); - reset_board(); - reset_mp(); - - // Turn on autoreload by default but before boot.py in case it wants to change it. - autoreload_enable(); - - // By default our internal flash is readonly to local python code and - // writable over USB. Set it here so that boot.py can change it. - filesystem_writable_by_python(false); - +void run_boot_py(safe_mode_t safe_mode) { // If not in safe mode, run boot before initing USB and capture output in a // file. if (filesystem_present() && safe_mode == NO_SAFE_MODE && MP_STATE_VM(vfs_mount_table) != NULL) { @@ -338,6 +305,43 @@ int __attribute__((used)) main(void) { reset_port(); reset_mp(); } +} + +int __attribute__((used)) main(void) { + // initialise the cpu and peripherals + safe_mode_t safe_mode = port_init(); + + rgb_led_status_init(); + + // Stack limit should be less than real stack size, so we have a chance + // to recover from limit hit. (Limit is measured in bytes.) + mp_stack_set_top((char*)&_estack); + mp_stack_set_limit((char*)&_estack - (char*)&_ebss - 1024); + +#if MICROPY_MAX_STACK_USAGE + // _ezero (same as _ebss) is an int, so start 4 bytes above it. + mp_stack_set_bottom(&_ezero + 1); + mp_stack_fill_with_sentinel(); +#endif + + // Create a new filesystem only if we're not in a safe mode. + // A power brownout here could make it appear as if there's + // no SPI flash filesystem, and we might erase the existing one. + filesystem_init(safe_mode == NO_SAFE_MODE, false); + + // Reset everything and prep MicroPython to run boot.py. + reset_port(); + reset_board(); + reset_mp(); + + // Turn on autoreload by default but before boot.py in case it wants to change it. + autoreload_enable(); + + // By default our internal flash is readonly to local python code and + // writable over USB. Set it here so that boot.py can change it. + filesystem_writable_by_python(false); + + run_boot_py(safe_mode); // Start serial and HID after giving boot.py a chance to tweak behavior. serial_init(); From 3a3817262788a6b6812564815c46fdf1bd59667b Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 6 Jun 2018 18:28:01 -0400 Subject: [PATCH 103/103] Fixed documentation formatting for RTD --- shared-bindings/rotaryio/IncrementalEncoder.c | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/shared-bindings/rotaryio/IncrementalEncoder.c b/shared-bindings/rotaryio/IncrementalEncoder.c index b2609f4a84..65d4ba98bd 100644 --- a/shared-bindings/rotaryio/IncrementalEncoder.c +++ b/shared-bindings/rotaryio/IncrementalEncoder.c @@ -50,19 +50,19 @@ //| :param ~microcontroller.Pin pin_a: First pin to read pulses from. //| :param ~microcontroller.Pin pin_b: Second pin to read pulses from. //| -//| For example:: +//| For example:: //| -//| import rotaryio -//| import time -//| from board import * +//| import rotaryio +//| import time +//| from board import * //| -//| enc = rotaryio.IncrementalEncoder(D1, D2) -//| last_position = None -//| while True; -//| position = enc.position -//| if last_position == None or position != last_position: -//| print(position) -//| last_position = position +//| enc = rotaryio.IncrementalEncoder(D1, D2) +//| last_position = None +//| while True: +//| position = enc.position +//| if last_position == None or position != last_position: +//| print(position) +//| last_position = position //| STATIC mp_obj_t rotaryio_incrementalencoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 2, 2, true);