upm  0.3.0
Sensor/Actuator repository for libmraa (v0.6.1)
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 
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 
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 
148  M24LR64E(int bus, AccessMode mode = USER_MODE);
149 
153  ~M24LR64E();
154 
160  bool submitPasswd(uint32_t passwd);
161 
167  bool writePasswd(uint32_t passwd);
168 
177  void sectorProtectConfig(unsigned int sectorNumber,
178  bool protectEnable,
179  SectorAccessRight accessRight,
180  SectorSelectPassWd passwd);
181 
185  void clearSectorProtect(void);
186 
194  void sectorWriteLockBit(unsigned int sectorNumber,
195  bool sockEnable);
196 
203  uint8_t getDSFID();
204 
211  uint8_t getAFI();
212 
219  void getUID(uint8_t* buf);
220 
227  uint32_t getMemorySize();
228 
232  void clearMemory();
233 
240  void writeByte(unsigned int address, uint8_t data);
241 
249  void writeBytes(unsigned int address, uint8_t* buf, unsigned int len);
250 
257  uint8_t readByte(unsigned int address);
258 
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 
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:96
~M24LR64E()
Definition: m24lr64e.cxx:53
C++ API for the M24LR64E based Grove NFC Tag.
Definition: m24lr64e.h:78
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