| | |
| | | #include "stdio.h" |
| | | #include "delay.h" |
| | | #include <stdlib.h> |
| | | #include <Lora.h> |
| | | #include "radio.h" |
| | | #define NSH1 0x0001 |
| | | #define GP 0x0002 |
| | | #define FLASH_HARDWARE_VERSION_MAP (uint32_t)0x08004F00 //硬件版本号和批次信息位置 |
| | | //#define DEBUG_MODE |
| | | #define DEBUG_MODE |
| | | UART_WakeUpTypeDef Selection; |
| | | /* USER CODE END Includes */ |
| | | |
| | |
| | | return sum; |
| | | } |
| | | |
| | | u32 Loratx_frequency; |
| | | //u32 Loratx_frequency; |
| | | u8 active_flag,nomovesleeptime; |
| | | void GetLoratx_frequency(void) |
| | | { |
| | | Loratx_frequency=470000000; |
| | | srand(HAL_LPTIM_ReadCounter(&hlptim1)); |
| | | Loratx_frequency=((rand()%5)+470)*1000000; |
| | | if(470000000<=Loratx_frequency&&Loratx_frequency<480000000) |
| | | g_com_map[NEARBASE_ID1]=1136+(Loratx_frequency/1000000-470); |
| | | if(480000000<=Loratx_frequency&&Loratx_frequency<490000000) |
| | | g_com_map[NEARBASE_ID1]=1152+(Loratx_frequency/1000000-480); |
| | | Loratx_frequency=470000000; |
| | | } |
| | | //void GetLoratx_frequency(void) |
| | | //{ |
| | | //Loratx_frequency=470000000; |
| | | //srand(HAL_LPTIM_ReadCounter(&hlptim1)); |
| | | // Loratx_frequency=((rand()%5)+470)*1000000; |
| | | // if(470000000<=Loratx_frequency&&Loratx_frequency<480000000) |
| | | // g_com_map[NEARBASE_ID1]=1136+(Loratx_frequency/1000000-470); |
| | | // if(480000000<=Loratx_frequency&&Loratx_frequency<490000000) |
| | | // g_com_map[NEARBASE_ID1]=1152+(Loratx_frequency/1000000-480); |
| | | //Loratx_frequency=470000000; |
| | | //} |
| | | void Program_Init(void) |
| | | { float temp; |
| | | uint16_t temp2; |
| | |
| | | //deca_sleep(1000); |
| | | g_com_map[VERSION] = (1<<8)|0; |
| | | // g_com_map[DEV_ID]=1158; |
| | | #ifdef DEBUG_MODE |
| | | // g_com_map[GROUP_ID]=21; |
| | | // g_com_map[DEV_ID]=4; |
| | | // g_com_map[COM_INTERVAL]=100; |
| | | // g_com_map[MAX_REPORT_ANC_NUM]=1; |
| | | // g_com_map[NOMOVESLEEP_TIME]=10; |
| | | #endif |
| | | g_com_map[GROUP_ID]=5; |
| | | // g_com_map[COM_INTERVAL] = 1000; |
| | | if(g_com_map[COM_INTERVAL]<1000) |
| | |
| | | |
| | | current_slotnum=1; |
| | | // current_time=GetLPTime(); |
| | | if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 32767) != HAL_OK) //system time is 1010ms 1 puls=30.518us |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | // if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 0) != HAL_OK) //system time is 1010ms 1 puls=30.518us |
| | | // { |
| | | // Error_Handler(); |
| | | // } |
| | | |
| | | //g_com_map[NOMOVESLEEP_TIME]=5; |
| | | printf("固件版本: 免布线基站LORA版本 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff); |
| | | // printf("固件版本: 免布线LORA工卡 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff); |
| | | memcpy(g_com_map2,g_com_map,COM_MAP_SIZE); |
| | | // printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]); |
| | | // printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]); |
| | |
| | | if(GPIO_Pin == GPIO_PIN_3) |
| | | { |
| | | SystemClock_Config(); |
| | | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); |
| | | // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); |
| | | // HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效 |
| | | // l_bIsVibration = HIDO_TRUE; |
| | | // nomove_time=0; |
| | |
| | | } |
| | | if(GPIO_Pin == GPIO_PIN_1)//按键 |
| | | { |
| | | SystemClock_Config(); |
| | | anjian_flag=1; |
| | | } |
| | | if(GPIO_Pin == GPIO_PIN_0)//加速度 |
| | | { |
| | | SystemClock_Config(); |
| | | stationary_flag =0; |
| | | stationary_num=0; |
| | | } |
| | |
| | | extern uint8_t Lora_tx_anc_electricity[50]; |
| | | u8 lora_seq_nb1; |
| | | static uint8_t send_frame1[EUART_RX_BUF_SIZE]; |
| | | uint8_t data_length1; |
| | | void LoraSendComMap(uint8_t data_length, uint8_t index) |
| | | { |
| | | uint16_t checksum = 0; |
| | | send_frame1[0] = 0x55; |
| | | send_frame1[1] = 0xAA; |
| | | send_frame1[2] = 0x39; |
| | | send_frame1[3] = data_length+5; |
| | | send_frame1[4] = 3; |
| | | send_frame1[5] = index; |
| | | send_frame1[6] = data_length; |
| | | memcpy(&send_frame1[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame1[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame1[2],5+data_length); |
| | | memcpy(&send_frame1[7+data_length],&checksum,2); |
| | | data_length1=data_length+9; |
| | | // UART_PushFrame(send_frame1, data_length+9); |
| | | } |
| | | #define BleRxBufferSize 1 |
| | | uint8_t aRxBuffer[1],group_id,GPSRxBuffer[BleRxBufferSize]; |
| | | uint8_t ble_rx_success_flag; |
| | | uint8_t fangzhijinrushuimian_flag; |
| | | uint8_t GPS_zhongzhuandate[200]; |
| | | uint8_t GPS_zhongzhuannum; |
| | | uint8_t GPS_GGAdate[200]={1,2,3,4,5,6,7,8,9}; |
| | | uint8_t GPSchangdu; |
| | | uint8_t Close_RMC[20]="$CCMSG,RMC,1,0,*04\r\n"; |
| | | uint8_t Close_GSA[20]="$CCMSG,GSA,1,0,*0D\r\n"; |
| | | uint8_t Close_GSV[20]="$CCMSG,GSV,1,0,*1A\r\n"; |
| | | uint8_t Close_GLL[20]="$CCMSG,GLL,1,0,*1F\r\n"; |
| | | uint8_t Close_TXT[20]="$CCMSG,TXT,1,0,*00\r\n"; |
| | | uint8_t gps_ceju=0; |
| | | uint8_t no_yingdaflag; |
| | | void Lora_Tx_Poll() |
| | | { |
| | | |
| | | if(lora_tx_flag) |
| | | { |
| | | lora_tx_flag=0; |
| | | if(du_flag==1) |
| | | { |
| | | Lora47X_Init(); |
| | | du_flag=0; |
| | | Radio.Send( send_frame1, data_length1); |
| | | no_yingdaflag=1; |
| | | } |
| | | else |
| | | { |
| | | Lora47X_Init(); |
| | | if(bat_percent>20) |
| | | {LED_TR_ON;} |
| | | else |
| | | {LED_TB_ON;} |
| | | |
| | | lora_seq_nb2++; |
| | | Lora_tx_zubao(); |
| | | // Lora_tx_mode(); |
| | | // if(usart_send[2]==0x99) |
| | | // {LED_TB_ON;} |
| | | Radio.Send( usart_send, 15+Lora_tx_ancnum*4); |
| | | Lora_tx_ancnum=0; |
| | | memset(Lora_tx_ancdist,0,sizeof(Lora_tx_ancdist)); |
| | | memset(Lora_tx_ancid,0,sizeof(Lora_tx_ancid)); |
| | | anjian_flag=0; |
| | | } |
| | | } |
| | | |
| | | } |
| | | void MX_ADC1_Init() |
| | | { |
| | | MX_ADC_Init(); |
| | |
| | | HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); |
| | | __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, 60); |
| | | } |
| | | #define BleRxBufferSize 1 |
| | | uint8_t aRxBuffer[1],group_id,BleRxBuffer[BleRxBufferSize]; |
| | | uint8_t ble_rx_success_flag; |
| | | uint8_t fangzhijinrushuimian_flag; |
| | | uint8_t ble_rxzhongzhuandate[200]; |
| | | uint8_t ble_zhongzhuannum; |
| | | uint8_t ble_rxdate[200]={1,2,3,4,5,6,7,8,9}; |
| | | uint8_t changdu; |
| | | uint8_t Close_RMC[20]="$CCMSG,RMC,1,0,*04\r\n"; |
| | | uint8_t Close_GSA[20]="$CCMSG,GSA,1,0,*0D\r\n"; |
| | | uint8_t Close_GSV[20]="$CCMSG,GSV,1,0,*1A\r\n"; |
| | | uint8_t Close_GLL[20]="$CCMSG,GLL,1,0,*1F\r\n"; |
| | | uint8_t Close_TXT[20]="$CCMSG,TXT,1,0,*00\r\n"; |
| | | uint8_t gps_ceju=0; |
| | | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
| | | { |
| | | //HAL_UART_Transmit(&huart1,&ble_zhongzhuannum,1,1000); |
| | | { |
| | | ble_rxzhongzhuandate[ble_zhongzhuannum]=BleRxBuffer[0]; |
| | | ble_zhongzhuannum++; |
| | | if(ble_rxzhongzhuandate[ble_zhongzhuannum-1]==0x0A&&ble_rxzhongzhuandate[ble_zhongzhuannum-2]==0x0D) |
| | | GPS_zhongzhuandate[GPS_zhongzhuannum]=GPSRxBuffer[0]; |
| | | GPS_zhongzhuannum++; |
| | | if(GPS_zhongzhuandate[GPS_zhongzhuannum-1]==0x0A&&GPS_zhongzhuandate[GPS_zhongzhuannum-2]==0x0D) |
| | | { |
| | | // HAL_UART_Transmit(&hlpuart1,ble_rxdate,5,1000); |
| | | memcpy(ble_rxdate,ble_rxzhongzhuandate,ble_zhongzhuannum); |
| | | changdu= ble_zhongzhuannum; |
| | | memset(ble_rxzhongzhuandate,0,sizeof(ble_rxzhongzhuandate)); |
| | | ble_zhongzhuannum=0; |
| | | memcpy(GPS_GGAdate,GPS_zhongzhuandate,GPS_zhongzhuannum); |
| | | GPSchangdu= GPS_zhongzhuannum; |
| | | memset(GPS_zhongzhuandate,0,sizeof(GPS_zhongzhuandate)); |
| | | GPS_zhongzhuannum=0; |
| | | fangzhijinrushuimian_flag=0; |
| | | ble_rx_success_flag=1; |
| | | if(changdu>40) |
| | | if(GPSchangdu>40) |
| | | { |
| | | if(state5v==0) |
| | | LED2_TB_ON; |
| | |
| | | LED2_TR_ON; |
| | | } |
| | | } |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)BleRxBuffer, 1); |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)GPSRxBuffer, 1); |
| | | } |
| | | } |
| | | uint8_t ble_rxdate111[3]={0x11,0x22,0x33}; |
| | | void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) |
| | | { |
| | | SystemClock_Config(); |
| | | UsartInit(); |
| | | // HAL_UART_Transmit(&hlpuart1,(uint8_t*)ble_rxdate111,3,1000); |
| | | //// huart->RxState = HAL_UART_STATE_BUSY_RX; |
| | | // ble_rxzhongzhuandate[ble_zhongzhuannum]=BleRxBuffer[0]; |
| | |
| | | // } |
| | | // if(lp_time>3) |
| | | fangzhijinrushuimian_flag=1; |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)BleRxBuffer, 1); |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)GPSRxBuffer, 1); |
| | | // HAL_UART_Transmit(&hlpuart1,ble_rxdate,2,1000); |
| | | } |
| | | void Ble_rx_Poll() |
| | | void PowerDownDetect(void) |
| | | { |
| | | static uint32_t l_u32QXpowerTick = 0; |
| | | static uint32_t powercount=0; |
| | | if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)) |
| | | { |
| | | delay_ms(1000); |
| | | if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)) |
| | | { |
| | | while(1) |
| | | { |
| | | if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==0) |
| | | { |
| | | delay_ms(100); |
| | | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | uint8_t usart5_state,flag_onesecond; |
| | | void OneSecondTask(void) |
| | | { |
| | | if(ble_rx_success_flag) |
| | | { |
| | | ble_rx_success_flag=0; |
| | | // if(ble_rxdate[0]==0x55&&ble_rxdate[1]==0xAA&&ble_rxdate[5]==0x0D&&ble_rxdate[6]==0x0A) |
| | | // { |
| | | // if(ble_rxdate[2]==0x01) |
| | | // { |
| | | // memcpy(&g_com_map[NEARBASE_ID1],&ble_rxdate[3],2); |
| | | // save_com_map_to_flash(); |
| | | // delay_ms(100); |
| | | //// NVIC_SystemReset(); |
| | | // } |
| | | // if(ble_rxdate[3]==0x02) |
| | | // { |
| | | // memcpy(&g_com_map[NEARBASE_ID2],&ble_rxdate[4],2); |
| | | // save_com_map_to_flash(); |
| | | // delay_ms(100); |
| | | //// NVIC_SystemReset(); |
| | | // } |
| | | |
| | | // |
| | | // } |
| | | HAL_UART_Transmit(&huart1,ble_rxdate,changdu,1000); |
| | | memset(ble_rxdate,0,sizeof(ble_rxdate)); |
| | | changdu=0; |
| | | Delay_Ms(100); |
| | | // memset(ble_rxdate,0,sizeof(ble_rxdate)); |
| | | } |
| | | if(flag_onesecond==1) |
| | | { |
| | | flag_onesecond = 0; |
| | | } |
| | | } |
| | | /* USER CODE END 0 */ |
| | | |
| | |
| | | LIS3DH_Data_Init(); |
| | | // Uwb_Lora_Switch(1,0); |
| | | Dw1000_Init(); |
| | | Dw1000_App_Init(); |
| | | Delay_Ms(10); |
| | | // Uwb_Lora_Switch(0,0); |
| | | // HardWareTypeDiffConfig(); |
| | |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | Delay_Ms(2); |
| | | Delay_Ms(10); |
| | | // Uwb_Lora_Switch(0,0); |
| | | // printf("RXD :66666\r\n"); |
| | | usart_send[0]=0x55; |
| | | usart_send[1]=0xaa; |
| | | bat_percent=Get_VDDVlotage(); |
| | | GetLoratx_frequency(); |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)BleRxBuffer, 1); |
| | | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)GPSRxBuffer, 1); |
| | | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); //开GPS电源 |
| | | LoraInit(); |
| | | Delay_Ms(1000); |
| | | HAL_UART_Transmit(&hlpuart1,Close_RMC,22,1000); |
| | | HAL_UART_Transmit(&hlpuart1,Close_GSA,22,1000); |
| | | HAL_UART_Transmit(&hlpuart1,Close_GSV,22,1000); |
| | | HAL_UART_Transmit(&hlpuart1,Close_GLL,22,1000); |
| | | HAL_UART_Transmit(&hlpuart1,Close_TXT,22,1000); |
| | | // Lora47X_Init(); |
| | | // mcu_sleep(); |
| | | //LED0_BLINK; |
| | | // SystemPower_Config(); |
| | | //HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); |
| | | // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); |
| | | // Lora47X_Init(); |
| | | // Radio.Sleep(); |
| | | // lora_tx_flag=1; |
| | | // Lora_Tx_Poll(); |
| | | if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 0) != HAL_OK) //system time is 1010ms 1 puls=30.518us |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | /* USER CODE END 2 */ |
| | | |
| | | /* Infinite loop */ |
| | |
| | | /* USER CODE END WHILE */ |
| | | |
| | | /* USER CODE BEGIN 3 */ |
| | | //Lora_rx_open(); |
| | | // Lora_Tx_Poll(); |
| | | Lora_Tx_Poll(); |
| | | Ble_rx_Poll(); |
| | | // HAL_IWDG_Refresh(&hiwdg); |
| | | IdleTask(); |
| | | // Lora_rece_error=0; |
| | | // Radio.IrqProcess( ); // Process Radio IRQ |
| | | // Anchor_App(); |
| | | OneSecondTask(); |
| | | IdleTask(); |
| | | |
| | | #ifndef DEBUG_MODE |
| | | if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin)) |
| | |
| | | |
| | | if( no_data_chongqi_num>600) |
| | | {NVIC_SystemReset();} |
| | | SetNextPollTime(0); |
| | | // if(lora_yingda_flag) |
| | | // { |
| | | // lora_yingdatime++; |
| | | // if(lora_yingdatime>=2) |
| | | // {lora_yingdatime=0; |
| | | // lora_yingda_flag=0; |
| | | // Lora_tx_mode(); |
| | | // Radio.Send( usart_send, 2); |
| | | // } |
| | | // } |
| | | // else |
| | | // { |
| | | // pinlv=g_com_map[COM_MAP_SIZE]/1000; |
| | | |
| | | if(lp_time%(g_com_map[COM_INTERVAL]/1000)==0) |
| | | { |
| | | lora_tx_flag=1; |
| | | lora_txanchor_power_flag=0; |
| | | |
| | | LoraReportPoll(); |
| | | flag_onesecond = 1; |
| | | |
| | | // lora_tx_flag=1; |
| | | // lora_txanchor_power_flag=0; |
| | | // } |
| | | if(anchor_send_num>=3600) |
| | | {anchor_send_num=0; |
| | | lora_txanchor_power_flag=1; |
| | | lora_tx_flag=0; |
| | | bat_percent=Get_VDDVlotage(); |
| | | } |
| | | // if(anchor_send_num>=3600) |
| | | // {anchor_send_num=0; |
| | | // lora_txanchor_power_flag=1; |
| | | // lora_tx_flag=0; |
| | | // bat_percent=Get_VDDVlotage(); |
| | | // } |
| | | } |
| | | |
| | | if(stationary_num>=10) |