hpm5100_uart_printf.ino
Configuration of UART0 for printf display on PC.
Configures UART0 at 115200 bauds. Implements _write to redirect stdout. Use USB-UART converter connected to UART0 TX/RX pins. Ensure uart0 clock group is enabled.
hpm5100_uart_printf.ino
#include "board.h"
#include "hpm_uart_drv.h"
#include <stdio.h>
#define UART_INSTANCE HPM_UART0
#define UART_BAUDRATE 115200
void uart_init(void) {
clock_add_to_group(clock_uart0, 0);
uart_config_t config;
uart_default_config(&config);
config.baudrate = UART_BAUDRATE;
config.parity = uart_parity_none;
config.stop_bits = uart_stop_bits_1;
config.data_bits = uart_data_bits_8;
uart_init(UART_INSTANCE, &config);
uart_enable_rx_dma(UART_INSTANCE, false);
uart_enable_tx_dma(UART_INSTANCE, false);
}
int _write(int file, char *ptr, int len) {
for (int i = 0; i < len; i++) {
while (!uart_is_tx_ready(UART_INSTANCE));
uart_write_char(UART_INSTANCE, ptr[i]);
}
return len;
}
int main(void) {
board_init();
uart_init();
while (1) {
printf("HPM5100 Uptime: %lu ms\n", mchtmr_get_count(HPM_MCHTMR) / (clock_get_frequency(clock_cpu0)/1000));
delay_ms(1000);
}
}