1
0

gd32f303e_eval.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. \file gd32f303e_eval.c
  3. \brief firmware functions to manage leds, keys, COM ports
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-05-19, V1.0.0, demo for GD32F30x
  8. */
  9. #include <gd32f30x.h>
  10. #include "gd32f303e_eval.h"
  11. /* private variables */
  12. static uint32_t GPIO_PORT[LEDn] = {LED2_GPIO_PORT, LED3_GPIO_PORT,
  13. LED4_GPIO_PORT, LED5_GPIO_PORT};
  14. static uint32_t GPIO_PIN[LEDn] = {LED2_PIN, LED3_PIN, LED4_PIN, LED5_PIN};
  15. static rcu_periph_enum COM_CLK[COMn] = {EVAL_COM1_CLK, EVAL_COM2_CLK};
  16. static uint32_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN, EVAL_COM2_TX_PIN};
  17. static uint32_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN, EVAL_COM2_RX_PIN};
  18. static uint32_t COM_GPIO_PORT[COMn] = {EVAL_COM1_GPIO_PORT, EVAL_COM2_GPIO_PORT};
  19. static rcu_periph_enum COM_GPIO_CLK[COMn] = {EVAL_COM1_GPIO_CLK, EVAL_COM2_GPIO_CLK};
  20. static rcu_periph_enum GPIO_CLK[LEDn] = {LED2_GPIO_CLK, LED3_GPIO_CLK,
  21. LED4_GPIO_CLK, LED5_GPIO_CLK};
  22. static uint32_t KEY_PORT[KEYn] = {WAKEUP_KEY_GPIO_PORT,
  23. TAMPER_KEY_GPIO_PORT,
  24. USER_KEY1_GPIO_PORT,
  25. USER_KEY2_GPIO_PORT};
  26. static uint32_t KEY_PIN[KEYn] = {WAKEUP_KEY_PIN, TAMPER_KEY_PIN,USER_KEY1_PIN,USER_KEY2_PIN};
  27. static rcu_periph_enum KEY_CLK[KEYn] = {WAKEUP_KEY_GPIO_CLK,
  28. TAMPER_KEY_GPIO_CLK,
  29. USER_KEY1_GPIO_CLK,
  30. USER_KEY2_GPIO_CLK};
  31. static exti_line_enum KEY_EXTI_LINE[KEYn] = {WAKEUP_KEY_EXTI_LINE,
  32. TAMPER_KEY_EXTI_LINE,
  33. USER_KEY1_EXTI_LINE,
  34. USER_KEY2_EXTI_LINE};
  35. static uint8_t KEY_PORT_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PORT_SOURCE,
  36. TAMPER_KEY_EXTI_PORT_SOURCE,
  37. USER_KEY1_EXTI_PORT_SOURCE,
  38. USER_KEY2_EXTI_PORT_SOURCE};
  39. static uint8_t KEY_PIN_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PIN_SOURCE,
  40. TAMPER_KEY_EXTI_PIN_SOURCE,
  41. USER_KEY1_EXTI_PIN_SOURCE,
  42. USER_KEY2_EXTI_PIN_SOURCE};
  43. static uint8_t KEY_IRQn[KEYn] = {WAKEUP_KEY_EXTI_IRQn,
  44. TAMPER_KEY_EXTI_IRQn,
  45. USER_KEY1_EXTI_IRQn,
  46. USER_KEY2_EXTI_IRQn};
  47. /*!
  48. \brief configure led GPIO
  49. \param[in] lednum: specify the led to be configured
  50. \arg LED2
  51. \arg LED3
  52. \arg LED4
  53. \arg LED5
  54. \param[out] none
  55. \retval none
  56. */
  57. void gd_eval_led_init (led_typedef_enum lednum)
  58. {
  59. /* enable the led clock */
  60. rcu_periph_clock_enable(GPIO_CLK[lednum]);
  61. /* configure led GPIO port */
  62. gpio_init(GPIO_PORT[lednum], GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN[lednum]);
  63. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  64. }
  65. /*!
  66. \brief turn on selected led
  67. \param[in] lednum: specify the led to be turned on
  68. \arg LED2
  69. \arg LED3
  70. \arg LED4
  71. \arg LED5
  72. \param[out] none
  73. \retval none
  74. */
  75. void gd_eval_led_on(led_typedef_enum lednum)
  76. {
  77. GPIO_BOP(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  78. }
  79. /*!
  80. \brief turn off selected led
  81. \param[in] lednum: specify the led to be turned off
  82. \arg LED2
  83. \arg LED3
  84. \arg LED4
  85. \arg LED5
  86. \param[out] none
  87. \retval none
  88. */
  89. void gd_eval_led_off(led_typedef_enum lednum)
  90. {
  91. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  92. }
  93. /*!
  94. \brief toggle selected led
  95. \param[in] lednum: specify the led to be toggled
  96. \arg LED2
  97. \arg LED3
  98. \arg LED4
  99. \arg LED5
  100. \param[out] none
  101. \retval none
  102. */
  103. void gd_eval_led_toggle(led_typedef_enum lednum)
  104. {
  105. gpio_bit_write(GPIO_PORT[lednum], GPIO_PIN[lednum],
  106. (bit_status)(1-gpio_input_bit_get(GPIO_PORT[lednum], GPIO_PIN[lednum])));
  107. }
  108. /*!
  109. \brief configure key
  110. \param[in] key_num: specify the key to be configured
  111. \arg KEY_TAMPER: tamper key
  112. \arg KEY_WAKEUP: wakeup key
  113. \arg KEY_USER1: user key1
  114. \arg KEY_USER2: user key2
  115. \param[in] key_mode: specify button mode
  116. \arg KEY_MODE_GPIO: key will be used as simple IO
  117. \arg KEY_MODE_EXTI: key will be connected to EXTI line with interrupt
  118. \param[out] none
  119. \retval none
  120. */
  121. void gd_eval_key_init(key_typedef_enum key_num, keymode_typedef_enum key_mode)
  122. {
  123. /* enable the key clock */
  124. rcu_periph_clock_enable(KEY_CLK[key_num]);
  125. rcu_periph_clock_enable(RCU_AF);
  126. /* configure button pin as input */
  127. gpio_init(KEY_PORT[key_num], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, KEY_PIN[key_num]);
  128. if (key_mode == KEY_MODE_EXTI) {
  129. /* enable and set key EXTI interrupt to the lowest priority */
  130. nvic_irq_enable(KEY_IRQn[key_num], 2U, 0U);
  131. /* connect key EXTI line to key GPIO pin */
  132. gpio_exti_source_select(KEY_PORT_SOURCE[key_num], KEY_PIN_SOURCE[key_num]);
  133. /* configure key EXTI line */
  134. exti_init(KEY_EXTI_LINE[key_num], EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  135. exti_interrupt_flag_clear(KEY_EXTI_LINE[key_num]);
  136. }
  137. }
  138. /*!
  139. \brief return the selected key state
  140. \param[in] key: specify the key to be checked
  141. \arg KEY_TAMPER: tamper key
  142. \arg KEY_WAKEUP: wakeup key
  143. \arg KEY_USER1: user key
  144. \arg KEY_USER2: user key2
  145. \param[out] none
  146. \retval the key's GPIO pin value
  147. */
  148. uint8_t gd_eval_key_state_get(key_typedef_enum key)
  149. {
  150. return gpio_input_bit_get(KEY_PORT[key], KEY_PIN[key]);
  151. }
  152. /*!
  153. \brief configure COM port
  154. \param[in] com: COM on the board
  155. \arg EVAL_COM1: COM1 on the board
  156. \arg EVAL_COM2: COM2 on the board
  157. \param[out] none
  158. \retval none
  159. */
  160. void gd_eval_com_init(uint32_t com)
  161. {
  162. uint32_t com_id = 0U;
  163. if(EVAL_COM1 == com){
  164. com_id = 0U;
  165. }else if(EVAL_COM2 == com){
  166. com_id = 1U;
  167. }
  168. /* enable GPIO clock */
  169. rcu_periph_clock_enable(COM_GPIO_CLK[com_id]);
  170. /* enable USART clock */
  171. rcu_periph_clock_enable(COM_CLK[com_id]);
  172. /* connect port to USARTx_Tx */
  173. gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_TX_PIN[com_id]);
  174. /* connect port to USARTx_Rx */
  175. gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_RX_PIN[com_id]);
  176. /* USART configure */
  177. usart_deinit(com);
  178. usart_baudrate_set(com, 115200U);
  179. usart_receive_config(com, USART_RECEIVE_ENABLE);
  180. usart_transmit_config(com, USART_TRANSMIT_ENABLE);
  181. usart_enable(com);
  182. }