zhyinch
2020-12-04 60c22dec163efa6611827bf1f71466a133aed731
Src/main.c
@@ -113,7 +113,7 @@
if(huart1.Instance->ISR&USART_ISR_FE)
   {
   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
//   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
   }   
//if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
//{
@@ -123,11 +123,8 @@
//   LED_TR_OFF;
   current_time=GetLPTime();
   //motor_state=2;
      if(g_com_map[MOTOR_ONTIME]>0)
   {
      g_com_map[MOTOR_ONTIME]--;
      motor_state = 1;
   }
if(g_com_map[MOTOR_ENABLE])
{
   switch(motor_state)
      {case 0:
         MOTOR_OFF;
@@ -148,6 +145,7 @@
         MOTOR_OFF;
         break;   
   }
}
   if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
   {
      nomove_count = 0;
@@ -210,27 +208,32 @@
   }
u16 total_slotnum,current_slotnum;
u16 current_slotnum;
extern int32_t offsettimeus;
#define FIXSLOT
//#define FIXSLOT
#define FIXSLOTPOS  0
u16 slotpos_intoatl;
uint16_t bigslot_num;
uint8_t fix_slotpos =8;
uint8_t fix_slotpos =FIXSLOTPOS;
int32_t lpcount,lptime,target_time;
void SetNextPollTime(u16 slotpos)
{
u16 slotpos_intoatl;
#ifdef FIXSLOT
   slotpos = fix_slotpos;
#endif
   if(current_slotnum>=total_slotnum)
      current_slotnum-=total_slotnum;
   if(current_slotnum>=tag_frequency)
      current_slotnum-=tag_frequency;
   slotpos_intoatl = current_slotnum*bigslot_num+slotpos;
   if(current_slotnum==19)
   {slotpos_intoatl = current_slotnum*bigslot_num+slotpos;}
   target_time=(slotpos_intoatl*SLOTTIME_MS+100)*100;
   //if(target_time<90000)
   {
      lptime=target_time-offsettimeus;
      lptime=target_time-offsettimeus-10000;
      lpcount = lptime/LPTIMER_LSB;
   if(lpcount>LPTIMER_1S_COUNT)
      lpcount-=LPTIMER_1S_COUNT;
@@ -241,6 +244,7 @@
   __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
}
}
u8 active_flag=0;
void Program_Init(void)
{   float temp;
   uint16_t temp2;
@@ -248,25 +252,36 @@
   Usart1ParseDataCallback = UsartParseDataHandler;
   parameter_init();
   //deca_sleep(1000);
   g_com_map[VERSION] = 0x0204;
   g_com_map[VERSION] = 0x020b;
#ifdef DEBUG_MODE
//   g_com_map[DEV_ROLE]=1;
      g_com_map[DEV_ID]=0x123;
   g_com_map[COM_INTERVAL]=100;
      g_com_map[DEV_ID]=0x11;
//   g_com_map[COM_INTERVAL]=100;
//   g_com_map[MAX_REPORT_ANC_NUM]=1;
//   g_com_map[NOMOVESLEEP_TIME]=10;
//         g_com_map[IMU_ENABLE]=0;
         g_com_map[IMU_ENABLE]=0;
   //g_com_map[COM_INTERVAL]=50;
#endif
//g_com_map[NOMOVESLEEP_TIME]=10;
    g_com_map[COM_INTERVAL]=50;
//   g_com_map[IMU_ENABLE]=0;
//    g_com_map[COM_INTERVAL]=50;
   active_flag = g_com_map[ACTIVE_INDEX];
   if(active_flag==0)
   { g_com_map[COM_INTERVAL]=1000;
      g_com_map[IMU_ENABLE]=1;
      g_com_map[NOMOVESLEEP_TIME]=10;
      g_com_map[MOTOR_ENABLE]=0;
   }
      if(g_com_map[COM_INTERVAL]==0)
   {
     g_com_map[COM_INTERVAL]=100;
   }
    if(module_power>MAX_RFPOWER)
      {module_power=MAX_RFPOWER;}
      if(module_power<0)
      {module_power=0;}
   if(g_com_map[IMU_THRES]>10)
   {g_com_map[IMU_THRES]=2;}
    if(g_com_map[POWER]>MAX_RFPOWER)
      {g_com_map[POWER]=MAX_RFPOWER;}
      if(g_com_map[POWER]<0)
      {g_com_map[POWER]=0;}
   module_power = g_com_map[POWER];
   imu_enable=g_com_map[IMU_ENABLE];
   motor_enable=g_com_map[MOTOR_ENABLE];
@@ -280,8 +295,7 @@
   slot_startcount=tyncpoll_time*1000/LPTIMER_LSB;
   lastpoll_time=tyncpoll_time;
  tag_frequency = 1000/g_com_map[COM_INTERVAL];
   total_slotnum = 1000/g_com_map[COM_INTERVAL];
      bigslot_num = TOTAL_SLOTNUM/tag_frequency;
   bigslot_num = TOTAL_SLOTNUM/tag_frequency;
   current_slotnum=1;
   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
@@ -391,7 +405,7 @@
//      {
//      MODBUS_Poll();
//      }
      if(g_start_send_flag)
      if(g_start_send_flag&&active_flag)
   { static uint16_t blink_count=0;
      SystemClock_Config();
      g_start_send_flag = 0;   
@@ -422,7 +436,8 @@
      }
         if(m_bEUARTTxEn==0&&waitusart_timer==0)
         {   
#ifndef DEBUG_MODE
#ifndef DEBUG_MODE
            if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
#endif
         }
@@ -809,16 +824,59 @@
  /*Enter the Standby mode*/
  HAL_PWR_EnterSTANDBYMode();   
}
extern u16 tagslotpos;
extern uint8_t tx_near_msg[80],stationary_flag,gotosleep_flag;
float key_keeptime;
extern float motor_ontime;
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
   static uint8_t i=0;
   i++;
   g_start_send_flag=1;
   current_slotnum++;
   SetNextPollTime(tyncpoll_time);
   SetNextPollTime(tagslotpos);
   nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
   if(active_flag==0)
   {
   if(!GET_USERKEY)
   {
      LED_LG_ON;
         key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
      if(key_keeptime>3)
      {
         u8 i=250,keystate=1;
         while(i--)
         {
            if(i%10==0)
            {
            LED_LG_BLINK;
            }
            if(GET_USERKEY)
            {keystate = 0;}
         if(keystate==0&&!GET_USERKEY)
         {
            parameter_init();
            g_com_map[ACTIVE_INDEX] = 1;
            save_com_map_to_flash();
            delay_ms(100);
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
         }
         delay_ms(10);
         }
      }
   }else{
      LED_LG_OFF;
      key_keeptime=0;
   }
   }
   if(motor_ontime>0)
   {
      motor_ontime-=(float)1/tag_frequency;
      motor_state = 2;
   }else{
      motor_state = 0;
   }
   if(!GET_USERKEY)
   {
      key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;