diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2020-11-24 00:38:09 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2020-11-24 00:53:28 -0700 |
commit | 2478a549b9f64c50310da41c861b8f86fdea2861 (patch) | |
tree | 3a62960f83453f354cbf309ac0722ed2ea6c27c7 /02-usart/src/kern/main.c | |
parent | 9dab2bf91ed3e6af7c7b07590ccc8c3b211a763a (diff) | |
download | stm32l4-2478a549b9f64c50310da41c861b8f86fdea2861.tar.gz stm32l4-2478a549b9f64c50310da41c861b8f86fdea2861.tar.bz2 stm32l4-2478a549b9f64c50310da41c861b8f86fdea2861.zip |
Add new system for startup.
Now instead of init() and main() being responsible for all
initialization, individual modules can link in their own
initialization routines.
There are 7 levels for these initializiation routines.
So far these are how the levels are defined
level 0 - Here the world is dark. Nothing is initialized.
This level is responsible for initializing the
system clock.
level 1 - The system clock has been configured, but nothing else. Not
even global variables. This level is responsible for loading
the data sections from flash and clearing the .bss section.
level 2 - USART2 is enabled and set to be the main kernel logging
vehicle. From this point on klogf(...) can be used.
level 3 - The NVIC is reset to point to the flash. From this point
on interrupts can be received. I expect this is where
most core initialization routines will take place
levels 4 to 7 - User initializiation levels.
main - main() is called after all 8 initialization levels have executed,
so in a sense main() is like a 9th initialization level, except
that there is can be only one main() routine whereas there can be
multiple initalization routines per level.
Diffstat (limited to '02-usart/src/kern/main.c')
-rw-r--r-- | 02-usart/src/kern/main.c | 109 |
1 files changed, 7 insertions, 102 deletions
diff --git a/02-usart/src/kern/main.c b/02-usart/src/kern/main.c index 32166bc..ebb2164 100644 --- a/02-usart/src/kern/main.c +++ b/02-usart/src/kern/main.c @@ -1,67 +1,12 @@ - #include "arch.h" +#include "kern/log.h" -#include "arch/stm32l4xxx/peripherals/clock.h" -#include "arch/stm32l4xxx/peripherals/dma.h" -#include "arch/stm32l4xxx/peripherals/gpio.h" #include "arch/stm32l4xxx/peripherals/system.h" -#include "arch/stm32l4xxx/peripherals/usart.h" -#include "arch/stm32l4xxx/peripherals/nvic.h" -#include "arch/stm32l4xxx/peripherals/irq.h" - -#include "kern/dma/dma_manager.h" -#include "kern/gpio/gpio_manager.h" -#include "kern/gpio/sysled.h" - -#include "kern/delay.h" -#include "kern/mem.h" -#include "kern/string.h" - -/** Overrides the default systick irq handler. */ -void on_systick() -{ - static int is_on = 0; - gpio_reserved_pin_t sysled = get_sysled(); - - if (is_on) { - set_gpio_pin_low(sysled); - } else { - set_gpio_pin_high(sysled); - } - - is_on = ! is_on; -} +#include "arch/stm32l4xxx/peripherals/clock.h" -void setup_usart2(uint32_t baud_rate) +void on_systick() /* Overrides weak-symbol on_systick. */ { - enable_hsi(&RCC, true); - - int ec = 0; - gpio_enable_alternate_function( - GPIO_ALTERNATE_FUNCTION_USART2_TX, GPIO_PIN_PA2, &ec); - - if (ec) { - unhandled_isr(ec & 0xff); - } - - gpio_enable_alternate_function( - GPIO_ALTERNATE_FUNCTION_USART2_RX, GPIO_PIN_PA15, &ec); - - if (ec) { - unhandled_isr(ec & 0xff); - } - - set_usart2_clock_src(&RCC, USART_CLK_SRC_HSI16); - set_usart2_clock_enabled(&RCC, USART_CLK_SRC_HSI16); - - /* 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); + klogf("Systick\n"); } #ifdef ARCH_STM32L4 @@ -69,56 +14,16 @@ void setup_usart2(uint32_t baud_rate) /* 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); - regset(USART2.c_r1, usart_txeie, 1); - regset(USART2.c_r1, usart_rxneie, 1); - usart_enable_dma(&USART2, USART_ENABLE_TX); - usart_set_enabled(&USART2, USART_ENABLE_TX | USART_ENABLE_RX); - - - dma_opts_t opts = DEFAULT_DMA_OPTS; - opts.transfer_complete_interrupt_enable = 1; - int ec = 0; - dma_mem2p_channel_t dma_chan = - select_dma_channel_mem2p(DMA1_PERIPH_USART2_TX, &opts, &ec); - enable_interrupt(dma_channel_get_interrupt(dma_chan.c_)); + klogf("Hello, World! Clock Mhz: %d\n", (uint32_t) get_clock_mhz()); - if (ec) { - usart_printf(&USART2, "Select DMA channel failed :( %d\n", ec); - for (;;); - } - - // const char* thing = "Good Thing This Works!"; - - char* str = halloc(128); - kstrcpy(str, "Hello, Heap!"); - - usart_printf(&USART2, "DATA_SEGMENT_START %p\n", &DATA_SEGMENT_START); - usart_printf(&USART2, "DATA_SEGMENT_STOP: %p\n", &DATA_SEGMENT_STOP); - usart_printf(&USART2, "str at: %p\n", str); - usart_printf(&USART2, "str: %s\n", str); - // usart_printf(&USART2, "%s\n", thing); - // regset(USART2.ic_r, usart_tccf, 1); - // dma_mem2p_initiate_transfer(dma_chan, thing, strlen(thing)); - - gpio_reserved_pin_t sysled = get_sysled(); - set_gpio_pin_high(sysled); - - // usart_printf(&USART2, "Start Configuring Countdown!\n"); - - /* Set the countdown to start from 1,000,0000. */ - SCB.strv_r = 1000000; + /* Set the countdown to start from 10,000,0000. */ + SCB.strv_r = 10000000; /* Enable interrupts. */ regset(SCB.stcs_r, scb_tickint, 1); /* Start the systick. */ regset(SCB.stcs_r, scb_enable, 1); - - // usart_printf(&USART2, "Start Countdown Started!\n"); } #endif |