Skip to main content
MCUpwr
Associate II
August 6, 2023
Solved

UART logger enable

  • August 6, 2023
  • 4 replies
  • 2579 views

I need help with USART LOGGER _ON enabling. How to enable logger to send serial data via usart?

I have in logger.h  - #define LOGGER_ON 1, also in logger.c 
#if (USE_LOGGER == LOGGER_ON)
.....
#endif /* #if USE_LOGGER == LOGGER_ON */

and platform.h

How to "USE_LOGGER" flag set to 1 (enabling logger) in main.c, where to put  USE_LOGGER = LOGGER_ON? Is it enabled via Cube_MX?

This topic has been closed for replies.
Best answer by RhSilicon

In this case, if you have the complete project, from where these files were obtained: there is a function in STM32CubeIDE that "Opens the declaration" (keyboard shortcut: CTRL+Mouse click on the word). Or you can also click once on the word to select it and press the F3 key.

STM32_IF.png

But if you don't have the complete project, you can put the declarations in any file that this file has access to, maybe in the file itself, or in main.h for example.

#define USE_LOGGER 1
#define LOGGER_ON 1

Ref.: https://fastbitlab.com/microcontroller-embedded-c-programming-lecture-182-conditional-compilation-directives/

4 replies

RhSilicon
Lead
August 7, 2023

On STM32F407VGT6 I use these routines in the main.c file:

#include <stdio.h>

/* USER CODE BEGIN 4 */
int __io_putchar(int ch) {
uint8_t c[1];
c[0] = ch & 0x00FF;
HAL_UART_Transmit(&huart1, &*c, 1, 10);
return ch;
}

int _write(int file, char *ptr, int len) {
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++) {
__io_putchar(*ptr++);
}
return len;
}

And these routines in the main.h file:

/**/
#ifndef UsrLog
#define UsrLog(...) do { \
printf(__VA_ARGS__); \
printf("\n"); \
} while (0)
#endif
/* USER CODE END EM */

To send a message:

UsrLog("Battery: error; tick: %lu", HAL_GetTick());

I don't know if it can help in your case, but I found these videos about advanced debugging:

https://youtube.com/playlist?list=PLnMKNibPkDnEDEsV7IBXNvg7oNn3MfRd6

MCUpwr
MCUpwrAuthor
Associate II
August 7, 2023

Thanks for your tip (it will be useful to me), but  I have problem with enabling logger to send a message via this function, when program reaches this function, USE _LOGGER is 0 (LOGGER_OFF) instead of 1 (LOGGER_ON), so routine just jump to Return0 without message send (code below is also highlighted in  IDE like inactive).

 

 

uint8_t logUsartTx(uint8_t *data, uint16_t dataLen)
{
 if(pLogUsart == 0)
 return ERR_INVALID_HANDLE;
 #if (USE_LOGGER == LOGGER_ON)
 {
 return HAL_UART_Transmit(pLogUsart, data, dataLen, USART_TIMEOUT);
 }
 #else
 {
 return 0;
 }
 #endif
}

 

 

 

 

RhSilicon
RhSiliconBest answer
Lead
August 7, 2023

In this case, if you have the complete project, from where these files were obtained: there is a function in STM32CubeIDE that "Opens the declaration" (keyboard shortcut: CTRL+Mouse click on the word). Or you can also click once on the word to select it and press the F3 key.

STM32_IF.png

But if you don't have the complete project, you can put the declarations in any file that this file has access to, maybe in the file itself, or in main.h for example.

#define USE_LOGGER 1
#define LOGGER_ON 1

Ref.: https://fastbitlab.com/microcontroller-embedded-c-programming-lecture-182-conditional-compilation-directives/

MCUpwr
MCUpwrAuthor
Associate II
August 10, 2023

Thanks, your tip help me to find out that USE_LOGGER wasn't never defined :thumbs_up: