From 7086e5f9e9a13347524a7929bd13118a7916fa2f Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 16 Jul 2025 18:00:08 +0200 Subject: [PATCH] uart --- main.c | 10 +--------- uart.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index 8899a99..30a3680 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,6 @@ static void gpio_setup(void) { /* Setup GPIO6 and 7 (in GPIO port A) for LED use. */ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); } - static void clock_setup(void) { rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); @@ -37,14 +36,7 @@ int main(void) { sys_tick_setup(); usart_setup(); - usart_send_blocking(USART1, 'H'); - usart_send_blocking(USART1, 'e'); - usart_send_blocking(USART1, 'l'); - usart_send_blocking(USART1, 'l'); - usart_send_blocking(USART1, 'o'); - usart_send_blocking(USART1, '\n'); - - printf("Well hello"); + printf("Printf is working!\n"); gpio_clear(GPIOC, GPIO13); gpio_set(GPIOC, GPIO13); diff --git a/uart.c b/uart.c index 30dca19..369ad15 100644 --- a/uart.c +++ b/uart.c @@ -5,6 +5,7 @@ #include #include #include +#include int _write(int file, char *ptr, int len); @@ -12,7 +13,6 @@ void usart_setup(void) { /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); - /* Setup UART parameters. */ usart_set_baudrate(USART1, 115200); usart_set_databits(USART1, 8); usart_set_stopbits(USART1, USART_STOPBITS_1); @@ -20,18 +20,19 @@ void usart_setup(void) { usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); usart_set_mode(USART1, USART_MODE_TX); - /* Finally enable the USART. */ usart_enable(USART1); } int _write(int file, char *ptr, int len) { - int i; - - if (file == 1) { - for (i = 0; i < len; i++) usart_send_blocking(USART1, ptr[i]); - return i; + if (file == STDOUT_FILENO || file == STDERR_FILENO) { + for (int i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(USART1, '\r'); + } + usart_send_blocking(USART1, ptr[i]); + } + return len; } - errno = EIO; return -1; }