main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*!
  2. \file main.c
  3. \brief deepsleep wakeup through RTC alarm interrupt
  4. \version 2021-12-30, V1.0.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2021, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f30x.h"
  30. #include "gd32f307c_eval.h"
  31. #include "systick.h"
  32. #include "main.h"
  33. void led_config(void);
  34. void nvic_configuration(void);
  35. void rtc_configuration(void);
  36. void fwdgt_configuration(void);
  37. /*!
  38. \brief main function
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. int main(void)
  44. {
  45. /* nvic configuration */
  46. nvic_configuration();
  47. /* LED configuration */
  48. led_config();
  49. /* systick configuration */
  50. systick_config();
  51. gd_eval_led_on(LED5);
  52. delay_1ms(2000U);
  53. gd_eval_led_off(LED5);
  54. /* RTC configuration */
  55. rtc_configuration();
  56. /* FWDGT configuration */
  57. fwdgt_configuration();
  58. while(1){
  59. delay_1ms(50U);
  60. /* PMU enters deepsleep mode */
  61. pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, PMU_LOWDRIVER_DISABLE, WFI_CMD);
  62. gd_eval_led_toggle(LED2);
  63. }
  64. }
  65. /*!
  66. \brief LED configuration
  67. \param[in] none
  68. \param[out] none
  69. \retval none
  70. */
  71. void led_config(void)
  72. {
  73. gd_eval_led_init(LED2);
  74. gd_eval_led_init(LED5);
  75. }
  76. /*!
  77. \brief configure the nested vectored interrupt controller
  78. \param[in] none
  79. \param[out] none
  80. \retval none
  81. */
  82. void nvic_configuration(void)
  83. {
  84. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  85. nvic_irq_enable(RTC_Alarm_IRQn, 2U, 1U);
  86. }
  87. /*!
  88. \brief configure the RTC
  89. \param[in] none
  90. \param[out] none
  91. \retval none
  92. */
  93. void rtc_configuration(void)
  94. {
  95. /* enable PMU and BKPI clocks */
  96. rcu_periph_clock_enable(RCU_BKPI);
  97. rcu_periph_clock_enable(RCU_PMU);
  98. /* allow access to backup domain */
  99. pmu_backup_write_enable();
  100. /* reset backup domain */
  101. bkp_deinit();
  102. /* enable IRC40K */
  103. rcu_osci_on(RCU_IRC40K);
  104. /* wait till IRC40K is ready */
  105. rcu_osci_stab_wait(RCU_IRC40K);
  106. /* select RCU_IRC40K as RTC clock source */
  107. rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  108. /* enable RTC Clock */
  109. rcu_periph_clock_enable(RCU_RTC);
  110. /* wait for RTC registers synchronization */
  111. rtc_register_sync_wait();
  112. /* wait until last write operation on RTC registers has finished */
  113. rtc_lwoff_wait();
  114. /* enable the RTC alarm interrupt */
  115. rtc_interrupt_enable(RTC_INT_ALARM);
  116. /* wait until last write operation on RTC registers has finished */
  117. rtc_lwoff_wait();
  118. /* set RTC prescaler: set RTC period to 1s */
  119. rtc_prescaler_set(40000);
  120. /* wait until last write operation on RTC registers has finished */
  121. rtc_lwoff_wait();
  122. rtc_counter_set(0U);
  123. /* wait until last write operation on RTC registers has finished */
  124. rtc_lwoff_wait();
  125. rtc_alarm_config(ALARM_TIME_INTERVAL);
  126. /* wait until last write operation on RTC registers has finished */
  127. rtc_lwoff_wait();
  128. /* EXTI configuration */
  129. exti_deinit();
  130. exti_init(EXTI_17,EXTI_INTERRUPT,EXTI_TRIG_RISING);
  131. rtc_flag_clear(RTC_FLAG_ALARM);
  132. exti_interrupt_flag_clear(EXTI_17);
  133. exti_interrupt_enable(EXTI_17);
  134. }
  135. /*!
  136. \brief configure the FWDGT
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void fwdgt_configuration(void)
  142. {
  143. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  144. fwdgt_config(1250, FWDGT_PSC_DIV256);
  145. while(FWDGT_STAT);
  146. /* after 8 seconds to generate a reset */
  147. fwdgt_enable();
  148. }