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.
- SCM-LTE-01 (sakura.io Module (LTE) )
- SCM-LTE-Beta (Sakura Communication Module (LTE) β)
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.
Revision 11:16e726ca39c6, committed 2017-09-28
- Comitter:
- chibiegg
- Date:
- Thu Sep 28 16:58:17 2017 +0900
- Parent:
- 10:bcf9c50a76ed
- Child:
- 12:70faddd6241b
- Commit message:
- Follow SakuraIOArduino v1.1.3
Changed in this revision
--- a/SakuraIO.cpp Thu Sep 07 13:38:19 2017 +0900
+++ b/SakuraIO.cpp Thu Sep 28 16:58:17 2017 +0900
@@ -27,11 +27,13 @@
#include "SakuraIO/commands.h"
#include "SakuraIO/debug.h"
+#define delay(ms) wait_ms(ms)
+
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;
+ uint8_t reservedResponseLength, tmpResponse, receivedResponseLength;
dbgln("executeCommand");
@@ -48,33 +50,31 @@
this->sendByte(parity);
//this->finishSending();
- tmpResponseLength = 0;
+ reservedResponseLength = 0;
if(responseLength != NULL){
- tmpResponseLength = *responseLength;
+ reservedResponseLength = *responseLength;
}
- wait_ms(10);
+ delay(10);
// response
- this->startReceive(tmpResponseLength+3);
+ this->startReceive(reservedResponseLength+3);
result = this->receiveByte();
if(result != CMD_ERROR_NONE){
dbgln("Invalid status");
this->end();
return result;
}
- tmpResponseLength = this->receiveByte();
- parity = result ^ tmpResponseLength;
+ receivedResponseLength = this->receiveByte();
if(responseLength != NULL){
- if(*responseLength < tmpResponseLength){
- tmpResponseLength = *responseLength;
- }
- *responseLength = tmpResponseLength;
+ *responseLength = receivedResponseLength;
}
- for(int16_t i=0; i<tmpResponseLength; i++){
+
+ parity = result ^ receivedResponseLength;
+ for(int16_t i=0; i<receivedResponseLength; i++){
tmpResponse = this->receiveByte();
parity ^= tmpResponse;
- if(response != NULL){
+ if(response != NULL && i<reservedResponseLength){
response[i] = tmpResponse;
}
}
@@ -224,6 +224,81 @@
return enqueueTx(ch, value, (uint32_t)0);
}
+uint8_t SakuraIO::sendImmediatelyRaw(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_SENDIMMED, requestLength, request, NULL, NULL);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, int32_t value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'i', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint32_t value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'I', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, int64_t value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'l', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint64_t value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'L', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, float value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'f', 4, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, double value, uint64_t offset){
+ return sendImmediatelyRaw(ch, 'd', 8, (uint8_t *)&value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint8_t value[8], uint64_t offset){
+ return sendImmediatelyRaw(ch, 'b', 8, (uint8_t *)value, offset);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, int32_t value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint32_t value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, int64_t value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint64_t value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, float value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, double value){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+uint8_t SakuraIO::sendImmediately(uint8_t ch, uint8_t value[8]){
+ return sendImmediately(ch, value, (uint32_t)0);
+}
+
+
+
uint8_t SakuraIO::getTxQueueLength(uint8_t *available, uint8_t *queued){
uint8_t response[2] = {0x00};
uint8_t responseLength = 2;
@@ -305,6 +380,39 @@
return executeCommand(CMD_RX_CLEAR, 0, NULL, NULL, NULL);
}
+/* File command */
+uint8_t SakuraIO::startFileDownload(uint16_t fileId){
+ return executeCommand(CMD_START_FILE_DOWNLOAD, 2, (uint8_t *)&fileId, NULL, NULL);
+}
+
+uint8_t SakuraIO::cancelFileDownload(){
+ return executeCommand(CMD_CANCEL_FILE_DOWNLOAD, 0, NULL, NULL, NULL);
+}
+
+uint8_t SakuraIO::getFileMetaData(uint8_t *status, uint32_t *totalSize, uint64_t *timestamp, uint32_t *crc){
+ uint8_t response[17] = {0x00};
+ uint8_t responseLength = 17;
+ uint8_t ret = executeCommand(CMD_GET_FILE_METADATA, 0, NULL, &responseLength, response);
+ *status = response[0];
+ *totalSize = *(uint32_t *)(response+1);
+ *timestamp = *(uint64_t *)(response+5);
+ *crc = *(uint32_t *)(response+13);
+ return ret;
+}
+
+uint8_t SakuraIO::getFileDownloadStatus(uint8_t *status, uint32_t *currentSize){
+ uint8_t response[5] = {0x00};
+ uint8_t responseLength = 5;
+ uint8_t ret = executeCommand(CMD_GET_FILE_DOWNLOAD_STATUS, 0, NULL, &responseLength, response);
+ *status = response[0];
+ *currentSize = *(uint32_t *)(response+1);
+ return ret;
+}
+
+uint8_t SakuraIO::getFileData(uint8_t *size, uint8_t *data){
+ return executeCommand(CMD_GET_FILE_DATA, 1, size, size, data);
+}
+
/* Operation command */
uint16_t SakuraIO::getProductID(){
--- a/SakuraIO.h Thu Sep 07 13:38:19 2017 +0900
+++ b/SakuraIO.h Thu Sep 28 16:58:17 2017 +0900
@@ -24,19 +24,19 @@
#ifndef _SAKURAIO_H_
#define _SAKURAIO_H_
-
+
#include "mbed.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;
};
@@ -47,18 +47,18 @@
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(); // Deprecated
uint8_t getSignalQuality();
uint64_t getUnixtime();
uint8_t echoback(uint8_t length, uint8_t *data, uint8_t *response);
- uint16_t getADC(uint8_t channel);
+ uint16_t getADC(uint8_t channel); // Deprecated
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);
@@ -73,6 +73,21 @@
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 sendImmediatelyRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, int32_t value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, uint32_t value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, int64_t value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, uint64_t value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, float value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, double value, uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, uint8_t value[8], uint64_t offset);
+ uint8_t sendImmediately(uint8_t ch, int32_t value);
+ uint8_t sendImmediately(uint8_t ch, uint32_t value);
+ uint8_t sendImmediately(uint8_t ch, int64_t value);
+ uint8_t sendImmediately(uint8_t ch, uint64_t value);
+ uint8_t sendImmediately(uint8_t ch, float value);
+ uint8_t sendImmediately(uint8_t ch, double value);
+ uint8_t sendImmediately(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);
@@ -81,6 +96,11 @@
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();
+ uint8_t startFileDownload(uint16_t fileId);
+ uint8_t cancelFileDownload();
+ uint8_t getFileMetaData(uint8_t *status, uint32_t *totalSize, uint64_t *timestamp, uint32_t *crc);
+ uint8_t getFileDownloadStatus(uint8_t *status, uint32_t *currentSize);
+ uint8_t getFileData(uint8_t *size, uint8_t *data);
uint16_t getProductID();
uint8_t getUniqueID(char *data);
uint8_t getFirmwareVersion(char *data);
@@ -89,7 +109,7 @@
uint8_t getFirmwareUpdateStatus();
uint8_t reset();
};
-
+
class SakuraIO_SPI : public SakuraIO
{
protected:
@@ -103,14 +123,14 @@
public:
SakuraIO_SPI(SPI &_spi, DigitalOut &_cs);
};
-
+
class SakuraIO_I2C : public SakuraIO
{
protected:
I2C *i2c_p;
I2C &i2c;
int _length;
-
+
virtual void begin();
virtual void end();
virtual void sendByte(uint8_t data);
@@ -122,6 +142,5 @@
SakuraIO_I2C (I2C &_i2c);
SakuraIO_I2C (PinName sda, PinName scl);
};
-
+
#endif // _SAKURAIO_H_
-
\ No newline at end of file
--- a/SakuraIO/commands.h Thu Sep 07 13:38:19 2017 +0900 +++ b/SakuraIO/commands.h Thu Sep 28 16:58:17 2017 +0900 @@ -1,19 +1,19 @@ /* SAKURA Internet IoT Communication Module Library for mbed - * + * * 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. @@ -23,43 +23,43 @@ */ // 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 +#define CMD_GET_CONNECTION_STATUS 0x01 +#define CMD_GET_SIGNAL_QUALITY 0x02 +#define CMD_GET_DATETIME 0x03 +#define CMD_ECHO_BACK 0x0f // IO -#define CMD_READ_ADC 0x10 // OK +#define CMD_READ_ADC 0x10 // 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 +#define CMD_TX_ENQUEUE 0x20 +#define CMD_TX_SENDIMMED 0x21 +#define CMD_TX_LENGTH 0x22 +#define CMD_TX_CLEAR 0x23 +#define CMD_TX_SEND 0x24 +#define CMD_TX_STAT 0x25 // 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 +#define CMD_RX_DEQUEUE 0x30 +#define CMD_RX_PEEK 0x31 +#define CMD_RX_LENGTH 0x32 +#define CMD_RX_CLEAR 0x33 // 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 +#define CMD_START_FILE_DOWNLOAD 0x40 +#define CMD_GET_FILE_METADATA 0x41 +#define CMD_GET_FILE_DOWNLOAD_STATUS 0x42 +#define CMD_CANCEL_FILE_DOWNLOAD 0x43 +#define CMD_GET_FILE_DATA 0x44 // 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 +#define CMD_GET_PRODUCT_ID 0xA0 +#define CMD_GET_UNIQUE_ID 0xA1 +#define CMD_GET_FIRMWARE_VERSION 0xA2 +#define CMD_UNLOCK 0xA8 +#define CMD_UPDATE_FIRMWARE 0xA9 +#define CMD_GET_UPDATE_FIRMWARE_STATUS 0xAA +#define CMD_SOFTWARE_RESET 0xAF // Response @@ -70,3 +70,11 @@ #define CMD_ERROR_RUNTIME 0x05 #define CMD_ERROR_LOCKED 0x06 #define CMD_ERROR_BUSY 0x07 + + +// FileStatus +#define FILE_STATUS_ERROR 0x01 +#define FILE_STATUS_INVALID_REQUEST 0x02 +#define FILE_STATUS_NOTFOUND 0x81 +#define FILE_STATUS_SERVER_ERROR 0x82 +#define FILE_STATUS_INVALID_DATA 0x83
--- a/SakuraIO_I2C.cpp Thu Sep 07 13:38:19 2017 +0900
+++ b/SakuraIO_I2C.cpp Thu Sep 28 16:58:17 2017 +0900
@@ -25,18 +25,18 @@
#include "mbed.h"
#include "SakuraIO.h"
#include "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) {
@@ -51,10 +51,10 @@
i2c.stop();
break;
}
-
+
mode = MODE_IDLE;
}
-
+
void SakuraIO_I2C::sendByte(uint8_t data)
{
if (mode != MODE_WRITE) {
@@ -66,7 +66,7 @@
dbg("Write=%02x\r\n", data);
i2c.write(data);
}
-
+
uint8_t SakuraIO_I2C::startReceive(uint8_t length)
{
if (mode != MODE_IDLE) {
@@ -75,17 +75,18 @@
}
dbg("requestForm=%d\r\n", length);
_length = length;
+ wait_ms(10);
i2c.start();
i2c.write(SAKURAIO_SLAVE_ADDR << 1 | 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;
@@ -99,14 +100,14 @@
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_p(new I2C(sda, scl)),
i2c(*i2c_p)
{
mode = MODE_IDLE;
- }
\ No newline at end of file
+ }
--- a/SakuraIO_SPI.cpp Thu Sep 07 13:38:19 2017 +0900 +++ b/SakuraIO_SPI.cpp Thu Sep 28 16:58:17 2017 +0900 @@ -1,16 +1,16 @@ /* SAKURA Internet IoT Communication Module Library for mbed - * + * * 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. *
SAKURA Internet
SCM-LTE-Beta
SCM-LTE-01