请问Segger Embedded Studio里printf要怎样才能在单片机的UART输出?
挺无语的,将keil工程移植到SES,其它功能都正常,但是printf时,它会在调试时输出到ide里的终端窗口,不能在MCU上输出,即时不使用debug,直接下载到flash,MCU的IO也无输出{:cry:}几个和串口有关的函数(程序肯定没有问题,Keil中一切正常):
#define USARTx USART2
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define UART_DISPLAY int __io_putchar(int ch)
#else
#define UART_DISPLAY int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USARTx_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USARTx;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/* USER CODE BEGIN 4 */
/**
* @briefRetargets the C library printf function to the USART.
* @paramNone
* @retval None
*/
UART_DISPLAY // printf重定向于USART2,需要添加此函数
{
/* 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;
}
/* USER CODE END 4 */
如图所示:
使用printf的信息,在终端窗口显示;直接调用HAL,串口有输出。
我从来不用自带的printf,一直用这个http://elm-chan.org/fsw/strf/xprintf.html 估计是你这句:#ifdef __GNUC__在后来的环境里没有生效,也就是没有定义 __GNUC__,导致串口重新定向根本就没有被编译进去,也就无法执行了。
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define UART_DISPLAY int __io_putchar(int ch)
#else
#define UART_DISPLAY int fputc(int ch, FILE *f)
#endif /* __GNUC__ */ 选项页面的usemicrolib打钩,或者再写几句,采用半主机模式打印。具体方法搜printf半主机模式 jianfengxixi 发表于 2024-3-20 19:20
我从来不用自带的printf,一直用这个http://elm-chan.org/fsw/strf/xprintf.html
(引用自3楼)
要解决printf的问题,不是自己的应用,是为了解决客户的问题。
原来客户开发的程序,网上很多例子,以及ST的例子,本身是用的ptintf,要直接方便的使用。 Segger Embedded Studio里原生支持RTT,所以很方便的。 pspice 发表于 2024-3-21 07:56
估计是你这句:#ifdef __GNUC__在后来的环境里没有生效,也就是没有定义 __GNUC__,导致串口重新定向根本就 ...
(引用自4楼)
不是这个原因。
1 在调试的时候,显示__GNUC__这一段是亮的,下面的几行是灰色的:
2 直接注释其它行,仍然是一样的效果:
//#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define UART_DISPLAY int __io_putchar(int ch)
//#else
//#define UART_DISPLAY int fputc(int ch, FILE *f)
//#endif /* __GNUC__ */ why800 发表于 2024-3-21 08:11
选项页面的usemicrolib打钩,或者再写几句,采用半主机模式打印。具体方法搜printf半主机模式 ...
(引用自5楼)
我们是为客户做FAE,不是自己写程序。客户原来的程序不能改。 yuyu87 发表于 2024-3-21 08:43
Segger Embedded Studio里原生支持RTT,所以很方便的。
(引用自7楼)
是的,可以在Library->Library I/O设为RTT,在调试的时候是方便,可脱机的时候,要的是在MCU的IO上输出。现成所有使用printf语句的应用,都在IO上输出不了 why800 发表于 2024-3-21 08:11
选项页面的usemicrolib打钩,或者再写几句,采用半主机模式打印。具体方法搜printf半主机模式 ...
(引用自5楼)
这是Keil里的用法,SES没有use microlib的选项 #define UART_DISPLAY int __io_putchar(int ch)
#define UART_DISPLAY int fputc(int ch, FILE *f)
这两个分别保留一个试试呢? 看上去没有简单方法,要修改代码:
https://wiki.segger.com/Embedded_Studio_Library_IO#STD pspice 发表于 2024-3-21 09:37
#define UART_DISPLAY int __io_putchar(int ch)
#define UART_DISPLAY int fputc(int ch, FILE *f)
这两个 ...
(引用自12楼)
都试过了,不行。
页:
[1]