WXK
2025-03-14 5631f61d1e227cf40802bcef95b1bf9c053cb04a
keil/include/main/main.c
@@ -61,8 +61,12 @@
#define TEST_UART_MODE TEST_UART_DMA_MODE
#define NUM_SAMPLES 1
#define SLEEP_START_TIME 120
#define SLEEP_START_TIME 60
#define FREQ_LOST_TIME 5
#define NOTAG_FREQ  1
#define BATTERY_GET_TIME 3600
uint8_t enable_sleep_count,sleep_flag;
uint32_t battery_get_count;
//#define DEBUG_MODE
struct UART_CFG_T test_uart_cfg =
@@ -88,7 +92,11 @@
        .int_tx = false,
#endif
      };
static void app_wdt_callback(void *dev, uint32_t status)
{
    ASSERT(status, "WDT TIMEOUT,程序复位");
      //LOG_INFO(TRACE_MODULE_APP, "程序卡死,看门狗复位");
}
static uint32_t sample[NUM_SAMPLES] = {0};
static struct ADC_CFG_T usr_adc_cfg = {
    .mode = ADC_MODE_CONTINUE,    /* Selected single conversion mode  */
@@ -103,6 +111,12 @@
    .high_pulse_time = 4,
    .settle_time = 1,
};
 struct WDT_CFG_T app_wdt_cfg = {
        .timeout = 32768 * 30,
        .rst_en = true,
        .int_en = true,
        .callback = app_wdt_callback,
    };
uint8_t state5v = 1;
uint8_t bat_percent=0,g_start_send_flag=1;
@@ -110,12 +124,15 @@
uint8_t bat_percent;
extern uint32_t dev_id;
extern uint8_t group_id;
uint8_t tag_frequence;
extern float freqlost_count;
uint8_t tag_frequency;
void UartDeinit(void);
void UartInit(void);
void Program_Init(void);
void IdleTask(void);
void boot_deinit(void);
void Get_batterty_Voltage(void);
void Calculate_battery_percent(void);
static void uart_receive_callback(void *dev, uint32_t err_code)
{      
uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
@@ -132,17 +149,56 @@
uart_close(UART_ID0);
uart_close(UART_ID1);
}
void Get_batterty_Voltage(void)
{
    battery_monitor_open();
    fVoltage_mv=battery_monitor_get();
   battery_monitor_close();
   Calculate_battery_percent();
}
void Calculate_battery_percent(void)
{
            if(fVoltage_mv < 3000)
        {
            bat_percent = 0;
        }
        else if(fVoltage_mv > 3500)
        {
            bat_percent = 100;
        }
        else
        {
            bat_percent = ((fVoltage_mv - 3000) /8);
        }
            //LOG_INFO(TRACE_MODULE_APP, "The voltage is %d ,percent is %%%d \r\n",fVoltage_mv,bat_percent);
}
static void sleep_timer_callback(void *dev, uint32_t time)
{
    //sleep_timer_start(__MS_TO_32K_CNT(1000));
   static uint8_t lost_jumpcount=0;
      enable_sleep_count++;
   if(enable_sleep_count==SLEEP_START_TIME){
      enable_sleep_count=0;
      sleep_flag=1;
   }
      g_start_send_flag=1;
    //LOG_INFO(TRACE_MODULE_APP, "Sleep timer interrupt callback\r\n");
   if(battery_get_count++>=BATTERY_GET_TIME)
   {
   Get_batterty_Voltage();
   battery_get_count=0;
   }
//   if(freqlost_count++>FREQ_LOST_TIME)
//   {
//            tag_frequency = NOTAG_FREQ;
//        if(lost_jumpcount++>=4) //无测距情况下,每5秒发一次;
//        {
//            lost_jumpcount = 0;
//            g_start_send_flag=1;
//        }
//   }else{
//      g_start_send_flag=1;
//   }
   g_start_send_flag=1;
}
static void adc_callback(void *data, uint32_t number)
{
@@ -166,8 +222,9 @@
            bat_percent = ((fVoltage_mv - 3300) /8);
        }
    }
       LOG_INFO(TRACE_MODULE_APP, "The voltage is %%%d \r\n",bat_percent);
       //LOG_INFO(TRACE_MODULE_APP, "The voltage is %%%d \r\n",bat_percent);
}
static void voltage_input_handler(enum IO_PIN_T pin)
{
//LOG_INFO(TRACE_MODULE_APP, "中断唤醒\r\n");
@@ -178,8 +235,8 @@
parameter_init();//g_com_map表初始化
group_id=g_com_map[GROUP_ID];
memcpy(&dev_id ,&g_com_map[DEV_ID],2);
tag_frequence=1000/g_com_map[COM_INTERVAL];
g_com_map[VERSION] = (1<<8)|5;
tag_frequency=1000/g_com_map[COM_INTERVAL];
g_com_map[VERSION] = (1<<8)|10;
LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n",dev_id);
LOG_INFO(TRACE_MODULE_APP,"固件版本:UWB-标签 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
}
@@ -245,6 +302,7 @@
    // Disable watchdog timer
    wdt_close(WDT_ID0);
      wdt_open(WDT_ID0,&app_wdt_cfg);//30s最大上限检测喂狗
    //LOG_INFO(TRACE_MODULE_APP, "UWB qiang  test example\r\n");
      
    // open system timer
@@ -260,12 +318,15 @@
       uart_receive(UART_ID0,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
      // Initialize low power mode
    power_init();
      Get_batterty_Voltage();//获取当前内部电压
      //Calculate_battery_percent();
      //adc_open(&usr_adc_cfg);
      //adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
    // Enable sleep timer
      Tag_uwb_init();
    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_time_count));//测试
      sleep_timer_start(__MS_TO_32K_CNT(SLEEP_COUNT));//测试
      //board_5V_input_init(voltage_input_handler);//有修改3.3V会一直高电平导致无法进入休眠
      #ifdef BOXING 
            io_pin_mux_set(IO_PIN_5, IO_FUNC0);//波形测试
@@ -274,7 +335,7 @@
   #endif
      //Serial0_PutString("进入app测试\r\n");
    while (1)
    {
    {  wdt_ping(WDT_ID0);//喂狗
         if(g_start_send_flag)
         {
         //LOG_INFO(TRACE_MODULE_APP, "测距ing");
@@ -287,6 +348,7 @@
         }else{
         IdleTask();   
         }
         LoraUp_Poll();
#ifndef DEBUG_MODE
         if(sleep_flag){//开始一段时间无休眠
                  trace_flush();