STM32日志消息

发布于 2025-02-10 23:13:25 字数 196 浏览 2 评论 0原文

美好的一天,我想使用STM32 Nucleo Board编写内容丰富的日志消息。 例如,我想显示程序在此期间启动时使用日志(“程序启动”)函数启动的消息,或者我想通过使用日志将错误消息传递给屏幕上(“”程序失败')函数当程序失败时。 我使用C编程langue和STM32Cubeide& Nucleo F207zg董事会提前感谢您的帮助,

感谢我解决了问题

good day, stm32 nucleo board I want to write informative Log messages using.
For example, I want to display the message that the program has started by using the Log("Program started") function when the program starts during this period, or I want to give the error message to the screen by using the Log("Program Failed') function when the program fails.
I using C programming langue and stm32cubeide & Nucleo f207zg board thanks in advance for your help

Thanks I solved the problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2025-02-17 23:13:25

鉴于:

  • 您知道UART/USART外围设备是什么是
  • 将正确的引脚连接到串行接口,
  • 将接口的另一端(例如FTDI芯片)连接到计算机,

您可以使用printf <printf < /代码>就像您在C中所做的那样。为什么?因为printf已经为您照顾了字符串的格式,没有人愿意重新进来,然后将printf输出重定向到处理低级序列协议的UART外围。

以下本指南这些步骤这些步骤非常容易:

添加#include&lt; stdio.h&gt;到主顶部的包含部分。C

#include "stdio.h"

复制并将以下代码粘贴到main.c文件之前,在main()函数之前,但在UART句号声明之后

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
  HAL_UART_Transmit(&huart?, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

Given that:

  • You know what the UART/USART peripheral is
  • You connected the right pins to a serial interface
  • You connected the other end of the interface (for instance an FTDI chip) to your computer

You can implement the logging with printf as you would do in C. Why? Because printf already takes care for you the formatting of the strings, which no one wants to reimplement, and then you redirect the printf output to the UART peripheral that handles low level serial protocol.

Following this guide the steps are easy enough:

Add #include <stdio.h> to the Includes section at the top of the main.c

#include "stdio.h"

Copy and paste the following code into the main.c file before the main() function but after the UART handle declaration

Note that you need to replace the question mark with the actual UART handle declaration, for instance huart1

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
  HAL_UART_Transmit(&huart?, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}
浪菊怪哟 2025-02-17 23:13:25

正如FRA93指出的那样,您需要实现UART/USART协议通信并将其打印到那里。我正在写这个答复,因为我感觉到您有一些基本的经验编写桌面应用程序,但没有嵌入,因此我会澄清几点。

无需打印控制台。没有日志。他们根本不存在。。不在桌面应用程序开发意义上。

通常这样做的是,MCU的UART之一用于将字符打印到ST-Link上,该字符将其转换为USB(或者可以是FTDI IC,CP2102或其他USB-UART BRIDGES/CONVENTERS)。因此,您的MCU将通过UART发送字符,

如果您从空项目开始并且没有使用的外部库,则USB-UART转换器将在COM端口终端显示它们(像Putty这样的程序),您将需要3打代码。一个人只是为了打印东西。最初,您什么都没有。您必须初始化UART,配置它,然后实现其处理字符,数字的方式。您需要从字面上写一个基本的UART驱动程序。您可以随心所欲地调用打印功能。您可以称其为log(“ Hello World”),您可以称其为窃窃私语(“嗨,那里”)。因为您实际上会创建此功能。你是老板。

现在,鉴于您对所有这些都不熟悉,这听起来可能是一项艰巨的任务。最肯定的是。从头开始写司机不是您想做的事情(除非您在第1天喜欢这种事情)。

我强烈建议您获取一些图书馆,例如由stmicroelectronics通过HAL,并熟悉它可以做的事情 - 包括如何与UART打印东西。互联网上充斥着有关如何与HAL一起使用UART的文章和视频,这实际上是要实施的第一件事。特别是出于原因,它像日志一样工作并简化了进一步的开发/学习。

因此,为了使“日志”有效,我会做以下操作:

  1. 让您的特定MCU熟悉
  2. UART熟悉自己 - 基本的内容,内容是关于它是什么以及它的工作原理。无需成为专家,只需了解什么。
  3. 熟悉如何使用
  4. Nucleo Board的PCB示意图将UART与HAL一起使用,找出MCU的UART与ST-Link连接到ST-Link(我检查了示意图:是USART3)
  5. 获取串行端口(COM)终端程序对于您的计算机,
  6. 使用HAL来初始化USART3并打印到USART3。无论您在那里打印什么,终端都可以看到。是的,您不会在IDE中看到任何东西。您需要一个单独的程序来显示您通过USB COM端口收到的内容。

最后,您的打印功能将是FRA93提出的功能。 HAL_UART_TRANSMIT是通过UART发送内容的最基本的HAL功能。如果您对使用“日志”作为您调用的函数非常挑剔,以通过UART打印,则可以始终将HAL_UART_TRANSMIT包装到自己的函数中,称为日志。

As pointed out by Fra93, you need to implement UART/USART protocol communication and print stuff to there. I'm writing this reply, because I'm getting a feeling you have some basic experience writing desktop applications, but not embedded, so I will clarify a few points.

There is no console to print to. There are no logs. They simply don't exist. At all. Not in a desktop app development sense.

What's usually done is that one of the UARTs of the MCU is used to print characters to the ST-Link, which converts it to USB (alternatively, can be FTDI IC, CP2102 or other USB-UART bridges/converters). So then your MCU will send out characters over UART, and the USB-UART converter will display them in a COM port terminal (programs like PuTTy)

If you start with empty project and no external libraries used, you will need 3 dozen lines of code alone just to be able to print stuff. Initially, you have nothing. You have to initialize UART, configure it, then implement how it handles characters, numbers. You need to literally write a basic UART driver. And you can call printing function whatever you want. You can call it Log("hello world"), you can call it Whisper("hi there"). Because you will actually create this function. You're the boss.

Now, given you are not very familiar with all of this, it may sound like a difficult task. And it most certainly is. Writing a driver from scratch on your day 1 is not a thing you want to do (unless you're into that kind of stuff on day 1).

I would strongly advise you to get some library, such as HAL by STMicroelectronics, and familiarize yourself with what it can do - including how to print stuff with UART. Internet is flooded with articles and videos of how to use UART with HAL, it's literally one of the first things to be implemented. Specifically for the reason, that it works like logs and simplifies further development/learning.

So, in order to get "logs" working, I would do the following:

  1. Get HAL for your specific MCU
  2. Familiarize yourself with UART - basic stuff about what it is and how it works. No need to be an expert, just learn what it is.
  3. Familiarize yourself with how to use UART with HAL
  4. From the schematic of the PCB of your nucleo board, find out which UART of the MCU is connected to ST-Link (I checked the schematic: it's USART3)
  5. Get serial port (COM) terminal program for your computer
  6. Use HAL to initialize USART3 and print to USART3. Whatever you print there will be visible in the terminal. Yes, you will not see anything in the IDE. You need a separate program to display what you receive via USB COM port.

In the end, your print function will be the one proposed by Fra93. HAL_UART_Transmit is the most basic HAL function to send stuff via UART. If you're very picky about using "Log" as a function you call to print via UART, you can always wrap HAL_UART_Transmit into your own function called Log.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文