From 90cba8f926153433e5722f4ff2121ec1c6264a00 Mon Sep 17 00:00:00 2001 From: WXK <287788329@qq.com> Date: 星期日, 18 二月 2024 10:02:52 +0800 Subject: [PATCH] 芯片改为L071,提交 --- Src/radio/Lora.c | 402 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 208 insertions(+), 194 deletions(-) diff --git a/Src/radio/Lora.c b/Src/radio/Lora.c index 9aafe2b..455f888 100644 --- a/Src/radio/Lora.c +++ b/Src/radio/Lora.c @@ -10,10 +10,14 @@ #include "stdio.h" #include "delay.h" #include "main.h" +#include <stdlib.h> static RadioEvents_t RadioEvents; #define USE_MODEM_LORA //#define USE_MODEM_FSK #define REGION_CN779 +void Lora47xrx_Init(void); +void Lora433_change(void); +void Lora433rx_Init(void); uint32_t RxDutyCycle_RX_time =128; uint32_t RxDutyCycle_SLEEP_time=256; #define LORA_FREQUENCY0 470200000 @@ -21,47 +25,16 @@ #define LORA_FREQUENCY2 472200000 #define LORA_FREQUENCY3 473200000 #define LORA_FREQUENCY4 474200000 -#if defined( REGION_AS923 ) - -#define RF_FREQUENCY 923000000 // Hz - -#elif defined( REGION_AU915 ) - -#define RF_FREQUENCY 915000000 // Hz - -#elif defined( REGION_CN779 ) +uint16_t Lora_rece_error; +#define LORA_RX_433 1 +#define LORA_TX_47X 2 +#define LORA_RX_47X 3 +uint8_t Lora_status=0; #define RF_FREQUENCY 433000000 // Hz #define RF_FREQUENCY_R 500000000 // Hz #define RF_FREQUENCY_T 470200000 // Hz - - -#elif defined( REGION_EU868 ) - -#define RF_FREQUENCY 868000000 // Hz - -#elif defined( REGION_KR920 ) - -#define RF_FREQUENCY 920000000 // Hz - -#elif defined( REGION_IN865 ) - -#define RF_FREQUENCY 865000000 // Hz - -#elif defined( REGION_US915 ) - -#define RF_FREQUENCY 915000000 // Hz - -#elif defined( REGION_US915_HYBRID ) - -#define RF_FREQUENCY 915000000 // Hz - -#else - - #error "Please define a frequency band in the compiler 'options." - -#endif - +void LoraSendComMap(uint8_t data_length, uint8_t index); #define TX_OUTPUT_POWER 22 // 22 dBm extern bool IrqFired; @@ -90,8 +63,8 @@ // 2: 4/6, // 3: 4/7, // 4: 4/8] -#define LORA_PREAMBLE_LENGTH_T 10 // Same for Tx -#define LORA_PREAMBLE_LENGTH_R 64 // Same for Rx +#define LORA_PREAMBLE_LENGTH_T 8 // Same for Tx +#define LORA_PREAMBLE_LENGTH_R 8 // Same for Rx //#define LORA_PREAMBLE_LENGTH 990 // Same for Tx and Rx #define LORA_SYMBOL_TIMEOUT 0 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false @@ -123,7 +96,7 @@ }States_t; //#define RX_TIMEOUT_VALUE 1000 -#define RX_TIMEOUT_VALUE 64000 +#define RX_TIMEOUT_VALUE 100 #define BUFFER_SIZE 64 // Define the payload size here const uint8_t PingMsg[] = "PING"; @@ -151,61 +124,64 @@ { // HAL_GPIO_TogglePin(GPIOB, LED1_Pin); } -void Lora_tx_mode( void ) -{ +//void Lora_tx_mode( void ) +//{ +//Lora_status=LORA_TX_47X; +// RadioEvents.TxDone = OnTxDone; +// RadioEvents.RxDone = OnRxDone; +// RadioEvents.TxTimeout = OnTxTimeout; +// RadioEvents.RxTimeout = OnRxTimeout; +// RadioEvents.RxError = OnRxError; - Radio.SetChannel( Loratx_frequency ); - - Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2, - 7, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, - false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 ); +// Radio.Init( &RadioEvents ); +// Radio.SetChannel( Loratx_frequency ); +// +// Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2, +// 7, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, +// false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 ); -} -void Lora_rx_open( void ) -{ - if(Lora_rx_open_flag==1) - { - Lora_Init(); - Lora_rx_open_flag=0; - } - if(lora_yingda_flag&&lora_chongfuyingda_flag==0) - { - printf("Lora:%d %d\r\n",lora_yingda_flag,lora_chongfuyingda_flag); - lora_chongfuyingda_flag=1; - RadioEvents.TxDone = OnTxDone; - RadioEvents.RxDone = OnRxDone; - RadioEvents.TxTimeout = OnTxTimeout; - RadioEvents.RxTimeout = OnRxTimeout; - RadioEvents.RxError = OnRxError; +//} +//void Lora_rx_open( void ) +//{ +// if(Lora_rx_open_flag==1) +// { +// Lora_Init(); +// Lora_rx_open_flag=0; +// } +// if(lora_yingda_flag&&lora_chongfuyingda_flag==0) +// { +// printf("Lora:%d %d\r\n",lora_yingda_flag,lora_chongfuyingda_flag); +// lora_chongfuyingda_flag=1; +// RadioEvents.TxDone = OnTxDone; +// RadioEvents.RxDone = OnRxDone; +// RadioEvents.TxTimeout = OnTxTimeout; +// RadioEvents.RxTimeout = OnRxTimeout; +// RadioEvents.RxError = OnRxError; - Radio.Init( &RadioEvents ); - Radio.SetChannel( 500000000 ); - Radio.SetRxConfig( MODEM_LORA, 0, 7, - LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R, - LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, - 0, false, 0, 0, true, 1 ); +// Radio.Init( &RadioEvents ); +// Radio.SetChannel( 500000000 ); +// Radio.SetRxConfig( MODEM_LORA, 0, 7, +// LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R, +// LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, +// 0, false, 0, 0, true, 1 ); - SX126xClearIrqStatus( IRQ_RADIO_ALL ); -// Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); - Radio.Rx( RX_TIMEOUT_VALUE ); +// SX126xClearIrqStatus( IRQ_RADIO_ALL ); +//// Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); +// Radio.Rx( RX_TIMEOUT_VALUE ); - - } -} +// +// } +//} +uint32_t lora_zhuangtai; void OnTxDone( void ) { - LED_TB_OFF; - if(lora_yingda_num>1000) - { - lora_yingda_num=0; - lora_yingda_flag=1; - } - - if(lora_yingda_flag==0) - Lora_rx_open_flag=1; + if(bat_percent>20) + {LED_TR_OFF;} else - {} - + {LED_TB_OFF;} + + Radio.Standby(); + Radio.Rx( RX_TIMEOUT_VALUE ); } extern uint32_t lp_time; @@ -224,11 +200,14 @@ u8 lora_seq_nb2; extern uint8_t stationary_flag; extern uint8_t anjian_flag; +uint16_t Lora_wangguanid=0; +extern uint8_t GPS_GGAdate[200]; +extern uint8_t GPSchangdu; void Lora_tx_zubao( void ) { uint16_t checksum; usart_send[2] = 0x22;//正常模式 - usart_send[3] = 9+Lora_tx_ancnum*4;//数据段长度 + usart_send[3] = 11+Lora_tx_ancnum*4;//数据段长度 memcpy(&usart_send[4],&dev_id,2); usart_send[6] = lora_seq_nb2;//包序 power_and_key=stationary_flag<<1;//把按键跟静止2合1,发出去 @@ -240,14 +219,30 @@ // usart_send[9] = lora_yingda_flag; memcpy(&usart_send[11],&Lora_tx_ancid,2*Lora_tx_ancnum); memcpy(&usart_send[11+Lora_tx_ancnum*2],&Lora_tx_ancdist,2*Lora_tx_ancnum); - checksum = Checksum_u16(&usart_send[2],8+4*Lora_tx_ancnum); - memcpy(&usart_send[11+4*Lora_tx_ancnum],&checksum,2); + memcpy(&usart_send[11+Lora_tx_ancnum*4],&Lora_wangguanid,2); + memcpy(&usart_send[13+Lora_tx_ancnum*4],GPS_GGAdate,GPSchangdu); + checksum = Checksum_u16(&usart_send[2],11+4*Lora_tx_ancnum+GPSchangdu); + memcpy(&usart_send[13+4*Lora_tx_ancnum+GPSchangdu],&checksum,2); } +extern uint8_t lora_tx_flag; uint16_t current_count; u16 Lora_recevcount; -extern uint8_t no_data_chongqi_num; +extern uint16_t no_data_chongqi_num; +uint8_t du_flag; +uint8_t xie_flag; +extern uint8_t no_yingdaflag; +uint16_t Beepchixutime; void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) { + BufferSize = size; + memcpy( RX_Buffer, payload, BufferSize ); + RssiValue = rssi; + SnrValue = snr; + Radio.Standby(); +printf("收到lora\r\n"); +// if(Lora_status==LORA_RX_433) + if(RX_Buffer[0]==0x50&&RX_Buffer[1]==0x49) + { Lora_recevcount=HAL_LPTIM_ReadCounter(&hlptim1); Lora_recevcount=Lora_recevcount+197;//197代表6ms if(Lora_recevcount>=32768) @@ -255,38 +250,14 @@ Lora_recevcount -=32768; } // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); - BufferSize = size; - memcpy( RX_Buffer, payload, BufferSize ); - RssiValue = rssi; - SnrValue = snr; - no_data_chongqi_num=0; - Radio.Standby(); +// BufferSize = size; +// memcpy( RX_Buffer, payload, BufferSize ); +// RssiValue = rssi; +// SnrValue = snr; +// no_data_chongqi_num=0; +// Radio.Standby(); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); - if(lora_yingda_flag) - { - memcpy(&lora_recv_devid,&RX_Buffer[4],2); - if(lora_recv_devid==dev_id) - { - - - } - } - if(lora_yingda_flag==0) - { - lora_chongfuyingda_flag=0; -// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET); -// Delay_Ms(10); -// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET); - uint16_t i=0; -// for(i=0;i<BufferSize;i++) - {printf("R %d:\r\n",RX_Buffer[3]);} - RX_TIMEOUTs++; -// printf("RX_num :%d\r\n",RX_TIMEOUTs); -// start_time=HAL_LPTIM_ReadCounter(&hlptim1); -// end_time=start_time+((32768/1000)*20); -// if(end_time>=32768) -// {end_time-=32768;} -// current_time=HAL_LPTIM_ReadCounter(&hlptim1); + HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_SET); //片选lora无效 id = dwt_readdevid() ; @@ -315,108 +286,151 @@ // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效 delay_ms(1); -// if(lp_time-lora_txtime_old>=1) -// { -// LED_TB_ON; -// lora_txtime_old=lp_time; -// lora_seq_nb2++; -// lora_yingda_num++; -// Lora_tx_zubao(); -// Lora_tx_mode(); -// Radio.Send( usart_send, 12+Lora_tx_ancnum*4); -// Lora_tx_ancnum=0; -// } -// else -// { - Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); -// } - - +// Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); + Radio.Rx( 9999 ); + printf("接收到uwb测距:%d",Lora_tx_ancdist[0]); + } + if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xEE) + { +// BufferSize = size; +// memcpy( RX_Buffer, payload, BufferSize ); +// RssiValue = rssi; +// SnrValue = snr; +// Radio.Standby(); + if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xEE&&!memcmp(&dev_id,&RX_Buffer[4],2)) + { + if(RX_Buffer[6]==0)//不需要操作,正常应答 + { + Lora_rece_error=0; + memcpy( &Lora_wangguanid, &RX_Buffer[2], 2 ); + } + if(RX_Buffer[6]==2) //读操作,下一次lora发送的时候要发出去 + { +// no_yingda_time=5; + Lora_rece_error=0; + if(no_yingdaflag==0) + { + du_flag=1; + LoraSendComMap(RX_Buffer[8],RX_Buffer[7]>>1); + } + } + if(RX_Buffer[6]==1)//写操作 + { + Lora_rece_error=0; +// uint16_t rxbuff1; +// xie_flag=1; +// memcpy(&rxbuff1, &RX_Buffer[9], 2); +// memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &rxbuff1, RX_Buffer[8]); +// no_yingda_time=5; + if(no_yingdaflag==0) + { + if(RX_Buffer[7]==0x10) + { + Beep_On(); + memcpy(&Beepchixutime, &RX_Buffer[9], 2); + } + else + { + memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &RX_Buffer[9], RX_Buffer[8]); + save_com_map_to_flash(); + delay_ms(100); + NVIC_SystemReset(); + } + } + } + } + Lora433_change(); } } void OnTxTimeout( void ) { Radio.Standby(); - Lora_Init(); - Lora_rx_open_flag=0; - - Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); + Lora433_change(); } - + uint8_t tm; +uint16_t Lora_rece_error; void OnRxTimeout( void ) { Radio.Standby(); - Lora_Init(); - Lora_rx_open_flag=0; - - Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); -// printf("slave OnRxTimeout\r\n"); - + if(Lora_status==LORA_RX_47X) //470rx超时,先调时间发送,5次后还不行,换频段 + { + Lora_rece_error++; + if(Lora_rece_error>5) + { + Loratx_frequency=Loratx_frequency+1000000; + if(Loratx_frequency>474000000) + {Loratx_frequency=470000000;} + Lora_rece_error=0; + Lora_wangguanid=0; + } + else + { + tm= (rand()%100); + SetNextPollTime(tm); + HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, tm); + } + } + Lora433_change(); } void OnRxError( void ) { Radio.Standby(); - Lora_Init(); - Lora_rx_open_flag=0; - Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); + Lora433_change(); } -void Lora_Init(void) +void Lora47X_Init(void) { + Lora_status=LORA_RX_47X; RadioEvents.TxDone = OnTxDone; RadioEvents.RxDone = OnRxDone; RadioEvents.TxTimeout = OnTxTimeout; RadioEvents.RxTimeout = OnRxTimeout; RadioEvents.RxError = OnRxError; -// printf("Lora_Init3\r\n"); + + Radio.Init( &RadioEvents ); + Radio.SetChannel( Loratx_frequency ); + + Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2, + 7, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, + false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 ); + + Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, 7, + LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R, + LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, + 0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 0 ); +} +void Lora433_change(void) +{ + Lora_status=LORA_RX_433; +// Radio.SetChannel( RF_FREQUENCY ); +// SX126x.ModulationParams.Params.LoRa.SpreadingFactor=5; +// SX126x.ModulationParams.Params.LoRa.Bandwidth=2; +// SX126x.ModulationParams.Params.LoRa.CodingRate=1; +// SX126xSetModulationParams( &SX126x.ModulationParams ); +// Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); +// +// Lora_status=LORA_RX_47X; + RadioEvents.TxDone = OnTxDone; + RadioEvents.RxDone = OnRxDone; + RadioEvents.TxTimeout = OnTxTimeout; + RadioEvents.RxTimeout = OnRxTimeout; + RadioEvents.RxError = OnRxError; + Radio.Init( &RadioEvents ); Radio.SetChannel( RF_FREQUENCY ); - -// Radio.SetStandby( STDBY_RC ); -// Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, -// LORA_SPREADING_FACTOR, LORA_CODINGRATE, -// LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, -// true, 0, 0, LORA_IQ_INVERSION_ON, 3000 ); - -// Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, -// LORA_SPREADING_FACTOR, LORA_CODINGRATE, -// LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, -// false, 0, 0, LORA_IQ_INVERSION_ON_T, 1000 ); - -// Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, -// LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, -// LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, -// 0, true, 0, 0, LORA_IQ_INVERSION_ON, false ); -// printf("Lora_Init1\r\n"); + Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2, + 5, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, + false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 ); + Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 1 ); + Radio.Rx( 9999 ); +// Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); -// Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, -// LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, -// LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, -// 0, false, 0, 0, LORA_IQ_INVERSION_ON, true ); - - - - { - SX126xClearIrqStatus( IRQ_RADIO_ALL ); -// SX126xSetDioIrqParams( IRQ_RX_DONE | IRQ_CRC_ERROR, -// IRQ_RX_DONE | IRQ_CRC_ERROR, -// IRQ_RADIO_NONE, IRQ_RADIO_NONE -// ); -// SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT, -// IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT, -// IRQ_RADIO_NONE, -// IRQ_RADIO_NONE ); - Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); -// Radio.Rx( RX_TIMEOUT_VALUE ); -// printf("I am Slave!\r\n"); - } - } -- Gitblit v1.9.3