WXK
3 天以前 7d13fbde75b58ff4c9deaef95ca0c03a1083f8e8
keil/include/main/main.c
@@ -183,31 +183,32 @@
//        LOG_INFO(TRACE_MODULE_APP, "The voltage measured %d mv\r\n",
//        ADC_INTERNAL_VREF_MV + adc_code_to_mv((int16_t)*result, ADC_INTERNAL_VREF_MV));
        fVoltage_mv=ADC_INTERNAL_VREF_MV + adc_code_to_mv((int16_t)*result, ADC_INTERNAL_VREF_MV);
        fVoltage_mv=fVoltage_mv*2;
        if(fVoltage_mv < 3300)
        fVoltage_mv=fVoltage_mv*2+1100;
        if(fVoltage_mv < 2000)
        {
            bat_percent = 0;
        }
        else if(fVoltage_mv > 4100)
        else if(fVoltage_mv > 3100)
        {
            bat_percent = 100;
        }
        else
        {
            bat_percent = ((fVoltage_mv - 3300) /8);
            bat_percent = ((fVoltage_mv - 2000) /11);
        }
    }
            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");
            }
      PCA9555_Set_One_Value_Output(ADC_MINIUS,1);//拉高
//    LOG_INFO(TRACE_MODULE_APP, "当前静止:%d 电量:%d\r\n",nomove_count,fVoltage_mv);
//            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");
//            }
//      PCA9555_Set_One_Value_Output(ADC_MINIUS,1);//拉高
  //  LOG_INFO(TRACE_MODULE_APP, "The voltage is %%%d \r\n",bat_percent);
}
extern uint8_t gps_uwb_flag,gps_need_data_flag;
@@ -219,7 +220,9 @@
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);
   {
        LOG_INFO(TRACE_MODULE_APP, "进入休眠\r\n");
        power_wakeup_enable((enum POWER_WAKEUP_SOURCE_T)ACCLERATE_DETECT_Pin, POWER_WAKEUP_LEVEL_HIGH);
      mcu_deep_sleep();
   }
}
@@ -266,16 +269,18 @@
}
void MinuteTask(void)
{      
      PCA9555_Set_One_Value_Output(ADC_MINIUS,0);//拉低
//      PCA9555_Set_One_Value_Output(ADC_MINIUS,0);//拉低
    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
}
//uint16_t adc_count;
//uint8_t adc_start_flag;
void SecondTask(void)
{static uint8_t second_count;
    if(second_count++>60)
    {
        second_count = 0;
        MinuteTask();
    }
//    if(adc_count++>3600&&adc_start_flag)
//    {
//        adc_count = 0;
//        MinuteTask();
//    }
      
//      if(!power_low_flag)
//      Gps_change();
@@ -342,8 +347,9 @@
uint8_t Get_anchor_appdata_flag;
uint8_t Get_anchor_appdata_num;
extern uint8_t update_tag_time;
uint8_t sleep_limit_time=1;
uint8_t sleep_limit_time=10;
uint8_t enable_sleep_count,sleep_flag;
extern  float motor_keeptime;
static void sleep_timer_callback(void *dev, uint32_t time)
{
 
@@ -354,7 +360,10 @@
    enable_sleep_count=0;
    sleep_flag=1;
    }
    if(motor_keeptime>0)
    {
    motor_keeptime--;
    }
    
    g_start_send_flag=1;
//    if(delaysleep_count>0)
@@ -365,7 +374,9 @@
static void move_handler(enum IO_PIN_T pin)
{
    nomove_count=0;
    Motion_Detection_callback();
//    LOG_INFO(TRACE_MODULE_APP, "加速度唤醒了\r\n");
//    LOG_INFO(TRACE_MODULE_APP, "当前静止:%d 电量:%d\r\n",nomove_count,fVoltage_mv);
//    Motion_Detection_callback();
}
static void pca_handler(enum IO_PIN_T pin)
{
@@ -420,11 +431,11 @@
    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)|1;
    g_com_map[VERSION] = (1<<8)|9;
    LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n",dev_id);
    LOG_INFO(TRACE_MODULE_APP,"固件版本:4G-GPS定位手环 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);
}
}
extern uint8_t motor_state;
extern  float motor_keeptime;
uint8_t test1;
@@ -504,7 +515,7 @@
        if(state5v==1)
        {
        state5v=0;
        sleep_limit_time=1;//新增插入串口后30s不休眠
        sleep_limit_time=10;//新增插入串口后30s不休眠
        enable_sleep_count=0;//重新插拔可以重置时间
        }
@@ -605,43 +616,6 @@
    uint32_t tx_en_start_u32;
    uint32_t tx_en_start_u321;
uint8_t baifenbi=5;
void Lora_upanc_ing_Poll( void )
{   uint16_t crc16;
//    if(Lora_upanc_flag==1)
//    {
//        Lora_deinit_Poll();
        if(Lora_qingqiu_flag==1)
        {
            CloseUWB();
//            mubiao_anchor_ID=0x1872;
            final_bag_num=(g_com_map[YAOGEIANC_UPWENJIAN_DAXIAO]/ONE_BAG_DAXIAO)+1;//计算一共要发都是包
//            final_bag_num=651;
            Lora_qingqiu_flag=0;
            send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_RESP;//LORA MSG TYPE 定义
            memcpy(&send_lora_data[SOURCE_ID_IDX],&dev_id,2);//自己ID 占用2个字节
            memcpy(&send_lora_data[DEST_ID_IDX],&mubiao_anchor_ID,2);//标签或者基站的设备ID 2个字节
            memcpy(&send_lora_data[MUQIAN_BAG],&Lora_qingqiu_bao,2);
            memcpy(&send_lora_data[DATA_IDX], (HIDO_VOID *)(APP_CONFIG_APPLICATION_ADDRESS+Lora_qingqiu_bao*ONE_BAG_DAXIAO), ONE_BAG_DAXIAO);
            crc16=Cal_CRC16(send_lora_data,DATA_IDX+ONE_BAG_DAXIAO);
            memcpy(&send_lora_data[DATA_IDX+ONE_BAG_DAXIAO],&crc16,2);
//            Lora_status=1;
            jindu=(Lora_qingqiu_bao*100)/final_bag_num;
//            LOG_INFO(TRACE_MODULE_APP, "升级进度%d\r\n",jindu);
            Dtu_shengji_jindu[4]=jindu;
            memcpy(&Dtu_shengji_jindu[5],&mubiao_anchor_ID,2);
            if(jindu>baifenbi)
            {
            uart_send(UART_ID0, Dtu_shengji_jindu,9, NULL);
                baifenbi=baifenbi+10;
            }
            tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(100U);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间
            tempflag=uwb_tx(send_lora_data,DATA_IDX+ONE_BAG_DAXIAO+2+2,1,tx_en_start_u32);//立即发送
            tx_en_start_u321= phy_timer_count_get();
//            while(mac_is_busy());//等待发送完成
        }
//    }
}
static void pwm_callback(void *dev, uint32_t status)
{
//    uint8_t id = *(uint8_t *)dev;
@@ -654,6 +628,9 @@
}
extern uint8_t shengji_flag;
extern uint16_t adc_caiji;
uint8_t send_frame1[10];
extern uint8_t adc_one;
int main(void)
{
    board_clock_run();
@@ -683,7 +660,7 @@
    LOG_INFO(TRACE_MODULE_APP, "UWB simple example\r\n");
    
    struct PWM_CFG_T usr_pwm_cfg = {
        .general_prescale = 15,
        .general_prescale = 7,
        .int_en = 1,
        .callback = pwm_callback,
    };
@@ -695,7 +672,7 @@
      
    IIC2_Init();
    SC7A22H_Motion_Detection_Init();
    gpio_pin_clr(ADC_GND_ENABLE);
//    gpio_pin_clr(ADC_GND_ENABLE);
    Program_Init();
    uart_receive(UART_ID0,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
    power_init();
@@ -707,10 +684,11 @@
    sleep_timer_start(__MS_TO_32K_CNT(5000));//测试
    #endif
    board_5V_input_init(voltage_input_handler);
    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
//    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
    board_acceleration_detection_init(move_handler);
    Uwb_init();
    Dw1000_App_Init();
//    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
    while (1)
    { 
        if(flag_secondtask)
@@ -720,8 +698,33 @@
        }
        if(g_start_send_flag)
        {
//                adc_caiji++;
//    if(adc_caiji>5&&adc_one==0)
//    {
//    adc_one=1;
//    adc_caiji=0;
//    MinuteTask();
//    }
//    else if(adc_one==1&&adc_caiji>3600)
//    {
//    adc_caiji=0;
//    MinuteTask();
//    }
//    send_frame1[0] = 0x55;
//    send_frame1[1] = 0xAA;
//    send_frame1[2] = nomove_count;
//    memcpy(&send_frame1[3],&fVoltage_mv,2);
////    send_frame1[3] = data_length+5;
////    send_frame1[4] = CMD_REPLY;
////    send_frame1[5] = index;
////    send_frame1[6] = data_length;
//    uart_send(UART_ID0, send_frame1,5, NULL);
            g_start_send_flag = 0;
            gpio_pin_set(IO_PIN_7);//亮
//            LOG_INFO(TRACE_MODULE_APP, "当前静止:%d 电量:%d\r\n",nomove_count,fVoltage_mv);
          SwitchTagState();   
            gpio_pin_clr(IO_PIN_7);//灭
        }
@@ -729,9 +732,10 @@
            IMUTask();
//            UserKeyTask();
        IdleTask();
        LoraUp_Poll();
#ifndef DEBUG_MODE
        
         if(sleep_flag&&state5v==0){//开始一段时间无休眠
         if(sleep_flag&&state5v==0&&motor_keeptime==0){//开始一段时间无休眠
                  trace_flush();
            uint32_t lock = int_lock();
                  //LOG_INFO(TRACE_MODULE_APP, "进入低功耗");