SAKURA Internet IoT Beta Communication Module Library for mbed
Dependents: SakuraIO_Standard SakuraIO_FirmwareUpdate
SakuraIO_I2C.cpp@0:f3b80b2bdb14, 2016-11-13 (annotated)
- Committer:
- sakurafan
- Date:
- Sun Nov 13 06:52:34 2016 +0000
- Revision:
- 0:f3b80b2bdb14
1st ported build;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakurafan | 0:f3b80b2bdb14 | 1 | #include "mbed.h" |
sakurafan | 0:f3b80b2bdb14 | 2 | #include "SakuraIO.h" |
sakurafan | 0:f3b80b2bdb14 | 3 | #include "debug.h" |
sakurafan | 0:f3b80b2bdb14 | 4 | |
sakurafan | 0:f3b80b2bdb14 | 5 | #define SAKURAIO_SLAVE_ADDR (0x4F << 1) |
sakurafan | 0:f3b80b2bdb14 | 6 | |
sakurafan | 0:f3b80b2bdb14 | 7 | #define MODE_IDLE 0x00 |
sakurafan | 0:f3b80b2bdb14 | 8 | #define MODE_WRITE 0x01 |
sakurafan | 0:f3b80b2bdb14 | 9 | #define MODE_READ 0x02 |
sakurafan | 0:f3b80b2bdb14 | 10 | |
sakurafan | 0:f3b80b2bdb14 | 11 | void SakuraIO_I2C::begin(){ |
sakurafan | 0:f3b80b2bdb14 | 12 | mode = MODE_IDLE; |
sakurafan | 0:f3b80b2bdb14 | 13 | } |
sakurafan | 0:f3b80b2bdb14 | 14 | |
sakurafan | 0:f3b80b2bdb14 | 15 | void SakuraIO_I2C::end(){ |
sakurafan | 0:f3b80b2bdb14 | 16 | switch(mode){ |
sakurafan | 0:f3b80b2bdb14 | 17 | case MODE_WRITE: |
sakurafan | 0:f3b80b2bdb14 | 18 | _i2c.stop(); |
sakurafan | 0:f3b80b2bdb14 | 19 | break; |
sakurafan | 0:f3b80b2bdb14 | 20 | case MODE_READ: |
sakurafan | 0:f3b80b2bdb14 | 21 | while (_length > 0) { |
sakurafan | 0:f3b80b2bdb14 | 22 | _length --; |
sakurafan | 0:f3b80b2bdb14 | 23 | _i2c.read(_length > 0 ? 1 : 0); |
sakurafan | 0:f3b80b2bdb14 | 24 | } |
sakurafan | 0:f3b80b2bdb14 | 25 | _i2c.stop(); |
sakurafan | 0:f3b80b2bdb14 | 26 | break; |
sakurafan | 0:f3b80b2bdb14 | 27 | } |
sakurafan | 0:f3b80b2bdb14 | 28 | |
sakurafan | 0:f3b80b2bdb14 | 29 | mode = MODE_IDLE; |
sakurafan | 0:f3b80b2bdb14 | 30 | } |
sakurafan | 0:f3b80b2bdb14 | 31 | |
sakurafan | 0:f3b80b2bdb14 | 32 | void SakuraIO_I2C::sendByte(uint8_t data){ |
sakurafan | 0:f3b80b2bdb14 | 33 | if(mode != MODE_WRITE){ |
sakurafan | 0:f3b80b2bdb14 | 34 | dbg("beginTr\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 35 | _i2c.start(); |
sakurafan | 0:f3b80b2bdb14 | 36 | _i2c.write(SAKURAIO_SLAVE_ADDR); |
sakurafan | 0:f3b80b2bdb14 | 37 | mode = MODE_WRITE; |
sakurafan | 0:f3b80b2bdb14 | 38 | } |
sakurafan | 0:f3b80b2bdb14 | 39 | dbg("Write=%02x\r\n", data); |
sakurafan | 0:f3b80b2bdb14 | 40 | _i2c.write(data); |
sakurafan | 0:f3b80b2bdb14 | 41 | } |
sakurafan | 0:f3b80b2bdb14 | 42 | |
sakurafan | 0:f3b80b2bdb14 | 43 | |
sakurafan | 0:f3b80b2bdb14 | 44 | uint8_t SakuraIO_I2C::startReceive(uint8_t length){ |
sakurafan | 0:f3b80b2bdb14 | 45 | if(mode != MODE_IDLE){ |
sakurafan | 0:f3b80b2bdb14 | 46 | dbg("endTransmission\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 47 | _i2c.stop(); |
sakurafan | 0:f3b80b2bdb14 | 48 | } |
sakurafan | 0:f3b80b2bdb14 | 49 | dbg("requestForm=%d\r\n", length); |
sakurafan | 0:f3b80b2bdb14 | 50 | _length = length; |
sakurafan | 0:f3b80b2bdb14 | 51 | _i2c.start(); |
sakurafan | 0:f3b80b2bdb14 | 52 | _i2c.write(SAKURAIO_SLAVE_ADDR | 1); |
sakurafan | 0:f3b80b2bdb14 | 53 | mode = MODE_READ; |
sakurafan | 0:f3b80b2bdb14 | 54 | return 0; |
sakurafan | 0:f3b80b2bdb14 | 55 | } |
sakurafan | 0:f3b80b2bdb14 | 56 | |
sakurafan | 0:f3b80b2bdb14 | 57 | uint8_t SakuraIO_I2C::receiveByte(){ |
sakurafan | 0:f3b80b2bdb14 | 58 | return receiveByte(false); |
sakurafan | 0:f3b80b2bdb14 | 59 | } |
sakurafan | 0:f3b80b2bdb14 | 60 | |
sakurafan | 0:f3b80b2bdb14 | 61 | uint8_t SakuraIO_I2C::receiveByte(bool stop){ |
sakurafan | 0:f3b80b2bdb14 | 62 | uint8_t ret = 0; |
sakurafan | 0:f3b80b2bdb14 | 63 | if (_length > 0) { |
sakurafan | 0:f3b80b2bdb14 | 64 | _length --; |
sakurafan | 0:f3b80b2bdb14 | 65 | ret = _i2c.read(_length > 0 ? 1 : 0); |
sakurafan | 0:f3b80b2bdb14 | 66 | } |
sakurafan | 0:f3b80b2bdb14 | 67 | if(stop){ |
sakurafan | 0:f3b80b2bdb14 | 68 | end(); |
sakurafan | 0:f3b80b2bdb14 | 69 | // _i2c.stop(); |
sakurafan | 0:f3b80b2bdb14 | 70 | // mode = MODE_IDLE; |
sakurafan | 0:f3b80b2bdb14 | 71 | } |
sakurafan | 0:f3b80b2bdb14 | 72 | dbg("Read=%d\r\n", ret); |
sakurafan | 0:f3b80b2bdb14 | 73 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 74 | } |
sakurafan | 0:f3b80b2bdb14 | 75 | |
sakurafan | 0:f3b80b2bdb14 | 76 | SakuraIO_I2C::SakuraIO_I2C (I2C &i2c) : _i2c(i2c) { |
sakurafan | 0:f3b80b2bdb14 | 77 | mode = MODE_IDLE; |
sakurafan | 0:f3b80b2bdb14 | 78 | } |
sakurafan | 0:f3b80b2bdb14 | 79 | |
sakurafan | 0:f3b80b2bdb14 | 80 | SakuraIO_I2C::SakuraIO_I2C (PinName sda, PinName scl) : _i2c(sda, scl) { |
sakurafan | 0:f3b80b2bdb14 | 81 | mode = MODE_IDLE; |
sakurafan | 0:f3b80b2bdb14 | 82 | } |