我正在 stm32f407vg 中开发一个小项目 (c),并遵循以下 UART 教程:
http://letanphuc.net/2015/09/stm32f0-uart-tutorial-5/#comment-346
我的问题是函数原型:
/* Includes ——————————————————————*/
#include “usart.h”
#include “gpio.h”
/* Private function prototypes ———————————————–*/
#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 USART */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
return ch;
}
/* USER CODE END PFP */
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
…
..
..
我必须如何在 usart.h 中进行声明,以便可以在项目的其余部分使用 printf()?
谢谢。
编辑:2017 年 1 月 20 日对纪尧姆·米歇尔的回应
我已经放入了 usart.h
#ifndef __usart_H
#define __usart_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "globals.h"
extern UART_HandleTypeDef huart1;
/* **********************************************
*
* **********************************************/
#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__ */
和 usart.c 中的 PUTCHAR_PROTOTYPE:
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "gpio.h"
#include "string.h"
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
return ch;
}
/* USER CODE END PFP */
//UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void){
..
..
}
在 main.c 中:
/* Includes ------------------------------------------------------------------*/
#include "globals.h"
#include "stm32f4xx_hal.h"
#include "syscfg.h"
#include "can.h"
#include "usart.h"
#include "gpio.h"
#include "kernel.h"
#include <stdio.h>
int main(void){
SysIniCfg();
printf("Hola");
while (1){
//kernelMotor();
HAL_GPIO_TogglePin(LED_G_GPIO_Port,LED_G_Pin);
}
}
我已经尝试在其他地方放置代码的两个部分,但这是我唯一没有收到警告或错误的地方