|
发表于 2013-5-8 04:54:39
|
显示全部楼层
Another example: i2c devices (or spi devices for that matter).
Typical implementation is to write i2c driver into the device driver file and compile them.
I do it over two layers: i2c layer and device layer.
In the device layer, I would call i2c_start(), i2c_stop(), i2c_write() and i2c_read() to perform i2c specific operations, without any knowledge of if the operations are done via software or hardware i2c.
So my device layer would look like this:
- ==========myi2c.c/.h=========
- #include "i2c_hw.h" //use hardware i2c
- //#include "i2c_sw.h" //use software i2c
- void myi2c_write(unsigned char addr, unsigned char data) {
- i2c_start(); //send the start condition
- i2c_write(addr, data); //write data to addr
- i2c_stop();
- }
复制代码 In i2c_hw or i2c_sw, I would implement the actual i2c_start(), i2c_stop(), i2c_write() .. routines - they have the same names!
So if I want to run my code using hardware i2c from a given mcu, I just need to include the i2c_hw.c/.h files for that platform; for software i2c, I just need to include i2c_sw.c/.h files.
At a higher level, I would then write myi2c.c/myi2c.h files so that the same drivers can be ported to different mcus.
|
|