目錄結構:
SX1278
|-- include
| |-- fifo.h
| |-- lora.h
| |-- platform.h
| |-- radio.h
| |-- spi.h
| |-- sx1276.h
| |-- sx1276Fsk.h
| |-- sx1276FskMisc.h
| |-- sx1276Hal.h
| |-- sx1276LoRa.h
| -- sx1276LoRaMisc.h
– src
|-- fifo.c
|-- lora.c
|-- radio.c
|-- spi.c
|-- sx1276.c
|-- sx1276Fsk.c
|-- sx1276FskMisc.c
|-- sx1276Hal.c
|-- sx1276LoRa.c
`-- sx1276LoRaMisc.c
除了lora.h和lora.c,其它文件均為sx1276相關驅動文件(sx1278也適用)。
需要關注的文件:
platform.h設置芯片選型,當前項目設置為:#define USE_SX1276_RADIO。
radio.h中設置芯片工作模式為LORA
spi.h、spi.c中定義spi通信,需要將spi.c的SpiInOut()函數中的spi句柄設置為主板的spi句柄,芯片和主板所有數據通過SPI進行傳輸。
#include <stdint.h>
#include "spi.h"
#include "main.h"uint8_t SpiInOut(uint8_t uotData)
{uint8_t pData = 0;if(HAL_SPI_TransmitReceive(&hspi2, &outData, &pDtata, 1,0xffff) != HAL_OK){return ERROR;}else{return pData;}
}
sx1276LoRa.c中主要關注SX1276LoRaGetRxPacket()、SX1276LoRaSetTxPacket()、SX1276LoRaProcess()函數,分別用于LoRa模式下的數據包獲取、數據包發送和接發調度。
void SX1726LoRaGetRxPacket(void *buffer, uint16_t *size)
{*size = RxPacketSize;RxPacketSize = 0;memcpy((void *)buffer, (void *)RFBuffer, (size_t)*size);
}void SX1276LoRaSetTxPacket(const void *buffer,uint16_t size)
{TxPacketSize = size;memcpy( ( void * )RFBuffer, buffer, ( size_t )TxPacketSize);RFLRState = RFLR_STATE_TX_INIT;
}
lora.h和lora.c為方便lora通信定義的文件。
LoRa通信使用流程
- 需要定義LoRa消息接收發送用戶數據緩沖區(Buffer)、lora操作指針Radio。
#include "lora.h"
tRadioDriver *Radio = NULL;
#define BUFFER_SIZE 30uint16_t BufferSize = BUFFER_SIZE;
uint8_t Buffer[BUFFER_SIZE];
uint8_t EnbaleMaster = false;uint8_t MY_TEST_Msg[] = "hello";void lora_init()
{GPIO_initTypeDef GPIO_InitStruct = {0};GPIO_InitStruct.Pin = GPIO_PIN_9;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);Radio = RadioDriverInit(); //Radio初始化,SX1276和SX1278使用相同驅動,在platform.h中進行選擇設置。Radio->Init(); //SX1278真正初始化,根據radio.h中設置的LORA變量選擇進行lora初始化還是fsk初始化
}
- 在sx1276LoRa.c中設置LoRaSettings變量,對LoRa通信進行參數配置。
- 調用RadioDriverInit()函數對Radio進行初始化,此函數定義于radio.c中。
- 調用Radio->Init(),進行lora通信相關初始化。
- 完成上述初始化后,定義自己的接發服務函數。