thanhbinh89 / pn532-esp-idf Goto Github PK
View Code? Open in Web Editor NEWpn532 ESP-IDF
License: The Unlicense
pn532 ESP-IDF
License: The Unlicense
Hi @thanhbinh89!
I'm using your code and it's working wonderfully, thanks for this porting!
As I'm already using different peripherals and already initializing the SPI in the main thread I was thinking to improve the code and add the standard functions provided by idf to communicate with SPI.
Before starting I did some research and found this documentation https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf but seems that I'm not too good to understand it completely.
And after, a day of try&catch I didn't yet find how to correctly set the configuration of the SPI.
So the questions:
pn532_spi_write
and pn532_spi_read
) were written in that form because they are not compatible with the standard idf functions for SPI or 3 years ago there were no such functions out of the box?As you have written these base functions, I'm pretty sure that you have really more understanding of me on what configuration is needed here. Is possible that you give me your opinion on that?
This is the code that I have written.
I also try a lot of flags to check if there is something that will change, but no.
The response from pn532_spi_read is always 0x00. So no good.
void pn532_spi_init(pn532_t *obj, spi_host_device_t spiHostDevice, uint8_t ss) {
gpio_set_direction((gpio_num_t) ss, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t) ss, 1);
//Config Frequency and SS GPIO
spi_device_interface_config_t deviceInterfaceConfig = {
.mode=0, //SPI mode 0
.clock_speed_hz= 2000 * 1000, // Uusually 1.2 Mhz
.input_delay_ns = 0,
.spics_io_num = ss,
.flags = SPI_DEVICE_BIT_LSBFIRST,
.queue_size = 1
};
spi_device_handle_t spiDeviceHandle;
//Attach the EPD to the SPI bus
ESP_ERROR_CHECK(spi_bus_add_device(spiHostDevice, &deviceInterfaceConfig, &spiDeviceHandle));
obj->spi = spiDeviceHandle;
obj->_ss = ss;
}
void pn532_spi_write(pn532_t *obj, uint8_t data) {
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length = 8;
t.tx_buffer = &data;
t.flags = (SPI_TRANS_CS_KEEP_ACTIVE); // instead of gpio_set_level(obj->_ss, 0); and gpio_set_level(obj->_ss, 1); I'm using spi_device_acquire_bus(obj->spi,portMAX_DELAY); and spi_device_release_bus(obj->spi);.
ESP_LOGI(TAG, "send data: 0x%02x", data);
ESP_ERROR_CHECK(spi_device_polling_transmit(obj->spi, &t));
}
uint8_t pn532_spi_read(pn532_t *obj) {
uint8_t data = 1;
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length = 8;
t.rxlength = 8;
t.tx_buffer = NULL;
t.rx_buffer = &data;
t.flags = (SPI_TRANS_CS_KEEP_ACTIVE); // instead of gpio_set_level(obj->_ss, 0); and gpio_set_level(obj->_ss, 1); I'm using spi_device_acquire_bus(obj->spi,portMAX_DELAY); and spi_device_release_bus(obj->spi);.
ESP_ERROR_CHECK(spi_device_polling_transmit(obj->spi, &t));
ESP_LOGI(TAG, "read data: 0x%02x", data);
return data;
}
Hope to listen from you soon! :)
How do I change the code to support more than one pn532 device on the same device?
diff --git a/components/pn532/pn532.c b/components/pn532/pn532.c
index 82d5165..f20d25a 100644
--- a/components/pn532/pn532.c
+++ b/components/pn532/pn532.c
@@ -1313,7 +1313,7 @@ void pn532_readdata(pn532_t *obj, uint8_t *buff, uint8_t n)
PN532_DEBUG("Reading:");
for (uint8_t i = 0; i < n; i++)
{
- PN532_DELAY(10);
+ PN532_DELAY(5);
buff[i] = pn532_spi_read(obj);
}
for (int i = 0; i < n; i++)
With 10 ticks it creates communication issues and unable to authenticate to read the blocks. By reducing to 5 ticks it works as expected.
Hi,
nice work porting the Adafruit library to IDF. As I'm porting a Arduino/PlatformIO project to IDF it could help me a lot. However, your project currently has no license, so it cannot be used by others, see https://choosealicense.com/no-permission/.
Could we use it in our project? Since the original Adafruit library uses a BSD license, maybe you could add this as well? See, https://help.github.com/en/github/building-a-strong-community/adding-a-license-to-a-repository.
Thank you!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.