BeRTOS
|
I2C generic driver functions. More...
Modules | |
I2C driver API | |
Old I2C API | |
This is the old and deprecated I2C API. | |
Functions | |
void | i2c_init_0 (void) |
Initialize I2C module (old API). | |
I2C bitbang devices enum | |
enum | { , I2C_BITBANG0 = 1000, I2C_BITBANG1 , I2C_BITBANG_CNT } |
I2C generic driver functions.
Some hardware requires you to declare the number of transferred bytes and the communication direction before actually reading or writing to the bus. Furthermore, sometimes you need to specify the first transferred byte before any data is sent over the bus.
The usage pattern for writing is the following:
i2c_init(args...); ... i2c_start_w(args...); i2c_write(i2c, buf, len);
The flags in i2c_start_w determine if the stop command is sent after the data. Notice that you don't need to explicitly call a stop function after the write.
Reading is a bit more complicated and it largely depends on the specific slave hardware. In general, the hardware may require you to first write something, then read the data without closing the communication. For example, EPROMs require first to write the reading address and then to read the actual data. Here is an example of how you can deal with such hardware:
// init a session without closing it i2c_start_w(i2c, dev, bytes, I2C_NOSTOP); // write the address to read from i2c_write(i2c, addr, bytes); if (i2c_error(i2c)) // check for errors during setup //... // now start the real data transfer i2c_start_r(i2c, dev, bytes, I2C_STOP); i2c_read(i2c, buf, bytes); // check for errors if (i2c_error(i2c)) //...
It's not guaranteed that after a single call to i2c_putc, i2c_getc etc. data will pass on the bus (this is hardware dependent). However, it IS guaranteed after you have sent all the data.
You can check error conditions by calling the function i2c_error after each function call. (This is similar to libc errno handling).
anonymous enum |