From 2c15d12f2f39be2612d2105c0a2e2d4cc430020d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 20 Sep 2018 20:45:30 -0400 Subject: [PATCH] allow multiple I2C and SPI; improve nrfx_config.h --- ports/nrf/common-hal/busio/I2C.c | 93 ++++++++++++++++++++++---------- ports/nrf/common-hal/busio/I2C.h | 8 ++- ports/nrf/common-hal/busio/SPI.c | 12 ++--- ports/nrf/nrfx_config.h | 22 ++++++-- 4 files changed, 96 insertions(+), 39 deletions(-) diff --git a/ports/nrf/common-hal/busio/I2C.c b/ports/nrf/common-hal/busio/I2C.c index 8ac76c304a..ad789c8c18 100644 --- a/ports/nrf/common-hal/busio/I2C.c +++ b/ports/nrf/common-hal/busio/I2C.c @@ -34,8 +34,27 @@ #include "nrfx_twim.h" #include "nrf_gpio.h" +#include "nrfx_spim.h" +#include "nrf_gpio.h" + +STATIC twim_peripheral_t twim_peripherals[] = { +#if NRFX_CHECK(NRFX_TWIM0_ENABLED) + // SPIM0 and TWIM0 share an address. + { .twim = NRFX_TWIM_INSTANCE(0), + .in_use = false, + .max_xfer_size = TWIM0_EASYDMA_MAXCNT_SIZE, + }, +#endif +#if NRFX_CHECK(NRFX_TWIM1_ENABLED) + // SPIM1 and TWIM1 share an address. + { .twim = NRFX_TWIM_INSTANCE(1), + .in_use = false, + .max_xfer_size = TWIM1_EASYDMA_MAXCNT_SIZE, + }, +#endif +}; + #define INST_NO 0 -#define MAX_XFER_SIZE ((1U << NRFX_CONCAT_3(TWIM, INST_NO, _EASYDMA_MAXCNT_SIZE)) - 1) static uint8_t twi_error_to_mp(const nrfx_err_t err) { switch (err) { @@ -54,11 +73,23 @@ static uint8_t twi_error_to_mp(const nrfx_err_t err) { } 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->number == sda->number) + if (scl->number == sda->number) { mp_raise_ValueError(translate("Invalid pins")); + } - const nrfx_twim_t instance = NRFX_TWIM_INSTANCE(INST_NO); - self->twim = instance; + // Find a free instance. + self->twim_peripheral = NULL; + for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) { + if (!twim_peripherals[i].in_use) { + self->twim_peripheral = &twim_peripherals[i]; + self->twim_peripheral->in_use = true; + break; + } + } + + if (self->twim_peripheral == NULL) { + mp_raise_ValueError(translate("All I2C peripherals are in use")); + } nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG; config.scl = scl->number; @@ -76,12 +107,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t * claim_pin(sda); claim_pin(scl); - nrfx_err_t err = nrfx_twim_init(&self->twim, &config, NULL, NULL); + nrfx_err_t err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL); // A soft reset doesn't uninit the driver so we might end up with a invalid state if (err == NRFX_ERROR_INVALID_STATE) { - nrfx_twim_uninit(&self->twim); - err = nrfx_twim_init(&self->twim, &config, NULL, NULL); + nrfx_twim_uninit(&self->twim_peripheral->twim); + err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL); } if (err != NRFX_SUCCESS) { @@ -99,20 +130,22 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { if (common_hal_busio_i2c_deinited(self)) return; - nrfx_twim_uninit(&self->twim); + nrfx_twim_uninit(&self->twim_peripheral->twim); reset_pin_number(self->sda_pin_number); reset_pin_number(self->scl_pin_number); self->sda_pin_number = NO_PIN; self->scl_pin_number = NO_PIN; + + self->twim_peripheral->in_use = false; } // nrfx_twim_tx doesn't support 0-length data so we fall back to the hal API bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { - NRF_TWIM_Type *reg = self->twim.p_twim; + NRF_TWIM_Type *reg = self->twim_peripheral->twim.p_twim; bool found = true; - nrfx_twim_enable(&self->twim); + nrfx_twim_enable(&self->twim_peripheral->twim); nrf_twim_address_set(reg, addr); nrf_twim_tx_buffer_set(reg, NULL, 0); @@ -135,19 +168,19 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { found = false; } - nrfx_twim_disable(&self->twim); + nrfx_twim_disable(&self->twim_peripheral->twim); return found; } bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { bool grabbed_lock = false; -// CRITICAL_SECTION_ENTER() - if (!self->has_lock) { - grabbed_lock = true; - self->has_lock = true; - } -// CRITICAL_SECTION_LEAVE(); + // NRFX_CRITICAL_SECTION_ENTER(); + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + // NRFX_CRITICAL_SECTION_EXIT(); return grabbed_lock; } @@ -163,22 +196,23 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const u if(len == 0) return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV; - const uint32_t parts = len / MAX_XFER_SIZE; - const uint32_t remainder = len % MAX_XFER_SIZE; + const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size; + const uint32_t parts = len / max_xfer_size; + const uint32_t remainder = len % max_xfer_size; nrfx_err_t err = NRFX_SUCCESS; - nrfx_twim_enable(&self->twim); + nrfx_twim_enable(&self->twim_peripheral->twim); for (uint32_t i = 0; i < parts; ++i) { - err = nrfx_twim_tx(&self->twim, addr, data + i * MAX_XFER_SIZE, MAX_XFER_SIZE, !stopBit); + err = nrfx_twim_tx(&self->twim_peripheral->twim, addr, data + i * max_xfer_size, max_xfer_size, !stopBit); if (err != NRFX_SUCCESS) break; } if ((remainder > 0) && (err == NRFX_SUCCESS)) - err = nrfx_twim_tx(&self->twim, addr, data + parts * MAX_XFER_SIZE, remainder, !stopBit); + err = nrfx_twim_tx(&self->twim_peripheral->twim, addr, data + parts * max_xfer_size, remainder, !stopBit); - nrfx_twim_disable(&self->twim); + nrfx_twim_disable(&self->twim_peripheral->twim); return twi_error_to_mp(err); } @@ -187,22 +221,23 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t if(len == 0) return 0; - const uint32_t parts = len / MAX_XFER_SIZE; - const uint32_t remainder = len % MAX_XFER_SIZE; + const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size; + const uint32_t parts = len / max_xfer_size; + const uint32_t remainder = len % max_xfer_size; nrfx_err_t err = NRFX_SUCCESS; - nrfx_twim_enable(&self->twim); + nrfx_twim_enable(&self->twim_peripheral->twim); for (uint32_t i = 0; i < parts; ++i) { - err = nrfx_twim_rx(&self->twim, addr, data + i * MAX_XFER_SIZE, MAX_XFER_SIZE); + err = nrfx_twim_rx(&self->twim_peripheral->twim, addr, data + i * max_xfer_size, max_xfer_size); if (err != NRFX_SUCCESS) break; } if ((remainder > 0) && (err == NRFX_SUCCESS)) - err = nrfx_twim_rx(&self->twim, addr, data + parts * MAX_XFER_SIZE, remainder); + err = nrfx_twim_rx(&self->twim_peripheral->twim, addr, data + parts * max_xfer_size, remainder); - nrfx_twim_disable(&self->twim); + nrfx_twim_disable(&self->twim_peripheral->twim); return twi_error_to_mp(err); } diff --git a/ports/nrf/common-hal/busio/I2C.h b/ports/nrf/common-hal/busio/I2C.h index a133ff071a..5606f46b0e 100644 --- a/ports/nrf/common-hal/busio/I2C.h +++ b/ports/nrf/common-hal/busio/I2C.h @@ -32,8 +32,14 @@ #include "py/obj.h" typedef struct { - mp_obj_base_t base; nrfx_twim_t twim; + bool in_use; + uint8_t max_xfer_size; +} twim_peripheral_t; + +typedef struct { + mp_obj_base_t base; + twim_peripheral_t* twim_peripheral; bool has_lock; uint8_t scl_pin_number; uint8_t sda_pin_number; diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index 21f80ff101..6e7f33dc73 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -186,12 +186,12 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, ui bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { bool grabbed_lock = false; -// CRITICAL_SECTION_ENTER() -// if (!self->has_lock) { - grabbed_lock = true; - self->has_lock = true; -// } -// CRITICAL_SECTION_LEAVE(); + // NRFX_CRITICAL_SECTION_ENTER(); + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + // NRFX_CRITICAL_SECTION_EXIT(); return grabbed_lock; } diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h index 36f2665a51..cc4d193277 100644 --- a/ports/nrf/nrfx_config.h +++ b/ports/nrf/nrfx_config.h @@ -5,11 +5,27 @@ #define NRFX_POWER_ENABLED 1 #define NRFX_POWER_CONFIG_IRQ_PRIORITY 7 +// Turn on nrfx supported workarounds for errata in Rev1/Rev2 of nRF52832 +#ifdef NRF52832_XXAA + #define NRFX_SPIS_NRF52_ANOMALY_109_WORKAROUND_ENABLED 1 +#endif + +// Turn on nrfx supported workarounds for errata in Rev1 of nRF52840 +#ifdef NRF52840_XXAA + #define NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED 1 +#endif + // SPI #define NRFX_SPIM_ENABLED 1 -#define NRFX_SPIM0_ENABLED 1 -#define NRFX_SPIM1_ENABLED 1 +// TWIM0 and TWIM1 are the same peripherals as SPIM0 and SPIM1. +// The IRQ handlers for these peripherals are set up at compile time, +// so out of the box TWIM0/SPIM0 and TWIM1/SPIM1 cannot be shared +// between common-hal/busio/I2C.c and SPI.c. +// We could write an interrupt handler that checks whether it's +// being used for SPI or I2C, but perhaps two I2C's and 1-2 SPI's are good enough for now. + +// Enable SPIM2 and SPIM3 (if available) #define NRFX_SPIM2_ENABLED 1 #ifdef NRF52840_XXAA #define NRFX_SPIM3_ENABLED 1 @@ -19,7 +35,7 @@ #define NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY 7 #define NRFX_SPIM_MISO_PULL_CFG 1 -// TWI aka. I2C +// TWI aka. I2C; enable TWIM0 and TWIM1 (no conflict with SPIM choices) #define NRFX_TWIM_ENABLED 1 #define NRFX_TWIM0_ENABLED 1 #define NRFX_TWIM1_ENABLED 1