| | |
| | | // 3: 4/7, |
| | | // 4: 4/8] |
| | | #define LORA_PREAMBLE_LENGTH_T 8 // 96 // Same for Tx |
| | | #define LORA_PREAMBLE_LENGTH_R 64 // Same for Rx |
| | | #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 |
| | |
| | | { |
| | | // HAL_GPIO_TogglePin(GPIOB, LED1_Pin); |
| | | } |
| | | uint8_t tttt; |
| | | |
| | | |
| | | |
| | | uint8_t huifushengjibao_flag; |
| | | uint8_t LoraUp_flag; |
| | | void OnTxDone( void ) |
| | | { |
| | | // T_R_Init(0); |
| | | // Radio.Standby(); |
| | | // Radio.Rx( RX_TIMEOUT_VALUE ); //进入接收 |
| | | Radio.Sleep( ); |
| | | tttt++; |
| | | if(LoraUp_flag) |
| | | { |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | huifushengjibao_flag=1; |
| | | Radio.Rx( 500 ); |
| | | } |
| | | else |
| | | { Radio.Sleep( );} |
| | | //printf("TX SUCCESS2\r\n"); |
| | | } |
| | | static uint8_t rxbuffer[255],rxbuff_len; |
| | |
| | | memcpy(lp_count,&rec_lp_count,2); |
| | | return rxbuff_len; |
| | | } |
| | | uint8_t lora_up_rec_flag; |
| | | extern uint16_t tx_num; |
| | | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| | | { |
| | |
| | | SnrValue = snr; |
| | | |
| | | Radio.Standby(); |
| | | |
| | | lora_up_rec_flag=1; |
| | | flag_lora_wait_sync = 0; |
| | | SetFlagSyncSuccess(1); |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | |
| | | // if(LoraUp_flag) |
| | | // { |
| | | // |
| | | // } |
| | | } |
| | | |
| | | void OnTxTimeout( void ) |
| | |
| | | } |
| | | void SwitchLoraSettings(uint32_t freq,uint8_t sf) |
| | | { |
| | | RadioEvents.TxDone = OnTxDone; |
| | | RadioEvents.RxDone = OnRxDone; |
| | | RadioEvents.TxTimeout = OnTxTimeout; |
| | | RadioEvents.RxTimeout = OnRxTimeout; |
| | | RadioEvents.RxError = OnRxError; |
| | | Radio.Init( &RadioEvents ); |
| | | Radio.SetChannel( freq ); |
| | | |
| | | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_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.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, |
| | | sf,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 SwitchLoraSettingstest(uint32_t freq,uint8_t sf) |
| | | { |
| | | RadioEvents.TxDone = OnTxDone; |
| | | RadioEvents.RxDone = OnRxDone; |
| | | RadioEvents.TxTimeout = OnTxTimeout; |
| | | RadioEvents.RxTimeout = OnRxTimeout; |
| | | RadioEvents.RxError = OnRxError; |
| | | Radio.Init( &RadioEvents ); |
| | |
| | | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, |
| | | 0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 1 ); |
| | | |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | } |
| | | |
| | | uint16_t rec_wenjian_daxiao; |
| | | uint16_t wangguan_up_id; |
| | | uint8_t send_lora_data[250]; |
| | | extern uint8_t lora_recbuffer[255]; |
| | | uint8_t mudeshengjibao; |
| | | uint8_t muqiandeshengjibao; |
| | | static uint16_t current_count1,target_count1,end_count1,start_count1; |
| | | uint8_t huifushengjibaoerror_num; |
| | | uint8_t final_bag_num; |
| | | void LoraUp_Poll() |
| | | { |
| | | { uint16_t result ; |
| | | uint16_t crc16; |
| | | rec_wenjian_daxiao=lora_recbuffer[WRITEPATE_VALUE_IDX]; |
| | | wangguan_up_id=lora_recbuffer[WG_ID_IDX]; |
| | | memcpy(&rec_wenjian_daxiao,&rxbuffer[WRITEPATE_VALUE_IDX],2);//网关ID 占用2个字节 |
| | | // rec_wenjian_daxiao=rxbuffer[WRITEPATE_VALUE_IDX]; |
| | | if(rec_wenjian_daxiao>0XAC00) |
| | | {}//文件过大,超出范围 |
| | | else |
| | | { |
| | | final_bag_num=(rec_wenjian_daxiao/220)+1;//计算一共要请求多少包 |
| | | wangguan_up_id=rxbuffer[WG_ID_IDX]; |
| | | send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATE_CONFIRM; |
| | | memcpy(&send_lora_data[WANGGUAN_ID],&wangguan_up_id,2);//网关ID 占用2个字节 |
| | | memcpy(&send_lora_data[JIZHAN_ID],&dev_id,2);//标签或者基站的设备ID 2个字节 |
| | | crc16=Cal_CRC16(send_lora_data,5); |
| | | memcpy(&send_lora_data[5],&crc16,2); |
| | | send_lora_data[MUQIAN_BAG]=0xFF; |
| | | crc16=Cal_CRC16(send_lora_data,6); |
| | | memcpy(&send_lora_data[6],&crc16,2); |
| | | huifushengjibao_flag=0; |
| | | LoraUp_flag=1; |
| | | Radio.Send(send_lora_data,7);//基站发送升级确认回复 |
| | | while(1) |
| | | { |
| | | |
| | | send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_REQUEST; |
| | | send_lora_data[MUQIAN_BAG]=mudeshengjibao; |
| | | Radio.Send(send_lora_data,8);//基站发送升级确认回复 |
| | | while(1) |
| | | { |
| | | if(huifushengjibao_flag==1) |
| | | {lora_up_rec_flag=1; |
| | | while(1) |
| | | { |
| | | send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_REQUEST; |
| | | memcpy(&send_lora_data[WANGGUAN_ID],&wangguan_up_id,2);//网关ID 占用2个字节 |
| | | memcpy(&send_lora_data[JIZHAN_ID],&dev_id,2);//标签或者基站的设备ID 2个字节 |
| | | send_lora_data[MUQIAN_BAG]=muqiandeshengjibao; |
| | | crc16=Cal_CRC16(send_lora_data,6); |
| | | memcpy(&send_lora_data[6],&crc16,2); |
| | | lora_up_rec_flag=0; |
| | | Radio.Send(send_lora_data,8);//基站发送升级确认回复 |
| | | |
| | | start_count1=HAL_LPTIM_ReadCounter(&hlptim1); //等待接收完成直到超时 |
| | | end_count1=start_count1+32768/2;//500ms等待 |
| | | if(end_count1>=32768) |
| | | {end_count1-=32768;} |
| | | current_count1=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | while(!lora_up_rec_flag) |
| | | { |
| | | current_count1=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_count1>=end_count1&¤t_count1<end_count1+15000) |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | if(lora_up_rec_flag) |
| | | { |
| | | crc16=Cal_CRC16(rxbuffer,6+ONE_BAG_DAXIAO); |
| | | if(!memcmp(&crc16,&rxbuffer[6+ONE_BAG_DAXIAO],2)&&!memcmp(&dev_id,&RX_Buffer[JIZHAN_ID],2)&& !memcmp(&wangguan_up_id,&RX_Buffer[WANGGUAN_ID],2)) |
| | | { |
| | | result = FLASH_Prepare(APP2_ADRESS+ONE_BAG_DAXIAO*rxbuffer[MUQIAN_BAG], ONE_BAG_DAXIAO); |
| | | if(result) |
| | | result = FLASH_Write(APP2_ADRESS+ONE_BAG_DAXIAO*rxbuffer[MUQIAN_BAG],rxbuffer[DATA_IDX],ONE_BAG_DAXIAO); |
| | | muqiandeshengjibao++; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | huifushengjibaoerror_num++; |
| | | if(huifushengjibaoerror_num>=20) |
| | | { |
| | | huifushengjibaoerror_num=0; |
| | | break; |
| | | } |
| | | } |
| | | if(muqiandeshengjibao==final_bag_num) |
| | | { |
| | | Delay_Ms(500); |
| | | HAL_NVIC_SystemReset(); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | Delay_Ms(500); |
| | | huifushengjibao_flag=0; |
| | | Radio.Send(send_lora_data,8);//基站发送升级确认回复 |
| | | huifushengjibaoerror_num++; |
| | | if(huifushengjibaoerror_num>=5) |
| | | { |
| | | huifushengjibaoerror_num=0; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | // LoraUp_flag=1; |
| | | // SetFlagLoraWaitSync(1); |
| | | // while(1) |
| | | // { |
| | | // start_count1=HAL_LPTIM_ReadCounter(&hlptim1); //等待接收完成直到超时 |
| | | //// poll_timeout=500*10; //单位0.1ms |
| | | // end_count1=start_count1+(5000<<2); |
| | | // if(end_count1>=32768) |
| | | // {end_count1-=32768;} |
| | | // current_count1=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | // while(GetFlagLoraWaitSync()) |
| | | // { |
| | | // current_count1=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | // if(current_count1>=end_count1&¤t_count1<end_count1+15000) |
| | | // { |
| | | // break; |
| | | // } |
| | | // } |
| | | // if(!GetFlagLoraWaitSync()) |
| | | // { |
| | | // send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_REQUEST; |
| | | // memcpy(&send_lora_data[WANGGUAN_ID],&wangguan_up_id,2);//网关ID 占用2个字节 |
| | | // memcpy(&send_lora_data[JIZHAN_ID],&dev_id,2);//标签或者基站的设备ID 2个字节 |
| | | // send_lora_data[MUQIAN_BAG]=mudeshengjibao; |
| | | // crc16=Cal_CRC16(send_lora_data,6); |
| | | // memcpy(&send_lora_data[6],&crc16,2); |
| | | // |
| | | // } |
| | | // } |
| | | } |
| | | } |
| | | |
| | | void test1() |
| | | { |
| | | if(!memcmp(&dev_id,&rxbuffer[ANCTAG_DEVICEID_IDX],2)) |
| | | {LoraUp_Poll();} |
| | | } |