Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 127 → Rev 128

/PIC Stuff/PIC_27J13/nfc.c
5,6 → 5,7
#include <delays.h>
 
static NFC_DATA nfc_data;
static NFC_DATA *nfc_data_p = &nfc_data;
 
// Const value arrays for comparison use
static char pn532response_firmwarevers[] = {0x01, 0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
23,17 → 24,17
 
// Configures the SAM (Secure Access Module)
unsigned char NFC_SAMConfig() {
nfc_data.packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
nfc_data.packetbuffer[1] = 0x01; // Normal mode
nfc_data.packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
nfc_data.packetbuffer[3] = 0x01; // Use IRQ pin
nfc_data_p->packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
nfc_data_p->packetbuffer[1] = 0x01; // Normal mode
nfc_data_p->packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
nfc_data_p->packetbuffer[3] = 0x01; // Use IRQ pin
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
NFC_I2C_Read_Data(nfc_data.packetbuffer, 8);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 8);
 
return (nfc_data.packetbuffer[7] == 0x15);
return (nfc_data_p->packetbuffer[7] == 0x15);
}
 
// Checks the firmware version of the PN5xx chip
41,23 → 42,23
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
 
// Create and send command
nfc_data.packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
nfc_data_p->packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 1))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 1))
return response;
 
// Read back data from the PN532
NFC_I2C_Read_Data(nfc_data.packetbuffer, 12);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
 
// Compare and check returned values
if (strncmp((char *) nfc_data.packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
if (strncmp((char *) nfc_data_p->packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
return response;
 
// Save and return info
response.IC = nfc_data.packetbuffer[8];
response.Ver = nfc_data.packetbuffer[9];
response.Rev = nfc_data.packetbuffer[10];
response.Support = nfc_data.packetbuffer[11];
response.IC = nfc_data_p->packetbuffer[8];
response.Ver = nfc_data_p->packetbuffer[9];
response.Rev = nfc_data_p->packetbuffer[10];
response.Support = nfc_data_p->packetbuffer[11];
 
return response;
}
90,17 → 91,17
// Passive polling, waits for an ISO14443A target to enter the field
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *cardData) {
nfc_data.packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
nfc_data.packetbuffer[1] = 2; // Max 2 cards at once
nfc_data.packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
nfc_data_p->packetbuffer[1] = 2; // Max 2 cards at once
nfc_data_p->packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 3))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 3))
return 0;
 
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data.packetbuffer, 35);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 35);
 
/* InListPassiveTarget response should be in the following format:
* Byte Description
113,53 → 114,53
*/
 
// Check # of tags found
if (!nfc_data.packetbuffer[8])
if (!nfc_data_p->packetbuffer[8])
return 0;
// Save data from first card
if (nfc_data.packetbuffer[13] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 9);
if (nfc_data_p->packetbuffer[13] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 9);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 12);
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 12);
}
 
// Save data from second card
if (nfc_data.packetbuffer[8] == 2) {
if (nfc_data_p->packetbuffer[8] == 2) {
// Offset will vary depending on length of first card
if (nfc_data.packetbuffer[13] == 4) {
if (nfc_data.packetbuffer[22] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 9);
if (nfc_data_p->packetbuffer[13] == 4) {
if (nfc_data_p->packetbuffer[22] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 12);
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 12);
}
} else { // Length of first UID is 7
if (nfc_data.packetbuffer[25] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 9);
if (nfc_data_p->packetbuffer[25] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 12);
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 12);
}
}
}
// Return the number of cards detected
return nfc_data.packetbuffer[8];
return nfc_data_p->packetbuffer[8];
}
 
// Active polling, returns number of cards in the field
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *cardData) {
nfc_data.packetbuffer[0] = PN532_COMMAND_INAUTOPOLL;
nfc_data.packetbuffer[1] = number; // Number of polling
nfc_data.packetbuffer[2] = period; // Polling period in units of 150ms
nfc_data.packetbuffer[3] = 0x10; // Check for Mifare cards only
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INAUTOPOLL;
nfc_data_p->packetbuffer[1] = number; // Number of polling
nfc_data_p->packetbuffer[2] = period; // Polling period in units of 150ms
nfc_data_p->packetbuffer[3] = 0x10; // Check for Mifare cards only
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data.packetbuffer, 37);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 37);
 
/* InAutoPoll response should be in the following format:
* Byte Description
174,36 → 175,36
*/
 
// Check # of tags found
if (!nfc_data.packetbuffer[8])
if (!nfc_data_p->packetbuffer[8])
return 0;
 
// Save data from first card
if (nfc_data.packetbuffer[15] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 9);
if (nfc_data_p->packetbuffer[15] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 9);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 12);
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 12);
}
 
// Save data from second card
if (nfc_data.packetbuffer[8] == 2) {
if (nfc_data_p->packetbuffer[8] == 2) {
// Offset will vary depending on length of first card
if (nfc_data.packetbuffer[15] == 4) {
if (nfc_data.packetbuffer[26] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 9);
if (nfc_data_p->packetbuffer[15] == 4) {
if (nfc_data_p->packetbuffer[26] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 12);
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 12);
}
} else {
if (nfc_data.packetbuffer[29] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 9);
if (nfc_data_p->packetbuffer[29] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 12);
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 12);
}
}
}
 
// Return the number of cards detected
return nfc_data.packetbuffer[8];
return nfc_data_p->packetbuffer[8];
}
 
// Indicates whether the specified block number is the first block
235,23 → 236,23
unsigned char i;
 
// Assemble frame data
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
nfc_data.packetbuffer[1] = 1; /* Max card numbers */
nfc_data.packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
nfc_data_p->packetbuffer[1] = 1; /* Max card numbers */
nfc_data_p->packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
for (i = 0; i < 6; i++) {
nfc_data.packetbuffer[4 + i] = keyData[i];
nfc_data_p->packetbuffer[4 + i] = keyData[i];
}
for (i = 0; i < uidLen; i++) {
nfc_data.packetbuffer[10 + i] = uid[i];
nfc_data_p->packetbuffer[10 + i] = uid[i];
}
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 10 + uidLen))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 10 + uidLen))
return 0;
 
// Read response from PN532
NFC_I2C_Read_Data(nfc_data.packetbuffer, 12);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
 
return 1;
}
261,20 → 262,20
unsigned char i;
 
// Assemble frame data
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data.packetbuffer[1] = 1; /* Card number */
nfc_data.packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data_p->packetbuffer[1] = 1; /* Card number */
nfc_data_p->packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
// Read reponse
NFC_I2C_Read_Data(nfc_data.packetbuffer, 26);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
 
// If byte 9 isnt 0x00 we probably have and error
if (nfc_data.packetbuffer[8] != 0x00) {
if (nfc_data_p->packetbuffer[8] != 0x00) {
return 0;
}
 
281,7 → 282,7
// Copy the 16 data bytes into the data buffer
// Block contents starts at byte 10 of a valid response
for (i = 0; i < 16; i++) {
data[i] = nfc_data.packetbuffer[9 + i];
data[i] = nfc_data_p->packetbuffer[9 + i];
}
 
return 1;
292,20 → 293,20
unsigned char i;
 
// Assemble frame data
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data.packetbuffer[1] = 1; /* Card number */
nfc_data.packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data_p->packetbuffer[1] = 1; /* Card number */
nfc_data_p->packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
for (i = 0; i < 16; i++) { /* Data Payload */
nfc_data.packetbuffer[4 + i] = data[i];
nfc_data_p->packetbuffer[4 + i] = data[i];
}
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 20))
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 20))
return 0;
 
// Read response
NFC_I2C_Read_Data(nfc_data.packetbuffer, 26);
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
 
return 1;
}