1.基于VSCODE的开发环境搭建
1.1 点击下载的链接 : ESP-IDF 1.2.开发环境构建 :vscode 1.3环境搭建参考链接:链接: 环境搭建教程
2.创建工程
参考环境搭建,将测试工程拷贝到自定义开发环境中,并打开ESP-IDF 4.4 PowerShell使用idf.py fullclean运行一次,清除配置文件。并使用idf.py build 对整个工程做一次构建。(PS:可以在Vscode 中完成)。
3.编写cmake 添加自定义文件夹
file(GLOB_RECURSE SOURCES nfc_sdk/*.c ) //填写实际对应的子文件夹名称
set(include_dirs
nfc_sdk //填写实际对应的子文件夹名称
)
idf_component_register(SRCS ${SOURCES} "main.c"
INCLUDE_DIRS ${include_dirs})
4. SPI 配置与使用
4.1 SPI初始化
spi_device_handle_t nfc_spi;
unsigned char Spi_init(void)
{
unsigned char sta = 0;
esp_err_t ret = 0;
spi_bus_config_t buscfg = {
.miso_io_num = PIN_NUM_MISO,
.mosi_io_num = PIN_NUM_MOSI,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 1024,
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = SPI_MASTER_FREQ_5M,
.mode = 0,
.spics_io_num = PIN_NUM_CS,
.queue_size = 7,
};
spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH2);
ESP_ERROR_CHECK(ret);
ret = spi_bus_add_device(SPI2_HOST, &devcfg, &nfc_spi);
ESP_ERROR_CHECK(ret);
return sta;
}
4.2 SPI传输发送
esp_err_t spi_write(spi_device_handle_t spi, uint8_t *data, uint8_t len)
{
esp_err_t ret;
spi_transaction_t t;
if (len == 0)
return;
memset(&t, 0, sizeof(t));
t.length = len * 8;
t.tx_buffer = data;
t.user = (void *)0;
ret = spi_device_polling_transmit(spi, &t);
assert(ret == ESP_OK);
return ret;
}
4.3 SPI传输接收
esp_err_t spi_read(spi_device_handle_t spi, uint8_t *data)
{
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 8;
t.flags = SPI_TRANS_USE_RXDATA;
t.user = (void *)1;
esp_err_t ret = spi_device_polling_transmit(spi, &t);
assert(ret == ESP_OK);
*data = t.rx_data[0];
return ret;
}
4.4 SPI特殊应用参考配置
void WRITEREG(unsigned char addr, unsigned char data)
{
unsigned char flag = 0, ucbuf[3] = {0};
esp_err_t ret;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.user = (void *)1;
gpio_set_level(PIN_NUM_CS, 1);
if (addr & 0x80)
{
flag = 1;
ucbuf[0] = ((0x00 << 1) & 0xFE);
ucbuf[1] = 0x01;
t.tx_buffer = &ucbuf;
t.length = 2 * 8;
ret = spi_device_polling_transmit(nfc_spi, &t);
assert(ret == ESP_OK);
}
ucbuf[0] = ((addr << 1) & 0xFE);
ucbuf[1] = data;
t.tx_buffer = &ucbuf;
t.length = 2 * 8;
ret = spi_device_polling_transmit(nfc_spi, &t);
if (flag)
{
ucbuf[0] = ((0x00 << 1) & 0xFE);
ucbuf[1] = 0x00;
t.tx_buffer = &ucbuf;
t.length = 2 * 8;
ret = spi_device_polling_transmit(nfc_spi, &t);
}
return ret;
}
unsigned char READREG(unsigned char addr)
{
unsigned char reg = 0xFF, ucbuf[3] = {0};
unsigned char flag = 0;
esp_err_t ret;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.user = (void *)1;
if (addr & 0x80)
{
flag = 1;
ucbuf[0] = ((0x00 << 1) & 0xFE);
ucbuf[1] = 0x01;
t.tx_buffer = &ucbuf;
t.length = 2 * 8;
ret = spi_device_polling_transmit(nfc_spi, &t);
}
ucbuf[0] = (((addr << 1) & 0xFE) | 0x01);
t.flags = SPI_TRANS_USE_RXDATA;
t.length = 2 * 8;
t.tx_buffer = &ucbuf;
ret = spi_device_polling_transmit(nfc_spi, &t);
assert(ret == ESP_OK);
reg = t.rx_data[1];
if (flag)
{
ucbuf[0] = ((0x00 << 1) & 0xFE);
ucbuf[1] = 0x00;
t.tx_buffer = &ucbuf;
t.length = 2 * 8;
ret = spi_device_polling_transmit(nfc_spi, &t);
}
return reg;
}
5 基于SPI通讯测试NFC模块
void app_main(void){
Spi_init();
while(1){
LOG_printf("[sy_test_task_handle]0x68 = 0x%x ", READREG(0x68));
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
|