caizhiwei 发表于 2021-2-13 11:46

【AT-START-F407测评】03 解决MDK5下AC6编译 Printf不能输出的问题


关于切换到AC6,可以看我上一篇贴子:
https://bbs.21ic.com/icview-3071490-1-1.html

但是切换后的副作用随之而来,之前串口打印的东西不见了,经过调查发现,问题出现在printf重定向:
修改bsp_uart.c 中的驱动代码如下可以解决问题:
#if 0/* only For AC5*/
/**
* @briefRetargets the C library printf function to the USART.
* @paramNone
* @retval None
*/
PUTCHAR_PROTOTYPE
{
USART_SendData(AT32_PRINT_UART, ch);
while ( USART_GetFlagStatus(AT32_PRINT_UART, USART_FLAG_TRAC) == RESET );   
return ch;
}

#else/* For AC5 and AC6 */


/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
USART_SendData(AT32_PRINT_UART, ch);
while ( USART_GetFlagStatus(AT32_PRINT_UART, USART_FLAG_TRAC) == RESET );   
return ch;
}

#endif


具体代码见附件:


wziyi 发表于 2021-2-14 11:11

明白了

caizhiwei 发表于 2021-3-27 16:01

本帖最后由 caizhiwei 于 2021-3-27 16:17 编辑

今天又回过头来review代码,发现这样写更合适:

如何让一份代码兼容GCC,ARMCC ,Clang 三种编译器呢?
高手是这样写的 :/**
* @briefRetargets the C library printf function to the USART.
* @paramNone
* @retval None
*/
#if defined ( __CC_ARM )

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)       
/*
* Arm Compiler above 6.10.1 (armclang)
*/
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100)

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

/*
* GNU Compiler
*/
#elif defined ( __GNUC__ )

/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#endif

caizhiwei 发表于 2021-3-27 16:03

ac6编译器不能输出,原因是默认定义了 __GNUC__,导致错误,我们自定义选择条件编译就好。
ac5 和ac6 都走fputc();

caizhiwei 发表于 2021-4-3 12:11

PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART1 and Loop until the end of transmission */
HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}


int fgetc(FILE *f)
{
    int ret;      
    HAL_UART_Receive(&hlpuart1, (uint8_t *)&ret, 1, HAL_MAX_DELAY);
    return ret;
}

51xlf 发表于 2021-4-5 13:59

keil5 重定向printf函数到串口输出

i1mcu 发表于 2021-4-5 13:59

使用MicroLib

pmp 发表于 2021-4-5 13:59

习惯了使用printf进行打印输出

mmbs 发表于 2021-4-5 14:00

使用KEIL5对stm32的printf函数进行重定向

1988020566 发表于 2021-4-5 14:00

一种是使用微库

lzbf 发表于 2021-4-5 14:01

使用keil5 ?

youtome 发表于 2021-4-5 14:01

使用KEIL5 的printf函数

cemaj 发表于 2021-4-5 14:02

直接使用printf()函数

jimmhu 发表于 2021-4-5 14:02

直接用sprintf格式化到字符串

uptown 发表于 2021-4-5 14:02

如果使用的是MDK,请在工程属性的“Target“-》”CodeGeneration“中勾驯Use MicroLIB“ 进行如上设置后编译一下

1988020566 发表于 2021-4-5 14:02

点击options for target,在Target标签下有个Use MicroLIB---取消勾选,不使用微库。

mmbs 发表于 2021-4-5 14:02

            

pmp 发表于 2021-4-5 14:03

printf函数的重定向。

i1mcu 发表于 2021-4-5 14:03

不勾选printf函数在main函数中还是用不了

51xlf 发表于 2021-4-5 14:03

利用printf函数打印调试信息到串口助手
页: [1] 2
查看完整版本: 【AT-START-F407测评】03 解决MDK5下AC6编译 Printf不能输出的问题