| | |
| | | #include "main.h" |
| | | #include <stdlib.h> |
| | | #include "dw_mbx_tag.h" |
| | | #include "Flash.h" |
| | | static RadioEvents_t RadioEvents; |
| | | #define USE_MODEM_LORA |
| | | //#define USE_MODEM_FSK |
| | | #define REGION_CN779 |
| | | void LoraUp_Poll(); |
| | | void Lora47xrx_Init(void); |
| | | void Lora433_change(void); |
| | | void Lora433rx_Init(void); |
| | |
| | | extern uint8_t lora_yingda_flag; |
| | | uint16_t lora_yingda_num; |
| | | uint16_t crc_value; |
| | | /*! |
| | | * Radio events function pointer |
| | | */ |
| | | /** |
| | | * @brief Update CRC16 for input byte |
| | | * @param CRC input value |
| | | * @param input byte |
| | | * @retval Updated CRC value |
| | | */ |
| | | uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte) |
| | | { |
| | | uint32_t crc = crcIn; |
| | | uint32_t in = byte|0x100; |
| | | |
| | | do |
| | | { |
| | | crc <<= 1; |
| | | in <<= 1; |
| | | |
| | | if(in&0x100) |
| | | { |
| | | ++crc; |
| | | } |
| | | |
| | | if(crc&0x10000) |
| | | { |
| | | crc ^= 0x1021; |
| | | } |
| | | } while(!(in&0x10000)); |
| | | |
| | | return (crc&0xffffu); |
| | | } |
| | | |
| | | /** |
| | | * @brief Cal CRC16 for YModem Packet |
| | | * @param data |
| | | * @param length |
| | | * @retval CRC value |
| | | */ |
| | | uint16_t Cal_CRC16(const uint8_t* data, uint32_t size) |
| | | { |
| | | uint32_t crc = 0; |
| | | const uint8_t* dataEnd = data+size; |
| | | |
| | | while(data<dataEnd) |
| | | { |
| | | crc = UpdateCRC16(crc,*data++); |
| | | } |
| | | crc = UpdateCRC16(crc,0); |
| | | crc = UpdateCRC16(crc,0); |
| | | |
| | | return (crc&0xffffu); |
| | | } |
| | | static RadioEvents_t RadioEvents; |
| | | |
| | | #if defined( USE_MODEM_LORA ) |
| | |
| | | uint8_t lora_chongfuyingda_flag; |
| | | uint8_t Lora_rx_open_flag; |
| | | extern u32 Loratx_frequency; |
| | | |
| | | uint8_t huifushengjibao_flag; |
| | | uint8_t LoraUp_flag; |
| | | uint8_t lora_up_rec_flag; |
| | | uint8_t flag_writepara_needreset = 0; |
| | | uint32_t wg_lost_count = 10; |
| | | uint32_t lora_zhuangtai; |
| | |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | Radio.Standby(); |
| | | if(LoraUp_flag) |
| | | { |
| | | huifushengjibao_flag=1; |
| | | Radio.Rx( 500 ); |
| | | } |
| | | else |
| | | { |
| | | Radio.Rx( 50 ); |
| | | LED_TB_OFF; |
| | | } |
| | | } |
| | | extern LPTIM_HandleTypeDef hlptim1; |
| | | uint16_t current_time,start_time,end_time; |
| | |
| | | SnrValue = snr; |
| | | Radio.Standby(); |
| | | no_rx_flag = 0; |
| | | lora_up_rec_flag=1; |
| | | if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_RANGEPOLL) |
| | | { |
| | | UWB_Wkup(); |
| | |
| | | rec_index = RX_Buffer[PWTAG_WRITE_IDX_IDX]; |
| | | switch(rec_index) |
| | | { |
| | | case 0xaa: //升级下发 |
| | | LoraUp_Poll(); |
| | | break; |
| | | case 0xdd: //语音下发 |
| | | break; |
| | | case 0x20: //蜂鸣 |
| | |
| | | } |
| | | if(!no_rx_flag) |
| | | { |
| | | if(lora_jianting_flag) |
| | | if(lora_jianting_flag&&LoraUp_flag==0) |
| | | { |
| | | SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0); |
| | | Radio.Rx(0); |
| | |
| | | // Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time); |
| | | |
| | | } |
| | | |
| | | uint16_t rec_wenjian_daxiao; |
| | | uint16_t wangguan_up_id; |
| | | uint8_t send_lora_data[250]; |
| | | //extern uint8_t lora_recbuffer[255]; |
| | | uint8_t muqiandeshengjibao; |
| | | static uint16_t current_count1,target_count1,end_count1,start_count1; |
| | | uint8_t huifushengjibaoerror_num; |
| | | uint8_t final_bag_num; |
| | | uint16_t testflag; |
| | | uint32_t Zhongjian_data[60]; |
| | | uint16_t app1_or_app2; |
| | | extern IWDG_HandleTypeDef hiwdg; |
| | | void LoraUp_Poll() |
| | | { uint16_t result ; |
| | | uint16_t crc16; |
| | | memcpy(&rec_wenjian_daxiao,&RX_Buffer[WRITEPARA_VALUE_IDX],2);//文件大小 |
| | | // rec_wenjian_daxiao=rxbuffer[WRITEPATE_VALUE_IDX]; |
| | | if(rec_wenjian_daxiao>0XAC00) |
| | | {}//文件过大,超出范围 |
| | | else |
| | | { |
| | | memcpy(&wangguan_up_id,&RX_Buffer[SOURCE_ID_IDX],2);//网关ID 占用2个字节 |
| | | send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATE_CONFIRM; |
| | | memcpy(&send_lora_data[SOURCE_ID_IDX],&dev_id,2);//网关ID 占用2个字节 |
| | | memcpy(&send_lora_data[DEST_ID_IDX],&wangguan_up_id,2);//标签或者基站的设备ID 2个字节 |
| | | // send_lora_data[MUQIAN_BAG]=0xFF; |
| | | crc16=Checksum_u16(send_lora_data,6); |
| | | memcpy(&send_lora_data[6],&crc16,2); |
| | | huifushengjibao_flag=0; |
| | | LoraUp_flag=1; |
| | | Radio.Send(send_lora_data,8);//基站发送升级确认回复 |
| | | Delay_Ms(1000); |
| | | SwitchLoraSettings(LORA_UPCHANNEL_FRQ,LORA_UPCHANNEL_SF,22); //切换lora升级频点 |
| | | delay_ms(200); |
| | | __disable_irq(); |
| | | // FLASH_Read(APP_1OR2_ADRESS,(uint8_t*)&app1_or_app2,2); |
| | | final_bag_num=(rec_wenjian_daxiao/220)+1;//计算一共要请求多少包 |
| | | while(1) |
| | | { |
| | | FLASH_Prepare(APP2_ADRESS, APP_SIZE); |
| | | delay_ms(500); |
| | | if(testflag==0) |
| | | {break;} |
| | | } |
| | | // FLASH_Write(APP2_ADRESS, (const uint8_t*)&tmp111, 2); |
| | | __enable_irq(); |
| | | 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[SOURCE_ID_IDX],&dev_id,2);//网关ID 占用2个字节 |
| | | memcpy(&send_lora_data[DEST_ID_IDX],&wangguan_up_id,2);//标签或者基站的设备ID 2个字节 |
| | | send_lora_data[MUQIAN_BAG]=muqiandeshengjibao; |
| | | crc16=Checksum_u16(send_lora_data,7); |
| | | memcpy(&send_lora_data[7],&crc16,2); |
| | | lora_up_rec_flag=0; |
| | | Radio.Send(send_lora_data,9);//基站发送请求第x包 |
| | | |
| | | 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; |
| | | } |
| | | delay_ms(100); |
| | | if(lora_up_rec_flag) |
| | | {break;} |
| | | Radio.Send(send_lora_data,9); |
| | | } |
| | | // while(1); |
| | | if(lora_up_rec_flag) |
| | | { |
| | | HAL_IWDG_Refresh(&hiwdg); |
| | | crc16=Cal_CRC16(RX_Buffer,DATA_IDX+ONE_BAG_DAXIAO); |
| | | if(muqiandeshengjibao==RX_Buffer[MUQIAN_BAG]&&!memcmp(&crc16,&RX_Buffer[DATA_IDX+ONE_BAG_DAXIAO],2)&&!memcmp(&dev_id,&RX_Buffer[DEST_ID_IDX],2)&& !memcmp(&wangguan_up_id,&RX_Buffer[SOURCE_ID_IDX],2)) |
| | | { |
| | | memcpy(Zhongjian_data,&RX_Buffer[DATA_IDX],ONE_BAG_DAXIAO); |
| | | // if(app1_or_app2==1) |
| | | // FLASH_Write(APP1_ADRESS+ONE_BAG_DAXIAO*rxbuffer[MUQIAN_BAG],(uint8_t*)Zhongjian_data,ONE_BAG_DAXIAO); |
| | | // else |
| | | FLASH_Write(APP2_ADRESS+ONE_BAG_DAXIAO*RX_Buffer[MUQIAN_BAG],(uint8_t*)Zhongjian_data,ONE_BAG_DAXIAO); |
| | | |
| | | muqiandeshengjibao++; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | HAL_IWDG_Refresh(&hiwdg); |
| | | huifushengjibaoerror_num++; |
| | | if(huifushengjibaoerror_num>=20) |
| | | { |
| | | huifushengjibaoerror_num=0; |
| | | muqiandeshengjibao=0; |
| | | Delay_Ms(500); |
| | | HAL_NVIC_SystemReset(); |
| | | break; |
| | | } |
| | | } |
| | | if(muqiandeshengjibao==final_bag_num) |
| | | { |
| | | uint16_t tmp11 ,result11; |
| | | tmp11=0XBB; |
| | | result11 = FLASH_Prepare(APP_1OR2_ADRESS, PAGE_SIZE); |
| | | if(result11) |
| | | result11 = FLASH_Write(APP_1OR2_ADRESS, (const uint8_t*)&tmp11, 2); |
| | | Delay_Ms(1500); |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |