【AT32A403A 车规MCU开发板】 串口配置及printf打印输出
[复制链接]
学习下AT32A403开发板串口输出及printf打印。
一、硬件串口部分
开发板AT-Link仿真器带有USB虚拟串口,连接到开发板的串口1,电路图部分如下
1.1、AT-Link接口
1.2、MCU串口1接口
二、程序代码
2.1、串口部分代码
串口代码使用AT32 Work Bench软件来生成
配置串口1
配置串口对应的端口
生成的代码截图
可以使用AT32 Work Bench软件来生成项目测试,也可以将生成的代码复制到自己的项目中
勾选USE MicroLib
usart.c
#include "main.h"
void init_usart(uint32_t baudrate)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
/* configure the TX pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_9;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* configure the RX pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_10;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* configure param */
usart_init(USART1, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_transmitter_enable(USART1, TRUE);
usart_receiver_enable(USART1, TRUE);
usart_parity_selection_config(USART1, USART_PARITY_NONE);
usart_hardware_flow_control_set(USART1, USART_HARDWARE_FLOW_NONE);
usart_enable(USART1, TRUE);
}
/* support printf function, usemicrolib is unnecessary */
#if (__ARMCC_VERSION > 6000000)
__asm (".global __use_no_semihosting\n\t");
void _sys_exit(int x)
{
x = x;
}
/* __use_no_semihosting was requested, but _ttywrch was */
void _ttywrch(int ch)
{
ch = ch;
}
FILE __stdout;
#else
#ifdef __CC_ARM
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
/* __use_no_semihosting was requested, but _ttywrch was */
void _ttywrch(int ch)
{
ch = ch;
}
#endif
#endif
#if defined (__GNUC__) && !defined (__clang__)
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] retargets the c library printf function to the usart.
* @param none
* @retval none
*/
PUTCHAR_PROTOTYPE
{
while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
usart_data_transmit(USART1, (uint16_t)ch);
while(usart_flag_get(USART1, USART_TDC_FLAG) == RESET);
return ch;
}
#if (defined (__GNUC__) && !defined (__clang__)) || (defined (__ICCARM__))
#if defined (__GNUC__) && !defined (__clang__)
int _write(int fd, char *pbuffer, int size)
#elif defined ( __ICCARM__ )
#pragma module_name = "?__write"
int __write(int fd, char *pbuffer, int size)
#endif
{
for(int i = 0; i < size; i ++)
{
while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
usart_data_transmit(PRINT_UART, (uint16_t)(*pbuffer++));
while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
}
return size;
}
#endif
2.2、main.c
#include "main.h"
int main(void)
{
system_clock_config();
delay_init();
init_usart(115200);
init_led();
while(1)
{
led2_tog();
delay_ms(100);
led3_tog();
delay_ms(100);
led4_tog();
delay_ms(100);
printf("https://bbs.eeworld.com.cn/at32a403 board uart test!\r\n");
}
}
三、运行结果
下载程序,复位开发板后,串口输出
|