| | |
| | | #include "lora_3029.h" |
| | | #include "mk_spi.h" |
| | | #include "global_param.h" |
| | | #include "pan_port.h" |
| | | //#include "WS2812.h" |
| | | //#include "pan_port.h" |
| | | //#include "pan_param.h" |
| | | //#include "pan_rf.h" |
| | | //#define RX_TIMEOUT_VALUE 1000 |
| | | //extern LPTIM_HandleTypeDef hlptim1; |
| | | /***lora接收,发送变量定义******/ |
| | | #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]; |
| | | |
| | | int8_t RssiValue = 0; |
| | | int8_t SnrValue = 0; |
| | | extern uint32_t current_count; |
| | | |
| | | extern uint32_t dev_id; |
| | | uint8_t yuyin_no_sleep_flag,no_rx_flag; |
| | | uint8_t lora_up_rec_flag; |
| | | static uint16_t source_id; |
| | | /***lora接收,发送变量定义******/ |
| | | uint32_t wg_lost_count = 10; |
| | | static uint8_t tx_buf[10] = {0x55, 0x44, 0x33, 0x22, 0x11}; |
| | | static uint8_t rx_buf[10] = {0x00}; |
| | | //extern uint32_t uwbled,gpsled,loraled,powerled; |
| | | extern uint8_t bat_percent,taglist_num; |
| | | uint8_t report_ancnum; |
| | | uint16_t report_ancdist[ANC_MAX_NUM],report_ancid[ANC_MAX_NUM]; |
| | | extern void spi_transfer_callback(void *dev, uint32_t err_code); |
| | | void Lora_init(void) |
| | | { |
| | |
| | | // uint8_t rx_data = 0; // 用于存储接收的数据 |
| | | // spi_transfer(SPI_ID0, 0x09, rx_buf, 1, spi_transfer_callback); |
| | | uint32_t ret = 0; |
| | | uint32_t cnt = 0; |
| | | uint32_t cnt = 0; |
| | | ret=rf_init(); |
| | | set_test_mode1_reg(); |
| | | if(ret== 1) |
| | | { |
| | | // printf("LORA Init Fail"); |
| | |
| | | // printf("LORA Init OK\r\n"); |
| | | } |
| | | } |
| | | wg_state_enum wg_state = WG_Lost; |
| | | uint8_t lora_sendbuffer[200]; |
| | | uint8_t seq_num; |
| | | extern uint16_t lora_up_uwb_flag; |
| | | //extern uint8_t wg_lost_count; |
| | | uint16_t wg_report_freq,wg_report_id; |
| | | uint8_t wg_report_sf; |
| | | #define WG_LOST_SWITCH_THRES 3 |
| | | #define WG_LOST_NOUWB_COUNT 60 |
| | | #define DEFAULT_WG_ID 0xFFFF |
| | | #define DEFAULT_LR_WG_ID 0xFFFE |
| | | uint8_t lora_jianting_flag = 1; |
| | | void LoraReportFreqPoll(void) |
| | | { |
| | | // wg_state = WG_Lost; |
| | | if(wg_lost_count++>WG_LOST_NOUWB_COUNT) |
| | | { |
| | | lora_jianting_flag = 0; |
| | | } else { |
| | | lora_jianting_flag = 1; |
| | | } |
| | | if(wg_state==WG_Lost) |
| | | { |
| | | static uint8_t channel_switch_count = 0; |
| | | // channel_switch_count = 10; |
| | | if(channel_switch_count++>1) |
| | | { |
| | | // channel_switch_count = 0; |
| | | // wg_report_id = DEFAULT_LR_WG_ID; |
| | | // wg_report_freq = LR_DATA_CHANNEL_FRQ; //如果丢失链接就进入WG管理信道。 |
| | | // wg_report_sf = LR_DATA_CHANNEL_SF; |
| | | } |
| | | else |
| | | { |
| | | wg_report_id = DEFAULT_WG_ID; |
| | | wg_report_freq = REPORT_MANGE_CHANNEL_FRQ; //如果丢失链接就进入WG管理信道。 |
| | | wg_report_sf = REPORT_CHANNEL_SF; |
| | | } |
| | | } else { |
| | | if(wg_lost_count>WG_LOST_SWITCH_THRES) |
| | | { |
| | | wg_state = WG_Lost; |
| | | } |
| | | } |
| | | } |
| | | static uint16_t checksum; |
| | | |
| | | //uint16_t wg_report_freq,wg_report_id; |
| | | uint8_t lora_sendbuffer[200]; |
| | | void Lora_UploadHeartBeartPoll(void) |
| | | { |
| | | |
| | | // lora_up_uwb_flag=2; |
| | | // rf_set_default_para(); |
| | | |
| | | rf_enter_continous_tx(); |
| | | /*****************************心跳包上传内容************************************/ |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGHEARTBEAT; |
| | | lora_sendbuffer[MSG_LENGTH] = 14; |
| | | memcpy(&lora_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2); |
| | | memcpy(&lora_sendbuffer[DEST_ID_IDX],&wg_report_id,2); |
| | | memcpy(&lora_sendbuffer[HB_VERSION_IDX],&g_com_map[VERSION],2); |
| | | lora_sendbuffer[HB_UWBPOWER_IDX] = g_com_map[POWER]; |
| | | lora_sendbuffer[HB_LORAPOWER_IDX] = g_com_map[LORA_POWER]; |
| | | checksum = Checksum_u16(lora_sendbuffer,14); |
| | | memcpy(&lora_sendbuffer[14],&checksum,2); |
| | | /*****************************心跳包上传内容*************************************/ |
| | | rf_set_transmit_flag(RADIO_FLAG_TXDONE); |
| | | |
| | | if(rf_get_transmit_flag()==RADIO_FLAG_TXDONE) |
| | | { |
| | | if(rf_continous_tx_send_data(lora_sendbuffer, 16)==OK) |
| | | { |
| | | //LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n"); |
| | | } |
| | | while (rf_get_transmit_flag() == RADIO_FLAG_IDLE) |
| | | ; |
| | | //rf_enter_continous_rx();; |
| | | rf_set_transmit_flag(RADIO_FLAG_IDLE); |
| | | } |
| | | |
| | | } |
| | | #define HEATBEAT_UPDATE_TIME 3600 |
| | | uint16_t heatbeat_count = HEATBEAT_UPDATE_TIME-1 ; |
| | | uint16_t flag_getwgresp = 0; |
| | | int16_t intheight; //气压 |
| | | void Lora_Report_poll(void) |
| | | { |
| | | LoraReportFreqPoll(); |
| | | flag_getwgresp = 0; //网关回复标志位 && WG_Connected == wg_state |
| | | uint32_t ret = 0; |
| | | ret = rf_init(); |
| | | |
| | | set_test_mode1_reg(); |
| | | if(ret==OK) |
| | | { |
| | | rf_set_default_para(wg_report_freq*1000000,wg_report_sf); |
| | | } |
| | | // |
| | | // |
| | | if(heatbeat_count++>HEATBEAT_UPDATE_TIME) //如果心跳包到达上传时间,并且网关处于链接状态,就上传心跳包 |
| | | { |
| | | heatbeat_count = 0; |
| | | Lora_UploadHeartBeartPoll(); |
| | | //rf_irq_process(); |
| | | return; |
| | | } |
| | | for(uint16_t i=0; i<report_ancnum-1; i++) |
| | | { |
| | | for(uint16_t j=0; j<report_ancnum-1-i; j++) |
| | | { |
| | | if(report_ancdist[j]>report_ancdist[j+1]) |
| | | { |
| | | uint16_t id,dist; |
| | | uint8_t bat; |
| | | id = report_ancid[j]; |
| | | dist = report_ancdist[j]; |
| | | report_ancid[j] = report_ancid[j+1]; |
| | | report_ancdist[j] = report_ancdist[j+1]; |
| | | report_ancid[j+1] = id; |
| | | report_ancdist[j+1] = dist; |
| | | } |
| | | } |
| | | } |
| | | if(report_ancnum>0) |
| | | { |
| | | //uwbled=0x11; //blue |
| | | } |
| | | else |
| | | { |
| | | //uwbled=0x1100; //red |
| | | } |
| | | if(report_ancnum>LORA_REPORT_MAXANC_NUM) //考虑lora传输时间,最多发送10个基站数据。 |
| | | report_ancnum = LORA_REPORT_MAXANC_NUM; |
| | | |
| | | #ifdef USE_GPS |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG_GPS; |
| | | lora_sendbuffer[MSG_LENGTH] = 4*report_ancnum+ANCID_IDX; |
| | | #else |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG; |
| | | lora_sendbuffer[MSG_LENGTH] = 4*report_ancnum+ANCID_IDX; |
| | | #endif |
| | | |
| | | // 2000f026 02020002 //000220200 |
| | | memcpy(&lora_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2); |
| | | memcpy(&lora_sendbuffer[DEST_ID_IDX],&wg_report_id,2); |
| | | lora_sendbuffer[SEQNUM_IDX] = seq_num++; |
| | | lora_sendbuffer[BAT_IDX] = bat_percent; |
| | | // lora_sendbuffer[STATE_IDX] = !GET_USERKEY|stationary_flag<<1; |
| | | #ifdef USE_GPS |
| | | memcpy(&lora_sendbuffer[GPS_JINGDU_IDX],&gps_jingdu,8); |
| | | memcpy(&lora_sendbuffer[GPS_WEIDU_IDX],&gps_weidu,8); |
| | | memcpy(&lora_sendbuffer[GPS_HEIGHT_IDX],&gps_height,8); |
| | | lora_sendbuffer[GPS_STATE_IDX] = gps_state; |
| | | lora_sendbuffer[GPS_SATEL_NUM_IDX] = gps_satel_num; |
| | | lora_sendbuffer[GPS_SPOWER_IDX] = gps_signalpower; |
| | | lora_sendbuffer[GPS_CHAFENLINGQI] = gps_chafenlingqi; |
| | | #endif |
| | | |
| | | memcpy(&lora_sendbuffer[BAR_HEIGHT_IDX],&intheight,2); |
| | | lora_sendbuffer[ANCNUM_IDX] = report_ancnum; |
| | | |
| | | memcpy(&lora_sendbuffer[ANCID_IDX],report_ancid,report_ancnum*2); |
| | | memcpy(&lora_sendbuffer[ANCID_IDX+report_ancnum*2],report_ancdist,report_ancnum*2); |
| | | |
| | | checksum = Checksum_u16(lora_sendbuffer,4*report_ancnum+ANCID_IDX); |
| | | memcpy(&lora_sendbuffer[ANCID_IDX+report_ancnum*4],&checksum,2); |
| | | rf_enter_continous_tx(); |
| | | //rf_irq_process(); |
| | | rf_set_transmit_flag(RADIO_FLAG_TXDONE); |
| | | |
| | | if(rf_get_transmit_flag()==RADIO_FLAG_TXDONE) |
| | | { |
| | | if(rf_continous_tx_send_data(lora_sendbuffer,ANCID_IDX+report_ancnum*4+2)==OK) |
| | | { |
| | | //LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n"); |
| | | } |
| | | while (rf_get_transmit_flag() == RADIO_FLAG_IDLE) |
| | | ; |
| | | //rf_enter_continous_rx();; |
| | | rf_set_transmit_flag(RADIO_FLAG_IDLE); |
| | | |
| | | } |
| | | // rf_enter_continous_rx(); |
| | | |
| | | } |
| | | extern uint8_t report_ancnum; |
| | | static uint16_t delaytime = 1200; |
| | | static uint16_t source_id; |
| | | uint8_t rec_index,rec_secdelay; |
| | | uint16_t rec_value,rec_delaytime,rx_count; |
| | | uint16_t rec_value,rec_delaytime,rx_count,datalen_offset; |
| | | int32_t target_count; |
| | | uint8_t shengji_flag,time=5; |
| | | extern uint32_t uwbled,gpsled,loraled,powerled; |
| | | extern struct RxDoneMsg RxDoneParams; |
| | | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| | | { uint16_t checksum1; |
| | | BufferSize = size; |
| | | memcpy(RX_Buffer,RxDoneParams.Payload,RxDoneParams.Size); |
| | | // memcpy( RX_Buffer, payload, BufferSize ); |
| | | RssiValue = rssi; |
| | | SnrValue = snr; |
| | | //Radio.Standby(); |
| | | no_rx_flag = 0; |
| | | lora_up_rec_flag=1; |
| | | // if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_RANGEPOLL) |
| | | // { |
| | | // UwbRange(); |
| | | // rf_delay_us(time*1000); |
| | | // // UWB_RXStart(10); |
| | | // // delay_ms(time); |
| | | // } |
| | | if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_WGRESPTAG) |
| | | { |
| | | checksum1=Checksum_u16(RX_Buffer,BufferSize-2); |
| | | if(!memcmp(&checksum1,&RX_Buffer[BufferSize-2],2)) |
| | | if(!memcmp(&dev_id,&RX_Buffer[DEST_ID_IDX],2)) |
| | | { |
| | | memcpy(&source_id,&RX_Buffer[SOURCE_ID_IDX],2); |
| | | if(wg_state==WG_Lost) |
| | | { |
| | | wg_state = WG_Connected; |
| | | wg_report_id = source_id; |
| | | } |
| | | if(!memcmp(&wg_report_id,&RX_Buffer[SOURCE_ID_IDX],2)) |
| | | { |
| | | wg_lost_count = 0; |
| | | // loraled=GREEN; |
| | | switch(RX_Buffer[PWTAG_RW_FLAG_IDX]) |
| | | { |
| | | 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); |
| | | rec_secdelay = RX_Buffer[PWTAG_SECDELAY_IDX]; |
| | | if(BufferSize!=13||rec_secdelay>20) |
| | | { |
| | | rec_secdelay = 0; |
| | | } |
| | | |
| | | if(report_ancnum<2) |
| | | { |
| | | datalen_offset = report_ancnum*85; |
| | | }else{ |
| | | datalen_offset = (report_ancnum-1)*46+85; |
| | | } |
| | | |
| | | target_count = current_count + rec_delaytime*3.2768 - delaytime-datalen_offset; |
| | | while(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<=DEV_ID) |
| | | return; |
| | | if(rec_index==0xDD) |
| | | { |
| | | // if(usart5_state==1) |
| | | // { |
| | | // 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[PWTAG_WRITE_VALUE_IDX],BufferSize-PWTAG_WRITE_VALUE_IDX-2); |
| | | // yuyin_sendflag=1; |
| | | // yuyinzidongguan_time=0; |
| | | // 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; |
| | | save_com_map_to_flash(); |
| | | // LoraRspWriteCommap(SUBMSG_WRITE_ANCPARA); |
| | | // flag_writepara_needreset = 1; |
| | | no_rx_flag = 1; |
| | | } |
| | | break; |
| | | case WGRSP_RWTAG_UPDATE: |
| | | rec_index = RX_Buffer[PWTAG_WRITE_IDX_IDX]; |
| | | switch(rec_index) |
| | | { |
| | | case 0xaa: //升级下发 |
| | | // memcpy(&rec_wenjian_daxiao,&RX_Buffer[WRITEPARA_VALUE_IDX],2);//文件大小 |
| | | // if(rec_wenjian_daxiao>APP_SIZE||rec_wenjian_daxiao==0) |
| | | // {}//文件过大,超出范围 |
| | | // else |
| | | // { |
| | | // memcpy(&wangguan_up_id,&RX_Buffer[SOURCE_ID_IDX],2);//网关ID 占用2个字节 |
| | | // shengji_flag=1; |
| | | // LoraUp_flag=1; |
| | | // } |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // if(!no_rx_flag&&LoraUp_flag==0) |
| | | // { |
| | | // if(RX_Buffer[MSG_TYPE_IDX]!=LORA_MSGTYPE_RANGEPOLL) |
| | | // SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | // Radio.Rx(0); |
| | | // } |
| | | } |
| | | //void LORA_mode_select(void) |
| | | //{ |
| | | // if(TX_RX_SELECT==1) |