Merge pull request #972 from arturo182/nrf_i2c

nrf: Rewrite the I2C common-hal using nrfx
This commit is contained in:
Scott Shawcroft 2018-07-02 14:33:06 -07:00 committed by GitHub
commit 05a088b369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 131 deletions

View File

@ -97,6 +97,7 @@ SRC_HAL = $(addprefix hal/,\
SRC_NRFX = $(addprefix nrfx/,\ SRC_NRFX = $(addprefix nrfx/,\
drivers/src/nrfx_spim.c \ drivers/src/nrfx_spim.c \
drivers/src/nrfx_twim.c \
) )
SRC_C += \ SRC_C += \

View File

@ -5,6 +5,7 @@
* *
* Copyright (c) 2016 Sandeep Mistry All right reserved. * Copyright (c) 2016 Sandeep Mistry All right reserved.
* Copyright (c) 2017 hathach * Copyright (c) 2017 hathach
* Copyright (c) 2018 Artur Pacholec
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -29,67 +30,103 @@
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "pins.h" #include "nrfx_twim.h"
#include "nrf.h" #include "nrf_gpio.h"
#define INST_NO 0
static uint8_t twi_error_to_mp(const nrfx_err_t err) {
switch (err) {
case NRFX_ERROR_DRV_TWI_ERR_ANACK:
return MP_ENODEV;
case NRFX_ERROR_BUSY:
return MP_EBUSY;
case NRFX_ERROR_DRV_TWI_ERR_DNACK:
case NRFX_ERROR_INVALID_ADDR:
return MP_EIO;
default:
break;
}
return 0;
}
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->pin == sda->pin) { if (scl->pin == sda->pin)
mp_raise_ValueError("Invalid pins"); mp_raise_ValueError("Invalid pins");
}
NRF_GPIO->PIN_CNF[scl->pin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) const nrfx_twim_t instance = NRFX_TWIM_INSTANCE(INST_NO);
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) self->twim = instance;
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[sda->pin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG;
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) config.scl = NRF_GPIO_PIN_MAP(scl->port, scl->pin);
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) config.sda = NRF_GPIO_PIN_MAP(sda->port, sda->pin);
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
// 1 for I2C, 0 for SPI
self->twi = NRF_TWIM1;
// change freq. only if it's less than the default 400K
if (frequency < 100000) { if (frequency < 100000) {
self->twi->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100; config.frequency = NRF_TWIM_FREQ_100K;
} else if (frequency < 250000) { } else if (frequency < 250000) {
self->twi->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250; config.frequency = NRF_TWIM_FREQ_250K;
}else {
self->twi->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400;
} }
self->twi->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos); nrfx_err_t err = nrfx_twim_init(&self->twim, &config, NULL, NULL);
self->twi->PSEL.SCL = scl->pin; // A soft reset doesn't uninit the driver so we might end up with a invalid state
self->twi->PSEL.SDA = sda->pin; if (err == NRFX_ERROR_INVALID_STATE) {
nrfx_twim_uninit(&self->twim);
err = nrfx_twim_init(&self->twim, &config, NULL, NULL);
}
if (err != NRFX_SUCCESS)
mp_raise_OSError(MP_EIO);
self->inited = true;
} }
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return self->twi->ENABLE == 0; return !self->inited;
} }
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { 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);
self->inited = false;
} }
uint8_t scl_pin = self->twi->PSEL.SCL; // nrfx_twim_tx doesn't support 0-length data so we fall back to the hal API
uint8_t sda_pin = self->twi->PSEL.SDA;
self->twi->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
self->twi->PSEL.SCL = (TWIM_PSEL_SCL_CONNECT_Disconnected << TWIM_PSEL_SCL_CONNECT_Pos);
self->twi->PSEL.SDA = (TWIM_PSEL_SDA_CONNECT_Disconnected << TWIM_PSEL_SDA_CONNECT_Pos);
reset_pin(scl_pin);
reset_pin(sda_pin);
}
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) {
// Write no data when just probing NRF_TWIM_Type *reg = self->twim.p_twim;
return 0 == common_hal_busio_i2c_write(self, addr, NULL, 0, true); bool found = true;
nrfx_twim_enable(&self->twim);
nrf_twim_address_set(reg, addr);
nrf_twim_tx_buffer_set(reg, NULL, 0);
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_RESUME);
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_STARTTX);
while (nrf_twim_event_check(reg, NRF_TWIM_EVENT_TXSTARTED) == 0 &&
nrf_twim_event_check(reg, NRF_TWIM_EVENT_ERROR) == 0);
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_TXSTARTED);
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_STOP);
while (nrf_twim_event_check(reg, NRF_TWIM_EVENT_STOPPED) == 0);
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_STOPPED);
if (nrf_twim_event_check(reg, NRF_TWIM_EVENT_ERROR)) {
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_ERROR);
nrf_twim_errorsrc_get_and_clear(reg);
found = false;
}
nrfx_twim_disable(&self->twim);
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) {
@ -112,96 +149,23 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
} }
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool stopBit) { uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool stopBit) {
NRF_TWIM_Type* twi = self->twi; if(len == 0)
return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV;
twi->ADDRESS = addr; nrfx_twim_enable(&self->twim);
twi->TASKS_RESUME = 1; const nrfx_err_t err = nrfx_twim_tx(&self->twim, addr, data, len, !stopBit);
nrfx_twim_disable(&self->twim);
twi->TXD.PTR = (uint32_t) data; return twi_error_to_mp(err);
twi->TXD.MAXCNT = len;
twi->TASKS_STARTTX = 1;
// Wait for TX started
while(!twi->EVENTS_TXSTARTED && !twi->EVENTS_ERROR) {}
twi->EVENTS_TXSTARTED = 0;
// Wait for TX complete
if ( len )
{
while(!twi->EVENTS_LASTTX && !twi->EVENTS_ERROR) {}
twi->EVENTS_LASTTX = 0x0UL;
}
if (stopBit || twi->EVENTS_ERROR)
{
twi->TASKS_STOP = 0x1UL;
while(!twi->EVENTS_STOPPED);
twi->EVENTS_STOPPED = 0x0UL;
}
else
{
twi->TASKS_SUSPEND = 0x1UL;
while(!twi->EVENTS_SUSPENDED);
twi->EVENTS_SUSPENDED = 0x0UL;
}
if (twi->EVENTS_ERROR)
{
twi->EVENTS_ERROR = 0x0UL;
uint32_t error = twi->ERRORSRC;
twi->ERRORSRC = error;
return error;
}
return 0;
} }
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) { uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) {
NRF_TWIM_Type* twi = self->twi; if(len == 0)
if(len == 0) return 0;
bool stopBit = true; // should be a parameter
twi->ADDRESS = addr;
twi->TASKS_RESUME = 0x1UL;
twi->RXD.PTR = (uint32_t) data;
twi->RXD.MAXCNT = len;
twi->TASKS_STARTRX = 0x1UL;
while(!twi->EVENTS_RXSTARTED && !twi->EVENTS_ERROR);
twi->EVENTS_RXSTARTED = 0x0UL;
while(!twi->EVENTS_LASTRX && !twi->EVENTS_ERROR);
twi->EVENTS_LASTRX = 0x0UL;
if (stopBit || twi->EVENTS_ERROR)
{
twi->TASKS_STOP = 0x1UL;
while(!twi->EVENTS_STOPPED);
twi->EVENTS_STOPPED = 0x0UL;
}
else
{
twi->TASKS_SUSPEND = 0x1UL;
while(!twi->EVENTS_SUSPENDED);
twi->EVENTS_SUSPENDED = 0x0UL;
}
if (twi->EVENTS_ERROR)
{
twi->EVENTS_ERROR = 0x0UL;
uint32_t error = twi->ERRORSRC;
twi->ERRORSRC = error;
return error;
}
// number of byte read
// (void) _p_twim->RXD.AMOUNT;
return 0; return 0;
nrfx_twim_enable(&self->twim);
const nrfx_err_t err = nrfx_twim_rx(&self->twim, addr, data, len);
nrfx_twim_disable(&self->twim);
return twi_error_to_mp(err);
} }

View File

@ -27,16 +27,15 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H #ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H #define MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H
#include "common-hal/microcontroller/Pin.h" #include "nrfx_twim.h"
//#include "hal/include/hal_i2c_m_sync.h"
#include "py/obj.h" #include "py/obj.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
volatile bool has_lock; nrfx_twim_t twim;
NRF_TWIM_Type* twi; bool inited;
bool has_lock;
} busio_i2c_obj_t; } busio_i2c_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H

View File

@ -1,6 +1,7 @@
#ifndef NRFX_CONFIG_H__ #ifndef NRFX_CONFIG_H__
#define NRFX_CONFIG_H__ #define NRFX_CONFIG_H__
// SPI
#define NRFX_SPIM_ENABLED 1 #define NRFX_SPIM_ENABLED 1
#ifdef NRF52840_XXAA #ifdef NRF52840_XXAA
@ -12,4 +13,12 @@
#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
#define NRFX_TWIM_ENABLED 1
#define NRFX_TWIM0_ENABLED 1
#define NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY 7
#define NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY NRF_TWIM_FREQ_400K
#define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0
#endif #endif