【问题标题】:stm32 c, export prototype to rest of projectstm32 c,将原型导出到项目的其余部分
【发布时间】:2017-06-04 10:39:52
【问题描述】:

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

    }
}

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

【问题讨论】:

    标签: c stm32 stm32f4 coocox


    【解决方案1】:

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

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

    编译时只使用函数的原型,链接工程时使用用户自定义函数。

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

    【讨论】:

      【解决方案2】:

      我很确定有一个问题,否则你不会问这个问题,但我认为如果你将下面的代码剪切并粘贴到你的 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

      【讨论】:

      • 我已经编辑了主要帖子作为回应。简报:我已经将代码放在 usart.h 和声明在 usart.c 我已经能够构建没有错误或警告,但它不会打印任何内容。
      • 我猜你在SysIniCfg 中调用MX_USART1_UART_Init。在本教程的第 3 节中,他们向您展示了使用 sprintf 发送数据的第二种方法。你可以试试这个并检查它是否有效。如果它有效,您需要进入您的 printf 函数以了解为什么它没有输出。
      猜你喜欢
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      相关资源
      最近更新 更多