| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "rtc.h"
- #include "Uart.h"
- void rtc_configuration(void)
- {
- rcu_periph_clock_enable(RCU_BKPI);/* enable PMU and BKPI clocks */
- rcu_periph_clock_enable(RCU_PMU);
- pmu_backup_write_enable();/* allow access to BKP domain */
- bkp_deinit();/* reset backup domain */
- rcu_osci_on(RCU_LXTAL);/* enable LXTAL */
- rcu_osci_stab_wait(RCU_LXTAL);/* wait till LXTAL is ready */
- rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);/* select RCU_LXTAL as RTC clock source */
- rcu_periph_clock_enable(RCU_RTC);/* enable RTC Clock */
- rtc_register_sync_wait();/* wait for RTC registers synchronization */
- rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
- //rtc_interrupt_enable(RTC_INT_SECOND);/* enable the RTC second interrupt*/
- rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
- rtc_prescaler_set(32767);/* set RTC prescaler: set RTC period to 1s */
- rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
- }
- void set_time(uint32_t time)
- {
- /* wait until last write operation on RTC registers has finished */
- rtc_lwoff_wait();
- /* change the current time */
- rtc_counter_set(time);
- /* wait until last write operation on RTC registers has finished */
- rtc_lwoff_wait();
- }
- void rct_init(void)
- {
- if (bkp_data_read(BKP_DATA_0) != 0xA5A5){
- printf("\r\nThis is a RTC demo!\r\n");
- printf("\r\n\n RTC not yet configured....");
- rtc_configuration();/* RTC configuration */
- printf("\r\n RTC configured....");
- /* adjust time by values entred by the user on the hyperterminal */
- set_time(1684302671);
- bkp_data_write(BKP_DATA_0, 0xA5A5);
- }else{
- if (rcu_flag_get(RCU_FLAG_PORRST) != RESET){/* check if the power on reset flag is set */
- printf("\r\n\n Power On Reset occurred....");
- }
- else if (rcu_flag_get(RCU_FLAG_SWRST) != RESET){
- printf("\r\n\n External Reset occurred....");/* check if the pin reset flag is set */
- }
- rcu_periph_clock_enable(RCU_PMU);/* allow access to BKP domain */
- pmu_backup_write_enable();
- printf("\r\n No need to configure RTC....");
- rtc_register_sync_wait();/* wait for RTC registers synchronization */
- //rtc_interrupt_enable(RTC_INT_SECOND); /* enable the RTC second */
- rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
- }
- //rcu_all_reset_flag_clear();/* clear reset flags */
- }
|