zhyinch
2020-06-20 3e165b6f13bfaf66385d857819cacff6e4d6f37c
Src/main.c
@@ -138,9 +138,39 @@
uint16_t tyncpoll_time,lpsettime;
uint16_t slottime,max_slotpos;
uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency;
uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
extern uint8_t module_power;
uint8_t imu_enable,motor_enable;   
u16 GetLPTime(void)
{
   u16 count=HAL_LPTIM_ReadCounter(&hlptim1);
 return count*LPTIMER_LSB/1000;
}
u16 total_slotnum,current_slotnum;
extern int32_t offsettimeus;
void SetNextPollTime(u16 time)
{
  int32_t lpcount,lptime,target_time;
   time=0;
   if(current_slotnum>=total_slotnum)
      current_slotnum-=total_slotnum;
   target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
   //if(target_time<90000)
   {
      lptime=target_time-offsettimeus;
      lpcount = lptime/LPTIMER_LSB;
   if(lpcount>LPTIMER_1S_COUNT)
      lpcount-=LPTIMER_1S_COUNT;
   if(lpcount<0)
   {
   lpcount+=LPTIMER_1S_COUNT;
   }
   __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
}
}
void Program_Init(void)
{   float temp;
   uint16_t temp2;
@@ -164,14 +194,15 @@
   
   group_id=g_com_map[GROUP_ID];
   dev_id = g_com_map[DEV_ID];
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*4/3);
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]/4)+4;
   max_slotpos=g_com_map[COM_INTERVAL]/slottime;
   tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;   
   slot_startcount=(float)(tyncpoll_time+10)*1000/LPTIMER_LSB;
   lastpoll_count=slot_startcount;
   interval_count=(float)g_com_map[COM_INTERVAL]*1000/LPTIMER_LSB;
   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];
   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
  {
    Error_Handler();
@@ -243,7 +274,7 @@
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPTIM1_Init();
  MX_USART1_UART_Init();
 // MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_ADC_Init();
  MX_DMA_Init();
@@ -254,7 +285,7 @@
   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=1;
   DW_DISABLE;
//   HAL_Delay(2000);
@@ -273,8 +304,9 @@
    /* USER CODE BEGIN 3 */
      if(g_start_send_flag)
   { static uint16_t blink_count=0;
      //SystemClock_Config();
      SystemClock_Config();
      g_start_send_flag = 0;   
//      if(current_count>slot_startcount&&current_count<slot_startcount+interval_count)
//      {
//      HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET);
@@ -452,10 +484,10 @@
    Error_Handler();
  }
  /* USER CODE BEGIN LPTIM1_Init 2 */
   if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK)
  {
    Error_Handler();
  }
//   if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK)
//  {
//    Error_Handler();
//  }
  /* USER CODE END LPTIM1_Init 2 */
}
@@ -605,7 +637,7 @@
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  /*Configure GPIO pin : SPI_CS_Pin */
  GPIO_InitStruct.Pin = SPI_CS_Pin;
  GPIO_InitStruct.Pin = SPI_CS_Pin|GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
@@ -688,13 +720,8 @@
   static uint8_t i=0;
   i++;
   g_start_send_flag=1;
   lastpoll_count+=interval_count;
   if(lastpoll_count>LPTIMER_1S_COUNT-30)
   {
      lastpoll_count = slot_startcount;
   }
   __HAL_LPTIM_COMPARE_SET(&hlptim1, lastpoll_count);
   current_slotnum++;
   SetNextPollTime(tyncpoll_time);
   nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
   if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
   {