STM32 C++ 和将 std::cout 重定向到 UART

问题描述 投票:0回答:2

我在尝试使用 STM32CubeIDE(通常是 STM32CubeIDE 的标准安装)在 STM32 上运行 std::cout 时遇到问题。

我已经查阅了许多有关出于 stdio.h 和 printf 目的而重定向 UART 的资料,但我正在尝试使用 std::cout 在 C++ 环境中实现这一切。 我找到的主要来源在这里:https://www.keil.com/support/man/docs/armlib/armlib_chr1358938931411.htm

根据包含标题的方式和时间,我会收到不同的错误,这是我尝试过的:

重定向.h:

#ifndef _RETARGET_H__
#define _RETARGET_H__

#include "stm32f1xx_hal.h"
#include <sys/stat.h>
#include <stdio.h>

void RetargetInit(UART_HandleTypeDef *huart);

int _isatty(int fd);
int _write(int fd, char* ptr, int len);
int _close(int fd);
int _lseek(int fd, int ptr, int dir);
int _read(int fd, char* ptr, int len);
int _fstat(int fd, struct stat* st);


namespace std {

int fputc(int, FILE *);

}

#endif //#ifndef _RETARGET_H__

retarget.cc(删减了一点)[更正这是一个 C++ 文件]

void RetargetInit(UART_HandleTypeDef *huart) {
  gHuart = huart;

  /* Disable I/O buffering for STDOUT stream, so that
   * chars are sent out as soon as they are printed. */
  setvbuf(stdout, NULL, _IONBF, 0);
}

int _write(int fd, char* ptr, int len) {
  HAL_StatusTypeDef hstatus;

  if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
    hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);
    if (hstatus == HAL_OK)
      return len;
    else
      return EIO;
  }
  errno = EBADF;
  return -1;
}

namespace std {

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;
FILE __stdin;
FILE __stderr;


int fputc(int c, FILE *stream)
{
      char tOut = c;

      return _write(STDOUT_FILENO, &tOut, 1);

  /* Your implementation of fputc(). */
}

}

和 main.cpp(也被剪掉了一点):

#include "retarget.h"
#include <iostream>

int main(void)
{
  /* HAL Init stuff Clipped */
  RetargetInit(&huart1);
  std::cout << "\n\nSTM32 main.c Startup\n" << std::endl;

  while(1){
      std::cout << "*";

      HAL_Delay(1000);
  }
}

如果我使用 printf(将 std::cout 更改为 printf),则一切正常,因此 _write 函数可以正确发送到 UART,所以我知道一切正常。

现在,来看看错误。

如所呈现的,编译器抛出:

In file included from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ext\string_conversions.h:43,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\basic_string.h:6557,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\string:55,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\locale_classes.h:40,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\ios_base.h:41,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ios:42,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ostream:38,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\iostream:39,
                 from ../Core/Src/main.cc:26:
c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\cstdio:111:11: error: 'int fputc(int, FILE*)' conflicts with a previous declaration
  111 |   using ::fputc;
      |           ^~~~~
In file included from ../Core/Src/main.cc:25:
../Core/Inc/retarget.h:23:5: note: previous declaration 'int std::fputc(int, FILE*)'
   23 | int fputc(int, FILE *);
      |     ^~~~~
make: *** [Core/Src/subdir.mk:41: Core/Src/main.o] Error 1 

如果我翻转 main.cc 文件中的包含内容,以便首先拉入 iostream,我会得到:

In file included from ../Core/Src/main.cc:26:
../Core/Inc/retarget.h:23:22: error: 'int std::fputc(int, FILE*)' conflicts with a previous declaration
   23 | int fputc(int, FILE *);
      |                      ^
In file included from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\cstdio:42,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ext\string_conversions.h:43,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\basic_string.h:6557,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\string:55,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\locale_classes.h:40,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\bits\ios_base.h:41,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ios:42,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\ostream:38,
                 from c:{stm32 tools path snipped}arm-none-eabi\include\c++\10.3.1\iostream:39,
                 from ../Core/Src/main.cc:25:
c:{stm32 tools path snipped}arm-none-eabi\include\stdio.h:214:5: note: previous declaration 'int fputc(int, FILE*)'
  214 | int fputc (int, FILE *);
      |     ^~~~~
make: *** [Core/Src/subdir.mk:41: Core/Src/main.o] Error 1

有什么建议吗? 预先感谢。

c++ arm stm32 iostream cout
2个回答
0
投票

终于偶然发现了解决方案,归结为

_write()
函数是如何编译的。 这个函数必须用C编译器编译才能正确工作(据我所知)。

所以,解决方案,就我而言:

我将

retarget.cc
重命名为
retarget.c
(它仍然是
retarget.c
,除了
retarget.h
的包含路径之外未修改)。

对于

retarget.h
,我使用了
retarget.h
的关联文件,将函数原型包装在 extern "C":"

#ifndef _RETARGET_H__
#define _RETARGET_H__

#include "stm32f1xx_hal.h"
#include <sys/stat.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C"
{
#endif

void RetargetInit(UART_HandleTypeDef *huart);

int _isatty(int fd);
int _write(int fd, char* ptr, int len);
int _close(int fd);
int _lseek(int fd, int ptr, int dir);
int _read(int fd, char* ptr, int len);
int _fstat(int fd, struct stat* st);

#ifdef __cplusplus
} //extern "C"
#endif

#endif //#ifndef _RETARGET_H__

现在一切都按预期进行 -

std::cout << "Working now!" << std::endl;


0
投票

std 流缓冲区函数(如 _write())的重定向会影响其他流缓冲区处理,例如如果您在项目的其他地方使用 ostraem...

因此我的方法是:创建一个单独的流缓冲区类,其中根据您的需要处理写入事件函数。当构造函数被调用时,转发要修改的ostream并覆盖它的流缓冲区。缺点是原始流缓冲区保留为未使用的静态内存分配。

template <class Elem = char, class Tr = std::char_traits<Elem> >
    class redirectostream : public std::basic_streambuf<Elem, Tr>
{
public:
    redirectostream(std::ostream &streamToChange)
    {
        //redirect the ostream
        streamToChange.rdbuf(this);
    };

    std::streamsize xsputn(const Elem *_Ptr, std::streamsize _Count)
    {
        HAL_StatusTypeDef status = HAL_UART_Transmit_IT(&huart1, _Ptr, _Count);
        return HAL_OK == status ? _Count: 0;
    }
};

//redirect std::cout
redirectostream<> redirectCoutObj(std::cout);

UART 初始化后,您可以按照习惯使用 std:cout。

#include <iostream>

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_USART2_UART_Init();

    std::cout << "Hallo World!";

    while (1)
    {
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
        HAL_Delay(500);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.