libopencm3 STM32G0B1RE usart_set_baudrate() results in getting stuck in blocking_handler()

58 Views Asked by At

I am trying to setup USART on an STM32G0B1RE NUCLEO dev board using the libopencm3 library. I have looked at a variety of example code, but most is for the STM32F boards, which have different function names/signatures in libopencm3. I then adapted my code to use the STM32G0 signatures. Here is some representative example code, that seems to use the same functions as I am: https://rhye.org/post/stm32-with-opencm3-1-usart-and-printf/

When I call usart_set_baudrate(), I end up stuck in blocking_handler(), which is just an infinite loop. I expect it to set the baudrate and return, then have the rest of my code execute.

Is there some interrupt that defaults to blocking_handler() that I need to write my own handler for?

When using a USB-to-UART cable I do not see any output, whether I call usart_set_baudrate() or not. If I call usart_set_baudrate(), I of course won't see anything for the obvious reason that nothing is being sent because we are stuck in blocking_handler(). When I don't call usart_set_baudrate(), the usart_send('A') call goes through fine, but I don't see anything on GNU screen or minicom.

I tried defining usart1_isr(), but for the body I just wrote return; as I wasn't sure what to do. This did not help.

My code is the following:

  • Send stream of 'A's on USART1
  • Flash an LED
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>

#define RCC_LED1 RCC_GPIOA
#define PORT_LED1 GPIOA
#define PIN_LED1 GPIO5

void usart_setup(void);

int main(void) {
    rcc_clock_setup(&rcc_clock_config[RCC_CLOCK_CONFIG_HSI_16MHZ]);
    
    usart_setup();

    // Setup LED.
    rcc_periph_clock_enable(RCC_LED1);
    gpio_mode_setup(PORT_LED1, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED1);

    while (1) {
        usart_send(USART1, 'A');
        
        // Wait a bit.
        for (int i = 0; i < 800000; i++) {
            __asm__("nop");
        }
        // Flash LED.
        gpio_toggle(PORT_LED1, PIN_LED1);
    }
}

void usart_setup(void) {
    rcc_periph_clock_enable(RCC_GPIOC);
    rcc_periph_clock_enable(RCC_USART1);

    gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO4 | GPIO5);
    gpio_set_af(GPIOC, GPIO_AF1, GPIO4 | GPIO5);

    usart_set_baudrate(USART1, 9600); // problem function
    usart_set_databits(USART1, 8);
    usart_set_stopbits(USART1, USART_STOPBITS_1);
    usart_set_mode(USART1, USART_MODE_TX);
    usart_set_parity(USART1, USART_PARITY_NONE);
    usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);

    usart_enable(USART1);
}
0

There are 0 best solutions below