分享

STM32F103 SX1278 驱动

 goodwangLib 2020-07-08

本文记录在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./uploads/documents/sx12xxDrivers-V2.1.0.zip)

                       相关文档 https://www./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 size
static uint8_t  Buffer[30];                // RF buffer
const uint8_t  send_data_buf[] = "lora send data\r\n";

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 ok\r\n", 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[i] = 0;
                    
            SX1276StartRx();
        }        
#else
        if(SX1276Process( ) == RF_TX_DONE){
            UART_ApiStructure.send_str("lora send ok\r\n", 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、编译、烧录、运行

    

 

 整个工程代码:stm32f103_sx1276.rar

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多