upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
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/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 
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 
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 
150  M24LR64E(int bus, AccessMode mode = USER_MODE);
151 
155  ~M24LR64E();
156 
162  bool submitPasswd(uint32_t passwd);
163 
169  bool writePasswd(uint32_t passwd);
170 
179  void sectorProtectConfig(unsigned int sectorNumber,
180  bool protectEnable,
181  SectorAccessRight accessRight,
182  SectorSelectPassWd passwd);
183 
187  void clearSectorProtect(void);
188 
196  void sectorWriteLockBit(unsigned int sectorNumber,
197  bool sockEnable);
198 
205  uint8_t getDSFID();
206 
213  uint8_t getAFI();
214 
222  uint8_t *getUID();
223 
230  uint32_t getMemorySize();
231 
235  void clearMemory();
236 
243  mraa::Result writeByte(unsigned int address, uint8_t data);
244 
252  mraa::Result writeBytes(unsigned int address, uint8_t* buffer, int len);
253 
260  uint8_t readByte(unsigned int address);
261 
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 
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
~M24LR64E()
Definition: m24lr64e.cxx:53
C++ API for the M24LR64E-based Grove NFC Tag.
Definition: m24lr64e.h:80
mraa::Result writeBytes(unsigned int address, uint8_t *buffer, int len)
Definition: m24lr64e.cxx:197
M24LR64E_ADDR_T
Definition: m24lr64e.h:98
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