【Esp32】Esp32+sx1268 Spi接口驱动SX1268模块

文章介绍了如何使用ESP32的SPI3接口驱动SX1268LoRa模块,实现数据的收发。在两个ESP32模块间进行PingPong通信,距离可达10米,但快速传输可能导致丢包。文章详细解析了SPI时序、封装的读写函数,并提供了LoRa参数介绍。此外,还提到了结合MQTT和智能监控平台的应用。
摘要由CSDN通过智能技术生成

日期: 2023-01-13
项目需要用乐鑫的 ESP32开发板驱动 Lora 模块,用的是  深圳SX1268ZTR4-GC的lora模块,
由于网上的参考资料大部分都是基于STM32驱动的,
本文实现了基于ESP32的SPI3接口驱动 sx1268 模块实现数据的收发。
用两个 esp32 + sx1268 模块的形式

关于lora数据通信+mqtt+智能监控平台的接入,参考文章:【Esp32】Esp32+sx1268+mqtt+智能监控平台

0. 实验结果

实现2个模块的数据收发,
实测距离10m左右,发数据太快会存在丢包现象
请添加图片描述

1.原理解析

【PingPong系统设计需求】: 将LoRa终端定义成两种角色:Master(主机)和Slave(从机)

  • Master主动发送PING数据,接收PANG数据
  • Slave如果接收到PING数据,回应PANG数据
  • 终端在LCD屏幕上显示终端类型及收发数据包个数

【PingPong通信机制:】
在这里插入图片描述

2.硬件接线

在这里插入图片描述

在这里插入图片描述

3. SX1268 模块的 SPI 时序分析

开始传输数据时,拉低NSS引脚(spi协议中的CS引脚),传输完之后,拉高引脚
Spi:模式0,全双工通信

在这里插入图片描述

4. 基于 ESP32 实现函数封装

封装读写字节函数:

bool spi_read_byte(uint8_t* rx_data, uint8_t* tx_data, size_t DataLength )
bool spi_write_byte(uint8_t* tx_data, size_t DataLength )

虽然叫 read_byte 但是可以当做读写使用。
当写字节时,rx_data = NULL;即可
当读字节时,先发送一个tx_data,再将读到的字节存到 rx_data中。为什么这么封装呢?

封装传输字节的函数:

uint8_t spi_transfer(uint8_t address)

当写字节时,参数address 表示写的内容,此时,函数的返回值无意义
当读字节时,address = 0,返回值表示,读取到的字

4.1 读寄存器

在这里插入图片描述

解释:NOP指的是空操作指令,相当于主机执行一次 write ,但是write的字节为0

读寄存器:比如读寄存器 addr 开始的2个字节,

spi_transfer(cmd)
spi_transfer(addr高四位)
spi_transfer(addr低四位)
spi_transfer(0)

rx_data[0] = spi_transfer(0);
rx_data[1] = spi_transfer(0);

4.2 写寄存器

在这里插入图片描述

spi_transfer(cmd)
spi_transfer(addr高四位)
spi_transfer(addr低四位)
spi_transfer(data[n]);     // n = 0……x

5. LoRa参数介绍

在这里插入图片描述
参考一篇大佬的资料:【物联网之LoRa开发与应用四(LoRaPingPang系统设计)】

若有错误,希望提出批评指教,在评论区留言或者私聊均可。

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
The aim of this project is to show an example of the endpoint LoRaWAN stack implementation. This project has 3 active branches in place. The master branch which provides the latest released source code (v4.4.2), the develop branch which provides the current source code development status to be released next (Milestone 4.4.3) and the feature/5.0.0 branch which provides a preview of the current source code development status for LoRaWAN Specification v1.1 specification.(Milestone 5.0.0) The master branch implementation is based on LoRaWAN Specification v1.0.3 and LoRaWAN Regional Parameters v1.0.3revA specifications. ClassA, ClassB and ClassC end-device classes are fully implemented. The develop branch implementation is based on LoRaWAN Specification v1.0.3 and LoRaWAN Regional Parameters v1.0.3revA specifications. ClassA, ClassB and ClassC end-device classes are fully implemented. The feature/5.0.0 branch implementation is based on LoRaWAN Specification v1.1 and LoRaWAN Regional Parameters v1.1rB specifications. ClassA, ClassB and ClassC end-device classes are fully implemented. This project also provides SX1272/73, SX1276/77/78/79 and SX1261/2 radio drivers. For each currently supported platform example applications are provided. LoRaMac/classA: ClassA end-device example application. LoRaMac/classB: ClassB end-device example application. LoRaMac/classC: ClassC end-device example application. LoRaMac/fuota-test-01: FUOTA test scenario 01 end-device example application. (Based on provided application common packages) LoRaMac/periodic-uplink-lpp: ClassA/B/C end-device example application. Periodically uplinks a frame using the Cayenne LPP protocol. (Based on provided application common packages) ping-pong: Point to point RF link example application. rx-sensi: Example application useful to measure the radio sensitivity level using an RF generator. tx-cw: Example application to show how to generate an RF Continuous Wave transmission. Note: Each LoRaWAN application example (LoRaMac/classX) includes an implementation of the LoRa-Alliance; LoRaWAN certification protocol. Note: The LoRaWAN stack API documentation can be found at: http://stackforce.github.io/LoRaMac-doc/
以下是ESP32-IDF中使用SX1268驱动代码示例: 1. 首先,需要在项目中引入SX1268驱动库。可以使用SPI接口进行通信,因此需要引入ESP32SPI驱动库。 ```c #include "sx126x/sx126x.h" #include "driver/spi_master.h" ``` 2. 定义SPI总线的配置参数。 ```c spi_device_interface_config_t spi_device_config = { .mode = 0, .clock_speed_hz = 4000000, .spics_io_num = GPIO_NUM_5, .queue_size = 1, .pre_cb = NULL, .post_cb = NULL, .flags = SPI_DEVICE_NO_DUMMY, }; ``` 其中,`mode`表示SPI总线模式,`clock_speed_hz`表示时钟速率,`spics_io_num`表示片选引脚,`queue_size`表示SPI传输队列长度。 3. 初始化SPI总线,并初始化SX1268模块。 ```c spi_bus_config_t spi_bus_config = { .miso_io_num = GPIO_NUM_19, .mosi_io_num = GPIO_NUM_23, .sclk_io_num = GPIO_NUM_18, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 0, }; spi_bus_initialize(VSPI_HOST, &spi_bus_config, 1); spi_device_handle_t spi_device_handle; spi_bus_add_device(VSPI_HOST, &spi_device_config, &spi_device_handle); sx126x_init(&spi_device_handle, GPIO_NUM_33, GPIO_NUM_32, GPIO_NUM_34, GPIO_NUM_35); ``` 其中,`miso_io_num`、`mosi_io_num`、`sclk_io_num`分别表示SPI总线的MISO、MOSI和时钟引脚,`quadwp_io_num`和`quadhd_io_num`表示Quad-SPI模式下的两个引脚(此处不需要使用),`max_transfer_sz`表示最大传输长度。`sx126x_init`函数中的四个参数分别表示SX1268模块的NSS、BUSY、DIO1和RESET引脚。 4. 设置SX1268模块的参数。 ```c sx126x_set_regulator_mode(SX126X_DCDC_MODE_AUTO); sx126x_set_buffer_base_address(0, 0); sx126x_set_tx_params(13, SX126X_RAMP_200_US); sx126x_set_rx_params(SX126X_RX_MODE_CONTINUOUS, 0, 10.0, SX126X_LORA_BW_125, SX126X_SF6, SX126X_CRC_2_BYTES_CCIT, SX126X_LORA_CR_4_5); ``` 这里设置了DCDC电源模式、缓冲区基地址、发送参数和接收参数。 5. 发送数据 ```c uint8_t tx_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05}; sx126x_set_dio_irq_params(SX126X_IRQ_TX_DONE | SX126X_IRQ_TIMEOUT, SX126X_IRQ_TX_DONE | SX126X_IRQ_TIMEOUT, 0, 0); sx126x_send(tx_buffer, sizeof(tx_buffer)); ``` 这里先定义了一个要发送的数据包tx_buffer,然后设置了DIO1中断参数(在发送完成或发送超时时触发中断),最后调用sx126x_send函数发送数据。 6. 接收数据 ```c uint8_t rx_buffer[64]; sx126x_set_dio_irq_params(SX126X_IRQ_RX_DONE | SX126X_IRQ_TIMEOUT, SX126X_IRQ_RX_DONE | SX126X_IRQ_TIMEOUT, 0, 0); sx126x_set_rx(0); sx126x_get_rx_buffer_status(NULL, NULL, NULL, NULL, rx_buffer); ``` 这里设置了DIO1中断参数(在接收完成或接收超时时触发中断),然后调用sx126x_set_rx函数开启接收模式。最后调用sx126x_get_rx_buffer_status函数获取接收到的数据。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积跬步、至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值