diff options
Diffstat (limited to 'src/io.c')
-rw-r--r-- | src/io.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..0da33d1 --- /dev/null +++ b/src/io.c @@ -0,0 +1,64 @@ +#include <stdint.h> +#include <stdio.h> + +#include "ch573/gpio.h" +#include "ch573/uart.h" + +#include "io.h" + +#ifndef DEBUG_UART +#define DEBUG_UART UART1 +#endif + +#define UART1 ch573_uart__uart1 +#define UART CH573_UART__UART_T_INTF + +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF + +#define gpio_usart1_tx_pin 9 + +static int uart1_FILE_put(char ch, FILE* unused) +{ + if (ch == '\n') { + uart1_FILE_put('\r', unused); + } + + while (!UART.lsr.thr_empty.get(UART1)); + UART.thr.set(UART1, ch); + + return 1; +} + +static int uart1_FILE_get(FILE* f) +{ + return -1; +} + +static int uart1_FILE_flush(FILE* f) +{ + return 0; +} + +FILE _uart1_FILE = (FILE){ + .put = uart1_FILE_put, + .get = uart1_FILE_get, + .flush = uart1_FILE_flush, + .flags = __SWR, +}; + +FILE* const stdout = &_uart1_FILE; + +void init_uart1_for_stdout(void) +{ + GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, gpio_usart1_tx_pin); + GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, gpio_usart1_tx_pin); + + UART.div.set(UART1, 1); + UART.fcr.set(UART1, 0x07); + UART.ier.txd_en.set(UART1, ON); + UART.lcr.word_sz.set(UART1, WORD_SZ_8_BITS); + + volatile uint32_t dl = (10 * 6400000 / 8 / BAUD_RATE + 5) / 10; + UART.dl.set(UART1, dl); +} |