aboutsummaryrefslogtreecommitdiff
path: root/src/kern/log.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2020-11-24 13:46:41 -0700
committerJosh Rahm <joshuarahm@gmail.com>2020-11-24 13:46:41 -0700
commit93b063fedfcf7409a67df035170ea5670cad22e1 (patch)
treea23321a7465d966b1ccf196ca00e65a70c9f9110 /src/kern/log.c
parentb040195d31df6ad759f16ea3456471897f55daa1 (diff)
downloadstm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.tar.gz
stm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.tar.bz2
stm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.zip
Moved action to top level.
Removed old iterations of the project and moved the files from 02-usart to the root directory since that's the sole place where the action is and that subproject has outgrown its initial title.
Diffstat (limited to 'src/kern/log.c')
-rw-r--r--src/kern/log.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/kern/log.c b/src/kern/log.c
new file mode 100644
index 0000000..a217183
--- /dev/null
+++ b/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);
+}