keil/include/main/main.c
@@ -76,7 +76,8 @@
#define WARING_LIMIT_TIME 10
#define UPDATE_TIME 10
#define VIBRATION_TIME 1  //单位500ms
#define NO_VIBRATION_TIME 20 //15s
extern uint8_t mUsartReceivePack[100];
extern uint8_t mUsart2ReceivePack[150];
extern uint8_t state5V_prase_flag,gps_prase_flag;
@@ -91,11 +92,12 @@
uint8_t bat_percent,g_start_send_flag=1;
uint8_t link_success_flag,motor_count;
uint16_t gps_wait_count;
extern uint8_t gps_timeout_flag;
uint8_t state5v = 1;
uint8_t motor_power_state=1,uwb_state,air780_power_state,gps_success_state,red_charge_state,green_charge_state,air780_success_state,first_motor_in_flag;
float nomove_count=0;
static uint32_t sample[NUM_SAMPLES] = {0};
uint8_t recev_error_num,send_messgae_count,send_flag,control_state;
uint8_t recev_error_num,send_messgae_count,send_flag,control_state,power_low_flag;
uint8_t flag_4g_usart_rx_state = 1;  //4G 串口RX当前是 RX状态,不是GPIO状态
int input_5v_flag,air780e_flag1,air780e_flag2,air780e_flag3,air780e_flag4;
typedef enum
@@ -148,6 +150,20 @@
{
    uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
}
void mcu_deep_sleep(void)
{
            uint32_t lock;
            trace_flush();
            lock = int_lock();
                  LOG_INFO(TRACE_MODULE_APP, "进入深度休眠\r\n");
                  gps_air780_power_change(0,0);//关闭gps,4G
                     sleep_timer_stop();
                  //adc_close();
            power_enter_power_down_mode(1);
                  //LOG_INFO(TRACE_MODULE_APP, "进入深度休眠\r\n");
                  sys_reset(0);
            int_unlock(lock);
}
static void adc_callback(void *data, uint32_t number)
{
    uint32_t *result = (uint32_t *)data;
@@ -171,7 +187,17 @@
        }
    }
      gpio_pin_set(ADC_GND_ENABLE);//在拉高
      tt2=gpio_pin_get_val(ADC_GND_ENABLE);
      if(fVoltage_mv<3300)
            {
               power_low_flag=1;
               gps_air780_power_change(gps_power_state,0);//gps原样,关闭4G
               LOG_INFO(TRACE_MODULE_APP, "电池电压低于3.3V,4G,GPS,马达停止工作\r\n");
            }else{
               power_low_flag=0;
               gps_air780_power_change(gps_power_state,1);//gps原样,开启4G
               LOG_INFO(TRACE_MODULE_APP, "电池电压正常,4G,GPS,马达正常工作\r\n");
            }
      //tt2=gpio_pin_get_val(ADC_GND_ENABLE);
  //  LOG_INFO(TRACE_MODULE_APP, "The voltage is %%%d \r\n",bat_percent);
}
//void UartInit(void)
@@ -181,11 +207,112 @@
//board_debug_console_open(TRACE_PORT_UART0);
//uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
//}
extern uint8_t gps_uwb_flag;
uint8_t TCP_reconnect_timer,flag_TCP_reconnectting = 1,flag_alam_state = 0;
extern uint8_t gps_uwb_flag,gps_need_data_flag;
static HIDO_UINT32 l_u32MotorPollTick = 0;
uint8_t TCP_reconnect_timer,flag_TCP_reconnectting = 1,flag_alam_state = 0,userkey_state;
uint32_t keystarttime,keystarttime2;
uint8_t motor_open_flag=1;
uint8_t gps_enable_flag,motor_open_air_flag,moter_open_uwb_flag,flag_fenli_alarm = 0,fenli_alarm_count,delaysleep_count,enbale_blink_flag;
int need_open_gps_count;
uint8_t change_vibration=2;
HIDO_UINT32 u32CurTick_Motor;
void MotorPoll(void)
{
   if(!power_low_flag)//确认是否休眠下才开启功能
   { //马达震动逻辑
    if(g_com_map[MOTOR_ENABLE])
    {
        if(motor_open_air_flag||moter_open_uwb_flag)
        {motor_power_state=0;
               if(motor_open_flag)
               {
                  if (motor_count>=change_vibration)
                   {
                     change_vibration=VIBRATION_TIME;
                     motor_power_state = 1;
                     motor_count=0;
                     motor_open_flag=0;
                     update_led_power_state();
                   }
                   motor_count++;
               }else {//修改
                        motor_power_state = 1;
                      if(motor_count>=NO_VIBRATION_TIME)
                      {
                        motor_power_state = 0;
                        motor_count=0;
                        motor_open_flag=1;
                     update_led_power_state();
                   }
                      motor_count++;
                }
        } else {
                  motor_open_flag=1;
                  motor_count=0;
            motor_power_state=1;
        }
    }else{
            motor_open_flag=1;
            motor_count=0;
        motor_power_state=1;
    }
   }else{
   motor_open_flag=1;
   motor_count=0;
  motor_power_state=1;
   }
}
void MotorPollCheck(void)
{
   if(!power_low_flag)//确认是否休眠下才开启功能
   { //马达震动逻辑
    if(g_com_map[MOTOR_ENABLE])
    {
        if(motor_open_air_flag||moter_open_uwb_flag)
        {motor_power_state=0;
               if(motor_open_flag)
               {
                  if (motor_count>=VIBRATION_TIME)
                   {
                     motor_power_state = 1;
                     motor_count=0;
                     motor_open_flag=0;
                     update_led_power_state();
                   }
               }else {//修改
                        motor_power_state = 1;
                      if(motor_count>=NO_VIBRATION_TIME)
                      {
                        motor_power_state = 0;
                        motor_count=0;
                        motor_open_flag=1;
                     update_led_power_state();
                   }
                }
        } else {
                  motor_open_flag=1;
                  motor_count=0;
            motor_power_state=1;
        }
    }else{
               motor_open_flag=1;
                  motor_count=0;
        motor_power_state=1;
    }
   }else{
   motor_open_flag=1;
   motor_count=0;
  motor_power_state=1;
   }
   update_led_power_state();
}
void StatusChange(void)
{
    if(UWB_work_state==UN_BIND) {
        gps_uwb_flag=0;//关闭GPS
@@ -260,7 +387,33 @@
        }
    }
   }
void IMUTask(void)
{
   if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&g_com_map[IMU_ENABLE]==1)
   {    power_wakeup_enable((enum POWER_WAKEUP_SOURCE_T)ACCLERATE_DETECT_Pin, POWER_WAKEUP_LEVEL_HIGH);//配置唤醒等待进入休眠
      mcu_deep_sleep();
   }
}
void UserKeyTask(void)
 {
     if(!GET_USERKEY)
     {
        if(HIDO_TimerGetTick() - keystarttime>3)
        {
            userkey_state = 1;
            keystarttime =  HIDO_TimerGetTick();
                  TCPHeartBeatUpload();//立即上传该包
        }
        if(HIDO_TimerGetTick() - keystarttime2>10)
        {
            g_com_map[CNT_RESTART] = 1;
        }
     }else
     {
       keystarttime =  HIDO_TimerGetTick();
       keystarttime2 =  HIDO_TimerGetTick();
     }
 }
void MinuteTask(void)
{      gpio_pin_clr(ADC_GND_ENABLE);//先拉低
    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
@@ -274,10 +427,22 @@
    }
    //UWB更新列表
    TagListUpdate();
      if(!power_low_flag)
      {
    GPS_Poll();
         //gps_power_state=1;//常开GPS
      }else{
      gps_power_state=0;//关闭GPS
      update_led_power_state();
      //初始化GPS计数数据
      gps_need_data_flag=1;
      gps_wait_count=0;
      gps_timeout_flag=0;
      }
      //gps_power_state=1;//测试gps长开
    //UWB状态检测
if(!power_low_flag)//低供电下不需要检测重连
   {
    if(IfTCPConnected())
    {
        TCP_reconnect_timer =0;
@@ -295,28 +460,38 @@
        }
    }
   }
    HIDO_TimerTick();
      if(nomove_count<=g_com_map[NOMOVESLEEP_TIME])//防止溢出
    nomove_count++;
      else{
      nomove_count=g_com_map[NOMOVESLEEP_TIME]+1;
      }
}
uint8_t tt=1;
uint8_t flag_sleeptimer,flag_secondtask,secondtask_count,log_4g_enable_flag;
uint8_t uwb_enable_flag=0;
uint8_t enable_system_function_flag;
static void sleep_timer_callback(void *dev, uint32_t time)
{
   // IO_control_init();
   if(enbale_blink_flag)
   {
   green_charge_state=0;
   charge_red_on();
   }
   if(tt)
    UWBPoll();
   //车载poll
      UWB_Tag_Poll();
    
    MotorPoll();
    StatusChange();
      MotorPoll();
      update_led_power_state();
    if(secondtask_count++%2==0)
    {
        flag_secondtask = 1;
@@ -327,25 +502,7 @@
    flag_sleeptimer = 1;
      charge_state_change();//充电状态判断
    //马达震动逻辑
    if(g_com_map[MOTOR_ENABLE])
    {
        if(motor_open_air_flag||moter_open_uwb_flag)
        {
           if (motor_count++%3!=0)
           {
            motor_power_state = 1;
           }else {//修改
            motor_power_state = 0;
           }
        } else {
            motor_power_state=1;
        }
    }else{
        motor_power_state=1;
    }
      if(enbale_blink_flag)
   {
   green_charge_state=0;
@@ -371,7 +528,36 @@
        flag_4G_recdata = 1;
        delaysleep_count = 3;
}
extern uint16_t ip0,ip1,ip2,ip3,port;
void Air780E_4G_POLL(void)
{
if(!power_low_flag)//确认是否休眠下才开启功能
         {
        if(flag_TCP_reconnectting||IfTCPConnected())
        {
        //    LOG_INFO(TRACE_MODULE_APP, "处理4G任务\r\n");
//            if(flag_4guart_needinit)
//            {
//                flag_4guart_needinit = 0;
//                AIR780EUartInit();
//            }//yuan
                 if(flag_4G_recdata==1)
            {
                flag_4G_recdata = 2;
                //LOG_INFO(TRACE_MODULE_APP, "运行Socket_RecvAll tt2 %d\r\n",tt2);
                Socket_RecvAll();
            }
                  air780_led_on();
            Internet_Poll();
            HIDO_ATLitePoll();
            HIDO_TimerPoll();
            TCPClient_Poll();
            air780_led_off();
        }
         }
}
extern uint16_t ip0,ip1,ip2,ip3,port,air780e_send_second;
extern uint8_t gps_4g_flag;
extern uint8_t gps_need_data_flag;
void Program_Init(void)
@@ -397,16 +583,21 @@
    }else{
        UWB_work_state = SEARCH_DEV;
    }
    if(g_com_map[SEND_4G_SECOND]<60)
        g_com_map[SEND_4G_SECOND]=60;
    g_com_map[MODBUS_MODE] = 0;
//    if(g_com_map[SEND_4G_SECOND]<60)
//        g_com_map[SEND_4G_SECOND]=60;
      if(g_com_map[SEND_4G_MINUTE]<1)
         g_com_map[SEND_4G_MINUTE]=1;
      //air780e_send_second=g_com_map[SEND_4G_MINUTE]*60;//yuan
    air780e_send_second=g_com_map[SEND_4G_MINUTE];
      g_com_map[MODBUS_MODE] = 0;
      log_4g_enable_flag=g_com_map[LOG_4G_ENABLE];
    ip0 = (g_com_map[TCP_IP_0]>>12&0xf)*1000+(g_com_map[TCP_IP_0]>>8&0xf)*100+(g_com_map[TCP_IP_0]>>4&0xf)*10+(g_com_map[TCP_IP_0]&0xf);
    ip1 = (g_com_map[TCP_IP_1]>>12&0xf)*1000+(g_com_map[TCP_IP_1]>>8&0xf)*100+(g_com_map[TCP_IP_1]>>4&0xf)*10+(g_com_map[TCP_IP_1]&0xf);
    ip2 = (g_com_map[TCP_IP_2]>>12&0xf)*1000+(g_com_map[TCP_IP_2]>>8&0xf)*100+(g_com_map[TCP_IP_2]>>4&0xf)*10+(g_com_map[TCP_IP_2]&0xf);
    ip3 = (g_com_map[TCP_IP_3]>>12&0xf)*1000+(g_com_map[TCP_IP_3]>>8&0xf)*100+(g_com_map[TCP_IP_3]>>4&0xf)*10+(g_com_map[TCP_IP_3]&0xf);
    port = g_com_map[TCP_PORT];
    g_com_map[VERSION] = (1<<8)|11;
    g_com_map[VERSION] = (1<<8)|14;
    LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n",dev_id);
    LOG_INFO(TRACE_MODULE_APP,"固件版本:UWB-4G车载手环 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
    LOG_INFO(TRACE_MODULE_APP,"服务器地址: %d.%d.%d.%d:%d.\r\n",ip0,ip1,ip2,ip3,port);
@@ -524,31 +715,15 @@
    gpio_open();
      board_output_init();
    //board_led_init();
      //防止充电电流过低导致设备无法启动
//      battery_monitor_open();
//      Voltage_input=battery_monitor_get();
//      battery_monitor_close();
//      if(Voltage_input<=3270)
//      {
//      enable_system_function_flag=0;//供电不足不能启动4G和GPS
//      }else{
//      enable_system_function_flag=1;
//      }
      //检测通过后改为正常adc采集模式
    adc_open(&usr_adc_cfg);
      
    IIC2_Init();
    Accelerometer_Init();
    IO_control_init();
      #ifdef BO_XING
    io_pin_mux_set(SCL_PIN, IO_FUNC0);//测试测距波形
    gpio_pin_set_dir(SCL_PIN , GPIO_DIR_OUT, 0);
    io_pull_set(SCL_PIN , IO_PULL_DOWN, IO_PULL_UP_LEVEL4);
      #endif
    gps_air780_power_change(0,1);//开启gps,4G 
      //加速度计初始化必须在IO_control_init之前因为复用SDA引脚
      gpio_pin_clr(ADC_GND_ENABLE);//Ïȵ
    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
         Program_Init();
    //uart_open(UART_ID1, &test_uart_cfg);
 
@@ -558,17 +733,21 @@
   
// Initialize low power mode
    power_init();
      AIR780E_Reset(); //4G模块重启,耗时1.5秒,这个是无法同步的关键
      //AIR780E_Reset(); //4G模块重启,耗时1.5秒,这个是无法同步的关键
    sleep_timer_open(true, SLEEP_TIMER_MODE_RELOAD, sleep_timer_callback);
    //sleep_timer_start(__MS_TO_32K_CNT(g_com_map[COM_INTERVAL]));//测试
    sleep_timer_start(__MS_TO_32K_CNT(SLEEP_TIMER_NUM));//测试
 //   board_5V_input_init(voltage_input_handler);
    board_acceleration_detection_init(move_handler);
      gpio_pin_clr(ADC_GND_ENABLE);//先拉低
      adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
      board_acceleration_detection_init(move_handler);
//       LOG_INFO(TRACE_MODULE_APP, "测试进入app");
      board_4GUsart_detection_init(_4gUsart_handler);
    while (1)
    { tt1=gpio_pin_get_val(ADC_GND_ENABLE);
         //tt2 = gpio_pin_get_val(_4G_USART_RX_Pin);
         if(!power_low_flag)//确认是否休眠下才开启功能
         {
        if(flag_TCP_reconnectting||IfTCPConnected())
        {
        //    LOG_INFO(TRACE_MODULE_APP, "处理4G任务\r\n");
@@ -592,6 +771,7 @@
            TCPClient_Poll();
            air780_led_off();
        }
         }
        if(flag_secondtask)
        {
            flag_secondtask = 0;
@@ -599,6 +779,9 @@
               
        }
        IdleTask();
            UserKeyTask();
            IMUTask();
#ifndef DEBUG_MODE
//if(flag_sleeptimer)
//        if(!gps_need_data_flag&&!gpio_pin_get_val(INPUT_5V_Pin)&&((IfTCPConnected()&&Socket_IsSendQueueEmpty(0))||!flag_TCP_reconnectting&&!IfTCPConnected())) //进入休眠条件(与关系,全满足才进):1.没有5V输入 2 TCP已连接时发送队列为空||TCP无连接时不在重连状态。
@@ -625,15 +808,15 @@
index2=AIR780E_IsIPIdle();
index3=Socket_IsSendQueueEmpty(0);
            if(delaysleep_count==0)
        if(!gps_power_state&&!gpio_pin_get_val(INPUT_5V_Pin)&&((IfTCPConnected()&&AIR780E_IsIPIdle()&&Socket_IsSendQueueEmpty(0))||((!flag_TCP_reconnectting)&&!IfTCPConnected())))
        if(!gps_power_state&&!gpio_pin_get_val(INPUT_5V_Pin)&&((IfTCPConnected()&&AIR780E_IsIPIdle()&&Socket_IsSendQueueEmpty(0))||((!flag_TCP_reconnectting)&&!IfTCPConnected())||power_low_flag))
        {
//               test1=gpio_pin_get_val(_4G_USART_RX_Pin);
            uint32_t lock;
            //flag_4guart_needinit = 1;
            Internet_Poll();
            HIDO_ATLitePoll();
            HIDO_TimerPoll();
            TCPClient_Poll();
//            Internet_Poll();
//            HIDO_ATLitePoll();
//            HIDO_TimerPoll();
//            TCPClient_Poll();
            
           // delay_us(300000);
            trace_flush();