/*
 * Initialize the RTC peripheral
 */
XMC_RTC_STATUS_t XMC_RTC_Init(const XMC_RTC_CONFIG_t *const config)
{
  if (XMC_RTC_IsRunning() == false)
  {
    if (XMC_RTC_IsEnabled() == false)
    {
      XMC_RTC_Enable();
	}
	
    XMC_RTC_SetPrescaler(config->prescaler);

	while ((XMC_SCU_GetMirrorStatus() & (SCU_GENERAL_MIRRSTS_RTC_TIM0_Msk | SCU_GENERAL_MIRRSTS_RTC_TIM1_Msk)) != 0U)
	{
      /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
    }
    RTC->TIM0 = config->time.raw0;
    RTC->TIM1 = config->time.raw1;
	
    while ((XMC_SCU_GetMirrorStatus() & (SCU_GENERAL_MIRRSTS_RTC_ATIM0_Msk | SCU_GENERAL_MIRRSTS_RTC_ATIM1_Msk)) != 0U)
    {
      /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
    }
    RTC->ATIM0 = config->alarm.raw0;	
    RTC->ATIM1 = config->alarm.raw1;    
  }
  return XMC_RTC_STATUS_OK;
}
Пример #2
0
/*
 *  This function is to get the time in seconds calculated from Epoch time
 *  (01/01/1970).
 */
RTC_STATUS_t RTC_Time(time_t* time_value)
{
  uint32_t elapsedyear;
  uint32_t elapsedmonth;
  uint32_t elapseddays;
  uint32_t elapsedseconds;

  RTC_STATUS_t status;
  XMC_RTC_TIME_t curr_time;
  
  XMC_ASSERT("RTC_Time: NULL pointer", time_value != NULL);

  /*Check if RTC module is enabled and no NULL pointer*/
  if (true == XMC_RTC_IsRunning())
  {
    /* Read values from TIM0 and TIM1 registers */
    XMC_RTC_GetTime(&curr_time);

    /*Count number of Days for Elapsed Years since Epoch*/
    elapseddays = ((uint32_t)curr_time.year - RTC_EPOCH_YEAR) * RTC_DAYS_IN_AN_YEAR;

    /* Add the number of days to be adjusted for leap years,
       start from previous year and check backward */
    for (elapsedyear = ((uint32_t)curr_time.year - 1U); elapsedyear>= (uint32_t)1970; elapsedyear--)
    {
      if (RTC_lleapyear((uint16_t)elapsedyear))
      {
        elapseddays++;
      }
    }
    /*If current year is leap year add 1 only if March or later*/
    if (RTC_lleapyear(curr_time.year))
    {
      if(curr_time.month > 2U)
      {
        elapseddays++;
      }
    }

    /*Add number of Days from Elapsed months from current year*/
    for (elapsedmonth = (curr_time.month); elapsedmonth != 0U; elapsedmonth--)
    {
      elapseddays += RTC_DAYS_IN_MONTH[elapsedmonth];
    }

    /*Add Elapsed days from current month*/
    elapseddays += curr_time.days;

    /*Accumulate the total seconds for ElapsedDays*/
    elapsedseconds = (elapseddays * RTC_SECONDS_IN_A_DAY);

    /*Add seconds for current hour, minute and seconds*/
    elapsedseconds += ((uint32_t)curr_time.hours * RTC_SECONDS_IN_AN_HOUR);
    elapsedseconds += ((uint32_t)curr_time.minutes * RTC_SECONDS_IN_A_MINUTE);
    elapsedseconds += (uint32_t)curr_time.seconds;

    *time_value = (time_t)elapsedseconds;
     status = RTC_STATUS_SUCCESS;
  }
  else
  {
    status = RTC_STATUS_FAILURE;
  }
  return (status);
}