zhyinch
2020-12-14 8b7f0ebd9a00c82ec30a6a0a68b7caca69b0f43c
Src/main.c
@@ -28,6 +28,8 @@
#include "deca_device_api.h"
#include "global_param.h"
#include "TrackingDiff.h"
#include "dw_app.h"
//#define DEBUG_MODE
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -85,22 +87,34 @@
uint16_t slottime,max_slotpos;
uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
extern uint8_t module_power;
extern float nomove_count;
uint8_t imu_enable,motor_enable;   
u16 GetLPTime(void)
{
   u16 count=HAL_LPTIM_ReadCounter(&hlptim1);
 return count*LPTIMER_LSB/1000;
}   
void UsartInit(void)
{
   waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME;
    MX_DMA_Init();
   MX_USART1_UART_Init();
 if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
   {
    Error_Handler();
   }
}
uint8_t state5v;
void IdleTask(void)
{u16 current_time;
   
         UART_CheckReceive();
      UART_CheckSend();
//if(huart1.Instance->ISR&USART_ISR_FE)
//   {
//   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
//   }
if(huart1.Instance->ISR&USART_ISR_FE)
   {
   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
   }
//if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
//{
//   LED_TR_ON;
@@ -109,12 +123,18 @@
//   LED_TR_OFF;
   current_time=GetLPTime();
   //motor_state=2;
//      if(g_com_map[MOTOR_ONTIME]>0)
//   {
//      g_com_map[MOTOR_ONTIME]--;
//      motor_state = 1;
//   }
   switch(motor_state)
      {case 0:
         MOTOR_OFF;
         break;
      case 1:
         if(current_time<MOTOR_ONTIME)
         if(current_time<WARNING_MOTORONTIME)
         {
            MOTOR_ON;      
         }else{
@@ -131,17 +151,24 @@
   }
   if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
   {
      nomove_count = 0;
      if(state5v==0)
      {
         state5v=1;
         UsartInit();
      }
      if(bat_percent>99)
      {
         LED_TR_OFF;
         LED_TB_ON;
         LED_LR_OFF;
         LED_LG_ON;
      }else{
         LED_TR_ON;
         LED_TB_OFF;
         LED_LR_ON;
         LED_LG_OFF;
      }      
   }else{
      LED_TR_OFF;
      LED_TB_OFF;
      state5v=0;
      LED_LR_OFF;
      LED_LG_OFF;
//   }
}
   if(g_com_map[CNT_UPDATE]==1)
@@ -174,6 +201,10 @@
         g_com_map[CNT_RESTART]=0;
         save_com_map_to_flash();
         delay_ms(100);
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
      }
         if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
      {
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
      }
@@ -211,7 +242,7 @@
   Usart1ParseDataCallback = UsartParseDataHandler;
   parameter_init();
   //deca_sleep(1000);
   g_com_map[VERSION] = 0x010b;
   g_com_map[VERSION] = 0x0113;
#ifdef DEBUG_MODE
//   g_com_map[DEV_ROLE]=1;
//      g_com_map[DEV_ID]=1;
@@ -219,15 +250,21 @@
//   g_com_map[MAX_REPORT_ANC_NUM]=1;
   g_com_map[NOMOVESLEEP_TIME]=10;
#endif
      if(g_com_map[COM_INTERVAL]==0)
   {
     g_com_map[COM_INTERVAL]=100;
   }
    if(module_power>67)
      {module_power=67;}
      if(module_power<0)
      {module_power=0;}
   module_power = g_com_map[POWER];
   imu_enable=g_com_map[IMU_ENABLE];
   motor_enable=g_com_map[MOTOR_ENABLE];
   
   group_id=g_com_map[GROUP_ID];
   dev_id = g_com_map[DEV_ID];
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*0.4)+2;
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
   max_slotpos=g_com_map[COM_INTERVAL]/slottime;
   tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;   
   slot_startcount=tyncpoll_time*1000/LPTIMER_LSB;
@@ -240,7 +277,7 @@
  {
    Error_Handler();
  }
//g_com_map[NOMOVESLEEP_TIME]=5;
   printf("标签ID: %d .\r\n",dev_id);
   printf("通讯间隔: %d ms.\r\n",g_com_map[COM_INTERVAL]);
   printf("单次通讯基站数量: %d个.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
@@ -306,18 +343,19 @@
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPTIM1_Init();
    MX_DMA_Init();
   MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_ADC_Init();
  /* USER CODE BEGIN 2 */
   LED_LG_ON;
   Program_Init();
   LIS3DH_Data_Init();   
   Dw1000_Init();
   Dw1000_App_Init();
   
   dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
   dwt_entersleep();
   //dwt_entersleep();
   waitusart_timer=tag_frequency*USART_KEEPWAKE_TIME;;
   DW_DISABLE;
    if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
@@ -353,13 +391,13 @@
      
         bat_percent=Get_Battary();
      if(bat_percent>15)
      {   LED0_ON;
      {   LED_TB_ON;
         Tag_App();
         LED0_OFF;
         LED_TB_OFF;
      }else{
         LED_LR_ON;
         LED_TR_ON;
         Tag_App();
         LED_LR_OFF;
         LED_TR_OFF;
      }
      //LED0_BLINK;      
      IdleTask();   
@@ -367,10 +405,12 @@
      {
         waitusart_timer--;
      }
         if(m_bEUARTTxEn==0&&waitusart_timer==0)
         if(m_bEUARTTxEn==0&&waitusart_timer==0&&(motor_state==0||motor_state==3))
         {   
#ifndef DEBUG_MODE
if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
#endif
         }
      
 
@@ -649,7 +689,7 @@
  HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET);
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|MOTOR_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|MOTOR_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);
@@ -696,8 +736,10 @@
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pins : MOTOR_Pin LED1_G_Pin LED1_R_Pin LED2_R_Pin */
  GPIO_InitStruct.Pin = MOTOR_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin;
  /*Configure GPIO pins : MOTOR_Pin LED1_G_Pin LED1_R_Pin LED2_R_Pin
                           PWR_CTL_Pin */
  GPIO_InitStruct.Pin = MOTOR_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin
                          |PWR_CTL_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@@ -753,7 +795,7 @@
  /*Enter the Standby mode*/
  HAL_PWR_EnterSTANDBYMode();   
}
extern float nomove_count;
extern uint8_t tx_near_msg[80],stationary_flag;
float key_keeptime;
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
@@ -773,6 +815,16 @@
   }else{
      key_keeptime=0;
   }
   if(nomove_count>STATIONARY_TIME)
   {
      stationary_flag = 1;
   }else{
      stationary_flag = 0;
   }
if(nomove_count>=g_com_map[NOMOVESLEEP_TIME]-1&&imu_enable)
   {
      tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
   }
   if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
   {
      mcu_sleep();