mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
324:406fd2029f23
Parent:
149:1fb5f62b92bd
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/rtc/fsl_rtc_hal.c	Mon Sep 15 15:30:06 2014 +0100
+++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/rtc/fsl_rtc_hal.c	Thu Sep 18 14:00:17 2014 +0100
@@ -32,139 +32,348 @@
 #include "fsl_device_registers.h"
 
 /*******************************************************************************
+ * Definitions
+ ******************************************************************************/
+
+#define SECONDS_IN_A_DAY     (86400U)
+#define SECONDS_IN_A_HOUR    (3600U)
+#define SECONDS_IN_A_MIN     (60U)
+#define MINS_IN_A_HOUR       (60U)
+#define HOURS_IN_A_DAY       (24U)
+#define DAYS_IN_A_YEAR       (365U)
+#define DAYS_IN_A_LEAP_YEAR  (366U)
+#define YEAR_RANGE_START     (1970U)
+#define YEAR_RANGE_END       (2099U)
+
+/*******************************************************************************
+ * Variables
+ ******************************************************************************/
+
+/* Table of month length (in days) for the Un-leap-year*/
+static const uint8_t ULY[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U,
+    31U,30U,31U};
+
+/* Table of month length (in days) for the Leap-year*/
+static const uint8_t LY[] = {0U, 31U, 29U, 31U, 30U, 31U, 30U, 31U, 31U, 30U,
+    31U,30U,31U};
+
+/* Number of days from begin of the non Leap-year*/
+static const uint16_t MONTH_DAYS[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U,
+    212U, 243U, 273U, 304U, 334U};
+
+/*******************************************************************************
  * Code
  ******************************************************************************/
 
+/*FUNCTION**********************************************************************
+ *
+ * Function Name : RTC_HAL_ConvertSecsToDatetime
+ * Description   : converts time data from seconds to a datetime structure.
+ * This function will convert time data from seconds to a datetime structure.
+ *
+ *END**************************************************************************/
+void RTC_HAL_ConvertSecsToDatetime(const uint32_t * seconds, rtc_datetime_t * datetime)
+{
+    uint32_t x;
+    uint32_t Seconds, Days, Days_in_year;
+    const uint8_t *Days_in_month;
+
+    /* Start from 1970-01-01*/
+    Seconds = *seconds;
+    /* days*/
+    Days = Seconds / SECONDS_IN_A_DAY;
+    /* seconds left*/
+    Seconds = Seconds % SECONDS_IN_A_DAY;
+    /* hours*/
+    datetime->hour = Seconds / SECONDS_IN_A_HOUR;
+    /* seconds left*/
+    Seconds = Seconds % SECONDS_IN_A_HOUR;
+    /* minutes*/
+    datetime->minute = Seconds / SECONDS_IN_A_MIN;
+    /* seconds*/
+    datetime->second = Seconds % SECONDS_IN_A_MIN;
+    /* year*/
+    datetime->year = YEAR_RANGE_START;
+    Days_in_year = DAYS_IN_A_YEAR;
+
+    while (Days > Days_in_year)
+    {
+        Days -= Days_in_year;
+        datetime->year++;
+        if  (datetime->year & 3U)
+        {
+            Days_in_year = DAYS_IN_A_YEAR;
+        }
+        else
+        {
+            Days_in_year = DAYS_IN_A_LEAP_YEAR;
+        }
+    }
+
+    if  (datetime->year & 3U)
+    {
+        Days_in_month = ULY;
+    }
+    else
+    {
+        Days_in_month = LY;
+    }
+
+    for (x=1U; x <= 12U; x++)
+    {
+        if (Days <= (*(Days_in_month + x)))
+        {
+            datetime->month = x;
+            break;
+        }
+        else
+        {
+            Days -= (*(Days_in_month + x));
+        }
+    }
+
+    datetime->day = Days;
+}
+
+/*FUNCTION**********************************************************************
+ *
+ * Function Name : RTC_HAL_IsDatetimeCorrectFormat
+ * Description   : checks if the datetime is in correct format.
+ * This function will check if the given datetime is in the correct format.
+ *
+ *END**************************************************************************/
+bool RTC_HAL_IsDatetimeCorrectFormat(const rtc_datetime_t * datetime)
+{
+    bool result = false;
+
+    /* Test correctness of given parameters*/
+    if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) ||
+        (datetime->month > 12U) || (datetime->month < 1U) ||
+        (datetime->day > 31U) || (datetime->day < 1U) ||
+        (datetime->hour >= HOURS_IN_A_DAY) || (datetime->minute >= MINS_IN_A_HOUR) ||
+        (datetime->second >= SECONDS_IN_A_MIN))
+    {
+        /* If not correct then error*/
+        result = false;
+    }
+    else
+    {
+        result = true;
+    }
+
+    /* Is given year un-leap-one?*/
+    /* Leap year calculation only looks for years divisible by 4 as acceptable years is limited */
+    if ( result && (datetime->year & 3U))
+    {
+        /* Does the obtained number of days exceed number of days in the appropriate month & year?*/
+        if (ULY[datetime->month] < datetime->day)
+        {
+            /* If yes (incorrect datetime inserted) then error*/
+            result = false;
+        }
+    }
+    else /* Is given year leap-one?*/
+    {
+        /* Does the obtained number of days exceed number of days in the appropriate month & year?*/
+        if (result && (LY[datetime->month] < datetime->day))
+        {
+            /* if yes (incorrect date inserted) then error*/
+            result = false;
+        }
+    }
+
+    return result;
+}
 
 /*FUNCTION**********************************************************************
  *
- * Function Name : rtc_hal_init
- * Description   : initializes the RTC module.
- * This function will first configure the oscillator load in pF, set/clear the
- * 32kHz clock out to peripheral, enable/disable 32.768 kHz oscillator. After
- * setting 32.768 kHz oscillator bit, wait the oscillator startup time before
- * enabling the time counter to allow the 32.768 kHz clock time to stabilize.
- * If devices have the wakeup pin, this function will enable/disable the wakeup
- * pin. Then this function configures time prescaler, time seconds, time alarm,
- * the compensation interval in seconds from 1 to 256 if requested, and the
- * number of 32.768 kHz clock cycles in each second. This function also
- * enable/disable the RTC interrupts as requested.
+ * Function Name : RTC_HAL_ConvertDatetimeToSecs
+ * Description   : converts time data from datetime to seconds.
+ * This function will convert time data from datetime to seconds.
  *
  *END**************************************************************************/
-void rtc_hal_init(rtc_hal_init_config_t * configs)
+void RTC_HAL_ConvertDatetimeToSecs(const rtc_datetime_t * datetime, uint32_t * seconds)
 {
-    /* check for null pointer*/
-    if(NULL == configs)
-    {
-        return;
-    }
-
-    /* Clear to the default value */
-    HW_RTC_CR_WR(0x0);
-
-    /* Use the following macro masks to set the flags which enable load config
-     */
-    switch(configs->enableOscillatorLoadConfg)
+    /* Compute number of days from 1970 till given year*/
+    *seconds = (datetime->year - 1970U) * DAYS_IN_A_YEAR;
+    /* Add leap year days */
+    *seconds += ((datetime->year / 4) - (1970U / 4));
+    /* Add number of days till given month*/
+    *seconds += MONTH_DAYS[datetime->month];
+    /* Add days in given month*/
+    *seconds += datetime->day;
+    /* For leap year if month less than or equal to Febraury, decrement day counter*/
+    if ((!(datetime->year & 3U)) && (datetime->month <= 2U))
     {
-    case 2:
-       HW_RTC_CR_WR(BM_RTC_CR_SC2P);
-       break;
-    case 4:
-       HW_RTC_CR_WR(BM_RTC_CR_SC4P);
-       break;
-    case 8:
-       HW_RTC_CR_WR(BM_RTC_CR_SC8P);
-       break;
-    case 16:
-       HW_RTC_CR_WR(BM_RTC_CR_SC16P);
-       break;
-    default:
-       break;
+        (*seconds)--;
     }
 
-    BW_RTC_CR_CLKO((uint32_t)configs->disableClockOutToPeripheral);
+    *seconds = ((*seconds) * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) +
+               (datetime->minute * SECONDS_IN_A_MIN) + datetime->second;
+}
 
-    /* After enabling this, will wait the oscillator startup time before enabling
-    * the time counter TSR[TSR] to allow the 32.768 kHz clock time to stabilize.
-    */
-    BW_RTC_CR_OSCE((uint32_t)configs->enable32kOscillator);
+/*FUNCTION**********************************************************************
+ *
+ * Function Name : RTC_HAL_Enable
+ * Description   : initializes the RTC module.
+ * This function will initiate a soft-reset of the RTC module to reset
+ * all the RTC registers. It also enables the RTC oscillator.
+ *
+ *END**************************************************************************/
+void RTC_HAL_Enable(uint32_t rtcBaseAddr)
+{
+    /* Enable RTC oscillator since it is required to start the counter*/
+    RTC_HAL_SetOscillatorCmd(rtcBaseAddr, true);
+}
+
+void RTC_HAL_Disable(uint32_t rtcBaseAddr)
+{
+    /* Disable counter*/
+    RTC_HAL_EnableCounter(rtcBaseAddr, false);
 
-#if FSL_FEATURE_RTC_HAS_WAKEUP_PIN
-    /*! For devices that have the wakeup pin this variable will indicate if it is
-    *  to be enabled (set to 'true') or not (set to 'false'). \n
-    *  See the device's user manual for details depending on each device's 
-    *  specific wakeup pin feature implementation.
-    */
-    BW_RTC_CR_WPE((uint32_t)configs->enableWakeupPin);
-#endif
+    /* Disable RTC oscillator */
+    RTC_HAL_SetOscillatorCmd(rtcBaseAddr, false);
+}
+
+void RTC_HAL_Init(uint32_t rtcBaseAddr)
+{
+    uint32_t seconds = 0x1;
+
+    /* Resets the RTC registers except for the SWR bit */
+    RTC_HAL_SoftwareReset(rtcBaseAddr);
+    RTC_HAL_SoftwareResetFlagClear(rtcBaseAddr);
+
+    /* Set TSR register to 0x1 to avoid the TIF bit being set in the SR register */
+    RTC_HAL_SetSecsReg(rtcBaseAddr, seconds);
+
+    /* Clear the interrupt enable register */
+    RTC_HAL_SetSecsIntCmd(rtcBaseAddr, false);
+    RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, false);
+    RTC_HAL_SetTimeOverflowIntCmd(rtcBaseAddr, false);
+    RTC_HAL_SetTimeInvalidIntCmd(rtcBaseAddr, false);
+}
+
+void RTC_HAL_SetDatetime(uint32_t rtcBaseAddr, const rtc_datetime_t * datetime)
+{
+    uint32_t seconds;
 
-    /* write prescaler before writing to counter register*/
-    if(configs->prescalerAt)
-    {
-        BW_RTC_SR_TCE(0x00U); /* disable counter before configuring the prescaler*/
-        HW_RTC_TPR_WR(configs->prescalerAt);
-    }
+    /* Protect against null pointers*/
+    assert(datetime);
+
+    RTC_HAL_ConvertDatetimeToSecs(datetime, &seconds);
+    /* Set time in seconds */
+    RTC_HAL_SetDatetimeInsecs(rtcBaseAddr, seconds);
+}
+
+void RTC_HAL_SetDatetimeInsecs(uint32_t rtcBaseAddr, const uint32_t seconds)
+{
+    /* Disable counter*/
+    RTC_HAL_EnableCounter(rtcBaseAddr, false);
+    /* Set seconds counter*/
+    RTC_HAL_SetSecsReg(rtcBaseAddr, seconds);
+    /* Enable the counter*/
+    RTC_HAL_EnableCounter(rtcBaseAddr, true);
+}
+
+void RTC_HAL_GetDatetime(uint32_t rtcBaseAddr, rtc_datetime_t * datetime)
+{
+    uint32_t seconds = 0;
+
+    /* Protect against null pointers*/
+    assert(datetime);
+
+    RTC_HAL_GetDatetimeInSecs(rtcBaseAddr, &seconds);
 
-    /* write time seconds should be written before writing to counter register*/
-    if(configs->startSecondsCounterAt)
+    RTC_HAL_ConvertSecsToDatetime(&seconds, datetime);
+}
+
+void RTC_HAL_GetDatetimeInSecs(uint32_t rtcBaseAddr, uint32_t * seconds)
+{
+    /* Protect against null pointers*/
+    assert(seconds);
+    *seconds = RTC_HAL_GetSecsReg(rtcBaseAddr);
+}
+
+bool RTC_HAL_SetAlarm(uint32_t rtcBaseAddr, const rtc_datetime_t * date)
+{
+    uint32_t alrm_seconds, curr_seconds;
+
+    /* Protect against null pointers*/
+    assert(date);
+
+    RTC_HAL_ConvertDatetimeToSecs(date, &alrm_seconds);
+
+    /* Get the current time */
+    curr_seconds = RTC_HAL_GetSecsReg(rtcBaseAddr);
+
+    /* Make sure the alarm is for a future time */
+    if (alrm_seconds <= curr_seconds)
     {
-        BW_RTC_SR_TCE(0x00U); /* disable time counter before configuring*/
-        HW_RTC_TSR_WR(configs->startSecondsCounterAt);
-    }
-
-    if(configs->alarmCounterAt)
-    {
-        HW_RTC_TAR_WR(configs->alarmCounterAt);
+        return false;
     }
 
-    /*  Configures the compensation interval in seconds from 1 to 256 to control
-    *  how frequently the TCR should adjust the number of 32.768 kHz cycles in
-    *  each second. The value written should be one less than the number of
-    *  seconds (for example, write zero to configure for a compensation interval
-    *  of one second). This register is double buffered and writes do not take
-    *  affect until the end of the current compensation interval.
-    */
-    if(configs->compensationInterval)
+    /* set alarm in seconds*/
+    RTC_HAL_SetAlarmReg(rtcBaseAddr, alrm_seconds);
+
+    return true;
+}
+
+void RTC_HAL_GetAlarm(uint32_t rtcBaseAddr, rtc_datetime_t * date)
+{
+    uint32_t seconds = 0;
+
+    /* Protect against null pointers*/
+    assert(date);
+
+    /* Get alarm in seconds  */
+    seconds = RTC_HAL_GetAlarmReg(rtcBaseAddr);
+
+    RTC_HAL_ConvertSecsToDatetime(&seconds, date);
+}
+
+#if FSL_FEATURE_RTC_HAS_MONOTONIC
+
+void RTC_HAL_GetMonotonicCounter(uint32_t rtcBaseAddr, uint64_t * counter)
+{
+    uint32_t tmpCountHigh = 0;
+    uint32_t tmpCountLow = 0;
+
+    tmpCountHigh = RTC_HAL_GetMonotonicCounterHigh(rtcBaseAddr);
+    tmpCountLow = RTC_HAL_GetMonotonicCounterLow(rtcBaseAddr);
+
+    *counter = (((uint64_t)(tmpCountHigh) << 32) | ((uint64_t)tmpCountLow));
+}
+
+void RTC_HAL_SetMonotonicCounter(uint32_t rtcBaseAddr, const uint64_t * counter)
+{
+    uint32_t tmpCountHigh = 0;
+    uint32_t tmpCountLow = 0;
+
+    tmpCountHigh = (uint32_t)((*counter) >> 32);
+    RTC_HAL_SetMonotonicCounterHigh(rtcBaseAddr, tmpCountHigh);
+    tmpCountLow = (uint32_t)(*counter);
+    RTC_HAL_SetMonotonicCounterLow(rtcBaseAddr, tmpCountLow);
+}
+
+bool RTC_HAL_IncrementMonotonicCounter(uint32_t rtcBaseAddr)
+{
+    bool result = false;
+
+    if((!(RTC_HAL_IsMonotonicCounterOverflow(rtcBaseAddr))) && (!(RTC_HAL_IsTimeInvalid(rtcBaseAddr))))
     {
-        BW_RTC_TCR_CIR(configs->compensationInterval);
+        /* prepare for incrementing after write*/
+        RTC_HAL_SetMonotonicEnableCmd(rtcBaseAddr, true);
+
+        /* write anything so the counter increments*/
+        BW_RTC_MCLR_MCL(rtcBaseAddr, 1U);
+
+        result = true;
     }
 
-    /*  Configures the number of 32.768 kHz clock cycles in each second. This
-    *  register is double buffered and writes do not take affect until the end
-    *  of the current compensation interval.\n
-    * \n
-    *    80h Time prescaler register overflows every 32896 clock cycles.\n
-    *    ... ...\n
-    *    FFh Time prescaler register overflows every 32769 clock cycles.\n
-    *    00h Time prescaler register overflows every 32768 clock cycles.\n
-    *    01h Time prescaler register overflows every 32767 clock cycles.\n
-    *    ... ...\n
-    *    7Fh Time prescaler register overflows every 32641 clock cycles.\n
-    */
-    BW_RTC_TCR_TCR(configs->timeCompensation);
+    return result;
+}
 
-    /* Set/clear any of the following bitfields to enable/disable the
-    * respective interrupts.\n
-    *   TSIE:    Time Seconds Interrupt Enable \n
-    *   TAIE:    Time Alarm Interrupt Enable \n
-    *   TOIE:    Time Overflow Interrupt Enable \n
-    *   TIIE:    Time Invalid Interrupt Enable \n
-    * \n
-    * For MCUs that have the Wakeup Pin only: \n
-    *   WPON:    Wakeup Pin On (see the corresponding MCU's reference manual)\n
-    * \n
-    * For MCUs that have the Monotonic Counter only: \n
-    *   MOIE:    Monotonic Overflow Interrupt Enable \n
-    */
-    rtc_hal_config_interrupts(&(configs->enableInterrupts));
-
-#if FSL_FEATURE_RTC_HAS_MONOTONIC
-    if(configs->monotonicCounterAt)
-    {
-        rtc_hal_set_monotonic_counter(configs->monotonicCounterAt);
-    }
 #endif
-}
 
 /*******************************************************************************
  * EOF