Skip to content

Commit

Permalink
Introduced custom type for crc result (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
abobija authored Sep 22, 2024
1 parent fb2c9db commit 949af61
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.1.4"
version: "3.1.5"
description: "Library for communication with RFID / NFC cards using MFRC522 module"
url: "https://github.com/abobija/esp-idf-rc522"
repository: "https://github.com/abobija/esp-idf-rc522.git"
Expand Down
12 changes: 11 additions & 1 deletion private_include/rc522_pcd_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,19 @@ typedef enum
RC522_PCD_FIRMWARE_COUNTERFEIT = 0x12, // counterfeit chip
} rc522_pcd_firmware_t;

typedef union
{
uint16_t value;
struct
{
uint8_t lsb;
uint8_t msb;
};
} rc522_pcd_crc_t;

esp_err_t rc522_pcd_reset(const rc522_handle_t rc522, uint32_t timeout_ms);

esp_err_t rc522_pcd_calculate_crc(const rc522_handle_t rc522, const rc522_bytes_t *bytes, uint16_t *result);
esp_err_t rc522_pcd_calculate_crc(const rc522_handle_t rc522, const rc522_bytes_t *bytes, rc522_pcd_crc_t *result);

esp_err_t rc522_pcd_init(const rc522_handle_t rc522);

Expand Down
12 changes: 6 additions & 6 deletions src/picc/rc522_mifare.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ esp_err_t rc522_mifare_read(
buffer_clone[1] = block_address;

// Calculate CRC_A
uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = buffer_clone, .length = 2 }, &crc));

buffer_clone[2] = crc & 0xFF;
buffer_clone[3] = crc >> 8;
buffer_clone[2] = crc.lsb;
buffer_clone[3] = crc.msb;

// Transmit the buffer and receive the response, validate CRC_A.
uint8_t _ = sizeof(buffer_clone);
Expand All @@ -187,12 +187,12 @@ static esp_err_t rc522_mifare_send(
// Copy sendData[] to cmdBuffer[] and add CRC_A
memcpy(cmd_buffer, send_data, send_length);

uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(
rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = cmd_buffer, .length = send_length }, &crc));

cmd_buffer[send_length] = crc & 0xFF;
cmd_buffer[send_length + 1] = crc >> 8;
cmd_buffer[send_length] = crc.lsb;
cmd_buffer[send_length + 1] = crc.msb;

send_length += 2;

Expand Down
16 changes: 8 additions & 8 deletions src/rc522_pcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RC522_LOG_DEFINE_BASE();
/**
* @see https://stackoverflow.com/a/48705557
*/
esp_err_t rc522_pcd_calculate_crc(const rc522_handle_t rc522, const rc522_bytes_t *bytes, uint16_t *result)
esp_err_t rc522_pcd_calculate_crc(const rc522_handle_t rc522, const rc522_bytes_t *bytes, rc522_pcd_crc_t *result)
{
RC522_CHECK(rc522 == NULL);
RC522_CHECK_BYTES(bytes);
Expand Down Expand Up @@ -44,20 +44,20 @@ esp_err_t rc522_pcd_calculate_crc(const rc522_handle_t rc522, const rc522_bytes_
return ESP_ERR_TIMEOUT;
}

uint8_t msb;
uint8_t lsb;
RC522_RETURN_ON_ERROR(rc522_pcd_stop_active_command(rc522));
RC522_RETURN_ON_ERROR(rc522_pcd_read(rc522, RC522_PCD_CRC_RESULT_MSB_REG, &msb));
RC522_RETURN_ON_ERROR(rc522_pcd_read(rc522, RC522_PCD_CRC_RESULT_LSB_REG, &lsb));
rc522_pcd_crc_t crc = { 0 };

*result = (msb << 8) | lsb;
RC522_RETURN_ON_ERROR(rc522_pcd_stop_active_command(rc522));
RC522_RETURN_ON_ERROR(rc522_pcd_read(rc522, RC522_PCD_CRC_RESULT_MSB_REG, &crc.msb));
RC522_RETURN_ON_ERROR(rc522_pcd_read(rc522, RC522_PCD_CRC_RESULT_LSB_REG, &crc.lsb));

if (RC522_LOG_LEVEL >= ESP_LOG_DEBUG) {
char debug_buffer[64];
rc522_buffer_to_hex_str(bytes->ptr, bytes->length, debug_buffer, sizeof(debug_buffer));
RC522_LOGD("crc(%s) = 0x%04" RC522_X, debug_buffer, *result);
RC522_LOGD("crc(%s) = 0x%04" RC522_X, debug_buffer, crc.value);
}

*result = crc;

return ESP_OK;
}

Expand Down
22 changes: 11 additions & 11 deletions src/rc522_picc.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ esp_err_t rc522_picc_comm(const rc522_handle_t rc522, rc522_pcd_command_t comman
}

// Verify CRC_A
uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(
rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = back_data, .length = *back_data_len - 2 }, &crc));

if ((back_data[*back_data_len - 2] != (crc & 0xFF)) || (back_data[*back_data_len - 1] != (crc >> 8))) {
if ((back_data[*back_data_len - 2] != crc.lsb) || (back_data[*back_data_len - 1] != crc.msb)) {
return RC522_ERR_CRC_WRONG;
}
}
Expand Down Expand Up @@ -421,12 +421,12 @@ esp_err_t rc522_picc_select(const rc522_handle_t rc522, rc522_picc_uid_t *out_ui
buffer[6] = buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5];
// Calculate CRC_A

uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(
rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = buffer, .length = 7 }, &crc));

buffer[7] = crc & 0xFF;
buffer[8] = crc >> 8;
buffer[7] = crc.lsb;
buffer[8] = crc.msb;

tx_last_bits = 0; // 0 => All 8 bits are valid.
buffer_used = 9;
Expand Down Expand Up @@ -544,12 +544,12 @@ esp_err_t rc522_picc_select(const rc522_handle_t rc522, rc522_picc_uid_t *out_ui
// compiler complains about uninitialized response_buffer even is
// no chance that response_buffer is NULL here, so ignore warning here
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(
rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = response_buffer, .length = 1 }, &crc));

buffer[2] = crc & 0xFF;
buffer[3] = crc >> 8;
buffer[2] = crc.lsb;
buffer[3] = crc.msb;

if ((buffer[2] != response_buffer[1]) || (buffer[3] != response_buffer[2])) {
RC522_LOGD("crc wrong");
Expand Down Expand Up @@ -679,11 +679,11 @@ esp_err_t rc522_picc_halta(const rc522_handle_t rc522, rc522_picc_t *picc)
buffer[0] = RC522_PICC_CMD_HLTA;
buffer[1] = 0;
// Calculate CRC_A
uint16_t crc = 0;
rc522_pcd_crc_t crc = { 0 };
RC522_RETURN_ON_ERROR(rc522_pcd_calculate_crc(rc522, &(rc522_bytes_t) { .ptr = buffer, .length = 2 }, &crc));

buffer[2] = crc & 0xFF;
buffer[3] = crc >> 8;
buffer[2] = crc.lsb;
buffer[3] = crc.msb;

// Send the command.
// The standard says:
Expand Down

0 comments on commit 949af61

Please sign in to comment.