SAKURA Internet / SakuraIO Featured

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SakuraIO_I2C.cpp Source File

SakuraIO_I2C.cpp

00001 /* SAKURA Internet IoT Communication Module Library for mbed
00002  *
00003  * The MIT License (MIT)
00004  *
00005  * Copyright (c) SAKURA Internet Inc.
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"),
00009  * to deal in the Software without restriction, including without limitation
00010  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011  * and/or sell copies of the Software, and to permit persons to whom the Software
00012  * is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included
00015  * in all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00018  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00020  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00021  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00022  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  */
00024 
00025  #include "mbed.h"
00026  #include "SakuraIO.h"
00027  #include "debug.h"
00028 
00029  #define SAKURAIO_SLAVE_ADDR 0x4F
00030 
00031  #define MODE_IDLE 0x00
00032  #define MODE_WRITE 0x01
00033  #define MODE_READ 0x02
00034 
00035  void SakuraIO_I2C::begin()
00036  {
00037      mode = MODE_IDLE;
00038  }
00039 
00040  void SakuraIO_I2C::end()
00041  {
00042      switch (mode) {
00043          case MODE_WRITE:
00044              i2c.stop();
00045              break;
00046          case MODE_READ:
00047              while (_length > 0) {
00048                  _length--;
00049                  i2c.read(_length > 0 ? 1 : 0);
00050              }
00051              i2c.stop();
00052              break;
00053      }
00054 
00055      mode = MODE_IDLE;
00056  }
00057 
00058  void SakuraIO_I2C::sendByte(uint8_t data)
00059  {
00060      if (mode != MODE_WRITE) {
00061          dbg("beginTr\r\n");
00062          i2c.start();
00063          i2c.write(SAKURAIO_SLAVE_ADDR << 1);
00064          mode = MODE_WRITE;
00065      }
00066      dbg("Write=%02x\r\n", data);
00067      i2c.write(data);
00068  }
00069 
00070  uint8_t SakuraIO_I2C::startReceive(uint8_t length)
00071  {
00072      if (mode != MODE_IDLE) {
00073          dbg("endTransmission\r\n");
00074          i2c.stop();
00075      }
00076      dbg("requestForm=%d\r\n", length);
00077      _length = length;
00078      wait_ms(10);
00079      i2c.start();
00080      i2c.write(SAKURAIO_SLAVE_ADDR << 1 | 1);
00081      mode = MODE_READ;
00082      return 0;
00083  }
00084 
00085  uint8_t SakuraIO_I2C::receiveByte()
00086  {
00087      return receiveByte(false);
00088  }
00089 
00090  uint8_t SakuraIO_I2C::receiveByte(bool stop)
00091  {
00092      uint8_t ret = 0;
00093      if (_length > 0) {
00094          _length--;
00095          ret = i2c.read(_length > 0 ? 1 : 0);
00096      }
00097      if (stop) {
00098          end();
00099      }
00100      dbg("Read=%d\r\n", ret);
00101      return ret;
00102  }
00103 
00104  SakuraIO_I2C::SakuraIO_I2C(I2C &_i2c) : i2c(_i2c)
00105  {
00106      mode = MODE_IDLE;
00107  }
00108 
00109  SakuraIO_I2C::SakuraIO_I2C(PinName sda, PinName scl) : i2c_p(new I2C(sda, scl)),
00110      i2c(*i2c_p)
00111  {
00112      mode = MODE_IDLE;
00113  }