本文记录在SX1278 在STM32F103 驱动,实现收发数据功能。(SPI 驱动参考 https://www.cnblogs.com/T0213-ZH/articles/10573067.html)
编译环境:MDK5.25
STM32标准库:V3.5.0
SX1278驱动:sx12xxDrivers-V2.1.0 (https://www.semtech.com/uploads/documents/sx12xxDrivers-V2.1.0.zip)
相关文档 https://www.semtech.com/products/wireless-rf/lora-transceivers/sx1278
1、MDK工程目录(创建工程方式略)
收发数据由宏“ SX1276_RX_DEF" 区别,定义了为接收数据,未定义为发送数据
2、修改默认sx1276-LoRa.c 里面默认的频率与带宽
编译时出现bool 没定义,增加 bool类型头文件,sx1276.h文件里
main.c 函数实现如下, 发送功能:每间隔1s 发送一组数据 发送完成后打印一串数据“lora send ok”, 接收功能:收到数据后串口打印(本全程打印 ”lora send data“)
#include "platform.h"static uint32_t TimeingDelay = 0; static uint32_t timeout_cnt = 1000; void Delayms(uint32_t ms){ TimeingDelay = ms; while(TimeingDelay != 0){};}void uart_recv_data(uint8_t *p, uint16_t len){ UART_ApiStructure.send_str(p, len);}extern void SX1276Init( void );extern void SX1276SetTxPacket( const void *buffer, uint16_t size );extern uint32_t SX1276Process( void );static uint16_t BufferSize = 30; // RF buffer sizestatic uint8_t Buffer[30]; // RF bufferconst uint8_t send_data_buf[] = "lora send datarn";extern void SpiInit( void );extern void Soft_delay_ms(u16 time);int main(void){ uint16_t count = 0, i; static uint32_t state = 0; //TIM_Configuration(); SYSTEM_tick_config_1ms(); Delayms(10); SX1276Init(); UART_ApiStructure.config(&UART_TypeDef_param, uart_recv_data); UART_ApiStructure.send_str("uart init okrn", 14); #if defined(SX1276_RX_DEF) SX1276StartRx();#else SX1276SetTxPacket( send_data_buf, 18 );#endif while (1){ if(!timeout_cnt){ timeout_cnt = 1000; count++; #if !defined(SX1276_RX_DEF) SX1276SetTxPacket( send_data_buf, strlen(send_data_buf));#endif } #if defined(SX1276_RX_DEF) if(SX1276Process( ) == RF_RX_DONE){ SX1276GetRxPacket( Buffer, ( uint16_t* )&BufferSize ); UART_ApiStructure.send_str(Buffer, BufferSize); for(i=0;i<30;i++) Buffer
= 0; SX1276StartRx(); } #else if(SX1276Process( ) == RF_TX_DONE){ UART_ApiStructure.send_str("lora send okrn", 14); } #endif }}volatile uint32_t TickCounter = 0;void TimeingDelay_Decrement(void){ if (TimeingDelay > 0){ TimeingDelay--; } if(timeout_cnt){ timeout_cnt--; } TickCounter++;}
3、SX1276 spi 抽象层接口修改 sx1276_hal.c, sx1276-Hal.h
SX1276-Hal.h 增加结构体,结构体实现在platform.c 文件中
sx1276_hal.c 实现如下
#include "platform.h"void SX1276InitIo( void ){ sx1278_ApiStructre.init();}void SX1276SetReset( uint8_t state ){ if( state == RADIO_RESET_ON ){ sx1278_ApiStructre.reset(0); }else{ sx1278_ApiStructre.reset(1); }}void SX1276Write( uint8_t addr, uint8_t data ){ SX1276WriteBuffer( addr, &data, 1 );}void SX1276Read( uint8_t addr, uint8_t *data ){ SX1276ReadBuffer( addr, data, 1 );}void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.write_buf(&sx1278_ApiStructre.spi, addr | 0x80, buffer, size);}void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.read_buf(&sx1278_ApiStructre.spi, addr & 0x7F, buffer, size);}void SX1276WriteFifo( uint8_t *buffer, uint8_t size ){ SX1276WriteBuffer( 0, buffer, size );}void SX1276ReadFifo( uint8_t *buffer, uint8_t size ){ SX1276ReadBuffer( 0, buffer, size );}/*inline*/ uint8_t SX1276ReadDio0( void ){ //return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN ); return sx1278_ApiStructre.rDio0();}/*inline*/ uint8_t SX1276ReadDio1( void ){ return 0;//GPIO_ReadInputDataBit( DIO1_IOPORT, DIO1_PIN );}/*inline*/ uint8_t SX1276ReadDio2( void ){ return 0;//GPIO_ReadInputDataBit( DIO2_IOPORT, DIO2_PIN );}/*inline*/ uint8_t SX1276ReadDio3( void ){ return 0;//GPIO_ReadInputDataBit( DIO3_IOPORT, DIO3_PIN );}/*inline*/ uint8_t SX1276ReadDio4( void ){ return 0;//GPIO_ReadInputDataBit( DIO4_IOPORT, DIO4_PIN );}/*inline*/ uint8_t SX1276ReadDio5( void ){ return 0;//GPIO_ReadInputDataBit( DIO5_IOPORT, DIO5_PIN );}/*inline*/ void SX1276WriteRxTx( uint8_t txEnable ){// if( txEnable != 0 )// {// Set_RF_Switch_TX(); // IoePinOn( FEM_CTX_PIN );// IoePinOff( FEM_CPS_PIN );// }// else// {// Set_RF_Switch_RX(); // IoePinOff( FEM_CTX_PIN );// IoePinOn( FEM_CPS_PIN );// }}
4、编译、烧录、运行
本文记录在SX1278 在STM32F103 驱动,实现收发数据功能。(SPI 驱动参考 https://www.cnblogs.com/T0213-ZH/articles/10573067.html)
编译环境:MDK5.25
STM32标准库:V3.5.0
SX1278驱动:sx12xxDrivers-V2.1.0 (https://www.semtech.com/uploads/documents/sx12xxDrivers-V2.1.0.zip)
相关文档 https://www.semtech.com/products/wireless-rf/lora-transceivers/sx1278
1、MDK工程目录(创建工程方式略)
收发数据由宏“ SX1276_RX_DEF" 区别,定义了为接收数据,未定义为发送数据
2、修改默认sx1276-LoRa.c 里面默认的频率与带宽
编译时出现bool 没定义,增加 bool类型头文件,sx1276.h文件里
main.c 函数实现如下, 发送功能:每间隔1s 发送一组数据 发送完成后打印一串数据“lora send ok”, 接收功能:收到数据后串口打印(本全程打印 ”lora send data“)
#include "platform.h"static uint32_t TimeingDelay = 0; static uint32_t timeout_cnt = 1000; void Delayms(uint32_t ms){ TimeingDelay = ms; while(TimeingDelay != 0){};}void uart_recv_data(uint8_t *p, uint16_t len){ UART_ApiStructure.send_str(p, len);}extern void SX1276Init( void );extern void SX1276SetTxPacket( const void *buffer, uint16_t size );extern uint32_t SX1276Process( void );static uint16_t BufferSize = 30; // RF buffer sizestatic uint8_t Buffer[30]; // RF bufferconst uint8_t send_data_buf[] = "lora send datarn";extern void SpiInit( void );extern void Soft_delay_ms(u16 time);int main(void){ uint16_t count = 0, i; static uint32_t state = 0; //TIM_Configuration(); SYSTEM_tick_config_1ms(); Delayms(10); SX1276Init(); UART_ApiStructure.config(&UART_TypeDef_param, uart_recv_data); UART_ApiStructure.send_str("uart init okrn", 14); #if defined(SX1276_RX_DEF) SX1276StartRx();#else SX1276SetTxPacket( send_data_buf, 18 );#endif while (1){ if(!timeout_cnt){ timeout_cnt = 1000; count++; #if !defined(SX1276_RX_DEF) SX1276SetTxPacket( send_data_buf, strlen(send_data_buf));#endif } #if defined(SX1276_RX_DEF) if(SX1276Process( ) == RF_RX_DONE){ SX1276GetRxPacket( Buffer, ( uint16_t* )&BufferSize ); UART_ApiStructure.send_str(Buffer, BufferSize); for(i=0;i<30;i++) Buffer
= 0; SX1276StartRx(); } #else if(SX1276Process( ) == RF_TX_DONE){ UART_ApiStructure.send_str("lora send okrn", 14); } #endif }}volatile uint32_t TickCounter = 0;void TimeingDelay_Decrement(void){ if (TimeingDelay > 0){ TimeingDelay--; } if(timeout_cnt){ timeout_cnt--; } TickCounter++;}
3、SX1276 spi 抽象层接口修改 sx1276_hal.c, sx1276-Hal.h
SX1276-Hal.h 增加结构体,结构体实现在platform.c 文件中
sx1276_hal.c 实现如下
#include "platform.h"void SX1276InitIo( void ){ sx1278_ApiStructre.init();}void SX1276SetReset( uint8_t state ){ if( state == RADIO_RESET_ON ){ sx1278_ApiStructre.reset(0); }else{ sx1278_ApiStructre.reset(1); }}void SX1276Write( uint8_t addr, uint8_t data ){ SX1276WriteBuffer( addr, &data, 1 );}void SX1276Read( uint8_t addr, uint8_t *data ){ SX1276ReadBuffer( addr, data, 1 );}void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.write_buf(&sx1278_ApiStructre.spi, addr | 0x80, buffer, size);}void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ){ SPI_ApiStructure.read_buf(&sx1278_ApiStructre.spi, addr & 0x7F, buffer, size);}void SX1276WriteFifo( uint8_t *buffer, uint8_t size ){ SX1276WriteBuffer( 0, buffer, size );}void SX1276ReadFifo( uint8_t *buffer, uint8_t size ){ SX1276ReadBuffer( 0, buffer, size );}/*inline*/ uint8_t SX1276ReadDio0( void ){ //return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN ); return sx1278_ApiStructre.rDio0();}/*inline*/ uint8_t SX1276ReadDio1( void ){ return 0;//GPIO_ReadInputDataBit( DIO1_IOPORT, DIO1_PIN );}/*inline*/ uint8_t SX1276ReadDio2( void ){ return 0;//GPIO_ReadInputDataBit( DIO2_IOPORT, DIO2_PIN );}/*inline*/ uint8_t SX1276ReadDio3( void ){ return 0;//GPIO_ReadInputDataBit( DIO3_IOPORT, DIO3_PIN );}/*inline*/ uint8_t SX1276ReadDio4( void ){ return 0;//GPIO_ReadInputDataBit( DIO4_IOPORT, DIO4_PIN );}/*inline*/ uint8_t SX1276ReadDio5( void ){ return 0;//GPIO_ReadInputDataBit( DIO5_IOPORT, DIO5_PIN );}/*inline*/ void SX1276WriteRxTx( uint8_t txEnable ){// if( txEnable != 0 )// {// Set_RF_Switch_TX(); // IoePinOn( FEM_CTX_PIN );// IoePinOff( FEM_CPS_PIN );// }// else// {// Set_RF_Switch_RX(); // IoePinOff( FEM_CTX_PIN );// IoePinOn( FEM_CPS_PIN );// }}
4、编译、烧录、运行
举报