SAKURA Internet IoT Beta Communication Module Library for mbed

Dependents:   SakuraIO_Standard SakuraIO_FirmwareUpdate

Files at this revision

API Documentation at this revision

Comitter:
sakurafan
Date:
Sun Nov 13 06:52:34 2016 +0000
Commit message:
1st ported build;

Changed in this revision

SakuraIO.cpp Show annotated file Show diff for this revision Revisions of this file
SakuraIO.h Show annotated file Show diff for this revision Revisions of this file
SakuraIOAlphaCompat.cpp Show annotated file Show diff for this revision Revisions of this file
SakuraIOAlphaCompat.h Show annotated file Show diff for this revision Revisions of this file
SakuraIO_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
SakuraIO_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
commands.h Show annotated file Show diff for this revision Revisions of this file
debug.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f3b80b2bdb14 SakuraIO.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO.cpp	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,349 @@
+/* SAKURA Internet IoT Communication Module Library for mbed
+ *
+ * Copyright (c) SAKURA Internet Inc.
+ *   The MIT License (MIT)
+ *   https://github.com/sakura-internet/SakuraIOArduino
+ *
+ * https://iot.sakura.ad.jp/
+ *
+ * Porting to mbed by sakurafan. 2016
+ */
+
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "commands.h"
+#include "debug.h"
+
+uint8_t SakuraIO::executeCommand(uint8_t cmd,uint8_t requestLength, uint8_t *request, uint8_t *responseLength, uint8_t *response)
+{
+  uint8_t parity = 0x00;
+  uint8_t result = 0x00;
+  uint8_t tmpResponseLength, tmpResponse;
+
+  dbg("executeCommand\r\n");
+
+  this->begin();
+
+  // request
+  this->sendByte(cmd);
+  this->sendByte(requestLength);
+  parity = cmd ^ requestLength;
+  for(int16_t i=0; i<requestLength; i++){
+    parity ^= request[i];
+    this->sendByte(request[i]);
+  }
+  this->sendByte(parity);
+  //this->finishSending();
+
+  tmpResponseLength = 0;
+  if(responseLength != NULL){
+    tmpResponseLength = *responseLength;
+  }
+
+  wait_ms(10);
+
+  // response
+  this->startReceive(tmpResponseLength+3);
+  result = this->receiveByte();
+  if(result != CMD_ERROR_NONE){
+    dbg("Invalid status\r\n");
+    this->end();
+    return result;
+  }
+  tmpResponseLength = this->receiveByte();
+  parity = result ^ tmpResponseLength;
+  if(responseLength != NULL){
+    if(*responseLength < tmpResponseLength){
+      tmpResponseLength = *responseLength;
+    }
+    *responseLength = tmpResponseLength;
+  }
+  for(int16_t i=0; i<tmpResponseLength; i++){
+    tmpResponse = this->receiveByte();
+    parity ^= tmpResponse;
+    if(response != NULL){
+      response[i] = tmpResponse;
+    }
+  }
+  dbg("Parity\r\n");
+  uint8_t p = this->receiveByte(true);
+  parity ^= p;
+  dbg("Parity=%02x\r\n", p);
+  if(parity != 0x00){
+    result = CMD_ERROR_PARITY;
+    dbg("Invalid parity\r\n");
+  }else{
+    dbg("Success\r\n");
+  }
+  //this->finishReceiving();
+
+  this->end();
+  return result;
+}
+
+/* Common Commands */
+
+uint8_t SakuraIO::getConnectionStatus(){
+  uint8_t responseLength = 1;
+  uint8_t response[1] = {0x00};
+  if(executeCommand(CMD_GET_CONNECTION_STATUS, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
+    return 0x7F;
+  }
+  return response[0];
+}
+
+uint8_t SakuraIO::getSignalQuarity(){
+  uint8_t responseLength = 1;
+  uint8_t response[1] = {0x00};
+
+  if(executeCommand(CMD_GET_SIGNAL_QUALITY, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
+    return 0x00;
+  }
+  return response[0];
+}
+
+uint64_t SakuraIO::getUnixtime(){
+  uint8_t responseLength = 8;
+  uint8_t response[8] = {0x00};
+  if(executeCommand(CMD_GET_DATETIME, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){
+    return 0x00;
+  }
+  return *((uint64_t *)response);
+}
+
+uint8_t SakuraIO::echoback(uint8_t length, uint8_t *data, uint8_t *response){
+  uint8_t responseLength = length;
+  if(executeCommand(CMD_ECHO_BACK, length, data, &responseLength, response) != CMD_ERROR_NONE){
+    return 0x00;
+  }
+  return responseLength;
+}
+
+/* IO Commands */
+
+uint16_t SakuraIO::getADC(uint8_t channel){
+  uint8_t request[1] = {channel};
+  uint8_t response[2] = {0x00};
+  uint8_t responseLength = sizeof(response);
+  if(executeCommand(CMD_READ_ADC, 1, request, &responseLength, response) != CMD_ERROR_NONE){
+    return 0xffff;
+  }
+  return *((uint16_t *)response);
+}
+
+/* TX Commands */
+uint8_t SakuraIO::enqueueTxRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset){
+  uint8_t request[18] = {0x00};
+  uint8_t requestLength = 10;
+  request[0] = ch;
+  request[1] = type;
+  for(uint8_t i=0;i<length;i++){
+    request[2+i] = data[i];
+  }
+  if(offset != 0){
+    requestLength = 18;
+    for(uint8_t i=0;i<8;i++){
+      request[10+i] = ((uint8_t *)&offset)[i];
+    }
+  }
+  return executeCommand(CMD_TX_ENQUEUE, requestLength, request, NULL, NULL);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value, uint64_t offset){
+  return enqueueTxRaw(ch, 'i', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value, uint64_t offset){
+  return enqueueTxRaw(ch, 'I', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value, uint64_t offset){
+  return enqueueTxRaw(ch, 'l', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value, uint64_t offset){
+  return enqueueTxRaw(ch, 'L', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, float value, uint64_t offset){
+  return enqueueTxRaw(ch, 'f', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, double value, uint64_t offset){
+  return enqueueTxRaw(ch, 'd', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8], uint64_t offset){
+  return enqueueTxRaw(ch, 'b', 8, (uint8_t *)value, offset);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, float value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, double value){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8]){
+  return enqueueTx(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::getTxQueueLength(uint8_t *available, uint8_t *queued){
+  uint8_t response[2] = {0x00};
+  uint8_t responseLength = 2;
+  uint8_t ret = executeCommand(CMD_TX_LENGTH, 0, NULL, &responseLength, response);
+  *available = response[0];
+  *queued = response[1];
+  return ret;
+}
+
+uint8_t SakuraIO::clearTx(){
+  return executeCommand(CMD_TX_CLEAR, 0, NULL, NULL, NULL);
+}
+
+uint8_t SakuraIO::send(){
+  return executeCommand(CMD_TX_SEND, 0, NULL, NULL, NULL);
+}
+
+uint8_t SakuraIO::getTxStatus(uint8_t *queue, uint8_t *immediate){
+  uint8_t response[2] = {0x00};
+  uint8_t responseLength = 2;
+  uint8_t ret = executeCommand(CMD_TX_STAT, 0, NULL, &responseLength, response);
+  *queue = response[0];
+  *immediate = response[1];
+  return ret;
+}
+
+/* RX Commands */
+
+uint8_t SakuraIO::dequeueRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){
+  uint8_t response[18] = {0x00};
+  uint8_t responseLength = 18;
+  uint8_t ret = executeCommand(CMD_RX_DEQUEUE, 0, NULL, &responseLength, response);
+  if(ret != CMD_ERROR_NONE){
+    return ret;
+  }
+
+  *ch = response[0];
+  *type = response[1];
+  for(uint8_t i=0; i<8; i++){
+    value[i] = response[2+i];
+  }
+  for(uint8_t i=0; i<8; i++){
+    ((uint8_t *)offset)[i] = response[10+i];
+  }
+
+  return ret;
+}
+
+uint8_t SakuraIO::peekRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){
+  uint8_t response[18] = {0x00};
+  uint8_t responseLength = 18;
+  uint8_t ret = executeCommand(CMD_RX_PEEK, 0, NULL, &responseLength, response);
+  if(ret != CMD_ERROR_NONE){
+    return ret;
+  }
+
+  *ch = response[0];
+  *type = response[1];
+  for(uint8_t i=0; i<8; i++){
+    value[0] = response[2+i];
+  }
+  for(uint8_t i=0; i<8; i++){
+    ((uint8_t *)offset)[i] = response[10+i];
+  }
+
+  return ret;
+}
+
+uint8_t SakuraIO::getRxQueueLength(uint8_t *available, uint8_t *queued){
+  uint8_t response[2] = {0x00};
+  uint8_t responseLength = 2;
+  uint8_t ret = executeCommand(CMD_RX_LENGTH, 0, NULL, &responseLength, response);
+  *available = response[0];
+  *queued = response[1];
+  return ret;
+}
+
+uint8_t SakuraIO::clearRx(){
+  return executeCommand(CMD_RX_CLEAR, 0, NULL, NULL, NULL);
+}
+
+/* Operation command */
+
+uint16_t SakuraIO::getProductID(){
+  uint8_t response[2] = {0x00};
+  uint8_t responseLength = 2;
+  uint8_t ret = executeCommand(CMD_GET_PRODUCT_ID, 0, NULL, &responseLength, response);
+  if(ret != CMD_ERROR_NONE){
+    return 0x00;
+  }
+  return *((uint16_t *)response);
+}
+
+uint8_t SakuraIO::getUniqueID(char *data){
+  uint8_t response[11] = {0x00};
+  uint8_t responseLength = 10;
+  uint8_t ret = executeCommand(CMD_GET_UNIQUE_ID, 0, NULL, &responseLength, response);
+  if(ret != CMD_ERROR_NONE){
+    return ret;
+  }
+  for(uint8_t i=0; i<responseLength; i++){
+    data[i] = (char)response[i];
+  }
+  data[responseLength] = 0x00;
+  return ret;
+}
+
+uint8_t SakuraIO::getFirmwareVersion(char *data){
+  uint8_t response[33] = {0x00};
+  uint8_t responseLength = 32;
+  uint8_t ret = executeCommand(CMD_GET_FIRMWARE_VERSION, 0, NULL, &responseLength, response);
+  if(ret != CMD_ERROR_NONE){
+    return ret;
+  }
+  for(uint8_t i=0; i<responseLength; i++){
+    data[i] = (char)response[i];
+  }
+  data[responseLength] = 0x00;
+  return ret;
+}
+
+uint8_t SakuraIO::unlock(){
+  uint8_t request[4] = {0x53, 0x6B, 0x72, 0x61};
+  return executeCommand(CMD_UNLOCK, 4, request, NULL, NULL);
+}
+
+uint8_t SakuraIO::updateFirmware(){
+  return executeCommand(CMD_UPDATE_FIRMWARE, 0, 0, NULL, NULL);
+}
+
+uint8_t SakuraIO::getFirmwareUpdateStatus(){
+  uint8_t response[1] = {0x00};
+  uint8_t responseLength = 1;
+  if(executeCommand(CMD_GET_UPDATE_FIRMWARE_STATUS, 0, 0, &responseLength, response) != CMD_ERROR_NONE){
+      return 0xff;
+  }
+  return response[0];
+}
+
+uint8_t SakuraIO::reset(){
+  return executeCommand(CMD_SOFTWARE_RESET, 0, 0, NULL, NULL);
+}
diff -r 000000000000 -r f3b80b2bdb14 SakuraIO.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO.h	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,107 @@
+/* SAKURA Internet IoT Communication Module Library for mbed
+ *
+ * Copyright (c) SAKURA Internet Inc.
+ *   The MIT License (MIT)
+ *   https://github.com/sakura-internet/SakuraIOArduino
+ *
+ * https://iot.sakura.ad.jp/
+ *
+ * Porting to mbed by sakurafan. 2016
+ */
+
+#ifndef _SAKURAIO_H_
+#define _SAKURAIO_H_
+
+#include "mbed.h"
+#include "commands.h"
+
+class SakuraIO
+{
+protected:
+  virtual void begin(){}
+  virtual void end(){}
+
+  virtual void sendByte(uint8_t data){}
+  //virtual void finishSending(){}
+
+  virtual uint8_t startReceive(uint8_t length){return length;};
+  virtual uint8_t receiveByte(){return 0x00;}
+  virtual uint8_t receiveByte(bool stop){return 0x00;}
+  //virtual void finishReceiving(){}
+
+  uint8_t executeCommand(uint8_t cmd,uint8_t requestLength, uint8_t *request, uint8_t *responseLength, uint8_t *response);
+
+  uint8_t enqueueTxRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset);
+
+public:
+  uint8_t getConnectionStatus();
+  uint8_t getSignalQuarity();
+  uint64_t getUnixtime();
+  uint8_t echoback(uint8_t length, uint8_t *data, uint8_t *response);
+  uint16_t getADC(uint8_t channel);
+  uint8_t enqueueTx(uint8_t ch, int32_t value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, uint32_t value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, int64_t value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, uint64_t value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, float value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, double value, uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, uint8_t value[8], uint64_t offset);
+  uint8_t enqueueTx(uint8_t ch, int32_t value);
+  uint8_t enqueueTx(uint8_t ch, uint32_t value);
+  uint8_t enqueueTx(uint8_t ch, int64_t value);
+  uint8_t enqueueTx(uint8_t ch, uint64_t value);
+  uint8_t enqueueTx(uint8_t ch, float value);
+  uint8_t enqueueTx(uint8_t ch, double value);
+  uint8_t enqueueTx(uint8_t ch, uint8_t value[8]);
+  uint8_t getTxQueueLength(uint8_t *available, uint8_t *queued);
+  uint8_t clearTx();
+  uint8_t getTxStatus(uint8_t *queue, uint8_t *immediate);
+  uint8_t send();
+  uint8_t dequeueRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset);
+  uint8_t peekRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset);
+  uint8_t getRxQueueLength(uint8_t *available, uint8_t *queued);
+  uint8_t clearRx();
+  uint16_t getProductID();
+  uint8_t getUniqueID(char *data);
+  uint8_t getFirmwareVersion(char *data);
+  uint8_t unlock();
+  uint8_t updateFirmware();
+  uint8_t getFirmwareUpdateStatus();
+  uint8_t reset();
+};
+
+class SakuraIO_SPI : public SakuraIO
+{
+protected:
+  SPI _spi;
+  DigitalOut _cs;
+
+  virtual void begin();
+  virtual void end();
+  virtual void sendByte(uint8_t data);
+  virtual uint8_t receiveByte(bool stop);
+  virtual uint8_t receiveByte();
+public:
+  SakuraIO_SPI(SPI &spi, PinName cs);
+  SakuraIO_SPI(PinName mosi, PinName miso, PinName sck, PinName cs);
+};
+
+class SakuraIO_I2C : public SakuraIO
+{
+protected:
+  I2C _i2c;
+  int _length;
+
+  virtual void begin();
+  virtual void end();
+  virtual void sendByte(uint8_t data);
+  virtual uint8_t startReceive(uint8_t length);
+  virtual uint8_t receiveByte(bool stop);
+  virtual uint8_t receiveByte();
+  uint8_t mode;
+public:
+  SakuraIO_I2C (I2C &i2c);
+  SakuraIO_I2C (PinName sda, PinName scl);
+};
+
+#endif // _SAKURAIO_H_
diff -r 000000000000 -r f3b80b2bdb14 SakuraIOAlphaCompat.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIOAlphaCompat.cpp	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,65 @@
+#include <stdint.h>
+#include "SakuraIO.h"
+#include "SakuraIOAlphaCompat.h"
+
+SakuraIOAlphaCompatSPI::SakuraIOAlphaCompatSPI(SPI &spi, PinName cs){
+  sakuraio = new SakuraIO_SPI(spi, cs);
+}
+
+SakuraIOAlphaCompatSPI::SakuraIOAlphaCompatSPI(PinName mosi, PinName miso, PinName sck, PinName cs){
+  sakuraio = new SakuraIO_SPI(mosi, miso, sck, cs);
+}
+
+SakuraIOAlphaCompatI2C::SakuraIOAlphaCompatI2C(I2C &i2c){
+  sakuraio = new SakuraIO_I2C(i2c);
+}
+
+SakuraIOAlphaCompatI2C::SakuraIOAlphaCompatI2C(PinName sda, PinName scl){
+  sakuraio = new SakuraIO_I2C(sda, scl);
+}
+
+uint8_t SakuraIOAlphaCompat::getNetworkStatus() {
+  if((sakuraio->getConnectionStatus() & 0x80) == 0x80){
+    return 1;
+  }
+  return 0;
+}
+
+void SakuraIOAlphaCompat::transmit(uint8_t mode){
+  if(mode != TRANSMIT_ONCE){
+    return;
+  }
+  sakuraio->send();
+}
+
+uint8_t SakuraIOAlphaCompat::getTxChannelStatus(uint8_t ch){
+  uint8_t available = 0;
+  uint8_t queued = 0;
+  sakuraio->getTxQueueLength(&available, &queued);
+  if(queued > 0){
+    return 1;
+  }
+  return 0;
+}
+
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, int32_t data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, uint32_t data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, int64_t data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, uint64_t data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, float data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, double data){
+  sakuraio->enqueueTx(ch, data);
+}
+void SakuraIOAlphaCompat::writeChannel(uint8_t ch, uint8_t *data){
+  sakuraio->enqueueTx(ch, data);
+}
diff -r 000000000000 -r f3b80b2bdb14 SakuraIOAlphaCompat.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIOAlphaCompat.h	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,46 @@
+#ifndef _SAKURAIOALPHACOMPAT_H_
+#define _SAKURAIOALPHACOMPAT_H_
+
+#include <stdint.h>
+#include "SakuraIO.h"
+
+#define TRANSMIT_NONE   0x00
+//#define TRANSMIT_AUTO   0x01 // deprecated
+#define TRANSMIT_ONCE   0x02
+
+#define SakuraAlphaSPI SakuraIOAlphaCompatSPI
+#define SakuraAlphaI2C SakuraIOAlphaCompatI2C
+
+class SakuraIOAlphaCompat {
+protected:
+  SakuraIO *sakuraio;
+public:
+  void writeChannel(uint8_t ch, int32_t data);
+  void writeChannel(uint8_t ch, uint32_t data);
+  void writeChannel(uint8_t ch, int64_t data);
+  void writeChannel(uint8_t ch, uint64_t data);
+  void writeChannel(uint8_t ch, float data);
+  void writeChannel(uint8_t ch, double data);
+  void writeChannel(uint8_t ch, uint8_t *data);
+  void transmit(uint8_t mode);
+  uint8_t getTxChannelStatus(uint8_t ch);
+  uint8_t getNetworkStatus();
+};
+
+class SakuraIOAlphaCompatSPI : public SakuraIOAlphaCompat
+{
+public:
+  SakuraIOAlphaCompatSPI(SPI &spi, PinName cs);
+  SakuraIOAlphaCompatSPI(PinName mosi, PinName miso, PinName sck, PinName cs);
+};
+
+class SakuraIOAlphaCompatI2C : public SakuraIOAlphaCompat
+{
+public:
+  SakuraIOAlphaCompatI2C(I2C &i2c);
+  SakuraIOAlphaCompatI2C(PinName sda, PinName scl);
+};
+
+
+
+#endif
diff -r 000000000000 -r f3b80b2bdb14 SakuraIO_I2C.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO_I2C.cpp	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "debug.h"
+
+#define SAKURAIO_SLAVE_ADDR (0x4F << 1)
+
+#define MODE_IDLE  0x00
+#define MODE_WRITE 0x01
+#define MODE_READ  0x02
+
+void SakuraIO_I2C::begin(){
+  mode = MODE_IDLE;
+}
+
+void SakuraIO_I2C::end(){
+  switch(mode){
+    case MODE_WRITE:
+      _i2c.stop();
+      break;
+    case MODE_READ:
+      while (_length > 0) {
+        _length --;
+        _i2c.read(_length > 0 ? 1 : 0);
+      }
+      _i2c.stop();
+      break;
+  }
+
+  mode = MODE_IDLE;
+}
+
+void SakuraIO_I2C::sendByte(uint8_t data){
+  if(mode != MODE_WRITE){
+    dbg("beginTr\r\n");
+    _i2c.start();
+    _i2c.write(SAKURAIO_SLAVE_ADDR);
+    mode = MODE_WRITE;
+  }
+  dbg("Write=%02x\r\n", data);
+  _i2c.write(data);
+}
+
+
+uint8_t SakuraIO_I2C::startReceive(uint8_t length){
+  if(mode != MODE_IDLE){
+    dbg("endTransmission\r\n");
+    _i2c.stop();
+  }
+  dbg("requestForm=%d\r\n", length);
+  _length = length;
+  _i2c.start();
+  _i2c.write(SAKURAIO_SLAVE_ADDR | 1);
+  mode = MODE_READ;
+  return 0;
+}
+
+uint8_t SakuraIO_I2C::receiveByte(){
+  return receiveByte(false);
+}
+
+uint8_t SakuraIO_I2C::receiveByte(bool stop){
+  uint8_t ret = 0;
+  if (_length > 0) {
+    _length --;
+    ret = _i2c.read(_length > 0 ? 1 : 0);
+  }
+  if(stop){
+    end();
+//    _i2c.stop();
+//    mode = MODE_IDLE;
+  }
+  dbg("Read=%d\r\n", ret);
+  return ret;
+}
+
+SakuraIO_I2C::SakuraIO_I2C (I2C &i2c) : _i2c(i2c) {
+  mode = MODE_IDLE;
+}
+
+SakuraIO_I2C::SakuraIO_I2C (PinName sda, PinName scl) : _i2c(sda, scl) {
+  mode = MODE_IDLE;
+}
diff -r 000000000000 -r f3b80b2bdb14 SakuraIO_SPI.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO_SPI.cpp	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "debug.h"
+
+
+void SakuraIO_SPI::begin(){
+  dbg("CS=0\r\n");
+  _cs = 0;
+}
+
+void SakuraIO_SPI::end(){
+  dbg("CS=1\r\n");
+  _cs = 1;
+  wait_ms(20);
+}
+
+void SakuraIO_SPI::sendByte(uint8_t data){
+  wait_ms(20);
+  dbg("Send=%02x\r\n", data);
+  _spi.write(data);
+}
+
+
+uint8_t SakuraIO_SPI::receiveByte(bool stop){
+  return receiveByte();
+}
+
+uint8_t SakuraIO_SPI::receiveByte(){
+  uint8_t ret;
+  wait_ms(20);
+  ret = _spi.write(0);
+  dbg("Recv=%d\r\n", ret);
+  return ret;
+}
+
+SakuraIO_SPI::SakuraIO_SPI(SPI &spi, PinName cs) : _spi(spi), _cs(cs) {
+    _cs = 1;
+}
+
+SakuraIO_SPI::SakuraIO_SPI(PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _cs(cs) {
+    _cs = 1;
+}
diff -r 000000000000 -r f3b80b2bdb14 commands.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.h	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,48 @@
+// Common
+#define CMD_GET_CONNECTION_STATUS 0x01  // OK
+#define CMD_GET_SIGNAL_QUALITY	  0x02  // OK
+#define CMD_GET_DATETIME	        0x03  // OK
+#define CMD_ECHO_BACK	            0x0f  // OK
+
+// IO
+#define CMD_READ_ADC              0x10  // OK
+
+// Transmit
+#define CMD_TX_ENQUEUE      	    0x20  // OK
+#define CMD_TX_SENDIMMED          0x21  // OK
+#define CMD_TX_LENGTH	            0x22  // OK
+#define CMD_TX_CLEAR	            0x23  // OK
+#define CMD_TX_SEND	              0x24  // OK
+#define CMD_TX_STAT	              0x25  // OK
+
+// Receive
+#define CMD_RX_DEQUEUE	          0x30  // OK
+#define CMD_RX_PEEK	              0x31  // OK
+#define CMD_RX_LENGTH	            0x32  // OK
+#define CMD_RX_CLEAR	            0x33  // OK
+
+// File Download
+#define CMD_START_FILE_DOWNLOAD       0x40  // OK
+#define CMD_GET_FILE_METADATA         0x41  // OK
+#define CMD_GET_FILE_DOWNLOAD_STATUS  0x42  // OK
+#define CMD_CANCEL_FILE_DOWNLOAD      0x43  // OK
+#define CMD_GET_FILE_DATA             0x44  // OK
+
+// Operation
+#define CMD_GET_PRODUCT_ID        0xA0  // OK
+#define CMD_GET_UNIQUE_ID         0xA1  // OK
+#define CMD_GET_FIRMWARE_VERSION  0xA2  // OK
+#define CMD_UNLOCK                0xA8  // OK
+#define CMD_UPDATE_FIRMWARE       0xA9  // OK
+#define CMD_GET_UPDATE_FIRMWARE_STATUS       0xAA  // OK
+#define CMD_SOFTWARE_RESET        0xAF  // OK
+
+
+// Response
+#define CMD_ERROR_NONE             0x01
+#define CMD_ERROR_PARITY           0x02
+#define CMD_ERROR_MISSING          0x03
+#define CMD_ERROR_INVALID_SYNTAX   0x04
+#define CMD_ERROR_RUNTIME          0x05
+#define CMD_ERROR_LOCKED           0x06
+#define CMD_ERROR_BUSY             0x07
diff -r 000000000000 -r f3b80b2bdb14 debug.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/debug.h	Sun Nov 13 06:52:34 2016 +0000
@@ -0,0 +1,11 @@
+#ifndef __SAKURA_IO_DEBUG_H__
+#define __SAKURA_IO_DEBUG_H__
+
+
+#if defined(SAKURA_DEBUG)
+#define dbg(...) printf("" __VA_ARGS__)
+#else
+#define dbg(...)
+#endif
+
+#endif //__SAKURA_IO_DEBUG_H__