1 /*!
2 * \file gd32vf103c_start.c
3 * \brief firmware functions to manage leds, keys, COM ports
4 *
5 * \version 2020-02-05, V1.0.0, rvstar board functions for GD32VF103
6 */
7
8 /*
9 Copyright (c) 2019, GigaDevice Semiconductor Inc.
10
11 Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13
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
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34
35 #include "gd32vf103v_rvstar.h"
36
37 /* private variables */
38 static const uint32_t GPIO_PORT[LEDn] = {LEDG_GPIO_PORT,LEDB_GPIO_PORT,LEDR_GPIO_PORT};
39
40 static const uint32_t GPIO_PIN[LEDn] = {LEDG_PIN,LEDB_PIN,LEDR_PIN};
41
42 static const rcu_periph_enum GPIO_CLK[LEDn] = {LEDG_GPIO_CLK,LEDB_GPIO_CLK,LEDR_GPIO_CLK};
43
44 static const uint32_t KEY_PORT[KEYn] = {WAKEUP_KEY_GPIO_PORT};
45
46 static const uint32_t KEY_PIN[KEYn] = {WAKEUP_KEY_PIN};
47
48 static const rcu_periph_enum KEY_CLK[KEYn] = {WAKEUP_KEY_GPIO_CLK};
49
50 static const exti_line_enum KEY_EXTI_LINE[KEYn] = {WAKEUP_KEY_EXTI_LINE};
51
52 static const uint8_t KEY_PORT_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PORT_SOURCE};
53
54 static const uint8_t KEY_PIN_SOURCE[KEYn] = {WAKEUP_KEY_EXTI_PIN_SOURCE};
55
56 static const uint8_t KEY_IRQn[KEYn] = {WAKEUP_KEY_EXTI_IRQn};
57
58 /* eval board low layer private functions */
59 /*!
60 * \brief configure led GPIO
61 * \param[in] lednum: specify the led to be configured
62 * \arg LED1
63 * \param[out] none
64 * \retval none
65 */
gd_led_init(led_typedef_enum lednum)66 void gd_led_init(led_typedef_enum lednum)
67 {
68 /* enable the led clock */
69 rcu_periph_clock_enable(GPIO_CLK[lednum]);
70 /* configure led GPIO port */
71 gpio_init(GPIO_PORT[lednum], GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN[lednum]);
72 GPIO_BOP(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
73 }
74
75 /*!
76 * \brief turn on selected led
77 * \param[in] lednum: specify the led to be turned on
78 * \arg LED1
79 * \param[out] none
80 * \retval none
81 */
gd_led_on(led_typedef_enum lednum)82 void gd_led_on(led_typedef_enum lednum)
83 {
84 GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
85 }
86
87 /*!
88 * \brief turn off selected led
89 * \param[in] lednum: specify the led to be turned off
90 * \arg LED1
91 * \param[out] none
92 * \retval none
93 */
gd_led_off(led_typedef_enum lednum)94 void gd_led_off(led_typedef_enum lednum)
95 {
96 GPIO_BOP(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
97 }
98
99 /*!
100 * \brief toggle selected led
101 * \param[in] lednum: specify the led to be toggled
102 * \arg LED1
103 * \param[out] none
104 * \retval none
105 */
gd_led_toggle(led_typedef_enum lednum)106 void gd_led_toggle(led_typedef_enum lednum)
107 {
108 gpio_bit_write(GPIO_PORT[lednum], GPIO_PIN[lednum],
109 (bit_status)(1-gpio_input_bit_get(GPIO_PORT[lednum], GPIO_PIN[lednum])));
110 }
111
112 /*!
113 * \brief configure key
114 * \param[in] keynum: specify the key to be configured
115 * \arg KEY_WAKEUP: wakeup key
116 * \param[in] keymode: specify button mode
117 * \arg KEY_MODE_GPIO: key will be used as simple IO
118 * \arg KEY_MODE_EXTI: key will be connected to EXTI line with interrupt
119 * \param[out] none
120 * \retval none
121 */
gd_key_init(key_typedef_enum keynum,keymode_typedef_enum keymode)122 void gd_key_init(key_typedef_enum keynum, keymode_typedef_enum keymode)
123 {
124 /* enable the key clock */
125 rcu_periph_clock_enable(KEY_CLK[keynum]);
126 rcu_periph_clock_enable(RCU_AF);
127
128 /* configure button pin as input */
129 gpio_init(KEY_PORT[keynum], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, KEY_PIN[keynum]);
130
131 if (keymode == KEY_MODE_EXTI) {
132 /* enable and set key EXTI interrupt to the lowest priority */
133 ECLIC_EnableIRQ(KEY_IRQn[keynum]);
134 ECLIC_SetLevelIRQ(KEY_IRQn[keynum],1);
135 ECLIC_SetPriorityIRQ(KEY_IRQn[keynum],1);
136
137 /* connect key EXTI line to key GPIO pin */
138 gpio_exti_source_select(KEY_PORT_SOURCE[keynum], KEY_PIN_SOURCE[keynum]);
139
140 /* configure key EXTI line */
141 exti_init(KEY_EXTI_LINE[keynum], EXTI_INTERRUPT, EXTI_TRIG_FALLING);
142 exti_interrupt_flag_clear(KEY_EXTI_LINE[keynum]);
143 }
144 }
145
146 /*!
147 * \brief return the selected key state
148 * \param[in] keynum: specify the key to be checked
149 * \arg KEY_WAKEUP: wakeup key
150 * \param[out] none
151 * \retval the key's GPIO pin value
152 */
gd_key_state_get(key_typedef_enum keynum)153 uint8_t gd_key_state_get(key_typedef_enum keynum)
154 {
155 return gpio_input_bit_get(KEY_PORT[keynum], KEY_PIN[keynum]);
156 }
157
158 /*!
159 * \brief configure COM port
160 * \param[in] com: COM on the board
161 * \arg GD32_COM0: COM0 on the board
162 * \param[out] none
163 * \retval none
164 */
gd_com_init(uint32_t usart_periph)165 void gd_com_init(uint32_t usart_periph)
166 {
167 /* enable GPIO TX and RX clock */
168 rcu_periph_clock_enable(GD32_COM_TX_GPIO_CLK);
169 rcu_periph_clock_enable(GD32_COM_RX_GPIO_CLK);
170
171 /* enable USART clock */
172 rcu_periph_clock_enable(GD32_COM_CLK);
173
174 /* connect port to USARTx_Tx */
175 gpio_init(GD32_COM_TX_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GD32_COM_TX_PIN);
176
177 /* connect port to USARTx_Rx */
178 gpio_init(GD32_COM_RX_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GD32_COM_RX_PIN);
179
180 /* USART configure */
181 usart_deinit(usart_periph);
182 usart_baudrate_set(usart_periph, 115200U);
183 usart_word_length_set(usart_periph, USART_WL_8BIT);
184 usart_stop_bit_set(usart_periph, USART_STB_1BIT);
185 usart_parity_config(usart_periph, USART_PM_NONE);
186 usart_hardware_flow_rts_config(usart_periph, USART_RTS_DISABLE);
187 usart_hardware_flow_cts_config(usart_periph, USART_CTS_DISABLE);
188 usart_receive_config(usart_periph, USART_RECEIVE_ENABLE);
189 usart_transmit_config(usart_periph, USART_TRANSMIT_ENABLE);
190 usart_enable(usart_periph);
191 }
192