grove nfc_tag sample program

Dependencies:   mbed

Committer:
superphil06
Date:
Fri Mar 31 11:20:13 2017 +0000
Revision:
1:c492babe8dc3
Parent:
0:b939441bcdc7
grove nfc tag example programm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
superphil06 0:b939441bcdc7 1 /*
superphil06 0:b939441bcdc7 2 * NfcTag.cpp
superphil06 0:b939441bcdc7 3 * a library to use NFC Tag
superphil06 0:b939441bcdc7 4 *
superphil06 0:b939441bcdc7 5 * Copyright (c) 2014 seeed technology inc.
superphil06 0:b939441bcdc7 6 * Website : www.seeed.cc
superphil06 0:b939441bcdc7 7 * Author : lawliet zou
superphil06 0:b939441bcdc7 8 * Create Time: March 2014
superphil06 0:b939441bcdc7 9 * Change Log :
superphil06 0:b939441bcdc7 10 *
superphil06 0:b939441bcdc7 11 * The MIT License (MIT)
superphil06 0:b939441bcdc7 12 *
superphil06 0:b939441bcdc7 13 * Permission is hereby granted, free of charge, to any person obtaining a copy
superphil06 0:b939441bcdc7 14 * of this software and associated documentation files (the "Software"), to deal
superphil06 0:b939441bcdc7 15 * in the Software without restriction, including without limitation the rights
superphil06 0:b939441bcdc7 16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
superphil06 0:b939441bcdc7 17 * copies of the Software, and to permit persons to whom the Software is
superphil06 0:b939441bcdc7 18 * furnished to do so, subject to the following conditions:
superphil06 0:b939441bcdc7 19 *
superphil06 0:b939441bcdc7 20 * The above copyright notice and this permission notice shall be included in
superphil06 0:b939441bcdc7 21 * all copies or substantial portions of the Software.
superphil06 0:b939441bcdc7 22 *
superphil06 0:b939441bcdc7 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
superphil06 0:b939441bcdc7 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
superphil06 0:b939441bcdc7 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
superphil06 0:b939441bcdc7 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
superphil06 0:b939441bcdc7 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
superphil06 0:b939441bcdc7 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
superphil06 0:b939441bcdc7 29 * THE SOFTWARE.
superphil06 0:b939441bcdc7 30 */
superphil06 0:b939441bcdc7 31
superphil06 0:b939441bcdc7 32 #include "nfc_tag.h"
superphil06 0:b939441bcdc7 33 /*
superphil06 0:b939441bcdc7 34 void NfcTag::init()
superphil06 0:b939441bcdc7 35 {
superphil06 0:b939441bcdc7 36 Wire.begin();
superphil06 0:b939441bcdc7 37 }*/
superphil06 0:b939441bcdc7 38
superphil06 0:b939441bcdc7 39 // Class Constructor
superphil06 0:b939441bcdc7 40
superphil06 0:b939441bcdc7 41 NfcTag::NfcTag(PinName sda,PinName scl,uint8_t address,AccessMode mode = USER_MODE): m_I2C(sda, scl)
superphil06 0:b939441bcdc7 42 {
superphil06 0:b939441bcdc7 43 if(mode == USER_MODE){
superphil06 1:c492babe8dc3 44 eeAddr = EEPROM_ADDR_E2_DISABLE<<1;
superphil06 0:b939441bcdc7 45 }else{
superphil06 1:c492babe8dc3 46 eeAddr = EEPROM_ADDR_E2_ENABLE<<1;
superphil06 0:b939441bcdc7 47 }
superphil06 0:b939441bcdc7 48 _address = address; // m_I2C Module Address
superphil06 0:b939441bcdc7 49
superphil06 0:b939441bcdc7 50 }
superphil06 0:b939441bcdc7 51
superphil06 0:b939441bcdc7 52 NfcTag::~NfcTag()
superphil06 0:b939441bcdc7 53 {
superphil06 0:b939441bcdc7 54
superphil06 0:b939441bcdc7 55 }
superphil06 0:b939441bcdc7 56
superphil06 0:b939441bcdc7 57 void NfcTag::submitPassWd(uint8_t* passWd)
superphil06 0:b939441bcdc7 58 {/*
superphil06 0:b939441bcdc7 59 Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 60 Wire.write(0x09);
superphil06 0:b939441bcdc7 61 Wire.write(0x00);
superphil06 0:b939441bcdc7 62 */
superphil06 0:b939441bcdc7 63 (void) m_I2C.start();
superphil06 0:b939441bcdc7 64 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 65 m_I2C.write(0x09);
superphil06 0:b939441bcdc7 66 m_I2C.write(0x00);
superphil06 0:b939441bcdc7 67
superphil06 0:b939441bcdc7 68
superphil06 0:b939441bcdc7 69 for(int i = 0; i < PASSWORD_LENGTH; i++){
superphil06 0:b939441bcdc7 70 // Wire.write(passWd[i]);
superphil06 0:b939441bcdc7 71 m_I2C.write(passWd[i]);
superphil06 0:b939441bcdc7 72
superphil06 0:b939441bcdc7 73 }
superphil06 0:b939441bcdc7 74 //Wire.write(0x09);
superphil06 0:b939441bcdc7 75 m_I2C.write(0x09);
superphil06 0:b939441bcdc7 76
superphil06 0:b939441bcdc7 77 for(int i = 0; i < PASSWORD_LENGTH; i++){
superphil06 0:b939441bcdc7 78 //Wire.write(passWd[i]);
superphil06 0:b939441bcdc7 79 m_I2C.write(passWd[i]);
superphil06 0:b939441bcdc7 80 }
superphil06 0:b939441bcdc7 81 //Wire.endTransmission();// stop transmitting
superphil06 0:b939441bcdc7 82 m_I2C.stop();
superphil06 0:b939441bcdc7 83 }
superphil06 0:b939441bcdc7 84
superphil06 0:b939441bcdc7 85 void NfcTag::writePassWd(uint8_t* passWd)
superphil06 0:b939441bcdc7 86 {
superphil06 0:b939441bcdc7 87 /*Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 88 Wire.write(0x09);
superphil06 0:b939441bcdc7 89 Wire.write(0x00);*/
superphil06 0:b939441bcdc7 90 (void) m_I2C.start();
superphil06 0:b939441bcdc7 91 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 92 m_I2C.write(0x09);
superphil06 0:b939441bcdc7 93 m_I2C.write(0x00);
superphil06 0:b939441bcdc7 94
superphil06 0:b939441bcdc7 95 for(int i = 0; i < PASSWORD_LENGTH; i++){
superphil06 0:b939441bcdc7 96 // Wire.write(passWd[i]);
superphil06 0:b939441bcdc7 97 m_I2C.write(passWd[i]);
superphil06 0:b939441bcdc7 98 }
superphil06 0:b939441bcdc7 99 //Wire.write(0x07);
superphil06 0:b939441bcdc7 100 m_I2C.write(0x07);
superphil06 0:b939441bcdc7 101 for(int i = 0; i < PASSWORD_LENGTH; i++){
superphil06 0:b939441bcdc7 102 // Wire.write(passWd[i]);
superphil06 0:b939441bcdc7 103 m_I2C.write(passWd[i]);
superphil06 0:b939441bcdc7 104 }
superphil06 0:b939441bcdc7 105 // Wire.endTransmission(); // stop transmitting
superphil06 0:b939441bcdc7 106 m_I2C.stop();
superphil06 0:b939441bcdc7 107 }
superphil06 0:b939441bcdc7 108
superphil06 0:b939441bcdc7 109 void NfcTag::sectorProtectConfig(unsigned int sectorNumber, bool protectEnable, SectorAccessRight accessRight, SectorSelectPassWd passWd)
superphil06 0:b939441bcdc7 110 {
superphil06 0:b939441bcdc7 111 if(!protectEnable){
superphil06 0:b939441bcdc7 112 _EEPROM_Write_uint8_t(sectorNumber,0x0);
superphil06 0:b939441bcdc7 113 }else{
superphil06 0:b939441bcdc7 114 _EEPROM_Write_uint8_t(sectorNumber,protectEnable|(accessRight<<1)|(passWd<<2));
superphil06 0:b939441bcdc7 115 }
superphil06 0:b939441bcdc7 116 }
superphil06 0:b939441bcdc7 117
superphil06 0:b939441bcdc7 118 void NfcTag::clearSectorProtect(void)
superphil06 0:b939441bcdc7 119 {
superphil06 0:b939441bcdc7 120 uint8_t buf[64]={0x0};
superphil06 0:b939441bcdc7 121 _EEPROM_Write_uint8_ts(0, buf, 64);
superphil06 0:b939441bcdc7 122 }
superphil06 0:b939441bcdc7 123
superphil06 0:b939441bcdc7 124
superphil06 0:b939441bcdc7 125 void NfcTag::sectorWriteSockConfig(unsigned int sectorNumber, bool sockEnable)
superphil06 0:b939441bcdc7 126 {
superphil06 0:b939441bcdc7 127 unsigned int sectorAddress = SECTOR_SECURITY_STATUS_BASE_ADDR + (sectorNumber/8);
superphil06 0:b939441bcdc7 128 uint8_t sectorBit = sectorNumber%8;
superphil06 0:b939441bcdc7 129 uint8_t preStatus = _EEPROM_Read_uint8_t(sectorAddress);
superphil06 0:b939441bcdc7 130 //bool status = (preStatus|(1<<sectorBit))>>sectorBit;
superphil06 0:b939441bcdc7 131 bool status = (preStatus>>sectorBit)&0x01;
superphil06 0:b939441bcdc7 132 if(status != sockEnable){
superphil06 0:b939441bcdc7 133 if(status == true){
superphil06 0:b939441bcdc7 134 writeuint8_t(sectorAddress,preStatus&(~(1<<sectorBit)));
superphil06 0:b939441bcdc7 135 }else{
superphil06 0:b939441bcdc7 136 writeuint8_t(sectorAddress,preStatus|(1<<sectorBit));
superphil06 0:b939441bcdc7 137 }
superphil06 0:b939441bcdc7 138 }
superphil06 0:b939441bcdc7 139 }
superphil06 0:b939441bcdc7 140
superphil06 0:b939441bcdc7 141
superphil06 0:b939441bcdc7 142 uint8_t NfcTag::getDSFID()
superphil06 0:b939441bcdc7 143 {
superphil06 0:b939441bcdc7 144 return _EEPROM_Read_uint8_t(DSFID_ADDR);
superphil06 0:b939441bcdc7 145 }
superphil06 0:b939441bcdc7 146
superphil06 0:b939441bcdc7 147 uint8_t NfcTag::getAFI()
superphil06 0:b939441bcdc7 148 {
superphil06 0:b939441bcdc7 149 return _EEPROM_Read_uint8_t(AFI_ADDR);
superphil06 0:b939441bcdc7 150 }
superphil06 0:b939441bcdc7 151
superphil06 0:b939441bcdc7 152 uint16_t NfcTag::getRFU()
superphil06 0:b939441bcdc7 153 {
superphil06 0:b939441bcdc7 154 uint16_t rfu = 0x00;
superphil06 0:b939441bcdc7 155 rfu = _EEPROM_Read_uint8_t(RFU_ADDR);
superphil06 0:b939441bcdc7 156 return (rfu<<8|_EEPROM_Read_uint8_t(RFU_ADDR+1));
superphil06 0:b939441bcdc7 157 }
superphil06 0:b939441bcdc7 158
superphil06 0:b939441bcdc7 159 uint8_t NfcTag::getUID(uint8_t* buf)
superphil06 0:b939441bcdc7 160 {
superphil06 0:b939441bcdc7 161 _EEPROM_Read_uint8_ts(UID_ADDR,buf,UID_LENGTH);
superphil06 0:b939441bcdc7 162 return UID_LENGTH;
superphil06 0:b939441bcdc7 163 }
superphil06 0:b939441bcdc7 164
superphil06 0:b939441bcdc7 165 uint32_t NfcTag::getMemoryVolume()
superphil06 0:b939441bcdc7 166 {
superphil06 0:b939441bcdc7 167 uint32_t volume = 0x0;
superphil06 0:b939441bcdc7 168 volume = _EEPROM_Read_uint8_t(MEMORY_VOLUME_ADDR);
superphil06 0:b939441bcdc7 169 volume = volume<<8|_EEPROM_Read_uint8_t(MEMORY_VOLUME_ADDR+1);
superphil06 0:b939441bcdc7 170 volume = volume<<8|_EEPROM_Read_uint8_t(MEMORY_VOLUME_ADDR+2);
superphil06 0:b939441bcdc7 171 return volume;
superphil06 0:b939441bcdc7 172 }
superphil06 0:b939441bcdc7 173
superphil06 0:b939441bcdc7 174 uint8_t NfcTag::getICNumber()
superphil06 0:b939441bcdc7 175 {
superphil06 0:b939441bcdc7 176 return _EEPROM_Read_uint8_t(IC_NUMBER_ADDR);
superphil06 0:b939441bcdc7 177 }
superphil06 0:b939441bcdc7 178
superphil06 0:b939441bcdc7 179 void NfcTag::clearMemory()
superphil06 0:b939441bcdc7 180 {
superphil06 0:b939441bcdc7 181 for(int i = 0; i < EEPROM_I2C_LENGTH; i++){
superphil06 0:b939441bcdc7 182 writeuint8_t(i,0x0);
superphil06 0:b939441bcdc7 183 }
superphil06 0:b939441bcdc7 184 }
superphil06 0:b939441bcdc7 185
superphil06 0:b939441bcdc7 186 void NfcTag::writeuint8_t(unsigned int address, uint8_t data)
superphil06 0:b939441bcdc7 187 {
superphil06 0:b939441bcdc7 188 _EEPROM_Write_uint8_t(address, data);
superphil06 0:b939441bcdc7 189 }
superphil06 0:b939441bcdc7 190
superphil06 0:b939441bcdc7 191 void NfcTag::writeuint8_ts(unsigned int address, uint8_t* buf, unsigned int len)
superphil06 0:b939441bcdc7 192 {
superphil06 0:b939441bcdc7 193 _EEPROM_Write_uint8_ts(address, buf, len);
superphil06 0:b939441bcdc7 194 }
superphil06 0:b939441bcdc7 195
superphil06 0:b939441bcdc7 196 uint8_t NfcTag::readuint8_t(unsigned int address)
superphil06 0:b939441bcdc7 197 {
superphil06 0:b939441bcdc7 198 return _EEPROM_Read_uint8_t(address);
superphil06 0:b939441bcdc7 199 }
superphil06 0:b939441bcdc7 200
superphil06 0:b939441bcdc7 201 void NfcTag::readuint8_ts(unsigned int address, uint8_t* buf, unsigned int len)
superphil06 0:b939441bcdc7 202 {
superphil06 0:b939441bcdc7 203 _EEPROM_Read_uint8_ts(address, buf, len);
superphil06 0:b939441bcdc7 204 }
superphil06 0:b939441bcdc7 205
superphil06 0:b939441bcdc7 206 void NfcTag::_EEPROM_Write_uint8_t(unsigned int address, uint8_t data)
superphil06 0:b939441bcdc7 207 {
superphil06 0:b939441bcdc7 208 /*Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 209 Wire.write((int)(address >> 8)); // MSB
superphil06 0:b939441bcdc7 210 Wire.write((int)(address & 0xFF));// LSB
superphil06 0:b939441bcdc7 211 Wire.write(data); // sends one uint8_t
superphil06 0:b939441bcdc7 212 //delay(5);
superphil06 0:b939441bcdc7 213 Wire.endTransmission(); // stop transmitting */
superphil06 0:b939441bcdc7 214 (void) m_I2C.start();
superphil06 0:b939441bcdc7 215 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 216 m_I2C.write((int)(address >> 8));
superphil06 0:b939441bcdc7 217 m_I2C.write((int)(address & 0xFF));
superphil06 0:b939441bcdc7 218 m_I2C.write(data);
superphil06 0:b939441bcdc7 219 wait_ms(5);
superphil06 0:b939441bcdc7 220 m_I2C.stop();
superphil06 0:b939441bcdc7 221
superphil06 0:b939441bcdc7 222
superphil06 0:b939441bcdc7 223 }
superphil06 0:b939441bcdc7 224
superphil06 0:b939441bcdc7 225 void NfcTag::_EEPROM_Write_uint8_ts(unsigned int address, uint8_t* buf, unsigned int len)
superphil06 0:b939441bcdc7 226 {
superphil06 0:b939441bcdc7 227 /*Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 228 Wire.write((int)(address >> 8)); // MSB
superphil06 0:b939441bcdc7 229 Wire.write((int)(address & 0xFF));// LSB */
superphil06 0:b939441bcdc7 230 (void) m_I2C.start();
superphil06 0:b939441bcdc7 231 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 232 m_I2C.write((int)(address >> 8));
superphil06 0:b939441bcdc7 233 m_I2C.write((int)(address & 0xFF));
superphil06 0:b939441bcdc7 234
superphil06 0:b939441bcdc7 235
superphil06 0:b939441bcdc7 236
superphil06 0:b939441bcdc7 237
superphil06 0:b939441bcdc7 238
superphil06 0:b939441bcdc7 239
superphil06 0:b939441bcdc7 240
superphil06 0:b939441bcdc7 241 for(int i = 0; i < len; i++){
superphil06 0:b939441bcdc7 242 // Wire.write(buf[i]); // sends one uint8_t
superphil06 0:b939441bcdc7 243 m_I2C.write(buf[i]); // sends one uint8_t
superphil06 0:b939441bcdc7 244 }
superphil06 0:b939441bcdc7 245 //Wire.endTransmission(); // stop transmitting
superphil06 0:b939441bcdc7 246 m_I2C.stop();
superphil06 0:b939441bcdc7 247 }
superphil06 0:b939441bcdc7 248
superphil06 0:b939441bcdc7 249 uint8_t NfcTag::_EEPROM_Read_uint8_t(unsigned int address)
superphil06 0:b939441bcdc7 250 {
superphil06 0:b939441bcdc7 251 uint8_t rdata = 0x7F;
superphil06 0:b939441bcdc7 252 /*Wire.beginTransmission(eeAddr); //Device Address
superphil06 0:b939441bcdc7 253 Wire.write((int)(address >> 8)); // EEPROM_MSB
superphil06 0:b939441bcdc7 254 Wire.write((int)(address & 0xFF)); // EEPROM_LSB
superphil06 0:b939441bcdc7 255 Wire.endTransmission();*/
superphil06 0:b939441bcdc7 256 (void) m_I2C.start();
superphil06 0:b939441bcdc7 257 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 258 m_I2C.write((int)(address >> 8));
superphil06 0:b939441bcdc7 259 m_I2C.write((int)(address & 0xFF));
superphil06 0:b939441bcdc7 260 m_I2C.stop();
superphil06 0:b939441bcdc7 261
superphil06 0:b939441bcdc7 262 //Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 263 (void) m_I2C.start();
superphil06 0:b939441bcdc7 264 // Wire.requestFrom(eeAddr,1);
superphil06 0:b939441bcdc7 265 m_I2C.write(eeAddr|1);// request read mode
superphil06 0:b939441bcdc7 266
superphil06 0:b939441bcdc7 267
superphil06 0:b939441bcdc7 268 /*if (Wire.available()){
superphil06 0:b939441bcdc7 269 rdata = Wire.read();
superphil06 0:b939441bcdc7 270 }*/
superphil06 0:b939441bcdc7 271 rdata = m_I2C.read(0);// read byte no ack
superphil06 0:b939441bcdc7 272 //Wire.endTransmission(); // end transmission
superphil06 0:b939441bcdc7 273 m_I2C.stop();
superphil06 0:b939441bcdc7 274 return rdata;
superphil06 0:b939441bcdc7 275 }
superphil06 0:b939441bcdc7 276
superphil06 0:b939441bcdc7 277 unsigned int NfcTag::_EEPROM_Read_uint8_ts(unsigned int address, uint8_t* buf, unsigned int len)
superphil06 0:b939441bcdc7 278 {
superphil06 0:b939441bcdc7 279 uint8_t rdata = 0x7F;
superphil06 0:b939441bcdc7 280 /*ire.beginTransmission(eeAddr); //Device Address
superphil06 0:b939441bcdc7 281 Wire.write((int)(address >> 8)); // EEPROM_MSB
superphil06 0:b939441bcdc7 282 Wire.write((int)(address & 0xFF)); // EEPROM_LSB
superphil06 0:b939441bcdc7 283 Wire.endTransmission();*/
superphil06 0:b939441bcdc7 284 (void) m_I2C.start();
superphil06 0:b939441bcdc7 285 m_I2C.write(eeAddr);
superphil06 0:b939441bcdc7 286 m_I2C.write((int)(address >> 8));
superphil06 0:b939441bcdc7 287 m_I2C.write((int)(address & 0xFF));
superphil06 0:b939441bcdc7 288 m_I2C.stop();
superphil06 0:b939441bcdc7 289
superphil06 0:b939441bcdc7 290 /* Wire.beginTransmission(eeAddr);
superphil06 0:b939441bcdc7 291 Wire.requestFrom(eeAddr,len);*/
superphil06 0:b939441bcdc7 292 (void) m_I2C.start();
superphil06 0:b939441bcdc7 293 //Wire.requestFrom(eeAddr,1);
superphil06 0:b939441bcdc7 294 m_I2C.write(eeAddr|1);// request read mode
superphil06 0:b939441bcdc7 295 int i = 0;
superphil06 0:b939441bcdc7 296 // while(Wire.available()){
superphil06 0:b939441bcdc7 297 while(i<len){
superphil06 0:b939441bcdc7 298 // buf[i++] = Wire.read();
superphil06 0:b939441bcdc7 299 buf[i++] = m_I2C.read(1);// read I2C with ack
superphil06 0:b939441bcdc7 300 }
superphil06 0:b939441bcdc7 301 // Wire.endTransmission(); // end transmission
superphil06 0:b939441bcdc7 302 m_I2C.stop();
superphil06 0:b939441bcdc7 303 return i;
superphil06 0:b939441bcdc7 304 }