eclipse下基于stm32 使用printf函数重定向的使用说明

大家都知道在嵌入式开发中将usart重定向到printf函数,在进行打印调试信息的时候非常方便,查了很多资料,大部分都是关于在keil下的printf重定向的说明,在eclipse下,对于printf的重定向和在keil下是不一样的,下面对实现方法进行说明:

1. 去ST的官方网站下载stm32的cube官方库,我下载的是stm321x  和stm32f1x 的都下载下来了,将其中一个下载下来就够了,然后解压找到里面的syscall.c文件,我在这个目录找到文件:

C:\Users\52500\Desktop\en.stm32cubel1\STM32Cube_FW_L1_V1.9.0\Projects\STM32L152D-EVAL\Examples\BSP\SW4STM32\syscalls.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:%d\n", year, mon,
                day, hour, min, sec, wkday);

      }

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值