SW4STM32下使用printf发送消息,将消息用串口助手接收
问题描述:
在keil环境下正常使用printf功能,但是在SW4STM32下使用不了,下面是添加到uart.c里面的重定向语句
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
如何才能正常使用printf呢?
8 个回复
admin
赞同来自:
SW4STM32
没用过,不发表意见了。。。
callmeSir - 80后it
赞同来自:
楼主解决了吗
ac
赞同来自:
金志峰
赞同来自: wansaiyon 、jun503380 、sky-blue1992
SW4STM32和AC6的printf重定向请用下面代码!(风信子)
int _write (int fd, char *ptr, int len)
{
HAL_UART_Transmit(&huart2, (uint8_t*) ptr, len, 0xFFFF);
return len;
}
int _read (int fd, char *ptr, int len)
{
*ptr = 0x00; // Flush the character buffer
HAL_UART_Receive(&huart2, (uint8_t*) ptr, 1, 0xFFFF);
return 1;
}
hanzixi_angel
赞同来自: admin 、taylorle
sw4stm32环境下如何添加printf支持.首先在安装目录下找到syscalls.c文件,将该文件放到工程的src文件下;然后需要使用串口的发送函数,编写下面的接口函数:
void __io_putchar(uint8_t ch)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);
//return (ch);
}
完成该操作后,printf应该是可以输出字符串和整型格式化的数据,但是对于浮点数显示的不对,此时需要添加编译选项,添加流程为Properties -> C/C++ Build -> Settings -> MCU GCC Linker -> Miscellaneous -> Linker flags后面增加-u _printf_float标志.重新编译即可实现printf功能.
wansaiyon
赞同来自:
结题:
参考官方例程及hanzixi_angel、金志峰朋友的分享,生成的例程中,给出一种解决方法:
将syscall.c放入SW4STM32文件夹下(与startup_stm32f407xx.s同根目录);
Properties -> C/C++ Build -> Settings -> MCU GCC Linker -> Miscellaneous -> Linker flags后面增加-u _printf_float标志
main.c中用户代码区增加
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
wansaiyon
赞同来自:
从Linker flags:-specs=nosys.specs -specs=nano.specs -u _p
修改为Linker flags:-specs=nosys.specs -specs=nano.specs -u _p -u _printf_float
wansaiyon
赞同来自:
后面使用过程中又出现了问题,这个帖子应该参考F767ZI UART_PRINTF的例程,将syscall.c除了放在与startup_stm32f407xx.s同根目录外还要放在SW4STM32文件夹下