m24lr64e: Initial Implementation

This module was developed and tested on the Grove NFC Tag.  It is an
8KB EEPROM accessable (with various protection capabilites) via I2C
and NFC capable devices.

The code was based on the Seeed Studio example code with some help
from the datasheet.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson
2015-06-01 17:14:07 -06:00
committed by Mihai Tudor Panu
parent 6740932c47
commit 5733918a2f
9 changed files with 778 additions and 0 deletions

290
src/m24lr64e/m24lr64e.cxx Normal file
View File

@@ -0,0 +1,290 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <math.h>
#include <iostream>
#include <string>
#include "m24lr64e.h"
using namespace upm;
using namespace std;
M24LR64E::M24LR64E(int bus, AccessMode mode):
m_i2c(bus)
{
if (mode == USER_MODE)
m_addr = M24LR64E_DEFAULT_I2C_ADDR;
else
m_addr = M24LR64E_DEFAULT_I2C_ADDR_E2;
mraa_result_t rv;
if ( (rv = m_i2c.address(m_addr)) != MRAA_SUCCESS)
{
cerr << "M24LR64E: Could not initialize i2c address. " << endl;
mraa_result_print(rv);
return;
}
}
M24LR64E::~M24LR64E()
{
}
bool M24LR64E::submitPasswd(uint32_t passwd)
{
// this device actually uses two bytes to address a register
const int pktLen = 11;
uint8_t buf[pktLen];
buf[0] = 0x09;
buf[1] = 0x00;
buf[2] = ((passwd >> 24) & 0xff);
buf[3] = ((passwd >> 16) & 0xff);
buf[4] = ((passwd >> 8) & 0xff);
buf[5] = (passwd & 0xff);
buf[6] = 0x09;
// the password is written twice
buf[7] = ((passwd >> 24) & 0xff);
buf[8] = ((passwd >> 16) & 0xff);
buf[9] = ((passwd >> 8) & 0xff);
buf[10] = (passwd & 0xff);
if (m_i2c.write(buf, pktLen))
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
return false;
}
return true;
}
bool M24LR64E::writePasswd(uint32_t passwd)
{
const int pktLen = 11;
uint8_t buf[pktLen];
buf[0] = 0x09;
buf[1] = 0x00;
buf[2] = ((passwd >> 24) & 0xff);
buf[3] = ((passwd >> 16) & 0xff);
buf[4] = ((passwd >> 8) & 0xff);
buf[5] = (passwd & 0xff);
buf[6] = 0x07;
// the password is written twice
buf[7] = ((passwd >> 24) & 0xff);
buf[8] = ((passwd >> 16) & 0xff);
buf[9] = ((passwd >> 8) & 0xff);
buf[10] = (passwd & 0xff);
if (m_i2c.write(buf, pktLen))
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
return false;
}
return true;
}
void M24LR64E::sectorProtectConfig(unsigned int sectorNumber,
bool protectEnable,
SectorAccessRight accessRight,
SectorSelectPassWd passwd)
{
if(!protectEnable) {
EEPROM_Write_Byte(sectorNumber,0x0);
} else {
EEPROM_Write_Byte(sectorNumber,
protectEnable | (accessRight<<1) |(passwd<<2));
}
}
void M24LR64E::clearSectorProtect(void)
{
uint8_t buf[64]={0x0};
EEPROM_Write_Bytes(0, buf, 64);
}
void M24LR64E::sectorWriteLockBit(unsigned int sectorNumber,
bool sockEnable)
{
unsigned int sectorAddress = SECTOR_SECURITY_STATUS_BASE_ADDR
+ (sectorNumber/8);
uint8_t sectorBit = sectorNumber % 8;
uint8_t preStatus = EEPROM_Read_Byte(sectorAddress);
bool status = (preStatus >> sectorBit) & 0x01;
if(status != sockEnable) {
if(status == true) {
writeByte(sectorAddress,preStatus&(~(1<<sectorBit)));
} else {
writeByte(sectorAddress,preStatus|(1<<sectorBit));
}
}
}
uint8_t M24LR64E::getDSFID()
{
return EEPROM_Read_Byte(DSFID_ADDR);
}
uint8_t M24LR64E::getAFI()
{
return EEPROM_Read_Byte(AFI_ADDR);
}
void M24LR64E::getUID(uint8_t* buf)
{
EEPROM_Read_Bytes(UID_ADDR,buf,UID_LENGTH);
}
uint32_t M24LR64E::getMemorySize()
{
uint32_t volume = 0x0;
volume = EEPROM_Read_Byte(MEM_SIZE_ADDR);
volume = volume<<8|EEPROM_Read_Byte(MEM_SIZE_ADDR+1);
volume = volume<<8|EEPROM_Read_Byte(MEM_SIZE_ADDR+2);
return volume;
}
void M24LR64E::clearMemory()
{
for(int i = 0; i < EEPROM_I2C_LENGTH; i++){
writeByte(i,0x0);
}
}
void M24LR64E::writeByte(unsigned int address, uint8_t data)
{
EEPROM_Write_Byte(address, data);
}
void M24LR64E::writeBytes(unsigned int address, uint8_t* buf, unsigned int len)
{
EEPROM_Write_Bytes(address, buf, len);
}
uint8_t M24LR64E::readByte(unsigned int address)
{
return EEPROM_Read_Byte(address);
}
void M24LR64E::readBytes(unsigned int address, uint8_t* buf, unsigned int len)
{
EEPROM_Read_Bytes(address, buf, len);
}
void M24LR64E::EEPROM_Write_Byte(unsigned int address, uint8_t data)
{
const int pktLen = 3;
uint8_t buf[pktLen];
buf[0] = ((address >> 8) & 0xff);
buf[1] = (address & 0xff);
buf[2] = data;
if (m_i2c.write(buf, pktLen))
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
usleep(I2C_WRITE_TIME * 1000);
}
void M24LR64E::EEPROM_Write_Bytes(unsigned int address, uint8_t* data,
unsigned int len)
{
const int pktLen = 2 + len;
uint8_t buf[pktLen];
buf[0] = ((address >> 8) & 0xff);
buf[1] = (address & 0xff);
for (int i=0; i<len; i++)
buf[2+i] = data[i];
if (m_i2c.write(buf, pktLen))
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
usleep(I2C_WRITE_TIME * 1000);
}
uint8_t M24LR64E::EEPROM_Read_Byte(unsigned int address)
{
const int apktLen = 2;
uint8_t abuf[apktLen];
abuf[0] = ((address >> 8) & 0xff);
abuf[1] = (address & 0xff);
if (m_i2c.write(abuf, apktLen))
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
return 0x00;
}
const int pktLen = 1;
uint8_t buf[apktLen];
buf[0] = 0;
if (m_i2c.read(buf, pktLen) != pktLen)
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": read failed" << endl;
return 0x00;
}
return buf[0];
}
unsigned int M24LR64E::EEPROM_Read_Bytes(unsigned int address,
uint8_t* buf, unsigned int len)
{
const int apktLen = 2;
uint8_t abuf[apktLen];
abuf[0] = ((address >> 8) & 0xff);
abuf[1] = (address & 0xff);
if (m_i2c.write(abuf, apktLen))
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl;
return false;
}
int rv = m_i2c.read(buf, len);
if (rv != len)
{
cerr << __FUNCTION__ << "@" << __LINE__ << ": read failed" << endl;
}
return rv;
}