初步实现 LoRa 模块的发送和接收
This commit is contained in:
parent
f529617823
commit
f9e3536619
@ -19,7 +19,7 @@ void LED_Init(void)
|
||||
GPIO_InitTypeDef GPIO_Initure;
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE(); //开启 PA 时钟
|
||||
|
||||
GPIO_Initure.Pin = GPIO_PIN_13; //PC13
|
||||
GPIO_Initure.Pin = GPIO_PIN_13; // PC13
|
||||
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; //推挽输出
|
||||
GPIO_Initure.Pull = GPIO_PULLUP; //上拉
|
||||
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH; //高速
|
||||
|
||||
7
Hardware/lora/README.md
Normal file
7
Hardware/lora/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
## LoRa-02
|
||||
|
||||
基于 [wdomski/SX1278](https://github.com/wdomski/SX1278) 库封装的简单的 LoRa 模块操作库。
|
||||
|
||||
由于实验板在设计时忽略了 DIO0 引脚的引出,导致无法使用 DIO0 上的接收、发送中断。因此,对 wdomski/SX1278 库进行了修改,采用检测 FifoRxCurrentaddr 是否移动的方法判断是否有新的接收。在发送时不检测发送中断。
|
||||
|
||||
本实现采用了手册上未定义的方法,可能出现各种问题,建议在可能的情况下引出 DIO0 使用。
|
||||
163
Hardware/lora/lora.c
Normal file
163
Hardware/lora/lora.c
Normal file
@ -0,0 +1,163 @@
|
||||
/**
|
||||
* @file lora.c
|
||||
* @author Myth
|
||||
* @version 0.1
|
||||
* @date 2021.10.15
|
||||
* @brief LoRa sx1278 Library for STM32 HAL
|
||||
*/
|
||||
|
||||
#include "softspi.h"
|
||||
#include "SX1278.h"
|
||||
|
||||
#include "lora.h"
|
||||
|
||||
SX1278_hw_t SX1278_hw;
|
||||
SX1278_t SX1278;
|
||||
SoftSPI_TypeDef sx1278_spi;
|
||||
|
||||
uint8_t is_in_rx_mode = 0;
|
||||
|
||||
uint8_t LoRa_EnterTxMode(uint8_t len);
|
||||
uint8_t LoRa_EnterRxMode(void);
|
||||
|
||||
/**
|
||||
* @brief LoRa 模块初始化
|
||||
*/
|
||||
void LoRa_Init(void)
|
||||
{
|
||||
//初始化各个引脚
|
||||
sx1278_spi.SCLK_GPIO = SX1278_SCLK_GPIO;
|
||||
sx1278_spi.SCLK_Pin = SX1278_SCLK_PIN;
|
||||
|
||||
sx1278_spi.MOSI_GPIO = SX1278_MOSI_GPIO;
|
||||
sx1278_spi.MOSI_Pin = SX1278_MOSI_PIN;
|
||||
|
||||
sx1278_spi.MISO_GPIO = SX1278_MISO_GPIO;
|
||||
sx1278_spi.MISO_Pin = SX1278_MISO_PIN;
|
||||
|
||||
sx1278_spi.SS_GPIO = SX1278_SS_GPIO;
|
||||
sx1278_spi.SS_Pin = SX1278_SS_PIN;
|
||||
|
||||
sx1278_spi.Delay_Time = 5;
|
||||
|
||||
SX1278_hw.nss.port = GPIOA;
|
||||
SX1278_hw.nss.pin = GPIO_PIN_6;
|
||||
SX1278_hw.reset.port = GPIOA;
|
||||
SX1278_hw.reset.pin = GPIO_PIN_7;
|
||||
SX1278_hw.spi = &sx1278_spi;
|
||||
|
||||
SX1278.hw = &SX1278_hw;
|
||||
|
||||
SoftSPI_Init(&sx1278_spi);
|
||||
|
||||
switch ((uint32_t)(SX1278_RESET_GPIO))
|
||||
{
|
||||
case (uint32_t)GPIOA:
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_Initure;
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_Initure.Pin = SX1278_RESET_PIN;
|
||||
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_Initure);
|
||||
}
|
||||
break;
|
||||
|
||||
case (uint32_t)GPIOB:
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_Initure;
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
GPIO_Initure.Pin = SX1278_RESET_PIN;
|
||||
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_Initure);
|
||||
}
|
||||
break;
|
||||
|
||||
case (uint32_t)GPIOC:
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_Initure;
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
|
||||
GPIO_Initure.Pin = SX1278_RESET_PIN;
|
||||
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_Initure);
|
||||
}
|
||||
break;
|
||||
|
||||
case (uint32_t)GPIOD:
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_Initure;
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
GPIO_Initure.Pin = SX1278_RESET_PIN;
|
||||
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_Initure);
|
||||
}
|
||||
}
|
||||
|
||||
SX1278_init(
|
||||
&SX1278,
|
||||
434000000,
|
||||
SX1278_POWER_17DBM,
|
||||
SX1278_LORA_SF_7,
|
||||
SX1278_LORA_BW_125KHZ,
|
||||
SX1278_LORA_CR_4_5,
|
||||
SX1278_LORA_CRC_EN,
|
||||
255);
|
||||
}
|
||||
|
||||
uint8_t LoRa_EnterTxMode(uint8_t len)
|
||||
{
|
||||
is_in_rx_mode = 0;
|
||||
return SX1278_LoRaEntryTx(&SX1278, len, 1000);
|
||||
}
|
||||
|
||||
uint8_t LoRa_EnterRxMode(void)
|
||||
{
|
||||
if (is_in_rx_mode)
|
||||
return 1;
|
||||
|
||||
if (SX1278_LoRaEntryRx(&SX1278, 255, 1000))
|
||||
is_in_rx_mode = 1;
|
||||
|
||||
return is_in_rx_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LoRa 模块发送
|
||||
* @param pdata
|
||||
* @param len
|
||||
* @retval 发送成功返回 1,发送失败返回 0
|
||||
*/
|
||||
uint8_t LoRa_Send(uint8_t *pdata, uint8_t len)
|
||||
{
|
||||
if (LoRa_EnterTxMode(len) == 0)
|
||||
return 0;
|
||||
|
||||
SX1278_LoRaTxPacket(&SX1278, pdata, len, 1000); //超时时间无意义,此函数立即返回
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LoRa 模块接收
|
||||
* @param pdata
|
||||
* @retval 接收到包的长度,0 表示未收到数据
|
||||
*/
|
||||
uint8_t LoRa_Receive(uint8_t *pdata)
|
||||
{
|
||||
if (LoRa_EnterRxMode() == 0)
|
||||
return 0;
|
||||
|
||||
uint8_t len = SX1278_LoRaRxPacket(&SX1278);
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
SX1278_read(&SX1278, pdata, len);
|
||||
return len;
|
||||
}
|
||||
35
Hardware/lora/lora.h
Normal file
35
Hardware/lora/lora.h
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @file lora.h
|
||||
* @author Myth
|
||||
* @version 0.1
|
||||
* @date 2021.10.15
|
||||
* @brief LoRa sx1278 Library for STM32 HAL
|
||||
* @note DO NOT USE THIS LIB IF YOU HAVE DIO0
|
||||
*/
|
||||
|
||||
#ifndef __LORA_H
|
||||
#define __LORA_H
|
||||
|
||||
#include "sys.h"
|
||||
|
||||
// SX1278 引脚设置
|
||||
#define SX1278_SCLK_GPIO GPIOA
|
||||
#define SX1278_SCLK_PIN GPIO_PIN_3
|
||||
|
||||
#define SX1278_MOSI_GPIO GPIOA
|
||||
#define SX1278_MOSI_PIN GPIO_PIN_5
|
||||
|
||||
#define SX1278_MISO_GPIO GPIOA
|
||||
#define SX1278_MISO_PIN GPIO_PIN_4
|
||||
|
||||
#define SX1278_SS_GPIO GPIOA
|
||||
#define SX1278_SS_PIN GPIO_PIN_6
|
||||
|
||||
#define SX1278_RESET_GPIO GPIOA
|
||||
#define SX1278_RESET_PIN GPIO_PIN_7
|
||||
|
||||
void LoRa_Init(void);
|
||||
uint8_t LoRa_Send(uint8_t *pdata, uint8_t len);
|
||||
uint8_t LoRa_Receive(uint8_t *pdata);
|
||||
|
||||
#endif
|
||||
@ -1 +1 @@
|
||||
Subproject commit d6d89363cef5642de8265371f15a1bdf47f3581e
|
||||
Subproject commit a7a42360525ea26f45b459f37c06580706f030b6
|
||||
@ -1 +1 @@
|
||||
Subproject commit e129b212f50433714371eb2460308c1440a8eb97
|
||||
Subproject commit b56448d008643005da02162361fa17fd162f279a
|
||||
@ -339,7 +339,7 @@
|
||||
<MiscControls></MiscControls>
|
||||
<Define>USE_HAL_DRIVER, STM32F103x6</Define>
|
||||
<Undefine></Undefine>
|
||||
<IncludePath>..\Core;..\Libraries\HAL_Lib\Inc;..\Libraries\SoftSPI_HAL_Lib;..\Libraries\SoftI2C_HAL_Lib;..\User\Main;..\System\sys;..\System\systick;..\System\uart;..\Hardware\led;..\Hardware\aht20;..\Hardware\bh1750</IncludePath>
|
||||
<IncludePath>..\Core;..\Libraries\HAL_Lib\Inc;..\Libraries\SoftSPI_HAL_Lib;..\Libraries\SoftI2C_HAL_Lib;..\Libraries\SX1278\driver;..\User\Main;..\System\sys;..\System\systick;..\System\uart;..\Hardware\led;..\Hardware\aht20;..\Hardware\bh1750;..\Hardware\lora</IncludePath>
|
||||
</VariousControls>
|
||||
</Cads>
|
||||
<Aads>
|
||||
@ -665,6 +665,21 @@
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>SX1278</GroupName>
|
||||
<Files>
|
||||
<File>
|
||||
<FileName>SX1278.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Libraries\SX1278\driver\SX1278.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>SX1278_hw.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Libraries\SX1278\driver\SX1278_hw.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>System</GroupName>
|
||||
<Files>
|
||||
@ -703,6 +718,11 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Hardware\bh1750\bh1750.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>lora.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Hardware\lora\lora.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
</Groups>
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
COM_PORT_E printf_Com = COM1;
|
||||
COM_PORT_E getchar_Com = COM1;
|
||||
|
||||
//USART1 PA9 PA10
|
||||
// USART1 PA9 PA10
|
||||
#define USART1_CLK_ENABLE() __HAL_RCC_USART1_CLK_ENABLE()
|
||||
|
||||
#define USART1_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
|
||||
@ -29,7 +29,7 @@ COM_PORT_E getchar_Com = COM1;
|
||||
#define USART1_RX_GPIO_PORT GPIOA
|
||||
#define USART1_RX_PIN GPIO_PIN_10
|
||||
|
||||
//USART2 PA2 PA3
|
||||
// USART2 PA2 PA3
|
||||
#define USART2_CLK_ENABLE() __HAL_RCC_USART2_CLK_ENABLE()
|
||||
|
||||
#define USART2_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @author Myth
|
||||
* @version 0.7
|
||||
* @version 0.8
|
||||
* @date 2021.10.15
|
||||
* @brief 工程主函数文件
|
||||
* @details 初始化及主循环
|
||||
* @note 此版本实现功能:
|
||||
* 串口回显,回显时 PC13 上的 LED 闪烁
|
||||
* AHT20 温度湿度读取及 BH1750 光照度读取
|
||||
* 此版本 AHT20 和 BH1750 在两次读取期间关闭 GPIO 时钟以降低功耗。每一次读取结束后 LED 闪烁
|
||||
* 接收来自主节点的字符串数据
|
||||
* JTAG 已禁用,请使用 SWD 调试
|
||||
*/
|
||||
|
||||
@ -17,8 +16,7 @@
|
||||
#include "uart.h"
|
||||
|
||||
#include "led.h"
|
||||
#include "aht20.h"
|
||||
#include "bh1750.h"
|
||||
#include "lora.h"
|
||||
|
||||
void Echo(uint8_t byte);
|
||||
|
||||
@ -34,37 +32,23 @@ int main(void)
|
||||
UART_Init(); //初始化串口
|
||||
|
||||
LED_Init(); //初始化 LED
|
||||
LoRa_Init(); //初始化 LoRa 模块
|
||||
|
||||
UART_BindReceiveHandle(COM1, Echo); //绑定 COM1 串口接收中断至 Echo 函数
|
||||
|
||||
AHT20_Init(); //初始化 AHT20
|
||||
float humi, temp;
|
||||
|
||||
BH1750_Init(); //初始化 BH1750
|
||||
float light;
|
||||
uint8_t buffer[255];
|
||||
uint8_t size;
|
||||
|
||||
while (1)
|
||||
{
|
||||
//程序主循环
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE(); //开启 GPIOB 时钟
|
||||
size = LoRa_Receive(buffer); //接收
|
||||
if (size == 0)
|
||||
continue;
|
||||
|
||||
AHT20_Start(); // AHT20 开始测量
|
||||
BH1750_Start(); // BH1750 开始测量
|
||||
printf("Receive %d bytes: %s\n", size, buffer);
|
||||
|
||||
Delay_ms(80);
|
||||
|
||||
AHT20_Read(&humi, &temp); //读取 AHT20
|
||||
|
||||
Delay_ms(100);
|
||||
|
||||
light = BH1750_Read(); //读取 BH1750
|
||||
|
||||
printf("humi: %.1f temp: %.1f light: %.1f\n", humi, temp, light);
|
||||
LED1_Toggle;
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_DISABLE(); //关闭 GPIOB 时钟以降低功耗
|
||||
|
||||
Delay_ms(500);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user