chen
6 天以前 33a728fdc172f59a313cee92da153b37c14c450e
keil/include/main/main.c
@@ -113,11 +113,13 @@
//*****************************************************************************
#define WSF_BUF_POOLS 5
void Fira_Change_Task(void);
extern uint8_t normal_flag,log_4g_enable_flag;
extern uint8_t normal_flag,log_4g_enable_flag,lora_tx_flag;
extern uint16_t ip0,ip1,ip2,ip3,port;
uint8_t group_id,enable_sleep_count;
uint32_t dev_id;
uint16_t disoffset;
uint8_t flag_sleeptimer,flag_secondtask,secondtask_count;
float nomove_count;
// Default pool descriptor.
static wsfBufPoolDesc_t poolDescriptors[WSF_BUF_POOLS] = {
    {32, 26}, {64, 24}, {128, 4}, {256 + 32, 4}, {1024 + 32, 2},
@@ -127,9 +129,22 @@
{
    // LOG_INFO(TRACE_MODULE_APP, "Wake up by sleep timer %d\r\n", time);
}
static void sleep_timer_callback_normal(void *dev, uint32_t time)
{
   if(secondtask_count++%2==0)
    {
        flag_secondtask = 1;
    }else{
        flag_secondtask = 0;
    }
// if(delaysleep_count>0)
//     delaysleep_count--;
}
void Program_Init(void)
{
    Usart1ParseDataCallback = UsartParseDataHandler;//需改为默认为gps处理,UsartParseDataHandler为升级处理当调试时候改为
    parameter_init_anchor();//g_com_map表初始化角色默认为基站
    dev_id=g_com_map[DEV_ID];//这里不太对
    group_id=(uint8_t)g_com_map[GROUP_ID];//组ID
@@ -155,10 +170,96 @@
    port = g_com_map[TCP_PORT];
    g_com_map[VERSION] = (1<<8)|0;
    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);
//      if(gpio_pin_get_val(MODE_CHANGE_PIN))
//    LOG_INFO(TRACE_MODULE_APP,"固件版本:MK_Air_tag模式 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
//      else{
//      LOG_INFO(TRACE_MODULE_APP,"固件版本:MK_免布线模式 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);
}
void MinuteTask(void)
{
//    adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc采样
}
/********************************************************************************************************/
static void Lora_irq_handler(enum IO_PIN_T pin)
{
   RadioIrqProcess();
}
void SecondTask(void)
{static uint8_t second_count;
    if(second_count++>60)
    {
        second_count = 0;
        MinuteTask();
    }
   lora_tx_flag=1;
      //Lora_Tx_Poll();
//    //UWB状态检测
//if(!power_low_flag)//低供电下不需要检测重连
//   {
//    if(IfTCPConnected())
//    {
//        TCP_reconnect_timer =0;
//        flag_TCP_reconnectting = 0;
//    } else {
//        if(TCP_reconnect_timer<30)//如果TCP没有连接,每隔10分钟尝试连接30秒
//        {
//            flag_TCP_reconnectting = 1;
//        } else {
//            flag_TCP_reconnectting = 0;
//        }
//        if(TCP_reconnect_timer++>600)
//        {
//            TCP_reconnect_timer = 0;
//        }
//    }
//   }
    HIDO_TimerTick();
      if(nomove_count<=g_com_map[NOMOVESLEEP_TIME])//防止溢出
    nomove_count++;
      else{
      nomove_count=g_com_map[NOMOVESLEEP_TIME]+1;
      }
}
void spi_init(void)
{
  struct SPI_CFG_T usr_spi_cfg =
    {
        .bit_rate = 1000000,
        .data_bits = 8,
//#if TEST_SPI_MASTER
        .slave = 0,
//#else
//        .slave = 1,
//#endif
        .clk_phase = 0,
        .clk_polarity = 0,
        .ti_mode = 0,
//#if (TEST_SPI_MODE == TEST_SPI_POLL_MODE)
        .dma_rx = false,
        .dma_tx = false,
        .int_rx = false,
        .int_tx = false,
//#elif (TEST_SPI_MODE == TEST_SPI_INTERUPT_MODE)
//        .dma_rx = false,
//        .dma_tx = false,
//        .int_rx = true,
//        .int_tx = true,
//#elif (TEST_SPI_MODE == TEST_SPI_DMA_MODE)
//        .dma_rx = true,
//        .dma_tx = true,
//        .int_rx = false,
//        .int_tx = false,
//#endif
    };
     spi_open(SPI_ID0, &usr_spi_cfg);
}
static void board_init(void)
{
    uint32_t internal_flash = (REG_READ(0x40000018) >> 17) & 0x1;
@@ -203,11 +304,14 @@
    // Configure IO_04 for RF Switch
    gpio_pin_set_dir(IO_PIN_4, GPIO_DIR_OUT, 0);
#else
      Program_Init();
    // led
    board_led_init();
      spi_init();
    // 模式判断脚和spi nrst脚初始化
    board_mode_pin_init();
            Program_Init();
//    board_led_on(BOARD_LED_1);
#endif
      Board_LORA_NVIC_Init(Lora_irq_handler);
    board_configure();
}
@@ -241,7 +345,22 @@
{
    uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
}
void uart0_receive_callback(void *dev, uint32_t err_code)
{
    uart_receive(UART_ID0,m_EUART0_DMA_RXBuf,EUART0_RX_BUF_SIZE,uart0_receive_callback);
}
uint8_t bat_percent;
uint8_t  stationary_flag;
void mcu_deep_sleep(void)
{
            uint32_t lock;
            trace_flush();
            lock = int_lock();
                     sleep_timer_stop();
            power_enter_power_down_mode(1);
            int_unlock(lock);
}
int main(void)
{      
   // Initialize MCU system
@@ -249,10 +368,12 @@
    // Disable watchdog timer
    wdt_close(WDT_ID0);
    LOG_INFO(TRACE_MODULE_APP, "UCI FiRa example\r\n");
   if(gpio_pin_get_val(MODE_CHANGE_PIN))
   //delay_ms(300);//使log打印完毕
   if(gpio_pin_get_val(SLEEP_PIN))
   {
   if(gpio_pin_get_val(MODE_CHANGE_PIN))//记得改回来与正式的相反
   {
      LOG_INFO(TRACE_MODULE_APP,"固件版本:MK_Air_tag模式 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
      // Platform init for WSF
    PalSysInit();
@@ -348,26 +469,49 @@
    while (1)
    {
        wsfOsDispatcher();
#ifdef MY_MODE
            if(normal_flag)
            {
            IdleTask();
            }
#endif
        power_manage();
            if(!gpio_pin_get_val(SLEEP_PIN))
            {
            LOG_INFO(TRACE_MODULE_APP, "进入休眠模式\r\n");
            mcu_deep_sleep();
            }
    }
   }else{
//             adc_open(&usr_adc_cfg);
    }else{
      LOG_INFO(TRACE_MODULE_APP,"固件版本:MK_免布线模式 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
      uart0_Init_normal();
      Lora_1268_Init();
      SwitchLoraSettings(478,7,22);
      Uwb_init();//默认为我们测距配置
      OpenUWB();
      uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
      uart_receive(UART_ID0,m_EUART0_DMA_RXBuf,EUART0_RX_BUF_SIZE,uart0_receive_callback);
    power_init();
    sleep_timer_open(true, SLEEP_TIMER_MODE_RELOAD, sleep_timer_callback_normal);
      sleep_timer_start(__MS_TO_32K_CNT(SLEEP_TIMER_NUM));//测试
          while (1)
            {
            uwb_app_poll();//我们的测距逻辑
            if(flag_secondtask)
        {
            flag_secondtask = 0;
            SecondTask();
               //Lora_Tx_Poll();
        }
            Lora_Tx_Poll();
            if(!gpio_pin_get_val(SLEEP_PIN))
            {
            LOG_INFO(TRACE_MODULE_APP, "进入休眠模式\r\n");
            mcu_deep_sleep();
            }
            IdleTask();
            }
   }    
}else{
//delay_ms(300);
   LOG_INFO(TRACE_MODULE_APP, "进入休眠模式\r\n");
    power_init();
   mcu_deep_sleep();
}
}
void app_restore_from_power_down(void)