allow multiple I2C and SPI; improve nrfx_config.h

This commit is contained in:
Dan Halbert 2018-09-20 20:45:30 -04:00
parent 2309e60c0d
commit 2c15d12f2f
4 changed files with 96 additions and 39 deletions

View File

@ -34,8 +34,27 @@
#include "nrfx_twim.h" #include "nrfx_twim.h"
#include "nrf_gpio.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 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) { static uint8_t twi_error_to_mp(const nrfx_err_t err) {
switch (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) { 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")); mp_raise_ValueError(translate("Invalid pins"));
}
const nrfx_twim_t instance = NRFX_TWIM_INSTANCE(INST_NO); // Find a free instance.
self->twim = 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; nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG;
config.scl = scl->number; 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(sda);
claim_pin(scl); 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 // A soft reset doesn't uninit the driver so we might end up with a invalid state
if (err == NRFX_ERROR_INVALID_STATE) { if (err == NRFX_ERROR_INVALID_STATE) {
nrfx_twim_uninit(&self->twim); nrfx_twim_uninit(&self->twim_peripheral->twim);
err = nrfx_twim_init(&self->twim, &config, NULL, NULL); err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL);
} }
if (err != NRFX_SUCCESS) { 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)) if (common_hal_busio_i2c_deinited(self))
return; return;
nrfx_twim_uninit(&self->twim); nrfx_twim_uninit(&self->twim_peripheral->twim);
reset_pin_number(self->sda_pin_number); reset_pin_number(self->sda_pin_number);
reset_pin_number(self->scl_pin_number); reset_pin_number(self->scl_pin_number);
self->sda_pin_number = NO_PIN; self->sda_pin_number = NO_PIN;
self->scl_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 // 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) { 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; bool found = true;
nrfx_twim_enable(&self->twim); nrfx_twim_enable(&self->twim_peripheral->twim);
nrf_twim_address_set(reg, addr); nrf_twim_address_set(reg, addr);
nrf_twim_tx_buffer_set(reg, NULL, 0); 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; found = false;
} }
nrfx_twim_disable(&self->twim); nrfx_twim_disable(&self->twim_peripheral->twim);
return found; return found;
} }
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
bool grabbed_lock = false; bool grabbed_lock = false;
// CRITICAL_SECTION_ENTER() // NRFX_CRITICAL_SECTION_ENTER();
if (!self->has_lock) { if (!self->has_lock) {
grabbed_lock = true; grabbed_lock = true;
self->has_lock = true; self->has_lock = true;
} }
// CRITICAL_SECTION_LEAVE(); // NRFX_CRITICAL_SECTION_EXIT();
return grabbed_lock; 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) if(len == 0)
return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV; return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV;
const uint32_t parts = len / MAX_XFER_SIZE; const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size;
const uint32_t remainder = len % 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_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) { 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) if (err != NRFX_SUCCESS)
break; break;
} }
if ((remainder > 0) && (err == NRFX_SUCCESS)) 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); 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) if(len == 0)
return 0; return 0;
const uint32_t parts = len / MAX_XFER_SIZE; const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size;
const uint32_t remainder = len % 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_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) { 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) if (err != NRFX_SUCCESS)
break; break;
} }
if ((remainder > 0) && (err == NRFX_SUCCESS)) 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); return twi_error_to_mp(err);
} }

View File

@ -32,8 +32,14 @@
#include "py/obj.h" #include "py/obj.h"
typedef struct { typedef struct {
mp_obj_base_t base;
nrfx_twim_t twim; 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; bool has_lock;
uint8_t scl_pin_number; uint8_t scl_pin_number;
uint8_t sda_pin_number; uint8_t sda_pin_number;

View File

@ -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 common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
bool grabbed_lock = false; bool grabbed_lock = false;
// CRITICAL_SECTION_ENTER() // NRFX_CRITICAL_SECTION_ENTER();
// if (!self->has_lock) { if (!self->has_lock) {
grabbed_lock = true; grabbed_lock = true;
self->has_lock = true; self->has_lock = true;
// } }
// CRITICAL_SECTION_LEAVE(); // NRFX_CRITICAL_SECTION_EXIT();
return grabbed_lock; return grabbed_lock;
} }

View File

@ -5,11 +5,27 @@
#define NRFX_POWER_ENABLED 1 #define NRFX_POWER_ENABLED 1
#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7 #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 // SPI
#define NRFX_SPIM_ENABLED 1 #define NRFX_SPIM_ENABLED 1
#define NRFX_SPIM0_ENABLED 1 // TWIM0 and TWIM1 are the same peripherals as SPIM0 and SPIM1.
#define NRFX_SPIM1_ENABLED 1 // 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 #define NRFX_SPIM2_ENABLED 1
#ifdef NRF52840_XXAA #ifdef NRF52840_XXAA
#define NRFX_SPIM3_ENABLED 1 #define NRFX_SPIM3_ENABLED 1
@ -19,7 +35,7 @@
#define NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY 7 #define NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY 7
#define NRFX_SPIM_MISO_PULL_CFG 1 #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_TWIM_ENABLED 1
#define NRFX_TWIM0_ENABLED 1 #define NRFX_TWIM0_ENABLED 1
#define NRFX_TWIM1_ENABLED 1 #define NRFX_TWIM1_ENABLED 1