From ed3f82222d567927cf20fa2d804d230fb5b5a715 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 16 Jul 2025 17:33:10 +0200 Subject: [PATCH] Uart setup --- main.c | 42 +++++++++++++++++++++++++++++++++--------- uart.c | 33 +++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index cf64bcd..8899a99 100644 --- a/main.c +++ b/main.c @@ -1,38 +1,62 @@ +#include "system.h" +#include "uart.h" #include #include #include #include #include +#include #include -#include "system.h" +#include static void gpio_setup(void) { - rcc_periph_clock_enable(RCC_GPIOC); gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + gpio_set(GPIOC, GPIO12); + + /* 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]); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_periph_clock_enable(RCC_GPIOC); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_periph_clock_enable(RCC_GPIOA); + rcc_periph_clock_enable(RCC_AFIO); + rcc_periph_clock_enable(RCC_USART1); + + rcc_periph_clock_enable(RCC_GPIOC); } int main(void) { - rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); - // rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSI_48MHZ]); + clock_setup(); gpio_setup(); 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"); gpio_clear(GPIOC, GPIO13); gpio_set(GPIOC, GPIO13); - // gpio_set(GPIOB, GPIO6); /* LED2 off */ - - // ticks = 0; uint64_t init = sys_ticks_get(); while (sys_ticks_get() == init); - /* We call this handler every 1ms so 1000ms = 1s on/off. */ uint64_t ref = 0; while (1) { if (sys_ticks_get() > ref) { ref += 500; gpio_toggle(GPIOC, GPIO13); - // while (sys_ticks_get() % 1000 == 0); } } diff --git a/uart.c b/uart.c index 7267a80..30dca19 100644 --- a/uart.c +++ b/uart.c @@ -1,20 +1,37 @@ -#include -#include -#include #include "uart.h" +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); void usart_setup(void) { - rcc_periph_clock_enable(RCC_GPIOA); - rcc_periph_clock_enable(RCC_USART1); - - // Set PA9 (TX) to alternate function, push-pull + /* 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); - usart_set_mode(USART1, USART_MODE_TX); usart_set_parity(USART1, USART_PARITY_NONE); 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; + } + + errno = EIO; + return -1; +}