STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
log.c
Go to the documentation of this file.
1 
25 /* Includes ------------------------------------------------------------------*/
26 #include "log.h"
27 
28 #include "board.h"
29 #include "error_handler.h"
30 #include "rcc.h"
31 
32 #include <stdarg.h>
33 #include <stdio.h>
34 
35 /* Private variables ---------------------------------------------------------*/
37 static UART_HandleTypeDef vcpUart;
38 
39 /* Functions -----------------------------------------------------------------*/
40 void LogPrint(const char* format, ...)
41 {
43  static uint8_t logBuffer[VCP_UART_BUFFER_SIZE] = {0};
44 
45  // Message: The standard header file <stdarg.h> shall not be used
46  // [misra-c2012-17.1]
47  // Reason: This function provides a flexible, printf-style method to
48  // print log messages over the UART interface. The
49  // implementation relies on the variadic functions.
50  // Risk: The Standard lists many instances of undefined behavior
51  // associated with the features of <stdarg.h>, including:
52  // - va_end not being used prior to end of a function in which
53  // va_start was used
54  // - va_arg being used in different functions on the same
55  // va_list
56  // - the type of an argument not being compatible with the
57  // type specified to va_arg
58  // Prevention: Code reviews and testing.
59  va_list arg; // cppcheck-suppress [misra-c2012-17.1]
60  va_start(arg, format); // cppcheck-suppress [misra-c2012-17.1]
61 
62  const int32_t length =
63  // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
64  vsnprintf((char*)logBuffer, VCP_UART_BUFFER_SIZE,
65  format, // NOLINT(clang-diagnostic-format-nonliteral)
66  arg);
67 
68  va_end(arg); // cppcheck-suppress [misra-c2012-17.1]
69 
70  if (length > 0)
71  {
72  (void)HAL_UART_Transmit(&vcpUart, (uint8_t*)logBuffer, (uint16_t)length,
74  }
75 }
76 
77 void LogInit(void)
78 {
80 
81  vcpUart.Instance = VCP_UART_INSTANCE;
82  vcpUart.Init.BaudRate = VCP_UART_BAUDRATE;
83  vcpUart.Init.WordLength = UART_WORDLENGTH_8B;
84  vcpUart.Init.StopBits = UART_STOPBITS_1;
85  vcpUart.Init.Parity = UART_PARITY_NONE;
86  vcpUart.Init.Mode = UART_MODE_TX_RX;
87  vcpUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
88  vcpUart.Init.OverSampling = UART_OVERSAMPLING_16;
89  vcpUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
90  vcpUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
91 
92  if (HAL_UART_Init(&vcpUart) != HAL_OK)
93  {
94  ErrorHandler();
95  }
96 }
97 
98 void LogDeInit(void)
99 {
100  (void)HAL_UART_DeInit(&vcpUart);
101 
102  RccDisableUart2();
103 }
104 
This file contains the board-specific GPIO pin definitions, peripheral configurations,...
This file contains the error handling function prototypes.
void ErrorHandler(void)
This function is executed in case of error occurrence.
Definition: error_handler.c:32
#define VCP_UART_BUFFER_SIZE
Definition: board.h:150
#define VCP_UART_TX_TIMEOUT
Definition: board.h:152
#define VCP_UART_BAUDRATE
Definition: board.h:159
#define VCP_UART_INSTANCE
Definition: board.h:156
void LogDeInit(void)
De-initialize the UART peripheral used for logging.
Definition: log.c:98
void LogInit(void)
Initialize the UART peripheral used for logging.
Definition: log.c:77
void LogPrint(const char *format,...)
Print a formatted log message over the UART interface.
Definition: log.c:40
static UART_HandleTypeDef vcpUart
Definition: log.c:37
void RccEnableUart2(void)
Enable the USART2 peripheral clock.
Definition: rcc.c:132
void RccDisableUart2(void)
Disable the USART2 peripheral clock.
Definition: rcc.c:137
This file contains the UART-based logging function prototypes for the debug output.
This file contains the RCC (Reset and Clock Control) driver function prototypes for enabling and disa...