I2c Communication in chibios

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
kalpesh
Posts: 19
Joined: Mon Dec 09, 2019 11:53 am

I2c Communication in chibios

Post by kalpesh »

Hi,

I want to ask how to configure the particular register for any sensor using I2c api. I am working on a sensor where I need to configure the register first, and then perform read operation. So I need to send slave address, reg location, tx buf , tx bytes which is not possible by using following api(there is no parameter for reg location).

i2cMasterTransmitTimeout(I2CDriver *i2cp,
i2caddr_t addr,
const uint8_t *txbuf,
size_t txbytes,
uint8_t *rxbuf,
size_t rxbytes,
sysinterval_t timeout)

And should I send the Slave address directly as given in datasheet or add 1 or 0 for read and write operation respectively.

Thanks
Kalpesh
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2c Communication in chibios

Post by Giovanni »

Hi,

Under /os/ex there are a lot of drivers that use I2C to access registers in peripherals, you can use those as example, "reg location" is part of the TX data probably, I2C knows nothing about registers and the API is very generic.

Giovanni
kalpesh
Posts: 19
Joined: Mon Dec 09, 2019 11:53 am

Re: I2c Communication in chibios

Post by kalpesh »

Giovanni wrote:Hi,

Under /os/ex there are a lot of drivers that use I2C to access registers in peripherals, you can use those as example, "reg location" is part of the TX data probably, I2C knows nothing about registers and the API is very generic.

Giovanni


is there any API in this format
int8_t I2C_write(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout)

where, I can mention reg address.

Thanks
User avatar
wurstnase
Posts: 121
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 43 times
Been thanked: 30 times

Re: I2c Communication in chibios

Post by wurstnase »

There is no easy way to have a generic version for this.
Some slave needs an address with 8bit, some with 16bit. Some are MSB others LSB.

You could use a struct for your purpose.

Code: Select all

struct my_data {
  uint8_t address;
  uint8_t data[SENDING_BUFFER_SIZE];
} __attribute__((packed));

void i2c_write(uint8_t addr, struct my_data *data) {
    data->address = addr;
    i2cMasterTransmit(&I2CD2, I2C_ADDR, (uint8_t*)data, sizeof(struct my_data), NULL, 0);
}


In that case you can easy handle your address.
Keep in mind that chibios is using DMA, so sending data must be "together".

This code is untested and just written in the browser.
Post Reply