grove nfc_tag sample program
Dependencies: mbed
nfc_tag.h@1:c492babe8dc3, 2017-03-31 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
superphil06 | 0:b939441bcdc7 | 1 | /* |
superphil06 | 0:b939441bcdc7 | 2 | * NfcTag.h |
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 "arduino.h" |
superphil06 | 0:b939441bcdc7 | 33 | //#include <Wire.h> |
superphil06 | 0:b939441bcdc7 | 34 | #include <mbed.h> |
superphil06 | 0:b939441bcdc7 | 35 | |
superphil06 | 0:b939441bcdc7 | 36 | #ifndef __NFC_TAG_H__ |
superphil06 | 0:b939441bcdc7 | 37 | #define __NFC_TAG_H__ |
superphil06 | 0:b939441bcdc7 | 38 | |
superphil06 | 0:b939441bcdc7 | 39 | |
superphil06 | 0:b939441bcdc7 | 40 | #define EEPROM_ADDR_E2_DISABLE 0x53 //disable E2 |
superphil06 | 0:b939441bcdc7 | 41 | #define EEPROM_ADDR_E2_ENABLE (0x53|0x04) //enable E2 |
superphil06 | 0:b939441bcdc7 | 42 | #define EEPROM_I2C_LENGTH 8192 |
superphil06 | 0:b939441bcdc7 | 43 | |
superphil06 | 0:b939441bcdc7 | 44 | #define PASSWORD_LENGTH 4 |
superphil06 | 0:b939441bcdc7 | 45 | #define SECTOR_SECURITY_STATUS_BASE_ADDR 0x800 //2048 |
superphil06 | 0:b939441bcdc7 | 46 | |
superphil06 | 0:b939441bcdc7 | 47 | #define LOCK_PROTECT_BIT BIT0 |
superphil06 | 0:b939441bcdc7 | 48 | #define WRITE_READ_PROTECT_BIT BIT1 |
superphil06 | 0:b939441bcdc7 | 49 | #define PASSWORD_CTRL_BIT BIT3 |
superphil06 | 0:b939441bcdc7 | 50 | |
superphil06 | 0:b939441bcdc7 | 51 | #define I2C_PASSWORD_ADDR 2304 |
superphil06 | 0:b939441bcdc7 | 52 | #define RF_PASSWORD_1_ADDR 2308 |
superphil06 | 0:b939441bcdc7 | 53 | #define RF_PASSWORD_2_ADDR 2312 |
superphil06 | 0:b939441bcdc7 | 54 | #define RF_PASSWORD_3_ADDR 2316 |
superphil06 | 0:b939441bcdc7 | 55 | #define DSFID_ADDR 2320 //1 uint8_t |
superphil06 | 0:b939441bcdc7 | 56 | #define AFI_ADDR 2321 //1 uint8_t |
superphil06 | 0:b939441bcdc7 | 57 | #define RFU_ADDR 2322 //2 uint8_ts |
superphil06 | 0:b939441bcdc7 | 58 | #define UID_ADDR 2324 //8 uint8_ts |
superphil06 | 0:b939441bcdc7 | 59 | #define UID_LENGTH 8 |
superphil06 | 0:b939441bcdc7 | 60 | #define MEMORY_VOLUME_ADDR 2332 //3 uint8_ts |
superphil06 | 0:b939441bcdc7 | 61 | #define IC_NUMBER_ADDR 2335 //1 uint8_t |
superphil06 | 0:b939441bcdc7 | 62 | |
superphil06 | 0:b939441bcdc7 | 63 | |
superphil06 | 0:b939441bcdc7 | 64 | enum AccessMode{ |
superphil06 | 0:b939441bcdc7 | 65 | USER_MODE = 0x0, // offer simple read/write access right |
superphil06 | 0:b939441bcdc7 | 66 | ROOT_MODE = 0x1, // offer password change access right |
superphil06 | 0:b939441bcdc7 | 67 | }; |
superphil06 | 0:b939441bcdc7 | 68 | |
superphil06 | 0:b939441bcdc7 | 69 | enum SectorAccessRight{ |
superphil06 | 0:b939441bcdc7 | 70 | // ********************************** |
superphil06 | 0:b939441bcdc7 | 71 | // * submit passWd * no submit * |
superphil06 | 0:b939441bcdc7 | 72 | //b2,b1 * Read * Write * Read * Write * |
superphil06 | 0:b939441bcdc7 | 73 | // 00 * 1 1 1 0 * |
superphil06 | 0:b939441bcdc7 | 74 | // 01 * 1 1 1 1 * |
superphil06 | 0:b939441bcdc7 | 75 | // 10 * 1 1 0 0 * |
superphil06 | 0:b939441bcdc7 | 76 | // 11 * 0 1 0 0 * |
superphil06 | 0:b939441bcdc7 | 77 | // ********************************** |
superphil06 | 0:b939441bcdc7 | 78 | Access_1110 = 0, |
superphil06 | 0:b939441bcdc7 | 79 | Access_1111 = 1, |
superphil06 | 0:b939441bcdc7 | 80 | Access_1100 = 2, |
superphil06 | 0:b939441bcdc7 | 81 | Access_0111 = 3, |
superphil06 | 0:b939441bcdc7 | 82 | }; |
superphil06 | 0:b939441bcdc7 | 83 | |
superphil06 | 0:b939441bcdc7 | 84 | enum SectorSelectPassWd{ |
superphil06 | 0:b939441bcdc7 | 85 | //00 => no passWd protect |
superphil06 | 0:b939441bcdc7 | 86 | //01 => passWd 1 |
superphil06 | 0:b939441bcdc7 | 87 | //10 => passWd 2 |
superphil06 | 0:b939441bcdc7 | 88 | //11 => passwd 3 |
superphil06 | 0:b939441bcdc7 | 89 | noPassWd = 0, |
superphil06 | 0:b939441bcdc7 | 90 | passWd_1 = 1, |
superphil06 | 0:b939441bcdc7 | 91 | passWd_2 = 2, |
superphil06 | 0:b939441bcdc7 | 92 | passWd_3 = 3, |
superphil06 | 0:b939441bcdc7 | 93 | }; |
superphil06 | 0:b939441bcdc7 | 94 | |
superphil06 | 0:b939441bcdc7 | 95 | class NfcTag |
superphil06 | 0:b939441bcdc7 | 96 | { |
superphil06 | 0:b939441bcdc7 | 97 | public: |
superphil06 | 0:b939441bcdc7 | 98 | /* NfcTag(AccessMode mode = USER_MODE){ |
superphil06 | 0:b939441bcdc7 | 99 | if(mode == USER_MODE){ |
superphil06 | 0:b939441bcdc7 | 100 | eeAddr = EEPROM_ADDR_E2_DISABLE; |
superphil06 | 0:b939441bcdc7 | 101 | }else{ |
superphil06 | 0:b939441bcdc7 | 102 | eeAddr = EEPROM_ADDR_E2_ENABLE; |
superphil06 | 0:b939441bcdc7 | 103 | } |
superphil06 | 0:b939441bcdc7 | 104 | };*/ |
superphil06 | 0:b939441bcdc7 | 105 | // constructor |
superphil06 | 0:b939441bcdc7 | 106 | NfcTag(PinName sda,PinName scl,uint8_t address,AccessMode mode); |
superphil06 | 0:b939441bcdc7 | 107 | ~NfcTag(); |
superphil06 | 0:b939441bcdc7 | 108 | |
superphil06 | 0:b939441bcdc7 | 109 | // void init(); |
superphil06 | 0:b939441bcdc7 | 110 | void submitPassWd(uint8_t* passWd); |
superphil06 | 0:b939441bcdc7 | 111 | void writePassWd(uint8_t* passWd); |
superphil06 | 0:b939441bcdc7 | 112 | void sectorProtectConfig(unsigned int sectorNumber, bool protectEnable, SectorAccessRight accessRight = Access_1110, SectorSelectPassWd passWd = noPassWd); |
superphil06 | 0:b939441bcdc7 | 113 | void clearSectorProtect(void); |
superphil06 | 0:b939441bcdc7 | 114 | void sectorWriteSockConfig(unsigned int sectorNumber, bool sockEnable); |
superphil06 | 0:b939441bcdc7 | 115 | uint8_t getDSFID(); |
superphil06 | 0:b939441bcdc7 | 116 | uint8_t getAFI(); |
superphil06 | 0:b939441bcdc7 | 117 | uint16_t getRFU(); |
superphil06 | 0:b939441bcdc7 | 118 | uint8_t getUID(uint8_t* buf); |
superphil06 | 0:b939441bcdc7 | 119 | uint32_t getMemoryVolume(); |
superphil06 | 0:b939441bcdc7 | 120 | uint8_t getICNumber(); |
superphil06 | 0:b939441bcdc7 | 121 | void clearMemory(); |
superphil06 | 0:b939441bcdc7 | 122 | void writeuint8_t(unsigned int address, uint8_t data); |
superphil06 | 0:b939441bcdc7 | 123 | void writeuint8_ts(unsigned int address, uint8_t* buf, unsigned int len); |
superphil06 | 0:b939441bcdc7 | 124 | uint8_t readuint8_t(unsigned int address); |
superphil06 | 0:b939441bcdc7 | 125 | void readuint8_ts(unsigned int address, uint8_t* buf, unsigned int len); |
superphil06 | 0:b939441bcdc7 | 126 | |
superphil06 | 0:b939441bcdc7 | 127 | private: |
superphil06 | 0:b939441bcdc7 | 128 | I2C m_I2C; |
superphil06 | 0:b939441bcdc7 | 129 | int eeAddr; |
superphil06 | 0:b939441bcdc7 | 130 | void _EEPROM_Write_uint8_t(unsigned int address, uint8_t data); |
superphil06 | 0:b939441bcdc7 | 131 | void _EEPROM_Write_uint8_ts(unsigned int address, uint8_t* buf, unsigned int len); |
superphil06 | 0:b939441bcdc7 | 132 | uint8_t _EEPROM_Read_uint8_t(unsigned int address); |
superphil06 | 0:b939441bcdc7 | 133 | unsigned int _EEPROM_Read_uint8_ts(unsigned int address, uint8_t* buf, unsigned int len); |
superphil06 | 0:b939441bcdc7 | 134 | uint8_t _address; // I2C Module Address |
superphil06 | 0:b939441bcdc7 | 135 | }; |
superphil06 | 0:b939441bcdc7 | 136 | #endif |