GD32 Usart配置与重定义fputc

 GD32F303X   Uart配置

void Usart_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_USART0);
	rcu_periph_clock_enable(RCU_AF);
	/* configure USART Tx as alternate function push-pull */
	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
	gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10); 
	
	/* USART configure */
	usart_deinit(USART0);
	nvic_irq_enable(USART0_IRQn, 0, 0);
	usart_baudrate_set(USART0, 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_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	usart_interrupt_enable(USART0, USART_INT_RBNE);
	usart_interrupt_enable(USART0, USART_INT_ERR);
	usart_enable(USART0); 
}

void UART_SendSpBuffer(uint32_t usart_periph,uint8_t* pSda,uint8_t len)
{
	int i=0;
	for(i=0;i<len;i++)
	{
	  while(RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));//等待数据寄存器变空
      usart_data_transmit(usart_periph, *pSda);		
	  pSda++;
	}
	 while(RESET == usart_flag_get(usart_periph, USART_FLAG_TC));
}

void USART0_IRQHandler(void )
{
	uint8_t value;
	if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
	{
		/* receive data */
		value = (uint8_t)usart_data_receive(USART0);
		usart_interrupt_flag_clear(USART0,USART_INT_FLAG_ERR_FERR);
	}
}

fputc重定义

#if 1
#pragma import(__use_no_semihosting)             
               
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
void _sys_exit(int x) 
{ 
	x = x; 
} 
/* 重定义fputc函数 */
int fputc(int ch, FILE *f)
{   
	while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
		;	
	usart_data_transmit(USART0, (uint8_t)ch);

	return ch;
}
#endif 

版权声明:本文为CSDN博主「Bourne76」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cftchaoxiaoshu/article/details/122702216

 GD32F303X   Uart配置

void Usart_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_USART0);
	rcu_periph_clock_enable(RCU_AF);
	/* configure USART Tx as alternate function push-pull */
	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
	gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10); 
	
	/* USART configure */
	usart_deinit(USART0);
	nvic_irq_enable(USART0_IRQn, 0, 0);
	usart_baudrate_set(USART0, 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_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	usart_interrupt_enable(USART0, USART_INT_RBNE);
	usart_interrupt_enable(USART0, USART_INT_ERR);
	usart_enable(USART0); 
}

void UART_SendSpBuffer(uint32_t usart_periph,uint8_t* pSda,uint8_t len)
{
	int i=0;
	for(i=0;i<len;i++)
	{
	  while(RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));//等待数据寄存器变空
      usart_data_transmit(usart_periph, *pSda);		
	  pSda++;
	}
	 while(RESET == usart_flag_get(usart_periph, USART_FLAG_TC));
}

void USART0_IRQHandler(void )
{
	uint8_t value;
	if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
	{
		/* receive data */
		value = (uint8_t)usart_data_receive(USART0);
		usart_interrupt_flag_clear(USART0,USART_INT_FLAG_ERR_FERR);
	}
}

fputc重定义

#if 1
#pragma import(__use_no_semihosting)             
               
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
void _sys_exit(int x) 
{ 
	x = x; 
} 
/* 重定义fputc函数 */
int fputc(int ch, FILE *f)
{   
	while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
		;	
	usart_data_transmit(USART0, (uint8_t)ch);

	return ch;
}
#endif 

版权声明:本文为CSDN博主「Bourne76」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cftchaoxiaoshu/article/details/122702216

生成海报
点赞 0

Bourne76

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

GD32的Keil环境搭建简述

以GD32F30x为例 从GD官网下载开发文件: GD32F30x_AddOn_V2.1.0.rar包含Keil所需的gd32相关芯片的pack: GD32F30x_Demo_Suites_V2.3.0.rar包

KEIL软件读取不到部分头文件

建立GD32的工程时,有个gd32f10x_libopt文件一直读取不到(或者读取的是安装路径下的此文件),反复检查头文件包含路径都没有问题。 最后发现KEIL软件读取头文件会优先从设置

GD32的Keil环境搭建简述

以GD32F30x为例 从GD官网下载开发文件: GD32F30x_AddOn_V2.1.0.rar包含Keil所需的gd32相关芯片的pack: GD32F30x_Demo_Suites_V2.3.0.rar包