| | |
| | | #include "main.h" |
| | | #include <stdlib.h> |
| | | #include "dw_mbx_tag.h" |
| | | #include "WS2812.h" |
| | | static RadioEvents_t RadioEvents; |
| | | #define USE_MODEM_LORA |
| | | //#define USE_MODEM_FSK |
| | |
| | | void Lora47xrx_Init(void); |
| | | void Lora433_change(void); |
| | | void Lora433rx_Init(void); |
| | | void SwitchLoraSettings(uint32_t freq,uint8_t sf,uint8_t power); |
| | | uint32_t RxDutyCycle_RX_time =128; |
| | | uint32_t RxDutyCycle_SLEEP_time=256; |
| | | #define LORA_FREQUENCY0 470200000 |
| | |
| | | #define LORA_TX_47X 2 |
| | | #define LORA_RX_47X 3 |
| | | uint8_t Lora_status=0; |
| | | extern uint32_t uwbled,gpsled,loraled,powerled; |
| | | |
| | | #define RF_FREQUENCY 433000000 // Hz |
| | | #define RF_FREQUENCY_R 500000000 // Hz |
| | | #define RF_FREQUENCY_T 470200000 // Hz |
| | | |
| | | #define TX_OUTPUT_POWER 22 // 22 dBm |
| | | |
| | | extern bool IrqFired; |
| | | extern uint8_t lora_yingda_flag; |
| | | uint16_t lora_yingda_num; |
| | | |
| | | |
| | | //bool EnableMaster=true;//主选择 |
| | | bool EnableMaster=false;//从选择 |
| | | |
| | | uint16_t crc_value; |
| | | /*! |
| | | * Radio events function pointer |
| | |
| | | #define RX_TIMEOUT_VALUE 0 |
| | | #define BUFFER_SIZE 255 // Define the payload size here |
| | | |
| | | |
| | | |
| | | uint16_t BufferSize = BUFFER_SIZE; |
| | | uint8_t TX_Buffer[BUFFER_SIZE]; |
| | | uint8_t RX_Buffer[BUFFER_SIZE]; |
| | |
| | | uint8_t lora_chongfuyingda_flag; |
| | | uint8_t Lora_rx_open_flag; |
| | | extern u32 Loratx_frequency; |
| | | void LedToggle(void) |
| | | |
| | | { |
| | | // HAL_GPIO_TogglePin(GPIOB, LED1_Pin); |
| | | } |
| | | //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.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; |
| | | |
| | | // 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 ); |
| | | |
| | | // |
| | | // } |
| | | //} |
| | | uint8_t flag_writepara_needreset = 0,wg_lost_count; |
| | | uint8_t flag_writepara_needreset = 0; |
| | | uint32_t wg_lost_count = 10; |
| | | uint32_t lora_zhuangtai; |
| | | uint16_t current_count; |
| | | extern wg_state_enum wg_state; |
| | |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | Radio.Standby(); |
| | | Radio.Rx( 10 ); |
| | | if(wg_lost_count++>3) |
| | | { |
| | | wg_state = WG_Lost; |
| | | } |
| | | Radio.Rx( 100 ); |
| | | // LED_TB_OFF; |
| | | } |
| | | extern LPTIM_HandleTypeDef hlptim1; |
| | | uint16_t current_time,start_time,end_time; |
| | |
| | | extern uint8_t lora_sendbuffer[200]; |
| | | void LoraSendComMap(uint8_t cmd) |
| | | { |
| | | uint8_t data_length = 0x28; |
| | | uint8_t data_length = 0x50; |
| | | uint16_t checksum = 0; |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_READPARARESP; |
| | | lora_sendbuffer[MSG_LENGTH] = data_length+9; |
| | | lora_sendbuffer[MSG_LENGTH] = data_length+11; |
| | | memcpy(&lora_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2); |
| | | memcpy(&lora_sendbuffer[DEST_ID_IDX],&wg_report_id,2); |
| | | lora_sendbuffer[RP_CMD_IDX] = cmd; |
| | | lora_sendbuffer[RP_INDEX_IDX] = 2; |
| | | lora_sendbuffer[RP_LEN_IDX] = data_length; |
| | | memcpy(&lora_sendbuffer[7], &g_com_map[1], data_length); |
| | | checksum = Checksum_u16(lora_sendbuffer,9+data_length); |
| | | memcpy(&lora_sendbuffer[9+data_length],&checksum,2); |
| | | Radio.Send(lora_sendbuffer,data_length+11); |
| | | memcpy(&lora_sendbuffer[9], &g_com_map[1], data_length); |
| | | checksum = Checksum_u16(lora_sendbuffer,11+data_length); |
| | | memcpy(&lora_sendbuffer[11+data_length],&checksum,2); |
| | | Radio.Send(lora_sendbuffer,data_length+13); |
| | | // Delay_Ms(100); |
| | | } |
| | | void LoraRspWriteCommap(uint8_t index) |
| | |
| | | Radio.Send(lora_sendbuffer,data_length+11); |
| | | // Delay_Ms(100); |
| | | } |
| | | static uint16_t delaytime = 1125; |
| | | static uint16_t delaytime = 1050; |
| | | static uint16_t source_id; |
| | | uint8_t rec_index; |
| | | uint16_t rec_value,rec_delaytime,target_count,rx_count; |
| | | uint16_t rec_value,rec_delaytime,rx_count; |
| | | int16_t target_count; |
| | | uint16_t test111; |
| | | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| | | { |
| | | BufferSize = size; |
| | |
| | | if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_RANGEPOLL) |
| | | { |
| | | UWB_Wkup(); |
| | | MbxTagUwbRec(); |
| | | // memcpy(&test111,&RX_Buffer[1],2); |
| | | MbxTagUwbRec(); |
| | | test111++; |
| | | // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET); |
| | | // printf("LORA IdleTask:%x",test111); |
| | | HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效 |
| | | } |
| | | if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_WGRESPTAG) |
| | |
| | | current_count = HAL_LPTIM_ReadCounter(&hlptim1); |
| | | wg_report_freq = RX_Buffer[POLL_FREQ_IDX]+400; |
| | | memcpy(&rec_delaytime,&RX_Buffer[NEXTPOLL_TIME_IDX],2); |
| | | target_count = current_count + rec_delaytime*32.8 - delaytime; |
| | | if(target_count>=32768) |
| | | target_count = current_count + rec_delaytime*3.2768 - delaytime; |
| | | while(target_count>=32768) |
| | | target_count-=32768; |
| | | __HAL_LPTIM_COMPARE_SET(&hlptim1, target_count); |
| | | rx_count = HAL_LPTIM_ReadCounter(&hlptim1); |
| | |
| | | } |
| | | HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET); |
| | | HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); |
| | | memcpy(&xiafayuyin_data,&RX_Buffer[9],RX_Buffer[8]); |
| | | memcpy(&xiafayuyin_data,&RX_Buffer[PWTAG_WRITE_VALUE_IDX],BufferSize-PWTAG_WRITE_VALUE_IDX); |
| | | yuyin_sendflag=1; |
| | | yuyinzidongguan_time=0; |
| | | yuyin_no_sleep_flag=1; |
| | | yuyin_no_sleep_flag=1; |
| | | // SYN_FrameInfo(0,xiafayuyin_data); //播放内容 |
| | | // memset(xiafayuyin_data,0,sizeof(xiafayuyin_data)); |
| | | LoraRspWriteCommap(SUBMSG_WRITE_ANCPARA); |
| | | no_rx_flag = 1; |
| | | }else{ |
| | | memcpy(&rec_value,&RX_Buffer[PWTAG_WRITE_VALUE_IDX],2); |
| | | g_com_map[rec_index/2] = rec_value; |
| | |
| | | void OnRxTimeout( void ) |
| | | { |
| | | Radio.Standby(); |
| | | current_count = HAL_LPTIM_ReadCounter(&hlptim1); |
| | | target_count-=current_count%3276; |
| | | while(target_count<0) |
| | | target_count+=32768; |
| | | __HAL_LPTIM_COMPARE_SET(&hlptim1, target_count); |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | Radio.Rx(0); |
| | | } |
| | | |
| | | void OnRxError( void ) |
| | |
| | | |
| | | |
| | | } |
| | | |
| | | extern u8 active_flag; |
| | | void LoraInit(void) |
| | | { |
| | | RadioEvents.TxDone = OnTxDone; |
| | |
| | | |
| | | Radio.Init( &RadioEvents ); |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],5,0); //切换lora接收频点 |
| | | Radio.Rx( 0 ); |
| | | if(active_flag) |
| | | { |
| | | Radio.Rx( 0 ); |
| | | }else{ |
| | | Radio.Sleep(); |
| | | } |
| | | // Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); |
| | | |
| | | } |