cancel
Showing results for 
Search instead for 
Did you mean: 

UART logger enable

MCUpwr
Associate III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
RhSilicon
Lead

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/

View solution in original post

4 REPLIES 4
RhSilicon
Lead

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

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
Lead

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/

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