|
@@ -0,0 +1,282 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Jon Trulson <jtrulson@ics.com>
|
|
3
|
+ * Copyright (c) 2015 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ *
|
|
6
|
+ * This code was adapted from the Seeed Studio code at:
|
|
7
|
+ * https://github.com/Seeed-Studio/NFC_Tag_M24LR6E
|
|
8
|
+ *
|
|
9
|
+ * Copyright (c) 2014 seeed technology inc.
|
|
10
|
+ * Website : www.seeed.cc
|
|
11
|
+ * Author : lawliet zou
|
|
12
|
+ * Create Time: March 2014
|
|
13
|
+ *
|
|
14
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
15
|
+ * a copy of this software and associated documentation files (the
|
|
16
|
+ * "Software"), to deal in the Software without restriction, including
|
|
17
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
18
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
19
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
20
|
+ * the following conditions:
|
|
21
|
+ *
|
|
22
|
+ * The above copyright notice and this permission notice shall be
|
|
23
|
+ * included in all copies or substantial portions of the Software.
|
|
24
|
+ *
|
|
25
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
26
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
27
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
28
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
29
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
30
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
31
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
32
|
+ */
|
|
33
|
+#pragma once
|
|
34
|
+
|
|
35
|
+#include <string>
|
|
36
|
+#include <mraa/i2c.hpp>
|
|
37
|
+
|
|
38
|
+#define M24LR64E_I2C_BUS 0
|
|
39
|
+#define M24LR64E_DEFAULT_I2C_ADDR 0x53
|
|
40
|
+#define M24LR64E_DEFAULT_I2C_ADDR_E2 (M24LR64E_DEFAULT_I2C_ADDR | 0x04)
|
|
41
|
+
|
|
42
|
+namespace upm {
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * @brief Grove NFC Tag
|
|
46
|
+ * @defgroup m24lr64e libupm-m24lr64e
|
|
47
|
+ * @ingroup seeed i2c other
|
|
48
|
+ */
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * @library m24lr64e
|
|
52
|
+ * @sensor m24lr64e
|
|
53
|
+ * @comname Grove NFC Tag
|
|
54
|
+ * @type other
|
|
55
|
+ * @man seeed
|
|
56
|
+ * @web http://www.seeedstudio.com/wiki/Grove_-_NFC_Tag
|
|
57
|
+ * @con i2c
|
|
58
|
+ *
|
|
59
|
+ * @brief C++ API for the M24LR64E based Grove NFC Tag
|
|
60
|
+ *
|
|
61
|
+ * The Grove NFC tag is, in essence, an 8KB EEPROM that can be written
|
|
62
|
+ * to or read from using I2C and NFC equipped devices.
|
|
63
|
+ *
|
|
64
|
+ * The USER mode (default) allows read and write access to all 8KB
|
|
65
|
+ * of space, provided the sector security status (SSS) allows it.
|
|
66
|
+ * The ROOT mode allows modification of the SSS data and other
|
|
67
|
+ * information, provided the proper password is submitted. The
|
|
68
|
+ * default password for a new tag is 0x00000000. See the data sheet
|
|
69
|
+ * for much more detailed information.
|
|
70
|
+ *
|
|
71
|
+ * The Seeed Studio wiki page for this device includes a link to an
|
|
72
|
+ * Android application that can be used to also read and write the
|
|
73
|
+ * device via NFC, as well as set the NFC passwords, which cannot be
|
|
74
|
+ * done via I2C.
|
|
75
|
+ *
|
|
76
|
+ * @snippet m24lr64e.cxx Interesting
|
|
77
|
+ */
|
|
78
|
+ class M24LR64E {
|
|
79
|
+ public:
|
|
80
|
+
|
|
81
|
+ static const int EEPROM_I2C_LENGTH = 8192;
|
|
82
|
+ static const int PASSWORD_LENGTH = 4;
|
|
83
|
+ static const int SECTOR_SECURITY_STATUS_BASE_ADDR = 0x800; // 2048
|
|
84
|
+
|
|
85
|
+ static const uint8_t LOCK_PROTECT_BIT = 0x01;
|
|
86
|
+ static const uint8_t WRITE_READ_PROTECT_BIT = 0x02;
|
|
87
|
+ static const uint8_t PASSWORD_CTRL_BIT = 0x04;
|
|
88
|
+
|
|
89
|
+ static const int UID_LENGTH = 8; // bytes
|
|
90
|
+
|
|
91
|
+ static const unsigned int I2C_WRITE_TIME = 5; // 5ms
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * M24LR64E addresses, accessable only in root mode
|
|
95
|
+ */
|
|
96
|
+ typedef enum {
|
|
97
|
+ I2C_PASSWORD_ADDR = 2304,
|
|
98
|
+ RF_PASSWORD_1_ADDR = 2308, // RF pwds not available in
|
|
99
|
+ RF_PASSWORD_2_ADDR = 2312, // i2c access modes
|
|
100
|
+ RF_PASSWORD_3_ADDR = 2316,
|
|
101
|
+ DSFID_ADDR = 2320, // 1 byte
|
|
102
|
+ AFI_ADDR = 2321, // 1 byte
|
|
103
|
+ RESV_ADDR = 2322, // 1 bytes
|
|
104
|
+ CONFIG_ADDR = 2323, // 1 bytes
|
|
105
|
+ UID_ADDR = 2324, // 8 bytes
|
|
106
|
+ MEM_SIZE_ADDR = 2332, // 3 bytes
|
|
107
|
+ IC_REF_ADDR = 2335, // 1 byte
|
|
108
|
+ PROG_COMP_ENERGY_HARVEST_ADDR = 2339 // 1 byte
|
|
109
|
+ } M24LR64E_ADDR_T;
|
|
110
|
+
|
|
111
|
+ enum AccessMode {
|
|
112
|
+ USER_MODE = 0x0, // offer simple read/write access right
|
|
113
|
+ ROOT_MODE = 0x1 // offer password change access right
|
|
114
|
+ };
|
|
115
|
+
|
|
116
|
+ enum SectorAccessRight {
|
|
117
|
+ // **********************************
|
|
118
|
+ // * submit passWd * no submit *
|
|
119
|
+ //b2,b1 * Read * Write * Read * Write *
|
|
120
|
+ // 00 * 1 1 1 0 *
|
|
121
|
+ // 01 * 1 1 1 1 *
|
|
122
|
+ // 10 * 1 1 0 0 *
|
|
123
|
+ // 11 * 0 1 0 0 *
|
|
124
|
+ // **********************************
|
|
125
|
+ Access_1110 = 0,
|
|
126
|
+ Access_1111 = 1,
|
|
127
|
+ Access_1100 = 2,
|
|
128
|
+ Access_0111 = 3,
|
|
129
|
+ };
|
|
130
|
+
|
|
131
|
+ enum SectorSelectPassWd {
|
|
132
|
+ //00 => no passwd protect
|
|
133
|
+ //01 => passWd 1
|
|
134
|
+ //10 => passWd 2
|
|
135
|
+ //11 => passwd 3
|
|
136
|
+ noPasswd = 0,
|
|
137
|
+ passwd_1 = 1,
|
|
138
|
+ passwd_2 = 2,
|
|
139
|
+ passwd_3 = 3,
|
|
140
|
+ };
|
|
141
|
+
|
|
142
|
+ /**
|
|
143
|
+ * m24lr64e constructor
|
|
144
|
+ *
|
|
145
|
+ * @param bus i2c bus to use
|
|
146
|
+ * @param mode the access mode (user or root) to use
|
|
147
|
+ */
|
|
148
|
+ M24LR64E(int bus, AccessMode mode = USER_MODE);
|
|
149
|
+
|
|
150
|
+ /**
|
|
151
|
+ * M24LR64E Destructor
|
|
152
|
+ */
|
|
153
|
+ ~M24LR64E();
|
|
154
|
+
|
|
155
|
+ /**
|
|
156
|
+ * submit an i2c access password
|
|
157
|
+ *
|
|
158
|
+ * @param passwd the 4-byte access password
|
|
159
|
+ */
|
|
160
|
+ bool submitPasswd(uint32_t passwd);
|
|
161
|
+
|
|
162
|
+ /**
|
|
163
|
+ * write a new i2c password
|
|
164
|
+ *
|
|
165
|
+ * @param passwd the 4-byte access password
|
|
166
|
+ */
|
|
167
|
+ bool writePasswd(uint32_t passwd);
|
|
168
|
+
|
|
169
|
+ /**
|
|
170
|
+ * Set the protection bit for a sector. Must be in ROOT mode
|
|
171
|
+ *
|
|
172
|
+ * @param sectorNumber the sector whose protection you are modifying
|
|
173
|
+ * @param protectEnable true if you are enabling protection
|
|
174
|
+ * @param accessRight the access rights to set
|
|
175
|
+ * @param passwd the password number to enable, if any
|
|
176
|
+ */
|
|
177
|
+ void sectorProtectConfig(unsigned int sectorNumber,
|
|
178
|
+ bool protectEnable,
|
|
179
|
+ SectorAccessRight accessRight,
|
|
180
|
+ SectorSelectPassWd passwd);
|
|
181
|
+
|
|
182
|
+ /**
|
|
183
|
+ * Clear the sector protection bits. Must be in ROOT mode.
|
|
184
|
+ */
|
|
185
|
+ void clearSectorProtect(void);
|
|
186
|
+
|
|
187
|
+ /**
|
|
188
|
+ * Set or clear the Sector Security Status Lock bit for a sector.
|
|
189
|
+ * Must be in ROOT mode.
|
|
190
|
+ *
|
|
191
|
+ * @param sectorNumber the sector who's SSS you want to modify
|
|
192
|
+ * @param sockEnable true r false to set or clear the bit
|
|
193
|
+ */
|
|
194
|
+ void sectorWriteLockBit(unsigned int sectorNumber,
|
|
195
|
+ bool sockEnable);
|
|
196
|
+
|
|
197
|
+ /**
|
|
198
|
+ * return the Data Storage Familiy Identifier
|
|
199
|
+ * Must be in ROOT mode.
|
|
200
|
+ *
|
|
201
|
+ * @return the DSFID
|
|
202
|
+ */
|
|
203
|
+ uint8_t getDSFID();
|
|
204
|
+
|
|
205
|
+ /**
|
|
206
|
+ * return the Application Family Identifier
|
|
207
|
+ * Must be in ROOT mode.
|
|
208
|
+ *
|
|
209
|
+ * @return the AFI
|
|
210
|
+ */
|
|
211
|
+ uint8_t getAFI();
|
|
212
|
+
|
|
213
|
+ /**
|
|
214
|
+ * return the Unique ID.
|
|
215
|
+ * Must be in ROOT mode.
|
|
216
|
+ *
|
|
217
|
+ * @param buf buffer to hold returned UID. Must be UID_LENGTH bytes.
|
|
218
|
+ */
|
|
219
|
+ void getUID(uint8_t* buf);
|
|
220
|
+
|
|
221
|
+ /**
|
|
222
|
+ * return the memory size
|
|
223
|
+ * Must be in ROOT mode.
|
|
224
|
+ *
|
|
225
|
+ * @return the amount of memory present
|
|
226
|
+ */
|
|
227
|
+ uint32_t getMemorySize();
|
|
228
|
+
|
|
229
|
+ /**
|
|
230
|
+ * set all memory to 0, if permissions allow
|
|
231
|
+ */
|
|
232
|
+ void clearMemory();
|
|
233
|
+
|
|
234
|
+ /**
|
|
235
|
+ * write a byte to EEPROM
|
|
236
|
+ *
|
|
237
|
+ * @param address address to write to
|
|
238
|
+ * @param data data to write
|
|
239
|
+ */
|
|
240
|
+ void writeByte(unsigned int address, uint8_t data);
|
|
241
|
+
|
|
242
|
+ /**
|
|
243
|
+ * write bytes to EEPROM
|
|
244
|
+ *
|
|
245
|
+ * @param address address to write to
|
|
246
|
+ * @param data data to write
|
|
247
|
+ * @param data length of data buffer
|
|
248
|
+ */
|
|
249
|
+ void writeBytes(unsigned int address, uint8_t* buf, unsigned int len);
|
|
250
|
+
|
|
251
|
+ /**
|
|
252
|
+ * read a byte from EEPROM
|
|
253
|
+ *
|
|
254
|
+ * @param address address to read from
|
|
255
|
+ * @return data value read
|
|
256
|
+ */
|
|
257
|
+ uint8_t readByte(unsigned int address);
|
|
258
|
+
|
|
259
|
+ /**
|
|
260
|
+ * read multiple bytes from EEPROM
|
|
261
|
+ *
|
|
262
|
+ * @param address address to read from
|
|
263
|
+ * @param buffer buffer to store data
|
|
264
|
+ * @param len number of bytes to read
|
|
265
|
+ */
|
|
266
|
+ void readBytes(unsigned int address, uint8_t* buf, unsigned int len);
|
|
267
|
+
|
|
268
|
+ protected:
|
|
269
|
+ mraa::I2c m_i2c;
|
|
270
|
+ void EEPROM_Write_Byte(unsigned int address, uint8_t data);
|
|
271
|
+ void EEPROM_Write_Bytes(unsigned int address, uint8_t* data,
|
|
272
|
+ unsigned int len);
|
|
273
|
+ uint8_t EEPROM_Read_Byte(unsigned int address);
|
|
274
|
+ unsigned int EEPROM_Read_Bytes(unsigned int address,
|
|
275
|
+ uint8_t* buf, unsigned int len);
|
|
276
|
+
|
|
277
|
+ private:
|
|
278
|
+ uint8_t m_addr;
|
|
279
|
+ };
|
|
280
|
+}
|
|
281
|
+
|
|
282
|
+
|