static void loop_rfid (void) { int res, line, t; // uint32_t counter; static unsigned char data[80]; /* fully initialized */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); /* read firmware revision */ debug_printf ("\nreading firmware version...\n"); data[0] = PN532_CMD_GetFirmwareVersion; while ((res = rfid_execute (&data, 1, sizeof (data))) < 0) { debug_printf ("Reading Firmware Version Error [%i]\n", res); pmu_wait_ms (450); GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (10); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); } debug_printf ("PN532 Firmware Version: "); if (data[1] != 0x32) rfid_hexdump (&data[1], data[0]); else debug_printf ("v%i.%i\n", data[2], data[3]); /* Set PN532 to virtual card */ data[0] = PN532_CMD_SAMConfiguration; data[1] = 0x02; /* Virtual Card Mode */ data[2] = 0x00; /* No Timeout Control */ data[3] = 0x00; /* No IRQ */ if ((res = rfid_execute (&data, 4, sizeof (data))) > 0) { debug_printf ("SAMConfiguration: "); rfid_hexdump (&data, res); } /* init RFID emulator */ rfid_init_emulator (); /* enable debug output */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); line = 0; t = 0; while (1) { #if 0 counter = LPC_TMR32B0->TC; pmu_wait_ms (100); counter = (LPC_TMR32B0->TC - counter) * 10; debug_printf ("LPC_TMR32B0[%08u]: %uHz\n", line++, counter); #endif GPIOSetValue (LED_PORT, LED_BIT, GPIOGetValue (1, 8)); } }
static void rfid_task(void *pvParameters) { int i; static unsigned char data[80]; /* touch unused Parameter */ (void) pvParameters; /* release reset line after 400ms */ vTaskDelay( 400 / portTICK_RATE_MS); rfid_reset(1); /* wait for PN532 to boot */ vTaskDelay( 100 / portTICK_RATE_MS); /* read firmware revision */ debug_printf("\nreading firmware version...\n"); data[0] = PN532_CMD_GetFirmwareVersion; rfid_execute(&data, 1, sizeof(data)); /* enable debug output */ debug_printf("\nenabling debug output...\n"); WriteRegister(0x6328, 0xFC); // select test bus signal WriteRegister(0x6321, 6); // select test bus type WriteRegister(0x6322, 0x07); while (1) { /* wait 100ms */ vTaskDelay( 100 / portTICK_RATE_MS); /* detect cards in field */ GPIOSetValue(LED_PORT, LED_BIT, LED_ON); debug_printf("\nchecking for cards...\n"); data[0] = PN532_CMD_InListPassiveTarget; data[1] = 0x01; /* MaxTg - maximum cards */ data[2] = 0x00; /* BrTy - 106 kbps type A */ if (((i = rfid_execute(&data, 3, sizeof(data))) >= 11) && (data[1] == 0x01) && (data[2] == 0x01)) { debug_printf("card id: "); rfid_hexdump(&data[7], data[6]); } else debug_printf("unknown response of %i bytes\n", i); GPIOSetValue(LED_PORT, LED_BIT, LED_OFF); /* turning field off */ debug_printf("\nturning field off again...\n"); data[0] = PN532_CMD_RFConfiguration; data[1] = 0x01; /* CfgItem = 0x01 */ data[2] = 0x00; /* RF Field = off */ rfid_execute(&data, 3, sizeof(data)); } }
int initiator_init(uint8_t *data, uint8_t size) { data[0] = PN532_CMD_InListPassiveTarget; /* 0x4a */ data[1] = 0x01; /* MaxTg - maximum cards */ data[2] = 0x00; /* BrTy - 106 kbps type A */ return rfid_execute(&data[0], 3, size); }
int mifare_reader_init(uint8_t *data, uint8_t size) { /* User Manual S.97 141520.pdf */ data[0] = PN532_CMD_SAMConfiguration; /* 0x14 */ data[1] = 0x01; /* Normal Mode */ return rfid_execute(&data[0], 2, size); }
int mifare_authenticate_block(uint8_t *data, uint8_t size, uint8_t block) { data[0] = PN532_CMD_InDataExchange; /* 0x40 */ data[1] = 0x01; /* card 1 */ data[2] = 0x60; /* MIFARE authenticate A */ data[3] = block; return rfid_execute(&data[0], 14, size); }
int turn_rf_off (uint8_t *data, uint8_t size) { /* wait 0.5s */ pmu_wait_ms(5); /* turning field off */ data[0] = PN532_CMD_RFConfiguration; data[1] = 0x01; /* CfgItem = 0x01 */ data[2] = 0x00; /* RF Field = off */ return rfid_execute(&data[0], 3, size); }
int mifare_write_block(uint8_t *data, uint8_t size, uint8_t block) { data[0] = PN532_CMD_InDataExchange; /* 0x40 */ data[1] = 0x01; /* card 1 */ data[2] = 0xA0; /* MIFARE write 16 bytes */ data[3] = block; /* block 1 */ return rfid_execute(&data[0], 20, size); }
int rfid_write_register(unsigned short address, unsigned char data) { unsigned char cmd[4]; /* write register */ cmd[0] = PN532_CMD_WriteRegister; /* high byte of address */ cmd[1] = address >> 8; /* low byte of address */ cmd[2] = address & 0xFF; /* data value */ cmd[3] = data; return rfid_execute(&cmd, sizeof(cmd), sizeof(data)); }
void WriteRegister(unsigned short address, unsigned char data) { unsigned char cmd[4]; /* write register */ cmd[0] = 0x08; /* high byte of address */ cmd[1] = address >> 8; /* low byte of address */ cmd[2] = address & 0xFF; /* data value */ cmd[3] = data; rfid_execute(&cmd, sizeof(cmd), sizeof(data)); }
int rfid_read_register(unsigned short address) { int res; unsigned char cmd[3]; /* write register */ cmd[0] = PN532_CMD_ReadRegister; /* high byte of address */ cmd[1] = address >> 8; /* low byte of address */ cmd[2] = address & 0xFF; if((res = rfid_execute(&cmd, sizeof(cmd), sizeof(cmd)))>1) return cmd[1]; else return res; }
static void loop_rfid (void) { int res; static unsigned char data[80]; /* release reset line after 400ms */ pmu_wait_ms (400); rfid_reset (1); /* wait for PN532 to boot */ pmu_wait_ms (100); /* fully initialized */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); /* read firmware revision */ debug_printf ("\nreading firmware version...\n"); data[0] = PN532_CMD_GetFirmwareVersion; while ((res = rfid_execute (&data, 1, sizeof (data))) < 0) { debug_printf ("Reading Firmware Version Error [%i]\n", res); pmu_wait_ms (450); GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (10); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); } debug_printf ("PN532 Firmware Version: "); if (data[1] != 0x32) rfid_hexdump (&data[1], data[0]); else debug_printf ("v%i.%i\n", data[2], data[3]); /* enable debug output */ debug_printf ("\nenabling debug output...\n"); rfid_write_register (0x6328, 0xFC); // select test bus signal rfid_write_register (0x6321, 6); // select test bus type rfid_write_register (0x6322, 0x07); /* enable debug output */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); while (1) { /* wait 10ms */ pmu_wait_ms (10); /* detect cards in field */ data[0] = PN532_CMD_InListPassiveTarget; data[1] = 0x01; /* MaxTg - maximum cards */ data[2] = 0x00; /* BrTy - 106 kbps type A */ if (((res = rfid_execute (&data, 3, sizeof (data))) >= 11) && (data[1] == 0x01) && (data[2] == 0x01)) { GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (50); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); debug_printf ("card id: "); rfid_hexdump (&data[7], data[6]); } else { GPIOSetValue (LED_PORT, LED_BIT, LED_ON); if (res != -8) debug_printf ("PN532 error res=%i\n", res); } /* turning field off */ data[0] = PN532_CMD_RFConfiguration; data[1] = 0x01; /* CfgItem = 0x01 */ data[2] = 0x00; /* RF Field = off */ rfid_execute (&data, 3, sizeof (data)); } }