blob: c5a9136eae7e680f67fb26eada64a24ddb5ca99f [file] [log] [blame]
Sylvain Munaut26bc4652020-09-14 10:19:49 +02001/*
2 * console.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: LGPL-3.0-or-later
6 */
7
8#include <stdint.h>
9
10#include "config.h"
11#include "mini-printf.h"
12
13
14struct wb_uart {
15 uint32_t data;
16 uint32_t clkdiv;
17} __attribute__((packed,aligned(4)));
18
19static volatile struct wb_uart * const uart_regs = (void*)(UART_BASE);
20
21
22static char _printf_buf[128];
23
24void console_init(void)
25{
26#ifdef BOARD_E1_TRACER
27 uart_regs->clkdiv = 22; /* ~1 Mbaud with clk=24MHz */
28#else
29 uart_regs->clkdiv = 29; /* ~1 Mbaud with clk=30.72MHz */
30#endif
31}
32
33char getchar(void)
34{
35 int32_t c;
36 do {
37 c = uart_regs->data;
38 } while (c & 0x80000000);
39 return c;
40}
41
42int getchar_nowait(void)
43{
44 int32_t c;
45 c = uart_regs->data;
46 return c & 0x80000000 ? -1 : (c & 0xff);
47}
48
49void putchar(char c)
50{
51 uart_regs->data = c;
52}
53
54void puts(const char *p)
55{
56 char c;
57 while ((c = *(p++)) != 0x00) {
58 if (c == '\n')
59 uart_regs->data = '\r';
60 uart_regs->data = c;
61 }
62}
63
64int printf(const char *fmt, ...)
65{
66 va_list va;
67 int l;
68
69 va_start(va, fmt);
70 l = mini_vsnprintf(_printf_buf, 128, fmt, va);
71 va_end(va);
72
73 puts(_printf_buf);
74
75 return l;
76}