grove: add documentation for grove module
[contrib/upm.git] / src / sm130 / sm130.cxx
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include <iostream>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <algorithm>
30
31 #include "sm130.h"
32
33 using namespace upm;
34
35 struct SM130Exception : public std::exception {
36     std::string message;
37     SM130Exception (std::string msg) : message (msg) { }
38     ~SM130Exception () throw () { }
39     const char* what() const throw () { return message.c_str(); }
40 };
41
42 SM130::SM130 (int bus, int devAddr, int rst, int dready) {
43     mraa_result_t error = MRAA_SUCCESS;
44
45     this->m_name = "SM130";
46
47     this->m_i2cAddr = devAddr;
48     this->m_bus = bus;
49
50     this->m_i2Ctx = mraa_i2c_init(this->m_bus);
51
52     mraa_result_t ret = mraa_i2c_address(this->m_i2Ctx, this->m_i2cAddr);
53     if (ret != MRAA_SUCCESS) {
54         throw SM130Exception ("Couldn't initilize I2C.");
55     }
56
57     this->m_resetPinCtx = mraa_gpio_init (rst);
58     if (m_resetPinCtx == NULL) {
59         throw SM130Exception ("Couldn't initilize RESET pin.");
60     }
61
62     this->m_dataReadyPinCtx = mraa_gpio_init (dready);
63     if (m_dataReadyPinCtx == NULL) {
64         throw SM130Exception ("Couldn't initilize DATA READY pin.");
65     }
66
67     error = mraa_gpio_dir (this->m_resetPinCtx, MRAA_GPIO_OUT);
68     if (error != MRAA_SUCCESS) {
69         throw SM130Exception ("Couldn't set direction for RESET pin.");
70     }
71
72     error = mraa_gpio_dir (this->m_dataReadyPinCtx, MRAA_GPIO_OUT);
73     if (error != MRAA_SUCCESS) {
74         throw SM130Exception ("Couldn't set direction for DATA READY pin.");
75     }
76 }
77
78 SM130::~SM130 () {
79     mraa_result_t error = MRAA_SUCCESS;
80
81     error = mraa_i2c_stop(this->m_i2Ctx);
82     if (error != MRAA_SUCCESS) {
83         mraa_result_print(error);
84     }
85     error = mraa_gpio_close (this->m_resetPinCtx);
86     if (error != MRAA_SUCCESS) {
87         mraa_result_print(error);
88     }
89     error = mraa_gpio_close (this->m_dataReadyPinCtx);
90     if (error != MRAA_SUCCESS) {
91         mraa_result_print(error);
92     }
93 }
94
95 const char*
96 SM130::getFirmwareVersion () {
97     // Send VERSION command and retry a few times if no response
98     for (uint8_t n = 0; n < 10; n++) {
99         sendCommand (CMD_VERSION);
100         if (available() && getCommand() == CMD_VERSION)
101         //  return versionString;
102         usleep(100 * 1000);
103     }
104
105     return 0;
106 }
107
108 uint8_t
109 SM130::available () {
110     // If in SEEK mode and using DREADY pin, check the status
111     if (this->m_LastCMD == CMD_SEEK_TAG) {
112         if (!mraa_gpio_read(this->m_dataReadyPinCtx)) {
113             return false;
114         }
115     }
116
117     // Set the maximum length of the expected response packet
118     uint8_t len;
119     switch(this->m_LastCMD) {
120         case CMD_ANTENNA_POWER:
121         case CMD_AUTHENTICATE:
122         case CMD_DEC_VALUE:
123         case CMD_INC_VALUE:
124         case CMD_WRITE_KEY:
125         case CMD_HALT_TAG:
126         case CMD_SLEEP:
127             len = 4;
128             break;
129         case CMD_WRITE4:
130         case CMD_WRITE_VALUE:
131         case CMD_READ_VALUE:
132             len = 8;
133         case CMD_SEEK_TAG:
134         case CMD_SELECT_TAG:
135             len = 11;
136             break;
137         default:
138             len = SIZE_PACKET;
139     }
140
141     // If valid data received, process the response packet
142     if (this->i2cRecievePacket(len) > 0) {
143         // Init response variables
144         this->m_TagType = this->m_TagLength = *this->m_TagString = 0;
145
146         // If packet length is 2, the command failed. Set error code.
147         errorCode = this->getPacketLength () < 3 ? this->m_Data[2] : 0;
148
149         // Process command response
150         switch (this->getCommand ()) {
151             case CMD_RESET:
152             case CMD_VERSION:
153                 // RESET and VERSION commands produce the firmware version
154                 len = std::min ((unsigned int) getPacketLength(), (unsigned int) sizeof(this->m_Version)) - 1;
155                 memcpy(this->m_Version, this->m_Data + 2, len);
156                 this->m_Version[len] = 0;
157                 break;
158
159             case CMD_SEEK_TAG:
160             case CMD_SELECT_TAG:
161                 // If no error, get tag number
162                 if(errorCode == 0 && this->getPacketLength () >= 6)
163                 {
164                     this->m_TagLength = this->getPacketLength () - 2;
165                     this->m_TagType = this->m_Data[2];
166                     memcpy(this->m_TagNumber, this->m_Data + 3, this->m_TagLength);
167                     this->arrayToHex (this->m_TagString, this->m_TagNumber, this->m_TagLength);
168                 }
169                 break;
170
171             case CMD_AUTHENTICATE:
172                 break;
173
174             case CMD_READ16:
175                 break;
176
177             case CMD_WRITE16:
178             case CMD_WRITE4:
179                 break;
180
181             case CMD_ANTENNA_POWER:
182                 errorCode = 0;
183                 antennaPower = this->m_Data[2];
184                 break;
185
186             case CMD_SLEEP:
187                 // If in SLEEP mode, no data is available
188                 return false;
189         }
190
191         // Data available
192         return true;
193     }
194     // No data available
195     return false;
196 }
197
198 uint16_t
199 SM130::i2cRecievePacket (uint32_t len) {
200     int readByte = 0;
201
202     mraa_i2c_address(this->m_i2Ctx, this->m_i2cAddr);
203     readByte = mraa_i2c_read(this->m_i2Ctx, this->m_Data, len);
204
205     if (readByte > 0) {
206         // verify checksum if length > 0 and <= SIZE_PAYLOAD
207         if (this->m_Data[0] > 0 && this->m_Data[0] <= SIZE_PAYLOAD)
208         {
209             uint8_t i, sum;
210             for (i = 0, sum = 0; i <= this->m_Data[0]; i++) {
211                 sum += this->m_Data[i];
212             }
213             // return with length of response, or -1 if invalid checksum
214             return sum == this->m_Data[i] ? this->m_Data[0] : -1;
215         }
216     }
217
218     return readByte;
219 }
220
221 void
222 SM130::arrayToHex (char *s, uint8_t array[], uint8_t len) {
223     for (uint8_t i = 0; i < len; i++) {
224         *s++ = toHex(array[i] >> 4);
225         *s++ = toHex(array[i]);
226     }
227
228     *s = 0;
229 }
230
231 char
232 SM130::toHex (uint8_t b) {
233     b = b & 0x0f;
234
235     return b < 10 ? b + '0' : b + 'A' - 10;
236 }
237
238 mraa_result_t
239 SM130::i2cTransmitPacket (uint32_t len) {
240     mraa_result_t error = MRAA_SUCCESS;
241     uint8_t sum = 0;
242
243     // Save last command
244     this->m_LastCMD = this->m_Data[0];
245
246     // calculate the sum check
247     for (int i = 0; i < len; i++) {
248         sum += this->m_Data[i];
249     }
250
251     // placing the sum check to the last byte of the packet
252     this->m_Data[len + 1] = sum;
253
254     error = mraa_i2c_address (this->m_i2Ctx, this->m_i2cAddr);
255     error = mraa_i2c_write (this->m_i2Ctx, this->m_Data, len + 1);
256
257     return error;
258 }
259
260 mraa_result_t
261 SM130::sendCommand (uint8_t cmd) {
262     this->m_Data[0] = 1;
263     this->m_Data[1] = cmd;
264     this->i2cTransmitPacket(2);
265 }
266