Basic Read and Writer implementation for an OEM Mifare RWD Device.

Dependencies:   mbed

Committer:
seblovett
Date:
Sat Jun 29 18:57:07 2013 +0000
Revision:
4:952267f44d02
Parent:
3:4af68a2b8622
v1.01 minor changes, mainly added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seblovett 0:340078fc5139 1 /*
seblovett 0:340078fc5139 2 Copyright (c) 2010 ARM Limited
seblovett 0:340078fc5139 3
seblovett 0:340078fc5139 4 Permission is hereby granted, free of charge, to any person obtaining a copy
seblovett 0:340078fc5139 5 of this software and associated documentation files (the "Software"), to deal
seblovett 0:340078fc5139 6 in the Software without restriction, including without limitation the rights
seblovett 0:340078fc5139 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
seblovett 0:340078fc5139 8 copies of the Software, and to permit persons to whom the Software is
seblovett 0:340078fc5139 9 furnished to do so, subject to the following conditions:
seblovett 0:340078fc5139 10
seblovett 0:340078fc5139 11 The above copyright notice and this permission notice shall be included in
seblovett 0:340078fc5139 12 all copies or substantial portions of the Software.
seblovett 0:340078fc5139 13
seblovett 0:340078fc5139 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
seblovett 0:340078fc5139 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
seblovett 0:340078fc5139 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
seblovett 0:340078fc5139 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
seblovett 0:340078fc5139 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
seblovett 0:340078fc5139 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
seblovett 0:340078fc5139 20 THE SOFTWARE.
seblovett 0:340078fc5139 21 */
seblovett 0:340078fc5139 22
seblovett 0:340078fc5139 23 #include "RWDMifare.h"
seblovett 0:340078fc5139 24
seblovett 0:340078fc5139 25 RWDMifare::RWDMifare(PinName tx, PinName rx, PinName cts) : RWDModule(tx, rx, cts)
seblovett 0:340078fc5139 26 {
seblovett 0:340078fc5139 27
seblovett 0:340078fc5139 28 }
seblovett 0:340078fc5139 29
seblovett 2:0f9d7a3d13a4 30
seblovett 0:340078fc5139 31 RWDMifare::~RWDMifare()
seblovett 0:340078fc5139 32 {
seblovett 0:340078fc5139 33
seblovett 0:340078fc5139 34 }
seblovett 1:c5be8c1c9e10 35 RWDMifare::RWDMifareErr RWDMifare::init()
seblovett 1:c5be8c1c9e10 36 {
seblovett 1:c5be8c1c9e10 37 const uint8_t config[] = {0x03, 0x00}; //Configure ready for Mifare Op (0x00 to addr 0x03 in EEPROM)
seblovett 1:c5be8c1c9e10 38 uint8_t resp[64] = {0};
seblovett 1:c5be8c1c9e10 39 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 1:c5be8c1c9e10 40 //Command 'P': cf p11
seblovett 1:c5be8c1c9e10 41 //EEPROM parameters: cf p12
seblovett 1:c5be8c1c9e10 42 //Ack check: cf p13
seblovett 1:c5be8c1c9e10 43 command(CMD_Prog_EEPROM, config, 2, resp, 0, 0x80, 0x89); //Program EEPROM command is 0x50 ('P')
seblovett 1:c5be8c1c9e10 44 while(!ready()) { //Wait for a response
seblovett 1:c5be8c1c9e10 45
seblovett 1:c5be8c1c9e10 46 }
seblovett 1:c5be8c1c9e10 47
seblovett 1:c5be8c1c9e10 48 if(!result()) { //If this fails, there is something wrong with the hardware
seblovett 1:c5be8c1c9e10 49 return MIFARE_HW;
seblovett 1:c5be8c1c9e10 50 }
seblovett 1:c5be8c1c9e10 51
seblovett 1:c5be8c1c9e10 52 return MIFARE_OK;
seblovett 1:c5be8c1c9e10 53 }
seblovett 0:340078fc5139 54 RWDMifare::RWDMifareErr RWDMifare::Prog_EEPROM(uint8_t Address, uint8_t Value)
seblovett 0:340078fc5139 55 {
seblovett 0:340078fc5139 56 const uint8_t config[] = {Address, Value}; //Configure ready for Mifare Op (0x00 to addr 0x03 in EEPROM)
seblovett 0:340078fc5139 57 uint8_t resp[64] = {0};
seblovett 0:340078fc5139 58 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 0:340078fc5139 59 //Command 'P': cf p11
seblovett 0:340078fc5139 60 //EEPROM parameters: cf p12
seblovett 0:340078fc5139 61 //Ack check: cf p13
seblovett 1:c5be8c1c9e10 62 command(CMD_Prog_EEPROM, config, 2, resp, 0, 0x80, 0x81); //Program EEPROM command is 0x50 ('P')
seblovett 0:340078fc5139 63 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 64
seblovett 0:340078fc5139 65 }
seblovett 0:340078fc5139 66
seblovett 0:340078fc5139 67 if(!result()) { //If this fails, there is something wrong with the hardware
seblovett 0:340078fc5139 68 return MIFARE_HW;
seblovett 0:340078fc5139 69 }
seblovett 0:340078fc5139 70
seblovett 0:340078fc5139 71 return MIFARE_OK;
seblovett 0:340078fc5139 72
seblovett 0:340078fc5139 73 }
seblovett 3:4af68a2b8622 74 RWDMifare::RWDMifareErr RWDMifare::printStatus(uint8_t Status)
seblovett 3:4af68a2b8622 75 {
seblovett 3:4af68a2b8622 76 printf("\n\rEEPROM Error\t= %d", (Status & Ack_Code_EEPROM_ERR));
seblovett 3:4af68a2b8622 77 printf("\n\rCard Okay \t= %d", (Status & Ack_Code_Card_OK)>>1);
seblovett 3:4af68a2b8622 78 printf("\n\rRX Okay \t= %d", (Status & Ack_Code_RX_OK)>>2);
seblovett 3:4af68a2b8622 79 printf("\n\rRS232 Error \t= %d", (Status & Ack_Code_RS232_ERR)>>3);
seblovett 3:4af68a2b8622 80 printf("\n\rMF Type \t= %d", (Status & Ack_Code_MF_Type)>>4);
seblovett 3:4af68a2b8622 81 printf("\n\rUL Type \t= %d", (Status & Ack_Code_UL_Type)>>5);
seblovett 3:4af68a2b8622 82 printf("\n\rMFRC Error \t= %d", (Status & Ack_Code_MRRFC_Err)>>6);
seblovett 3:4af68a2b8622 83 return MIFARE_OK;
seblovett 3:4af68a2b8622 84 }
seblovett 0:340078fc5139 85 RWDMifare::RWDMifareErr RWDMifare::getStatus(uint8_t* Status)
seblovett 0:340078fc5139 86 {
seblovett 0:340078fc5139 87 //Gets the status code of the Mifare
seblovett 0:340078fc5139 88 command(CMD_Get_Status, NULL, 0, Status, 1, 0x86,0x86);
seblovett 3:4af68a2b8622 89 printf("\n\rStatus 0x%02x", *Status);
seblovett 0:340078fc5139 90
seblovett 0:340078fc5139 91 return MIFARE_OK;
seblovett 0:340078fc5139 92 }
seblovett 0:340078fc5139 93 RWDMifare::RWDMifareErr RWDMifare::getUID(uint8_t* pUID, size_t* pLen) //pUID must be at least 10-bytes long
seblovett 0:340078fc5139 94 //(A Mifare UID can either be 4, 7, or 10 bytes long)
seblovett 0:340078fc5139 95 //This reader does not support 10 bytes uids
seblovett 0:340078fc5139 96 {
seblovett 0:340078fc5139 97 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 0:340078fc5139 98 //Command 'U': cf p19
seblovett 0:340078fc5139 99 //Ack check: cf p13
seblovett 1:c5be8c1c9e10 100 command(CMD_Get_UID, NULL, 0, pUID, 7, 0x86, 0x86); //UID command is 0x55 ('U')
seblovett 0:340078fc5139 101 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 102
seblovett 0:340078fc5139 103 }
seblovett 0:340078fc5139 104
seblovett 0:340078fc5139 105 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 106 return MIFARE_NOCARD;
seblovett 0:340078fc5139 107 }
seblovett 0:340078fc5139 108
seblovett 0:340078fc5139 109 //printf("Got card.\n\r");
seblovett 0:340078fc5139 110
seblovett 0:340078fc5139 111 //Checks UID length returned by reader
seblovett 0:340078fc5139 112 int i;
seblovett 0:340078fc5139 113 for(i = 0; i < 7; i++) {
seblovett 0:340078fc5139 114 if(pUID[i] == 0) //End of UID, cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf p19
seblovett 0:340078fc5139 115 break;
seblovett 0:340078fc5139 116 }
seblovett 0:340078fc5139 117 *pLen = i;
seblovett 0:340078fc5139 118
seblovett 0:340078fc5139 119 return MIFARE_OK;
seblovett 0:340078fc5139 120 }
seblovett 0:340078fc5139 121
seblovett 1:c5be8c1c9e10 122 RWDMifare::RWDMifareErr RWDMifare::StoreKey(uint8_t KeyNumber, uint8_t* Key)
seblovett 0:340078fc5139 123 {
seblovett 1:c5be8c1c9e10 124 uint8_t cmd[7] = {0};
seblovett 1:c5be8c1c9e10 125 //command[0] = CMD_Store_Key;
seblovett 1:c5be8c1c9e10 126 cmd[0] = KeyNumber & 31; //mask it.
seblovett 0:340078fc5139 127 for(int i = 0; i < 6; i++)
seblovett 0:340078fc5139 128 {
seblovett 1:c5be8c1c9e10 129 cmd[i+1] = Key[i];
seblovett 0:340078fc5139 130 }
seblovett 1:c5be8c1c9e10 131 /* printf("Command:\n");
seblovett 1:c5be8c1c9e10 132 for(int i = 0; i < 8; i ++){
seblovett 1:c5be8c1c9e10 133 printf("0x%02x ", command[i]);
seblovett 1:c5be8c1c9e10 134 }*/
seblovett 1:c5be8c1c9e10 135 command(CMD_Store_Key, cmd, 7, NULL, 0, 0x80, 0x89);
seblovett 1:c5be8c1c9e10 136 while(!ready()) { //Wait for a response
seblovett 1:c5be8c1c9e10 137
seblovett 1:c5be8c1c9e10 138 }
seblovett 1:c5be8c1c9e10 139
seblovett 1:c5be8c1c9e10 140 if(!result()) { //Error detected, there is no card in field
seblovett 1:c5be8c1c9e10 141 return MIFARE_NOCARD;
seblovett 1:c5be8c1c9e10 142 }
seblovett 1:c5be8c1c9e10 143
seblovett 1:c5be8c1c9e10 144 return MIFARE_OK;
seblovett 1:c5be8c1c9e10 145 }
seblovett 0:340078fc5139 146 RWDMifare::RWDMifareErr RWDMifare::AuthAllCards()
seblovett 0:340078fc5139 147 {
seblovett 0:340078fc5139 148 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 149 return MIFARE_HW;
seblovett 0:340078fc5139 150 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 151 return MIFARE_HW;
seblovett 0:340078fc5139 152 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 153 return MIFARE_HW;
seblovett 0:340078fc5139 154 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 155 return MIFARE_HW;
seblovett 1:c5be8c1c9e10 156 return MIFARE_OK;
seblovett 0:340078fc5139 157 }
seblovett 3:4af68a2b8622 158
seblovett 3:4af68a2b8622 159 RWDMifare::RWDMifareErr RWDMifare::ReadBlock(uint8_t Addr, uint8_t KeyNumber_Type, uint8_t* Data)
seblovett 0:340078fc5139 160 {
seblovett 3:4af68a2b8622 161 uint8_t cmd[2] = {Addr, KeyNumber_Type};
seblovett 0:340078fc5139 162 command(CMD_Read_Block, cmd, 2, Data, 16, 0x96, 0xFE);
seblovett 0:340078fc5139 163 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 164
seblovett 0:340078fc5139 165 }
seblovett 0:340078fc5139 166
seblovett 0:340078fc5139 167 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 168 return MIFARE_NOCARD;
seblovett 0:340078fc5139 169 }
seblovett 0:340078fc5139 170
seblovett 3:4af68a2b8622 171 return MIFARE_OK;
seblovett 0:340078fc5139 172
seblovett 3:4af68a2b8622 173 }
seblovett 3:4af68a2b8622 174 RWDMifare::RWDMifareErr RWDMifare::ReadBlock(uint8_t Addr, uint8_t KeyNumber, uint8_t Type, uint8_t* Data)
seblovett 3:4af68a2b8622 175 {
seblovett 3:4af68a2b8622 176 KeyNumber &= 0x1F;
seblovett 3:4af68a2b8622 177 if(Type)
seblovett 3:4af68a2b8622 178 KeyNumber |= 0x80;
seblovett 3:4af68a2b8622 179 return ReadBlock(Addr, KeyNumber, Data);
seblovett 3:4af68a2b8622 180
seblovett 1:c5be8c1c9e10 181 }
seblovett 1:c5be8c1c9e10 182 RWDMifare::RWDMifareErr RWDMifare::WriteBlock(uint8_t Addr, uint8_t KeyNumber, uint8_t Type, uint8_t* Data)
seblovett 1:c5be8c1c9e10 183 {
seblovett 3:4af68a2b8622 184 //CARE MUST BE TAKEN WHEN WRITING TO A SECTION TRAILER.
seblovett 3:4af68a2b8622 185 //THIS METHOD WILL NOT WRITE TO A SECTION TRAILER, ONLY USER DATA
seblovett 3:4af68a2b8622 186 if(Addr < 32)
seblovett 3:4af68a2b8622 187 {
seblovett 3:4af68a2b8622 188 if ((Addr & 0x3) == 0x3) // section trailer occurs on blocks 3, 7, 11, 15 . . .
seblovett 3:4af68a2b8622 189 return MIFARE_TRAIL;
seblovett 3:4af68a2b8622 190 }
seblovett 3:4af68a2b8622 191 else //only applicable for 4k cards
seblovett 3:4af68a2b8622 192 {
seblovett 3:4af68a2b8622 193 if ((Addr & 0xF) == 0xF) //for sectors 32 - 39, trailer occurs in block 15.
seblovett 4:952267f44d02 194 return MIFARE_TRAIL;
seblovett 3:4af68a2b8622 195 }
seblovett 3:4af68a2b8622 196
seblovett 3:4af68a2b8622 197 //get here, writing to a user field.
seblovett 1:c5be8c1c9e10 198 KeyNumber &= 0x1F;
seblovett 1:c5be8c1c9e10 199 if(Type)
seblovett 1:c5be8c1c9e10 200 KeyNumber |= 0x80;
seblovett 1:c5be8c1c9e10 201
seblovett 1:c5be8c1c9e10 202 uint8_t cmd[18];
seblovett 1:c5be8c1c9e10 203 cmd[0] = Addr;
seblovett 1:c5be8c1c9e10 204 cmd[1] = KeyNumber;
seblovett 2:0f9d7a3d13a4 205 for(int i = 0; i < 16; i ++)
seblovett 1:c5be8c1c9e10 206 {
seblovett 1:c5be8c1c9e10 207 cmd[i+2] = Data[i];
seblovett 1:c5be8c1c9e10 208 }
seblovett 1:c5be8c1c9e10 209 printf("Command to write:\n");
seblovett 1:c5be8c1c9e10 210 for(int i = 0; i < 18;i++){
seblovett 1:c5be8c1c9e10 211 printf("0x%02x ", cmd[i]);
seblovett 1:c5be8c1c9e10 212 }
seblovett 2:0f9d7a3d13a4 213 printf("\r\n");
seblovett 1:c5be8c1c9e10 214 command(CMD_Write_Block, cmd, 18, NULL, 0, 0x96, 0xFE);
seblovett 1:c5be8c1c9e10 215 while(!ready()) { //Wait for a response
seblovett 1:c5be8c1c9e10 216
seblovett 1:c5be8c1c9e10 217 }
seblovett 1:c5be8c1c9e10 218
seblovett 1:c5be8c1c9e10 219 if(!result()) { //Error detected, there is no card in field
seblovett 1:c5be8c1c9e10 220 return MIFARE_NOCARD;
seblovett 1:c5be8c1c9e10 221 }
seblovett 1:c5be8c1c9e10 222 return MIFARE_OK;
seblovett 0:340078fc5139 223 }