upm  1.6.0
Sensor/Actuator repository for libmraa (v1.9.0)
m24lr64e.hpp
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 
82  class M24LR64E {
83  public:
84 
85  static const int EEPROM_I2C_LENGTH = 8192;
86  static const int PASSWORD_LENGTH = 4;
87  static const int SECTOR_SECURITY_STATUS_BASE_ADDR = 0x800; // 2048
88 
89  static const uint8_t LOCK_PROTECT_BIT = 0x01;
90  static const uint8_t WRITE_READ_PROTECT_BIT = 0x02;
91  static const uint8_t PASSWORD_CTRL_BIT = 0x04;
92 
93  static const int UID_LENGTH = 8; // bytes
94 
95  static const unsigned int I2C_WRITE_TIME = 5; // 5ms
96 
100  typedef enum {
101  I2C_PASSWORD_ADDR = 2304,
102  RF_PASSWORD_1_ADDR = 2308, // RF pwds not available in
103  RF_PASSWORD_2_ADDR = 2312, // I2C access modes
104  RF_PASSWORD_3_ADDR = 2316,
105  DSFID_ADDR = 2320, // 1 byte
106  AFI_ADDR = 2321, // 1 byte
107  RESV_ADDR = 2322, // 1 bytes
108  CONFIG_ADDR = 2323, // 1 bytes
109  UID_ADDR = 2324, // 8 bytes
110  MEM_SIZE_ADDR = 2332, // 3 bytes
111  IC_REF_ADDR = 2335, // 1 byte
112  PROG_COMP_ENERGY_HARVEST_ADDR = 2339 // 1 byte
113  } M24LR64E_ADDR_T;
114 
115  enum AccessMode {
116  USER_MODE = 0x0, // offers simple read/write access right
117  ROOT_MODE = 0x1 // offers password change access right
118  };
119 
120  enum SectorAccessRight {
121  // **********************************
122  // * submit passWd * no submit *
123  //b2,b1 * Read * Write * Read * Write *
124  // 00 * 1 1 1 0 *
125  // 01 * 1 1 1 1 *
126  // 10 * 1 1 0 0 *
127  // 11 * 0 1 0 0 *
128  // **********************************
129  Access_1110 = 0,
130  Access_1111 = 1,
131  Access_1100 = 2,
132  Access_0111 = 3,
133  };
134 
135  enum SectorSelectPassWd {
136  //00 => no passwd protect
137  //01 => passWd 1
138  //10 => passWd 2
139  //11 => passwd 3
140  noPasswd = 0,
141  passwd_1 = 1,
142  passwd_2 = 2,
143  passwd_3 = 3,
144  };
145 
152  M24LR64E(int bus, AccessMode mode = USER_MODE);
153 
157  ~M24LR64E();
158 
164  bool submitPasswd(uint32_t passwd);
165 
171  bool writePasswd(uint32_t passwd);
172 
181  void sectorProtectConfig(unsigned int sectorNumber,
182  bool protectEnable,
183  SectorAccessRight accessRight,
184  SectorSelectPassWd passwd);
185 
189  void clearSectorProtect(void);
190 
198  void sectorWriteLockBit(unsigned int sectorNumber,
199  bool sockEnable);
200 
207  uint8_t getDSFID();
208 
215  uint8_t getAFI();
216 
224  uint8_t *getUID();
225 
232  uint32_t getMemorySize();
233 
237  void clearMemory();
238 
245  mraa::Result writeByte(unsigned int address, uint8_t data);
246 
254  mraa::Result writeBytes(unsigned int address, uint8_t* buffer, int len);
255 
262  uint8_t readByte(unsigned int address);
263 
271  int readBytes(unsigned int address, uint8_t* buffer, int len);
272 
273  protected:
274  mraa::I2c m_i2c;
275  mraa::Result EEPROM_Write_Byte(unsigned int address, uint8_t data);
276  mraa::Result EEPROM_Write_Bytes(unsigned int address, uint8_t* data,
277  int len);
278  uint8_t EEPROM_Read_Byte(unsigned int address);
279  int EEPROM_Read_Bytes(unsigned int address,
280  uint8_t* buffer, int len);
281 
282  private:
283  uint8_t m_addr;
284  };
285 }
286 
287 
bool writePasswd(uint32_t passwd)
Definition: m24lr64e.cxx:89
uint8_t getDSFID()
Definition: m24lr64e.cxx:158
int readBytes(unsigned int address, uint8_t *buffer, int len)
Definition: m24lr64e.cxx:207
uint8_t readByte(unsigned int address)
Definition: m24lr64e.cxx:202
void clearMemory()
Definition: m24lr64e.cxx:185
uint8_t * getUID()
Definition: m24lr64e.cxx:168
M24LR64E(int bus, AccessMode mode=USER_MODE)
Definition: m24lr64e.cxx:36
uint32_t getMemorySize()
Definition: m24lr64e.cxx:176
bool submitPasswd(uint32_t passwd)
Definition: m24lr64e.cxx:57
C++ API for the kxtj3 driver.
Definition: a110x.hpp:29
~M24LR64E()
Definition: m24lr64e.cxx:53
C++ API for the M24LR64E NFC Tag.
Definition: m24lr64e.hpp:82
mraa::Result writeBytes(unsigned int address, uint8_t *buffer, int len)
Definition: m24lr64e.cxx:197
M24LR64E_ADDR_T
Definition: m24lr64e.hpp:100
uint8_t getAFI()
Definition: m24lr64e.cxx:163
mraa::Result writeByte(unsigned int address, uint8_t data)
Definition: m24lr64e.cxx:192
void sectorProtectConfig(unsigned int sectorNumber, bool protectEnable, SectorAccessRight accessRight, SectorSelectPassWd passwd)
Definition: m24lr64e.cxx:120
void sectorWriteLockBit(unsigned int sectorNumber, bool sockEnable)
Definition: m24lr64e.cxx:140
void clearSectorProtect(void)
Definition: m24lr64e.cxx:133