Fixed MFRC522 UART driver

This commit is contained in:
Marcos Vives Del Sol 2015-06-12 20:46:57 +02:00
parent 6b4ba0dbf3
commit 96839468e8

View File

@ -249,7 +249,7 @@ int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t
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, sizeof(cmd), timeout); pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout);
if (pnd->last_error < 0) { if (pnd->last_error < 0) {
goto error; goto error;
} }
@ -263,7 +263,7 @@ int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t
data++; data++;
} }
return 0; return NFC_SUCCESS;
error: error:
uart_flush_input(DRIVER_DATA(pnd)->port, true); uart_flush_input(DRIVER_DATA(pnd)->port, true);
@ -271,21 +271,43 @@ 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, unsigned int timeout) {
uint8_t cmd[2]; uint8_t cmd = rc522_uart_pack(reg, WRITE);
cmd[0] = rc522_uart_pack(reg, WRITE);
while (size > 0) { while (size > 0) {
cmd[1] = *data; // First: send write request
pnd->last_error = uart_send(pnd->driver_data, &cmd, sizeof(cmd), timeout); pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout);
if (pnd->last_error < 0) {
goto error;
}
// Second: wait for a reply
uint8_t reply;
pnd->last_error = uart_receive(pnd->driver_data, &reply, 1, timeout);
if (pnd->last_error < 0) { if (pnd->last_error < 0) {
return pnd->last_error; return pnd->last_error;
} }
// Third: compare sent and received. They must match.
if (cmd != reply) {
pnd->last_error = NFC_EIO;
goto error;
}
// Fourth: send register data
pnd->last_error = uart_send(pnd->driver_data, data, 1, timeout);
if (pnd->last_error < 0) {
goto error;
}
size--; size--;
data++; data++;
} }
return 0; return NFC_SUCCESS;
error:
uart_flush_input(DRIVER_DATA(pnd)->port, true);
return pnd->last_error;
} }
const struct rc522_io rc522_uart_io = { const struct rc522_io rc522_uart_io = {