Esempio n. 1
0
/*FUNCTION**********************************************************************
 *
 * Function Name : RTC_DRV_SetDatetime
 * Description   : Sets the RTC date and time according to the given time struct.
 * This function will set the RTC date and time according to the given time
 * struct, if start_after_set is true, the RTC oscillator will be enabled and
 * the counter will start.
 *
 *END**************************************************************************/
bool RTC_DRV_SetDatetime(uint32_t instance, rtc_datetime_t *datetime)
{
    assert(datetime);
    RTC_Type *rtcBase = g_rtcBase[instance];
    uint32_t srcClock = 0;
    uint32_t seconds = 0;
    uint16_t preScaler = 0;
    uint64_t tmp = 0;

    /* Return error if the time provided is not valid */
    if (!(RTC_HAL_IsDatetimeCorrectFormat(datetime)))
    {
        return false;
    }

    RTC_HAL_ConvertDatetimeToSecs(datetime, &seconds);

    if ((srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U)
    {
        /* As the seconds register will not increment every second, we need to adjust the value
         * programmed to the seconds register */
        tmp = (uint64_t)seconds * (uint64_t)srcClock;
        preScaler = (uint32_t)(tmp & 0x7FFFU);
        seconds = (uint32_t)(tmp >> 15U);
    }
Esempio n. 2
0
void RtcInit( void )
{
    if ( RtcInitalized == false ) {
        rtc_datetime_t datetime;
        uint32_t srcClock = 0;
        uint32_t seconds = 0;
        uint16_t preScaler = 0;
        uint64_t tmp = 0;

        /* Enable clock gate to RTC module */
        CLOCK_SYS_EnableRtcClock(0U);

        /* Initialize the general configuration for RTC module.*/
        RTC_HAL_Init (RTC_BASE_PTR);

        /* Clear pending interrupts before enabling them */
        NVIC_ClearPendingIRQ (RTC_IRQn);
        INT_SYS_EnableIRQ(RTC_IRQn);

        /* Configure RTC external clock */
        CLOCK_SYS_RtcOscInit(0U, &rtcOscConfig);

        // Set a start date time and start RT.
        datetime.year = 2014U;
        datetime.month = 12U;
        datetime.day = 25U;
        datetime.hour = 19U;
        datetime.minute = 0;
        datetime.second = 0;

        RTC_HAL_ConvertDatetimeToSecs(&datetime, &seconds);

        if ( (srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U ) {
            /* As the seconds register will not increment every second, we need to adjust the value
             * programmed to the seconds register */
            tmp = (uint64_t) seconds * (uint64_t) srcClock;
            preScaler = (uint32_t)(tmp & 0x7FFFU);
            seconds = (uint32_t)(tmp >> 15U);
        }