SakuraIO

Fork of SakuraIO by SAKURA Internet

Committer:
chibiegg
Date:
Sat Nov 19 15:19:49 2016 +0000
Revision:
3:c54a1eba22c4
Parent:
1:20e1dfe45dab
add license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chibiegg 3:c54a1eba22c4 1 /* SAKURA Internet IoT Communication Module Library for mbed
chibiegg 3:c54a1eba22c4 2 *
chibiegg 3:c54a1eba22c4 3 * The MIT License (MIT)
chibiegg 3:c54a1eba22c4 4 *
chibiegg 3:c54a1eba22c4 5 * Copyright (c) SAKURA Internet Inc.
chibiegg 3:c54a1eba22c4 6 *
chibiegg 3:c54a1eba22c4 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
chibiegg 3:c54a1eba22c4 8 * of this software and associated documentation files (the "Software"),
chibiegg 3:c54a1eba22c4 9 * to deal in the Software without restriction, including without limitation
chibiegg 3:c54a1eba22c4 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
chibiegg 3:c54a1eba22c4 11 * and/or sell copies of the Software, and to permit persons to whom the Software
chibiegg 3:c54a1eba22c4 12 * is furnished to do so, subject to the following conditions:
chibiegg 3:c54a1eba22c4 13 *
chibiegg 3:c54a1eba22c4 14 * The above copyright notice and this permission notice shall be included
chibiegg 3:c54a1eba22c4 15 * in all copies or substantial portions of the Software.
chibiegg 3:c54a1eba22c4 16 *
chibiegg 3:c54a1eba22c4 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
chibiegg 3:c54a1eba22c4 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
chibiegg 3:c54a1eba22c4 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
chibiegg 3:c54a1eba22c4 20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
chibiegg 3:c54a1eba22c4 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
chibiegg 3:c54a1eba22c4 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
chibiegg 3:c54a1eba22c4 23 */
chibiegg 3:c54a1eba22c4 24
spiralray 0:8d34375d954c 25 #include "mbed.h"
spiralray 0:8d34375d954c 26 #include "SakuraIO.h"
spiralray 1:20e1dfe45dab 27 #include "debug.h"
spiralray 1:20e1dfe45dab 28
spiralray 0:8d34375d954c 29 #define SAKURAIO_SLAVE_ADDR 0x4F
spiralray 1:20e1dfe45dab 30
spiralray 0:8d34375d954c 31 #define MODE_IDLE 0x00
spiralray 0:8d34375d954c 32 #define MODE_WRITE 0x01
spiralray 0:8d34375d954c 33 #define MODE_READ 0x02
spiralray 1:20e1dfe45dab 34
spiralray 0:8d34375d954c 35 void SakuraIO_I2C::begin(){
spiralray 0:8d34375d954c 36 mode = MODE_IDLE;
spiralray 0:8d34375d954c 37 }
spiralray 1:20e1dfe45dab 38
spiralray 0:8d34375d954c 39 void SakuraIO_I2C::end(){
spiralray 0:8d34375d954c 40 switch(mode){
spiralray 0:8d34375d954c 41 case MODE_WRITE:
spiralray 0:8d34375d954c 42 i2c.stop();
spiralray 0:8d34375d954c 43 break;
spiralray 0:8d34375d954c 44 case MODE_READ:
spiralray 1:20e1dfe45dab 45 while (_length > 0) {
spiralray 1:20e1dfe45dab 46 _length --;
spiralray 1:20e1dfe45dab 47 i2c.read(_length > 0 ? 1 : 0);
spiralray 1:20e1dfe45dab 48 }
spiralray 1:20e1dfe45dab 49 i2c.stop();
spiralray 0:8d34375d954c 50 break;
spiralray 0:8d34375d954c 51 }
spiralray 1:20e1dfe45dab 52
spiralray 0:8d34375d954c 53 mode = MODE_IDLE;
spiralray 0:8d34375d954c 54 }
spiralray 1:20e1dfe45dab 55
spiralray 0:8d34375d954c 56 void SakuraIO_I2C::sendByte(uint8_t data){
spiralray 0:8d34375d954c 57 if(mode != MODE_WRITE){
spiralray 1:20e1dfe45dab 58 dbg("beginTr\r\n");
spiralray 0:8d34375d954c 59 i2c.start();
spiralray 0:8d34375d954c 60 i2c.write(SAKURAIO_SLAVE_ADDR<<1);
spiralray 0:8d34375d954c 61 mode = MODE_WRITE;
spiralray 0:8d34375d954c 62 }
spiralray 1:20e1dfe45dab 63 dbg("Write=%02x\r\n", data);
spiralray 0:8d34375d954c 64 i2c.write(data);
spiralray 0:8d34375d954c 65 }
spiralray 1:20e1dfe45dab 66
spiralray 1:20e1dfe45dab 67
spiralray 0:8d34375d954c 68 uint8_t SakuraIO_I2C::startReceive(uint8_t length){
spiralray 1:20e1dfe45dab 69 if(mode != MODE_IDLE){
spiralray 1:20e1dfe45dab 70 dbg("endTransmission\r\n");
spiralray 1:20e1dfe45dab 71 i2c.stop();
spiralray 1:20e1dfe45dab 72 }
spiralray 1:20e1dfe45dab 73 dbg("requestForm=%d\r\n", length);
spiralray 1:20e1dfe45dab 74 _length = length;
spiralray 0:8d34375d954c 75 i2c.start();
spiralray 0:8d34375d954c 76 i2c.write(SAKURAIO_SLAVE_ADDR<<1 | 1);
spiralray 0:8d34375d954c 77 mode = MODE_READ;
spiralray 1:20e1dfe45dab 78 return 0;
spiralray 0:8d34375d954c 79 }
spiralray 1:20e1dfe45dab 80
spiralray 0:8d34375d954c 81 uint8_t SakuraIO_I2C::receiveByte(){
spiralray 1:20e1dfe45dab 82 return receiveByte(false);
spiralray 0:8d34375d954c 83 }
spiralray 1:20e1dfe45dab 84
spiralray 0:8d34375d954c 85 uint8_t SakuraIO_I2C::receiveByte(bool stop){
spiralray 0:8d34375d954c 86 uint8_t ret = 0;
spiralray 1:20e1dfe45dab 87 if (_length > 0) {
spiralray 1:20e1dfe45dab 88 _length --;
spiralray 1:20e1dfe45dab 89 ret = i2c.read(_length > 0 ? 1 : 0);
spiralray 1:20e1dfe45dab 90 }
spiralray 1:20e1dfe45dab 91 if(stop){
spiralray 1:20e1dfe45dab 92 end();
spiralray 1:20e1dfe45dab 93 }
spiralray 1:20e1dfe45dab 94 dbg("Read=%d\r\n", ret);
spiralray 0:8d34375d954c 95 return ret;
spiralray 0:8d34375d954c 96 }
spiralray 1:20e1dfe45dab 97
spiralray 1:20e1dfe45dab 98 SakuraIO_I2C::SakuraIO_I2C (I2C &_i2c) : i2c(_i2c) {
spiralray 0:8d34375d954c 99 mode = MODE_IDLE;
spiralray 0:8d34375d954c 100 }
spiralray 1:20e1dfe45dab 101
spiralray 1:20e1dfe45dab 102 SakuraIO_I2C::SakuraIO_I2C (PinName sda, PinName scl) : i2c(sda, scl) {
spiralray 1:20e1dfe45dab 103 mode = MODE_IDLE;
spiralray 1:20e1dfe45dab 104 }