diff options
Diffstat (limited to '02-usart/src/main.c')
-rw-r--r-- | 02-usart/src/main.c | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/02-usart/src/main.c b/02-usart/src/main.c index 73ccb17..72b3fe7 100644 --- a/02-usart/src/main.c +++ b/02-usart/src/main.c @@ -3,19 +3,13 @@ #include "core/clock.h" #include "core/dma.h" #include "core/gpio.h" -#include "core/isr_vector.h" #include "core/system.h" #include "core/usart.h" - #include "delay.h" -#include "lib.h" #include "mem.h" #include "spin.h" #include "string.h" - -#ifdef ARCH_STM32L4 - /** Overrides the default systick irq handler. */ void on_systick() { @@ -33,12 +27,42 @@ void on_systick() is_on = ! is_on; } +void setup_usart2(uint32_t baud_rate) +{ + __IO gpio_port_t* port_a = enable_gpio(GPIO_PORT_A); + enable_hsi(&RCC, true); + + set_usart2_clock_src(&RCC, USART_CLK_SRC_HSI16); + set_usart2_clock_enabled(&RCC, USART_CLK_SRC_HSI16); + + set_gpio_pin_mode(port_a, PIN_2, MODE_ALTERNATE); + set_gpio_pin_mode(port_a, PIN_15, MODE_ALTERNATE); + set_gpio_alternate_function(port_a, PIN_2, AFN_7); + set_gpio_alternate_function(port_a, PIN_15, AFN_3); + + /* De-assert reset of USART2 */ + regset(RCC.apb1rst1_r, rcc_usart2rst, 0); + + USART2.c_r1 = 0; + USART2.c_r2 = 0; + USART2.c_r3 = 0; + + usart_set_divisor(&USART2, 16000000 / baud_rate); +} + +#ifdef ARCH_STM32L4 + /* Main function. This gets executed from the interrupt vector defined above. */ int main() { /* Enable a higher clock frequency. */ set_system_clock_MHz(80); + setup_usart2(115200); + usart_set_enabled(&USART2, USART_ENABLE_TX | USART_ENABLE_RX); + + usart_printf(&USART2, "Start Configuring Countdown!\n"); + /* Set the countdown to start from 1,000,0000. */ SCB.strv_r = 10000000; @@ -47,6 +71,8 @@ int main() /* Start the systick. */ SCB.stcs_bf.enable = 1; + + usart_printf(&USART2, "Start Countdown Started!\n"); } #endif |