1. SOEM代码中好多编译标准貌似是GNU标准,比如匿名UNION,数组大小不能开为0等等
2. 需要在Option->C/C++里的编译选项中加上“--gnu”
3. 这样会导致printf函数重定向时进的是__io_putchar函数,而这个函数目前没有找到,因此代码里没有定义,如果这里不改的话会卡死在printf函数。
4. 为了省事,暂时先将条件编译中非GNU的代码复制一下,虽然我不确定这样是否有其他问题
#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)
//comment_20190422: soem needs --gnu compile option,
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#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;
}
5. 改完确实printf好用了
关于GCC和GNU编译选项在printf打印串口数据中的区别,可以参考 http://blog.sina.com.cn/s/blog_dc9540b00102xd1d.html
思路都是串口重定向。