mirror of
https://github.com/whowechina/aic_pico.git
synced 2024-12-01 00:17:16 +01:00
AIME on Bana mode working
This commit is contained in:
parent
1a28a6e940
commit
dee5521ce8
Binary file not shown.
@ -60,4 +60,7 @@ bool nfc_mifare_read(uint8_t block_id, uint8_t block_data[16]);
|
||||
|
||||
bool nfc_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]);
|
||||
|
||||
void nfc_select();
|
||||
void nfc_deselect();
|
||||
|
||||
#endif
|
||||
|
@ -115,7 +115,7 @@ static void send_response()
|
||||
int total_len = 7 + response.hdr.len;
|
||||
bana_puts((const char *)response.raw, total_len);
|
||||
|
||||
DEBUG("\n\033[33mResp %2d:%02x >>", response.hdr.len - 2, response.cmd);
|
||||
DEBUG("\n\033[33m%6ld<< %02x", time_us_32() / 1000, response.cmd);
|
||||
for (int i = 0; i < response.hdr.len - 2; i++) {
|
||||
DEBUG(" %02x", response.data[i]);
|
||||
}
|
||||
@ -198,8 +198,6 @@ static void handle_no_card()
|
||||
|
||||
static void cmd_poll_card()
|
||||
{
|
||||
send_ack();
|
||||
|
||||
nfc_card_t card = nfc_detect_card_ex(true, true, false);
|
||||
if (debug) {
|
||||
display_card(&card);
|
||||
@ -276,6 +274,23 @@ static void cmd_mifare()
|
||||
}
|
||||
}
|
||||
|
||||
static void cmd_select()
|
||||
{
|
||||
send_response_data("\x00", 1);
|
||||
nfc_select();
|
||||
}
|
||||
|
||||
static void cmd_deselect()
|
||||
{
|
||||
nfc_deselect();
|
||||
send_response_data("\x01\x00", 2);
|
||||
}
|
||||
|
||||
static void cmd_release()
|
||||
{
|
||||
send_response_data("\x01\x00", 2);
|
||||
}
|
||||
|
||||
/* https://github.com/chujohiroto/Raspberry-RCS620S/blob/master/rcs620s.py */
|
||||
static void cmd_felica_read(void *read_req)
|
||||
{
|
||||
@ -298,7 +313,6 @@ static void cmd_felica_read(void *read_req)
|
||||
uint8_t block[4][16];
|
||||
} resp;
|
||||
|
||||
send_ack();
|
||||
int block_num = read->block_num;
|
||||
DEBUG("\nFelica read: ");
|
||||
|
||||
@ -343,12 +357,6 @@ static uint32_t led_color = 0;
|
||||
|
||||
static void handle_frame()
|
||||
{
|
||||
DEBUG("\n\033[32mBana %d:%02x >>", request.hdr.len - 2, request.cmd);
|
||||
for (int i = 0; i < request.hdr.len - 2; i++) {
|
||||
DEBUG(" %02x", request.data[i]);
|
||||
}
|
||||
DEBUG("\033[0m");
|
||||
|
||||
switch (request.cmd) {
|
||||
case 0x18:
|
||||
case 0x12:
|
||||
@ -381,16 +389,16 @@ static void handle_frame()
|
||||
cmd_mifare();
|
||||
break;
|
||||
case 0x44:
|
||||
send_response_data("\x01\x00", 2);
|
||||
cmd_deselect();
|
||||
break;
|
||||
case 0xa0:
|
||||
cmd_felica();
|
||||
break;
|
||||
case 0x52:
|
||||
send_response_data("\x01\x00", 2);
|
||||
cmd_release();
|
||||
break;
|
||||
case 0x54:
|
||||
send_response_data("\x00", 1);
|
||||
cmd_select();
|
||||
break;
|
||||
default:
|
||||
printf("\nUnknown cmd: %02x\n", request.cmd);
|
||||
|
@ -57,20 +57,26 @@ struct {
|
||||
bool (*mifare_read)(uint8_t block_id, uint8_t block_data[16]);
|
||||
bool (*felica_read)(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]);
|
||||
void (*set_wait_loop)(nfc_wait_loop_t loop);
|
||||
void (*select)();
|
||||
void (*deselect)();
|
||||
} api[3] = {
|
||||
{
|
||||
pn532_poll_mifare, pn532_poll_felica, func_null,
|
||||
pn532_rf_field,
|
||||
pn532_mifare_auth, pn532_mifare_read,
|
||||
pn532_felica_read,
|
||||
pn532_set_wait_loop
|
||||
pn532_set_wait_loop,
|
||||
pn532_select,
|
||||
NULL,
|
||||
},
|
||||
{
|
||||
pn5180_poll_mifare, pn5180_poll_felica, pn5180_poll_vicinity,
|
||||
pn5180_rf_field,
|
||||
pn5180_mifare_auth, pn5180_mifare_read,
|
||||
pn5180_felica_read,
|
||||
pn5180_set_wait_loop
|
||||
pn5180_set_wait_loop,
|
||||
pn5180_select,
|
||||
pn5180_deselect,
|
||||
},
|
||||
{ 0 },
|
||||
};
|
||||
@ -113,7 +119,7 @@ void nfc_attach_spi(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy)
|
||||
void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
|
||||
uint8_t rst, uint8_t nss, uint8_t busy)
|
||||
{
|
||||
spi_init(port, 2000 * 1000);
|
||||
spi_init(port, 8000 * 1000);
|
||||
spi_set_format(port, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
|
||||
gpio_set_function(miso, GPIO_FUNC_SPI);
|
||||
@ -265,3 +271,17 @@ bool nfc_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16
|
||||
}
|
||||
return api[nfc_module].felica_read(svc_code, block_id, block_data);
|
||||
}
|
||||
|
||||
void nfc_select()
|
||||
{
|
||||
if (api[nfc_module].select) {
|
||||
api[nfc_module].select();
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_deselect()
|
||||
{
|
||||
if (api[nfc_module].deselect) {
|
||||
api[nfc_module].deselect();
|
||||
}
|
||||
}
|
@ -15,6 +15,8 @@
|
||||
|
||||
#include "pn5180.h"
|
||||
|
||||
#define DEBUG(...) { if (0) printf(__VA_ARGS__); }
|
||||
|
||||
#define IO_TIMEOUT_US 1000
|
||||
#define PN5180_I2C_ADDRESS 0x24
|
||||
|
||||
@ -240,43 +242,53 @@ static void anti_collision(uint8_t code, uint8_t uid[5], uint8_t *sak)
|
||||
pn5180_read_data(sak, 1); // sak
|
||||
}
|
||||
|
||||
bool pn5180_poll_mifare(uint8_t uid[7], int *len)
|
||||
static struct {
|
||||
uint8_t buf[5];
|
||||
uint8_t sak;
|
||||
uint8_t uid[7];
|
||||
uint8_t len;
|
||||
} mi_poll;
|
||||
|
||||
static void poll_mifare_1()
|
||||
{
|
||||
pn5180_reset();
|
||||
pn5180_load_rf_config(0x00, 0x80);
|
||||
pn5180_rf_field(true);
|
||||
|
||||
rf_crc_off();
|
||||
|
||||
pn5180_and_reg(PN5180_REG_IRQ_CLEAR, 0x000fffff);
|
||||
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8);
|
||||
pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03);
|
||||
|
||||
uint8_t cmd[1] = {0x26};
|
||||
pn5180_send_data(cmd, 1, 7);
|
||||
uint8_t buf[5] = {0};
|
||||
pn5180_read_data(buf, 2);
|
||||
pn5180_read_data(mi_poll.buf, 2);
|
||||
}
|
||||
|
||||
uint8_t sak;
|
||||
|
||||
anti_collision(0x93, buf, &sak);
|
||||
|
||||
bool result = false;
|
||||
if ((sak & 0x04) == 0) {
|
||||
memmove(uid, buf, 4);
|
||||
*len = 4;
|
||||
result = true;
|
||||
} else if (buf[0] == 0x88) {
|
||||
memmove(uid, buf + 1, 3);
|
||||
anti_collision(0x95, buf, &sak);
|
||||
if (sak != 0xff) {
|
||||
memmove(uid + 3, buf, 4);
|
||||
*len = 7;
|
||||
result = true;
|
||||
static void poll_mifare_2()
|
||||
{
|
||||
anti_collision(0x93, mi_poll.buf, &mi_poll.sak);
|
||||
mi_poll.len = 0;
|
||||
if ((mi_poll.sak & 0x04) == 0) {
|
||||
memmove(mi_poll.uid, mi_poll.buf, 4);
|
||||
mi_poll.len = 4;
|
||||
} else if (mi_poll.buf[0] == 0x88) {
|
||||
memmove(mi_poll.uid, mi_poll.buf + 1, 3);
|
||||
anti_collision(0x95, mi_poll.buf, &mi_poll.sak);
|
||||
if (mi_poll.sak != 0xff) {
|
||||
memmove(mi_poll.uid + 3, mi_poll.buf, 4);
|
||||
mi_poll.len = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
bool pn5180_poll_mifare(uint8_t uid[7], int *len)
|
||||
{
|
||||
poll_mifare_1();
|
||||
poll_mifare_2();
|
||||
|
||||
memcpy(uid, mi_poll.uid, mi_poll.len);
|
||||
*len = mi_poll.len;
|
||||
return *len > 0;
|
||||
}
|
||||
|
||||
static uint8_t idm_cache[8] = {0};
|
||||
@ -359,6 +371,29 @@ bool pn5180_poll_vicinity(uint8_t uid[8])
|
||||
|
||||
bool pn5180_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6])
|
||||
{
|
||||
static struct {
|
||||
uint32_t time;
|
||||
uint32_t uid;
|
||||
uint8_t key_id;
|
||||
uint8_t block_id;
|
||||
bool result;
|
||||
} cache = { 0 };
|
||||
|
||||
uint32_t now = time_us_32();
|
||||
if ((now < cache.time + 1000000) &&
|
||||
(cache.uid == *(uint32_t *)uid) &&
|
||||
(cache.key_id == key_id) &&
|
||||
(cache .block_id == block_id)) {
|
||||
cache.time = now;
|
||||
return cache.result;
|
||||
}
|
||||
|
||||
cache.time = now;
|
||||
cache.uid = *(uint32_t *)uid;
|
||||
cache.key_id = key_id;
|
||||
cache.block_id = block_id;
|
||||
cache.result = false;
|
||||
|
||||
uint8_t cmd[] = {
|
||||
CMD_MIFARE_AUTHENTICATE,
|
||||
key[0], key[1], key[2], key[3], key[4], key[5],
|
||||
@ -370,15 +405,34 @@ bool pn5180_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id,
|
||||
read_write(cmd, sizeof(cmd), &response, 1);
|
||||
|
||||
if ((response == 1) || (response == 2)) {
|
||||
printf("\nMifare auth failed: %d, [%d:%d]", response, cmd[7], block_id);
|
||||
DEBUG("\nPN5180 Mifare auth failed: %d, [%02x:%d]", response, cmd[7], block_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t ignored[16];
|
||||
if (!pn5180_mifare_read(block_id & 0xfc, ignored)) {
|
||||
DEBUG("\nPN5180 Mifare auth check bad");
|
||||
return false;
|
||||
}
|
||||
|
||||
cache.result = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16])
|
||||
{
|
||||
static struct {
|
||||
uint32_t time;
|
||||
uint8_t data[16];
|
||||
} cache = { 0 };
|
||||
|
||||
uint32_t now = time_us_32();
|
||||
if ((now < cache.time + 1000000) && (block_id == 0)) {
|
||||
memcpy(block_data, cache.data, 16);
|
||||
cache.time = now;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t cmd[] = { CMD_MIFARE_READ, block_id };
|
||||
pn5180_send_data(cmd, sizeof(cmd), 0);
|
||||
|
||||
@ -386,11 +440,17 @@ bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16])
|
||||
|
||||
uint16_t len = pn5180_get_rx() & 0x1ff;
|
||||
if (len != 16) {
|
||||
printf("\nMifare read error (block %d): %d", block_id, len);
|
||||
DEBUG("\nPN5180 Mifare read error (block %d): %d", block_id, len);
|
||||
return false;
|
||||
}
|
||||
|
||||
pn5180_read_data(block_data, 16);
|
||||
|
||||
if (block_id == 0) {
|
||||
memcpy(cache.data, block_data, 16);
|
||||
cache.time = time_us_32();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -418,7 +478,7 @@ bool pn5180_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data
|
||||
pn5180_read_data((uint8_t *)&out, sizeof(out));
|
||||
|
||||
if ((out.len != sizeof(out)) || (out.cmd != 0x07) || (out.status != 0x00)) {
|
||||
printf("\nPN532 Felica read failed [%04x:%04x]", svc_code, block_id);
|
||||
DEBUG("\nPN5180 Felica read failed [%04x:%04x]", svc_code, block_id);
|
||||
memset(block_data, 0, 16);
|
||||
return false;
|
||||
}
|
||||
@ -427,3 +487,14 @@ bool pn5180_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Not real select, just a time distribution of a poll */
|
||||
void pn5180_select()
|
||||
{
|
||||
poll_mifare_1();
|
||||
}
|
||||
|
||||
/* Not real deselect, just a time distribution of a poll */
|
||||
void pn5180_deselect()
|
||||
{
|
||||
poll_mifare_2();
|
||||
}
|
@ -52,4 +52,7 @@ bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16]);
|
||||
|
||||
bool pn5180_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]);
|
||||
|
||||
void pn5180_select();
|
||||
void pn5180_deselect();
|
||||
|
||||
#endif
|
||||
|
@ -13,6 +13,8 @@
|
||||
|
||||
#include "pn532.h"
|
||||
|
||||
#define DEBUG(...) { if (0) printf(__VA_ARGS__); }
|
||||
|
||||
#define IO_TIMEOUT_US 1000
|
||||
#define PN532_I2C_ADDRESS 0x24
|
||||
|
||||
@ -75,14 +77,6 @@ static int read_frame(uint8_t *frame, uint8_t len)
|
||||
uint8_t buf[len + 1];
|
||||
int ret = pn532_read(buf, len + 1);
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("I2C data read: %d -", ret);
|
||||
for (int i = 0; i < len + 1; i++) {
|
||||
printf(" %02x", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
if (ret == len + 1) {
|
||||
memcpy(frame, buf + 1, len);
|
||||
return len;
|
||||
@ -92,14 +86,6 @@ static int read_frame(uint8_t *frame, uint8_t len)
|
||||
|
||||
static int write_frame(const uint8_t *frame, uint8_t len)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("I2C frame write: %d -", len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf(" %02x", frame[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
return pn532_write(frame, len);
|
||||
}
|
||||
|
||||
@ -351,7 +337,6 @@ bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
|
||||
uint8_t param[] = { 1, 1, 0, 0xff, 0xff, 1, 0};
|
||||
int ret = pn532_write_command(0x4a, param, sizeof(param));
|
||||
if (ret < 0) {
|
||||
printf("%d\n", __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -378,19 +363,21 @@ bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, c
|
||||
key[0], key[1], key[2], key[3], key[4], key[5],
|
||||
uid[0], uid[1], uid[2], uid[3]
|
||||
};
|
||||
|
||||
int ret = pn532_write_command(0x40, param, sizeof(param));
|
||||
if (ret < 0) {
|
||||
printf("\nPN532 failed mifare auth command");
|
||||
DEBUG("\nPN532 failed mifare auth command");
|
||||
return false;
|
||||
}
|
||||
int result = pn532_read_response(0x40, readbuf, sizeof(readbuf));
|
||||
if (readbuf[0] != 0) {
|
||||
printf("\nPN532 Mifare AUTH failed %d %02x key[%2x:%d]: ", result, readbuf[0], param[1], param[2]);
|
||||
DEBUG("\nPN532 Mifare AUTH failed %d %02x key[%2x:%d]: ", result, readbuf[0], param[1], param[2]);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("%02x", key[i]);
|
||||
DEBUG("%02x", key[i]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -400,14 +387,14 @@ bool pn532_mifare_read(uint8_t block_id, uint8_t block_data[16])
|
||||
|
||||
int ret = pn532_write_command(0x40, param, sizeof(param));
|
||||
if (ret < 0) {
|
||||
printf("\nPN532 failed mifare read command");
|
||||
DEBUG("\nPN532 failed mifare read command");
|
||||
return false;
|
||||
}
|
||||
|
||||
int result = pn532_read_response(0x40, readbuf, sizeof(readbuf));
|
||||
|
||||
if (readbuf[0] != 0 || result != 17) {
|
||||
printf("\nPN532 Mifare READ failed %d %02x", result, readbuf[0]);
|
||||
DEBUG("\nPN532 Mifare READ failed %d %02x", result, readbuf[0]);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -429,7 +416,7 @@ int pn532_felica_command(uint8_t cmd, const uint8_t *param, uint8_t param_len, u
|
||||
|
||||
int ret = pn532_write_command(0x40, cmd_buf, sizeof(cmd_buf));
|
||||
if (ret < 0) {
|
||||
printf("\nFailed send felica command");
|
||||
DEBUG("\nFailed send felica command");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -454,7 +441,7 @@ bool pn532_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data[
|
||||
int result = pn532_felica_command(0x06, param, sizeof(param), readbuf);
|
||||
|
||||
if (result != 12 + 16 || readbuf[9] != 0 || readbuf[10] != 0) {
|
||||
printf("\nPN532 Felica read failed [%04x:%04x]", svc_code, block_id);
|
||||
DEBUG("\nPN532 Felica read failed [%04x:%04x]", svc_code, block_id);
|
||||
memset(block_data, 0, 16);
|
||||
return true; // we fake the result when it fails
|
||||
}
|
||||
@ -473,13 +460,20 @@ bool pn532_felica_write(uint16_t svc_code, uint16_t block_id, const uint8_t bloc
|
||||
int result = pn532_felica_command(0x08, param, sizeof(param), readbuf);
|
||||
|
||||
if (result < 0) {
|
||||
printf("\nPN532 Felica WRITE failed %d", result);
|
||||
DEBUG("\nPN532 Felica WRITE failed %d", result);
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("\nPN532 Felica WRITE success ");
|
||||
DEBUG("\nPN532 Felica WRITE success ");
|
||||
for (int i = 0; i < result; i++) {
|
||||
printf(" %02x", readbuf[i]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void pn532_select()
|
||||
{
|
||||
uint8_t ignore_buf[7];
|
||||
int ignore_len;
|
||||
pn532_poll_mifare(ignore_buf, &ignore_len);
|
||||
}
|
||||
|
@ -35,4 +35,6 @@ bool pn532_mifare_read(uint8_t block_id, uint8_t block_data[16]);
|
||||
bool pn532_felica_read(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]);
|
||||
bool pn532_felica_write(uint16_t svc_code, uint16_t block_id, const uint8_t block_data[16]);
|
||||
|
||||
void pn532_select();
|
||||
|
||||
#endif
|
||||
|
@ -35,6 +35,8 @@
|
||||
#include "aime.h"
|
||||
#include "bana.h"
|
||||
|
||||
#define DEBUG(...) { if (0) printf(__VA_ARGS__); }
|
||||
|
||||
static struct {
|
||||
uint8_t current[9];
|
||||
uint8_t reported[9];
|
||||
@ -196,6 +198,12 @@ static void aime_poll_data()
|
||||
int count = tud_cdc_n_read(aime_intf, aime.buf + aime.pos,
|
||||
sizeof(aime.buf) - aime.pos);
|
||||
if (count > 0) {
|
||||
uint32_t now = time_us_32();
|
||||
DEBUG("\n\033[32m%6ld>>", now / 1000);
|
||||
for (int i = 6; i < count; i++) {
|
||||
DEBUG(" %02X", aime.buf[aime.pos + i]);
|
||||
}
|
||||
DEBUG("\033[0m");
|
||||
aime.pos += count;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user