技术

STM32G070串口问题

为了将串口输出重定位到 printf() 函数上,有两种方式:

第一种,直接重写 fputc()函数,但是需要在MDK上面勾选 MicroLib,否则无法启动。

第二种,添加如下文件代码,此时不需要勾选 MicroLib

#include <stdio.h>
#include "main.h"

//#pragma import(__use_no_semihosting_swi)

extern UART_HandleTypeDef huart1;


struct __FILE {
    int handle;
    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
    /* is required. */
};

FILE __stdout;


int fputc(int ch, FILE *f)
{
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
    return ch;
}

#if 0
int ferror(FILE *f)
{
    /* Your implementation of ferror(). */
    return 0;
}


void _sys_exit(int return_code) 
{
label:
    goto label;  /* endless loop */
}
#endif

Leave a Reply

Your email address will not be published. Required fields are marked *