From 48a3aafdd2152b53efa7ce00116796cbf92f59f6 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 25 Sep 2018 15:12:10 -0400 Subject: [PATCH] reset I2C and SPI on ctrl-D --- ports/nrf/common-hal/busio/I2C.c | 6 +++++- ports/nrf/common-hal/busio/I2C.h | 2 ++ ports/nrf/common-hal/busio/SPI.c | 6 ++++++ ports/nrf/common-hal/busio/SPI.h | 2 ++ ports/nrf/supervisor/port.c | 11 ++++++++--- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ports/nrf/common-hal/busio/I2C.c b/ports/nrf/common-hal/busio/I2C.c index ad789c8c18..c2a6cab702 100644 --- a/ports/nrf/common-hal/busio/I2C.c +++ b/ports/nrf/common-hal/busio/I2C.c @@ -54,7 +54,11 @@ STATIC twim_peripheral_t twim_peripherals[] = { #endif }; -#define INST_NO 0 +void i2c_reset(void) { + for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) { + twim_peripherals[i].in_use = false; + } +} static uint8_t twi_error_to_mp(const nrfx_err_t err) { switch (err) { diff --git a/ports/nrf/common-hal/busio/I2C.h b/ports/nrf/common-hal/busio/I2C.h index 5606f46b0e..c8ba84418f 100644 --- a/ports/nrf/common-hal/busio/I2C.h +++ b/ports/nrf/common-hal/busio/I2C.h @@ -45,4 +45,6 @@ typedef struct { uint8_t sda_pin_number; } busio_i2c_obj_t; +void i2c_reset(void); + #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index 6e7f33dc73..54ae6e47c5 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -59,6 +59,12 @@ STATIC spim_peripheral_t spim_peripherals[] = { #endif }; +void spi_reset(void) { + for (size_t i = 0 ; i < MP_ARRAY_SIZE(spim_peripherals); i++) { + nrfx_spim_uninit(&spim_peripherals[i].spim); + } +} + // Convert frequency to clock-speed-dependent value static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate) { if (baudrate <= 125000) { diff --git a/ports/nrf/common-hal/busio/SPI.h b/ports/nrf/common-hal/busio/SPI.h index c05fb1e18f..1b0de8acfd 100644 --- a/ports/nrf/common-hal/busio/SPI.h +++ b/ports/nrf/common-hal/busio/SPI.h @@ -45,4 +45,6 @@ typedef struct { uint8_t MISO_pin_number; } busio_spi_obj_t; +void spi_reset(void); + #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index e6bb254583..c858a36485 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -34,6 +34,8 @@ #include "shared-module/gamepad/__init__.h" #include "common-hal/microcontroller/Pin.h" +#include "common-hal/busio/I2C.h" +#include "common-hal/busio/SPI.h" #include "common-hal/pulseio/PWMOut.h" #include "tick.h" @@ -72,11 +74,14 @@ safe_mode_t port_init(void) { } void reset_port(void) { - #ifdef CIRCUITPY_GAMEPAD_TICKS - gamepad_reset(); - #endif +#ifdef CIRCUITPY_GAMEPAD_TICKS + gamepad_reset(); +#endif + i2c_reset(); + spi_reset(); pwmout_reset(); + reset_all_pins(); }