stm32f1_rtc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * File : stm32f1_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-01-05 Bernard the first version.
  13. * 2011-11-26 aozima implementation time.
  14. * 2015-07-16 FlyM rename rtc to stm32f1_rtc. remove finsh export function
  15. */
  16. #include <rtthread.h>
  17. #include <stm32f10x.h>
  18. #include "stm32f1_rtc.h"
  19. static struct rt_device rtc;
  20. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  21. {
  22. if (dev->rx_indicate != RT_NULL)
  23. {
  24. /* Open Interrupt */
  25. }
  26. return RT_EOK;
  27. }
  28. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  29. {
  30. return 0;
  31. }
  32. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  33. {
  34. rt_time_t *time;
  35. RT_ASSERT(dev != RT_NULL);
  36. switch (cmd)
  37. {
  38. case RT_DEVICE_CTRL_RTC_GET_TIME:
  39. time = (rt_time_t *)args;
  40. /* read device */
  41. *time = RTC_GetCounter();
  42. break;
  43. case RT_DEVICE_CTRL_RTC_SET_TIME:
  44. {
  45. time = (rt_time_t *)args;
  46. /* Enable PWR and BKP clocks */
  47. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  48. /* Allow access to BKP Domain */
  49. PWR_BackupAccessCmd(ENABLE);
  50. /* Wait until last write operation on RTC registers has finished */
  51. RTC_WaitForLastTask();
  52. /* Change the current time */
  53. RTC_SetCounter(*time);
  54. /* Wait until last write operation on RTC registers has finished */
  55. RTC_WaitForLastTask();
  56. BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  57. }
  58. break;
  59. }
  60. return RT_EOK;
  61. }
  62. /*******************************************************************************
  63. * Function Name : RTC_Configuration
  64. * Description : Configures the RTC.
  65. * Input : None
  66. * Output : None
  67. * Return : 0 reday,-1 error.
  68. *******************************************************************************/
  69. int RTC_Configuration(void)
  70. {
  71. u32 count=0x200000;
  72. /* Enable PWR and BKP clocks */
  73. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  74. /* Allow access to BKP Domain */
  75. PWR_BackupAccessCmd(ENABLE);
  76. /* Reset Backup Domain */
  77. BKP_DeInit();
  78. /* Enable LSE */
  79. RCC_LSEConfig(RCC_LSE_ON);
  80. /* Wait till LSE is ready */
  81. while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
  82. if ( count == 0 )
  83. {
  84. return -1;
  85. }
  86. /* Select LSE as RTC Clock Source */
  87. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  88. /* Enable RTC Clock */
  89. RCC_RTCCLKCmd(ENABLE);
  90. /* Wait for RTC registers synchronization */
  91. RTC_WaitForSynchro();
  92. /* Wait until last write operation on RTC registers has finished */
  93. RTC_WaitForLastTask();
  94. /* Set RTC prescaler: set RTC period to 1sec */
  95. RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  96. /* Wait until last write operation on RTC registers has finished */
  97. RTC_WaitForLastTask();
  98. return 0;
  99. }
  100. void rt_hw_rtc_init(void)
  101. {
  102. rtc.type = RT_Device_Class_RTC;
  103. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  104. {
  105. rt_kprintf("rtc is not configured\n");
  106. rt_kprintf("please configure with set_date and set_time\n");
  107. if ( RTC_Configuration() != 0)
  108. {
  109. rt_kprintf("rtc configure fail...\r\n");
  110. return ;
  111. }
  112. }
  113. else
  114. {
  115. /* Wait for RTC registers synchronization */
  116. RTC_WaitForSynchro();
  117. }
  118. /* register rtc device */
  119. rtc.init = RT_NULL;
  120. rtc.open = rt_rtc_open;
  121. rtc.close = RT_NULL;
  122. rtc.read = rt_rtc_read;
  123. rtc.write = RT_NULL;
  124. rtc.control = rt_rtc_control;
  125. /* no private */
  126. rtc.user_data = RT_NULL;
  127. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  128. return;
  129. }