【GD32307E-START】+Serial communication function test
[Copy link]
This post was last edited by jinglixixi on 2020-11-9 22:20
The official website provides a routine for serial communication function, but the weird thing is the hardware design. Since the GD-Link prompt is printed on the board, there will be no confusion when using it. In terms of power supply, the switch is marked with "DC" and "Link". However, the use of the virtual serial port is weird. It is not possible to use the USB of the virtual serial port for power supply, nor can it use GD-Link to form a virtual serial port. The result is that two USB cables are required to complete a simple serial communication test, as shown in Figure 1.
Before you understand these, you will never be able to tinker with the virtual serial port!
Figure 1 Serial communication test connection
After tinkering with the virtual serial port, testing is relatively simple. After compiling and downloading the program, you can see the content shown in Figure 2, that is, every time the USER key is pressed, LED1 lights up, and at the same time a "USART printf example" is output through the serial port.
Figure 2 Serial port output content
So how is the program designed?
To light up LED1, it needs to be defined and initialized. The initialization function is:
void led_init(void)
{
gd_eval_led_init(LED1);
}
The function that makes LED1 flash is as follows:
void led_flash(int times)
{
int i;
for(i=0; i<times; i++)
{
/* delay 400 ms */
delay_1ms(400);
/* turn on LEDs */
gd_eval_led_on(LED1);
/* delay 400 ms */
delay_1ms(400);
/* turn off LEDs */
gd_eval_led_off(LED1);
}
}
That is to say, the state is switched every 400 milliseconds.
For serial communication, it involves sending and receiving. The redirected printf statement is used for sending, and the fputc function is used for receiving. The content is as follows:
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM1, (uint8_t)ch);
while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
return ch;
}
Finally, let's take a look at the main program. It has few functions but is very bloated. Its contents are as follows:
int main(void)
{
/* initialize the LEDs */
led_init();
/* configure systick */
systick_config();
/* flash the LEDs for 1 time */
led_flash(1);
/* configure EVAL_COM1 */
gd_eval_com_init(EVAL_COM1);
/* configure TAMPER key */
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
/* output a message on hyperterminal using printf function */
printf("\r\n USART printf example: please press the User key \r\n");
/* wait for completion of USART transmission */
while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC)){
}
while(1)
{
/* check if the user key is pressed */
if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
{
delay_1ms(50);
if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
{
delay_1ms(50);
if(RESET == gd_eval_key_state_get(KEY_WAKEUP))
{
/* turn on LED1 */
gd_eval_led_on(LED1);
/* output a message on hyperterminal using printf function */
printf("\r\n USART printf example \r\n");
/* wait for completion of USART transmission */
while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC)){
}
}
else
{
/* turn off LED1 */
gd_eval_led_off(LED1);
}
}
else
{
/* turn off LED1 */
gd_eval_led_off(LED1);
}
}
else
{
/* turn off LED1 */
gd_eval_led_off(LED1);
}
}
}
|