Dependencies:   mbed

Committer:
seblovett
Date:
Thu Jun 27 21:30:01 2013 +0000
Revision:
2:0f9d7a3d13a4
Parent:
1:c5be8c1c9e10
Child:
3:4af68a2b8622
got all basic functions working. Need to start implementing a good UI

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 0:340078fc5139 74
seblovett 0:340078fc5139 75 RWDMifare::RWDMifareErr RWDMifare::getStatus(uint8_t* Status)
seblovett 0:340078fc5139 76 {
seblovett 0:340078fc5139 77 //Gets the status code of the Mifare
seblovett 0:340078fc5139 78 command(CMD_Get_Status, NULL, 0, Status, 1, 0x86,0x86);
seblovett 0:340078fc5139 79 printf("Got Status %x\n\r", *Status);
seblovett 0:340078fc5139 80
seblovett 0:340078fc5139 81 return MIFARE_OK;
seblovett 0:340078fc5139 82 }
seblovett 0:340078fc5139 83 RWDMifare::RWDMifareErr RWDMifare::getUID(uint8_t* pUID, size_t* pLen) //pUID must be at least 10-bytes long
seblovett 0:340078fc5139 84 //(A Mifare UID can either be 4, 7, or 10 bytes long)
seblovett 0:340078fc5139 85 //This reader does not support 10 bytes uids
seblovett 0:340078fc5139 86 {
seblovett 0:340078fc5139 87 //Cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
seblovett 0:340078fc5139 88 //Command 'U': cf p19
seblovett 0:340078fc5139 89 //Ack check: cf p13
seblovett 1:c5be8c1c9e10 90 command(CMD_Get_UID, NULL, 0, pUID, 7, 0x86, 0x86); //UID command is 0x55 ('U')
seblovett 0:340078fc5139 91 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 92
seblovett 0:340078fc5139 93 }
seblovett 0:340078fc5139 94
seblovett 0:340078fc5139 95 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 96 return MIFARE_NOCARD;
seblovett 0:340078fc5139 97 }
seblovett 0:340078fc5139 98
seblovett 0:340078fc5139 99 //printf("Got card.\n\r");
seblovett 0:340078fc5139 100
seblovett 0:340078fc5139 101 //Checks UID length returned by reader
seblovett 0:340078fc5139 102 int i;
seblovett 0:340078fc5139 103 for(i = 0; i < 7; i++) {
seblovett 0:340078fc5139 104 if(pUID[i] == 0) //End of UID, cf http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf p19
seblovett 0:340078fc5139 105 break;
seblovett 0:340078fc5139 106 }
seblovett 0:340078fc5139 107 *pLen = i;
seblovett 0:340078fc5139 108
seblovett 0:340078fc5139 109 return MIFARE_OK;
seblovett 0:340078fc5139 110 }
seblovett 0:340078fc5139 111
seblovett 1:c5be8c1c9e10 112 RWDMifare::RWDMifareErr RWDMifare::StoreKey(uint8_t KeyNumber, uint8_t* Key)
seblovett 0:340078fc5139 113 {
seblovett 1:c5be8c1c9e10 114 uint8_t cmd[7] = {0};
seblovett 1:c5be8c1c9e10 115 //command[0] = CMD_Store_Key;
seblovett 1:c5be8c1c9e10 116 cmd[0] = KeyNumber & 31; //mask it.
seblovett 0:340078fc5139 117 for(int i = 0; i < 6; i++)
seblovett 0:340078fc5139 118 {
seblovett 1:c5be8c1c9e10 119 cmd[i+1] = Key[i];
seblovett 0:340078fc5139 120 }
seblovett 1:c5be8c1c9e10 121 /* printf("Command:\n");
seblovett 1:c5be8c1c9e10 122 for(int i = 0; i < 8; i ++){
seblovett 1:c5be8c1c9e10 123 printf("0x%02x ", command[i]);
seblovett 1:c5be8c1c9e10 124 }*/
seblovett 1:c5be8c1c9e10 125 command(CMD_Store_Key, cmd, 7, NULL, 0, 0x80, 0x89);
seblovett 1:c5be8c1c9e10 126 while(!ready()) { //Wait for a response
seblovett 1:c5be8c1c9e10 127
seblovett 1:c5be8c1c9e10 128 }
seblovett 1:c5be8c1c9e10 129
seblovett 1:c5be8c1c9e10 130 if(!result()) { //Error detected, there is no card in field
seblovett 1:c5be8c1c9e10 131 return MIFARE_NOCARD;
seblovett 1:c5be8c1c9e10 132 }
seblovett 1:c5be8c1c9e10 133
seblovett 1:c5be8c1c9e10 134 return MIFARE_OK;
seblovett 1:c5be8c1c9e10 135 }
seblovett 0:340078fc5139 136 RWDMifare::RWDMifareErr RWDMifare::AuthAllCards()
seblovett 0:340078fc5139 137 {
seblovett 0:340078fc5139 138 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 139 return MIFARE_HW;
seblovett 0:340078fc5139 140 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 141 return MIFARE_HW;
seblovett 0:340078fc5139 142 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 143 return MIFARE_HW;
seblovett 0:340078fc5139 144 if (Prog_EEPROM(0x0C, 0xFF))
seblovett 0:340078fc5139 145 return MIFARE_HW;
seblovett 1:c5be8c1c9e10 146 return MIFARE_OK;
seblovett 0:340078fc5139 147 }
seblovett 0:340078fc5139 148 RWDMifare::RWDMifareErr RWDMifare::ReadBlock(uint8_t Addr, uint8_t KeyNumber, uint8_t Type, uint8_t* Data)
seblovett 0:340078fc5139 149 {
seblovett 0:340078fc5139 150 KeyNumber &= 0x1F;
seblovett 0:340078fc5139 151 if(Type)
seblovett 0:340078fc5139 152 KeyNumber |= 0x80;
seblovett 0:340078fc5139 153
seblovett 0:340078fc5139 154 uint8_t cmd[2] = {Addr, KeyNumber};
seblovett 0:340078fc5139 155 command(CMD_Read_Block, cmd, 2, Data, 16, 0x96, 0xFE);
seblovett 0:340078fc5139 156 while(!ready()) { //Wait for a response
seblovett 0:340078fc5139 157
seblovett 0:340078fc5139 158 }
seblovett 0:340078fc5139 159
seblovett 0:340078fc5139 160 if(!result()) { //Error detected, there is no card in field
seblovett 0:340078fc5139 161 return MIFARE_NOCARD;
seblovett 0:340078fc5139 162 }
seblovett 0:340078fc5139 163
seblovett 0:340078fc5139 164 //printf("Got card.\n\r");
seblovett 0:340078fc5139 165
seblovett 0:340078fc5139 166
seblovett 0:340078fc5139 167 return MIFARE_OK;
seblovett 1:c5be8c1c9e10 168 }
seblovett 1:c5be8c1c9e10 169 RWDMifare::RWDMifareErr RWDMifare::WriteBlock(uint8_t Addr, uint8_t KeyNumber, uint8_t Type, uint8_t* Data)
seblovett 1:c5be8c1c9e10 170 {
seblovett 1:c5be8c1c9e10 171 KeyNumber &= 0x1F;
seblovett 1:c5be8c1c9e10 172 if(Type)
seblovett 1:c5be8c1c9e10 173 KeyNumber |= 0x80;
seblovett 1:c5be8c1c9e10 174
seblovett 1:c5be8c1c9e10 175 uint8_t cmd[18];
seblovett 1:c5be8c1c9e10 176 cmd[0] = Addr;
seblovett 1:c5be8c1c9e10 177 cmd[1] = KeyNumber;
seblovett 2:0f9d7a3d13a4 178 for(int i = 0; i < 16; i ++)
seblovett 1:c5be8c1c9e10 179 {
seblovett 1:c5be8c1c9e10 180 cmd[i+2] = Data[i];
seblovett 1:c5be8c1c9e10 181 }
seblovett 1:c5be8c1c9e10 182 printf("Command to write:\n");
seblovett 1:c5be8c1c9e10 183 for(int i = 0; i < 18;i++){
seblovett 1:c5be8c1c9e10 184 printf("0x%02x ", cmd[i]);
seblovett 1:c5be8c1c9e10 185 }
seblovett 2:0f9d7a3d13a4 186 printf("\r\n");
seblovett 1:c5be8c1c9e10 187 command(CMD_Write_Block, cmd, 18, NULL, 0, 0x96, 0xFE);
seblovett 1:c5be8c1c9e10 188 while(!ready()) { //Wait for a response
seblovett 1:c5be8c1c9e10 189
seblovett 1:c5be8c1c9e10 190 }
seblovett 1:c5be8c1c9e10 191
seblovett 1:c5be8c1c9e10 192 if(!result()) { //Error detected, there is no card in field
seblovett 1:c5be8c1c9e10 193 return MIFARE_NOCARD;
seblovett 1:c5be8c1c9e10 194 }
seblovett 1:c5be8c1c9e10 195 return MIFARE_OK;
seblovett 0:340078fc5139 196 }