Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for st25tb #142

Open
wants to merge 2 commits into
base: arduino
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions PN532/PN532.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,158 @@ bool PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) {
}
}

/***** ISO14443B SRIX4K Commands ******/

/**************************************************************************/
/*!
Waits for an ISO14443B target to enter the field
*/
/**************************************************************************/
bool PN532::st25tb_init()
{
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
pn532_packetbuffer[1] = 0x01; // Max 1 target at once
pn532_packetbuffer[2] = 0x03; // Set target to 106 kbps type B (ISO/IEC14443-3B)
pn532_packetbuffer[3] = 0x00; // Init data

if (HAL(writeCommand)(pn532_packetbuffer, 4)) {
return 0x0; // command failed
}
}

bool PN532::st25tb_select()
{
// INITIATE
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_INITIATE;
pn532_packetbuffer[2] = 0x00;

if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
return 0x0; // command failed
}

// read data packet
if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)) < 0) {
return 0x0;
}

// Check for errors
if (pn532_packetbuffer[0] != 0x00) {
return 0;
}

// SELECT
uint8_t chip_id = pn532_packetbuffer[1];
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_SELECT;
pn532_packetbuffer[2] = chip_id;

if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
return 0x0; // command failed
}

// read data packet
if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)) < 0) {
return 0x0;
}

// Check for errors
if (pn532_packetbuffer[0] != 0x00) {
return 0;
}

return 1;
}

bool PN532::st25tb_get_uid(uint8_t *uid)
{
// GET_UID
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_GET_UID;

if (HAL(writeCommand)(pn532_packetbuffer, 2)) {
return 0x0; // command failed
}

// read data packet
if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)) < 0) {
return 0x0;
}

// Check for errors
if (pn532_packetbuffer[0] != 0x00) {
return 0;
}

uid[0] = pn532_packetbuffer[1];
uid[1] = pn532_packetbuffer[2];
uid[2] = pn532_packetbuffer[3];
uid[3] = pn532_packetbuffer[4];
uid[4] = pn532_packetbuffer[5];
uid[5] = pn532_packetbuffer[6];
uid[6] = pn532_packetbuffer[7];
uid[7] = pn532_packetbuffer[8];

return 1;
}

bool PN532::st25tb_reset_to_inventory()
{
// RESET_TO_INVENTORY
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_RESET_TO_INVENTORY;

if (HAL(writeCommand)(pn532_packetbuffer, 2)) {
return 0x0; // command failed
}

return 1;
}

bool PN532::st25tb_read_block(uint8_t address, uint8_t *block)
{
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_READ_BLOCK;
pn532_packetbuffer[2] = address;

if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
return 0x0; // command failed
}

// read data packet
if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)) < 0) {
return 0x0;
}

// Check for errors
if (pn532_packetbuffer[0] != 0x00) {
return 0;
}

block[0] = pn532_packetbuffer[1];
block[1] = pn532_packetbuffer[2];
block[2] = pn532_packetbuffer[3];
block[3] = pn532_packetbuffer[4];

return 1;
}

bool PN532::st25tb_write_block(uint8_t address, uint8_t *block)
{
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = ST25TB_CMD_WRITE_BLOCK;
pn532_packetbuffer[2] = address;
pn532_packetbuffer[3] = block[0];
pn532_packetbuffer[4] = block[1];
pn532_packetbuffer[5] = block[2];
pn532_packetbuffer[6] = block[3];

if (HAL(writeCommand)(pn532_packetbuffer, 7)) {
return 0x0; // command failed
}

return 1;
}
/**************************************************************************/
/*!
Waits for an ISO14443A target to enter the field
Expand Down
20 changes: 20 additions & 0 deletions PN532/PN532.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
#define MIFARE_CMD_INCREMENT (0xC1)
#define MIFARE_CMD_STORE (0xC2)

//ST25TB Command
#define ST25TB_CMD_INITIATE (0x06)
#define ST25TB_CMD_SELECT (0x0E)
#define ST25TB_CMD_RESET_TO_INVENTORY (0x0C)
#define ST25TB_CMD_GET_UID (0x0B)
#define ST25TB_CMD_READ_BLOCK (0x08)
#define ST25TB_CMD_WRITE_BLOCK (0x09)
#define ST25TB_4K_BLOCK_NUM (0x80)
#define ST25TB_4K_SYSTEM_BLOCK (0xFF)
#define ST25TB_BLOCK_SIZE (0x04)
#define ST25TB_UID_SIZE (0x08)

// FeliCa Commands
#define FELICA_CMD_POLLING (0x00)
#define FELICA_CMD_REQUEST_SERVICE (0x02)
Expand Down Expand Up @@ -173,6 +185,14 @@ class PN532
uint8_t mifareclassic_FormatNDEF (void);
uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char *url);

// St25tb functions
bool st25tb_init();
bool st25tb_select();
bool st25tb_get_uid(uint8_t *uid);
bool st25tb_read_block(uint8_t address, uint8_t *block);
bool st25tb_write_block(uint8_t address, uint8_t *block);
bool st25tb_reset_to_inventory();

// Mifare Ultralight functions
uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t *buffer);
uint8_t mifareultralight_WritePage (uint8_t page, uint8_t *buffer);
Expand Down