main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*!
  2. \file main.c
  3. \brief standby 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 rtc_configuration(void);
  35. /*!
  36. \brief main function
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. int main(void)
  42. {
  43. systick_config();
  44. /* LED configuration and turn on all LEDs */
  45. led_config();
  46. gd_eval_led_on(LED2);
  47. gd_eval_led_on(LED3);
  48. gd_eval_led_on(LED4);
  49. gd_eval_led_on(LED5);
  50. /* delay 2s */
  51. delay_1ms(2000);
  52. /* enable PMU clock */
  53. rcu_periph_clock_enable(RCU_PMU);
  54. /* configure RTC */
  55. rtc_configuration();
  56. /* PMU enters standby mode */
  57. pmu_to_standbymode();
  58. while(1) {
  59. }
  60. }
  61. /*!
  62. \brief configure LEDs
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void led_config(void)
  68. {
  69. gd_eval_led_init(LED2);
  70. gd_eval_led_init(LED3);
  71. gd_eval_led_init(LED4);
  72. gd_eval_led_init(LED5);
  73. }
  74. /*!
  75. \brief configure the RTC
  76. \param[in] none
  77. \param[out] none
  78. \retval none
  79. */
  80. void rtc_configuration(void)
  81. {
  82. /* enable BKPI clock */
  83. rcu_periph_clock_enable(RCU_BKPI);
  84. /* allow access to backup domain */
  85. pmu_backup_write_enable();
  86. /* reset backup domain */
  87. bkp_deinit();
  88. /* enable IRC40K */
  89. rcu_osci_on(RCU_IRC40K);
  90. /* wait till IRC40K is ready */
  91. rcu_osci_stab_wait(RCU_IRC40K);
  92. /* select RCU_IRC40K as RTC clock source */
  93. rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  94. /* enable RTC clock */
  95. rcu_periph_clock_enable(RCU_RTC);
  96. /* wait for RTC registers synchronization */
  97. rtc_register_sync_wait();
  98. /* wait until last write operation on RTC registers has finished */
  99. rtc_lwoff_wait();
  100. /* enable the RTC alarm interrupt */
  101. rtc_interrupt_enable(RTC_INT_ALARM);
  102. /* wait until last write operation on RTC registers has finished */
  103. rtc_lwoff_wait();
  104. /* set RTC prescaler: set RTC period to 1s */
  105. rtc_prescaler_set(40000);
  106. /* wait until last write operation on RTC registers has finished */
  107. rtc_lwoff_wait();
  108. rtc_counter_set(0U);
  109. /* wait until last write operation on RTC registers has finished */
  110. rtc_lwoff_wait();
  111. rtc_alarm_config(ALARM_TIME_INTERVAL);
  112. /* wait until last write operation on RTC registers has finished */
  113. rtc_lwoff_wait();
  114. /* clear the RTC alarm flag */
  115. rtc_flag_clear(RTC_FLAG_ALARM);
  116. }