Further work for RC522
This commit is contained in:
parent
96839468e8
commit
6953cbea2c
@ -3,7 +3,11 @@
|
|||||||
#ifndef __NFC_CHIPS_RC522_INTERNAL_H__
|
#ifndef __NFC_CHIPS_RC522_INTERNAL_H__
|
||||||
#define __NFC_CHIPS_RC522_INTERNAL_H__
|
#define __NFC_CHIPS_RC522_INTERNAL_H__
|
||||||
|
|
||||||
|
#define RC522_FIFO_SIZE 64
|
||||||
|
|
||||||
#define RC522_REG_CommandReg 0x01
|
#define RC522_REG_CommandReg 0x01
|
||||||
|
#define RC522_REG_CommandReg_RcvOff (1 << 5)
|
||||||
|
#define RC522_REG_CommandReg_PowerDown (1 << 4)
|
||||||
|
|
||||||
#define RC522_REG_ComlEnReg 0x02
|
#define RC522_REG_ComlEnReg 0x02
|
||||||
|
|
||||||
@ -92,4 +96,15 @@
|
|||||||
#define RC522_REG_TestDAC2Reg 0x3A
|
#define RC522_REG_TestDAC2Reg 0x3A
|
||||||
#define RC522_REG_TestADCReg 0x3B
|
#define RC522_REG_TestADCReg 0x3B
|
||||||
|
|
||||||
|
#define RC522_CMD_Idle 0x0
|
||||||
|
#define RC522_CMD_Mem 0x1
|
||||||
|
#define RC522_CMD_GenerateRandomId 0x2
|
||||||
|
#define RC522_CMD_CalcCRC 0x3
|
||||||
|
#define RC522_CMD_Transmit 0x4
|
||||||
|
#define RC522_CMD_NoCmdChange 0x7
|
||||||
|
#define RC522_CMD_Receive 0x8
|
||||||
|
#define RC522_CMD_Transceive 0xC
|
||||||
|
#define RC522_CMD_MFAuthent 0xE
|
||||||
|
#define RC522_CMD_SoftReset 0xF
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -54,7 +54,7 @@ int rc522_data_new(struct nfc_device * pnd, const struct rc522_io * io) {
|
|||||||
int rc522_read_reg(struct nfc_device * pnd, uint8_t reg) {
|
int rc522_read_reg(struct nfc_device * pnd, uint8_t reg) {
|
||||||
uint8_t val;
|
uint8_t val;
|
||||||
|
|
||||||
int ret = CHIP_DATA(pnd)->io->read(pnd, reg, &val, 1, RC522_TIMEOUT);
|
int ret = CHIP_DATA(pnd)->io->read(pnd, reg, &val, 1);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -72,11 +72,12 @@ int rc522_write_reg(struct nfc_device * pnd, uint8_t reg, uint8_t val, uint8_t m
|
|||||||
val = (val & mask) | (oldval & ~mask);
|
val = (val & mask) | (oldval & ~mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CHIP_DATA(pnd)->io->write(pnd, reg, &val, 1, RC522_TIMEOUT);
|
return CHIP_DATA(pnd)->io->write(pnd, reg, &val, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rc522_set_speed(struct nfc_device * pnd, nfc_baud_rate speed) {
|
int rc522_set_baud_rate(struct nfc_device * pnd, nfc_baud_rate speed) {
|
||||||
uint8_t txVal, rxVal;
|
uint8_t txVal, rxVal;
|
||||||
|
int ret;
|
||||||
|
|
||||||
switch (speed) {
|
switch (speed) {
|
||||||
case NBR_106:
|
case NBR_106:
|
||||||
@ -108,6 +109,23 @@ int rc522_set_speed(struct nfc_device * pnd, nfc_baud_rate speed) {
|
|||||||
rc522_write_reg(pnd, RC522_REG_RxModeReg, rxVal, RC522_REG_RxModeReg_RxSpeed_MASK);
|
rc522_write_reg(pnd, RC522_REG_RxModeReg, rxVal, RC522_REG_RxModeReg_RxSpeed_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int rc522_initiator_select_passive_target_ext(struct nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt, int timeout)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (nm.nmt != NMT_ISO14443A) {
|
||||||
|
return NFC_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = rc522_set_baud_rate(pnd, nm.nbr);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
return NFC_ENOTIMPL;
|
||||||
|
}
|
||||||
|
|
||||||
int rc522_get_supported_modulation(struct nfc_device * pnd, const nfc_mode mode, const nfc_modulation_type ** const supported_mt) {
|
int rc522_get_supported_modulation(struct nfc_device * pnd, const nfc_mode mode, const nfc_modulation_type ** const supported_mt) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case N_INITIATOR:
|
case N_INITIATOR:
|
||||||
@ -196,7 +214,7 @@ int rc522_set_property_bool(struct nfc_device * pnd, const nfc_property property
|
|||||||
return NFC_SUCCESS;
|
return NFC_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc522_set_speed(pnd, NBR_106);
|
return rc522_set_baud_rate(pnd, NBR_106);
|
||||||
|
|
||||||
case NP_AUTO_ISO14443_4:
|
case NP_AUTO_ISO14443_4:
|
||||||
case NP_ACCEPT_INVALID_FRAMES:
|
case NP_ACCEPT_INVALID_FRAMES:
|
||||||
@ -210,3 +228,14 @@ int rc522_set_property_bool(struct nfc_device * pnd, const nfc_property property
|
|||||||
|
|
||||||
return NFC_EINVARG;
|
return NFC_EINVARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int rc522_set_property_int(struct nfc_device * pnd, const nfc_property property, const int value) {
|
||||||
|
// TODO
|
||||||
|
return NFC_ENOTIMPL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rc522_idle(struct nfc_device * pnd) {
|
||||||
|
// Set idle and disable RX demodulator to save energy
|
||||||
|
return rc522_write_reg(pnd, RC522_REG_CommandReg, RC522_CMD_Idle | RC522_REG_CommandReg_PowerDown, 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,8 +23,8 @@
|
|||||||
#include <nfc/nfc-types.h>
|
#include <nfc/nfc-types.h>
|
||||||
|
|
||||||
struct rc522_io {
|
struct rc522_io {
|
||||||
int (*read)(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout);
|
int (*read)(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size);
|
||||||
int (*write)(struct nfc_device * pnd, uint8_t reg, const uint8_t * data, size_t size, unsigned int timeout);
|
int (*write)(struct nfc_device * pnd, uint8_t reg, const uint8_t * data, size_t size);
|
||||||
};
|
};
|
||||||
|
|
||||||
int rc522_data_new(struct nfc_device * pnd, const struct rc522_io * io);
|
int rc522_data_new(struct nfc_device * pnd, const struct rc522_io * io);
|
||||||
|
|||||||
@ -38,6 +38,7 @@
|
|||||||
|
|
||||||
#define RC522_UART_DEFAULT_SPEED 9600
|
#define RC522_UART_DEFAULT_SPEED 9600
|
||||||
#define RC522_UART_DRIVER_NAME "rc522_uart"
|
#define RC522_UART_DRIVER_NAME "rc522_uart"
|
||||||
|
#define RC522_UART_IO_TIMEOUT 50
|
||||||
|
|
||||||
#define LOG_CATEGORY "libnfc.driver.rc522_uart"
|
#define LOG_CATEGORY "libnfc.driver.rc522_uart"
|
||||||
#define LOG_GROUP NFC_LOG_GROUP_DRIVER
|
#define LOG_GROUP NFC_LOG_GROUP_DRIVER
|
||||||
@ -245,16 +246,16 @@ uint8_t rc522_uart_pack(int reg, int op) {
|
|||||||
return op << 7 | reg;
|
return op << 7 | reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout) {
|
int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size) {
|
||||||
uint8_t cmd = rc522_uart_pack(reg, READ);
|
uint8_t cmd = rc522_uart_pack(reg, READ);
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout);
|
pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, RC522_UART_IO_TIMEOUT);
|
||||||
if (pnd->last_error < 0) {
|
if (pnd->last_error < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
pnd->last_error = uart_receive(pnd->driver_data, data, 1, timeout);
|
pnd->last_error = uart_receive(pnd->driver_data, data, 1, RC522_UART_IO_TIMEOUT);
|
||||||
if (pnd->last_error < 0) {
|
if (pnd->last_error < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
@ -270,19 +271,19 @@ error:
|
|||||||
return pnd->last_error;
|
return pnd->last_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout) {
|
int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size) {
|
||||||
uint8_t cmd = rc522_uart_pack(reg, WRITE);
|
uint8_t cmd = rc522_uart_pack(reg, WRITE);
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
// First: send write request
|
// First: send write request
|
||||||
pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout);
|
pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, RC522_UART_IO_TIMEOUT);
|
||||||
if (pnd->last_error < 0) {
|
if (pnd->last_error < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second: wait for a reply
|
// Second: wait for a reply
|
||||||
uint8_t reply;
|
uint8_t reply;
|
||||||
pnd->last_error = uart_receive(pnd->driver_data, &reply, 1, timeout);
|
pnd->last_error = uart_receive(pnd->driver_data, &reply, 1, RC522_UART_IO_TIMEOUT);
|
||||||
if (pnd->last_error < 0) {
|
if (pnd->last_error < 0) {
|
||||||
return pnd->last_error;
|
return pnd->last_error;
|
||||||
}
|
}
|
||||||
@ -294,7 +295,7 @@ int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fourth: send register data
|
// Fourth: send register data
|
||||||
pnd->last_error = uart_send(pnd->driver_data, data, 1, timeout);
|
pnd->last_error = uart_send(pnd->driver_data, data, 1, RC522_UART_IO_TIMEOUT);
|
||||||
if (pnd->last_error < 0) {
|
if (pnd->last_error < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
@ -349,7 +350,7 @@ const struct nfc_driver rc522_uart_driver = {
|
|||||||
.get_supported_baud_rate = rc522_get_supported_baud_rate,
|
.get_supported_baud_rate = rc522_get_supported_baud_rate,
|
||||||
.device_get_information_about = rc522_get_information_about,
|
.device_get_information_about = rc522_get_information_about,
|
||||||
|
|
||||||
.abort_command = rc522_uart_abort_command,
|
.abort_command = rc522_idle,
|
||||||
.idle = rc522_idle,
|
.idle = rc522_idle,
|
||||||
.powerdown = rc522_softdown,
|
.powerdown = rc522_softdown,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user