Browse Source

periph/i2c: convert char to uint8_t where applicapable

In general, data transferred through I2C are bytes and thus should have
type uint8_t, not char.

Also convert uint8_t ptrs to void ptrs
pr/spi.typo
Kees Bakker 6 years ago
parent
commit
bac5cda1e3
  1. 28
      cpu/cc2538/periph/i2c.c
  2. 26
      cpu/kinetis_common/periph/i2c.c
  3. 34
      cpu/sam21_common/periph/i2c.c
  4. 41
      cpu/stm32f1/periph/i2c.c
  5. 52
      cpu/stm32f2/periph/i2c.c
  6. 28
      cpu/stm32f3/periph/i2c.c
  7. 39
      cpu/stm32f4/periph/i2c.c
  8. 39
      cpu/stm32l1/periph/i2c.c
  9. 8
      drivers/at30tse75x/at30tse75x.c
  10. 2
      drivers/bh1750fvi/bh1750fvi.c
  11. 12
      drivers/bmp180/bmp180.c
  12. 16
      drivers/hdc1000/hdc1000.c
  13. 4
      drivers/hih6130/hih6130.c
  14. 4
      drivers/ina220/ina220.c
  15. 16
      drivers/include/periph/i2c.h
  16. 8
      drivers/isl29020/isl29020.c
  17. 10
      drivers/isl29125/isl29125.c
  18. 8
      drivers/l3g4200d/l3g4200d.c
  19. 10
      drivers/lis3mdl/lis3mdl.c
  20. 10
      drivers/lps331ap/lps331ap.c
  21. 16
      drivers/lsm303dlhc/lsm303dlhc.c
  22. 28
      drivers/mag3110/mag3110.c
  23. 22
      drivers/mma8652/mma8652.c
  24. 16
      drivers/mpl3115a2/mpl3115a2.c
  25. 30
      drivers/mpu9150/mpu9150.c
  26. 14
      drivers/si70xx/si70xx.c
  27. 4
      drivers/srf02/srf02.c
  28. 4
      drivers/srf08/srf08.c
  29. 10
      drivers/tcs37727/tcs37727.c
  30. 26
      drivers/tmp006/tmp006.c
  31. 2
      pkg/u8g2/patches/0002-u8g2-add-riot-os-interface.patch
  32. 44
      tests/periph_i2c/main.c

28
cpu/cc2538/periph/i2c.c

@ -348,12 +348,12 @@ static bool i2c_busy(void) {
return false;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
static int i2c_read_bytes_dumb(uint8_t address, char *data, int length)
static int i2c_read_bytes_dumb(uint8_t address, uint8_t *data, int length)
{
int n = 0;
uint_fast8_t stat;
@ -416,7 +416,7 @@ static int i2c_read_bytes_dumb(uint8_t address, char *data, int length)
return n;
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
switch (dev) {
#if I2C_0_EN
@ -447,12 +447,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return i2c_read_bytes_dumb(address, data, length);
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
uint_fast8_t stat;
@ -492,14 +492,15 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
}
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
int n = 0;
const uint8_t *my_data = data;
if (dev != I2C_0) {
return -1;
@ -521,7 +522,7 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
for (n = 0; n < length; n++) {
if (n >= length - 1) flags |= STOP;
WARN_IF(I2CM_STAT & BUSY);
I2CM_DR = data[n];
I2CM_DR = my_data[n];
i2c_ctrl_blocking(flags);
WARN_IF(I2CM_STAT & ARBLST);
@ -542,20 +543,21 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
if (n < length) {
DEBUG("%s(%u, %p, %u): %u/%u bytes delivered.\n",
__FUNCTION__, address, (void *)data, length, n, length);
__FUNCTION__, address, (void *)my_data, length, n, length);
}
return n;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
uint_fast8_t stat;
const uint8_t *my_data = data;
if (dev != I2C_0) {
return -1;
@ -598,7 +600,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
for (n = 0; n < length; n++) {
if (n >= length - 1) flags |= STOP;
WARN_IF(I2CM_STAT & BUSY);
I2CM_DR = data[n];
I2CM_DR = my_data[n];
i2c_ctrl_blocking(flags);
@ -623,7 +625,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
dev,
address,
reg,
(void *)data,
data,
length,
n,
length

26
cpu/kinetis_common/periph/i2c.c

@ -282,7 +282,7 @@ static inline int _i2c_receive(I2C_Type *dev, uint8_t *data, int length)
dev->C1 &= ~I2C_C1_MST_MASK;
}
data[n] = (char)dev->D;
data[n] = dev->D;
TRACE("i2c: rx: %02x\n", (unsigned int)data[n]);
n++;
}
@ -290,12 +290,12 @@ static inline int _i2c_receive(I2C_Type *dev, uint8_t *data, int length)
return n;
}
static inline int _i2c_transmit(I2C_Type *dev, uint8_t *data, int length)
static inline int _i2c_transmit(I2C_Type *dev, const uint8_t *data, int length)
{
int n = 0;
while (length > 0) {
TRACE("i2c: tx: %02x\n", (unsigned int)data[n]);
TRACE("i2c: tx: %02x\n", data[n]);
dev->D = data[n];
while (!(dev->S & I2C_S_IICIF_MASK));
@ -331,12 +331,12 @@ static inline void _i2c_reset(I2C_Type *dev)
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
I2C_Type *i2c;
int n = 0;
@ -369,12 +369,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return n;
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
I2C_Type *i2c;
int n = 0;
@ -396,19 +396,19 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
return -1;
}
n = _i2c_transmit(i2c, (uint8_t *)data, length);
n = _i2c_transmit(i2c, data, length);
_i2c_stop(i2c);
return n;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
I2C_Type *i2c;
int n = 0;
@ -454,12 +454,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return n;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
I2C_Type *i2c;
int n = 0;
@ -488,7 +488,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
return n;
}
n = _i2c_transmit(i2c, (uint8_t *)data, length);
n = _i2c_transmit(i2c, data, length);
_i2c_stop(i2c);
return n;

34
cpu/sam21_common/periph/i2c.c

@ -45,8 +45,8 @@ static void _i2c_poweron(SercomI2cm *sercom);
static void _i2c_poweroff(SercomI2cm *sercom);
static inline int _start(SercomI2cm *dev, uint8_t address, uint8_t rw_flag);
static inline int _write(SercomI2cm *dev, char *data, int length);
static inline int _read(SercomI2cm *dev, char *data, int length);
static inline int _write(SercomI2cm *dev, const uint8_t *data, int length);
static inline int _read(SercomI2cm *dev, uint8_t *data, int length);
static inline void _stop(SercomI2cm *dev);
/**
@ -204,12 +204,12 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
SercomI2cm *i2c;
@ -232,12 +232,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
SercomI2cm *i2c;
@ -252,19 +252,19 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
}
/* start transmission and send slave address */
if(_start(i2c, address, I2C_FLAG_WRITE) < 0) return 0;
if (_start(i2c, address, I2C_FLAG_WRITE) < 0) return 0;
/* send register address/command and wait for complete transfer to
* be finished */
if(_write(i2c, (char *)(&reg), 1) < 0) return 0;
if (_write(i2c, &reg, 1) < 0) return 0;
return i2c_read_bytes(dev, address, data, length);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
SercomI2cm *I2CSercom;
@ -285,12 +285,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
SercomI2cm *i2c;
@ -305,11 +305,11 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
}
/* start transmission and send slave address */
if(_start(i2c, address, I2C_FLAG_WRITE) < 0) return 0;
if (_start(i2c, address, I2C_FLAG_WRITE) < 0) return 0;
/* send register address and wait for complete transfer to be finished */
if(_write(i2c, (char *)(&reg), 1) < 0) return 0;
if (_write(i2c, &reg, 1) < 0) return 0;
/* write data to register */
if(_write(i2c, data, length) < 0) return 0;
if (_write(i2c, data, length) < 0) return 0;
/* finish transfer */
_stop(i2c);
return length;
@ -405,7 +405,7 @@ static int _start(SercomI2cm *dev, uint8_t address, uint8_t rw_flag)
return 0;
}
static inline int _write(SercomI2cm *dev, char *data, int length)
static inline int _write(SercomI2cm *dev, const uint8_t *data, int length)
{
uint16_t tmp_data_length = length;
uint32_t timeout_counter = 0;
@ -445,7 +445,7 @@ static inline int _write(SercomI2cm *dev, char *data, int length)
return 0;
}
static inline int _read(SercomI2cm *dev, char *data, int length)
static inline int _read(SercomI2cm *dev, uint8_t *data, int length)
{
uint32_t timeout_counter = 0;
uint8_t count = 0;

41
cpu/stm32f1/periph/i2c.c

@ -42,10 +42,10 @@
/* static function definitions */
static void _i2c_init(I2C_TypeDef *i2c, int ccr);
static void _pin_config(gpio_t pin_scl, gpio_t pin_sda);
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, char *err);
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, uint8_t *err);
static inline void _clear_addr(I2C_TypeDef *dev);
static inline void _write(I2C_TypeDef *dev, char *data, int length, char *err);
static inline void _stop(I2C_TypeDef *dev, char *err);
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length, uint8_t *err);
static inline void _stop(I2C_TypeDef *dev, uint8_t *err);
/**
* @brief Array holding one pre-initialized mutex for each I2C device
@ -65,7 +65,7 @@ static mutex_t locks[] = {
#endif
};
static char err_flag[] = {
static uint8_t err_flag[] = {
#if I2C_0_EN
[I2C_0] = 0x00,
#endif
@ -199,15 +199,16 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
int i = 0;
I2C_TypeDef *i2c;
uint8_t *my_data = data;
switch (dev) {
#if I2C_0_EN
@ -255,14 +256,14 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
DEBUG("Wait until byte was received\n");
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Copy byte from DR\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
}
DEBUG("Reading the last 3 bytes, waiting for BTF flag\n");
while (!(i2c->SR1 & I2C_SR1_BTF)) {}
DEBUG("Read N-3 byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
}
DEBUG("Clear ACK\n");
@ -276,7 +277,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Read byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
}
DEBUG("wait for STOP bit to be cleared again\n");
@ -289,13 +290,13 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
I2C_TypeDef *i2c;
@ -330,12 +331,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return i2c_read_bytes(dev, address, data, length);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -373,12 +374,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
}
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -401,7 +402,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
_start(i2c, address, I2C_FLAG_WRITE, &err_flag[dev]);
_clear_addr(i2c);
/* send register address and wait for complete transfer to be finished*/
_write(i2c, (char *)(&reg), 1, &err_flag[dev]);
_write(i2c, &reg, 1, &err_flag[dev]);
/* write data to register */
_write(i2c, data, length, &err_flag[dev]);
/* finish transfer */
@ -450,7 +451,7 @@ void i2c_poweroff(i2c_t dev)
}
}
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, char *err)
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, uint8_t *err)
{
/* flag that there's no error (yet) */
*err = 0x00;
@ -476,12 +477,12 @@ static inline void _clear_addr(I2C_TypeDef *dev)
dev->SR2;
}
static inline void _write(I2C_TypeDef *dev, char *data, int length, char *err)
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length, uint8_t *err)
{
DEBUG("Looping through bytes\n");
for (int i = 0; i < length && !(*err); i++) {
/* write data to data register */
dev->DR = (uint8_t)data[i];
dev->DR = data[i];
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
/* wait for transfer to finish */
while (!(dev->SR1 & I2C_SR1_TXE) && !(*err)) {}
@ -490,7 +491,7 @@ static inline void _write(I2C_TypeDef *dev, char *data, int length, char *err)
}
static inline void _stop(I2C_TypeDef *dev, char *err)
static inline void _stop(I2C_TypeDef *dev, uint8_t *err)
{
/* make sure last byte was send */
while (!(dev->SR1 & I2C_SR1_BTF) && !(*err)) {}

52
cpu/stm32f2/periph/i2c.c

@ -38,13 +38,13 @@
#define I2C_MAX_LOOP_CNT 10000
/* static function definitions */
static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length, char *err);
static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, uint8_t *data, int length, uint8_t *err);
static void _i2c_init(I2C_TypeDef *i2c, int ccr);
static void _pin_config(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda);
static int _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, char *err);
static int _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, uint8_t *err);
static inline void _clear_addr(I2C_TypeDef *dev);
static inline int _write(I2C_TypeDef *dev, char *data, int length, char *err);
static inline int _stop(I2C_TypeDef *dev, char *err);
static inline int _write(I2C_TypeDef *dev, const uint8_t *data, int length, uint8_t *err);
static inline int _stop(I2C_TypeDef *dev, uint8_t *err);
static inline int _wait_ready(I2C_TypeDef *dev);
/**
@ -65,7 +65,7 @@ static mutex_t locks[] = {
#endif
};
static char err_flag[] = {
static uint8_t err_flag[] = {
#if I2C_0_EN
[I2C_0] = 0x00,
#endif
@ -211,12 +211,12 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
I2C_TypeDef *i2c;
@ -252,7 +252,7 @@ static inline int _wait_ready(I2C_TypeDef *dev)
return 0;
}
static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length, char *err)
static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, uint8_t *data, int length, uint8_t *err)
{
unsigned int state;
int i = 0;
@ -289,7 +289,7 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
}
DEBUG("Read received data\n");
*data = (char)i2c->DR;
*data = i2c->DR;
/* wait until STOP is cleared by hardware */
cnt = 0;
@ -332,11 +332,11 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
DEBUG("Crit block: set STOP and read first byte\n");
state = irq_disable();
i2c->CR1 |= (I2C_CR1_STOP);
data[0] = (char)i2c->DR;
data[0] = i2c->DR;
irq_restore(state);
DEBUG("read second byte\n");
data[1] = (char)i2c->DR;
data[1] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -371,7 +371,7 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
}
DEBUG("Copy byte from DR\n");
data[i++] = (char)i2c->DR;
data[i++] = i2c->DR;
}
DEBUG("Reading the last 3 bytes, waiting for BTF flag\n");
@ -388,12 +388,12 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
DEBUG("Crit block: set STOP and read N-2 byte\n");
state = irq_disable();
data[i++] = (char)i2c->DR;
data[i++] = i2c->DR;
i2c->CR1 |= (I2C_CR1_STOP);
irq_restore(state);
DEBUG("Read N-1 byte\n");
data[i++] = (char)i2c->DR;
data[i++] = i2c->DR;
cnt = 0;
*err = 0;
@ -404,7 +404,7 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
DEBUG("Read last byte\n");
data[i++] = (char)i2c->DR;
data[i++] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -423,12 +423,12 @@ static int _read_bytes(I2C_TypeDef *i2c, uint8_t address, char *data, int length
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
I2C_TypeDef *i2c;
int res;
@ -464,12 +464,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return _read_bytes(i2c, address, data, length, &err_flag[dev]);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
I2C_TypeDef *i2c;
int res;
@ -520,12 +520,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
I2C_TypeDef *i2c;
int res;
@ -555,7 +555,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
}
_clear_addr(i2c);
/* send register address and wait for complete transfer to be finished*/
res = _write(i2c, (char *)(&reg), 1, &err_flag[dev]);
res = _write(i2c, &reg, 1, &err_flag[dev]);
if (res != 0) {
return res;
}
@ -608,7 +608,7 @@ void i2c_poweroff(i2c_t dev)
}
}
static int _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, char *err)
static int _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag, uint8_t *err)
{
int cnt = 0;
@ -646,13 +646,13 @@ static inline void _clear_addr(I2C_TypeDef *dev)
DEBUG("Cleared address\n");
}
static inline int _write(I2C_TypeDef *dev, char *data, int length, char *err)
static inline int _write(I2C_TypeDef *dev, const uint8_t *data, int length, uint8_t *err)
{
DEBUG("Looping through bytes\n");
for (int i = 0; i < length; i++) {
/* write data to data register */
dev->DR = (uint8_t)data[i];
dev->DR = data[i];
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
/* wait for transfer to finish */
@ -669,7 +669,7 @@ static inline int _write(I2C_TypeDef *dev, char *data, int length, char *err)
return 0;
}
static inline int _stop(I2C_TypeDef *dev, char *err)
static inline int _stop(I2C_TypeDef *dev, uint8_t *err)
{
/* make sure last byte was send */
DEBUG("Wait if last byte hasn't been sent\n");

28
cpu/stm32f3/periph/i2c.c

@ -46,8 +46,8 @@ static void _pin_config(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda,
int pin_scl, int pin_sda);
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t length,
uint8_t rw_flag);
static inline void _read(I2C_TypeDef *dev, char *data, int length);
static inline void _write(I2C_TypeDef *dev, char *data, int length);
static inline void _read(I2C_TypeDef *dev, uint8_t *data, int length);
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length);
static inline void _stop(I2C_TypeDef *dev);
/**
@ -245,12 +245,12 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
I2C_TypeDef *i2c;
@ -282,12 +282,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
I2C_TypeDef *i2c;
@ -323,12 +323,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return i2c_read_bytes(dev, address, data, length);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -360,12 +360,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -467,7 +467,7 @@ static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t length, uint8_t rw
dev->CR2 |= I2C_CR2_START;
}
static inline void _read(I2C_TypeDef *dev, char *data, int length)
static inline void _read(I2C_TypeDef *dev, uint8_t *data, int length)
{
for (int i = 0; i < length; i++) {
/* wait for transfer to finish */
@ -476,12 +476,12 @@ static inline void _read(I2C_TypeDef *dev, char *data, int length)
DEBUG("DR is now full\n");
/* read data from data register */
data[i] = (uint8_t)dev->RXDR;
data[i] = dev->RXDR;
DEBUG("Read byte %i from DR\n", i);
}
}
static inline void _write(I2C_TypeDef *dev, char *data, int length)
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length)
{
for (int i = 0; i < length; i++) {
/* wait for ack */
@ -490,7 +490,7 @@ static inline void _write(I2C_TypeDef *dev, char *data, int length)
/* write data to data register */
DEBUG("Write byte %i to DR\n", i);
dev->TXDR = (uint8_t)data[i];
dev->TXDR = data[i];
DEBUG("Sending data\n");
}
}

39
cpu/stm32f4/periph/i2c.c

@ -42,7 +42,7 @@ static void _toggle_pins(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin
static void _pin_config(GPIO_TypeDef *port_scl, GPIO_TypeDef *port_sda, int pin_scl, int pin_sda);
static void _start(I2C_TypeDef *dev, uint8_t address, uint8_t rw_flag);
static inline void _clear_addr(I2C_TypeDef *dev);
static inline void _write(I2C_TypeDef *dev, char *data, int length);
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length);
static inline void _stop(I2C_TypeDef *dev);
/**
@ -233,16 +233,17 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
unsigned int state;
int i = 0;
I2C_TypeDef *i2c;
uint8_t *my_data = data;
switch (dev) {
#if I2C_0_EN
@ -274,7 +275,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Read received data\n");
*data = (char)i2c->DR;
*my_data = i2c->DR;
/* wait until STOP is cleared by hardware */
while (i2c->CR1 & I2C_CR1_STOP) {}
@ -301,11 +302,11 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
DEBUG("Crit block: set STOP and read first byte\n");
state = irq_disable();
i2c->CR1 |= (I2C_CR1_STOP);
data[0] = (char)i2c->DR;
my_data[0] = i2c->DR;
irq_restore(state);
DEBUG("read second byte\n");
data[1] = (char)i2c->DR;
my_data[1] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -327,7 +328,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Copy byte from DR\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
}
DEBUG("Reading the last 3 bytes, waiting for BTF flag\n");
@ -339,18 +340,18 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
DEBUG("Crit block: set STOP and read N-2 byte\n");
state = irq_disable();
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
i2c->CR1 |= (I2C_CR1_STOP);
irq_restore(state);
DEBUG("Read N-1 byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Read last byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -364,12 +365,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
I2C_TypeDef *i2c;
@ -395,12 +396,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return i2c_read_bytes(dev, address, data, length);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -428,12 +429,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
I2C_TypeDef *i2c;
@ -452,7 +453,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
_start(i2c, address, I2C_FLAG_WRITE);
_clear_addr(i2c);
/* send register address and wait for complete transfer to be finished*/
_write(i2c, (char *)(&reg), 1);
_write(i2c, &reg, 1);
/* write data to register */
_write(i2c, data, length);
/* finish transfer */
@ -515,13 +516,13 @@ static inline void _clear_addr(I2C_TypeDef *dev)
DEBUG("Cleared address\n");
}
static inline void _write(I2C_TypeDef *dev, char *data, int length)
static inline void _write(I2C_TypeDef *dev, const uint8_t *data, int length)
{
DEBUG("Looping through bytes\n");
for (int i = 0; i < length; i++) {
/* write data to data register */
dev->DR = (uint8_t)data[i];
dev->DR = data[i];
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
/* wait for transfer to finish */

39
cpu/stm32l1/periph/i2c.c

@ -43,7 +43,7 @@
static void _i2c_init(I2C_TypeDef *i2c, int ccr);
static void _start(I2C_TypeDef *i2c, uint8_t address, uint8_t rw_flag);
static inline void _clear_addr(I2C_TypeDef *i2c);
static inline void _write(I2C_TypeDef *i2c, char *data, int length);
static inline void _write(I2C_TypeDef *i2c, const uint8_t *data, int length);
static inline void _stop(I2C_TypeDef *i2c);
/**
@ -140,15 +140,16 @@ int i2c_release(i2c_t dev)
return 0;
}
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
int i2c_read_byte(i2c_t dev, uint8_t address, void *data)
{
return i2c_read_bytes(dev, address, data, 1);
}
int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
unsigned int state;
int i = 0;
uint8_t *my_data = data;
if ((unsigned int)dev >= I2C_NUMOF) {
return -1;
@ -174,7 +175,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Read received data\n");
*data = (char)i2c->DR;
*my_data = i2c->DR;
/* wait until STOP is cleared by hardware */
while (i2c->CR1 & I2C_CR1_STOP) {}
@ -201,11 +202,11 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
DEBUG("Crit block: set STOP and read first byte\n");
state = irq_disable();
i2c->CR1 |= (I2C_CR1_STOP);
data[0] = (char)i2c->DR;
my_data[0] = i2c->DR;
irq_restore(state);
DEBUG("read second byte\n");
data[1] = (char)i2c->DR;
my_data[1] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -227,7 +228,7 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Copy byte from DR\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
}
DEBUG("Reading the last 3 bytes, waiting for BTF flag\n");
@ -239,18 +240,18 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
DEBUG("Crit block: set STOP and read N-2 byte\n");
state = irq_disable();
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
i2c->CR1 |= (I2C_CR1_STOP);
irq_restore(state);
DEBUG("Read N-1 byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
while (!(i2c->SR1 & I2C_SR1_RXNE)) {}
DEBUG("Read last byte\n");
data[i++] = (char)i2c->DR;
my_data[i++] = i2c->DR;
DEBUG("wait for STOP bit to be cleared again\n");
@ -264,12 +265,12 @@ int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data)
{
return i2c_read_regs(dev, address, reg, data, 1);
}
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length)
{
if ((unsigned int)dev >= I2C_NUMOF) {
return -1;
@ -288,12 +289,12 @@ int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int lengt
return i2c_read_bytes(dev, address, data, length);
}
int i2c_write_byte(i2c_t dev, uint8_t address, char data)
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
}
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length)
{
if ((unsigned int)dev >= I2C_NUMOF) {
return -1;
@ -314,12 +315,12 @@ int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
return length;
}
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, char data)
int i2c_write_reg(i2c_t dev, uint8_t address, uint8_t reg, uint8_t data)
{
return i2c_write_regs(dev, address, reg, &data, 1);
}
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, const void *data, int length)
{
if ((unsigned int)dev >= I2C_NUMOF) {
return -1;
@ -331,7 +332,7 @@ int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int leng
_start(i2c, address, I2C_FLAG_WRITE);
_clear_addr(i2c);
/* send register address and wait for complete transfer to be finished*/
_write(i2c, (char *)(&reg), 1);
_write(i2c, &reg, 1);
/* write data to register */
_write(i2c, data, length);
/* finish transfer */
@ -385,13 +386,13 @@ static inline void _clear_addr(I2C_TypeDef *i2c)
DEBUG("Cleared address\n");
}
static inline void _write(I2C_TypeDef *i2c, char *data, int length)
static inline void _write(I2C_TypeDef *i2c, const uint8_t *data, int length)
{
DEBUG("Looping through bytes\n");
for (int i = 0; i < length; i++) {
/* write data to data register */
i2c->DR = (uint8_t)data[i];
i2c->DR = data[i];
DEBUG("Written %i byte to data reg, now waiting for DR to be empty again\n", i);
/* wait for transfer to finish */

8
drivers/at30tse75x/at30tse75x.c

@ -38,7 +38,7 @@ static int at30tse75x_get_register(at30tse75x_t* dev, uint8_t reg, uint16_t* dat
{
i2c_acquire(dev->i2c);
xtimer_spin(AT30TSE75X_BUS_FREE_TIME_US);
if(i2c_read_regs(dev->i2c, dev->addr, reg, (char*) data, 2) <= 0) {
if (i2c_read_regs(dev->i2c, dev->addr, reg, data, 2) <= 0) {
DEBUG("[at30tse75x] Can't read register 0x%x\n", reg);
i2c_release(dev->i2c);
return -1;
@ -52,7 +52,7 @@ static int at30tse75x_set_register(at30tse75x_t* dev, uint8_t reg, uint16_t* dat
{
i2c_acquire(dev->i2c);
xtimer_spin(AT30TSE75X_BUS_FREE_TIME_US);
if(i2c_write_regs(dev->i2c, dev->addr, reg, (char*) data, 2) <= 0) {
if (i2c_write_regs(dev->i2c, dev->addr, reg, data, 2) <= 0) {
DEBUG("[at30tse75x] Can't write to register 0x%x\n", reg);
i2c_release(dev->i2c);
return -1;
@ -80,7 +80,7 @@ int at30tse75x_get_config(at30tse75x_t* dev, uint8_t* data)
{
i2c_acquire(dev->i2c);
xtimer_spin(AT30TSE75X_BUS_FREE_TIME_US);
if(i2c_read_reg(dev->i2c, dev->addr, AT30TSE75X_REG__CONFIG, (char*) data) <= 0) {
if (i2c_read_reg(dev->i2c, dev->addr, AT30TSE75X_REG__CONFIG, data) <= 0) {
DEBUG("[at30tse75x] Can't read CONFIG register\n");
i2c_release(dev->i2c);
return -1;
@ -94,7 +94,7 @@ int at30tse75x_set_config(at30tse75x_t* dev, uint8_t data)
{
i2c_acquire(dev->i2c);
xtimer_spin(AT30TSE75X_BUS_FREE_TIME_US);