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

Dependencies:   mbed

Committer:
seblovett
Date:
Wed Jun 26 19:09:58 2013 +0000
Revision:
0:340078fc5139
Child:
1:c5be8c1c9e10
Got read, get uid and write to  EEPROM working!

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 0:340078fc5139 30 RWDMifare::~RWDMifare()
seblovett 0:340078fc5139 31 {
seblovett 0:340078fc5139 32
seblovett 0:340078fc5139 33 }
seblovett 0:340078fc5139 34 RWDMifare::RWDMifareErr RWDMifare::Prog_EEPROM(uint8_t Address, uint8_t Value)
seblovett 0:340078fc5139 35 {
seblovett 0:340078fc5139 36 const uint8_t config[] = {Address, Value}; //Configure ready for Mifare Op (0x00 to addr 0x03 in EEPROM)
seblovett 0:340078fc5139 37 uint8_t resp[64] = {0};
seblovett 0:340078fc5139 38 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 0:340078fc5139 39 //Command 'P': cf p11
seblovett 0:340078fc5139 40 //EEPROM parameters: cf p12
seblovett 0:340078fc5139 41 //Ack check: cf p13
seblovett 0:340078fc5139 42 command(0x50, config, 2, resp, 0, 0x80, 0x81); //Program EEPROM command is 0x50 ('P')
seblovett 0:340078fc5139 43 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 44
seblovett 0:340078fc5139 45 }
seblovett 0:340078fc5139 46
seblovett 0:340078fc5139 47 if(!result()) { //If this fails, there is something wrong with the hardware
seblovett 0:340078fc5139 48 return MIFARE_HW;
seblovett 0:340078fc5139 49 }
seblovett 0:340078fc5139 50
seblovett 0:340078fc5139 51 return MIFARE_OK;
seblovett 0:340078fc5139 52
seblovett 0:340078fc5139 53 }
seblovett 0:340078fc5139 54 RWDMifare::RWDMifareErr RWDMifare::init()
seblovett 0:340078fc5139 55 {
seblovett 0:340078fc5139 56 const uint8_t config[] = {0x03, 0x00}; //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 0:340078fc5139 62 command(0x50, config, 2, resp, 0, 0x80, 0x89); //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 RWDMifare::RWDMifareErr RWDMifare::getStatus(uint8_t* Status)
seblovett 0:340078fc5139 74 {
seblovett 0:340078fc5139 75 //Gets the status code of the Mifare
seblovett 0:340078fc5139 76 command(CMD_Get_Status, NULL, 0, Status, 1, 0x86,0x86);
seblovett 0:340078fc5139 77 printf("Got Status %x\n\r", *Status);
seblovett 0:340078fc5139 78
seblovett 0:340078fc5139 79 return MIFARE_OK;
seblovett 0:340078fc5139 80 }
seblovett 0:340078fc5139 81 RWDMifare::RWDMifareErr RWDMifare::getUID(uint8_t* pUID, size_t* pLen) //pUID must be at least 10-bytes long
seblovett 0:340078fc5139 82 //(A Mifare UID can either be 4, 7, or 10 bytes long)
seblovett 0:340078fc5139 83 //This reader does not support 10 bytes uids
seblovett 0:340078fc5139 84 {
seblovett 0:340078fc5139 85 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 0:340078fc5139 86 //Command 'U': cf p19
seblovett 0:340078fc5139 87 //Ack check: cf p13
seblovett 0:340078fc5139 88 command(0x55, NULL, 0, pUID, 7, 0x86, 0x86); //UID command is 0x55 ('U')
seblovett 0:340078fc5139 89 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 90
seblovett 0:340078fc5139 91 }
seblovett 0:340078fc5139 92
seblovett 0:340078fc5139 93 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 94 return MIFARE_NOCARD;
seblovett 0:340078fc5139 95 }
seblovett 0:340078fc5139 96
seblovett 0:340078fc5139 97 //printf("Got card.\n\r");
seblovett 0:340078fc5139 98
seblovett 0:340078fc5139 99 //Checks UID length returned by reader
seblovett 0:340078fc5139 100 int i;
seblovett 0:340078fc5139 101 for(i = 0; i < 7; i++) {
seblovett 0:340078fc5139 102 if(pUID[i] == 0) //End of UID, cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf p19
seblovett 0:340078fc5139 103 break;
seblovett 0:340078fc5139 104 }
seblovett 0:340078fc5139 105 *pLen = i;
seblovett 0:340078fc5139 106
seblovett 0:340078fc5139 107 return MIFARE_OK;
seblovett 0:340078fc5139 108 }
seblovett 0:340078fc5139 109
seblovett 0:340078fc5139 110 /*DMifare::RWDMifareErr RWDMifare::StoreKey(uint8_t* KeyNumber, uint8_t* Key)
seblovett 0:340078fc5139 111 {
seblovett 0:340078fc5139 112 uint8_t command[8] = {0};
seblovett 0:340078fc5139 113 command[0] = CMD_Store_Key;
seblovett 0:340078fc5139 114 command[1] = *KeyNumber & 31; //mask it.
seblovett 0:340078fc5139 115 for(int i = 0; i < 6; i++)
seblovett 0:340078fc5139 116 {
seblovett 0:340078fc5139 117 command[i+2] = Key[i];
seblovett 0:340078fc5139 118 }
seblovett 0:340078fc5139 119
seblovett 0:340078fc5139 120 }*/
seblovett 0:340078fc5139 121 RWDMifare::RWDMifareErr RWDMifare::AuthAllCards()
seblovett 0:340078fc5139 122 {
seblovett 0:340078fc5139 123 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 124 return MIFARE_HW;
seblovett 0:340078fc5139 125 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 126 return MIFARE_HW;
seblovett 0:340078fc5139 127 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 128 return MIFARE_HW;
seblovett 0:340078fc5139 129 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 130 return MIFARE_HW;
seblovett 0:340078fc5139 131 return MIFARE_OK;
seblovett 0:340078fc5139 132
seblovett 0:340078fc5139 133 }
seblovett 0:340078fc5139 134 RWDMifare::RWDMifareErr RWDMifare::ReadBlock(uint8_t Addr, uint8_t KeyNumber, uint8_t Type, uint8_t* Data)
seblovett 0:340078fc5139 135 {
seblovett 0:340078fc5139 136 KeyNumber &= 0x1F;
seblovett 0:340078fc5139 137 if(Type)
seblovett 0:340078fc5139 138 KeyNumber |= 0x80;
seblovett 0:340078fc5139 139
seblovett 0:340078fc5139 140 uint8_t cmd[2] = {Addr, KeyNumber};
seblovett 0:340078fc5139 141 command(CMD_Read_Block, cmd, 2, Data, 16, 0x96, 0xFE);
seblovett 0:340078fc5139 142 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 143
seblovett 0:340078fc5139 144 }
seblovett 0:340078fc5139 145
seblovett 0:340078fc5139 146 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 147 return MIFARE_NOCARD;
seblovett 0:340078fc5139 148 }
seblovett 0:340078fc5139 149
seblovett 0:340078fc5139 150 //printf("Got card.\n\r");
seblovett 0:340078fc5139 151
seblovett 0:340078fc5139 152
seblovett 0:340078fc5139 153 return MIFARE_OK;
seblovett 0:340078fc5139 154 }