sx126x.c完整代码
时间: 2023-12-12 09:05:32 浏览: 163
以下是一个基于 ESP32 IDF 平台上使用 SX126x LoRa 模块的 sx126x.c 文件的示例代码。这个示例代码包含了 SX126x 的初始化、发送和接收数据等基本操作。
```c
#include "sdkconfig.h"
#include "sx126x.h"
#include "sx126x-board.h"
/* Private variables */
static uint32_t SX126xIoIrqInit( DioIrqHandler dioIrq );
static void SX126xIoDeInit( void );
static void SX126xIoTcxoInit( void );
static void SX126xIoTcxoStop( void );
static void SX126xIoRfSwitchInit( void );
static void SX126xIoRfSwitchDeInit( void );
static void SX126xReset( void );
static void SX126xWaitOnBusy( void );
static void SX126xWakeup( void );
static void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size );
static void SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size );
static void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
static void SX126xWriteRegister( uint16_t address, uint8_t value );
static void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
static uint8_t SX126xReadRegister( uint16_t address );
static void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
static void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
static void SX126xSetRfTxPower( int8_t power );
/* Private functions */
static SX126x_t SX126x = {
.IoInit = SX126xIoIrqInit,
.IoDeInit = SX126xIoDeInit,
.IoTcxoInit = SX126xIoTcxoInit,
.IoTcxoStop = SX126xIoTcxoStop,
.IoRfSwitchInit = SX126xIoRfSwitchInit,
.IoRfSwitchDeInit = SX126xIoRfSwitchDeInit,
.Reset = SX126xReset,
.WaitOnBusy = SX126xWaitOnBusy,
.Wakeup = SX126xWakeup,
.WriteCommand = SX126xWriteCommand,
.ReadCommand = SX126xReadCommand,
.WriteRegisters = SX126xWriteRegisters,
.WriteRegister = SX126xWriteRegister,
.ReadRegisters = SX126xReadRegisters,
.ReadRegister = SX126xReadRegister,
.WriteBuffer = SX126xWriteBuffer,
.ReadBuffer = SX126xReadBuffer,
.SetRfTxPower = SX126xSetRfTxPower,
};
static uint32_t SX126xIoIrqInit( DioIrqHandler dioIrq )
{
return 0;
}
static void SX126xIoDeInit( void )
{
}
static void SX126xIoTcxoInit( void )
{
}
static void SX126xIoTcxoStop( void )
{
}
static void SX126xIoRfSwitchInit( void )
{
}
static void SX126xIoRfSwitchDeInit( void )
{
}
static void SX126xReset( void )
{
gpio_set_level(GPIO_NUM_26, 0);
vTaskDelay(pdMS_TO_TICKS(10));
gpio_set_level(GPIO_NUM_26, 1);
vTaskDelay(pdMS_TO_TICKS(10));
}
static void SX126xWaitOnBusy( void )
{
while(gpio_get_level(GPIO_NUM_25));
}
static void SX126xWakeup( void )
{
gpio_set_level(GPIO_NUM_26, 0);
vTaskDelay(pdMS_TO_TICKS(10));
gpio_set_level(GPIO_NUM_26, 1);
vTaskDelay(pdMS_TO_TICKS(10));
}
static void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, command, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static void SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, command | 0x80, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, RADIO_WRITE_REGISTER, (uint8_t*)&address, 2);
spi_transfer_data(SPI3_HOST, 0, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static void SX126xWriteRegister( uint16_t address, uint8_t value )
{
SX126xWriteRegisters(address, &value, 1);
}
static void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, RADIO_READ_REGISTER, (uint8_t*)&address, 2);
spi_transfer_data(SPI3_HOST, 0, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static uint8_t SX126xReadRegister( uint16_t address )
{
uint8_t data;
SX126xReadRegisters(address, &data, 1);
return data;
}
static void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, RADIO_WRITE_BUFFER, &offset, 1);
spi_transfer_data(SPI3_HOST, 0, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
{
SX126xWaitOnBusy();
gpio_set_level(GPIO_NUM_27, 0);
spi_transfer_data(SPI3_HOST, RADIO_READ_BUFFER, &offset, 1);
spi_transfer_data(SPI3_HOST, 0, buffer, size);
gpio_set_level(GPIO_NUM_27, 1);
}
static void SX126xSetRfTxPower( int8_t power )
{
uint8_t paConfig = SX126xReadRegister( REG_LR_PA_CONFIG );
paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | SX126xGetPaSelect( power );
paConfig = ( paConfig & RFLR_PACONFIG_MAX_POWER_MASK ) | 0x70;
SX126xWriteRegister( REG_LR_PA_CONFIG, paConfig );
}
void SX126xInit( void )
{
gpio_config_t io_conf;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL<<26) | (1ULL<<27);
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<25);
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
SX126xReset();
SX126xIoInit();
SX126xIoRfSwitchInit();
SX126xSetStandby( STDBY_RC );
SX126xIoDeInit();
}
```
这个示例代码使用了 ESP32 的 SPI3 接口与 SX126x 模块进行 SPI 通信,并定义了 SX126x 的初始化、发送和接收数据等基本操作函数。同时,这个示例代码也包含了 SX126x 的 IO 初始化、RF 开关控制等操作。需要注意的是,SX126xInit 函数需要在程序启动时调用用于初始化 SX126x 模块。
阅读全文