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.

Files at this revision

API Documentation at this revision

Comitter:
spiralray
Date:
Sat Nov 19 03:20:17 2016 +0000
Child:
1:20e1dfe45dab
Commit message:
First commit(This only works with SPI)

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
SakuraIO/commands.h Show annotated file Show diff for this revision Revisions of this file
SakuraIO/debug.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO.cpp	Sat Nov 19 03:20:17 2016 +0000
@@ -0,0 +1,339 @@
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "SakuraIO/commands.h"
+#include "SakuraIO/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;
+
+  dbgln("executeCommand");
+
+  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){
+    dbgln("Invalid status");
+    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;
+    }
+  }
+  dbgln("Parity");
+  uint8_t p = this->receiveByte(true);
+  parity ^= p;
+  dbg("Parity=");
+  dbgln(p);
+  if(parity != 0x00){
+    result = CMD_ERROR_PARITY;
+    dbgln("Invalid parity");
+  }else{
+    dbgln("Success");
+  }
+  //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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO.h	Sat Nov 19 03:20:17 2016 +0000
@@ -0,0 +1,90 @@
+#ifndef _SAKURAIO_H_
+#define _SAKURAIO_H_
+
+#include <SakuraIO/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;
+    void begin();
+    void end();
+    void sendByte(uint8_t data);
+    uint8_t receiveByte(bool stop);
+    uint8_t receiveByte();
+public:
+    SakuraIO_SPI(SPI &_spi, DigitalOut &_cs);
+};
+
+class SakuraIO_I2C : public SakuraIO
+{
+protected:
+  I2C &i2c;
+  void begin();
+  void end();
+  void sendByte(uint8_t data);
+  uint8_t startReceive(uint8_t length);
+  uint8_t receiveByte(bool stop);
+  uint8_t receiveByte();
+  uint8_t mode;
+public:
+  SakuraIO_I2C(I2C &_i2c);
+};
+
+#endif // _SAKURAIO_H_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO/commands.h	Sat Nov 19 03:20:17 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO/debug.h	Sat Nov 19 03:20:17 2016 +0000
@@ -0,0 +1,14 @@
+#ifndef __SAKURA_IO_DEBUG_H__
+#define __SAKURA_IO_DEBUG_H__
+
+
+#if defined(SAKURA_DEBUG)
+#define dbg(...) printf(__VA_ARGS__)
+#define dbgln(...) DEBUG_PRINTF_LN(__VA_ARGS__, "\r\n")
+#define DEBUG_PRINTF_LN(type, fmt, ...) printf(fmt "%s", __VA_ARGS__)
+#else
+#define dbg(...)
+#define dbgln(...)
+#endif
+
+#endif //__SAKURA_IO_DEBUG_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO_I2C.cpp	Sat Nov 19 03:20:17 2016 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "SakuraIO/debug.h"
+
+#define SAKURAIO_SLAVE_ADDR 0x4F
+
+#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:
+      i2c.read(true);
+      break;
+  }
+
+  mode = MODE_IDLE;
+}
+
+void SakuraIO_I2C::sendByte(uint8_t data){
+  if(mode != MODE_WRITE){
+    dbgln("beginTr");
+    i2c.start();
+    i2c.write(SAKURAIO_SLAVE_ADDR<<1);
+    mode = MODE_WRITE;
+  }
+  dbg("Write=");
+  dbgln(data);
+  i2c.write(data);
+}
+
+
+uint8_t SakuraIO_I2C::startReceive(uint8_t length){
+  dbgln("endTransmission");
+  i2c.stop();
+  dbg("requestForm=");
+  dbgln(length);
+  i2c.start();
+  i2c.write(SAKURAIO_SLAVE_ADDR<<1 | 1);
+  mode = MODE_READ;
+}
+
+uint8_t SakuraIO_I2C::receiveByte(){
+  return i2c.read(true);
+}
+
+uint8_t SakuraIO_I2C::receiveByte(bool stop){
+  uint8_t ret = 0;
+  ret = i2c.read(!stop);
+  dbg("Read=");
+  dbgln(ret);
+  return ret;
+}
+
+SakuraIO_I2C::SakuraIO_I2C(I2C &_i2c): i2c(_i2c){
+  mode = MODE_IDLE;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SakuraIO_SPI.cpp	Sat Nov 19 03:20:17 2016 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+#include "SakuraIO.h"
+#include "SakuraIO/debug.h"
+
+
+void SakuraIO_SPI::begin(){
+  dbgln("CS=0");
+  cs = 0;
+}
+
+void SakuraIO_SPI::end(){
+  dbgln("CS=1");
+  cs = 1;
+  wait_us(20);
+}
+
+void SakuraIO_SPI::sendByte(uint8_t data){
+  wait_us(20);
+  dbg("Send=");
+  dbgln(data);
+  wait_us(10);
+  spi.write(data);
+}
+
+
+uint8_t SakuraIO_SPI::receiveByte(bool stop){
+  return receiveByte();
+}
+
+uint8_t SakuraIO_SPI::receiveByte(){
+  uint8_t ret;
+  wait_us(10);
+  ret = spi.write(0x00);
+  dbg("Recv=");
+  dbgln(ret);
+  return ret;
+}
+
+SakuraIO_SPI::SakuraIO_SPI(SPI &_spi, DigitalOut &_cs): spi(_spi), cs(_cs){
+    cs = 1;
+}