WXK
2023-09-15 671f59b69396b30ee80c3ddb57e4cd11b979d642
Src/main.c
@@ -30,10 +30,15 @@
#include "TrackingDiff.h"
#include "dw_app.h"
#include "Flash.h"
#include "user.h"
#include "sx126x-board.h"
#include "string.h"
#include "stdio.h"
#include "delay.h"
#define NSH1 0x0001
#define GP   0x0002
#define FLASH_HARDWARE_VERSION_MAP      (uint32_t)0x08004F00    //硬件版本号和批次信息位置
//#define DEBUG_MODE
#define DEBUG_MODE
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -91,7 +96,7 @@
extern u8 motor_state;
uint16_t tyncpoll_time,lpsettime;
uint16_t slottime,max_slotpos;
uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time;
extern uint8_t module_power;
extern float nomove_count;
uint8_t imu_enable,motor_enable;   
@@ -137,7 +142,7 @@
uint8_t state5v = 1;
float motor_keeptime;
void IdleTask(void)
{u16 current_time;
{
//if(huart1.Instance->ISR&USART_ISR_FE)
//   {
//   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
@@ -265,7 +270,7 @@
      {module_power=0;}
   current_slotnum=1;
   current_time=GetLPTime();
//   current_time=GetLPTime();
      if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
  {
    Error_Handler();
@@ -339,6 +344,34 @@
    HAL_ADC_DeInit(&hadc);
    HAL_ADCEx_DisableVREFINT();
}
uint16_t irq_num;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == GPIO_PIN_11)
    {
//        l_bIsVibration = HIDO_TRUE;
//        nomove_time=0;
//        printf("RXD :77777\r\n");
        irq_num++;
        RadioIrqProcess();
    }
//    if(GPIO_Pin == GPIO_PIN_8)
//    {
//     Anchor_App();
//    }
}
void Uwb_Lora_Switch(uint8_t uwb, uint8_t lora)
{
if(uwb)
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);
if(lora)
HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_RESET);
}
/* USER CODE END 0 */
/**
@@ -370,27 +403,35 @@
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPTIM1_Init();
    MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_ADC_Init();
  MX_IWDG_Init();
  MX_DMA_Init();
//  MX_IWDG_Init();
  /* USER CODE BEGIN 2 */
    LED_LG_ON;
    Program_Init();
    //   LIS3DH_Data_Init();   
//    Uwb_Lora_Switch(1,0);
    Dw1000_Init();
    Dw1000_App_Init();
    Delay_Ms(10);
//    Uwb_Lora_Switch(0,0);
    //   HardWareTypeDiffConfig();
    dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
    dwt_entersleep();
    DW_DISABLE;
//    dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
//    dwt_entersleep();
     if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
    {
     Error_Handler();
    }
    HAL_Delay(2000);
//    Uwb_Lora_Switch(0,1);
    Lora_Init();
    Delay_Ms(10);
//    Uwb_Lora_Switch(0,0);
    printf("RXD :66666\r\n");
    usart_send[0]=0x55;
    usart_send[1]=0xaa;
//   mcu_sleep();
   //LED0_BLINK;      
//   SystemPower_Config();
@@ -404,53 +445,16 @@
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
      if(g_start_send_flag)
   { static uint16_t blink_count=0;
      SystemClock_Config();
      g_start_send_flag = 0;
      HAL_IWDG_Refresh(&hiwdg);
//      if(current_count>slot_startcount&&current_count<slot_startcount+interval_count)
//      {
//      HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET);
//      }else{
//      HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET);
//      }
//      LED_TR_BLINK;s
      if(bat_percent>15)
      {   LED_TB_ON;
         Tag_App();
         LED_TB_OFF;
      }else{
         LED_TR_ON;
         Tag_App();
         LED_TR_OFF;
      }
      //LED0_BLINK;
      IdleTask();
      if(waitusart_timer>0)
      {
         waitusart_timer--;
      }
   }else{
   IdleTask();
   }
    if(m_bEUARTTxEn==0&&waitusart_timer==0)
    {
     if(memcmp(g_com_map2,g_com_map,200)!=0)
     {
        SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
     }
Lora_rx_open();
//    HAL_IWDG_Refresh(&hiwdg);
//    IdleTask();
//   Radio.IrqProcess( ); // Process Radio IRQ
//     Anchor_App();
    #ifndef DEBUG_MODE   
    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))            
                    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
    #endif
    }
// HAL_Delay(100);
    //  Get_Battary();
   //   LIS3DH_Data_Init();
  }
  /* USER CODE END 3 */
}
@@ -753,13 +757,14 @@
  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|PWR_CTL_Pin, GPIO_PIN_SET);
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|RADIO_nRESET_Pin
                          |RADIO_NSS_Pin, GPIO_PIN_RESET);
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin, GPIO_PIN_RESET);
  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  /*Configure GPIO pins : PA0 RADIO_DIO1_Pin */
  GPIO_InitStruct.Pin = GPIO_PIN_0|RADIO_DIO1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -797,8 +802,10 @@
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(INPUT_5V_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin LED2_R_Pin PWR_CTL_Pin */
  GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|PWR_CTL_Pin;
  /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin LED2_R_Pin RADIO_nRESET_Pin
                           RADIO_NSS_Pin PWR_CTL_Pin */
  GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|RADIO_nRESET_Pin
                          |RADIO_NSS_Pin|PWR_CTL_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@@ -811,6 +818,12 @@
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  /*Configure GPIO pin : RADIO_BUSY_Pin */
  GPIO_InitStruct.Pin = RADIO_BUSY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(RADIO_BUSY_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pin : PWR_ON_Pin */
  GPIO_InitStruct.Pin = PWR_ON_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
@@ -820,6 +833,9 @@
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
@@ -851,65 +867,11 @@
extern uint8_t tx_near_msg[80],stationary_flag;
float key_keeptime;
extern float freqlost_count,range_lost_time;
uint32_t lp_time;
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
   static uint8_t i=0,lost_jumpcount=0;
   i++;
   current_slotnum++;
    freqlost_count+=1/(float)tag_frequency;
    range_lost_time +=1/(float)tag_frequency;
    lp_time++;
    
    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;
    }
    if(range_lost_time<1||freqlost_count>FREQ_LOST_TIME)
    {
        SetNextPollTime(0);
    }else{
        SetNextPollTime(5);
    }
   nomove_count+=1/(float)tag_frequency;
    if(motor_keeptime>0)
   {
        nomove_count+=1/(float)tag_frequency;
   }
   if(!GET_USERKEY)
   {
      key_keeptime+=1/(float)tag_frequency;
      if(key_keeptime>=KEY_KEEPRESET_TIME)
      {
            g_com_map[ACTIVE_INDEX]=!active_flag;
            save_com_map_to_flash();
            MOTOR_ON;
            HAL_Delay(2000);
         SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
      }
   }else{
      key_keeptime=0;
   }
//   if(nomove_count>STATIONARY_TIME)
//   {
//      stationary_flag = 1;
//   }else{
//      stationary_flag = 0;
//   }
//if(nomove_count>=nomovesleeptime-1&&imu_enable)
//   {
//      tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
//   }
//   if(nomove_count>nomovesleeptime&&imu_enable)
//   {
//      mcu_sleep();
//   }
}
/* USER CODE END 4 */