gd32f307c_eval.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*!
  2. \file gd32f307c_eval.c
  3. \brief firmware functions to manage leds, keys, COM ports
  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. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include <gd32f30x.h>
  33. #include "gd32f307c_eval.h"
  34. /* private variables */
  35. static uint32_t GPIO_PORT[LEDn] = {LED2_GPIO_PORT, LED3_GPIO_PORT,
  36. LED4_GPIO_PORT, LED5_GPIO_PORT};
  37. static uint32_t GPIO_PIN[LEDn] = {LED2_PIN, LED3_PIN, LED4_PIN, LED5_PIN};
  38. static rcu_periph_enum COM_CLK[COMn] = {EVAL_COM0_CLK, EVAL_COM1_CLK};
  39. static uint32_t COM_TX_PIN[COMn] = {EVAL_COM0_TX_PIN, EVAL_COM1_TX_PIN};
  40. static uint32_t COM_RX_PIN[COMn] = {EVAL_COM0_RX_PIN, EVAL_COM1_RX_PIN};
  41. static uint32_t COM_GPIO_PORT[COMn] = {EVAL_COM0_GPIO_PORT, EVAL_COM1_GPIO_PORT};
  42. static rcu_periph_enum COM_GPIO_CLK[COMn] = {EVAL_COM0_GPIO_CLK, EVAL_COM1_GPIO_CLK};
  43. static rcu_periph_enum GPIO_CLK[LEDn] = {LED2_GPIO_CLK, LED3_GPIO_CLK,
  44. LED4_GPIO_CLK, LED5_GPIO_CLK};
  45. static uint32_t KEY_PORT[KEYn] = {WAKEUP_KEY_GPIO_PORT,
  46. TAMPER_KEY_GPIO_PORT,
  47. USER_KEY_GPIO_PORT};
  48. static uint32_t KEY_PIN[KEYn] = {WAKEUP_KEY_PIN, TAMPER_KEY_PIN,USER_KEY_PIN,};
  49. static rcu_periph_enum KEY_CLK[KEYn] = {WAKEUP_KEY_GPIO_CLK,
  50. TAMPER_KEY_GPIO_CLK,
  51. USER_KEY_GPIO_CLK};
  52. static exti_line_enum KEY_EXTI_LINE[KEYn] = {WAKEUP_KEY_EXTI_LINE,
  53. TAMPER_KEY_EXTI_LINE,
  54. USER_KEY_EXTI_LINE};
  55. static uint8_t KEY_PORT_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PORT_SOURCE,
  56. TAMPER_KEY_EXTI_PORT_SOURCE,
  57. USER_KEY_EXTI_PORT_SOURCE};
  58. static uint8_t KEY_PIN_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PIN_SOURCE,
  59. TAMPER_KEY_EXTI_PIN_SOURCE,
  60. USER_KEY_EXTI_PIN_SOURCE};
  61. static uint8_t KEY_IRQn[KEYn] = {WAKEUP_KEY_EXTI_IRQn,
  62. TAMPER_KEY_EXTI_IRQn,
  63. USER_KEY_EXTI_IRQn};
  64. /*!
  65. \brief configure led GPIO
  66. \param[in] lednum: specify the led to be configured
  67. \arg LED2
  68. \arg LED3
  69. \arg LED4
  70. \arg LED5
  71. \param[out] none
  72. \retval none
  73. */
  74. void gd_eval_led_init (led_typedef_enum lednum)
  75. {
  76. /* enable the led clock */
  77. rcu_periph_clock_enable(GPIO_CLK[lednum]);
  78. /* configure led GPIO port */
  79. gpio_init(GPIO_PORT[lednum], GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN[lednum]);
  80. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  81. }
  82. /*!
  83. \brief turn on selected led
  84. \param[in] lednum: specify the led to be turned on
  85. \arg LED2
  86. \arg LED3
  87. \arg LED4
  88. \arg LED5
  89. \param[out] none
  90. \retval none
  91. */
  92. void gd_eval_led_on(led_typedef_enum lednum)
  93. {
  94. GPIO_BOP(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  95. }
  96. /*!
  97. \brief turn off selected led
  98. \param[in] lednum: specify the led to be turned off
  99. \arg LED2
  100. \arg LED3
  101. \arg LED4
  102. \arg LED5
  103. \param[out] none
  104. \retval none
  105. */
  106. void gd_eval_led_off(led_typedef_enum lednum)
  107. {
  108. GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
  109. }
  110. /*!
  111. \brief toggle selected led
  112. \param[in] lednum: specify the led to be toggled
  113. \arg LED2
  114. \arg LED3
  115. \arg LED4
  116. \arg LED5
  117. \param[out] none
  118. \retval none
  119. */
  120. void gd_eval_led_toggle(led_typedef_enum lednum)
  121. {
  122. gpio_bit_write(GPIO_PORT[lednum], GPIO_PIN[lednum],
  123. (bit_status)(1-gpio_input_bit_get(GPIO_PORT[lednum], GPIO_PIN[lednum])));
  124. }
  125. /*!
  126. \brief configure key
  127. \param[in] key_num: specify the key to be configured
  128. \arg KEY_TAMPER: tamper key
  129. \arg KEY_WAKEUP: wakeup key
  130. \arg KEY_USER: user key
  131. \param[in] key_mode: specify button mode
  132. \arg KEY_MODE_GPIO: key will be used as simple IO
  133. \arg KEY_MODE_EXTI: key will be connected to EXTI line with interrupt
  134. \param[out] none
  135. \retval none
  136. */
  137. void gd_eval_key_init(key_typedef_enum key_num, keymode_typedef_enum key_mode)
  138. {
  139. /* enable the key clock */
  140. rcu_periph_clock_enable(KEY_CLK[key_num]);
  141. rcu_periph_clock_enable(RCU_AF);
  142. /* configure button pin as input */
  143. gpio_init(KEY_PORT[key_num], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, KEY_PIN[key_num]);
  144. if (key_mode == KEY_MODE_EXTI) {
  145. /* enable and set key EXTI interrupt to the lowest priority */
  146. nvic_irq_enable(KEY_IRQn[key_num], 2U, 0U);
  147. /* connect key EXTI line to key GPIO pin */
  148. gpio_exti_source_select(KEY_PORT_SOURCE[key_num], KEY_PIN_SOURCE[key_num]);
  149. /* configure key EXTI line */
  150. exti_init(KEY_EXTI_LINE[key_num], EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  151. exti_interrupt_flag_clear(KEY_EXTI_LINE[key_num]);
  152. }
  153. }
  154. /*!
  155. \brief return the selected key state
  156. \param[in] key: specify the key to be checked
  157. \arg KEY_TAMPER: tamper key
  158. \arg KEY_WAKEUP: wakeup key
  159. \arg KEY_USER: user key
  160. \param[out] none
  161. \retval the key's GPIO pin value
  162. */
  163. uint8_t gd_eval_key_state_get(key_typedef_enum key)
  164. {
  165. return gpio_input_bit_get(KEY_PORT[key], KEY_PIN[key]);
  166. }
  167. /*!
  168. \brief configure COM port
  169. \param[in] com: COM on the board
  170. \arg EVAL_COM0: COM0 on the board
  171. \arg EVAL_COM1: COM1 on the board
  172. \param[out] none
  173. \retval none
  174. */
  175. void gd_eval_com_init(uint32_t com)
  176. {
  177. uint32_t com_id = 0U;
  178. if(EVAL_COM0 == com){
  179. com_id = 0U;
  180. }else if(EVAL_COM1 == com){
  181. com_id = 1U;
  182. }
  183. /* enable GPIO clock */
  184. rcu_periph_clock_enable(COM_GPIO_CLK[com_id]);
  185. /* enable USART clock */
  186. rcu_periph_clock_enable(COM_CLK[com_id]);
  187. /* connect port to USARTx_Tx */
  188. gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_TX_PIN[com_id]);
  189. /* connect port to USARTx_Rx */
  190. gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_RX_PIN[com_id]);
  191. /* USART configure */
  192. usart_deinit(com);
  193. usart_baudrate_set(com, 115200U);
  194. usart_receive_config(com, USART_RECEIVE_ENABLE);
  195. usart_transmit_config(com, USART_TRANSMIT_ENABLE);
  196. usart_enable(com);
  197. }