main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. \file main.c
  3. \brief RTC timestamp demo
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. #define RTC_CLOCK_SOURCE_IRC40K
  13. #define BKP_VALUE 0x32F0
  14. rtc_timestamp_struct rtc_timestamp;
  15. rtc_parameter_struct rtc_initpara;
  16. __IO uint32_t prescaler_a = 0, prescaler_s = 0;
  17. void rtc_setup(void);
  18. void rtc_show_time(void);
  19. void rtc_show_timestamp(void);
  20. uint8_t usart_input_threshold(uint32_t value);
  21. void rtc_pre_config(void);
  22. /*!
  23. \brief main function
  24. \param[in] none
  25. \param[out] none
  26. \retval none
  27. */
  28. int main(void)
  29. {
  30. gd_eval_com_init(EVAL_COM1);
  31. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  32. printf("\n\r ****************** RTC time-stamp demo ******************\n\r");
  33. gd_eval_key_init(KEY_TAMPER,KEY_MODE_GPIO);
  34. /* enable access to RTC registers in Backup domain */
  35. rcu_periph_clock_enable(RCU_PMU);
  36. pmu_backup_write_enable();
  37. rtc_pre_config();
  38. /* check if RTC has aready been configured */
  39. if (BKP_VALUE != RTC_BKP0){
  40. rtc_setup();
  41. }else{
  42. /* detect the reset source */
  43. if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){
  44. printf("power on reset occurred....\n\r");
  45. }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){
  46. printf("external reset occurred....\n\r");
  47. }
  48. printf("no need to configure RTC....\n\r");
  49. rtc_show_time();
  50. }
  51. rcu_all_reset_flag_clear();
  52. gd_eval_led_init(LED1);
  53. gd_eval_led_on(LED1);
  54. exti_flag_clear(EXTI_19);
  55. exti_init(EXTI_19,EXTI_INTERRUPT,EXTI_TRIG_RISING);
  56. nvic_irq_enable(RTC_IRQn,0,0);
  57. /* RTC timestamp configuration */
  58. rtc_timestamp_enable(RTC_TIMESTAMP_FALLING_EDGE);
  59. rtc_interrupt_enable(RTC_INT_TIMESTAMP);
  60. rtc_flag_clear(RTC_STAT_TSF|RTC_STAT_TSOVRF);
  61. while (1);
  62. }
  63. /*!
  64. \brief RTC configuration function
  65. \param[in] none
  66. \param[out] none
  67. \retval none
  68. */
  69. void rtc_pre_config(void)
  70. {
  71. #if defined (RTC_CLOCK_SOURCE_IRC40K)
  72. rcu_osci_on(RCU_IRC40K);
  73. rcu_osci_stab_wait(RCU_IRC40K);
  74. rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  75. prescaler_s = 0x18F;
  76. prescaler_a = 0x63;
  77. #elif defined (RTC_CLOCK_SOURCE_LXTAL)
  78. rcu_osci_on(RCU_LXTAL);
  79. rcu_osci_stab_wait(RCU_LXTAL);
  80. rcu_rtc_clock_config(RCU_RTC_LXTAL);
  81. prescaler_s = 0xFF;
  82. prescaler_a = 0x7F;
  83. #else
  84. #error RTC clock source should be defined.
  85. #endif /* RTC_CLOCK_SOURCE_IRC40K */
  86. rcu_periph_clock_enable(RCU_RTC);
  87. rtc_register_sync_wait();
  88. }
  89. /*!
  90. \brief use hyperterminal to setup RTC time and alarm
  91. \param[in] none
  92. \param[out] none
  93. \retval none
  94. */
  95. void rtc_setup(void)
  96. {
  97. /* setup RTC time value */
  98. uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
  99. rtc_initpara.rtc_factor_asyn = prescaler_a;
  100. rtc_initpara.rtc_factor_syn = prescaler_s;
  101. rtc_initpara.rtc_year = 0x16;
  102. rtc_initpara.rtc_day_of_week = RTC_SATURDAY;
  103. rtc_initpara.rtc_month = RTC_APR;
  104. rtc_initpara.rtc_date = 0x30;
  105. rtc_initpara.rtc_display_format = RTC_24HOUR;
  106. rtc_initpara.rtc_am_pm = RTC_AM;
  107. /* current time input */
  108. printf("=======Configure RTC Time========\n\r");
  109. printf(" please input hour:\n\r");
  110. while (tmp_hh == 0xFF){
  111. tmp_hh = usart_input_threshold(23);
  112. rtc_initpara.rtc_hour = tmp_hh;
  113. }
  114. printf(" %0.2x\n\r", tmp_hh);
  115. printf(" please input minute:\n\r");
  116. while (tmp_mm == 0xFF){
  117. tmp_mm = usart_input_threshold(59);
  118. rtc_initpara.rtc_minute = tmp_mm;
  119. }
  120. printf(" %0.2x\n\r", tmp_mm);
  121. printf(" please input second:\n\r");
  122. while (tmp_ss == 0xFF){
  123. tmp_ss = usart_input_threshold(59);
  124. rtc_initpara.rtc_second = tmp_ss;
  125. }
  126. printf(" %0.2x\n\r", tmp_ss);
  127. /* RTC current time configuration */
  128. if(ERROR == rtc_init(&rtc_initpara)){
  129. printf("\n\r** RTC time configuration failed! **\n\r");
  130. }else{
  131. printf("\n\r** RTC time configuration success! **\n\r");
  132. rtc_show_time();
  133. RTC_BKP0 = BKP_VALUE;
  134. }
  135. }
  136. /*!
  137. \brief display the timestamp time
  138. \param[in] none
  139. \param[out] none
  140. \retval none
  141. */
  142. void rtc_show_timestamp(void)
  143. {
  144. uint32_t ts_subsecond = 0;
  145. uint8_t ts_subsecond_ss,ts_subsecond_ts,ts_subsecond_hs ;
  146. rtc_timestamp_get(&rtc_timestamp);
  147. /* get the subsecond value of timestamp time, and convert it into fractional format */
  148. ts_subsecond = rtc_timestamp_subsecond_get();
  149. ts_subsecond_ss=(1000-(ts_subsecond*1000+1000)/400)/100;
  150. ts_subsecond_ts=(1000-(ts_subsecond*1000+1000)/400)%100/10;
  151. ts_subsecond_hs=(1000-(ts_subsecond*1000+1000)/400)%10;
  152. printf("Get the time-stamp time: %0.2x:%0.2x:%0.2x .%d%d%d \n\r", \
  153. rtc_timestamp.rtc_timestamp_hour, rtc_timestamp.rtc_timestamp_minute, rtc_timestamp.rtc_timestamp_second,\
  154. ts_subsecond_ss, ts_subsecond_ts, ts_subsecond_hs);
  155. }
  156. /*!
  157. \brief display the current time
  158. \param[in] none
  159. \param[out] none
  160. \retval none
  161. */
  162. void rtc_show_time(void)
  163. {
  164. uint32_t time_subsecond = 0;
  165. uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0;
  166. rtc_current_time_get(&rtc_initpara);
  167. /* get the subsecond value of current time, and convert it into fractional format */
  168. time_subsecond = rtc_subsecond_get();
  169. subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100;
  170. subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10;
  171. subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10;
  172. printf("Current time: %0.2x:%0.2x:%0.2x .%d%d%d \n\r", \
  173. rtc_initpara.rtc_hour, rtc_initpara.rtc_minute, rtc_initpara.rtc_second,\
  174. subsecond_ss, subsecond_ts, subsecond_hs);
  175. }
  176. /*!
  177. \brief get the input character string and check if it is valid
  178. \param[in] none
  179. \param[out] none
  180. \retval input value in BCD mode
  181. */
  182. uint8_t usart_input_threshold(uint32_t value)
  183. {
  184. uint32_t index = 0;
  185. uint32_t tmp[2] = {0, 0};
  186. while (index < 2){
  187. while (RESET == usart_flag_get(EVAL_COM1, USART_FLAG_RBNE));
  188. tmp[index++] = usart_data_receive(EVAL_COM1);
  189. if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){
  190. printf("\n\r please input a valid number between 0 and 9 \n\r");
  191. index--;
  192. }
  193. }
  194. index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
  195. if (index > value){
  196. printf("\n\r please input a valid number between 0 and %d \n\r", value);
  197. return 0xFF;
  198. }
  199. index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4);
  200. return index;
  201. }
  202. /* retarget the C library printf function to the USART */
  203. int fputc(int ch, FILE *f)
  204. {
  205. usart_data_transmit(EVAL_COM1, (uint8_t) ch);
  206. while (RESET == usart_flag_get(EVAL_COM1,USART_FLAG_TC));
  207. return ch;
  208. }