测试串口,使用printf打印输出。
一、硬件电路部分
仿真器带调试串口,调试串口连接硬件的MCU的USRAT1。
二、程序
2.1、usart.c
#include "stdio.h"
#include "at32f423.h"
#include "usart/usart.h"
/* 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=247401]@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
void init_usart(uint32_t baudrate)
{
gpio_init_type gpio_init_struct;
#if defined (__GNUC__) && !defined (__clang__)
setvbuf(stdout, NULL, _IONBF, 0);
#endif
/* enable the uart and gpio clock */
crm_periph_clock_enable(USART1_CLOCK, TRUE);
crm_periph_clock_enable(USART1_GPIO_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
/* configure the uart tx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = USART1_TX_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(USART1_GPIO, &gpio_init_struct);
gpio_pin_mux_config(USART1_GPIO, USART1_TX_PIN_SOURCE, USART1_TX_PIN_MUX_NUM);
/* configure uart param */
usart_init(USART1, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_transmitter_enable(USART1, TRUE);
usart_enable(USART1, TRUE);
}
2.2、usart.h
#ifndef _USART_H
#define _USART_H
#define USART1_CLOCK CRM_USART1_PERIPH_CLOCK
#define USART1_TX_PIN GPIO_PINS_9
#define USART1_GPIO GPIOA
#define USART1_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define USART1_TX_PIN_SOURCE GPIO_PINS_SOURCE9
#define USART1_TX_PIN_MUX_NUM GPIO_MUX_7
void init_usart(uint32_t baudrate);
#endif
2.3、main.c
#include "at32f423_board.h"
#include "at32f423_clock.h"
#include "led/led.h"
#include "key/key.h"
#include "usart/usart.h"
int main(void)
{
system_clock_config();
at32_board_init();
init_led();
init_key();
init_usart(115200);
while(1)
{
led2_tog();
delay_ms(200);
led3_tog();
delay_ms(200);
led4_tog();
delay_ms(200);
printf("AT32F423 USART1 TEST! \r\n");
}
}
三、程序运行
|