Example #1
0
static int setup_rtc(void)
{
	/* turn on power block to enable unlocking */
	rcc_periph_clock_enable(RCC_PWR);
	pwr_disable_backup_domain_write_protect();

	/* reset rtc */
	RCC_CSR |= RCC_CSR_RTCRST;
	RCC_CSR &= ~RCC_CSR_RTCRST;

	/* We want to use the LSE fitted on the discovery board */
	rcc_osc_on(RCC_LSE);
	rcc_wait_for_osc_ready(RCC_LSE);

	/* Select the LSE as rtc clock */
	rcc_rtc_select_clock(RCC_CSR_RTCSEL_LSE);

	/* ?! Stdperiph examples don't turn this on until _afterwards_ which
	 * simply doesn't work.  It must be on at least to be able to
	 * configure it */
	RCC_CSR |= RCC_CSR_RTCEN;

	rtc_unlock();

	/* enter init mode */
	RTC_ISR |= RTC_ISR_INIT;
	while ((RTC_ISR & RTC_ISR_INITF) == 0);

	/* set synch prescaler, using defaults for 1Hz out */
	uint32_t sync = 255;
	uint32_t async = 127;
	rtc_set_prescaler(sync, async);

	/* load time and date here if desired, and hour format */

	/* exit init mode */
	RTC_ISR &= ~(RTC_ISR_INIT);

	/* and write protect again */
	rtc_lock();

	/* and finally enable the clock */
	RCC_CSR |= RCC_CSR_RTCEN;

	/* And wait for synchro.. */
	rtc_wait_for_synchro();
	return 0;
}
Example #2
0
void plc_rtc_init( tm* time )
{
    uint32_t tmp=0;
    uint32_t year;
    uint32_t i;

    rcc_periph_clock_enable( RCC_PWR );

    pwr_disable_backup_domain_write_protect();

    PLC_BKP_RTC_IS_OK = 0;

    /* LSE oscillator clock used as the RTC clock */
    RCC_BDCR |= 0x00000100;
    RCC_BDCR |= RCC_BDCR_LSEON;

    rcc_periph_clock_enable( RCC_RTC );

    rtc_unlock();

    RTC_ISR |= RTC_ISR_INIT;
    for(i=0; i<1000000; i++)
    {
        if( RTC_ISR & RTC_ISR_INITF )
        {
            break;
        }
    }

    if( !(RTC_ISR & RTC_ISR_INITF) )
    {
        plc_hw_status |= PLC_HW_ERR_LSE;

        RTC_ISR &= ~RTC_ISR_INIT;
        pwr_enable_backup_domain_write_protect();
        return;
    }

    rtc_set_prescaler( 0x1FF, 0x3F );

    tmp  =  (unsigned long)(time->tm_sec%10 );
    tmp |= ((unsigned long)(time->tm_sec/10 )) <<  4;
    tmp |= ((unsigned long)(time->tm_min%10 )) <<  8;
    tmp |= ((unsigned long)(time->tm_min/10 )) << 12;
    tmp |= ((unsigned long)(time->tm_hour%10)) << 16;
    tmp |= ((unsigned long)(time->tm_hour/10)) << 20;
    RTC_TR = tmp;

    /* Only 2 digits used!!! *time may be const, so use year var. */
    year = time->tm_year % 100;

    tmp  =  (unsigned long)(time->tm_day%10 );
    tmp |= ((unsigned long)(time->tm_day/10 )) <<  4;
    tmp |= ((unsigned long)(time->tm_mon%10 )) <<  8;
    tmp |= ((unsigned long)(time->tm_mon/10 )) << 12;
    tmp |= ((unsigned long)(year%10)) << 16;
    tmp |= ((unsigned long)(year/10)) << 20;
    RTC_DR = tmp;

    /* exit from initialization mode */
    RTC_ISR &= ~RTC_ISR_INIT;

    PLC_BKP_RTC_IS_OK = 1;

    pwr_enable_backup_domain_write_protect();
}