STM32 LoRa源码解读

目录结构:

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进行传输。

c 复制代码
#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模式下的数据包获取、数据包发送和接发调度。

c 复制代码
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通信使用流程

  1. 需要定义LoRa消息接收发送用户数据缓冲区(Buffer)、lora操作指针Radio。
c 复制代码
#include "lora.h"
tRadioDriver *Radio = NULL;
#define BUFFER_SIZE 30

uint16_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初始化
}
  1. 在sx1276LoRa.c中设置LoRaSettings变量,对LoRa通信进行参数配置。
  2. 调用RadioDriverInit()函数对Radio进行初始化,此函数定义于radio.c中。
  3. 调用Radio->Init(),进行lora通信相关初始化。
  4. 完成上述初始化后,定义自己的接发服务函数。
相关推荐
xxpro27 分钟前
S3C2440开发板点亮LED灯+PWM定时器
单片机·嵌入式硬件
百里与司空34 分钟前
学习CubeIDE——定时器开发
stm32·单片机·嵌入式硬件·学习
天蓝蓝235282 小时前
嵌入式硬件基础知识
嵌入式硬件
代码总长两年半2 小时前
STM32+FATFS+SD卡+RTC(生成.CSV格式文件)
stm32·嵌入式硬件·实时音视频
honey ball4 小时前
仪表放大器AD620
运维·单片机·嵌入式硬件·物联网·学习
luckyluckypolar4 小时前
STM32 -中断
stm32·单片机·嵌入式硬件·物联网
启明云端wireless-tag8 小时前
ESP32无线WiFi蓝牙SOC,设备物联网通信方案,启明云端乐鑫代理商
嵌入式硬件·物联网·wifi·esp32·乐鑫·wifi模组
小狗爱吃黄桃罐头9 小时前
江协科技STM32学习- P14 示例程序(定时器定时中断和定时器外部时钟)
stm32·江科大stm32
@@庆9 小时前
stm32 PWR电源控制(修改主频&睡眠模式&停机模式&待机模式)
stm32·单片机·嵌入式硬件
JT灬新一9 小时前
STM32巡回研讨会总结(2024)
stm32·单片机·嵌入式硬件