hpm5100_uart_echo.ino

HPMicro HPM5100: 400 MHz RISC-V MCU with Integrated Analog Peripherals · File : hpm5100_uart_echo.ino · Language : C · Use case : Implémenter une boucle d'écho série pour le débogage via l'interface UART.

Configures UART0 at 115200 bauds and echoes each received character back on the same line.

Requires a USB-to-UART serial connection to the TX/RX pins of UART0. The code uses simple polling on the RX FIFO. Useful for testing serial communication before integrating complex protocols like CAN FD or USB.

hpm5100_uart_echo.ino

#include "board.h"
#include "hpm_uart_drv.h"
#include "hpm_clock_drv.h"

#define UART_PORT HPM_UART0
#define UART_BAUDRATE 115200

void init_uart(void)
{
    uart_config_t uart_config;

    clock_add_to_group(clock_uart0, 0);
    
    uart_get_default_config(&uart_config);
    uart_config.baudrate = UART_BAUDRATE;
    uart_config.data_bits = uart_data_bits_8;
    uart_config.stop_bits = uart_stop_bits_1;
    uart_config.parity = uart_parity_none;
    uart_init(UART_PORT, &uart_config);
}

void main(void)
{
    uint8_t rx_data;
    board_init();
    init_uart();

    while (1) {
        if (uart_get_rx_fifo_status(UART_PORT, uart_rx_fifo_not_empty)) {
            rx_data = uart_read_byte(UART_PORT);
            uart_write_byte(UART_PORT, rx_data);
        }
    }
}

← Back to the board