Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
stm32 c,将原型导出到项目的其余部分_C_Stm32_Stm32f4_Coocox - Fatal编程技术网

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

stm32 c,将原型导出到项目的其余部分,c,stm32,stm32f4,coocox,C,Stm32,Stm32f4,Coocox,我正在stm32f407vg中开发一个小项目(c),并遵循中的UART教程: 我的问题在于功能原型: /* Includes ——————————————————————*/ #include “usart.h” #include “gpio.h” /* Private function prototypes ———————————————–*/ #ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker-&g

我正在stm32f407vg中开发一个小项目(c),并遵循中的UART教程:

我的问题在于功能原型:

/* 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的Putcharu原型:

/* 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){
..
..
}
主要内容如下:

/* 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);

    }
}
/*包括------------------------------------------------------------------*/
#包括“globals.h”
#包括“stm32f4xx_hal.h”
#包括“syscfg.h”
#包括“can.h”
#包括“usart.h”
#包括“gpio.h”
#包括“kernel.h”
#包括
内部主(空){
SysIniCfg();
printf(“Hola”);
而(1){
//核电机();
HAL_GPIO_开关引脚(LED_G_GPIO_端口,LED_G_引脚);
}
}

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

我很确定这是一个陷阱,否则你不会问这个问题,但我认为如果你在
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__ */

如果您在项目中使用Newlib libc(STM32Cube生成),您只需要在项目中的任何地方实现
int\uu io\u putchar(int ch)
printf
,此实现用于将字符输出到标准输出(通过printf)

注意,
extern int\uu io\u putchar(int ch)\uu属性(弱))在syscalls.c中弱链接

在编译期间,仅使用函数的原型,在项目链接期间,使用用户定义的函数


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

我已经编辑了主要帖子作为回应。简介:我把代码放在了usart.h,把声明放在了usart.c,我可以在没有错误和警告的情况下构建,但它不会打印任何内容。我想你可以在
SysIniCfg
中调用
MX\u USART1\u UART\u Init
。在本教程的第3节中,他们向您展示了使用sprintf发送数据的第二种方法。你能试试这个并检查它是否有效吗。如果它能工作,您需要进入
printf
函数,以了解它为什么不输出任何内容。