main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. \file main.c
  3. \brief Deepsleep wakeup
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "gd32f3x0_it.h"
  11. #include "gd32f3x0_eval.h"
  12. extern __IO uint8_t counter0;
  13. static void system_clock_reconfig(void);
  14. void delay_s(uint32_t nTime);
  15. /*!
  16. \brief main function
  17. \param[in] none
  18. \param[out] none
  19. \retval none
  20. */
  21. int main(void)
  22. {
  23. SysTick_Config((SystemCoreClock / 1000));
  24. gd_eval_led_init(LED2);
  25. /* USART configuration the CK_IRC8M as USART clock */
  26. rcu_usart_clock_config(RCU_USART0SRC_IRC8M);
  27. gd_eval_com_init(EVAL_COM1);
  28. nvic_irq_enable(USART0_IRQn, 0, 0);
  29. delay_s(20);
  30. {
  31. /* use start bit wakeup mcu */
  32. usart_wakeup_mode_config(EVAL_COM1, USART_WUM_STARTB);
  33. /* enable USART */
  34. usart_enable(EVAL_COM1);
  35. /* ensure USART is enabled */
  36. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_REA));
  37. /* check USART is not transmitting */
  38. while(SET == usart_flag_get(EVAL_COM1, USART_FLAG_BSY));
  39. usart_wakeup_enable(EVAL_COM1);
  40. /* enable the WUIE interrupt */
  41. usart_interrupt_enable(EVAL_COM1, USART_INT_WU);
  42. /* enable PWU APB clock */
  43. rcu_periph_clock_enable(RCU_PMU);
  44. /* enter deep-sleep mode */
  45. pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
  46. /* wait a WUIE interrup event */
  47. while(0x00 == counter0);
  48. /* disable USART peripheral in deepsleep mode */
  49. usart_wakeup_disable(EVAL_COM1);
  50. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_RBNE));
  51. usart_data_receive(EVAL_COM1);
  52. usart_receive_config(EVAL_COM1,USART_RECEIVE_ENABLE);
  53. while (RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC));
  54. /* disable the USART */
  55. usart_disable(EVAL_COM1);
  56. }
  57. /* reconfigure systemclock */
  58. system_clock_reconfig();
  59. /* configure and enable the systick timer to generate an interrupt each 1 ms */
  60. SysTick_Config((SystemCoreClock / 1000));
  61. while (1);
  62. }
  63. /*!
  64. \brief delay function
  65. \param[in] nTime
  66. \param[out] none
  67. \retval none
  68. */
  69. void delay_s(uint32_t nTime)
  70. {
  71. uint32_t TimingDelay = 10800000*nTime;
  72. while(TimingDelay != 0)
  73. TimingDelay--;
  74. }
  75. /*!
  76. \brief restore peripheral config before entering deepsleep mode
  77. \param[in] none
  78. \param[out] none
  79. \retval none
  80. */
  81. static void system_clock_reconfig(void)
  82. {
  83. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  84. /* enable HXTAL */
  85. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  86. HSEStatus = rcu_osci_stab_wait(RCU_HXTAL);
  87. if (SUCCESS == HSEStatus){
  88. /* configure AHB */
  89. rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1);
  90. /* configure APB1, APB2 */
  91. rcu_apb1_clock_config(RCU_APB1_CKAHB_DIV1);
  92. rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1);
  93. /* PLL configuration: = HXTAL/2 * 27 = 108 MHz */
  94. rcu_hxtal_prediv_config(RCU_PLL_PREDV2);
  95. rcu_pll_config(RCU_PLLSRC_HXTAL_IRC48M, RCU_PLL_MUL27);
  96. /* enable PLL */
  97. RCU_CTL0 |= RCU_CTL0_PLLEN;
  98. /* select PLL as system clock */
  99. RCU_CFG0 &= ~RCU_CFG0_SCS;
  100. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  101. }
  102. }
  103. /*!
  104. \brief LED spark
  105. \param[in] none
  106. \param[out] none
  107. \retval none
  108. */
  109. void LED_Spark(void)
  110. {
  111. static __IO uint32_t TimingDelayLocal = 0;
  112. if (0x00 != TimingDelayLocal){
  113. if(TimingDelayLocal < 50){
  114. /* light on */
  115. gd_eval_led_on(LED2);
  116. }else{
  117. /* light off */
  118. gd_eval_led_off(LED2);
  119. }
  120. TimingDelayLocal--;
  121. }else{
  122. TimingDelayLocal = 100;
  123. }
  124. }