Skip to content

Commit

Permalink
Property .spi.bus_is_initialized
Browse files Browse the repository at this point in the history
If user needs to just attach rc522 to the already
existing spi bus, this property in configuration
needs to be set to true to prevent bus initialization
in time of rc522 creation.

This resolves #9
  • Loading branch information
abobija committed Aug 25, 2022
1 parent d176f4d commit 47f9b1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 13 additions & 11 deletions rc522.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ static esp_err_t rc522_create_transport(rc522_handle_t rc522)

switch(rc522->config->transport) {
case RC522_TRANSPORT_SPI: {
spi_bus_config_t buscfg = {
.miso_io_num = rc522->config->spi.miso_gpio,
.mosi_io_num = rc522->config->spi.mosi_gpio,
.sclk_io_num = rc522->config->spi.sck_gpio,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};

spi_device_interface_config_t devcfg = {
.clock_speed_hz = rc522->config->spi.clock_speed_hz,
.mode = 0,
Expand All @@ -155,8 +147,18 @@ static esp_err_t rc522_create_transport(rc522_handle_t rc522)
.flags = rc522->config->spi.device_flags,
};

if(ESP_OK != (ret = spi_bus_initialize(rc522->config->spi.host, &buscfg, 0))) {
break;
if(! rc522->config->spi.bus_is_initialized) {
spi_bus_config_t buscfg = {
.miso_io_num = rc522->config->spi.miso_gpio,
.mosi_io_num = rc522->config->spi.mosi_gpio,
.sclk_io_num = rc522->config->spi.sck_gpio,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};

if(ESP_OK != (ret = spi_bus_initialize(rc522->config->spi.host, &buscfg, 0))) {
break;
}
}

ret = spi_bus_add_device(rc522->config->spi.host, &devcfg, &rc522->spi_handle);
Expand Down Expand Up @@ -543,7 +545,7 @@ static esp_err_t rc522_spi_receive(rc522_handle_t rc522, uint8_t* buffer, uint8_

esp_err_t ret;

if(SPI_DEVICE_HALFDUPLEX == (rc522->config->spi.device_flags & SPI_DEVICE_HALFDUPLEX)) {
if(SPI_DEVICE_HALFDUPLEX & rc522->config->spi.device_flags) {
ret = spi_device_transmit(rc522->spi_handle, &(spi_transaction_t){
.flags = SPI_TRANS_USE_TXDATA,
.length = 8,
Expand Down
7 changes: 7 additions & 0 deletions rc522.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ typedef struct {
int sda_gpio;
int clock_speed_hz;
uint32_t device_flags; /*<! Bitwise OR of SPI_DEVICE_* flags */
/**
* @brief Set to true if the bus is already initialized.
* NOTE: This property will be removed in future,
* once when https://github.com/espressif/esp-idf/issues/8745 is resolved
*
*/
bool bus_is_initialized;
} spi;
struct {
i2c_port_t port;
Expand Down

0 comments on commit 47f9b1c

Please sign in to comment.