stm32 c,将原型导出到项目的其余部分

stm32 c, export prototype to rest of project

我正在 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/01/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);

    }
}

我已经尝试在其他地方放置代码的两部分,但这是唯一一个我没有收到警告或错误的地方

我很确定这里有问题,否则你不会问这个问题,但我认为如果你将下面的代码剪切并粘贴到你的 uart.h 中,那应该可以工作。

#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__ */

不要忘记在您调用 printf

的任何地方包含 uart.h

如果您在项目中使用 Newlib libc(STM32Cube 生成),您只需要在项目中的任何位置实施 int __io_putchar(int ch)(main.c,如果您愿意),此实施用于将字符输出到标准输出(通过 printf)。

注意 extern int __io_putchar(int ch) __attribute__((weak)); 在 syscalls.c 中是弱链接的。

在编译过程中只使用函数的原型,在项目链接过程中使用用户定义的函数。

在整个项目中包含 uart.h 并不是一个完美的解决方案。确保包含其余的 Newlib 文件。