/**
  ******************************************************************************
  * @brief Modify the tick values for specific cases when the H/W timer doesn't
  * work (halt, ...).
  * @param [in] Delay Time to add to the ticks (unit is 500 us). Range is [1..65535].
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_Timer_Adjust(u16 Delay)
{

  disableInterrupts();

  do
  {
    if (Delay > TICK_FACTOR_10MS) /* delay > 10ms */
    {
      TSL_Tick_Base += TICK_FACTOR_10MS;
      Delay -= TICK_FACTOR_10MS;
      TSL_Timer_Check_10ms_Tick();
    }
    else
    {
      TSL_Tick_Base++;
      Delay--;
      TSL_Timer_Check_10ms_Tick();
    }
  }
  while ( Delay );

  enableInterrupts();
  
}
/**
  ******************************************************************************
  * @brief Modify the tick values for specific cases when the H/W timer doesn't
  * work (halt, ...).
  * @param[in] adjust_delay Time to add to the ticks (unit is 500µs).
  * @retval None
  ******************************************************************************
  */
void TSL_Timer_Adjust(uint32_t adjust_delay)
{

  disableInterrupts();

  do
  {
    if (adjust_delay > TICK_FACTOR_10MS)
    {
      TSL_Tick_Base += TICK_FACTOR_10MS;
      adjust_delay -= TICK_FACTOR_10MS;
      TSL_Timer_Check_10ms_Tick();
    }
    else
    {
      TSL_Tick_Base++;
      adjust_delay--;
      TSL_Timer_Check_10ms_Tick();
    }
  }
  while (adjust_delay);

  enableInterrupts();

}
__interrupt void TSL_Timer_ISR(void)
{

  TIMTICK->SR1 = 0;      // clear overflow flag

  TSL_Tick_Base++;
  TSL_Timer_Check_10ms_Tick();

  if (TSL_Tick_Flags.b.User_Start_100ms) /* Application request */
  {
    TSL_Tick_Flags.b.User_Start_100ms = 0;
    TSL_Tick_Flags.b.User_Flag_100ms = 0;
    TSL_Tick_User = (TICK_FACTOR_10MS * 10);
  }
  
  if (TSL_Tick_User > 0)
  {
    TSL_Tick_User--;
    if (TSL_Tick_User == 0)
    {
      TSL_Tick_Flags.b.User_Flag_100ms = 1; /* Give information to Application */
    }
  }

}
/**
  ******************************************************************************
  * @brief Interrupt Service Routine for HW Timer used as timebase.
  * It manages the timer ticks for the whole application (IT every 500µs).
  * @param None
  * @retval None
  ******************************************************************************
  */
void TSL_Timer_ISR(void)
{

  TSL_Tick_Base++;

  // Generic timing delay
  if (TSL_TimingDelay > 0)
  {
    TSL_TimingDelay--;
  }

  TSL_Timer_Check_10ms_Tick();

  if (TSL_Tick_Flags.b.User1_Start_100ms) /* Application request */
  {
    TSL_Tick_Flags.b.User1_Start_100ms = 0;
    TSL_Tick_Flags.b.User1_Flag_100ms = 0;
    TSL_Tick_User1 = (TICK_FACTOR_10MS * 10);
  }

  if (TSL_Tick_Flags.b.User2_Start_100ms) /* Application request */
  {
    TSL_Tick_Flags.b.User2_Start_100ms = 0;
    TSL_Tick_Flags.b.User2_Flag_100ms = 0;
    TSL_Tick_User2 = (TICK_FACTOR_10MS * 10);
  }

  if (TSL_Tick_User1 > 0)
  {
    TSL_Tick_User1--;
    if (TSL_Tick_User1 == 0)
    {
      TSL_Tick_Flags.b.User1_Flag_100ms = 1; /* Give information to Application */
    }
  }

  if (TSL_Tick_User2 > 0)
  {
    TSL_Tick_User2--;
    if (TSL_Tick_User2 == 0)
    {
      TSL_Tick_Flags.b.User2_Flag_100ms = 1; /* Give information to Application */
    }
  }

#if TIMER_CALLBACK
  USER_TickTimerCallback();
#endif

}