rtc.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "rtc.h"
  2. #include "Uart.h"
  3. void rtc_configuration(void)
  4. {
  5. rcu_periph_clock_enable(RCU_BKPI);/* enable PMU and BKPI clocks */
  6. rcu_periph_clock_enable(RCU_PMU);
  7. pmu_backup_write_enable();/* allow access to BKP domain */
  8. bkp_deinit();/* reset backup domain */
  9. rcu_osci_on(RCU_LXTAL);/* enable LXTAL */
  10. rcu_osci_stab_wait(RCU_LXTAL);/* wait till LXTAL is ready */
  11. rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);/* select RCU_LXTAL as RTC clock source */
  12. rcu_periph_clock_enable(RCU_RTC);/* enable RTC Clock */
  13. rtc_register_sync_wait();/* wait for RTC registers synchronization */
  14. rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
  15. //rtc_interrupt_enable(RTC_INT_SECOND);/* enable the RTC second interrupt*/
  16. rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
  17. rtc_prescaler_set(32767);/* set RTC prescaler: set RTC period to 1s */
  18. rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
  19. }
  20. void set_time(uint32_t time)
  21. {
  22. /* wait until last write operation on RTC registers has finished */
  23. rtc_lwoff_wait();
  24. /* change the current time */
  25. rtc_counter_set(time);
  26. /* wait until last write operation on RTC registers has finished */
  27. rtc_lwoff_wait();
  28. }
  29. void rct_init(void)
  30. {
  31. if (bkp_data_read(BKP_DATA_0) != 0xA5A5){
  32. printf("\r\nThis is a RTC demo!\r\n");
  33. printf("\r\n\n RTC not yet configured....");
  34. rtc_configuration();/* RTC configuration */
  35. printf("\r\n RTC configured....");
  36. /* adjust time by values entred by the user on the hyperterminal */
  37. set_time(1684302671);
  38. bkp_data_write(BKP_DATA_0, 0xA5A5);
  39. }else{
  40. if (rcu_flag_get(RCU_FLAG_PORRST) != RESET){/* check if the power on reset flag is set */
  41. printf("\r\n\n Power On Reset occurred....");
  42. }
  43. else if (rcu_flag_get(RCU_FLAG_SWRST) != RESET){
  44. printf("\r\n\n External Reset occurred....");/* check if the pin reset flag is set */
  45. }
  46. rcu_periph_clock_enable(RCU_PMU);/* allow access to BKP domain */
  47. pmu_backup_write_enable();
  48. printf("\r\n No need to configure RTC....");
  49. rtc_register_sync_wait();/* wait for RTC registers synchronization */
  50. rtc_lwoff_wait();/* wait until last write operation on RTC registers has finished */
  51. }
  52. //rcu_all_reset_flag_clear();/* clear reset flags */
  53. }