Hello,
I'm in the middle of my first proper MCU project and I am starting to write my first I2C device drivers. (Drivers for I2C devices, not for the microcontrollers I2C module)
Around the vendor supplied MCU drivers I am creating a thin hardware/mcu independent wrapper interface. This is with the purpose of writing application that is MCU/hardware independent and to be able to develop that code supported by unit tests and mocks on desktop system. This seems to work fine, but since I don't have that much experience in writing I2C driver nor designing such API interfaces, I would like to ask you guys for your advice and opinions.
I believe the design goals would be:
- As narrow API as reasonable. So all accessing drivers would use the limited sets same commands
- A interface that is good enough to support most needs from a variety of devices -> so that there will be little need to change the interface in later on.
What would be the optimal I2C HAL/API interface for you? I have not tought much about open() / close() and eventual interupt driven callbacks, but for covering write/read variations.
I took at what interface Linux kernel supplies:
int i2c_master_send(struct i2c_client *client, const char *buf, int count);
int i2c_master_recv(struct i2c_client *client, char *buf, int count);
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num);
https://www.kernel.org/doc/Documentation/i2c/writing-clientsIn addition to direct functions for most of the SMBus transactions. I think the i2c_transfer(..) seems interesting.
This sends a series of messages. Each message can be a read or write,
and they can be mixed in any way. The transactions are combined: no
stop bit is sent between transaction. The i2c_msg structure contains
for each message the client address, the number of bytes of the message
and the message data itself.
Do I read it right, that with these three functions, I could basically cover all my I2C write/read needs? I have for example now a command i2c_writeNoStop(..) to mimic the start of a combined write+read transaction. But the Linux i2c_transfer interface would cover such transaction, and other needs?
Any experiences with using such interface or would you recommend an other layout?All opinions are appreciated, Thanks.