I2c Communication in chibios

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

I2c Communication in chibios

Postby kalpesh » Mon Dec 09, 2019 12:04 pm

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: 13012
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 745 times
Been thanked: 621 times
Contact:

Re: I2c Communication in chibios

Postby Giovanni » Mon Dec 09, 2019 12:11 pm

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

Postby kalpesh » Mon Dec 09, 2019 3:06 pm

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
Contact:

Re: I2c Communication in chibios

Postby wurstnase » Tue Dec 10, 2019 2:18 pm

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.
\o/ Nico


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 2 guests