Home > Blockchain >  Stm32 Log Messages
Stm32 Log Messages

Time:06-28

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

Actually, I want to write a general Log() function and indicate the cases where the code fails or works as a message using this function.

CodePudding user response:

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;
}

CodePudding user response:

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.

  • Related