SAKURA Internet IoT Beta Communication Module Library for mbed
Dependents: SakuraIO_Standard SakuraIO_FirmwareUpdate
SakuraIO.cpp@0:f3b80b2bdb14, 2016-11-13 (annotated)
- Committer:
- sakurafan
- Date:
- Sun Nov 13 06:52:34 2016 +0000
- Revision:
- 0:f3b80b2bdb14
1st ported build;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakurafan | 0:f3b80b2bdb14 | 1 | /* SAKURA Internet IoT Communication Module Library for mbed |
sakurafan | 0:f3b80b2bdb14 | 2 | * |
sakurafan | 0:f3b80b2bdb14 | 3 | * Copyright (c) SAKURA Internet Inc. |
sakurafan | 0:f3b80b2bdb14 | 4 | * The MIT License (MIT) |
sakurafan | 0:f3b80b2bdb14 | 5 | * https://github.com/sakura-internet/SakuraIOArduino |
sakurafan | 0:f3b80b2bdb14 | 6 | * |
sakurafan | 0:f3b80b2bdb14 | 7 | * https://iot.sakura.ad.jp/ |
sakurafan | 0:f3b80b2bdb14 | 8 | * |
sakurafan | 0:f3b80b2bdb14 | 9 | * Porting to mbed by sakurafan. 2016 |
sakurafan | 0:f3b80b2bdb14 | 10 | */ |
sakurafan | 0:f3b80b2bdb14 | 11 | |
sakurafan | 0:f3b80b2bdb14 | 12 | #include "mbed.h" |
sakurafan | 0:f3b80b2bdb14 | 13 | #include "SakuraIO.h" |
sakurafan | 0:f3b80b2bdb14 | 14 | #include "commands.h" |
sakurafan | 0:f3b80b2bdb14 | 15 | #include "debug.h" |
sakurafan | 0:f3b80b2bdb14 | 16 | |
sakurafan | 0:f3b80b2bdb14 | 17 | uint8_t SakuraIO::executeCommand(uint8_t cmd,uint8_t requestLength, uint8_t *request, uint8_t *responseLength, uint8_t *response) |
sakurafan | 0:f3b80b2bdb14 | 18 | { |
sakurafan | 0:f3b80b2bdb14 | 19 | uint8_t parity = 0x00; |
sakurafan | 0:f3b80b2bdb14 | 20 | uint8_t result = 0x00; |
sakurafan | 0:f3b80b2bdb14 | 21 | uint8_t tmpResponseLength, tmpResponse; |
sakurafan | 0:f3b80b2bdb14 | 22 | |
sakurafan | 0:f3b80b2bdb14 | 23 | dbg("executeCommand\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 24 | |
sakurafan | 0:f3b80b2bdb14 | 25 | this->begin(); |
sakurafan | 0:f3b80b2bdb14 | 26 | |
sakurafan | 0:f3b80b2bdb14 | 27 | // request |
sakurafan | 0:f3b80b2bdb14 | 28 | this->sendByte(cmd); |
sakurafan | 0:f3b80b2bdb14 | 29 | this->sendByte(requestLength); |
sakurafan | 0:f3b80b2bdb14 | 30 | parity = cmd ^ requestLength; |
sakurafan | 0:f3b80b2bdb14 | 31 | for(int16_t i=0; i<requestLength; i++){ |
sakurafan | 0:f3b80b2bdb14 | 32 | parity ^= request[i]; |
sakurafan | 0:f3b80b2bdb14 | 33 | this->sendByte(request[i]); |
sakurafan | 0:f3b80b2bdb14 | 34 | } |
sakurafan | 0:f3b80b2bdb14 | 35 | this->sendByte(parity); |
sakurafan | 0:f3b80b2bdb14 | 36 | //this->finishSending(); |
sakurafan | 0:f3b80b2bdb14 | 37 | |
sakurafan | 0:f3b80b2bdb14 | 38 | tmpResponseLength = 0; |
sakurafan | 0:f3b80b2bdb14 | 39 | if(responseLength != NULL){ |
sakurafan | 0:f3b80b2bdb14 | 40 | tmpResponseLength = *responseLength; |
sakurafan | 0:f3b80b2bdb14 | 41 | } |
sakurafan | 0:f3b80b2bdb14 | 42 | |
sakurafan | 0:f3b80b2bdb14 | 43 | wait_ms(10); |
sakurafan | 0:f3b80b2bdb14 | 44 | |
sakurafan | 0:f3b80b2bdb14 | 45 | // response |
sakurafan | 0:f3b80b2bdb14 | 46 | this->startReceive(tmpResponseLength+3); |
sakurafan | 0:f3b80b2bdb14 | 47 | result = this->receiveByte(); |
sakurafan | 0:f3b80b2bdb14 | 48 | if(result != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 49 | dbg("Invalid status\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 50 | this->end(); |
sakurafan | 0:f3b80b2bdb14 | 51 | return result; |
sakurafan | 0:f3b80b2bdb14 | 52 | } |
sakurafan | 0:f3b80b2bdb14 | 53 | tmpResponseLength = this->receiveByte(); |
sakurafan | 0:f3b80b2bdb14 | 54 | parity = result ^ tmpResponseLength; |
sakurafan | 0:f3b80b2bdb14 | 55 | if(responseLength != NULL){ |
sakurafan | 0:f3b80b2bdb14 | 56 | if(*responseLength < tmpResponseLength){ |
sakurafan | 0:f3b80b2bdb14 | 57 | tmpResponseLength = *responseLength; |
sakurafan | 0:f3b80b2bdb14 | 58 | } |
sakurafan | 0:f3b80b2bdb14 | 59 | *responseLength = tmpResponseLength; |
sakurafan | 0:f3b80b2bdb14 | 60 | } |
sakurafan | 0:f3b80b2bdb14 | 61 | for(int16_t i=0; i<tmpResponseLength; i++){ |
sakurafan | 0:f3b80b2bdb14 | 62 | tmpResponse = this->receiveByte(); |
sakurafan | 0:f3b80b2bdb14 | 63 | parity ^= tmpResponse; |
sakurafan | 0:f3b80b2bdb14 | 64 | if(response != NULL){ |
sakurafan | 0:f3b80b2bdb14 | 65 | response[i] = tmpResponse; |
sakurafan | 0:f3b80b2bdb14 | 66 | } |
sakurafan | 0:f3b80b2bdb14 | 67 | } |
sakurafan | 0:f3b80b2bdb14 | 68 | dbg("Parity\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 69 | uint8_t p = this->receiveByte(true); |
sakurafan | 0:f3b80b2bdb14 | 70 | parity ^= p; |
sakurafan | 0:f3b80b2bdb14 | 71 | dbg("Parity=%02x\r\n", p); |
sakurafan | 0:f3b80b2bdb14 | 72 | if(parity != 0x00){ |
sakurafan | 0:f3b80b2bdb14 | 73 | result = CMD_ERROR_PARITY; |
sakurafan | 0:f3b80b2bdb14 | 74 | dbg("Invalid parity\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 75 | }else{ |
sakurafan | 0:f3b80b2bdb14 | 76 | dbg("Success\r\n"); |
sakurafan | 0:f3b80b2bdb14 | 77 | } |
sakurafan | 0:f3b80b2bdb14 | 78 | //this->finishReceiving(); |
sakurafan | 0:f3b80b2bdb14 | 79 | |
sakurafan | 0:f3b80b2bdb14 | 80 | this->end(); |
sakurafan | 0:f3b80b2bdb14 | 81 | return result; |
sakurafan | 0:f3b80b2bdb14 | 82 | } |
sakurafan | 0:f3b80b2bdb14 | 83 | |
sakurafan | 0:f3b80b2bdb14 | 84 | /* Common Commands */ |
sakurafan | 0:f3b80b2bdb14 | 85 | |
sakurafan | 0:f3b80b2bdb14 | 86 | uint8_t SakuraIO::getConnectionStatus(){ |
sakurafan | 0:f3b80b2bdb14 | 87 | uint8_t responseLength = 1; |
sakurafan | 0:f3b80b2bdb14 | 88 | uint8_t response[1] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 89 | if(executeCommand(CMD_GET_CONNECTION_STATUS, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 90 | return 0x7F; |
sakurafan | 0:f3b80b2bdb14 | 91 | } |
sakurafan | 0:f3b80b2bdb14 | 92 | return response[0]; |
sakurafan | 0:f3b80b2bdb14 | 93 | } |
sakurafan | 0:f3b80b2bdb14 | 94 | |
sakurafan | 0:f3b80b2bdb14 | 95 | uint8_t SakuraIO::getSignalQuarity(){ |
sakurafan | 0:f3b80b2bdb14 | 96 | uint8_t responseLength = 1; |
sakurafan | 0:f3b80b2bdb14 | 97 | uint8_t response[1] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 98 | |
sakurafan | 0:f3b80b2bdb14 | 99 | if(executeCommand(CMD_GET_SIGNAL_QUALITY, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 100 | return 0x00; |
sakurafan | 0:f3b80b2bdb14 | 101 | } |
sakurafan | 0:f3b80b2bdb14 | 102 | return response[0]; |
sakurafan | 0:f3b80b2bdb14 | 103 | } |
sakurafan | 0:f3b80b2bdb14 | 104 | |
sakurafan | 0:f3b80b2bdb14 | 105 | uint64_t SakuraIO::getUnixtime(){ |
sakurafan | 0:f3b80b2bdb14 | 106 | uint8_t responseLength = 8; |
sakurafan | 0:f3b80b2bdb14 | 107 | uint8_t response[8] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 108 | if(executeCommand(CMD_GET_DATETIME, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 109 | return 0x00; |
sakurafan | 0:f3b80b2bdb14 | 110 | } |
sakurafan | 0:f3b80b2bdb14 | 111 | return *((uint64_t *)response); |
sakurafan | 0:f3b80b2bdb14 | 112 | } |
sakurafan | 0:f3b80b2bdb14 | 113 | |
sakurafan | 0:f3b80b2bdb14 | 114 | uint8_t SakuraIO::echoback(uint8_t length, uint8_t *data, uint8_t *response){ |
sakurafan | 0:f3b80b2bdb14 | 115 | uint8_t responseLength = length; |
sakurafan | 0:f3b80b2bdb14 | 116 | if(executeCommand(CMD_ECHO_BACK, length, data, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 117 | return 0x00; |
sakurafan | 0:f3b80b2bdb14 | 118 | } |
sakurafan | 0:f3b80b2bdb14 | 119 | return responseLength; |
sakurafan | 0:f3b80b2bdb14 | 120 | } |
sakurafan | 0:f3b80b2bdb14 | 121 | |
sakurafan | 0:f3b80b2bdb14 | 122 | /* IO Commands */ |
sakurafan | 0:f3b80b2bdb14 | 123 | |
sakurafan | 0:f3b80b2bdb14 | 124 | uint16_t SakuraIO::getADC(uint8_t channel){ |
sakurafan | 0:f3b80b2bdb14 | 125 | uint8_t request[1] = {channel}; |
sakurafan | 0:f3b80b2bdb14 | 126 | uint8_t response[2] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 127 | uint8_t responseLength = sizeof(response); |
sakurafan | 0:f3b80b2bdb14 | 128 | if(executeCommand(CMD_READ_ADC, 1, request, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 129 | return 0xffff; |
sakurafan | 0:f3b80b2bdb14 | 130 | } |
sakurafan | 0:f3b80b2bdb14 | 131 | return *((uint16_t *)response); |
sakurafan | 0:f3b80b2bdb14 | 132 | } |
sakurafan | 0:f3b80b2bdb14 | 133 | |
sakurafan | 0:f3b80b2bdb14 | 134 | /* TX Commands */ |
sakurafan | 0:f3b80b2bdb14 | 135 | uint8_t SakuraIO::enqueueTxRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 136 | uint8_t request[18] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 137 | uint8_t requestLength = 10; |
sakurafan | 0:f3b80b2bdb14 | 138 | request[0] = ch; |
sakurafan | 0:f3b80b2bdb14 | 139 | request[1] = type; |
sakurafan | 0:f3b80b2bdb14 | 140 | for(uint8_t i=0;i<length;i++){ |
sakurafan | 0:f3b80b2bdb14 | 141 | request[2+i] = data[i]; |
sakurafan | 0:f3b80b2bdb14 | 142 | } |
sakurafan | 0:f3b80b2bdb14 | 143 | if(offset != 0){ |
sakurafan | 0:f3b80b2bdb14 | 144 | requestLength = 18; |
sakurafan | 0:f3b80b2bdb14 | 145 | for(uint8_t i=0;i<8;i++){ |
sakurafan | 0:f3b80b2bdb14 | 146 | request[10+i] = ((uint8_t *)&offset)[i]; |
sakurafan | 0:f3b80b2bdb14 | 147 | } |
sakurafan | 0:f3b80b2bdb14 | 148 | } |
sakurafan | 0:f3b80b2bdb14 | 149 | return executeCommand(CMD_TX_ENQUEUE, requestLength, request, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 150 | } |
sakurafan | 0:f3b80b2bdb14 | 151 | |
sakurafan | 0:f3b80b2bdb14 | 152 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 153 | return enqueueTxRaw(ch, 'i', 4, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 154 | } |
sakurafan | 0:f3b80b2bdb14 | 155 | |
sakurafan | 0:f3b80b2bdb14 | 156 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 157 | return enqueueTxRaw(ch, 'I', 4, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 158 | } |
sakurafan | 0:f3b80b2bdb14 | 159 | |
sakurafan | 0:f3b80b2bdb14 | 160 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 161 | return enqueueTxRaw(ch, 'l', 8, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 162 | } |
sakurafan | 0:f3b80b2bdb14 | 163 | |
sakurafan | 0:f3b80b2bdb14 | 164 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 165 | return enqueueTxRaw(ch, 'L', 8, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 166 | } |
sakurafan | 0:f3b80b2bdb14 | 167 | |
sakurafan | 0:f3b80b2bdb14 | 168 | uint8_t SakuraIO::enqueueTx(uint8_t ch, float value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 169 | return enqueueTxRaw(ch, 'f', 4, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 170 | } |
sakurafan | 0:f3b80b2bdb14 | 171 | |
sakurafan | 0:f3b80b2bdb14 | 172 | uint8_t SakuraIO::enqueueTx(uint8_t ch, double value, uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 173 | return enqueueTxRaw(ch, 'd', 8, (uint8_t *)&value, offset); |
sakurafan | 0:f3b80b2bdb14 | 174 | } |
sakurafan | 0:f3b80b2bdb14 | 175 | |
sakurafan | 0:f3b80b2bdb14 | 176 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8], uint64_t offset){ |
sakurafan | 0:f3b80b2bdb14 | 177 | return enqueueTxRaw(ch, 'b', 8, (uint8_t *)value, offset); |
sakurafan | 0:f3b80b2bdb14 | 178 | } |
sakurafan | 0:f3b80b2bdb14 | 179 | |
sakurafan | 0:f3b80b2bdb14 | 180 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value){ |
sakurafan | 0:f3b80b2bdb14 | 181 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 182 | } |
sakurafan | 0:f3b80b2bdb14 | 183 | |
sakurafan | 0:f3b80b2bdb14 | 184 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value){ |
sakurafan | 0:f3b80b2bdb14 | 185 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 186 | } |
sakurafan | 0:f3b80b2bdb14 | 187 | |
sakurafan | 0:f3b80b2bdb14 | 188 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value){ |
sakurafan | 0:f3b80b2bdb14 | 189 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 190 | } |
sakurafan | 0:f3b80b2bdb14 | 191 | |
sakurafan | 0:f3b80b2bdb14 | 192 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value){ |
sakurafan | 0:f3b80b2bdb14 | 193 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 194 | } |
sakurafan | 0:f3b80b2bdb14 | 195 | |
sakurafan | 0:f3b80b2bdb14 | 196 | uint8_t SakuraIO::enqueueTx(uint8_t ch, float value){ |
sakurafan | 0:f3b80b2bdb14 | 197 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 198 | } |
sakurafan | 0:f3b80b2bdb14 | 199 | |
sakurafan | 0:f3b80b2bdb14 | 200 | uint8_t SakuraIO::enqueueTx(uint8_t ch, double value){ |
sakurafan | 0:f3b80b2bdb14 | 201 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 202 | } |
sakurafan | 0:f3b80b2bdb14 | 203 | |
sakurafan | 0:f3b80b2bdb14 | 204 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8]){ |
sakurafan | 0:f3b80b2bdb14 | 205 | return enqueueTx(ch, value, (uint32_t)0); |
sakurafan | 0:f3b80b2bdb14 | 206 | } |
sakurafan | 0:f3b80b2bdb14 | 207 | |
sakurafan | 0:f3b80b2bdb14 | 208 | uint8_t SakuraIO::getTxQueueLength(uint8_t *available, uint8_t *queued){ |
sakurafan | 0:f3b80b2bdb14 | 209 | uint8_t response[2] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 210 | uint8_t responseLength = 2; |
sakurafan | 0:f3b80b2bdb14 | 211 | uint8_t ret = executeCommand(CMD_TX_LENGTH, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 212 | *available = response[0]; |
sakurafan | 0:f3b80b2bdb14 | 213 | *queued = response[1]; |
sakurafan | 0:f3b80b2bdb14 | 214 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 215 | } |
sakurafan | 0:f3b80b2bdb14 | 216 | |
sakurafan | 0:f3b80b2bdb14 | 217 | uint8_t SakuraIO::clearTx(){ |
sakurafan | 0:f3b80b2bdb14 | 218 | return executeCommand(CMD_TX_CLEAR, 0, NULL, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 219 | } |
sakurafan | 0:f3b80b2bdb14 | 220 | |
sakurafan | 0:f3b80b2bdb14 | 221 | uint8_t SakuraIO::send(){ |
sakurafan | 0:f3b80b2bdb14 | 222 | return executeCommand(CMD_TX_SEND, 0, NULL, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 223 | } |
sakurafan | 0:f3b80b2bdb14 | 224 | |
sakurafan | 0:f3b80b2bdb14 | 225 | uint8_t SakuraIO::getTxStatus(uint8_t *queue, uint8_t *immediate){ |
sakurafan | 0:f3b80b2bdb14 | 226 | uint8_t response[2] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 227 | uint8_t responseLength = 2; |
sakurafan | 0:f3b80b2bdb14 | 228 | uint8_t ret = executeCommand(CMD_TX_STAT, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 229 | *queue = response[0]; |
sakurafan | 0:f3b80b2bdb14 | 230 | *immediate = response[1]; |
sakurafan | 0:f3b80b2bdb14 | 231 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 232 | } |
sakurafan | 0:f3b80b2bdb14 | 233 | |
sakurafan | 0:f3b80b2bdb14 | 234 | /* RX Commands */ |
sakurafan | 0:f3b80b2bdb14 | 235 | |
sakurafan | 0:f3b80b2bdb14 | 236 | uint8_t SakuraIO::dequeueRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){ |
sakurafan | 0:f3b80b2bdb14 | 237 | uint8_t response[18] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 238 | uint8_t responseLength = 18; |
sakurafan | 0:f3b80b2bdb14 | 239 | uint8_t ret = executeCommand(CMD_RX_DEQUEUE, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 240 | if(ret != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 241 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 242 | } |
sakurafan | 0:f3b80b2bdb14 | 243 | |
sakurafan | 0:f3b80b2bdb14 | 244 | *ch = response[0]; |
sakurafan | 0:f3b80b2bdb14 | 245 | *type = response[1]; |
sakurafan | 0:f3b80b2bdb14 | 246 | for(uint8_t i=0; i<8; i++){ |
sakurafan | 0:f3b80b2bdb14 | 247 | value[i] = response[2+i]; |
sakurafan | 0:f3b80b2bdb14 | 248 | } |
sakurafan | 0:f3b80b2bdb14 | 249 | for(uint8_t i=0; i<8; i++){ |
sakurafan | 0:f3b80b2bdb14 | 250 | ((uint8_t *)offset)[i] = response[10+i]; |
sakurafan | 0:f3b80b2bdb14 | 251 | } |
sakurafan | 0:f3b80b2bdb14 | 252 | |
sakurafan | 0:f3b80b2bdb14 | 253 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 254 | } |
sakurafan | 0:f3b80b2bdb14 | 255 | |
sakurafan | 0:f3b80b2bdb14 | 256 | uint8_t SakuraIO::peekRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){ |
sakurafan | 0:f3b80b2bdb14 | 257 | uint8_t response[18] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 258 | uint8_t responseLength = 18; |
sakurafan | 0:f3b80b2bdb14 | 259 | uint8_t ret = executeCommand(CMD_RX_PEEK, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 260 | if(ret != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 261 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 262 | } |
sakurafan | 0:f3b80b2bdb14 | 263 | |
sakurafan | 0:f3b80b2bdb14 | 264 | *ch = response[0]; |
sakurafan | 0:f3b80b2bdb14 | 265 | *type = response[1]; |
sakurafan | 0:f3b80b2bdb14 | 266 | for(uint8_t i=0; i<8; i++){ |
sakurafan | 0:f3b80b2bdb14 | 267 | value[0] = response[2+i]; |
sakurafan | 0:f3b80b2bdb14 | 268 | } |
sakurafan | 0:f3b80b2bdb14 | 269 | for(uint8_t i=0; i<8; i++){ |
sakurafan | 0:f3b80b2bdb14 | 270 | ((uint8_t *)offset)[i] = response[10+i]; |
sakurafan | 0:f3b80b2bdb14 | 271 | } |
sakurafan | 0:f3b80b2bdb14 | 272 | |
sakurafan | 0:f3b80b2bdb14 | 273 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 274 | } |
sakurafan | 0:f3b80b2bdb14 | 275 | |
sakurafan | 0:f3b80b2bdb14 | 276 | uint8_t SakuraIO::getRxQueueLength(uint8_t *available, uint8_t *queued){ |
sakurafan | 0:f3b80b2bdb14 | 277 | uint8_t response[2] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 278 | uint8_t responseLength = 2; |
sakurafan | 0:f3b80b2bdb14 | 279 | uint8_t ret = executeCommand(CMD_RX_LENGTH, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 280 | *available = response[0]; |
sakurafan | 0:f3b80b2bdb14 | 281 | *queued = response[1]; |
sakurafan | 0:f3b80b2bdb14 | 282 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 283 | } |
sakurafan | 0:f3b80b2bdb14 | 284 | |
sakurafan | 0:f3b80b2bdb14 | 285 | uint8_t SakuraIO::clearRx(){ |
sakurafan | 0:f3b80b2bdb14 | 286 | return executeCommand(CMD_RX_CLEAR, 0, NULL, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 287 | } |
sakurafan | 0:f3b80b2bdb14 | 288 | |
sakurafan | 0:f3b80b2bdb14 | 289 | /* Operation command */ |
sakurafan | 0:f3b80b2bdb14 | 290 | |
sakurafan | 0:f3b80b2bdb14 | 291 | uint16_t SakuraIO::getProductID(){ |
sakurafan | 0:f3b80b2bdb14 | 292 | uint8_t response[2] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 293 | uint8_t responseLength = 2; |
sakurafan | 0:f3b80b2bdb14 | 294 | uint8_t ret = executeCommand(CMD_GET_PRODUCT_ID, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 295 | if(ret != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 296 | return 0x00; |
sakurafan | 0:f3b80b2bdb14 | 297 | } |
sakurafan | 0:f3b80b2bdb14 | 298 | return *((uint16_t *)response); |
sakurafan | 0:f3b80b2bdb14 | 299 | } |
sakurafan | 0:f3b80b2bdb14 | 300 | |
sakurafan | 0:f3b80b2bdb14 | 301 | uint8_t SakuraIO::getUniqueID(char *data){ |
sakurafan | 0:f3b80b2bdb14 | 302 | uint8_t response[11] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 303 | uint8_t responseLength = 10; |
sakurafan | 0:f3b80b2bdb14 | 304 | uint8_t ret = executeCommand(CMD_GET_UNIQUE_ID, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 305 | if(ret != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 306 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 307 | } |
sakurafan | 0:f3b80b2bdb14 | 308 | for(uint8_t i=0; i<responseLength; i++){ |
sakurafan | 0:f3b80b2bdb14 | 309 | data[i] = (char)response[i]; |
sakurafan | 0:f3b80b2bdb14 | 310 | } |
sakurafan | 0:f3b80b2bdb14 | 311 | data[responseLength] = 0x00; |
sakurafan | 0:f3b80b2bdb14 | 312 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 313 | } |
sakurafan | 0:f3b80b2bdb14 | 314 | |
sakurafan | 0:f3b80b2bdb14 | 315 | uint8_t SakuraIO::getFirmwareVersion(char *data){ |
sakurafan | 0:f3b80b2bdb14 | 316 | uint8_t response[33] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 317 | uint8_t responseLength = 32; |
sakurafan | 0:f3b80b2bdb14 | 318 | uint8_t ret = executeCommand(CMD_GET_FIRMWARE_VERSION, 0, NULL, &responseLength, response); |
sakurafan | 0:f3b80b2bdb14 | 319 | if(ret != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 320 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 321 | } |
sakurafan | 0:f3b80b2bdb14 | 322 | for(uint8_t i=0; i<responseLength; i++){ |
sakurafan | 0:f3b80b2bdb14 | 323 | data[i] = (char)response[i]; |
sakurafan | 0:f3b80b2bdb14 | 324 | } |
sakurafan | 0:f3b80b2bdb14 | 325 | data[responseLength] = 0x00; |
sakurafan | 0:f3b80b2bdb14 | 326 | return ret; |
sakurafan | 0:f3b80b2bdb14 | 327 | } |
sakurafan | 0:f3b80b2bdb14 | 328 | |
sakurafan | 0:f3b80b2bdb14 | 329 | uint8_t SakuraIO::unlock(){ |
sakurafan | 0:f3b80b2bdb14 | 330 | uint8_t request[4] = {0x53, 0x6B, 0x72, 0x61}; |
sakurafan | 0:f3b80b2bdb14 | 331 | return executeCommand(CMD_UNLOCK, 4, request, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 332 | } |
sakurafan | 0:f3b80b2bdb14 | 333 | |
sakurafan | 0:f3b80b2bdb14 | 334 | uint8_t SakuraIO::updateFirmware(){ |
sakurafan | 0:f3b80b2bdb14 | 335 | return executeCommand(CMD_UPDATE_FIRMWARE, 0, 0, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 336 | } |
sakurafan | 0:f3b80b2bdb14 | 337 | |
sakurafan | 0:f3b80b2bdb14 | 338 | uint8_t SakuraIO::getFirmwareUpdateStatus(){ |
sakurafan | 0:f3b80b2bdb14 | 339 | uint8_t response[1] = {0x00}; |
sakurafan | 0:f3b80b2bdb14 | 340 | uint8_t responseLength = 1; |
sakurafan | 0:f3b80b2bdb14 | 341 | if(executeCommand(CMD_GET_UPDATE_FIRMWARE_STATUS, 0, 0, &responseLength, response) != CMD_ERROR_NONE){ |
sakurafan | 0:f3b80b2bdb14 | 342 | return 0xff; |
sakurafan | 0:f3b80b2bdb14 | 343 | } |
sakurafan | 0:f3b80b2bdb14 | 344 | return response[0]; |
sakurafan | 0:f3b80b2bdb14 | 345 | } |
sakurafan | 0:f3b80b2bdb14 | 346 | |
sakurafan | 0:f3b80b2bdb14 | 347 | uint8_t SakuraIO::reset(){ |
sakurafan | 0:f3b80b2bdb14 | 348 | return executeCommand(CMD_SOFTWARE_RESET, 0, 0, NULL, NULL); |
sakurafan | 0:f3b80b2bdb14 | 349 | } |