Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 123 → Rev 126

/PIC Stuff/PIC_27J13/adc.c
12,7 → 12,7
adc_data.result = 0;
 
ADCON0bits.VCFG1 = 0; // VRef- = AVss
ADCON0bits.VCFG0 = 0; // VRef+ = AVdd
ADCON0bits.VCFG0 = 1; // VRef+ != AVdd
ADCON1bits.ADFM = 1; // Right justified result
ADCON1bits.ADCAL = 1; // Calibrate A/D
ADCON1bits.ACQT = TAD;
/PIC Stuff/PIC_27J13/adc.h
17,7 → 17,7
#define ADC_CHANNEL_VDDCORE 0b1110
#define ADC_CHANNEL_ABG 0b1111
 
#define ADC_TAD_20 0b111;
#define ADC_TAD_20 0b111
#define ADC_TAD_16 0b110
#define ADC_TAD_12 0b101
#define ADC_TAD_8 0b100
/PIC Stuff/PIC_27J13/led_backpack.c
42,6 → 42,7
LED_blinkRate(HT16K33_BLINK_OFF);
LED_setBrightness(15); // Max brightness
LED_clear();
LED_writeDisplay();
}
 
void LED_setBrightness(unsigned char c) {
/PIC Stuff/PIC_27J13/main.c
20,7 → 20,7
#pragma config CFGPLLEN = ON // Enable PLL on startup
#pragma config PLLSEL = PLL96 // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
//#pragma config SOSCSEL = HIGH // High Power T1OSC/SOSC circuit selected
#pragma config ADCSEL = BIT12 // 12-bit ADrC
//#pragma config ADCSEL = BIT12 // 12-bit ADrC
//#pragma config IOL1WAY = OFF // IOLOCK bit can be set and cleared as needed
 
/* ----------- IO Pins -----------
210,15 → 210,12
#ifdef _TEST_NFC
 
void main(void) {
unsigned char i = 0;
unsigned char length = 0;
unsigned char result = 0;
NFC_FIRMWARE_VERSION version;
unsigned char buffer[50];
unsigned char i, length = 0;
 
// NFC stuff
unsigned char uid[7];
unsigned char uidLen;
NFC_FIRMWARE_VERSION version;
NFC_TargetDataMiFare cardData[2];
NFC_TargetDataMiFare cardData_prev[2];
 
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
251,21 → 248,88
DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
NFC_SAMConfig();
 
memset(cardData, 0, 24);
while (1) {
// // This query will hang until the NFC chip replies (card detected)
// length = NFC_readPassiveTargetID(cardData);
// if (length) {
// DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[0].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// if (length == 2) {
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[1].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// }
// }
 
// This call will hang until the NFC chip replies (card detected)
if (NFC_readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLen)) {
DBG_PRINT_MAIN("Card Found!\r\n");
DBG_PRINT_MAIN("UID Length: %d bytes\r\n", uidLen);
DBG_PRINT_MAIN("UID: ");
for (i = 0; i < uidLen; i++) {
DBG_PRINT_MAIN("%02X ", uid[i]);
// This query will not wait for a detection before responding
length = NFC_pollTargets(1, 1, cardData);
if (!length) {
memset(cardData_prev, 0, 24);
} else if (length == 1) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
} else {
DBG_PRINT_MAIN("UID: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
}
DBG_PRINT_MAIN("\r\n");
memset(&cardData_prev[1], 0, 12);
} else if (length == 2) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
memcpy((char *)&cardData_prev[1], (const char *)&cardData[1], 12);
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// First card matched
DBG_PRINT_MAIN("UID2: ");
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy(&cardData_prev[1], (const char *)&cardData[1], 12);
} else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Second card matched
DBG_PRINT_MAIN("UID1: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
} else {
// No match
DBG_PRINT_MAIN("UID1: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
DBG_PRINT_MAIN("UID2: ");
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *)&cardData_prev[1], &cardData[1], 12);
}
}
 
// Delay10KTCYx(255);
// Delay10KTCYx(255);
}
}
#endif
636,7 → 700,7
#ifdef _TEST_ADC
 
void main(void) {
unsigned int x,y,z;
unsigned int x, y, z;
unsigned char buffer[60];
 
/* --------------------- Oscillator Configuration --------------------- */
652,7 → 716,7
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
ADC_Init(ADC_TAD_0, ADC_FOSC_64);
ADC_Init(ADC_TAD_20, ADC_FOSC_64);
 
// I2C_Configure_Master(I2C_400KHZ);
SSD1331_Begin();
669,20 → 733,20
while (1) {
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN0);
GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,0);
GFX_fillRect(0, 0, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 0);
while (!ADC_Get_Result(&x));
GFX_writeString("X: %u", x);
 
ADC_Start(ADC_CHANNEL_AN1);
GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,8);
GFX_fillRect(0, 8, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 8);
while (!ADC_Get_Result(&y));
GFX_writeString("Y: %u", y);
 
ADC_Start(ADC_CHANNEL_AN2);
GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,16);
GFX_fillRect(0, 16, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 16);
while (!ADC_Get_Result(&z));
GFX_writeString("Z: %u", z);
}
697,7 → 761,6
!defined(_TEST_ADC)
 
void main(void) {
unsigned int x,y,z;
unsigned char length = 0;
unsigned char buffer[60];
 
712,21 → 775,22
ANCON1 = 0x1F;
 
UART1_Init(); // Initialize the UART handler code
// I2C_Init(); // Initialize the I2C handler code
I2C_Init(); // Initialize the I2C handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
// NFC_Init(); // Initialize the NFC chip (uses I2C)
// LED_Init(); // Initialize the LED backpack (uses I2C)
ADC_Init(ADC_TAD_0, ADC_FOSC_64);
LED_Init(); // Initialize the LED backpack (uses I2C)
 
// I2C_Configure_Master(I2C_400KHZ);
SSD1331_Begin();
 
I2C_Configure_Master(I2C_400KHZ);
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
 
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
 
SSD1331_Begin();
LED_Start();
memset(buffer, 0, 60);
GFX_clearScreen();
GFX_setRotation(3);
734,30 → 798,12
while (1) {
 
// Reads input from UART and prints to display
// length = UART1_Read_Buffer(buffer);
// if (length != 0) {
// buffer[length] = 0;
// GFX_appendString("%s", buffer);
// }
 
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN0);
GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,0);
while (!ADC_Get_Result(&x));
GFX_writeString("X: %u", x);
 
ADC_Start(ADC_CHANNEL_AN1);
GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,8);
while (!ADC_Get_Result(&y));
GFX_writeString("Y: %u", y);
 
ADC_Start(ADC_CHANNEL_AN2);
GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,16);
while (!ADC_Get_Result(&z));
GFX_writeString("Z: %u", z);
length = UART1_Read_Buffer(buffer);
if (length != 0) {
buffer[length] = 0;
GFX_appendString("%s", buffer);
LED_writeNum(length-1);
}
}
}
#endif
/PIC Stuff/PIC_27J13/maindefs.h
2,10 → 2,9
#define __maindefs_h
 
#include <p18f27j13.h>
#include <stdio.h>
#include "uart.h"
 
//#define _DEBUG
#define _DEBUG
//#define _TEST_UART
//#define _TEST_I2C_MASTER
//#define _TEST_I2C_SLAVE
14,7 → 13,7
//#define _TEST_LED_BACKPACK
//#define _TEST_SSD1306_OLED
//#define _TEST_SSD1331_OLED
#define _TEST_ADC
//#define _TEST_ADC
 
// Option to choose between active OLED displays
//#define GFX_SSD1306
22,10 → 21,10
 
// Enable or disable debug prints depending on project preprocessor (_DEBUG)
#ifdef _DEBUG
#define DBG_PRINT_MAIN(x) printf(x)
#define DBG_PRINT_UART(x) printf(x)
#define DBG_PRINT_I2C(x) printf(x)
#define DBG_PRINT_SPI(x) printf(x)
#define DBG_PRINT_MAIN(x) UART1_WriteS(x)
#define DBG_PRINT_UART(x) UART1_WriteS(x)
#define DBG_PRINT_I2C(x) UART1_WriteS(x)
#define DBG_PRINT_SPI(x) UART1_WriteS(x)
#define DBG_PRINT_XBEE(x)
#define DBG_PRINT_PORTB_INT(x)
#define DBG_PRINT_INT(x)
/PIC Stuff/PIC_27J13/nfc.c
11,9 → 11,9
static char pn532ack[] = {0x01, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
 
void NFC_Init() {
TRISCbits.TRISC1 = 1; // IRQ Pin is RC5
TRISCbits.TRISC2 = 0; // Reset Pin is RC2
TRISCbits.TRISC1 = 1; // IRQ Pin is RC5
TRISCbits.TRISC2 = 0; // Reset Pin is RC2
 
// Reset the PN532
LATCbits.LATC2 = 1;
LATCbits.LATC2 = 0;
24,9 → 24,9
// 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.packetbuffer[1] = 0x01; // Normal mode
nfc_data.packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
nfc_data.packetbuffer[3] = 0x01; // Use IRQ pin
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
return 0;
38,7 → 38,7
 
// Checks the firmware version of the PN5xx chip
NFC_FIRMWARE_VERSION NFC_getFirmwareVersion(void) {
NFC_FIRMWARE_VERSION response = {0,0,0,0};
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
 
// Create and send command
nfc_data.packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
50,7 → 50,7
NFC_I2C_Read_Data(nfc_data.packetbuffer, 12);
 
// Compare and check returned values
if (strncmp((char *)nfc_data.packetbuffer, (char *)pn532response_firmwarevers, 8) != 0)
if (strncmp((char *) nfc_data.packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
return response;
 
// Save and return info
87,13 → 87,12
return 1;
}
 
// Waits for an ISO14443A target to enter the field
unsigned char NFC_readPassiveTargetID(unsigned char cardbaudrate, unsigned char * uid, unsigned char * uidLength) {
unsigned char i = 0;
 
// 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] = 1; // max 1 cards at once (we can set this to 2 later)
nfc_data.packetbuffer[2] = cardbaudrate;
nfc_data.packetbuffer[1] = 2; // Max 2 cards at once
nfc_data.packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 3))
return 0;
101,33 → 100,110
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data.packetbuffer, 20);
NFC_I2C_Read_Data(nfc_data.packetbuffer, 35);
 
/* ISO14443A card response should be in the following format:
// (byte 0 is actually 0x01 (data ready))
byte Description
------------- ------------------------------------------
b0..6 Frame header and preamble
b7 Tags Found
b8 Tag Number (only one used in this example)
b9..10 SENS_RES
b11 SEL_RES
b12 NFCID Length
b13..NFCIDLen NFCID */
/* InListPassiveTarget response should be in the following format:
* Byte Description
* ---------- ------------------
* b0 Data ACK
* b1..7 Frame header and preamble
* b8 Tags found
* b9..N NFC_TargetDataMiFare[2]
* bN+1..N+2 Checksum + postamble
*/
 
// Check # of tags found
if (nfc_data.packetbuffer[8] != 1)
if (!nfc_data.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);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 12);
}
 
// Save UID length
*uidLength = nfc_data.packetbuffer[13];
// Save data from second card
if (nfc_data.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);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.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);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 12);
}
}
}
// Return the number of cards detected
return nfc_data.packetbuffer[8];
}
 
// Save UID
for (i = 0; i < *uidLength; i++) {
uid[i] = nfc_data.packetbuffer[14+i];
// 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
 
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
return 0;
 
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data.packetbuffer, 37);
 
/* InAutoPoll response should be in the following format:
* Byte Description
* ---------- ------------------
* b0 Data ACK
* b1..7 Frame header and preamble
* b6 Tags found
* b7 Polled target type (should be 0x10 Mifare)
* b8 TargetData length (1/2)
* b9..N NFC_TargetDataMiFare[1/2]
* bN+1..N+2 Checksum + postamble
*/
 
// Check # of tags found
if (!nfc_data.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);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 12);
}
 
return 1;
// Save data from second card
if (nfc_data.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);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 12);
}
} else {
if (nfc_data.packetbuffer[29] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 12);
}
}
}
 
// Return the number of cards detected
return nfc_data.packetbuffer[8];
}
 
// Indicates whether the specified block number is the first block
155,23 → 231,23
// blockNumber = The block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards)\
// keyNumber = Which key type to use during authentication (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)
// keyData = Pointer to a byte array containing the 6 byte key value
 
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[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.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.packetbuffer[4 + i] = keyData[i];
}
for (i = 0; i < uidLen; i++) {
nfc_data.packetbuffer[10+i] = uid[i];
nfc_data.packetbuffer[10 + i] = uid[i];
}
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 10+uidLen))
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 10 + uidLen))
return 0;
 
// Read response from PN532
205,7 → 281,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.packetbuffer[9 + i];
}
 
return 1;
214,14 → 290,14
// Tries to write an entire 16-byte data block at the specified block address
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data) {
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) */
for (i = 0; i < 16; i++) { /* Data Payload */
nfc_data.packetbuffer[4+i] = data[i];
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) */
for (i = 0; i < 16; i++) { /* Data Payload */
nfc_data.packetbuffer[4 + i] = data[i];
}
 
// Send frame and check for ACK
234,7 → 310,7
return 1;
}
 
// Formats a Mifare Classic card to store NDEF Records
// Formats a Mifare Classic card to store NDEF Records
unsigned char NFC_mifareclassic_FormatNDEF(void) {
unsigned char sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
unsigned char sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
263,7 → 339,7
 
// Figure out how long the string is
unsigned char len = strlen(url);
 
unsigned char sectorbuffer1[16] = {0x00, 0x00, 0x03, len + 5, 0xD1, 0x01, len + 1, 0x55, uriIdentifier, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
323,8 → 399,8
// Check ACK
NFC_I2C_Read_Data(buffer, 6);
 
// Return if the 7 bytes matches the ACK
return (strncmp((char *)buffer, (char *)pn532ack, 7) == 0);
// Return if the 7 bytes matches the ACK pattern
return (strncmp((char *) buffer, (char *) pn532ack, 7) == 0);
}
 
// Checks the IRQ pin to know if the PN532 is ready
344,25 → 420,25
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
// Read bytes from PN532 into buffer
I2C_Master_Recv(PN532_I2C_ADDRESS, length+2);
I2C_Master_Recv(PN532_I2C_ADDRESS, length + 2);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
I2C_Read_Buffer((char *)buffer);
I2C_Read_Buffer((char *) buffer);
 
// Note: First byte is always 0x01 (ready status from PN532)
/* Remaining packet byte layout is as follows:
Byte Description
----- ----------------------
* 0 Preamble (0x00)
* 1-2 Start code (0x00,0xFF)
* 3 Length (TFI to N)
* 4 Length Checksum (Length + LCS = 0x00)
* 5 TFI (Frame identifier)
* 0 Data ready ACK
* 1 Preamble (0x00)
* 2-3 Start code (0x00,0xFF)
* 4 Length (TFI to N)
* 5 Length Checksum (Length + LCS = 0x00)
* 6 TFI (Frame identifier)
* 0xD4 - Host to PN532
* 0xD5 - PN532 to Host
* 6-N Data (Length - 1 bytes)
* 7-N Data (Length - 1 bytes)
* N+1 Data checksum (TFI + Data~N + DCS = 0x00)
* N+2 Postamble (0x00) */
}
371,7 → 447,7
void NFC_I2C_Write_Cmd(unsigned char* cmd, unsigned char cmdlen) {
int i;
unsigned char checksum;
unsigned char buffer[PN532_PACKBUFFSIZ+8];
unsigned char buffer[PN532_PACKBUFFSIZ + 8];
unsigned char buffer_ind = 6;
cmdlen++;
 
387,12 → 463,12
 
 
// Copy cmd to be sent
for (i = 0; i < cmdlen-1; i++) {
for (i = 0; i < cmdlen - 1; i++) {
checksum += cmd[i];
buffer[buffer_ind] = cmd[i];
buffer_ind++;
}
 
buffer[buffer_ind] = ~checksum;
buffer_ind++;
buffer[buffer_ind] = PN532_POSTAMBLE;
/PIC Stuff/PIC_27J13/nfc.h
125,6 → 125,15
unsigned char Support;
} NFC_FIRMWARE_VERSION;
 
typedef struct {
unsigned char TG;
unsigned char SENS_RES[2];
unsigned char SEL_RES;
unsigned char NFCID_LEN;
unsigned char NFCID[7];
} NFC_TargetDataMiFare;
// Size can be 9 or 12 bytes
 
typedef struct __NFC_DATA {
unsigned char packetbuffer[PN532_PACKBUFFSIZ];
} NFC_DATA;
139,7 → 148,8
//unsigned char NFC_readGPIO(void);
 
// ISO14443A functions
unsigned char NFC_readPassiveTargetID(unsigned char cardbaudrate, unsigned char * uid, unsigned char * uidLength);
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *uidData);
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *uidData);
 
// Mifare Classic functions
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock);
/PIC Stuff/PIC_27J13/uart.c
1,6 → 1,7
#include "maindefs.h"
#include "uart.h"
#include <string.h>
#include <stdio.h>
 
static UART_DATA uart_1_data;
 
59,6 → 60,7
if (PIR1bits.RC1IF) { // Check if data receive flag is set
c = RCREG1;
 
#ifdef UART1_RECV_BUFFER
// Save received data into buffer
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c;
if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) {
83,6 → 85,10
uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
uart_1_data.buffer_in_len_tmp = 0;
}
#endif
#ifdef UART1_RECV_XBEE
 
#endif
}
 
if (RCSTAbits.OERR == 1) {
120,7 → 126,21
}
 
void UART1_WriteS(const rom char *fmt, ...) {
#ifdef _DEBUG
unsigned char i;
va_list args;
va_start(args, fmt);
vsprintf((char *) uart_1_data.buffer_out, fmt, args);
va_end(args);
uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
uart_1_data.buffer_out_ind = 1;
for (i = 0; i < uart_1_data.buffer_out_len; i++) {
TXREG1 = uart_1_data.buffer_out[i];
Nop();
while(!PIR1bits.TX1IF); // Wait for byte to be transmitted
}
#else
va_list args;
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
va_start(args, fmt);
vsprintf((char *) uart_1_data.buffer_out, fmt, args);
129,10 → 149,19
uart_1_data.buffer_out_ind = 1;
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
#endif
}
 
void UART1_WriteB(const char *msg, unsigned char length) {
unsigned char i;
#ifdef _DEBUG
for (i = 0; i < length; i++) {
TXREG1 = msg[i];
Nop();
while(!PIR1bits.TX1IF); // Wait for byte to be transmitted
}
#else
unsigned char i;
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
uart_1_data.buffer_out_len = length;
uart_1_data.buffer_out_ind = 1;
141,6 → 170,7
}
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
#endif
}
 
unsigned char UART1_Buffer_Len() {
/PIC Stuff/PIC_27J13/uart.h
5,6 → 5,9
 
#define UART1_BREAK_CHAR 0x0D //(CR)
 
#define UART1_RECV_BUFFER
//#define UART1_RECV_XBEE
 
typedef struct __UART_DATA {
unsigned char buffer_in[MAXUARTBUF];
unsigned char buffer_in_read_ind;