From ed6cc64c4988387e79ac6889e0fd7f4984a5dfc0 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Sep 2020 16:59:41 -0500 Subject: [PATCH 1/2] Corrected issue with BLE not working on nrf52840 --- ports/nrf/common-hal/busio/SPI.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index 380ec27de455..d7d0e0fe81ae 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -104,8 +104,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { never_reset[i] = true; never_reset_pin_number(self->clock_pin_number); - never_reset_pin_number(self->MOSI_pin_number); - never_reset_pin_number(self->MISO_pin_number); + if ( self->MOSI_pin_number != NO_PIN) { + never_reset_pin_number(self->MOSI_pin_number); + } + if ( self->MISO_pin_number != NO_PIN) { + never_reset_pin_number(self->MISO_pin_number); + } break; } } @@ -180,7 +184,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * if (miso != NULL) { config.miso_pin = miso->number; - self->MISO_pin_number = mosi->number; + self->MISO_pin_number = miso->number; claim_pin(miso); } else { self->MISO_pin_number = NO_PIN; @@ -204,8 +208,12 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { nrfx_spim_uninit(&self->spim_peripheral->spim); reset_pin_number(self->clock_pin_number); - reset_pin_number(self->MOSI_pin_number); - reset_pin_number(self->MISO_pin_number); + if ( self->MOSI_pin_number != NO_PIN) { + reset_pin_number(self->MOSI_pin_number); + } + if ( self->MISO_pin_number != NO_PIN) { + reset_pin_number(self->MISO_pin_number); + } } bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { From 2690faec4313bf36d83374ccbfab02f6abbef6dc Mon Sep 17 00:00:00 2001 From: root Date: Sat, 5 Sep 2020 11:42:06 -0500 Subject: [PATCH 2/2] Moved checks for invalid pin to common-hal/microcontroller/Pin.c --- .../atmel-samd/common-hal/microcontroller/Pin.c | 4 ++++ ports/esp32s2/common-hal/microcontroller/Pin.c | 6 ++++++ ports/nrf/common-hal/busio/SPI.c | 16 ++++------------ ports/nrf/common-hal/microcontroller/Pin.c | 3 +++ ports/stm/common-hal/microcontroller/Pin.c | 7 +++++++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ports/atmel-samd/common-hal/microcontroller/Pin.c b/ports/atmel-samd/common-hal/microcontroller/Pin.c index 5ce077eb86c3..ef1d8fffaa2d 100644 --- a/ports/atmel-samd/common-hal/microcontroller/Pin.c +++ b/ports/atmel-samd/common-hal/microcontroller/Pin.c @@ -106,6 +106,10 @@ void reset_all_pins(void) { } void never_reset_pin_number(uint8_t pin_number) { + if (pin_number >= PORT_BITS) { + return; + } + never_reset_pins[GPIO_PORT(pin_number)] |= 1 << GPIO_PIN(pin_number); } diff --git a/ports/esp32s2/common-hal/microcontroller/Pin.c b/ports/esp32s2/common-hal/microcontroller/Pin.c index 03a83cfe6799..546dca848c5b 100644 --- a/ports/esp32s2/common-hal/microcontroller/Pin.c +++ b/ports/esp32s2/common-hal/microcontroller/Pin.c @@ -45,6 +45,9 @@ bool apa102_mosi_in_use; bool apa102_sck_in_use; void never_reset_pin_number(gpio_num_t pin_number) { + if (pin_number == -1 ) { + return; + } never_reset_pins[pin_number / 32] |= 1 << pin_number % 32; } @@ -54,6 +57,9 @@ void common_hal_never_reset_pin(const mcu_pin_obj_t* pin) { // Mark pin as free and return it to a quiescent state. void reset_pin_number(gpio_num_t pin_number) { + if (pin_number == -1 ) { + return; + } never_reset_pins[pin_number / 32] &= ~(1 << pin_number % 32); in_use[pin_number / 32] &= ~(1 << pin_number % 32); diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index d7d0e0fe81ae..1cf074955f9c 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -104,12 +104,8 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { never_reset[i] = true; never_reset_pin_number(self->clock_pin_number); - if ( self->MOSI_pin_number != NO_PIN) { - never_reset_pin_number(self->MOSI_pin_number); - } - if ( self->MISO_pin_number != NO_PIN) { - never_reset_pin_number(self->MISO_pin_number); - } + never_reset_pin_number(self->MOSI_pin_number); + never_reset_pin_number(self->MISO_pin_number); break; } } @@ -208,12 +204,8 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { nrfx_spim_uninit(&self->spim_peripheral->spim); reset_pin_number(self->clock_pin_number); - if ( self->MOSI_pin_number != NO_PIN) { - reset_pin_number(self->MOSI_pin_number); - } - if ( self->MISO_pin_number != NO_PIN) { - reset_pin_number(self->MISO_pin_number); - } + reset_pin_number(self->MOSI_pin_number); + reset_pin_number(self->MISO_pin_number); } bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { diff --git a/ports/nrf/common-hal/microcontroller/Pin.c b/ports/nrf/common-hal/microcontroller/Pin.c index d294c1dd5d18..bc7ff831a563 100644 --- a/ports/nrf/common-hal/microcontroller/Pin.c +++ b/ports/nrf/common-hal/microcontroller/Pin.c @@ -123,6 +123,9 @@ void reset_pin_number(uint8_t pin_number) { void never_reset_pin_number(uint8_t pin_number) { + if (pin_number == NO_PIN) { + return; + } never_reset_pins[nrf_pin_port(pin_number)] |= 1 << nrf_relative_pin_number(pin_number); } diff --git a/ports/stm/common-hal/microcontroller/Pin.c b/ports/stm/common-hal/microcontroller/Pin.c index 9fbdedeade19..99651327030b 100644 --- a/ports/stm/common-hal/microcontroller/Pin.c +++ b/ports/stm/common-hal/microcontroller/Pin.c @@ -70,6 +70,10 @@ void reset_all_pins(void) { // Mark pin as free and return it to a quiescent state. void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { + if ( pin_number == NO_PIN ) { + return; + } + if (pin_port == 0x0F) { return; } @@ -88,6 +92,9 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { } void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) { + if ( pin_number == NO_PIN ) { + return; + } never_reset_pins[pin_port] |= 1<