【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
具体代码见附件:
明白了 本帖最后由 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
ac6编译器不能输出,原因是默认定义了 __GNUC__,导致错误,我们自定义选择条件编译就好。
ac5 和ac6 都走fputc(); 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;
}
keil5 重定向printf函数到串口输出 使用MicroLib 习惯了使用printf进行打印输出 使用KEIL5对stm32的printf函数进行重定向 一种是使用微库 使用keil5 ? 使用KEIL5 的printf函数 直接使用printf()函数 直接用sprintf格式化到字符串 如果使用的是MDK,请在工程属性的“Target“-》”CodeGeneration“中勾驯Use MicroLIB“ 进行如上设置后编译一下 点击options for target,在Target标签下有个Use MicroLIB---取消勾选,不使用微库。 printf函数的重定向。 不勾选printf函数在main函数中还是用不了 利用printf函数打印调试信息到串口助手
页:
[1]
2