本文記錄在SX1278 在STM32F103 驅(qū)動(dòng),實(shí)現(xiàn)收發(fā)數(shù)據(jù)功能。(SPI 驅(qū)動(dòng)參考 https://www.cnblogs.com/T0213-ZH/articles/10573067.html) 編譯環(huán)境:MDK5.25 STM32標(biāo)準(zhǔn)庫(kù):V3.5.0 SX1278驅(qū)動(dòng):sx12xxDrivers-V2.1.0 (https://www./uploads/documents/sx12xxDrivers-V2.1.0.zip) 相關(guān)文檔 https://www./products/wireless-rf/lora-transceivers/sx1278
1、MDK工程目錄(創(chuàng)建工程方式略) 收發(fā)數(shù)據(jù)由宏“ SX1276_RX_DEF" 區(qū)別,定義了為接收數(shù)據(jù),未定義為發(fā)送數(shù)據(jù) 2、修改默認(rèn)sx1276-LoRa.c 里面默認(rèn)的頻率與帶寬 編譯時(shí)出現(xiàn)bool 沒(méi)定義,增加 bool類型頭文件,sx1276.h文件里
main.c 函數(shù)實(shí)現(xiàn)如下, 發(fā)送功能:每間隔1s 發(fā)送一組數(shù)據(jù) 發(fā)送完成后打印一串?dāng)?shù)據(jù)“l(fā)ora send ok”, 接收功能:收到數(shù)據(jù)后串口打印(本全程打印 ”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 增加結(jié)構(gòu)體,結(jié)構(gòu)體實(shí)現(xiàn)在platform.c 文件中
sx1276_hal.c 實(shí)現(xiàn)如下 #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、編譯、燒錄、運(yùn)行
整個(gè)工程代碼:stm32f103_sx1276.rar
|
|
來(lái)自: goodwangLib > 《Lora》