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/common.hpp>
37 #include <mraa/i2c.hpp>
38 
39 #define M24LR64E_I2C_BUS 0
40 #define M24LR64E_DEFAULT_I2C_ADDR 0x53
41 #define M24LR64E_DEFAULT_I2C_ADDR_E2 (M24LR64E_DEFAULT_I2C_ADDR | 0x04)
42 
43 namespace upm {
44 
45   /**
46    * @brief Grove NFC Tag
47    * @defgroup m24lr64e libupm-m24lr64e
48    * @ingroup seeed i2c other
49    */
50 
51   /**
52    * @library m24lr64e
53    * @sensor m24lr64e
54    * @comname Grove NFC Tag
55    * @type other
56    * @man seeed
57    * @web http://www.seeedstudio.com/wiki/Grove_-_NFC_Tag
58    * @con i2c
59    *
60    * @brief C++ API for the M24LR64E-based Grove NFC Tag
61    *
62    * Grove NFC tag is an 8KB electrically erasable programmable read-only memory (EEPROM)
63    * that can be written to or read from using I2C and NFC-equipped devices.
64    *
65    * The user mode (default) allows read and write access to all 8KB
66    * of space, provided the sector security status (SSS) allows it.
67    * The root mode allows modification of the SSS data and other
68    * information, provided the proper password is submitted.  The
69    * default password for a new tag is 0x00000000. See the datasheet
70    * for more details.
71    *
72    * The Seeed Studio* wiki page for this device includes a link to an
73    * Android* application that can be used to also read and write the
74    * device via NFC, as well as set NFC passwords, which cannot be
75    * done via I2C.
76    *
77    * @image html m24lr64e.jpg
78    * @snippet m24lr64e.cxx Interesting
79    */
80   class M24LR64E {
81   public:
82 
83     static const int EEPROM_I2C_LENGTH          = 8192;
84     static const int PASSWORD_LENGTH            = 4;
85     static const int SECTOR_SECURITY_STATUS_BASE_ADDR = 0x800; // 2048
86 
87     static const uint8_t LOCK_PROTECT_BIT       = 0x01;
88     static const uint8_t WRITE_READ_PROTECT_BIT = 0x02;
89     static const uint8_t PASSWORD_CTRL_BIT      = 0x04;
90 
91     static const int UID_LENGTH                 = 8; // bytes
92 
93     static const unsigned int I2C_WRITE_TIME    = 5; // 5ms
94 
95     /**
96      * M24LR64E addresses, accessible only in the root mode
97      */
98     typedef enum {
99       I2C_PASSWORD_ADDR                  = 2304,
100       RF_PASSWORD_1_ADDR                 = 2308, // RF pwds not available in
101       RF_PASSWORD_2_ADDR                 = 2312, // I2C access modes
102       RF_PASSWORD_3_ADDR                 = 2316,
103       DSFID_ADDR                         = 2320, // 1 byte
104       AFI_ADDR                           = 2321, // 1 byte
105       RESV_ADDR                          = 2322, // 1 bytes
106       CONFIG_ADDR                        = 2323, // 1 bytes
107       UID_ADDR                           = 2324, // 8 bytes
108       MEM_SIZE_ADDR                      = 2332, // 3 bytes
109       IC_REF_ADDR                        = 2335, // 1 byte
110       PROG_COMP_ENERGY_HARVEST_ADDR      = 2339  // 1 byte
111     } M24LR64E_ADDR_T;
112 
113     enum AccessMode {
114       USER_MODE = 0x0,   // offers simple read/write access right
115       ROOT_MODE = 0x1    // offers password change access right
116     };
117 
118     enum SectorAccessRight {
119       //      **********************************
120       //      *  submit passWd *   no submit   *
121       //b2,b1 *  Read * Write  *  Read * Write *
122       // 00   *    1       1        1      0   *
123       // 01   *    1       1        1      1   *
124       // 10   *    1       1        0      0   *
125       // 11   *    0       1        0      0   *
126       //      **********************************
127       Access_1110 = 0,
128       Access_1111 = 1,
129       Access_1100 = 2,
130       Access_0111 = 3,
131     };
132 
133     enum SectorSelectPassWd {
134       //00 => no passwd protect
135       //01 => passWd 1
136       //10 => passWd 2
137       //11 => passwd 3
138       noPasswd = 0,
139       passwd_1 = 1,
140       passwd_2 = 2,
141       passwd_3 = 3,
142     };
143 
144     /**
145      * M24LR64E constructor
146      *
147      * @param bus I2C bus to use
148      * @param mode Access mode (user or root) to use
149      */
150     M24LR64E(int bus, AccessMode mode = USER_MODE);
151 
152     /**
153      * M24LR64E destructor
154      */
155     ~M24LR64E();
156 
157     /**
158      * Submits an I2C access password
159      *
160      * @param passwd 4-byte access password
161      */
162     bool submitPasswd(uint32_t passwd);
163 
164     /**
165      * Writes a new I2C password
166      *
167      * @param passwd 4-byte access password
168      */
169     bool writePasswd(uint32_t passwd);
170 
171     /**
172      * Sets a protection bit for a sector. Must be in the root mode
173      *
174      * @param sectorNumber Sector whose protection you are modifying
175      * @param protectEnable True if you are enabling protection
176      * @param accessRight Access rights to set
177      * @param passwd Password number to enable, if any
178      */
179     void sectorProtectConfig(unsigned int sectorNumber,
180                              bool protectEnable,
181                              SectorAccessRight accessRight,
182                              SectorSelectPassWd passwd);
183 
184     /**
185      * Clears sector protection bits. Must be in the root mode.
186      */
187     void clearSectorProtect(void);
188 
189     /**
190      * Sets or clears a sector security status lock bit for a sector.
191      * Must be in the root mode.
192      *
193      * @param sectorNumber Sector whose SSS you want to modify
194      * @param sockEnable True to set the bit, false to clear it
195      */
196     void sectorWriteLockBit(unsigned int sectorNumber,
197                             bool sockEnable);
198 
199     /**
200      * Returns a data storage family identifier (DSFID)
201      * Must be in the root mode.
202      *
203      * @return DSFID
204      */
205     uint8_t getDSFID();
206 
207     /**
208      * Returns an application family identifier (AFI)
209      * Must be in the root mode.
210      *
211      * @return AFI
212      */
213     uint8_t getAFI();
214 
215     /**
216      * Returns a unique ID.
217      * Must be in the root mode.
218      * Maintained to preserve compatibility with older code.
219      *
220      * @result buf Buffer to hold the UID. Must be UID_LENGTH bytes.
221      */
222     uint8_t *getUID();
223 
224     /**
225      * Returns the memory size
226      * Must be in the root mode.
227      *
228      * @return Amount of memory present
229      */
230     uint32_t getMemorySize();
231 
232     /**
233      * Sets all memory to 0, if permissions allow
234      */
235     void clearMemory();
236 
237     /**
238      * Writes a byte to the EEPROM
239      *
240      * @param address Address to write to
241      * @param data Data to write
242      */
243     mraa::Result writeByte(unsigned int address, uint8_t data);
244 
245     /**
246      * Writes bytes to the EEPROM
247      *
248      * @param address Address to write to
249      * @param data Data to write
250      * @param data Length of the data buffer
251      */
252     mraa::Result writeBytes(unsigned int address, uint8_t* buffer, int len);
253 
254     /**
255      * Reads a byte from the EEPROM
256      *
257      * @param address Address to read from
258      * @return data Value read
259      */
260     uint8_t readByte(unsigned int address);
261 
262     /**
263      * Reads multiple bytes from the EEPROM
264      *
265      * @param address Address to read from
266      * @param buffer Buffer to store data
267      * @param len Number of bytes to read
268      */
269     int readBytes(unsigned int address, uint8_t* buffer, int len);
270 
271   protected:
272     mraa::I2c m_i2c;
273     mraa::Result EEPROM_Write_Byte(unsigned int address, uint8_t data);
274     mraa::Result EEPROM_Write_Bytes(unsigned int address, uint8_t* data,
275                             int len);
276     uint8_t EEPROM_Read_Byte(unsigned int address);
277     int EEPROM_Read_Bytes(unsigned int address,
278                                    uint8_t* buffer, int len);
279 
280   private:
281     uint8_t m_addr;
282   };
283 }
284 
285 
286