2237 views|2 replies

295

Posts

0

Resources
The OP
 

【GD32L233C-START Review】-V. GD32 transplants atomic serial port debugging components to implement serial port function debugging [Copy link]

When I used stm32 before, I was deeply impressed by the serial port debugging component of Atom, so I transplanted the component to test the serial port function debugging function.

The Atom component contains 3 files. You only need to add the function to be debugged in usmart_config.c to implement serial port debugging.

Serial port window after the function is implemented

A brief description of the specific implementation process

First rewrite the usart.c function, and adjust the received length value according to the component.

Define the serial port buffer and length indication value

u8 USART0_RECEIVE_DATA[USART0_RECV_LEN];
u16 USART0_RX_STA=0;

Configuring the Serial Port

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

 gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);//启用PA9端口USART0_TX功能
 gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);//启用PA10端口USART0_RX功能

 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);//推挽输出

 gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_10);//使能上拉
 gpio_output_options_set(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_10MHZ, GPIO_PIN_10);

 nvic_irq_enable(USART0_IRQn, 1);
 
 usart_deinit(USART0);//8,1,N; 波特率=115200;
 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);
 usart_interrupt_enable(USART0, USART_INT_RBNE);
}

Rewrite the serial port receive interrupt function

void USART0_IRQHandler(void)
{
  
	u8 Res;

		if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET)  //接收中断
		{
				Res =usart_data_receive(USART0);	//接收读到的数据
				//printf("中断中 \r \n");
				
				if((USART0_RX_STA&0x8000)==0)//接收未完成
				{
						if(USART0_RX_STA&0x4000)//接收到0x0d
						{
								if(Res!=0x0a) USART0_RX_STA=0;//接收错误,重新开始
								else USART0_RX_STA|=0x8000;	//接收完成了
						}
						else //还没收到0x0d
						{	
									if(Res==0x0d) USART0_RX_STA|=0x4000;
									else
									{
											USART0_RECEIVE_DATA[USART0_RX_STA&0X3FFF]=Res ;
											USART0_RX_STA++;
											if(USART0_RX_STA>(USART0_RECV_LEN-1)) USART0_RX_STA=0;//接收数据错误,重新开始接收  
									}		 
						}
				}   		 
		} 
}

The serial port buffer and receiving length value need to be referenced in the component

In order not to rewrite, macro definition is used to implement

#define USART_REC_LEN  		USART0_RECV_LEN  	//定义最大接收字节数 256
#define EN_USART1_RX 			1		//使能(1)/禁止(0)串口1接收
#define	USART_RX_BUF			USART0_RECEIVE_DATA

extern u8  USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符 
extern u16 USART0_RX_STA;         		//接收状态标记	
extern vu8 Task100MS;

#define	USART_RX_STA			USART0_RX_STA

The timer functions are rewritten in the component. Since there is no timer 4, TIMER5 is used to implement specific timing. I will not go into details.

The main function is as follows

/*!
    \file    main.c
    \brief   Template

    \version 2021-11-15, V1.0.0, demo for GD32L23x
*/

#include "gd32l23x.h"
#include "systick.h"
#include "usart.h"
#include "led.h"
#include "usmart.h"

vu8 Task100MS=0;	//100ms定时器中断产生,其他用途
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
	systick_config();
	USART_Config();
	LED_Config();
	//组件初始化,64是为了配合实现10Khz定时器分频,计64M/64*100 = 10Khz,每100ms产生一个定时中断
	usmart_init(64);
	
	printf("\r\nGD32L233C_START 原子串口组件测试");
  printf("\r\n利用串口助手通过USART0调试函数");
  printf("\r\n串口参数为 115200 8-N-1 \n");
  printf("list可列出调试函数 \r");

//只需要在usmart_config.c中加入需要调试的函数就可以实现串口调试
	
	while(1) 
	{
			//对接收到的串口信息进行解释和处理
			usmart_scan();
		
  		delay_1ms(100);
	}
}


The project file USMART.rar USMART.rar (369.42 KB, downloads: 21)

is available for those who need it.

This post is from GD32 MCU

Latest reply

Serial port debugging is the basic skill of programming Well done   Details Published on 2022-2-21 21:05
 

6555

Posts

0

Resources
2
 

Serial port debugging is the basic skill of programming

Well done

This post is from GD32 MCU

Comments

Thanks for the encouragement  Details Published on 2022-2-22 09:27
 
 
 

295

Posts

0

Resources
3
 
[quote]Jacktang posted on 2022-2-21 21:05 Serial port debugging is a basic skill of programming.

Thanks for the encouragement

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