rtc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*!
  2. \file rtc.c
  3. \brief RTC check and config,time_show and time_adjust function
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "rtc.h"
  33. /* enter the second interruption,set the second interrupt flag to 1 */
  34. __IO uint32_t timedisplay;
  35. /*!
  36. \brief configure the nested vectored interrupt controller
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void nvic_configuration(void)
  42. {
  43. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  44. nvic_irq_enable(RTC_IRQn,1,0);
  45. }
  46. /*!
  47. \brief configure the RTC
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. void rtc_configuration(void)
  53. {
  54. /* enable PMU and BKPI clocks */
  55. rcu_periph_clock_enable(RCU_BKPI);
  56. rcu_periph_clock_enable(RCU_PMU);
  57. /* allow access to BKP domain */
  58. pmu_backup_write_enable();
  59. /* reset backup domain */
  60. bkp_deinit();
  61. /* enable LXTAL */
  62. rcu_osci_on(RCU_LXTAL);
  63. /* wait till LXTAL is ready */
  64. rcu_osci_stab_wait(RCU_LXTAL);
  65. /* select RCU_LXTAL as RTC clock source */
  66. rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
  67. /* enable RTC Clock */
  68. rcu_periph_clock_enable(RCU_RTC);
  69. /* wait for RTC registers synchronization */
  70. rtc_register_sync_wait();
  71. /* wait until last write operation on RTC registers has finished */
  72. rtc_lwoff_wait();
  73. /* enable the RTC second interrupt*/
  74. rtc_interrupt_enable(RTC_INT_SECOND);
  75. /* wait until last write operation on RTC registers has finished */
  76. rtc_lwoff_wait();
  77. /* set RTC prescaler: set RTC period to 1s */
  78. rtc_prescaler_set(32767);
  79. /* wait until last write operation on RTC registers has finished */
  80. rtc_lwoff_wait();
  81. }
  82. /*!
  83. \brief return the time entered by user, using Hyperterminal
  84. \param[in] none
  85. \param[out] none
  86. \retval current time of RTC counter value
  87. */
  88. uint32_t time_regulate(void)
  89. {
  90. uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
  91. printf("\r\n==============Time Settings=====================================");
  92. printf("\r\n Please Set Hours");
  93. while (tmp_hh == 0xFF){
  94. tmp_hh = usart_scanf(23);
  95. }
  96. printf(": %d", tmp_hh);
  97. printf("\r\n Please Set Minutes");
  98. while (tmp_mm == 0xFF){
  99. tmp_mm = usart_scanf(59);
  100. }
  101. printf(": %d", tmp_mm);
  102. printf("\r\n Please Set Seconds");
  103. while (tmp_ss == 0xFF){
  104. tmp_ss = usart_scanf(59);
  105. }
  106. printf(": %d", tmp_ss);
  107. /* return the value store in RTC counter register */
  108. return((tmp_hh*3600 + tmp_mm*60 + tmp_ss));
  109. }
  110. /*!
  111. \brief adjust time
  112. \param[in] none
  113. \param[out] none
  114. \retval none
  115. */
  116. void time_adjust(void)
  117. {
  118. /* wait until last write operation on RTC registers has finished */
  119. rtc_lwoff_wait();
  120. /* change the current time */
  121. rtc_counter_set(time_regulate());
  122. /* wait until last write operation on RTC registers has finished */
  123. rtc_lwoff_wait();
  124. }
  125. /*!
  126. \brief display the current time
  127. \param[in] timeVar: RTC counter value
  128. \param[out] none
  129. \retval none
  130. */
  131. void time_display(uint32_t timevar)
  132. {
  133. uint32_t thh = 0, tmm = 0, tss = 0;
  134. /* compute hours */
  135. thh = timevar / 3600;
  136. /* compute minutes */
  137. tmm = (timevar % 3600) / 60;
  138. /* compute seconds */
  139. tss = (timevar % 3600) % 60;
  140. printf(" Time: %0.2d:%0.2d:%0.2d\r\n", thh, tmm, tss);
  141. }
  142. /*!
  143. \brief show the current time (HH:MM:SS) on the Hyperterminal
  144. \param[in] none
  145. \param[out] none
  146. \retval none
  147. */
  148. void time_show(void)
  149. {
  150. printf("\n\r");
  151. /* infinite loop */
  152. while (1){
  153. /* if 1s has paased */
  154. if (timedisplay == 1){
  155. /* display current time */
  156. time_display(rtc_counter_get());
  157. timedisplay = 0;
  158. }
  159. }
  160. }
  161. /*!
  162. \brief get numeric values from the hyperterminal
  163. \param[in] value: input value from the hyperterminal
  164. \param[out] none
  165. \retval input value in BCD mode
  166. */
  167. uint8_t usart_scanf(uint32_t value)
  168. {
  169. uint32_t index = 0;
  170. uint32_t tmp[2] = {0, 0};
  171. while (index < 2){
  172. /* loop until RBNE = 1 */
  173. while (usart_flag_get(USART0, USART_FLAG_RBNE) == RESET);
  174. tmp[index++] = (usart_data_receive(USART0));
  175. if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){
  176. printf("\n\rPlease enter valid number between 0 and 9\n");
  177. index--;
  178. }
  179. }
  180. /* calculate the Corresponding value */
  181. index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
  182. /* check */
  183. if (index > value){
  184. printf("\n\rPlease enter valid number between 0 and %d\n", value);
  185. return 0xFF;
  186. }
  187. return index;
  188. }