From 1f3de5174e236a4d6431e336163b30a47f44dffb Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 26 Jun 2018 19:49:55 -0400 Subject: [PATCH 01/19] Freeze Adafruit_CircuitPython_Crickit into crickit builds --- .gitmodules | 3 +++ frozen/Adafruit_CircuitPython_Crickit | 1 + frozen/Adafruit_CircuitPython_seesaw | 2 +- ports/atmel-samd/Makefile | 3 +++ .../boards/circuitplayground_express_crickit/mpconfigboard.mk | 3 +++ .../boards/feather_m0_express_crickit/mpconfigboard.mk | 3 +++ 6 files changed, 14 insertions(+), 1 deletion(-) create mode 160000 frozen/Adafruit_CircuitPython_Crickit diff --git a/.gitmodules b/.gitmodules index 61f5b5572e..8c834ba53f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -70,3 +70,6 @@ [submodule "ports/atmel-samd/peripherals"] path = ports/atmel-samd/peripherals url = https://github.com/adafruit/samd-peripherals.git +[submodule "frozen/Adafruit_CircuitPython_Crickit"] + path = frozen/Adafruit_CircuitPython_Crickit + url = https://github.com/adafruit/Adafruit_CircuitPython_Crickit diff --git a/frozen/Adafruit_CircuitPython_Crickit b/frozen/Adafruit_CircuitPython_Crickit new file mode 160000 index 0000000000..69669f8533 --- /dev/null +++ b/frozen/Adafruit_CircuitPython_Crickit @@ -0,0 +1 @@ +Subproject commit 69669f85339f23e7c32ea07cfb2ee00b06c37e7b diff --git a/frozen/Adafruit_CircuitPython_seesaw b/frozen/Adafruit_CircuitPython_seesaw index 498f59bf92..bf808037b3 160000 --- a/frozen/Adafruit_CircuitPython_seesaw +++ b/frozen/Adafruit_CircuitPython_seesaw @@ -1 +1 @@ -Subproject commit 498f59bf926477b3a8fb8eb157ca05eb12c3e298 +Subproject commit bf808037b3fb918353f514ef847d47a2d53a9546 diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index b7181aa3b1..62f058c3d7 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -117,6 +117,9 @@ else ifdef INTERNAL_FLASH_FILESYSTEM CFLAGS += -finline-limit=55 endif + ifdef CFLAGS_INLINE_LIMIT + CFLAGS += -finline-limit=$(CFLAGS_INLINE_LIMIT) + endif CFLAGS += -flto endif diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk index 0145bf7850..a1c40424f0 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk @@ -11,9 +11,12 @@ LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +CFLAGS_INLINE_LIMIT = 65 + # 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_Crickit FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk index 4096560c22..bc9ec89ac6 100644 --- a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk @@ -10,8 +10,11 @@ LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +CFLAGS_INLINE_LIMIT = 65 + # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_seesaw From f5df3d37eaa320fed9968a4e7640ec333e7f6cdd Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 26 Jun 2018 19:56:10 -0400 Subject: [PATCH 02/19] add Crickit library to docs/drivers.rst --- docs/drivers.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/drivers.rst b/docs/drivers.rst index 951967e09e..37bebb46ac 100644 --- a/docs/drivers.rst +++ b/docs/drivers.rst @@ -191,6 +191,7 @@ These provide functionality similar to `analogio`, `digitalio`, `pulseio`, and ` Adafruit SeeSaw ADS1x15 Analog-to-Digital Converter + Crickit Robotics Boards < DS2413 OneWire GPIO Expander FocalTech Capacitive Touch MCP230xx GPIO Expander @@ -200,7 +201,6 @@ These provide functionality similar to `analogio`, `digitalio`, `pulseio`, and ` TLC59711 12 x 16-bit PWM Driver MPR121 Capacitive Touch Sensor - Miscellaneous ---------------- From d98701c8d16974177dbfd3c28dc5204223fb9a65 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 27 Jun 2018 21:27:10 -0400 Subject: [PATCH 03/19] updates crickit and motor frozn libs with latest changes, including 550-2400 default servo range --- frozen/Adafruit_CircuitPython_Crickit | 2 +- frozen/Adafruit_CircuitPython_Motor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frozen/Adafruit_CircuitPython_Crickit b/frozen/Adafruit_CircuitPython_Crickit index 69669f8533..abae54a380 160000 --- a/frozen/Adafruit_CircuitPython_Crickit +++ b/frozen/Adafruit_CircuitPython_Crickit @@ -1 +1 @@ -Subproject commit 69669f85339f23e7c32ea07cfb2ee00b06c37e7b +Subproject commit abae54a38035dcf0b9a5fd688940880dbc68d61d diff --git a/frozen/Adafruit_CircuitPython_Motor b/frozen/Adafruit_CircuitPython_Motor index 6da5d14b98..683004104d 160000 --- a/frozen/Adafruit_CircuitPython_Motor +++ b/frozen/Adafruit_CircuitPython_Motor @@ -1 +1 @@ -Subproject commit 6da5d14b98ac3d929662731f73ce03c0c958b520 +Subproject commit 683004104d4612f230de12946a46467abbc507e8 From 52a11547cbd3d8bf132bdfdd2fa3f804920665f3 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 28 Jun 2018 23:06:08 -0400 Subject: [PATCH 04/19] fix OneWire timing and DigitalInOut.switch_to_input() --- .../common-hal/digitalio/DigitalInOut.c | 34 ++++++++++++------- ports/atmel-samd/mphalport.c | 20 +++++++++++ ports/atmel-samd/mphalport.h | 3 +- ports/atmel-samd/tick.c | 4 +-- ports/nrf/mphalport.c | 14 ++++++++ ports/nrf/mphalport.h | 3 +- shared-module/bitbangio/OneWire.c | 21 +++++++----- 7 files changed, 74 insertions(+), 25 deletions(-) diff --git a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c index cd0e3f91e5..03bd225b14 100644 --- a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c +++ b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c @@ -60,7 +60,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self void common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) { self->output = false; - + // This also sets direction to input. common_hal_digitalio_digitalinout_set_pull(self, pull); } @@ -69,13 +69,13 @@ void common_hal_digitalio_digitalinout_switch_to_output( digitalio_drive_mode_t drive_mode) { const uint8_t pin = self->pin->pin; gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF); - gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); - // Turn on "strong" pin driving (more current available). See DRVSTR doc in datasheet. hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)); self->output = true; self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + + // Direction is set in set_value. We don't need to do it here. common_hal_digitalio_digitalinout_set_value(self, value); } @@ -86,16 +86,22 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t* self, bool value) { + const uint8_t pin = self->pin->pin; + const uint8_t port = GPIO_PORT(pin); + const uint32_t pin_mask = 1U << GPIO_PIN(pin); if (value) { if (self->open_drain) { - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN); + // Assertion: pull is off, so the pin is floating in this case. + // We do open-drain high output (no sinking of current) + // by changing the direction to input with no pulls. + hri_port_clear_DIR_DIR_bf(PORT, port, pin_mask); } else { - gpio_set_pin_level(self->pin->pin, true); - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT); + hri_port_set_DIR_DIR_bf(PORT, port, pin_mask); + hri_port_set_OUT_OUT_bf(PORT, port, pin_mask); } } else { - gpio_set_pin_level(self->pin->pin, false); - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT); + hri_port_set_DIR_DIR_bf(PORT, port, pin_mask); + hri_port_clear_OUT_OUT_bf(PORT,port, pin_mask); } } @@ -105,10 +111,10 @@ bool common_hal_digitalio_digitalinout_get_value( if (!self->output) { return gpio_get_pin_level(pin); } else { - if (self->open_drain && hri_port_get_DIR_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) { + if (self->open_drain && hri_port_get_DIR_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) { return true; } else { - return hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)); + return hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)); } } } @@ -119,7 +125,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode( bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; // True is implemented differently between modes so reset the value to make - // sure its correct for the new mode. + // sure it's correct for the new mode. if (value) { common_hal_digitalio_digitalinout_set_value(self, value); } @@ -148,7 +154,9 @@ void common_hal_digitalio_digitalinout_set_pull( default: break; } + // Set pull first to avoid glitches. gpio_set_pin_pull_mode(self->pin->pin, asf_pull); + gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN); } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( @@ -158,9 +166,9 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( mp_raise_AttributeError("Cannot get pull while in output mode"); return PULL_NONE; } else { - if (hri_port_get_PINCFG_PULLEN_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)) == 0) { + if (hri_port_get_PINCFG_PULLEN_bit(PORT, GPIO_PORT(pin), GPIO_PIN(pin)) == 0) { return PULL_NONE; - } if (hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) { + } if (hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) { return PULL_UP; } else { return PULL_DOWN; diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index c1b8ce5504..dd6a147134 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -14,6 +14,7 @@ #include "hal/include/hal_delay.h" #include "hal/include/hal_gpio.h" #include "hal/include/hal_sleep.h" +#include "sam.h" #include "mpconfigboard.h" #include "mphalport.h" @@ -22,6 +23,7 @@ #include "usb.h" extern struct usart_module usart_instance; +extern uint32_t common_hal_mcu_processor_get_frequency(void); int mp_hal_stdin_rx_chr(void) { for (;;) { @@ -75,6 +77,24 @@ void mp_hal_delay_us(mp_uint_t delay) { tick_delay(delay); } +// Do a simple timing loop to wait for a certain number of microseconds. +// Can be used when interrupts are disabled, which makes tick_delay() unreliable. +// +// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51, multiplication and division cancel out. +// But get the frequency just in case. +#ifdef SAMD21 +#define DELAY_LOOP_ITERATIONS_PER_US ( (10U*48000000U) / common_hal_mcu_processor_get_frequency()) +#endif +#ifdef SAMD51 +#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) +#endif + +void mp_hal_delay_us_loop(uint32_t us) { + for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { + asm volatile("nop"); + } +} + void mp_hal_disable_all_interrupts(void) { common_hal_mcu_disable_interrupts(); } diff --git a/ports/atmel-samd/mphalport.h b/ports/atmel-samd/mphalport.h index 3b5955a4ff..31d34c8c39 100644 --- a/ports/atmel-samd/mphalport.h +++ b/ports/atmel-samd/mphalport.h @@ -48,7 +48,8 @@ int receive_usb(void); void mp_hal_set_interrupt_char(int c); void mp_hal_disable_all_interrupts(void); - void mp_hal_enable_all_interrupts(void); +void mp_hal_delay_us_loop(uint32_t us); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 9753a1a0c7..1493477933 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -64,8 +64,8 @@ void tick_init() { for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) { NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL); } - // Bump up the systick interrupt. - NVIC_SetPriority(SysTick_IRQn, 1); + // Bump up the systick interrupt so nothing else interferes with timekeeping. + NVIC_SetPriority(SysTick_IRQn, 0); #ifdef SAMD21 NVIC_SetPriority(USB_IRQn, 1); #endif diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 957d3535d2..1f732e1889 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -83,3 +83,17 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } + +// Do a simple timing loop to wait for a certain number of microseconds. +// Can be used when interrupts are disabled, which makes tick_delay() unreliable. +// +// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). +// TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. +// Fraction should compensate. +#define DELAY_LOOP_ITERATIONS_PER_US (30U*120000000U) / common_hal_mcu_processor_get_frequency()) + +void mp_hal_delay_us_loop(uint32_t us) { + for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { + asm volatile("nop"); + } +} diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 3334fd7fe9..79e1d2d58e 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -52,6 +52,7 @@ static inline uint32_t hal_tick_fake(void) { extern const unsigned char mp_hal_status_to_errno_table[4]; + NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable @@ -69,6 +70,7 @@ bool mp_hal_stdin_any(void); #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED) +void mp_hal_delay_us_loop(uint32_t us); // TODO: empty implementation for now. Used by machine_spi.c:69 #define mp_hal_delay_us_fast(p) @@ -76,4 +78,3 @@ bool mp_hal_stdin_any(void); #define mp_hal_ticks_cpu() (0) #endif - diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index e77bdb3b29..b226b9d784 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include "mphalport.h" + #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/microcontroller/__init__.h" @@ -49,14 +51,17 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { common_hal_digitalio_digitalinout_deinit(&self->pin); } +// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate +// due to SysTick rollover checking, done by an interrupt. + bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(480); + mp_hal_delay_us_loop(480); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - common_hal_mcu_delay_us(70); + mp_hal_delay_us_loop(70); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - common_hal_mcu_delay_us(410); + mp_hal_delay_us_loop(410); common_hal_mcu_enable_interrupts(); return value; } @@ -64,14 +69,14 @@ bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(6); + mp_hal_delay_us_loop(6); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); // TODO(tannewt): Test with more devices and maybe make the delays // configurable. This should be 9 by the datasheet but all bits read as 1 // then. - common_hal_mcu_delay_us(6); + mp_hal_delay_us_loop(6); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - common_hal_mcu_delay_us(55); + mp_hal_delay_us_loop(55); common_hal_mcu_enable_interrupts(); return value; } @@ -80,8 +85,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(bit? 6 : 60); + mp_hal_delay_us_loop(bit? 6 : 60); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - common_hal_mcu_delay_us(bit? 64 : 10); + mp_hal_delay_us_loop(bit? 64 : 10); common_hal_mcu_enable_interrupts(); } From b4fd77bb7c041d2855dba826f380ce66944c23d4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 00:00:05 -0400 Subject: [PATCH 05/19] fix nrf and esp8266 builds for OneWire fix --- ports/atmel-samd/mphalport.c | 26 ++++++++++++++++++++++++++ ports/esp8266/Makefile | 1 + ports/esp8266/mphalport.c | 33 +++++++++++++++++++++++++++++++++ ports/esp8266/mphalport.h | 34 ++++++++++++++++++++++++++++++++++ ports/nrf/mphalport.c | 4 +++- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 ports/esp8266/mphalport.c create mode 100644 ports/esp8266/mphalport.h diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index dd6a147134..7bb423a3e6 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -1,3 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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. + */ + #include #include "lib/mp-readline/readline.h" diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 8fabd3412a..02b75eadd5 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -92,6 +92,7 @@ SRC_C = \ machine_hspi.c \ modesp.c \ modnetwork.c \ + mphalport.c \ ets_alt_task.c \ fatfs_port.c \ posix_helpers.c \ diff --git a/ports/esp8266/mphalport.c b/ports/esp8266/mphalport.c new file mode 100644 index 0000000000..e22facef04 --- /dev/null +++ b/ports/esp8266/mphalport.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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 "esp_mphal.h" + +#include + +void mp_hal_delay_us_loop(uint32_t us) { + mp_hal_delay_us(us); +} diff --git a/ports/esp8266/mphalport.h b/ports/esp8266/mphalport.h new file mode 100644 index 0000000000..9cf2fcdd6f --- /dev/null +++ b/ports/esp8266/mphalport.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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. + */ + +#ifndef MICROPY_INCLUDED_ESP8266_MPHALPORT_H +#define MICROPY_INCLUDED_ESP8266_MPHALPORT_H + +#include + +void mp_hal_delay_us_loop(uint32_t us); + +#endif // MICROPY_INCLUDED_ESP8266_MPHALPORT_H diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 1f732e1889..014d410ecf 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -32,6 +32,8 @@ #include "py/mperrno.h" #include "hal_uart.h" +extern uint32_t common_hal_mcu_processor_get_frequency(void); + #define UART_INSTANCE UART_BASE(0) FIL* boot_output_file; @@ -90,7 +92,7 @@ void mp_hal_stdout_tx_str(const char *str) { // Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). // TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. // Fraction should compensate. -#define DELAY_LOOP_ITERATIONS_PER_US (30U*120000000U) / common_hal_mcu_processor_get_frequency()) +#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) void mp_hal_delay_us_loop(uint32_t us) { for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { From 2a0b8576439b0cfefff909135dba424a2a179275 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:01:46 -0400 Subject: [PATCH 06/19] implement mp_hal_delay_us() to not need interrupts, and use it --- ports/atmel-samd/mphalport.c | 9 +++----- ports/atmel-samd/mphalport.h | 2 -- ports/esp8266/Makefile | 1 - .../nrf/common-hal/microcontroller/__init__.c | 3 ++- ports/nrf/mphalport.c | 14 ------------ ports/nrf/mphalport.h | 2 -- shared-module/bitbangio/OneWire.c | 22 +++++++++---------- 7 files changed, 15 insertions(+), 38 deletions(-) diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index 7bb423a3e6..a4eca1c748 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -99,10 +99,7 @@ void mp_hal_delay_ms(mp_uint_t delay) { } } -void mp_hal_delay_us(mp_uint_t delay) { - tick_delay(delay); -} - +// Use mp_hal_delay_us() for timing of less than 1ms. // Do a simple timing loop to wait for a certain number of microseconds. // Can be used when interrupts are disabled, which makes tick_delay() unreliable. // @@ -115,8 +112,8 @@ void mp_hal_delay_us(mp_uint_t delay) { #define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) #endif -void mp_hal_delay_us_loop(uint32_t us) { - for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { +void mp_hal_delay_us(mp_uint_t delay) { + for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { asm volatile("nop"); } } diff --git a/ports/atmel-samd/mphalport.h b/ports/atmel-samd/mphalport.h index 31d34c8c39..3bc824d4e9 100644 --- a/ports/atmel-samd/mphalport.h +++ b/ports/atmel-samd/mphalport.h @@ -50,6 +50,4 @@ void mp_hal_set_interrupt_char(int c); void mp_hal_disable_all_interrupts(void); void mp_hal_enable_all_interrupts(void); -void mp_hal_delay_us_loop(uint32_t us); - #endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 02b75eadd5..8fabd3412a 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -92,7 +92,6 @@ SRC_C = \ machine_hspi.c \ modesp.c \ modnetwork.c \ - mphalport.c \ ets_alt_task.c \ fatfs_port.c \ posix_helpers.c \ diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index e26acff1b4..4399e7708c 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -33,6 +33,8 @@ #include "shared-bindings/microcontroller/Processor.h" // TODO porting common_hal_mcu +// This routine should work even when interrupts are disabled. Used by OneWire +// for precise timing. void common_hal_mcu_delay_us(uint32_t delay) { // os_delay_us(delay); } @@ -58,4 +60,3 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = { .type = &mcu_processor_type, }, }; - diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 014d410ecf..d0447f06c3 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -85,17 +85,3 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } - -// Do a simple timing loop to wait for a certain number of microseconds. -// Can be used when interrupts are disabled, which makes tick_delay() unreliable. -// -// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). -// TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. -// Fraction should compensate. -#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) - -void mp_hal_delay_us_loop(uint32_t us) { - for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { - asm volatile("nop"); - } -} diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 79e1d2d58e..a872477370 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -52,7 +52,6 @@ static inline uint32_t hal_tick_fake(void) { extern const unsigned char mp_hal_status_to_errno_table[4]; - NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable @@ -70,7 +69,6 @@ bool mp_hal_stdin_any(void); #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED) -void mp_hal_delay_us_loop(uint32_t us); // TODO: empty implementation for now. Used by machine_spi.c:69 #define mp_hal_delay_us_fast(p) diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index b226b9d784..f5f4790876 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -24,8 +24,6 @@ * THE SOFTWARE. */ -#include "mphalport.h" - #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/microcontroller/__init__.h" @@ -51,17 +49,17 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { common_hal_digitalio_digitalinout_deinit(&self->pin); } -// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate -// due to SysTick rollover checking, done by an interrupt. +// We use common_hal_mcu_delay_us(). It should not be dependent on interrupts +// to do accurate timekeeping, since we disable interrupts during the delays below. bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(480); + common_hal_mcu_delay_us(480); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - mp_hal_delay_us_loop(70); + common_hal_mcu_delay_us(70); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - mp_hal_delay_us_loop(410); + common_hal_mcu_delay_us(410); common_hal_mcu_enable_interrupts(); return value; } @@ -69,14 +67,14 @@ bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(6); + common_hal_mcu_delay_us(6); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); // TODO(tannewt): Test with more devices and maybe make the delays // configurable. This should be 9 by the datasheet but all bits read as 1 // then. - mp_hal_delay_us_loop(6); + common_hal_mcu_delay_us(6); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - mp_hal_delay_us_loop(55); + common_hal_mcu_delay_us(55); common_hal_mcu_enable_interrupts(); return value; } @@ -85,8 +83,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(bit? 6 : 60); + common_hal_mcu_delay_us(bit? 6 : 60); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - mp_hal_delay_us_loop(bit? 64 : 10); + common_hal_mcu_delay_us(bit? 64 : 10); common_hal_mcu_enable_interrupts(); } From 7028a399c2f29ee71dafbbf1536244b1b01b8303 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:04:18 -0400 Subject: [PATCH 07/19] remove previously added esp8266/mphalport.{c,h} --- ports/esp8266/mphalport.c | 33 --------------------------------- ports/esp8266/mphalport.h | 34 ---------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 ports/esp8266/mphalport.c delete mode 100644 ports/esp8266/mphalport.h diff --git a/ports/esp8266/mphalport.c b/ports/esp8266/mphalport.c deleted file mode 100644 index e22facef04..0000000000 --- a/ports/esp8266/mphalport.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 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 "esp_mphal.h" - -#include - -void mp_hal_delay_us_loop(uint32_t us) { - mp_hal_delay_us(us); -} diff --git a/ports/esp8266/mphalport.h b/ports/esp8266/mphalport.h deleted file mode 100644 index 9cf2fcdd6f..0000000000 --- a/ports/esp8266/mphalport.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 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. - */ - -#ifndef MICROPY_INCLUDED_ESP8266_MPHALPORT_H -#define MICROPY_INCLUDED_ESP8266_MPHALPORT_H - -#include - -void mp_hal_delay_us_loop(uint32_t us); - -#endif // MICROPY_INCLUDED_ESP8266_MPHALPORT_H From 7c9a0e29960948eaad4485481355a84059148df4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:06:51 -0400 Subject: [PATCH 08/19] remove unnecessary extern in nrf/mphalport.c --- ports/nrf/mphalport.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index d0447f06c3..957d3535d2 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -32,8 +32,6 @@ #include "py/mperrno.h" #include "hal_uart.h" -extern uint32_t common_hal_mcu_processor_get_frequency(void); - #define UART_INSTANCE UART_BASE(0) FIL* boot_output_file; From b68cd143919ddec64f23dc403b5c5f58513e135a Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:32:25 -0400 Subject: [PATCH 09/19] update crickit-related libs --- frozen/Adafruit_CircuitPython_Crickit | 2 +- frozen/Adafruit_CircuitPython_Motor | 2 +- frozen/Adafruit_CircuitPython_seesaw | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frozen/Adafruit_CircuitPython_Crickit b/frozen/Adafruit_CircuitPython_Crickit index abae54a380..44e1b7847d 160000 --- a/frozen/Adafruit_CircuitPython_Crickit +++ b/frozen/Adafruit_CircuitPython_Crickit @@ -1 +1 @@ -Subproject commit abae54a38035dcf0b9a5fd688940880dbc68d61d +Subproject commit 44e1b7847de6f1e1575873b52fd3baeb2e1fe770 diff --git a/frozen/Adafruit_CircuitPython_Motor b/frozen/Adafruit_CircuitPython_Motor index 683004104d..c34abe2c01 160000 --- a/frozen/Adafruit_CircuitPython_Motor +++ b/frozen/Adafruit_CircuitPython_Motor @@ -1 +1 @@ -Subproject commit 683004104d4612f230de12946a46467abbc507e8 +Subproject commit c34abe2c0109637b30f8c6610dd848f383160041 diff --git a/frozen/Adafruit_CircuitPython_seesaw b/frozen/Adafruit_CircuitPython_seesaw index bf808037b3..abab0a96ef 160000 --- a/frozen/Adafruit_CircuitPython_seesaw +++ b/frozen/Adafruit_CircuitPython_seesaw @@ -1 +1 @@ -Subproject commit bf808037b3fb918353f514ef847d47a2d53a9546 +Subproject commit abab0a96ef5644b153bb052180d32d1a2d7e592f From d9723bb61314463d00aa4e47093bfb82f97aeed0 Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Fri, 29 Jun 2018 05:45:38 -0700 Subject: [PATCH 10/19] include RTC fix for SAMd51 in circuitpython build --- ports/atmel-samd/asf4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/asf4 b/ports/atmel-samd/asf4 index cdde379f78..88a67f9c83 160000 --- a/ports/atmel-samd/asf4 +++ b/ports/atmel-samd/asf4 @@ -1 +1 @@ -Subproject commit cdde379f78989436590a556cadbc0e201666ee04 +Subproject commit 88a67f9c83eeb225fe65f596078f8aaa3f6de1ec From bf9981e8db8d3ea35beb50579f06e2f1544fa2f0 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 3 Jul 2018 14:39:21 -0400 Subject: [PATCH 11/19] bring all frozen libraries up to date --- frozen/Adafruit_CircuitPython_BusDevice | 2 +- frozen/Adafruit_CircuitPython_CircuitPlayground | 2 +- frozen/Adafruit_CircuitPython_Crickit | 2 +- frozen/Adafruit_CircuitPython_HID | 2 +- frozen/Adafruit_CircuitPython_Motor | 2 +- frozen/Adafruit_CircuitPython_seesaw | 2 +- .../boards/circuitplayground_express_crickit/mpconfigboard.mk | 2 +- .../boards/feather_m0_express_crickit/mpconfigboard.mk | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frozen/Adafruit_CircuitPython_BusDevice b/frozen/Adafruit_CircuitPython_BusDevice index 376a80e62a..0791964147 160000 --- a/frozen/Adafruit_CircuitPython_BusDevice +++ b/frozen/Adafruit_CircuitPython_BusDevice @@ -1 +1 @@ -Subproject commit 376a80e62acb3ba8b9a1e5c11bb75d36352f2190 +Subproject commit 07919641470edb602585c6a91f7b8eacf17e664b diff --git a/frozen/Adafruit_CircuitPython_CircuitPlayground b/frozen/Adafruit_CircuitPython_CircuitPlayground index d0022de9d5..d0aa6dc56d 160000 --- a/frozen/Adafruit_CircuitPython_CircuitPlayground +++ b/frozen/Adafruit_CircuitPython_CircuitPlayground @@ -1 +1 @@ -Subproject commit d0022de9d5e9b4c3d4998a5d708e59ee147f0a21 +Subproject commit d0aa6dc56d66decfae92daced7384c1e3518a666 diff --git a/frozen/Adafruit_CircuitPython_Crickit b/frozen/Adafruit_CircuitPython_Crickit index 44e1b7847d..f948e09d38 160000 --- a/frozen/Adafruit_CircuitPython_Crickit +++ b/frozen/Adafruit_CircuitPython_Crickit @@ -1 +1 @@ -Subproject commit 44e1b7847de6f1e1575873b52fd3baeb2e1fe770 +Subproject commit f948e09d38aaa8d3f59350372388cd5d603f95a6 diff --git a/frozen/Adafruit_CircuitPython_HID b/frozen/Adafruit_CircuitPython_HID index 1da880cee3..5c2f6ef1ed 160000 --- a/frozen/Adafruit_CircuitPython_HID +++ b/frozen/Adafruit_CircuitPython_HID @@ -1 +1 @@ -Subproject commit 1da880cee3c2d64e2e7e4ca5082f3a74c4a37a5a +Subproject commit 5c2f6ef1ed80f24b6a3878067d40350d3725e198 diff --git a/frozen/Adafruit_CircuitPython_Motor b/frozen/Adafruit_CircuitPython_Motor index c34abe2c01..e0b709f171 160000 --- a/frozen/Adafruit_CircuitPython_Motor +++ b/frozen/Adafruit_CircuitPython_Motor @@ -1 +1 @@ -Subproject commit c34abe2c0109637b30f8c6610dd848f383160041 +Subproject commit e0b709f1710555da67705360870ba0d14ced7e06 diff --git a/frozen/Adafruit_CircuitPython_seesaw b/frozen/Adafruit_CircuitPython_seesaw index abab0a96ef..340cd17fad 160000 --- a/frozen/Adafruit_CircuitPython_seesaw +++ b/frozen/Adafruit_CircuitPython_seesaw @@ -1 +1 @@ -Subproject commit abab0a96ef5644b153bb052180d32d1a2d7e592f +Subproject commit 340cd17fad0c29d3a70d6e298a30ecc753df054e diff --git a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk index a1c40424f0..972c4b10dc 100644 --- a/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk @@ -11,7 +11,7 @@ LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 -CFLAGS_INLINE_LIMIT = 65 +CFLAGS_INLINE_LIMIT = 55 # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk index bc9ec89ac6..7ff5337a34 100644 --- a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk @@ -10,7 +10,7 @@ LONGINT_IMPL = MPZ CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 -CFLAGS_INLINE_LIMIT = 65 +CFLAGS_INLINE_LIMIT = 55 # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice From 53d821332fa933a3b6272710dadc24bf140a3152 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 5 Jul 2018 17:35:20 -0400 Subject: [PATCH 12/19] update Crickit library to fix stepper Drive issues --- frozen/Adafruit_CircuitPython_Crickit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozen/Adafruit_CircuitPython_Crickit b/frozen/Adafruit_CircuitPython_Crickit index f948e09d38..44f52c5dac 160000 --- a/frozen/Adafruit_CircuitPython_Crickit +++ b/frozen/Adafruit_CircuitPython_Crickit @@ -1 +1 @@ -Subproject commit f948e09d38aaa8d3f59350372388cd5d603f95a6 +Subproject commit 44f52c5dacd9fc605565e5794e95c9a785aaf693 From 8bb363f7c4f537e90659bc98e1a1cd0097424d96 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 9 Jul 2018 17:18:06 -0400 Subject: [PATCH 13/19] must set pull after setting direction with gpio_set_pin routines --- ports/atmel-samd/common-hal/digitalio/DigitalInOut.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c index 03bd225b14..c5c8f48e9f 100644 --- a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c +++ b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c @@ -40,8 +40,9 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( claim_pin(pin); self->pin = pin; - gpio_set_pin_pull_mode(pin->pin, GPIO_PULL_OFF); + // Must set pull after setting direction. gpio_set_pin_direction(pin->pin, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(pin->pin, GPIO_PULL_OFF); return DIGITALINOUT_OK; } @@ -154,9 +155,9 @@ void common_hal_digitalio_digitalinout_set_pull( default: break; } - // Set pull first to avoid glitches. - gpio_set_pin_pull_mode(self->pin->pin, asf_pull); + // Must set pull after setting direction. gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(self->pin->pin, asf_pull); } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( From 3525d65af3cd8ad08b626070fa01048b0697fedc Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 17 Jul 2018 21:24:49 +0700 Subject: [PATCH 14/19] should fix #1021 - update tinyusb for wanted char - move usb code into usb.c --- lib/tinyusb | 2 +- ports/nrf/Makefile | 1 + ports/nrf/README.md | 11 +--- ports/nrf/background.c | 2 +- ports/nrf/boards/pca10056/board.c | 45 +------------ ports/nrf/mpconfigport.h | 2 +- ports/nrf/usb/usb.c | 102 ++++++++++++++++++++++++++++++ ports/nrf/usb/usb.h | 42 ++++++++++++ 8 files changed, 154 insertions(+), 53 deletions(-) create mode 100644 ports/nrf/usb/usb.c create mode 100644 ports/nrf/usb/usb.h diff --git a/lib/tinyusb b/lib/tinyusb index a0849fee9f..6129670e4a 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit a0849fee9f4b268ccc9798c70c654a835f8e20c8 +Subproject commit 6129670e4a23812d1926b15c191d76538f7a325a diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 74b6ec2ad4..113fb6b253 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -122,6 +122,7 @@ SRC_C += \ ifeq ($(MCU_SUB_VARIANT),nrf52840) SRC_C += \ + usb/usb.c \ usb/tusb_descriptors.c \ usb/usb_msc_flash.c \ lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \ diff --git a/ports/nrf/README.md b/ports/nrf/README.md index 7b3e86c38b..be799a6a27 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -51,15 +51,10 @@ Prerequisite steps for building the nrf port: git submodule update --init make -C mpy-cross -By default, the feather52832 is used as compile target. To build and flash issue the following command inside the ports/nrf/ folder: +To build and flash issue the following command inside the ports/nrf/ folder: - make - make flash - -Alternatively the target board could be defined: - - make BOARD=pca10056 - make flash + make BOARD=pca10056 + make BOARD=pca10056 flash ## Compile and Flash with Bluetooth Stack diff --git a/ports/nrf/background.c b/ports/nrf/background.c index 9f8c8b34b4..d19604f680 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -29,6 +29,6 @@ void run_background_tasks(void) { #ifdef NRF52840_XXAA tusb_task(); - tud_cdc_flush(); + tud_cdc_write_flush(); #endif } diff --git a/ports/nrf/boards/pca10056/board.c b/ports/nrf/boards/pca10056/board.c index f97be07486..0b667aa9d9 100644 --- a/ports/nrf/boards/pca10056/board.c +++ b/ports/nrf/boards/pca10056/board.c @@ -26,13 +26,9 @@ #include #include - -#include "nrfx.h" -#include "nrfx_power.h" #include "boards/board.h" - -#include "tick.h" -#include "tusb.h" +#include "nrfx.h" +#include "usb.h" void board_init(void) { @@ -40,27 +36,7 @@ void board_init(void) { NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); NRF_CLOCK->TASKS_LFCLKSTART = 1UL; - // Init USB - -#ifdef SOFTDEVICE_PRESENT - // TODO support Softdevice config -#else - // Softdevice is not present, init power module and register tusb power event function - // for vusb detect, ready, removed - extern void tusb_hal_nrf_power_event(uint32_t event); - - // Power module init - const nrfx_power_config_t pwr_cfg = { 0 }; - nrfx_power_init(&pwr_cfg); - - // USB Power detection - const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; - nrfx_power_usbevt_init(&config); - - nrfx_power_usbevt_enable(); -#endif - - tusb_init(); + usb_init(); } bool board_requests_safe_mode(void) { @@ -72,21 +48,6 @@ void reset_board(void) { } -//--------------------------------------------------------------------+ -// tinyusb callbacks -//--------------------------------------------------------------------+ -void tud_mount_cb(uint8_t rhport) { - (void) rhport; -} -void tud_umount_cb(uint8_t rhport) { - (void) rhport; -} -uint32_t tusb_hal_millis(void) { - uint64_t ms; - uint32_t us; - current_tick(&ms, &us); - return (uint32_t) ms; -} diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 85396e5dd0..7731534b9d 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -232,7 +232,7 @@ extern const struct _mp_obj_module_t mp_module_ubluepy; // We need to provide a declaration/definition of alloca() #include -extern void run_background_tasks(void); +void run_background_tasks(void); #define MICROPY_VM_HOOK_LOOP run_background_tasks(); #define MICROPY_VM_HOOK_RETURN run_background_tasks(); diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c new file mode 100644 index 0000000000..d2314b7798 --- /dev/null +++ b/ports/nrf/usb/usb.c @@ -0,0 +1,102 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 hathach 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 "nrfx.h" +#include "nrfx_power.h" +#include "tick.h" +#include "usb.h" + +#include "lib/mp-readline/readline.h" +#include "lib/utils/interrupt_char.h" + +#ifdef NRF52840_XXAA + +void usb_init(void) { + +#ifdef SOFTDEVICE_PRESENT + // TODO support Softdevice config +#else + // Softdevice is not present, init power module and register tusb power event function + // for vusb detect, ready, removed + extern void tusb_hal_nrf_power_event(uint32_t event); + + // Power module init + const nrfx_power_config_t pwr_cfg = { 0 }; + nrfx_power_init(&pwr_cfg); + + // USB Power detection + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; + nrfx_power_usbevt_init(&config); + + nrfx_power_usbevt_enable(); +#endif + + tusb_init(); + +#if MICROPY_KBD_EXCEPTION + // set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() fired when Ctrl+C is received + // TODO should be called in mp_hal_set_interrupt_char() + tud_cdc_set_wanted_char(CHAR_CTRL_C); +#endif +} + +//--------------------------------------------------------------------+ +// tinyusb callbacks +//--------------------------------------------------------------------+ +void tud_mount_cb(void) { +} + +void tud_umount_cb(void) { +} + +uint32_t tusb_hal_millis(void) { + uint64_t ms; + uint32_t us; + current_tick(&ms, &us); + return (uint32_t) ms; +} + +#if MICROPY_KBD_EXCEPTION + +/** + * Callback invoked when received an "wanted" char. + * @param itf Interface index (for multiple cdc interfaces) + * @param wanted_char The wanted char (set previously) + */ +void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) +{ + (void) itf; // not used + + if (mp_interrupt_char == wanted_char) { + tud_cdc_read_flush(); // flush read fifo + mp_keyboard_interrupt(); + } +} + +#endif + +#endif + diff --git a/ports/nrf/usb/usb.h b/ports/nrf/usb/usb.h new file mode 100644 index 0000000000..9808c85156 --- /dev/null +++ b/ports/nrf/usb/usb.h @@ -0,0 +1,42 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 hathach 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_NRF_USB_H +#define MICROPY_INCLUDED_NRF_USB_H + +#include "tusb.h" + +#ifdef NRF52840_XXAA + +void usb_init(void); + +#else + +#define usb_init() + +#endif + +#endif // MICROPY_INCLUDED_NRF_USB_H From a0b9871ba93f970ddf6ca03a2e40a2dc1f9caf0f Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 17 Jul 2018 21:52:20 +0700 Subject: [PATCH 15/19] use seperate interrupt_char.c for nrf move usb descriptors into usb.c --- ports/nrf/Makefile | 3 +- ports/nrf/interrupt_char.c | 55 ++++++++++++++++++++++ ports/nrf/usb/tusb_descriptors.c | 78 -------------------------------- ports/nrf/usb/usb.c | 51 +++++++++++++++++---- ports/nrf/usb/usb.h | 4 +- 5 files changed, 99 insertions(+), 92 deletions(-) create mode 100644 ports/nrf/interrupt_char.c delete mode 100644 ports/nrf/usb/tusb_descriptors.c diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 113fb6b253..0057bdc249 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -102,6 +102,7 @@ SRC_C += \ tick.c \ background.c \ internal_flash.c \ + interrupt_char.c \ drivers/bluetooth/ble_drv.c \ drivers/bluetooth/ble_uart.c \ boards/$(BOARD)/board.c \ @@ -113,7 +114,6 @@ SRC_C += \ lib/timeutils/timeutils.c \ lib/utils/buffer_helper.c \ lib/utils/context_manager_helpers.c \ - lib/utils/interrupt_char.c \ lib/utils/pyexec.c \ lib/utils/stdout_helpers.c \ lib/libc/string0.c \ @@ -123,7 +123,6 @@ ifeq ($(MCU_SUB_VARIANT),nrf52840) SRC_C += \ usb/usb.c \ - usb/tusb_descriptors.c \ usb/usb_msc_flash.c \ lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \ lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \ diff --git a/ports/nrf/interrupt_char.c b/ports/nrf/interrupt_char.c new file mode 100644 index 0000000000..f0f6b3a35f --- /dev/null +++ b/ports/nrf/interrupt_char.c @@ -0,0 +1,55 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2016 Damien P. George + * + * 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 "py/obj.h" +#include "py/mpstate.h" +#include "usb.h" + +#if MICROPY_KBD_EXCEPTION + +int mp_interrupt_char; + +void mp_hal_set_interrupt_char(int c) { + if (c != -1) { + mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); + } + mp_interrupt_char = c; + +#ifdef NRF52840_XXAA + tud_cdc_set_wanted_char(c); +#endif +} + +void mp_keyboard_interrupt(void) { + MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)); + #if MICROPY_ENABLE_SCHEDULER + if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { + MP_STATE_VM(sched_state) = MP_SCHED_PENDING; + } + #endif +} + +#endif diff --git a/ports/nrf/usb/tusb_descriptors.c b/ports/nrf/usb/tusb_descriptors.c deleted file mode 100644 index 80b847d688..0000000000 --- a/ports/nrf/usb/tusb_descriptors.c +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************/ -/*! - @file tusb_descriptors.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "tusb.h" - -//--------------------------------------------------------------------+ -// STRING DESCRIPTORS -//--------------------------------------------------------------------+ - -// array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = -{ - // 0 index is supported language = English - TUD_DESC_STRCONV(0x0409), - - // Manufacturer - TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'), - - // Product - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'), - - // Serials TODO use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5'), - - // CDC Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'), - - // MSC Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','t','o','r','a','g','e'), - - // Custom Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','C','u','s','t','o','m') -}; - -// tud_desc_set is required by tinyusb stack -// since CFG_TUD_DESC_AUTO is enabled, we only need to set string_arr -tud_desc_set_t tud_desc_set = -{ - .device = NULL, - .config = NULL, - .string_arr = (uint8_t const **) string_desc_arr, - .hid_report = NULL -}; diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index d2314b7798..0105ef6bfd 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -28,12 +28,49 @@ #include "nrfx_power.h" #include "tick.h" #include "usb.h" - -#include "lib/mp-readline/readline.h" #include "lib/utils/interrupt_char.h" #ifdef NRF52840_XXAA +//--------------------------------------------------------------------+ +// STRING DESCRIPTORS +//--------------------------------------------------------------------+ + +// array of pointer to string descriptors +uint16_t const * const string_desc_arr [] = +{ + // 0 index is supported language = English + TUD_DESC_STRCONV(0x0409), + + // Manufacturer + TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'), + + // Product + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'), + + // Serials TODO use chip ID + TUD_DESC_STRCONV('1', '2', '3', '4', '5'), + + // CDC Interface + TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'), + + // MSC Interface + TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','t','o','r','a','g','e'), + + // Custom Interface + TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','C','u','s','t','o','m') +}; + +// tud_desc_set is required by tinyusb stack +// since CFG_TUD_DESC_AUTO is enabled, we only need to set string_arr +tud_desc_set_t tud_desc_set = +{ + .device = NULL, + .config = NULL, + .string_arr = (uint8_t const **) string_desc_arr, + .hid_report = NULL +}; + void usb_init(void) { #ifdef SOFTDEVICE_PRESENT @@ -55,12 +92,6 @@ void usb_init(void) { #endif tusb_init(); - -#if MICROPY_KBD_EXCEPTION - // set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() fired when Ctrl+C is received - // TODO should be called in mp_hal_set_interrupt_char() - tud_cdc_set_wanted_char(CHAR_CTRL_C); -#endif } //--------------------------------------------------------------------+ @@ -91,12 +122,12 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) (void) itf; // not used if (mp_interrupt_char == wanted_char) { - tud_cdc_read_flush(); // flush read fifo + tud_cdc_read_flush(); // flush read fifo mp_keyboard_interrupt(); } } #endif -#endif +#endif // NRF52840_XXAA diff --git a/ports/nrf/usb/usb.h b/ports/nrf/usb/usb.h index 9808c85156..c39b59a3f4 100644 --- a/ports/nrf/usb/usb.h +++ b/ports/nrf/usb/usb.h @@ -27,10 +27,10 @@ #ifndef MICROPY_INCLUDED_NRF_USB_H #define MICROPY_INCLUDED_NRF_USB_H -#include "tusb.h" - #ifdef NRF52840_XXAA +#include "tusb.h" + void usb_init(void); #else From bb28faf39c49c785475aff1e6b1680b9360d140f Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 17 Jul 2018 23:57:49 +0700 Subject: [PATCH 16/19] arturo182 review --- lib/tinyusb | 2 +- ports/nrf/interrupt_char.c | 3 ++ ports/nrf/usb/usb.c | 56 ++++++++++++++++++++++++++++---------- ports/nrf/usb/usb.h | 8 ------ 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 6129670e4a..421ae8fc82 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 6129670e4a23812d1926b15c191d76538f7a325a +Subproject commit 421ae8fc82b884e087e99c8ab3fa25883a3dd623 diff --git a/ports/nrf/interrupt_char.c b/ports/nrf/interrupt_char.c index f0f6b3a35f..8793b0d889 100644 --- a/ports/nrf/interrupt_char.c +++ b/ports/nrf/interrupt_char.c @@ -26,7 +26,10 @@ #include "py/obj.h" #include "py/mpstate.h" + +#ifdef NRF52840_XXAA #include "usb.h" +#endif #if MICROPY_KBD_EXCEPTION diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 0105ef6bfd..648ee708dd 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -30,7 +30,10 @@ #include "usb.h" #include "lib/utils/interrupt_char.h" -#ifdef NRF52840_XXAA +#ifdef SOFTDEVICE_PRESENT +#include "nrf_sdm.h" +#include "nrf_soc.h" +#endif //--------------------------------------------------------------------+ // STRING DESCRIPTORS @@ -71,26 +74,53 @@ tud_desc_set_t tud_desc_set = .hid_report = NULL }; + +/* tinyusb function that handles power event (detected, ready, removed) + * We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. + */ +extern void tusb_hal_nrf_power_event(uint32_t event); + void usb_init(void) { + // USB power may already be ready at this time -> no event generated + // We need to invoke the handler based on the status initially + uint32_t usb_reg; + #ifdef SOFTDEVICE_PRESENT - // TODO support Softdevice config + uint8_t sd_en = false; + (void) sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) { + sd_power_usbdetected_enable(true); + sd_power_usbpwrrdy_enable(true); + sd_power_usbremoved_enable(true); + + sd_power_usbregstatus_get(&usb_reg); + }else #else - // Softdevice is not present, init power module and register tusb power event function - // for vusb detect, ready, removed - extern void tusb_hal_nrf_power_event(uint32_t event); + { + // Power module init + const nrfx_power_config_t pwr_cfg = { 0 }; + nrfx_power_init(&pwr_cfg); - // Power module init - const nrfx_power_config_t pwr_cfg = { 0 }; - nrfx_power_init(&pwr_cfg); + // Register tusb function as USB power handler + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; + nrfx_power_usbevt_init(&config); - // USB Power detection - const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; - nrfx_power_usbevt_init(&config); + nrfx_power_usbevt_enable(); - nrfx_power_usbevt_enable(); + usb_reg = NRF_POWER->USBREGSTATUS; + } #endif + if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); + } + + if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); + } + tusb_init(); } @@ -129,5 +159,3 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) #endif -#endif // NRF52840_XXAA - diff --git a/ports/nrf/usb/usb.h b/ports/nrf/usb/usb.h index c39b59a3f4..2032608749 100644 --- a/ports/nrf/usb/usb.h +++ b/ports/nrf/usb/usb.h @@ -27,16 +27,8 @@ #ifndef MICROPY_INCLUDED_NRF_USB_H #define MICROPY_INCLUDED_NRF_USB_H -#ifdef NRF52840_XXAA - #include "tusb.h" void usb_init(void); -#else - -#define usb_init() - -#endif - #endif // MICROPY_INCLUDED_NRF_USB_H From be1d882a8b5d2878c9b4402ae641abf9a88cd682 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 19 Jul 2018 15:19:21 -0400 Subject: [PATCH 17/19] Prevent repetitive recursive scanning of dicts when making them long-lived --- py/gc_long_lived.c | 12 +++++++++++- py/obj.h | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/py/gc_long_lived.c b/py/gc_long_lived.c index d34fde5d9b..dd5f0e8fd0 100644 --- a/py/gc_long_lived.c +++ b/py/gc_long_lived.c @@ -91,6 +91,13 @@ mp_obj_dict_t *make_dict_long_lived(mp_obj_dict_t *dict, uint8_t max_depth) { if (dict == NULL || max_depth == 0) { return dict; } + // Don't recurse unnecessarily. Return immediately if we've already seen this dict. + if (dict->map.scanning) { + return dict; + } + // Mark that we're processing this dict. + dict->map.scanning = 1; + // Update all of the references first so that we reduce the chance of references to the old // copies. dict->map.table = gc_make_long_lived(dict->map.table); @@ -100,7 +107,10 @@ mp_obj_dict_t *make_dict_long_lived(mp_obj_dict_t *dict, uint8_t max_depth) { dict->map.table[i].value = make_obj_long_lived(value, max_depth - 1); } } - return gc_make_long_lived(dict); + dict = gc_make_long_lived(dict); + // Done recursing through this dict. + dict->map.scanning = 0; + return dict; } mp_obj_str_t *make_str_long_lived(mp_obj_str_t *str) { diff --git a/py/obj.h b/py/obj.h index d086d3c149..8e95983f06 100644 --- a/py/obj.h +++ b/py/obj.h @@ -359,7 +359,9 @@ typedef struct _mp_map_t { size_t all_keys_are_qstrs : 1; size_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered size_t is_ordered : 1; // an ordered array - size_t used : (8 * sizeof(size_t) - 3); + size_t scanning : 1; // true if we're in the middle of scanning linked dictionaries, + // e.g., make_dict_long_lived() + size_t used : (8 * sizeof(size_t) - 4); size_t alloc; mp_map_elem_t *table; } mp_map_t; From 055725f744ad52d2d104a53713b8be458ca948d3 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 25 Jul 2018 07:45:55 -0700 Subject: [PATCH 18/19] Update travis to gcc 2018q2. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 .travis.yml diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 index 58b40f168b..2208519108 --- a/.travis.yml +++ b/.travis.yml @@ -50,8 +50,8 @@ before_script: - sudo dpkg --add-architecture i386 - ([[ -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)) + - ([[ -z "$TRAVIS_BOARD" ]] || (wget https://s3.amazonaws.com/adafruit-circuit-python/gcc-arm-embedded_7-2018q2-1~trusty1_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-2018q2-1~trusty1_amd64.deb && sudo dpkg -i gcc-arm-embedded*_amd64.deb)) # For nrf builds - ([[ $TRAVIS_BOARD != "feather52832" && $TRAVIS_BOARD != "pca10056" ]] || sudo ports/nrf/drivers/bluetooth/download_ble_stack.sh) From 70df242f724c067d66fd9f977ed78efb403b6314 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 25 Jul 2018 09:39:46 -0700 Subject: [PATCH 19/19] Update uf2 tools to support python3. --- tools/uf2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/uf2 b/tools/uf2 index 6c37e72342..968716efd3 160000 --- a/tools/uf2 +++ b/tools/uf2 @@ -1 +1 @@ -Subproject commit 6c37e72342d814079bc9b760e2840dc0d766f94f +Subproject commit 968716efd30600984139d706bcbd8ec1a1b2336d