3356 views|9 replies

1452

Posts

1

Resources
The OP
 

[GigaDevice GD32F310 Review] + UART Application [Copy link]

This post was last edited by jinglixixi on 2022-5-30 00:17

GD32F310 is equipped with standard and advanced communication interfaces including 2 USARTs, which can be used for debugging output and communication between serial port devices.

1. printf output

To realize printf output, it is necessary to configure the pins and serial communication. The main functions of this work are usart0_gpio_config(),

usart0_config() and fputc() are used to complete the operation. The content is as follows:

void usart0_gpio_config(void)
{
 rcu_periph_clock_enable(RCU_GPIOA);
 /* connect port to USARTx_Tx */
 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
 /* connect port to USARTx_Rx */
 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
}

void usart0_config(void)
{
 rcu_periph_clock_enable(RCU_USART0);
 usart_deinit(USART0);
 usart_word_length_set(USART0, USART_WL_8BIT);
 usart_stop_bit_set(USART0, USART_STB_1BIT);
 usart_parity_config(USART0, USART_PM_NONE);
 usart_baudrate_set(USART0, 115200U);
 usart_receive_config(USART0, USART_RECEIVE_ENABLE);
 usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
 usart_enable(USART0);
}

int fputc(int ch, FILE *f)
{
 usart_data_transmit(USART0, (uint8_t) ch);
 while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
 return ch;
}

With the support of the above functions, the printf output function can be realized, and the test effect is shown in Figure 1.

Figure 1 Output test

The main program to implement the output test is:

int main(void)
{
 systick_config();
 usart0_gpio_config();
 usart0_config();
 printf("usart transmit test example!");
while(1);
}

2. Send characters

In serial communication, sending various instructions and parameters is very common and very useful. The problem can be solved by using the function usart_data_transmit in the function library. If sending instructions, the instructions can also be stored in an array to simplify processing.

The test procedure for sending characters is as follows:

int main(void)
{
 systick_config();
 usart0_gpio_config();
 usart0_config();
 printf("usart transmit test example!");
while(1){
 delay_1ms(1000);
 usart_data_transmit(USART0, 'A');
 }
}

After downloading and testing, the test result is shown in Figure 2, indicating that the function is normal.

Figure 2 Sending characters

3. Send and receive processing

In serial communication, the sending and receiving response between devices is very important, which can ensure the reliable operation of the devices.

Use the receiving function usart_data_receive() to complete the test of the receiving and sending process. The test procedure is as follows:

int main(void)
{
 uint16_t m,p;
 systick_config();
 usart0_gpio_config();
 usart0_config();
while(1){
    m=usart_data_receive(USART0);
if(m!=p)
{
usart_data_transmit(USART0, m);
p=m;
}
 }
}

The program is downloaded, and the test result is shown in FIG3 , that is, each time an inconsistent character is sent, the corresponding content is echoed.

Figure 3 Character reception and transmission

This post is from GD32 MCU

Latest reply

Thank you for sharing! Let's learn and progress together. Come on! Come on!   Details Published on 2022-6-3 22:35
 

6555

Posts

0

Resources
2
 

UART application is a basic application and the evaluation is normal

This post is from GD32 MCU
 
 
 

6818

Posts

11

Resources
3
 
Great tutorial, thanks for sharing.
This post is from GD32 MCU

Comments

Thanks for your support!!!  Details Published on 2022-5-31 16:53
 
 
 

121

Posts

0

Resources
4
 

Thank you for sharing, I have learned a lot and hope to have the opportunity to communicate more about this.

This post is from GD32 MCU
 
 
 

1452

Posts

1

Resources
5
 
Chip Veteran Published on 2022-5-31 13:52 Thank you for sharing, I have learned a lot, and I hope to have the opportunity to communicate more about these.

This post is from GD32 MCU
 
 
 

1452

Posts

1

Resources
6
 
lugl4313820 posted on 2022-5-30 08:19 Very good tutorial, thank you for sharing.

Thanks for your support!!!

This post is from GD32 MCU
 
 
 

121

Posts

0

Resources
7
 

Thanks for sharing, very professional.

This post is from GD32 MCU
 
 
 

121

Posts

0

Resources
8
 

Thank you for sharing, very professional, I hope to have the opportunity to learn.

This post is from GD32 MCU
 
 
 

7

Posts

0

Resources
9
 

Thank you for sharing! Let's learn and progress together. Come on! Come on!

This post is from GD32 MCU
 
 
 

1452

Posts

1

Resources
10
 
ZanbaTea posted on 2022-6-3 22:35 Thank you for sharing! Let's learn and progress together, come on! Come on!

This post is from GD32 MCU
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list