gd32f3x0_eval.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*!
  2. \file gd32f3x0_eval.c
  3. \brief firmware functions to manage leds, keys, COM ports
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_eval.h"
  10. /* private variables */
  11. static uint32_t GPIO_PORT[LEDn] = {LED1_GPIO_PORT,
  12. LED2_GPIO_PORT,
  13. LED3_GPIO_PORT,
  14. LED4_GPIO_PORT};
  15. static uint32_t GPIO_PIN[LEDn] = {LED1_PIN,
  16. LED2_PIN,
  17. LED3_PIN,
  18. LED4_PIN};
  19. static rcu_periph_enum COM_CLK[COMn] = {EVAL_COM1_CLK};
  20. static uint32_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN};
  21. static uint32_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN};
  22. static rcu_periph_enum GPIO_CLK[LEDn] = {LED1_GPIO_CLK,
  23. LED2_GPIO_CLK,
  24. LED3_GPIO_CLK,
  25. LED4_GPIO_CLK};
  26. static uint32_t KEY_PORT[KEYn] = {WAKEUP_KEY_GPIO_PORT,
  27. TAMPER_KEY_GPIO_PORT,
  28. USER_KEY_GPIO_PORT};
  29. static uint32_t KEY_PIN[KEYn] = {WAKEUP_KEY_PIN,
  30. TAMPER_KEY_PIN,
  31. USER_KEY_PIN};
  32. static rcu_periph_enum KEY_CLK[KEYn] = {WAKEUP_KEY_GPIO_CLK,
  33. TAMPER_KEY_GPIO_CLK,
  34. USER_KEY_GPIO_CLK};
  35. static exti_line_enum KEY_EXTI_LINE[KEYn] = {WAKEUP_KEY_EXTI_LINE,
  36. TAMPER_KEY_EXTI_LINE,
  37. USER_KEY_EXTI_LINE};
  38. static uint8_t KEY_PORT_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PORT_SOURCE,
  39. TAMPER_KEY_EXTI_PORT_SOURCE,
  40. USER_KEY_EXTI_PORT_SOURCE};
  41. static uint8_t KEY_PIN_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PIN_SOURCE,
  42. TAMPER_KEY_EXTI_PIN_SOURCE,
  43. USER_KEY_EXTI_PIN_SOURCE};
  44. static uint8_t KEY_IRQn[KEYn] = {WAKEUP_KEY_EXTI_IRQn,
  45. TAMPER_KEY_EXTI_IRQn,
  46. USER_KEY_EXTI_IRQn};
  47. /* eval board low layer private functions */
  48. /*!
  49. \brief configure led GPIO
  50. \param[in] lednum: specify the led to be configured
  51. \arg LED1
  52. \arg LED2
  53. \arg LED3
  54. \arg LED4
  55. \param[out] none
  56. \retval none
  57. */
  58. void gd_eval_led_init(led_typedef_enum lednum)
  59. {
  60. /* enable the led clock */
  61. rcu_periph_clock_enable(GPIO_CLK[lednum]);
  62. /* configure led GPIO port */
  63. gpio_mode_set(GPIO_PORT[lednum], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN[lednum]);
  64. gpio_output_options_set(GPIO_PORT[lednum], GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN[lednum]);
  65. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  66. }
  67. /*!
  68. \brief turn on selected led
  69. \param[in] lednum: specify the led to be turned on
  70. \arg LED1
  71. \arg LED2
  72. \arg LED3
  73. \arg LED4
  74. \param[out] none
  75. \retval none
  76. */
  77. void gd_eval_led_on(led_typedef_enum lednum)
  78. {
  79. GPIO_BOP(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  80. }
  81. /*!
  82. \brief turn off selected led
  83. \param[in] lednum: specify the led to be turned off
  84. \arg LED1
  85. \arg LED2
  86. \arg LED3
  87. \arg LED4
  88. \param[out] none
  89. \retval none
  90. */
  91. void gd_eval_led_off(led_typedef_enum lednum)
  92. {
  93. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  94. }
  95. /*!
  96. \brief toggle selected led
  97. \param[in] lednum: specify the led to be toggled
  98. \arg LED1
  99. \arg LED2
  100. \arg LED3
  101. \arg LED4
  102. \param[out] none
  103. \retval none
  104. */
  105. void gd_eval_led_toggle(led_typedef_enum lednum)
  106. {
  107. GPIO_TG(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  108. }
  109. /*!
  110. \brief configure key
  111. \param[in] keynum: specify the key to be configured
  112. \arg KEY_TAMPER: tamper key
  113. \arg KEY_WAKEUP: wakeup key
  114. \arg KEY_USER: user key
  115. \param[in] keymode: 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 keynum, keymode_typedef_enum keymode)
  122. {
  123. /* enable the key clock */
  124. rcu_periph_clock_enable(KEY_CLK[keynum]);
  125. rcu_periph_clock_enable(RCU_CFGCMP);
  126. /* configure button pin as input */
  127. gpio_mode_set(KEY_PORT[keynum], GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY_PIN[keynum]);
  128. if (keymode == KEY_MODE_EXTI) {
  129. /* enable and set key EXTI interrupt to the lowest priority */
  130. nvic_irq_enable(KEY_IRQn[keynum], 2U, 0U);
  131. /* connect key EXTI line to key GPIO pin */
  132. syscfg_exti_line_config(KEY_PORT_SOURCE[keynum], KEY_PIN_SOURCE[keynum]);
  133. /* configure key EXTI line */
  134. exti_init(KEY_EXTI_LINE[keynum], EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  135. exti_interrupt_flag_clear(KEY_EXTI_LINE[keynum]);
  136. }
  137. }
  138. /*!
  139. \brief return the selected key state
  140. \param[in] keynum: specify the key to be checked
  141. \arg KEY_TAMPER: tamper key
  142. \arg KEY_WAKEUP: wakeup key
  143. \arg KEY_USER: user key
  144. \param[out] none
  145. \retval the key's GPIO pin value
  146. */
  147. uint8_t gd_eval_key_state_get(key_typedef_enum keynum)
  148. {
  149. return gpio_input_bit_get(KEY_PORT[keynum], KEY_PIN[keynum]);
  150. }
  151. /*!
  152. \brief configure COM port
  153. \param[in] com: COM on the board
  154. \arg EVAL_COM1: COM1 on the board
  155. \param[out] none
  156. \retval none
  157. */
  158. void gd_eval_com_init(uint32_t com)
  159. {
  160. uint32_t COM_ID;
  161. if(EVAL_COM1 == com){
  162. COM_ID = 0U;
  163. }else{
  164. }
  165. /* enable COM GPIO clock */
  166. rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);
  167. /* enable USART clock */
  168. rcu_periph_clock_enable(COM_CLK[COM_ID]);
  169. /* connect port to USARTx_Tx */
  170. gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);
  171. /* connect port to USARTx_Rx */
  172. gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
  173. /* configure USART Tx as alternate function push-pull */
  174. gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
  175. gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
  176. /* configure USART Rx as alternate function push-pull */
  177. gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
  178. gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
  179. /* USART configure */
  180. usart_deinit(com);
  181. usart_baudrate_set(com, 115200U);
  182. usart_receive_config(com, USART_RECEIVE_ENABLE);
  183. usart_transmit_config(com, USART_TRANSMIT_ENABLE);
  184. usart_enable(com);
  185. }