Preface

When it comes to programming the STM32 MCU for OBC, it is often helpful to have a console like print out the quickly debug. From experience, I find the built in debuggers can be clunky to use and print statements are often faster to get critical information to understand how a program is operating. Thankfully, OBC has been designed with a JTAG interface which is used for flashing the STM32 MCU and also provides us with an integrated UART interface (OBC design specific) we can use for communications. Unfortunately, due to the nature of programming on an embedded system, there are often fewer convenient function calls when compared to programming on an OS. As such, we have to do a bit of set up to create an environment for proper communication.

Set up

Pinout

  1. In your STM32CubeIDE, locate your project and the associated .ioc file.
  2. Locate the PB6 and PB7 pins close to the top left of the STM32G431RBTx pinout view
  3. Set their function to be USART1_TX and USART1_RX respectively. Next, under the Categories → Connectivity → USART1, select Asynchronous for the Mode.

Code

  1. (This step can be skipped if you wish to only pass hardcoded strings): In your program, locate areas which you want to have print statement, and define either

    1. one global character array so all print statements can use the same array
    2. local character arrays wherever the print statements will be
    char msg[100];
    
  2. In the locations where you want to the print statement, set the content of the character array to be the output you desire using the sprintf function

    int sprintf(char *str, const char *format, ...);
    // example
    sprintf(msg, "test output: %x\\n", test_val);
    
  3. Afterwards, send the ‘string’ to the UART using the HAL_UART_Transmit

    HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout);
    // example
    HAL_UART_Transmit(&huart1, msg, strlen(msg), 100);
    HAL_UART_Transmit(&huart1, "test function", 14, 100);
    // note, as with any character array, make sure to add 1 to the 'length' to account for the null space character at the end
    

Console

  1. On macOS and Linux, locate the port the STM32 MCU is connected through by running ls

    ls /dev/tty.*
    ls /dev/ttyUSB*
    
    /dev/tty.usbserial-XXXXX
    /dev/tty.usbmodem11303
    /dev/ttyUSB0
    
  2. Open a continuous stream of the incoming data using the screen command

    screen -L <COM port> <baud rate>
    
    screen -L /dev/tty.usbmodem11303 115200