SAKURA Internet IoT Communication Module Library for mbed

Dependents:   patlite_sakuraio sakuraio_lte_firmwareupdater shownet2017-iinebutton patlite_sakuraio_stack ... more

SAKURA Internet IoT Communication Module Library for mbed

Sakura Communication Module (with sakura.io) library for mbed.

Support

This library supports following products.

Reference

Please see the datasheet.

License

The MIT License (MIT)

Copyright (c) SAKURA Internet Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Committer:
spiralray
Date:
Sat Nov 19 12:32:11 2016 +0000
Revision:
1:20e1dfe45dab
Parent:
0:8d34375d954c
Child:
3:c54a1eba22c4
Support I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spiralray 0:8d34375d954c 1 #include "mbed.h"
spiralray 0:8d34375d954c 2 #include "SakuraIO.h"
spiralray 1:20e1dfe45dab 3 #include "debug.h"
spiralray 1:20e1dfe45dab 4
spiralray 0:8d34375d954c 5 #define SAKURAIO_SLAVE_ADDR 0x4F
spiralray 1:20e1dfe45dab 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 1:20e1dfe45dab 10
spiralray 0:8d34375d954c 11 void SakuraIO_I2C::begin(){
spiralray 0:8d34375d954c 12 mode = MODE_IDLE;
spiralray 0:8d34375d954c 13 }
spiralray 1:20e1dfe45dab 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 1:20e1dfe45dab 21 while (_length > 0) {
spiralray 1:20e1dfe45dab 22 _length --;
spiralray 1:20e1dfe45dab 23 i2c.read(_length > 0 ? 1 : 0);
spiralray 1:20e1dfe45dab 24 }
spiralray 1:20e1dfe45dab 25 i2c.stop();
spiralray 0:8d34375d954c 26 break;
spiralray 0:8d34375d954c 27 }
spiralray 1:20e1dfe45dab 28
spiralray 0:8d34375d954c 29 mode = MODE_IDLE;
spiralray 0:8d34375d954c 30 }
spiralray 1:20e1dfe45dab 31
spiralray 0:8d34375d954c 32 void SakuraIO_I2C::sendByte(uint8_t data){
spiralray 0:8d34375d954c 33 if(mode != MODE_WRITE){
spiralray 1:20e1dfe45dab 34 dbg("beginTr\r\n");
spiralray 0:8d34375d954c 35 i2c.start();
spiralray 0:8d34375d954c 36 i2c.write(SAKURAIO_SLAVE_ADDR<<1);
spiralray 0:8d34375d954c 37 mode = MODE_WRITE;
spiralray 0:8d34375d954c 38 }
spiralray 1:20e1dfe45dab 39 dbg("Write=%02x\r\n", data);
spiralray 0:8d34375d954c 40 i2c.write(data);
spiralray 0:8d34375d954c 41 }
spiralray 1:20e1dfe45dab 42
spiralray 1:20e1dfe45dab 43
spiralray 0:8d34375d954c 44 uint8_t SakuraIO_I2C::startReceive(uint8_t length){
spiralray 1:20e1dfe45dab 45 if(mode != MODE_IDLE){
spiralray 1:20e1dfe45dab 46 dbg("endTransmission\r\n");
spiralray 1:20e1dfe45dab 47 i2c.stop();
spiralray 1:20e1dfe45dab 48 }
spiralray 1:20e1dfe45dab 49 dbg("requestForm=%d\r\n", length);
spiralray 1:20e1dfe45dab 50 _length = length;
spiralray 0:8d34375d954c 51 i2c.start();
spiralray 0:8d34375d954c 52 i2c.write(SAKURAIO_SLAVE_ADDR<<1 | 1);
spiralray 0:8d34375d954c 53 mode = MODE_READ;
spiralray 1:20e1dfe45dab 54 return 0;
spiralray 0:8d34375d954c 55 }
spiralray 1:20e1dfe45dab 56
spiralray 0:8d34375d954c 57 uint8_t SakuraIO_I2C::receiveByte(){
spiralray 1:20e1dfe45dab 58 return receiveByte(false);
spiralray 0:8d34375d954c 59 }
spiralray 1:20e1dfe45dab 60
spiralray 0:8d34375d954c 61 uint8_t SakuraIO_I2C::receiveByte(bool stop){
spiralray 0:8d34375d954c 62 uint8_t ret = 0;
spiralray 1:20e1dfe45dab 63 if (_length > 0) {
spiralray 1:20e1dfe45dab 64 _length --;
spiralray 1:20e1dfe45dab 65 ret = i2c.read(_length > 0 ? 1 : 0);
spiralray 1:20e1dfe45dab 66 }
spiralray 1:20e1dfe45dab 67 if(stop){
spiralray 1:20e1dfe45dab 68 end();
spiralray 1:20e1dfe45dab 69 }
spiralray 1:20e1dfe45dab 70 dbg("Read=%d\r\n", ret);
spiralray 0:8d34375d954c 71 return ret;
spiralray 0:8d34375d954c 72 }
spiralray 1:20e1dfe45dab 73
spiralray 1:20e1dfe45dab 74 SakuraIO_I2C::SakuraIO_I2C (I2C &_i2c) : i2c(_i2c) {
spiralray 0:8d34375d954c 75 mode = MODE_IDLE;
spiralray 0:8d34375d954c 76 }
spiralray 1:20e1dfe45dab 77
spiralray 1:20e1dfe45dab 78 SakuraIO_I2C::SakuraIO_I2C (PinName sda, PinName scl) : i2c(sda, scl) {
spiralray 1:20e1dfe45dab 79 mode = MODE_IDLE;
spiralray 1:20e1dfe45dab 80 }