upm  0.3.1
Sensor/Actuator repository for libmraa (v0.7.2)
m24lr64e.h
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 
79  class M24LR64E {
80  public:
81 
82  static const int EEPROM_I2C_LENGTH = 8192;
83  static const int PASSWORD_LENGTH = 4;
84  static const int SECTOR_SECURITY_STATUS_BASE_ADDR = 0x800; // 2048
85 
86  static const uint8_t LOCK_PROTECT_BIT = 0x01;
87  static const uint8_t WRITE_READ_PROTECT_BIT = 0x02;
88  static const uint8_t PASSWORD_CTRL_BIT = 0x04;
89 
90  static const int UID_LENGTH = 8; // bytes
91 
92  static const unsigned int I2C_WRITE_TIME = 5; // 5ms
93 
97  typedef enum {
98  I2C_PASSWORD_ADDR = 2304,
99  RF_PASSWORD_1_ADDR = 2308, // RF pwds not available in
100  RF_PASSWORD_2_ADDR = 2312, // i2c access modes
101  RF_PASSWORD_3_ADDR = 2316,
102  DSFID_ADDR = 2320, // 1 byte
103  AFI_ADDR = 2321, // 1 byte
104  RESV_ADDR = 2322, // 1 bytes
105  CONFIG_ADDR = 2323, // 1 bytes
106  UID_ADDR = 2324, // 8 bytes
107  MEM_SIZE_ADDR = 2332, // 3 bytes
108  IC_REF_ADDR = 2335, // 1 byte
109  PROG_COMP_ENERGY_HARVEST_ADDR = 2339 // 1 byte
110  } M24LR64E_ADDR_T;
111 
112  enum AccessMode {
113  USER_MODE = 0x0, // offer simple read/write access right
114  ROOT_MODE = 0x1 // offer password change access right
115  };
116 
117  enum SectorAccessRight {
118  // **********************************
119  // * submit passWd * no submit *
120  //b2,b1 * Read * Write * Read * Write *
121  // 00 * 1 1 1 0 *
122  // 01 * 1 1 1 1 *
123  // 10 * 1 1 0 0 *
124  // 11 * 0 1 0 0 *
125  // **********************************
126  Access_1110 = 0,
127  Access_1111 = 1,
128  Access_1100 = 2,
129  Access_0111 = 3,
130  };
131 
132  enum SectorSelectPassWd {
133  //00 => no passwd protect
134  //01 => passWd 1
135  //10 => passWd 2
136  //11 => passwd 3
137  noPasswd = 0,
138  passwd_1 = 1,
139  passwd_2 = 2,
140  passwd_3 = 3,
141  };
142 
149  M24LR64E(int bus, AccessMode mode = USER_MODE);
150 
154  ~M24LR64E();
155 
161  bool submitPasswd(uint32_t passwd);
162 
168  bool writePasswd(uint32_t passwd);
169 
178  void sectorProtectConfig(unsigned int sectorNumber,
179  bool protectEnable,
180  SectorAccessRight accessRight,
181  SectorSelectPassWd passwd);
182 
186  void clearSectorProtect(void);
187 
195  void sectorWriteLockBit(unsigned int sectorNumber,
196  bool sockEnable);
197 
204  uint8_t getDSFID();
205 
212  uint8_t getAFI();
213 
220  void getUID(uint8_t* buf);
221 
228  uint32_t getMemorySize();
229 
233  void clearMemory();
234 
241  void writeByte(unsigned int address, uint8_t data);
242 
250  void writeBytes(unsigned int address, uint8_t* buf, unsigned int len);
251 
258  uint8_t readByte(unsigned int address);
259 
267  void readBytes(unsigned int address, uint8_t* buf, unsigned int len);
268 
269  protected:
270  mraa::I2c m_i2c;
271  void EEPROM_Write_Byte(unsigned int address, uint8_t data);
272  void EEPROM_Write_Bytes(unsigned int address, uint8_t* data,
273  unsigned int len);
274  uint8_t EEPROM_Read_Byte(unsigned int address);
275  unsigned int EEPROM_Read_Bytes(unsigned int address,
276  uint8_t* buf, unsigned int len);
277 
278  private:
279  uint8_t m_addr;
280  };
281 }
282 
283 
bool writePasswd(uint32_t passwd)
Definition: m24lr64e.cxx:88
uint8_t getDSFID()
Definition: m24lr64e.cxx:156
uint8_t readByte(unsigned int address)
Definition: m24lr64e.cxx:197
void clearMemory()
Definition: m24lr64e.cxx:180
M24LR64E(int bus, AccessMode mode=USER_MODE)
Definition: m24lr64e.cxx:36
uint32_t getMemorySize()
Definition: m24lr64e.cxx:171
bool submitPasswd(uint32_t passwd)
Definition: m24lr64e.cxx:57
Definition: a110x.h:29
M24LR64E_ADDR_T
Definition: m24lr64e.h:97
~M24LR64E()
Definition: m24lr64e.cxx:53
C++ API for the M24LR64E based Grove NFC Tag.
Definition: m24lr64e.h:79
uint8_t getAFI()
Definition: m24lr64e.cxx:161
void getUID(uint8_t *buf)
Definition: m24lr64e.cxx:166
void readBytes(unsigned int address, uint8_t *buf, unsigned int len)
Definition: m24lr64e.cxx:202
void sectorProtectConfig(unsigned int sectorNumber, bool protectEnable, SectorAccessRight accessRight, SectorSelectPassWd passwd)
Definition: m24lr64e.cxx:118
void writeByte(unsigned int address, uint8_t data)
Definition: m24lr64e.cxx:187
void sectorWriteLockBit(unsigned int sectorNumber, bool sockEnable)
Definition: m24lr64e.cxx:138
void writeBytes(unsigned int address, uint8_t *buf, unsigned int len)
Definition: m24lr64e.cxx:192
void clearSectorProtect(void)
Definition: m24lr64e.cxx:131