SakuraIO
Fork of SakuraIO by
SakuraIO_I2C.cpp@0:8d34375d954c, 2016-11-19 (annotated)
- Committer:
- spiralray
- Date:
- Sat Nov 19 03:20:17 2016 +0000
- Revision:
- 0:8d34375d954c
- Child:
- 1:20e1dfe45dab
First commit(This only works with SPI)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spiralray | 0:8d34375d954c | 1 | #include "mbed.h" |
spiralray | 0:8d34375d954c | 2 | #include "SakuraIO.h" |
spiralray | 0:8d34375d954c | 3 | #include "SakuraIO/debug.h" |
spiralray | 0:8d34375d954c | 4 | |
spiralray | 0:8d34375d954c | 5 | #define SAKURAIO_SLAVE_ADDR 0x4F |
spiralray | 0:8d34375d954c | 6 | |
spiralray | 0:8d34375d954c | 7 | #define MODE_IDLE 0x00 |
spiralray | 0:8d34375d954c | 8 | #define MODE_WRITE 0x01 |
spiralray | 0:8d34375d954c | 9 | #define MODE_READ 0x02 |
spiralray | 0:8d34375d954c | 10 | |
spiralray | 0:8d34375d954c | 11 | void SakuraIO_I2C::begin(){ |
spiralray | 0:8d34375d954c | 12 | mode = MODE_IDLE; |
spiralray | 0:8d34375d954c | 13 | } |
spiralray | 0:8d34375d954c | 14 | |
spiralray | 0:8d34375d954c | 15 | void SakuraIO_I2C::end(){ |
spiralray | 0:8d34375d954c | 16 | switch(mode){ |
spiralray | 0:8d34375d954c | 17 | case MODE_WRITE: |
spiralray | 0:8d34375d954c | 18 | i2c.stop(); |
spiralray | 0:8d34375d954c | 19 | break; |
spiralray | 0:8d34375d954c | 20 | case MODE_READ: |
spiralray | 0:8d34375d954c | 21 | i2c.read(true); |
spiralray | 0:8d34375d954c | 22 | break; |
spiralray | 0:8d34375d954c | 23 | } |
spiralray | 0:8d34375d954c | 24 | |
spiralray | 0:8d34375d954c | 25 | mode = MODE_IDLE; |
spiralray | 0:8d34375d954c | 26 | } |
spiralray | 0:8d34375d954c | 27 | |
spiralray | 0:8d34375d954c | 28 | void SakuraIO_I2C::sendByte(uint8_t data){ |
spiralray | 0:8d34375d954c | 29 | if(mode != MODE_WRITE){ |
spiralray | 0:8d34375d954c | 30 | dbgln("beginTr"); |
spiralray | 0:8d34375d954c | 31 | i2c.start(); |
spiralray | 0:8d34375d954c | 32 | i2c.write(SAKURAIO_SLAVE_ADDR<<1); |
spiralray | 0:8d34375d954c | 33 | mode = MODE_WRITE; |
spiralray | 0:8d34375d954c | 34 | } |
spiralray | 0:8d34375d954c | 35 | dbg("Write="); |
spiralray | 0:8d34375d954c | 36 | dbgln(data); |
spiralray | 0:8d34375d954c | 37 | i2c.write(data); |
spiralray | 0:8d34375d954c | 38 | } |
spiralray | 0:8d34375d954c | 39 | |
spiralray | 0:8d34375d954c | 40 | |
spiralray | 0:8d34375d954c | 41 | uint8_t SakuraIO_I2C::startReceive(uint8_t length){ |
spiralray | 0:8d34375d954c | 42 | dbgln("endTransmission"); |
spiralray | 0:8d34375d954c | 43 | i2c.stop(); |
spiralray | 0:8d34375d954c | 44 | dbg("requestForm="); |
spiralray | 0:8d34375d954c | 45 | dbgln(length); |
spiralray | 0:8d34375d954c | 46 | i2c.start(); |
spiralray | 0:8d34375d954c | 47 | i2c.write(SAKURAIO_SLAVE_ADDR<<1 | 1); |
spiralray | 0:8d34375d954c | 48 | mode = MODE_READ; |
spiralray | 0:8d34375d954c | 49 | } |
spiralray | 0:8d34375d954c | 50 | |
spiralray | 0:8d34375d954c | 51 | uint8_t SakuraIO_I2C::receiveByte(){ |
spiralray | 0:8d34375d954c | 52 | return i2c.read(true); |
spiralray | 0:8d34375d954c | 53 | } |
spiralray | 0:8d34375d954c | 54 | |
spiralray | 0:8d34375d954c | 55 | uint8_t SakuraIO_I2C::receiveByte(bool stop){ |
spiralray | 0:8d34375d954c | 56 | uint8_t ret = 0; |
spiralray | 0:8d34375d954c | 57 | ret = i2c.read(!stop); |
spiralray | 0:8d34375d954c | 58 | dbg("Read="); |
spiralray | 0:8d34375d954c | 59 | dbgln(ret); |
spiralray | 0:8d34375d954c | 60 | return ret; |
spiralray | 0:8d34375d954c | 61 | } |
spiralray | 0:8d34375d954c | 62 | |
spiralray | 0:8d34375d954c | 63 | SakuraIO_I2C::SakuraIO_I2C(I2C &_i2c): i2c(_i2c){ |
spiralray | 0:8d34375d954c | 64 | mode = MODE_IDLE; |
spiralray | 0:8d34375d954c | 65 | } |