Skip to content
Closed
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
8 changes: 8 additions & 0 deletions include/driver/rc522_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ typedef struct
* Set to -1 if the RST pin is not connected.
*/
gpio_num_t rst_io_num;
/**
* GPIO number of the RC522 nCS pin.
* This is used if you have more than 3 devices on your SPI bus (typically SPI3).
* If you wish to use this parameter, you must set `dev_config.spics_io_num`
* to -1. For backwards compatibility, this parameter is ignored if you have set
* `dev_config.spics_io_num` to any other value than -1.
*/
gpio_num_t ncs_io_num;
} rc522_spi_config_t;

esp_err_t rc522_spi_create(const rc522_spi_config_t *config, rc522_driver_handle_t *driver);
Expand Down
1 change: 1 addition & 0 deletions internal/rc522_driver_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "rc522_types_internal.h"
#include "rc522_driver.h"

#define RC522_DRIVER_NCS_PIN_SELECT (0)
#define RC522_DRIVER_HARD_RST_PIN_PWR_DOWN_LEVEL (0)
#define RC522_DRIVER_HARD_RST_PULSE_DURATION_MS (15)

Expand Down
9 changes: 9 additions & 0 deletions internal/rc522_types_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ typedef struct
} \
while (0)

#define RC522_LOG_ON_ERROR(x) \
do { \
esp_err_t err_rc_ = (x); \
if (unlikely(err_rc_ != ESP_OK)) { \
ESP_LOGE(TAG, "%s(%d): %04" RC522_X "", __FUNCTION__, __LINE__, err_rc_); \
} \
} \
while (0)

#define RC522_RETURN_ON_FALSE(a, err_code) ESP_RETURN_ON_FALSE(a, err_code, TAG, "")
#define RC522_CHECK_WITH_MESSAGE(a, message) ESP_RETURN_ON_FALSE(!(a), ESP_ERR_INVALID_ARG, TAG, message)
#define RC522_CHECK_AND_RETURN(a, ret_val) ESP_RETURN_ON_FALSE(!(a), ret_val, TAG, #a)
Expand Down
24 changes: 24 additions & 0 deletions src/driver/rc522_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

RC522_LOG_DEFINE_BASE();

static void rc522_spi_transaction_pre_cb(spi_transaction_t *trans);
static void rc522_spi_transaction_post_cb(spi_transaction_t *trans);

static esp_err_t rc522_spi_install(const rc522_driver_handle_t driver)
{
RC522_CHECK(driver == NULL);
Expand Down Expand Up @@ -44,6 +47,13 @@ static esp_err_t rc522_spi_install(const rc522_driver_handle_t driver)
conf->dev_config.dummy_bits = 1;
// }}

if (conf->dev_config.spics_io_num == -1) {
RC522_CHECK(conf->ncs_io_num == -1);
conf->dev_config.spics_io_num = -1;
conf->dev_config.pre_cb = &rc522_spi_transaction_pre_cb;
conf->dev_config.post_cb = &rc522_spi_transaction_post_cb;
}

RC522_RETURN_ON_ERROR(
spi_bus_add_device(conf->host_id, &conf->dev_config, (spi_device_handle_t *)(&driver->device)));

Expand All @@ -66,6 +76,7 @@ static esp_err_t rc522_spi_send(const rc522_driver_handle_t driver, uint8_t addr
.addr = address,
.length = 8 * bytes->length,
.tx_buffer = bytes->ptr,
.user = (void *)driver->config,
});

return ret;
Expand All @@ -87,6 +98,7 @@ static esp_err_t rc522_spi_receive(const rc522_driver_handle_t driver, uint8_t a
.addr = address,
.rxlength = 8,
.rx_buffer = (bytes->ptr + i),
.user = (void *)driver->config,
}));
}

Expand Down Expand Up @@ -147,3 +159,15 @@ esp_err_t rc522_spi_create(const rc522_spi_config_t *config, rc522_driver_handle

return ESP_OK;
}

static void rc522_spi_transaction_pre_cb(spi_transaction_t *trans)
{
rc522_spi_config_t *conf = (rc522_spi_config_t *)trans->user;
RC522_LOG_ON_ERROR(gpio_set_level(conf->ncs_io_num, RC522_DRIVER_NCS_PIN_SELECT));
}

static void rc522_spi_transaction_post_cb(spi_transaction_t *trans)
{
rc522_spi_config_t *conf = (rc522_spi_config_t *)trans->user;
RC522_LOG_ON_ERROR(gpio_set_level(conf->ncs_io_num, !RC522_DRIVER_NCS_PIN_SELECT));
}
18 changes: 18 additions & 0 deletions src/rc522_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

RC522_LOG_DEFINE_BASE();

esp_err_t rc522_driver_init_ncs_pin(gpio_num_t rst_io_num)
{
RC522_CHECK(rst_io_num < 0);

gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = (1ULL << rst_io_num),
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_DISABLE,
};

RC522_RETURN_ON_ERROR(gpio_config(&io_conf));
RC522_RETURN_ON_ERROR(gpio_set_level(rst_io_num, !RC522_DRIVER_NCS_PIN_SELECT));

return ESP_OK;
}

esp_err_t rc522_driver_init_rst_pin(gpio_num_t rst_io_num)
{
RC522_CHECK(rst_io_num < 0);
Expand Down