main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. \file main.c
  3. \brief RCU clock out demo
  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. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #include "gd32f30x.h"
  34. #include "gd32f307c_eval.h"
  35. #include <stdio.h>
  36. typedef enum {
  37. CKOUT0_SYSTEMCLOCK_CKOUT = 0,
  38. CKOUT0_IRC8M_CKOUT = 1,
  39. CKOUT0_HXTAL_CKOUT = 2,
  40. } clock_output_enum;
  41. void rcu_config(void);
  42. void gpio_config(void);
  43. void all_led_init(void);
  44. void all_led_off(void);
  45. void led_flash(int times);
  46. void dac_config(void);
  47. void clock_output_select(uint8_t seq);
  48. void clock_output_config(void);
  49. extern uint8_t g_button_press_flag;
  50. /*!
  51. \brief main function
  52. \param[in] none
  53. \param[out] none
  54. \retval none
  55. */
  56. int main(void)
  57. {
  58. uint8_t func_seq = 0;
  59. /* initialize the USART */
  60. gd_eval_com_init(EVAL_COM0);
  61. printf("\r\n /=========== Gigadevice Clock output Demo ===========/ \r\n");
  62. printf("press tamper key to select clock output source \r\n");
  63. /* initialize the LEDs */
  64. all_led_init();
  65. /* initialize the clock output */
  66. clock_output_config();
  67. /* initialize the tamper key */
  68. gd_eval_key_init(KEY_TAMPER, KEY_MODE_EXTI);
  69. while(1) {
  70. if(1 == g_button_press_flag) {
  71. /* if the button is pressed */
  72. g_button_press_flag = 0;
  73. /* control the led */
  74. all_led_off();
  75. gd_eval_led_on((led_typedef_enum)func_seq);
  76. /* select the clock output mode */
  77. clock_output_select(func_seq);
  78. g_button_press_flag = 0;
  79. func_seq++;
  80. func_seq %= 3;
  81. }
  82. }
  83. }
  84. /*!
  85. \brief all LEDs turn off
  86. \param[in] none
  87. \param[out] none
  88. \retval none
  89. */
  90. void all_led_off(void)
  91. {
  92. gd_eval_led_off(LED2);
  93. gd_eval_led_off(LED3);
  94. gd_eval_led_off(LED4);
  95. }
  96. /*!
  97. \brief configure clock output function
  98. \param[in] none
  99. \param[out] none
  100. \retval none
  101. */
  102. void clock_output_config(void)
  103. {
  104. /* peripheral clock enable */
  105. rcu_periph_clock_enable(RCU_GPIOA);
  106. rcu_periph_clock_enable(RCU_GPIOC);
  107. rcu_osci_on(RCU_IRC8M);
  108. if(ERROR == rcu_osci_stab_wait(RCU_IRC8M)) {
  109. printf("RCU_IRC8M rcu_osci_stab_wait timeout! \r\n");
  110. while(1);
  111. }
  112. /* configure clock output pin */
  113. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
  114. }
  115. /*!
  116. \brief select type of clock output
  117. \param[in] none
  118. \param[out] none
  119. \retval none
  120. */
  121. void clock_output_select(uint8_t seq)
  122. {
  123. switch(seq) {
  124. case CKOUT0_SYSTEMCLOCK_CKOUT:
  125. rcu_ckout0_config(RCU_CKOUT0SRC_CKSYS);
  126. printf("CK_OUT0: system clock \r\n");
  127. break;
  128. case CKOUT0_IRC8M_CKOUT:
  129. rcu_ckout0_config(RCU_CKOUT0SRC_IRC8M);
  130. printf("CK_OUT0: IRC8M \r\n");
  131. break;
  132. case CKOUT0_HXTAL_CKOUT:
  133. rcu_ckout0_config(RCU_CKOUT0SRC_HXTAL);
  134. printf("CK_OUT0: HXTAL \r\n");
  135. break;
  136. default:
  137. printf("clock output select parameter error! \r\n");
  138. while(1);
  139. }
  140. }
  141. /*!
  142. \brief initialize the LEDs
  143. \param[in] none
  144. \param[out] none
  145. \retval none
  146. */
  147. void all_led_init(void)
  148. {
  149. gd_eval_led_init(LED2);
  150. gd_eval_led_init(LED3);
  151. gd_eval_led_init(LED4);
  152. }
  153. /* retarget the C library printf function to the USART */
  154. int fputc(int ch, FILE *f)
  155. {
  156. usart_data_transmit(EVAL_COM0, (uint8_t)ch);
  157. while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TC));
  158. return ch;
  159. }