| | |
| | | }States_t; |
| | | |
| | | //#define RX_TIMEOUT_VALUE 1000 |
| | | #define RX_TIMEOUT_VALUE 10 |
| | | #define RX_TIMEOUT_VALUE 0 |
| | | #define BUFFER_SIZE 255 // Define the payload size here |
| | | |
| | | |
| | |
| | | // |
| | | // } |
| | | //} |
| | | uint8_t flag_writepara_needreset = 0,wg_lost_count; |
| | | uint32_t lora_zhuangtai; |
| | | uint16_t current_count; |
| | | extern wg_state_enum wg_state; |
| | | void OnTxDone( void ) |
| | | { |
| | | if(flag_writepara_needreset) //配置写入完成设备需要重启 |
| | | { |
| | | printf("网关下发配置写入完成,重启"); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | Radio.Standby(); |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | Radio.Rx( 10 ); |
| | | if(wg_lost_count++>3) |
| | | { |
| | | wg_state = WG_Lost; |
| | | } |
| | | } |
| | | |
| | | extern uint32_t lp_time; |
| | | uint32_t lora_txtime_old; |
| | | extern uint8_t frame_seq_nb2,rec_nearbase_num,ancidlist_num; |
| | | uint16_t RX_TIMEOUTs; |
| | | extern LPTIM_HandleTypeDef hlptim1; |
| | | uint16_t current_time,start_time,end_time; |
| | | uint16_t lora_recv_devid=0; |
| | | u32 id; |
| | | extern uint8_t Lora_tx_ancnum; |
| | | extern uint16_t Lora_tx_ancid[50]; |
| | | extern uint16_t Lora_tx_ancdist[50]; |
| | | uint8_t power_and_key=0; |
| | | extern u8 battary,button; |
| | | u8 lora_seq_nb2; |
| | | extern uint8_t stationary_flag; |
| | | extern uint8_t anjian_flag; |
| | | uint16_t Lora_wangguanid=0; |
| | | |
| | | extern uint8_t lora_tx_flag; |
| | | uint16_t current_count; |
| | | u16 Lora_recevcount; |
| | | extern uint16_t no_data_chongqi_num; |
| | | uint8_t du_flag; |
| | | uint8_t xie_flag; |
| | | extern uint16_t wg_report_id,wg_report_freq; |
| | | extern uint8_t no_yingdaflag; |
| | | uint16_t Beepchixutime; |
| | | uint8_t xiafayuyin_data[200]; |
| | | uint8_t yuyin_sendflag; |
| | | uint8_t yuyinzidongguan_time; |
| | | uint8_t yuyin_no_sleep_flag; |
| | | uint8_t yuyin_no_sleep_flag,no_rx_flag; |
| | | extern uint8_t usart5_state; |
| | | extern uint8_t lora_sendbuffer[200]; |
| | | void LoraSendComMap(uint8_t cmd) |
| | | { |
| | | uint8_t data_length = 0x28; |
| | | uint16_t checksum = 0; |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_READPARARESP; |
| | | lora_sendbuffer[MSG_LENGTH] = data_length+9; |
| | | 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); |
| | | // Delay_Ms(100); |
| | | } |
| | | void LoraRspWriteCommap(uint8_t index) |
| | | { |
| | | uint8_t data_length = 2; |
| | | uint16_t checksum = 0; |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_READPARARESP; |
| | | lora_sendbuffer[MSG_LENGTH] = data_length+9; |
| | | 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] = WGRSP_RWTAG_WRITE; |
| | | lora_sendbuffer[RP_INDEX_IDX] = index; |
| | | lora_sendbuffer[RP_LEN_IDX] = data_length; |
| | | memcpy(&lora_sendbuffer[7], &g_com_map[index/2], 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); |
| | | // Delay_Ms(100); |
| | | } |
| | | static uint16_t delaytime = 1125; |
| | | static uint16_t source_id; |
| | | uint8_t rec_index; |
| | | uint16_t rec_value,rec_delaytime,target_count,rx_count; |
| | | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| | | { |
| | | BufferSize = size; |
| | |
| | | RssiValue = rssi; |
| | | SnrValue = snr; |
| | | Radio.Standby(); |
| | | //printf("收到lora\r\n"); |
| | | // if(Lora_status==LORA_RX_433) |
| | | no_data_chongqi_num=0; |
| | | no_rx_flag = 0; |
| | | if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_RANGEPOLL) |
| | | { |
| | | UWB_Wkup(); |
| | |
| | | { |
| | | if(!memcmp(&dev_id,&RX_Buffer[DEST_ID_IDX],2)) |
| | | { |
| | | if(RX_Buffer[6]==0)//不需要操作,正常应答 |
| | | memcpy(&source_id,&RX_Buffer[SOURCE_ID_IDX],2); |
| | | if(wg_state==WG_Lost) |
| | | { |
| | | Lora_rece_error=0; |
| | | memcpy( &Lora_wangguanid, &RX_Buffer[2], 2 ); |
| | | wg_state = WG_Connected; |
| | | wg_report_id = source_id; |
| | | } |
| | | if(RX_Buffer[6]==2) //读操作,下一次lora发送的时候要发出去 |
| | | { |
| | | // no_yingda_time=5; |
| | | Lora_rece_error=0; |
| | | if(no_yingdaflag==0) |
| | | if(!memcmp(&wg_report_id,&RX_Buffer[SOURCE_ID_IDX],2)) |
| | | { |
| | | wg_lost_count = 0; |
| | | switch(RX_Buffer[PWTAG_RW_FLAG_IDX]) |
| | | { |
| | | du_flag=1; |
| | | LoraSendComMap(RX_Buffer[8],RX_Buffer[7]>>1); |
| | | } |
| | | } |
| | | if(RX_Buffer[6]==1)//写操作 |
| | | { |
| | | Lora_rece_error=0; |
| | | |
| | | if(no_yingdaflag==0) |
| | | { |
| | | if(RX_Buffer[7]==0x10) |
| | | { |
| | | |
| | | } |
| | | else |
| | | { |
| | | if(RX_Buffer[7]==0xDD)//下发语音 |
| | | { |
| | | case WGRSP_RWTAG_NONE: |
| | | 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-=32768; |
| | | __HAL_LPTIM_COMPARE_SET(&hlptim1, target_count); |
| | | rx_count = HAL_LPTIM_ReadCounter(&hlptim1); |
| | | // printf("tx %d,rx %d,delay %d",current_count>>5,rx_count>>5,rec_delaytime); |
| | | break; |
| | | case WGRSP_RWTAG_READ: |
| | | no_rx_flag = 1; |
| | | LoraSendComMap(WGRSP_RWTAG_READ); |
| | | break; |
| | | case WGRSP_RWTAG_WRITE: |
| | | rec_index = RX_Buffer[PWTAG_WRITE_IDX_IDX]; |
| | | if(rec_index==0xDD) |
| | | { |
| | | if(usart5_state==1) |
| | | { |
| | | Usart5Init(); |
| | | usart5_state=0; |
| | | Usart5Init(); |
| | | usart5_state=0; |
| | | } |
| | | 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]); |
| | | yuyin_sendflag=1; |
| | | yuyinzidongguan_time=0; |
| | | yuyin_no_sleep_flag=1; |
| | | } |
| | | else |
| | | { |
| | | uint16_t pinlv1; |
| | | if(RX_Buffer[7]==0x06) |
| | | { |
| | | memcpy(&pinlv1, &RX_Buffer[9], RX_Buffer[8]); |
| | | pinlv1=(1000/pinlv1)*1000; |
| | | memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &pinlv1, RX_Buffer[8]); |
| | | 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]); |
| | | yuyin_sendflag=1; |
| | | yuyinzidongguan_time=0; |
| | | yuyin_no_sleep_flag=1; |
| | | }else{ |
| | | memcpy(&rec_value,&RX_Buffer[PWTAG_WRITE_VALUE_IDX],2); |
| | | g_com_map[rec_index/2] = rec_value; |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | LoraRspWriteCommap(SUBMSG_WRITE_ANCPARA); |
| | | flag_writepara_needreset = 1; |
| | | no_rx_flag = 1; |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | } |
| | | Radio.Rx(0); |
| | | if(!no_rx_flag) |
| | | { |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | Radio.Rx(0); |
| | | } |
| | | } |
| | | |
| | | void OnTxTimeout( void ) |
| | | { |
| | | Radio.Standby(); |
| | | Radio.Standby(); |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | } |
| | | uint8_t tm; |
| | |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | } |
| | | |
| | | |
| | | uint16_t freq_test; |
| | | void SwitchLoraSettings(uint32_t freq,uint8_t sf,uint8_t power) |
| | | { |
| | | |
| | |
| | | Radio.Standby(); |
| | | // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); |
| | | Radio.SetChannel( freq*1000000 ); |
| | | |
| | | freq_test = freq; |
| | | Radio.SetTxConfig( MODEM_LORA, power, 0, 2, |
| | | sf, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON, |
| | | false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 ); |
| | |
| | | |
| | | Radio.Init( &RadioEvents ); |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],5,0); //切换lora接收频点 |
| | | Radio.Rx( 9999 ); |
| | | Radio.Rx( 0 ); |
| | | // Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); |
| | | |
| | | } |