1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #6142 from haukepetersen/add_nrf51_i2c

cpu/nrf51: added I2C driver implementation
This commit is contained in:
Peter Kietzmann 2017-01-10 15:25:18 +01:00 committed by GitHub
commit dcfd0d656b
31 changed files with 430 additions and 42 deletions

View File

@ -4,6 +4,7 @@ FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_flashpage
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -94,6 +94,28 @@ static const timer_conf_t timer_config[] = {
#define SPI_0_PIN_SCK 15
/** @} */
/**
* @brief I2C (TWI) configuration
* @{
*/
static const i2c_conf_t i2c_config[] = {
{
.dev = NRF_TWI0,
.pin_scl = 7,
.pin_sda = 8,
.ppi = 0
},
{
.dev = NRF_TWI1,
.pin_scl = 9,
.pin_sda = 10,
.ppi = 1
}
};
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0]))
/** @} */
/**
* @brief ADC configuration
*

View File

@ -2,6 +2,7 @@
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -96,6 +96,22 @@ static const timer_conf_t timer_config[] = {
#define UART_PIN_TX 24
/** @} */
/**
* @brief I2C (TWI) configuration
* @{
*/
static const i2c_conf_t i2c_config[] = {
{
.dev = NRF_TWI0,
.pin_scl = 0,
.pin_sda = 30,
.ppi = 0
}
};
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0]))
/** @} */
/**
* @brief ADC configuration
*

View File

@ -3,6 +3,7 @@ FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_flashpage
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

View File

@ -96,6 +96,22 @@ static const timer_conf_t timer_config[] = {
#define UART_PIN_TX 24
/** @} */
/**
* @brief I2C (TWI) configuration
* @{
*/
static const i2c_conf_t i2c_config[] = {
{
.dev = NRF_TWI0,
.pin_scl = 0,
.pin_sda = 30,
.ppi = 0
}
};
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0]))
/** @} */
/**
* @brief ADC configuration
*

View File

@ -4,6 +4,7 @@ FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_flashpage
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer

View File

@ -99,6 +99,22 @@ static const timer_conf_t timer_config[] = {
#define SPI_1_PIN_SCK 22
/** @} */
/**
* @brief I2C (TWI) configuration
* @{
*/
static const i2c_conf_t i2c_config[] = {
{
.dev = NRF_TWI0,
.pin_scl = 23,
.pin_sda = 24,
.ppi = 0
}
};
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0]))
/** @} */
/**
* @brief ADC configuration
*

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2015-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_nrf51
* @{
*
* @file
* @brief nRF51 specific definitions for handling peripherals
*
* @author Hauke Petersen <hauke.peterse@fu-berlin.de>
*/
#ifndef PERIPH_CPU_
#define PERIPH_CPU_
#include "periph_cpu_common.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Redefine some peripheral names to unify them between nRF51 and 52
* @{
*/
#define GPIO_BASE (NRF_GPIO)
#define UART_IRQN (UART0_IRQn)
/** @} */
/**
* @brief Override I2C speed settings
* @{
*/
#define HAVE_I2C_SPEED_T
typedef enum {
I2C_SPEED_LOW = 0x01, /**< not supported */
I2C_SPEED_NORMAL = TWI_FREQUENCY_FREQUENCY_K100, /**< 100kbit/s */
I2C_SPEED_FAST = TWI_FREQUENCY_FREQUENCY_K400, /**< 400kbit/s */
I2C_SPEED_FAST_PLUS = 0x02, /**< not supported */
I2C_SPEED_HIGH = 0x03, /**< not supported */
} i2c_speed_t;
/** @} */
/**
* @brief I2C (TWI) configuration options
*/
typedef struct {
NRF_TWI_Type *dev; /**< hardware device */
uint8_t pin_scl; /**< SCL pin */
uint8_t pin_sda; /**< SDA pin */
uint8_t ppi; /**< PPI channel to use */
} i2c_conf_t;
#ifdef __cplusplus
}
#endif
#endif /* PERIPH_CPU_ */
/** @} */

224
cpu/nrf51/periph/i2c.c Normal file
View File

@ -0,0 +1,224 @@
/*
* Copyright (C) 2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup cpu_nrf51
* @{
*
* @file
* @brief Low-level I2C driver implementation
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/i2c.h"
#include "periph_conf.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#ifdef I2C_NUMOF
/**
* @brief If any of the 4 lower bits are set, the speed value is invalid
*/
#define INVALID_SPEED_MASK (0x0f)
/**
* @brief Initialized bus locks (we have a maximum of two devices...)
*/
static mutex_t locks[] = {
MUTEX_INIT,
MUTEX_INIT
};
static inline NRF_TWI_Type *dev(i2c_t bus)
{
return i2c_config[bus].dev;
}
static int error(i2c_t bus)
{
DEBUG("[i2c] error 0x%02x\n", (int)dev(bus)->ERRORSRC);
dev(bus)->ERRORSRC = (TWI_ERRORSRC_DNACK_Clear |
TWI_ERRORSRC_ANACK_Clear |
TWI_ERRORSRC_OVERRUN_Clear);
dev(bus)->EVENTS_ERROR = 0;
return -1;
}
static int write(i2c_t bus, uint8_t addr, const void *data, int len, int stop)
{
uint8_t *buf = (uint8_t *)data;
assert((bus <= I2C_NUMOF) && (len > 0));
DEBUG("i2c: writing %i byte to the bus\n", len);
dev(bus)->ADDRESS = (addr & 0x7f);
for (int i = 0; i < len; i++) {
dev(bus)->TXD = *buf++;
dev(bus)->EVENTS_TXDSENT = 0;
dev(bus)->TASKS_STARTTX = 1;
while (!(dev(bus)->EVENTS_TXDSENT) && !(dev(bus)->EVENTS_ERROR)) {}
if (dev(bus)->EVENTS_ERROR) {
return error(bus);
}
}
if (stop) {
dev(bus)->EVENTS_STOPPED = 0;
dev(bus)->TASKS_STOP = 1;
while (!(dev(bus)->EVENTS_STOPPED) && !(dev(bus)->EVENTS_ERROR)) {}
if (dev(bus)->EVENTS_ERROR) {
return error(bus);
}
}
return len;
}
int i2c_init_master(i2c_t bus, i2c_speed_t speed)
{
if (bus >= I2C_NUMOF) {
return -1;
}
if (speed & INVALID_SPEED_MASK) {
return -2;
}
/* power on the bus */
dev(bus)->POWER = TWI_POWER_POWER_Enabled;
/* pin configuration */
GPIO_BASE->PIN_CNF[i2c_config[bus].pin_scl] = (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos);
GPIO_BASE->PIN_CNF[i2c_config[bus].pin_scl] = (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos);
dev(bus)->PSELSCL = i2c_config[bus].pin_scl;
dev(bus)->PSELSDA = i2c_config[bus].pin_sda;
NRF_PPI->CHENCLR = (1 << i2c_config[bus].ppi);
NRF_PPI->CH[i2c_config[bus].ppi].EEP = (uint32_t)&dev(bus)->EVENTS_BB;
/* bus clock speed configuration */
dev(bus)->FREQUENCY = speed;
/* enable the device */
dev(bus)->ENABLE = TWI_ENABLE_ENABLE_Enabled;
return 0;
}
int i2c_acquire(i2c_t bus)
{
assert(bus <= I2C_NUMOF);
mutex_lock(&locks[bus]);
return 0;
}
int i2c_release(i2c_t bus)
{
assert(bus <= I2C_NUMOF);
mutex_unlock(&locks[bus]);
return 0;
}
int i2c_read_byte(i2c_t bus, uint8_t address, void *data)
{
return i2c_read_bytes(bus, address, data, 1);
}
int i2c_read_bytes(i2c_t bus, uint8_t address, void *data, int length)
{
uint8_t *in_buf = (uint8_t *)data;
assert((bus <= I2C_NUMOF) && (length > 0));
DEBUG("[i2c] reading %i byte from the bus\n", length);
/* set the client address */
dev(bus)->ADDRESS = (address & 0x7f);
/* setup PPI channel as alternative to the broken SHORTS
* -> see PAN notice #36: "Shortcuts described in nRF51 Reference Manual are
* not functional." */
if (length == 1) {
NRF_PPI->CH[i2c_config[bus].ppi].TEP = (uint32_t)&dev(bus)->TASKS_STOP;
}
else {
NRF_PPI->CH[i2c_config[bus].ppi].TEP = (uint32_t)&dev(bus)->TASKS_SUSPEND;
}
NRF_PPI->CHENSET = (1 << i2c_config[bus].ppi);
dev(bus)->EVENTS_RXDREADY = 0;
dev(bus)->EVENTS_STOPPED = 0;
dev(bus)->TASKS_STARTRX = 1;
for (int i = (length - 1); i >= 0; i--) {
while (!(dev(bus)->EVENTS_RXDREADY) && !(dev(bus)->EVENTS_ERROR)) {}
if (dev(bus)->EVENTS_ERROR) {
return error(bus);
}
*in_buf++ = (uint8_t)dev(bus)->RXD;
if (i == 1) {
NRF_PPI->CH[i2c_config[bus].ppi].TEP = (uint32_t)&dev(bus)->TASKS_STOP;
}
dev(bus)->EVENTS_RXDREADY = 0;
dev(bus)->TASKS_RESUME = 1;
}
/* wait for the device to finish up */
while (dev(bus)->EVENTS_STOPPED == 0) {}
NRF_PPI->CHENCLR = (1 << i2c_config[bus].ppi);
return length;
}
int i2c_read_reg(i2c_t bus, uint8_t address, uint8_t reg, void *data)
{
write(bus, address, &reg, 1, 0);
return i2c_read_bytes(bus, address, data, 1);
}
int i2c_read_regs(i2c_t bus, uint8_t address, uint8_t reg,
void *data, int length)
{
write(bus, address, &reg, 1, 0);
return i2c_read_bytes(bus, address, data, length);
}
int i2c_write_byte(i2c_t bus, uint8_t address, uint8_t data)
{
return write(bus, address, &data, 1, 1);
}
int i2c_write_bytes(i2c_t bus, uint8_t address, const void *data, int length)
{
return write(bus, address, data, length, 1);
}
int i2c_write_reg(i2c_t bus, uint8_t address, uint8_t reg, uint8_t data)
{
write(bus, address, &reg, 1, 0);
return write(bus, address, &data, 1, 1);
}
int i2c_write_regs(i2c_t bus, uint8_t address, uint8_t reg,
const void *data, int length)
{
write(bus, address, &reg, 1, 0);
return write(bus, address, data, length, 1);
}
#endif /* I2C_NUMOF */

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2015-2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_nrf52
* @{
*
* @file
* @brief nRF52 specific definitions for handling peripherals
*
* @author Hauke Petersen <hauke.peterse@fu-berlin.de>
*/
#ifndef PERIPH_CPU_
#define PERIPH_CPU_
#include "periph_cpu_common.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Redefine some peripheral names to unify them between nRF51 and 52
* @{
*/
#define GPIO_BASE (NRF_P0)
#define UART_IRQN (UARTE0_UART0_IRQn)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* PERIPH_CPU_ */
/** @} */

View File

@ -11,13 +11,13 @@
* @{
*
* @file
* @brief CPU specific definitions for handling peripherals
* @brief nRF5x common definitions for handling peripherals
*
* @author Hauke Petersen <hauke.peterse@fu-berlin.de>
*/
#ifndef CPU_PERIPH_H
#define CPU_PERIPH_H
#ifndef PERIPH_CPU_COMMON_H_
#define PERIPH_CPU_COMMON_H_
#include "cpu.h"
@ -25,22 +25,6 @@
extern "C" {
#endif
/**
* @brief Iron out some differences in register and IRQ channel naming between
* the different nRFx family members
* @{
*/
#if defined(CPU_FAM_NRF51)
#define GPIO_BASE (NRF_GPIO)
#define UART_IRQN (UART0_IRQn)
#elif defined(CPU_FAM_NRF52)
#define GPIO_BASE (NRF_P0)
#define UART_IRQN (UARTE0_UART0_IRQn)
#else
#error "nrf5x_common: no valid value for CPU_FAM_XX defined"
#endif
/** @} */
/**
* @brief Length of the CPU_ID in octets
*/
@ -116,15 +100,15 @@ typedef enum {
* @brief Timer configuration options
*/
typedef struct {
NRF_TIMER_Type *dev;
uint8_t channels;
uint8_t bitmode;
uint8_t irqn;
NRF_TIMER_Type *dev; /**< timer device */
uint8_t channels; /**< number of channels available */
uint8_t bitmode; /**< counter width */
uint8_t irqn; /**< IRQ number of the timer device */
} timer_conf_t;
#ifdef __cplusplus
}
#endif
#endif /* CPU_PERIPH_H */
#endif /* PERIPH_CPU_COMMON_H_ */
/** @} */

View File

@ -6,7 +6,7 @@ USEMODULE += xtimer
USEMODULE += printf_float
# set default device parameters in case they are undefined
TEST_I2C ?= I2C_0
TEST_I2C ?= I2C_DEV\(0\)
TEST_MEASURE_OVERSAMPLING ?= BMP180_ULTRALOWPOWER
TEST_ALTITUDE ?= 158 # altitude in Polytechnique School campus

View File

@ -7,7 +7,7 @@ USEMODULE += hdc1000
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_HDC1000_I2C ?= I2C_0
TEST_HDC1000_I2C ?= I2C_DEV\(0\)
TEST_HDC1000_ADDR ?= 0x43
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += hih6130
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_HIH6130_I2C ?= I2C_0
TEST_HIH6130_I2C ?= I2C_DEV\(0\)
TEST_HIH6130_ADDR ?= 0x27
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += ina220
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_INA220_I2C ?= I2C_0
TEST_INA220_I2C ?= I2C_DEV\(0\)
TEST_INA220_ADDR ?= 0x40
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += isl29020
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_ISL29020_I2C ?= I2C_0
TEST_ISL29020_I2C ?= I2C_DEV\(0\)
TEST_ISL29020_ADDR ?= 68
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += isl29125
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_ISL29125_I2C ?= I2C_0
TEST_ISL29125_I2C ?= I2C_DEV\(0\)
TEST_ISL29125_IRQ_PIN ?= GPIO_PIN\(0,0\)
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += l3g4200d
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_L3G4200D_I2C ?= I2C_0
TEST_L3G4200D_I2C ?= I2C_DEV\(0\)
TEST_L3G4200D_ADDR ?= 104
TEST_L3G4200D_INT ?= GPIO_PIN\(0,0\)
TEST_L3G4200D_DRDY ?= GPIO_PIN\(0,1\)

View File

@ -13,7 +13,7 @@ ifneq (,$(TEST_LIS3MDL_I2C))
CFLAGS += -DTEST_LIS3MDL_I2C=$(TEST_LIS3MDL_I2C)
else
# set random default
CFLAGS += -DTEST_LIS3MDL_I2C=I2C_1
CFLAGS += -DTEST_LIS3MDL_I2C=I2C_DEV\(1\)
endif
ifneq (,$(TEST_LIS3MDL_MAG_ADDR))
CFLAGS += -DTEST_LIS3MDL_MAG_ADDR=$(TEST_LIS3MDL_MAG_ADDR)

View File

@ -7,7 +7,7 @@ USEMODULE += lps331ap
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_LPS331AP_I2C ?= I2C_0
TEST_LPS331AP_I2C ?= I2C_DEV\(0\)
TEST_LPS331AP_ADDR ?= 92
# export parameters

View File

@ -13,7 +13,7 @@ ifneq (,$(filter iotlab-m3,$(BOARD)))
endif
# set default device parameters in case they are undefined
TEST_LSM303DLHC_I2C ?= I2C_0
TEST_LSM303DLHC_I2C ?= I2C_DEV\(0\)
TEST_LSM303DLHC_ACC_ADDR ?= 25
TEST_LSM303DLHC_MAG_ADDR ?= 30
TEST_LSM303DLHC_ACC_PIN ?= GPIO_PIN\(0,0\)

View File

@ -7,7 +7,7 @@ USEMODULE += mag3110
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_MAG3110_I2C ?= I2C_0
TEST_MAG3110_I2C ?= I2C_DEV\(0\)
TEST_MAG3110_ADDR ?= 0x0E
TEST_MAG3110_USER_OFFSET_X ?= -2000
TEST_MAG3110_USER_OFFSET_Y ?= 180

View File

@ -7,7 +7,7 @@ USEMODULE += mma8652
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_MMA8652_I2C ?= I2C_0
TEST_MMA8652_I2C ?= I2C_DEV\(0\)
TEST_MMA8652_ADDR ?= 0x1D
TEST_MMA8652_USER_OFFSET_X ?= 0
TEST_MMA8652_USER_OFFSET_Y ?= 0

View File

@ -7,7 +7,7 @@ USEMODULE += mpl3115a2
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_MPL3115A2_I2C ?= I2C_0
TEST_MPL3115A2_I2C ?= I2C_DEV\(0\)
TEST_MPL3115A2_ADDR ?= 0x60
# export parameters

View File

@ -14,7 +14,7 @@ ifneq (,$(filter msbiot,$(BOARD)))
endif
# set default device parameters in case they are undefined
TEST_I2C ?= I2C_0
TEST_I2C ?= I2C_DEV\(0\)
TEST_HW_ADDR ?= MPU9150_HW_ADDR_HEX_68
TEST_COMP_ADDR ?= MPU9150_COMP_ADDR_HEX_0C

View File

@ -8,7 +8,7 @@ USEMODULE += srf02
USEMODULE += shell
# set default device parameters in case they are undefined
TEST_SRF02_I2C ?= I2C_0
TEST_SRF02_I2C ?= I2C_DEV\(0\)
TEST_MODE ?= SRF02_MODE_REAL_CM
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += xtimer
USEMODULE += srf08
# set default device parameters in case they are undefined
export TEST_SRF08_I2C ?= I2C_0
export TEST_SRF08_I2C ?= I2C_DEV\(0\)
export TEST_SRF08_SPEED ?= I2C_SPEED_NORMAL
export TEST_MODE ?= SRF08_MODE_CM
export TEST_NUM_ECHOS ?= 3

View File

@ -7,7 +7,7 @@ USEMODULE += tcs37727
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_TCS37727_I2C ?= I2C_0
TEST_TCS37727_I2C ?= I2C_DEV\(0\)
TEST_TCS37727_ADDR ?= 0x29
# export parameters

View File

@ -7,7 +7,7 @@ USEMODULE += tmp006
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_TMP006_I2C ?= I2C_0
TEST_TMP006_I2C ?= I2C_DEV\(0\)
TEST_TMP006_ADDR ?= 0x41
TEST_TMP006_CONFIG_CR ?= TMP006_CONFIG_CR_DEF