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:
okuhara
Date:
Fri Apr 13 10:23:27 2018 +0000
Revision:
13:a9dbb86be232
Parent:
11:16e726ca39c6
Add the PowerSaveMode commands.

Who changed what in which revision?

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