2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 struct SM130Exception : public std::exception {
36 SM130Exception (std::string msg) : message (msg) { }
37 ~SM130Exception () throw () { }
38 const char* what() const throw () { return message.c_str(); }
41 SM130::SM130 (int bus, int devAddr, int rst, int dready) {
42 mraa_result_t error = MRAA_SUCCESS;
44 this->m_name = "SM130";
46 this->m_i2cAddr = devAddr;
49 this->m_i2Ctx = mraa_i2c_init(this->m_bus);
51 mraa_result_t ret = mraa_i2c_address(this->m_i2Ctx, this->m_i2cAddr);
52 if (ret != MRAA_SUCCESS) {
53 throw SM130Exception ("Couldn't initilize I2C.");
56 this->m_resetPinCtx = mraa_gpio_init (rst);
57 if (m_resetPinCtx == NULL) {
58 throw SM130Exception ("Couldn't initilize RESET pin.");
61 this->m_dataReadyPinCtx = mraa_gpio_init (dready);
62 if (m_dataReadyPinCtx == NULL) {
63 throw SM130Exception ("Couldn't initilize DATA READY pin.");
66 error = mraa_gpio_dir (this->m_resetPinCtx, MRAA_GPIO_OUT);
67 if (error != MRAA_SUCCESS) {
68 throw SM130Exception ("Couldn't set direction for RESET pin.");
71 error = mraa_gpio_dir (this->m_dataReadyPinCtx, MRAA_GPIO_OUT);
72 if (error != MRAA_SUCCESS) {
73 throw SM130Exception ("Couldn't set direction for DATA READY pin.");
78 mraa_result_t error = MRAA_SUCCESS;
80 error = mraa_i2c_stop(this->m_i2Ctx);
81 if (error != MRAA_SUCCESS) {
82 mraa_result_print(error);
84 error = mraa_gpio_close (this->m_resetPinCtx);
85 if (error != MRAA_SUCCESS) {
86 mraa_result_print(error);
88 error = mraa_gpio_close (this->m_dataReadyPinCtx);
89 if (error != MRAA_SUCCESS) {
90 mraa_result_print(error);
95 SM130::getFirmwareVersion () {
96 // Send VERSION command and retry a few times if no response
97 for (uint8_t n = 0; n < 10; n++) {
98 sendCommand (CMD_VERSION);
99 if (available() && getCommand() == CMD_VERSION)
100 // return versionString;
108 SM130::available () {
109 // If in SEEK mode and using DREADY pin, check the status
110 if (this->m_LastCMD == CMD_SEEK_TAG) {
111 if (!mraa_gpio_read(this->m_dataReadyPinCtx)) {
116 // Set the maximum length of the expected response packet
118 switch(this->m_LastCMD) {
119 case CMD_ANTENNA_POWER:
120 case CMD_AUTHENTICATE:
129 case CMD_WRITE_VALUE:
140 // If valid data received, process the response packet
141 if (this->i2cRecievePacket(len) > 0) {
142 // Init response variables
143 this->m_TagType = this->m_TagLength = *this->m_TagString = 0;
145 // If packet length is 2, the command failed. Set error code.
146 errorCode = this->getPacketLength () < 3 ? this->m_Data[2] : 0;
148 // Process command response
149 switch (this->getCommand ()) {
152 // RESET and VERSION commands produce the firmware version
153 len = std::min ((unsigned int) getPacketLength (), sizeof (this->m_Version)) - 1;
154 memcpy(this->m_Version, this->m_Data + 2, len);
155 this->m_Version[len] = 0;
160 // If no error, get tag number
161 if(errorCode == 0 && this->getPacketLength () >= 6)
163 this->m_TagLength = this->getPacketLength () - 2;
164 this->m_TagType = this->m_Data[2];
165 memcpy(this->m_TagNumber, this->m_Data + 3, this->m_TagLength);
166 this->arrayToHex (this->m_TagString, this->m_TagNumber, this->m_TagLength);
170 case CMD_AUTHENTICATE:
180 case CMD_ANTENNA_POWER:
182 antennaPower = this->m_Data[2];
186 // If in SLEEP mode, no data is available
198 SM130::i2cRecievePacket (uint32_t len) {
201 mraa_i2c_address(this->m_i2Ctx, this->m_i2cAddr);
202 readByte = mraa_i2c_read(this->m_i2Ctx, this->m_Data, len);
205 // verify checksum if length > 0 and <= SIZE_PAYLOAD
206 if (this->m_Data[0] > 0 && this->m_Data[0] <= SIZE_PAYLOAD)
209 for (i = 0, sum = 0; i <= this->m_Data[0]; i++) {
210 sum += this->m_Data[i];
212 // return with length of response, or -1 if invalid checksum
213 return sum == this->m_Data[i] ? this->m_Data[0] : -1;
221 SM130::arrayToHex (char *s, uint8_t array[], uint8_t len) {
222 for (uint8_t i = 0; i < len; i++) {
223 *s++ = toHex(array[i] >> 4);
224 *s++ = toHex(array[i]);
231 SM130::toHex (uint8_t b) {
234 return b < 10 ? b + '0' : b + 'A' - 10;
238 SM130::i2cTransmitPacket (uint32_t len) {
239 mraa_result_t error = MRAA_SUCCESS;
243 this->m_LastCMD = this->m_Data[0];
245 // calculate the sum check
246 for (int i = 0; i < len; i++) {
247 sum += this->m_Data[i];
250 // placing the sum check to the last byte of the packet
251 this->m_Data[len + 1] = sum;
253 error = mraa_i2c_address (this->m_i2Ctx, this->m_i2cAddr);
254 error = mraa_i2c_write (this->m_i2Ctx, this->m_Data, len + 1);
260 SM130::sendCommand (uint8_t cmd) {
262 this->m_Data[1] = cmd;
263 this->i2cTransmitPacket(2);