4323 views|1 replies

4

Posts

0

Resources
The OP
 

GD32L233C-START evaluates the implementation of serial port USART0 printf redirection [Copy link]

1. Introduction to the onboard serial port of the GD32L233C-START evaluation board

The specific chip model used in the GD32L233C-START evaluation board is GD32L233CCT6, which supports 2-way UART, full-duplex/half-duplex asynchronous serial communication, programmable baud rate signal generator, and the communication baud rate can reach up to 8MBits/s. The serial port circuit schematic of the GD32L233C-START evaluation board uses a Mini_USB interface, which is connected to the serial port UART0 through the CH340 USB to serial port chip, which is convenient for customers to debug the serial port. The serial port information output generally plays an important role in the process of code debugging. Generally, it can be redirected through printf to facilitate the printing and output of debugging information during program debugging.

2. Principle of printf redirection

1. Rewrite C library functions

The user rewrites the C standard library function fputc() (printf() is actually a macro in the C standard library function, which actually calls the fputc() function). When the linker detects that the user has written a function with the same name as the C library function, it will give priority to calling the user-written function, so that redirection can be achieved.

2. Redirecting printf() function

By default, fputc() outputs characters to the debugger control window. To output data to the serial port assistant through USART, the output of fputc()'s printf() function needs to be redirected to the USART port. To use the USART function, the fputc() function needs to be redirected.

3. Establishing Project and Code Implementation

1. Create a new project and transplant the standard library files. The directory tree of the project after establishment is as follows. You can directly transplant the project through the routine, or refer to other friends' posts to transplant the project code (if you encounter problems during the transplant process, you can leave a message to discuss. If necessary, we can also introduce the transplant methods and steps of the basic project separately later).

2. To facilitate modularization, create uart.c and uart.h files and add them to the project. They are placed in the HARDWARE project directory. After that, the files related to the peripheral hardware resources are placed under this file.

Writing Program Code

The uart.c file code is as follows:

GD32L23x standard firmware library -> GD32L23x_Firmware_Library_V1.0.0 -> Examples -> USART -> Printf -> main.c for code transplantation and rewriting. The code is UART1, the serial port connected to the onboard Mini_USB of the GD32L233C-START evaluation board. As shown in the circuit schematic diagram above, serial port 0 USART0 is used. Appropriate modifications are required.

#include "uart.h"

void com_usart_init(void)
{
    /* 使能GPIOA时钟 */
    rcu_periph_clock_enable(RCU_GPIOA);
    /* 使能USART0时钟  */
    rcu_periph_clock_enable(RCU_USART0);

    /* PA9连接到USART TX */
    gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
    /* PA10连接到UUSART RX */
    gpio_af_set(GPIOA, GPIO_AF_7, 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);

    /* USART 初始化 */
    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);
    
    //使能串口USART0
    usart_enable(USART0);
}

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

The uart.h header file code is as follows:

#ifndef _UART_H_
#define  _UART_H_

#include "gd32l23x.h"
#include <stdio.h>
#include <string.h>

void com_usart_init(void);

#endif

main.c test code

#include "gd32l23x.h"
#include "systick.h"
#include "uart.h"

int main(void)
{
   uint32_t cnt = 0;
    systick_config();
    com_usart_init();
    printf("HELLO GD32L233C-START...\r\n");
    while(1) 
    {
        cnt++;       
        printf("%d ",cnt);
        if(cnt == 100)
            cnt = 0;
        printf("兆易创新GD32L233C 测评!\r\n");
        delay_1ms(1000);
    }
}

3. Project configuration

Note: Check Micro Library

Download Settings

4. Output debugging information

V. Conclusion

Printf redirection often plays an important role in the development and debugging of programs, so it is necessary to conduct corresponding tests to facilitate the implementation of the debugging information output printing function in the subsequent code development process. When learning these peripherals, the main learning method should be to transplant the code according to the DEMO routines provided by the official, and then complete the corresponding functions.

This post is from GD32 MCU

Latest reply

The code is stuck in the printf function and cannot be exited   Details Published on 2024-1-8 13:29
 

4

Posts

0

Resources
2
 

The code is stuck in the printf function and cannot be exited

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

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

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