大家都知道在嵌入式开发中将usart重定向到printf函数,在进行打印调试信息的时候非常方便,查了很多资料,大部分都是关于在keil下的printf重定向的说明,在eclipse下,对于printf的重定向和在keil下是不一样的,下面对实现方法进行说明:
1. 去ST的官方网站下载stm32的cube官方库,我下载的是stm321x 和stm32f1x 的都下载下来了,将其中一个下载下来就够了,然后解压找到里面的syscall.c文件,我在这个目录找到文件:
C:Users52500Desktopen.stm32cubel1STM32Cube_FW_L1_V1.9.0ProjectsSTM32L152D-EVALExamplesBSPSW4STM32syscalls.c
在你的eclipse工程目录下创建newlib文件夹,将syscall.c文件拷贝到newlib下,然后在右键refresh 刷新你的工程,可以看到syscall.c添加到你的工程中了,打开这个文件看一下,里面有一个_write函数:
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
eclipse中用的是newlib接口,底层用的_write(), 而keil底层用的是fputc().
2.在uart.c(我的usart配置放在这个.c文件中,名字根据你自己的来)中加入如下函数:
//eclipse use __io_putchar() to printf
int __io_putchar(int ch){
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==0);
return (ch);
}
下面也付上usart1的配置:
void Usart1_Init(uint32_t BaudRate, uint16_t Parity, uint16_t WordLength) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //uart1在APB2上
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //这两句一定要有,因为这是开启PA9,PA19的usart复用功能
/* Configure USART2 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //这里选择AF
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //这里不需要ENABLE TXE也可以进入发送中断
USART_InitStruct.USART_BaudRate = BaudRate;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx| USART_Mode_Tx;
USART_InitStruct.USART_Parity = Parity; //USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = WordLength; //USART_WordLength_8b;
USART_Init(USART1, &USART_InitStruct);
USART_Cmd(USART1, ENABLE);
}
做好上面的配置后,可以直接调用printf()进行串口输出打印了。也可以通过串口进行串口命令设置,如参数配置等。
int main(void) {
platform_rcc_init(); //SYSCLK,PLLCKL,HCLK,PCK1(APB1),PLK2(APB2)时钟初始化配置
RTC_Configuration(); //RTC的时钟配置
Usart1_Init(9600, USART_Parity_No, USART_WordLength_8b);
printf("Boot Initialization!n");
SetRTC(75900, 191028, 1);
while (1) {
LED_OUT(0);
printf("while begin!n");
process_usart(); //对于串口命令的处理
updatetime();
printf("datetime:%02d-%02d-%02d %02d:%02d:%02d weakday:%dn", year, mon,
day, hour, min, sec, wkday);
}
}
|
|
2021-12-1 10:12:53
评论
举报
|
|
|