aboutsummaryrefslogtreecommitdiff
path: root/02-usart/src/kern/log.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2020-11-24 00:38:09 -0700
committerJosh Rahm <joshuarahm@gmail.com>2020-11-24 00:53:28 -0700
commit2478a549b9f64c50310da41c861b8f86fdea2861 (patch)
tree3a62960f83453f354cbf309ac0722ed2ea6c27c7 /02-usart/src/kern/log.c
parent9dab2bf91ed3e6af7c7b07590ccc8c3b211a763a (diff)
downloadstm32l4-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/log.c')
-rw-r--r--02-usart/src/kern/log.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/02-usart/src/kern/log.c b/02-usart/src/kern/log.c
new file mode 100644
index 0000000..a217183
--- /dev/null
+++ b/02-usart/src/kern/log.c
@@ -0,0 +1,55 @@
+#include "arch/stm32l4xxx/peripherals/usart.h"
+#include "arch/stm32l4xxx/peripherals/clock.h"
+
+#include "kern/log.h"
+#include "kern/init.h"
+#include "kern/gpio/gpio_manager.h"
+
+#include "kern/common.h"
+
+void setup_usart2(uint32_t baud_rate);
+
+/** This module requires an initialization routine. This is a level2 routine,
+ * so anything running at level3 or lower is guaranteed to have access
+ * to the klong. */
+init2()
+{
+ setup_usart2(115200);
+ regset(USART2.c_r1, usart_txeie, 1);
+ regset(USART2.c_r1, usart_rxneie, 1);
+ usart_set_enabled(&USART2, USART_ENABLE_TX | USART_ENABLE_RX);
+
+ klogf("klog() enabled on USART2\n");
+}
+
+void klogf(const char* fmt, ...)
+{
+ va_list l;
+ va_start(l, fmt);
+
+ usart_vprintf(&USART2, fmt, l);
+}
+
+void setup_usart2(uint32_t baud_rate)
+{
+ enable_hsi(&RCC, true);
+
+ int ec = 0;
+ gpio_enable_alternate_function(
+ GPIO_ALTERNATE_FUNCTION_USART2_TX, GPIO_PIN_PA2, &ec);
+
+ gpio_enable_alternate_function(
+ GPIO_ALTERNATE_FUNCTION_USART2_RX, GPIO_PIN_PA15, &ec);
+
+ 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);
+}