i2cmaster class without interrupt process
Revision 0:a7ab96dc6a35, committed 2017-02-26
- Comitter:
- sgrsn
- Date:
- Sun Feb 26 07:02:27 2017 +0000
- Commit message:
- i2cmaster class without interrut process
Changed in this revision
i2cmaster.cpp | Show annotated file Show diff for this revision Revisions of this file |
i2cmaster.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r a7ab96dc6a35 i2cmaster.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/i2cmaster.cpp Sun Feb 26 07:02:27 2017 +0000 @@ -0,0 +1,28 @@ +#include "i2cmaster.h" + +i2c::i2c(PinName sda, PinName scl) : I2C(sda, scl) +{ + frequency(400000); +} + +void i2c::sendi2c(char addr, char registar, int data, int size) +{ + if(size > 0) size = size; + else if(data > 0xFFFFFF) size = 4; + else if(data > 0xFFFF) size = 3; + else if(data > 0xFF) size = 2; + else size = 1; + char DATA[4] = {data & 0xFF, (data>>8) & 0xFF, (data>>16) & 0xFF, (data>>24) & 0xFF}; + char i2cData[3] = {size, registar, SendData}; + write(addr, i2cData, 3); + write(addr, DATA, size); +} + +int i2c::getSlaveData(char addr, char registar, int size) +{ + char i2cData[3] = {size, registar, RequestData}; + char getData[12] = {}; + write(addr, i2cData, 3); + read(addr, getData, size); + return getData[0] | (getData[1] << 8) | (getData[2] << 16) | (getData[3] << 24); +} \ No newline at end of file
diff -r 000000000000 -r a7ab96dc6a35 i2cmaster.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/i2cmaster.h Sun Feb 26 07:02:27 2017 +0000 @@ -0,0 +1,40 @@ +#ifndef MBED_I2CMASTER_H +#define MBED_I2CMASTER_H + +#include "mbed.h" + +#define SendData 0 +#define RequestData 1 + +class i2c : public I2C +{ + public: + i2c(PinName sda, PinName scl); + /*{ + frequency(400000); + }*/ + + void sendi2c(char addr, char registar, int data, int size = 0); + /*{ + if(size > 0) size = size; + else if(data > 0xFFFFFF) size = 4; + else if(data > 0xFFFF) size = 3; + else if(data > 0xFF) size = 2; + else size = 1; + char DATA[4] = {data & 0xFF, (data>>8) & 0xFF, (data>>16) & 0xFF, (data>>24) & 0xFF}; + char i2cData[3] = {size, registar, SendData}; + write(addr, i2cData, 3); + write(addr, DATA, size); + }*/ + + int getSlaveData(char addr, char registar, int size = 1); + /*{ + char i2cData[3] = {size, registar, RequestData}; + char getData[12] = {}; + write(addr, i2cData, 3); + read(addr, getData, size); + return getData[0] | (getData[1] << 8) | (getData[2] << 16) | (getData[3] << 24); + }*/ +}; + +#endif