2017-09-01 15:33:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 HAW Hamburg
|
2018-01-19 22:09:41 +01:00
|
|
|
* 2018 Freie Universität Berlin
|
2017-09-01 15:33:10 +02:00
|
|
|
*
|
2018-01-19 22:09:41 +01:00
|
|
|
* 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.
|
2017-09-01 15:33:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_nrf52
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2018-01-19 22:09:41 +01:00
|
|
|
* @brief Low-level I2C (TWI) peripheral driver implementation
|
2017-09-01 15:33:10 +02:00
|
|
|
*
|
|
|
|
* @author Dimitri Nahm <dimitri.nahm@haw-hamburg.de>
|
2018-01-19 22:09:41 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2017-09-01 15:33:10 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-09-01 15:33:10 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "periph/i2c.h"
|
2018-01-19 22:09:41 +01:00
|
|
|
#include "periph/gpio.h"
|
2017-09-01 15:33:10 +02:00
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/**
|
2018-01-19 22:09:41 +01:00
|
|
|
* @brief If any of the 8 lower bits are set, the speed value is invalid
|
2017-09-01 15:33:10 +02:00
|
|
|
*/
|
2018-01-19 22:09:41 +01:00
|
|
|
#define INVALID_SPEED_MASK (0xff)
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-30 19:07:26 +01:00
|
|
|
/**
|
|
|
|
* @brief Initialized bus locks (we have a maximum of two devices...)
|
|
|
|
*/
|
|
|
|
static mutex_t locks[] = {
|
|
|
|
MUTEX_INIT,
|
|
|
|
MUTEX_INIT
|
|
|
|
};
|
2017-09-01 15:33:10 +02:00
|
|
|
|
|
|
|
static inline NRF_TWIM_Type *dev(i2c_t bus)
|
|
|
|
{
|
|
|
|
return i2c_config[bus].dev;
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
static int finish(i2c_t bus, int len)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
DEBUG("[i2c] waiting for STOPPED or ERROR event\n");
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
while ((!(dev(bus)->EVENTS_STOPPED)) && (!(dev(bus)->EVENTS_ERROR))) {
|
|
|
|
nrf52_sleep();
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
if ((dev(bus)->EVENTS_STOPPED)) {
|
|
|
|
dev(bus)->EVENTS_STOPPED = 0;
|
|
|
|
DEBUG("[i2c] finish: stop event occurred\n");
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev(bus)->EVENTS_ERROR) {
|
2018-01-19 22:09:41 +01:00
|
|
|
dev(bus)->EVENTS_ERROR = 0;
|
|
|
|
if (dev(bus)->ERRORSRC & TWIM_ERRORSRC_ANACK_Msk) {
|
|
|
|
dev(bus)->ERRORSRC = TWIM_ERRORSRC_ANACK_Msk;
|
|
|
|
DEBUG("[i2c] check_error: NACK on address byte\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (dev(bus)->ERRORSRC & TWIM_ERRORSRC_DNACK_Msk) {
|
|
|
|
dev(bus)->ERRORSRC = TWIM_ERRORSRC_DNACK_Msk;
|
|
|
|
DEBUG("[i2c] check_error: NACK on data byte\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
return len;
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_init_master(i2c_t bus, i2c_speed_t speed)
|
|
|
|
{
|
|
|
|
if (bus >= I2C_NUMOF) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (speed & INVALID_SPEED_MASK) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
/* disable device during initialization, will be enabled when acquire is
|
|
|
|
* called */
|
|
|
|
dev(bus)->ENABLE = TWIM_ENABLE_ENABLE_Disabled;
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
/* configure pins */
|
|
|
|
gpio_init(i2c_config[bus].scl, GPIO_IN_PU);
|
|
|
|
gpio_init(i2c_config[bus].sda, GPIO_IN_PU);
|
|
|
|
dev(bus)->PSEL.SCL = i2c_config[bus].scl;
|
|
|
|
dev(bus)->PSEL.SDA = i2c_config[bus].sda;
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
/* configure bus clock speed */
|
|
|
|
dev(bus)->FREQUENCY = speed;
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
/* re-enable the device. We expect that the device was being acquired before
|
|
|
|
* the i2c_init_master() function is called, so it should be enabled when
|
|
|
|
* exiting this function. */
|
|
|
|
dev(bus)->ENABLE = TWIM_ENABLE_ENABLE_Enabled;
|
2017-09-01 15:33:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_acquire(i2c_t bus)
|
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert(bus < I2C_NUMOF);
|
|
|
|
|
2017-09-01 15:33:10 +02:00
|
|
|
mutex_lock(&locks[bus]);
|
2018-01-19 22:09:41 +01:00
|
|
|
dev(bus)->ENABLE = TWIM_ENABLE_ENABLE_Enabled;
|
|
|
|
|
|
|
|
DEBUG("[i2c] acquired bus %i\n", (int)bus);
|
2017-09-01 15:33:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_release(i2c_t bus)
|
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert(bus < I2C_NUMOF);
|
|
|
|
|
|
|
|
dev(bus)->ENABLE = TWIM_ENABLE_ENABLE_Disabled;
|
2017-09-01 15:33:10 +02:00
|
|
|
mutex_unlock(&locks[bus]);
|
2018-01-19 22:09:41 +01:00
|
|
|
|
|
|
|
DEBUG("[i2c] released bus %i\n", (int)bus);
|
2017-09-01 15:33:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_read_byte(i2c_t bus, uint8_t addr, void *data)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
return i2c_read_bytes(bus, addr, data, 1);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_read_bytes(i2c_t bus, uint8_t addr, void *data, int len)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert((bus < I2C_NUMOF) && data && (len > 0) && (len < 256));
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
DEBUG("[i2c] read_bytes: %i bytes from addr 0x%02x\n", (int)len, (int)addr);
|
2017-09-01 15:33:10 +02:00
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
dev(bus)->ADDRESS = addr;
|
|
|
|
dev(bus)->RXD.PTR = (uint32_t)data;
|
|
|
|
dev(bus)->RXD.MAXCNT = (uint8_t)len;
|
|
|
|
dev(bus)->SHORTS = TWIM_SHORTS_LASTRX_STOP_Msk;
|
2017-09-01 15:33:10 +02:00
|
|
|
dev(bus)->TASKS_STARTRX = 1;
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
return finish(bus, len);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_read_reg(i2c_t bus, uint8_t addr, uint8_t reg, void *data)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
return i2c_read_regs(bus, addr, reg, data, 1);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_read_regs(i2c_t bus, uint8_t addr, uint8_t reg,
|
|
|
|
void *data, int len)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert((bus < I2C_NUMOF) && data && (len > 0) && (len < 256));
|
|
|
|
|
|
|
|
DEBUG("[i2c] read_regs: %i byte(s) from reg 0x%02x at addr 0x%02x\n",
|
|
|
|
(int)len, (int)reg, (int)addr);
|
|
|
|
|
|
|
|
dev(bus)->ADDRESS = addr;
|
|
|
|
dev(bus)->TXD.PTR = (uint32_t)®
|
|
|
|
dev(bus)->TXD.MAXCNT = 1;
|
|
|
|
dev(bus)->RXD.PTR = (uint32_t)data;
|
|
|
|
dev(bus)->RXD.MAXCNT = (uint8_t)len;
|
|
|
|
dev(bus)->SHORTS = (TWIM_SHORTS_LASTTX_STARTRX_Msk |
|
|
|
|
TWIM_SHORTS_LASTRX_STOP_Msk);
|
|
|
|
dev(bus)->TASKS_STARTTX = 1;
|
|
|
|
|
|
|
|
return finish(bus, len);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_write_byte(i2c_t bus, uint8_t addr, uint8_t data)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
return i2c_write_bytes(bus, addr, &data, 1);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_write_bytes(i2c_t bus, uint8_t addr, const void *data, int len)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert((bus < I2C_NUMOF) && data && (len > 0) && (len < 256));
|
|
|
|
|
|
|
|
DEBUG("[i2c] write_bytes: %i byte(s) to addr 0x%02x\n", (int)len, (int)addr);
|
|
|
|
|
|
|
|
dev(bus)->ADDRESS = addr;
|
|
|
|
dev(bus)->TXD.PTR = (uint32_t)data;
|
|
|
|
dev(bus)->TXD.MAXCNT = (uint8_t)len;
|
|
|
|
dev(bus)->SHORTS = TWIM_SHORTS_LASTTX_STOP_Msk;
|
|
|
|
dev(bus)->TASKS_STARTTX = 1;
|
|
|
|
|
|
|
|
return finish(bus, len);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_write_reg(i2c_t bus, uint8_t addr, uint8_t reg, uint8_t data)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
return i2c_write_regs(bus, addr, reg, &data, 1);
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:09:41 +01:00
|
|
|
int i2c_write_regs(i2c_t bus, uint8_t addr, uint8_t reg,
|
|
|
|
const void *data, int len)
|
2017-09-01 15:33:10 +02:00
|
|
|
{
|
2018-01-19 22:09:41 +01:00
|
|
|
assert((bus < I2C_NUMOF) && data && (len > 0) && (len < 255));
|
|
|
|
|
|
|
|
/* the nrf52's TWI device does not support to do two consecutive transfers
|
|
|
|
* without a repeated start condition in between. So we have to put all data
|
|
|
|
* to be transfered into a temporary buffer
|
|
|
|
*
|
|
|
|
* CAUTION: this might become critical when transferring large blocks of
|
|
|
|
* data as the temporary buffer is allocated on the stack... */
|
|
|
|
uint8_t buf_tmp[len + 1];
|
|
|
|
buf_tmp[0] = reg;
|
|
|
|
memcpy(&buf_tmp[1], data, len);
|
|
|
|
return i2c_write_bytes(bus, addr, buf_tmp, (len + 1)) - 1;
|
2017-09-01 15:33:10 +02:00
|
|
|
}
|