embox
98 строк · 2.0 Кб
1/**
2* @file
3* @brief UART driver for ELVEES UART
4* @author Denis Deryugin <deryugin.denis@gmail.com>
5* @version
6* @date 14.11.2016
7*/
8
9
10#include <drivers/serial/uart_dev.h>11#include <drivers/serial/diag_serial.h>12
13#define UART(x) (*(volatile uint32_t *)(dev->base_addr + (x)))14
15#define RBR 0x0016#define THR 0x0017#define DLL 0x0018#define DLH 0x0419#define IER 0x0420#define IIR 0x0821#define FCR 0x0822#define LCR 0x0C23#define MCR 0x1024#define LSR 0x1425#define MSR 0x1826#define SCR 0x1C27#define SRBR 0x3028#define STHR 0x3029#define USR 0x7C30#define TFL 0x8031#define RFL 0x8432#define SRR 0x8833#define SRTS 0x8C34#define SBCR 0x9035#define SFE 0x9836#define SRT 0x9C37#define STET 0xA038#define HTX 0XA439
40#define USR_BUSY (1 << 0)41#define USR_TFNF (1 << 1)42
43#define LSR_RDR (1 << 0)44#define LSR_THRE (1 << 5)45#define LCR_DLAB (1 << 7)46
47#define IER_ERBFI (1 << 0)48
49#define LCR_DLS_8BITS (3 << 0)50
51static inline void elvees_uart_setup_baud_rate(struct uart *dev, int baudrate_bps) {52uint32_t src_clock_hz = 48000000;53uint32_t divisor = (uint32_t) (src_clock_hz / (baudrate_bps * 16));54uint32_t tmp_lcr;55
56tmp_lcr = UART(LCR);57UART(LCR) = tmp_lcr | LCR_DLAB;58
59UART(DLH) = (divisor >> 8) & 0xFF;60
61UART(DLL)= divisor & 0xFF;62
63UART(LCR) = tmp_lcr;64}
65
66int elvees_uart_setup_common(struct uart *dev, const struct uart_params *params) {67
68UART(SRR) = 1; /* soft reset */69UART(FCR) = 0; /* without fifo */70UART(LCR) = LCR_DLS_8BITS; /* 8 bit */71
72elvees_uart_setup_baud_rate(dev, params->baud_rate);73
74/*enable rx interrupt*/75if (params->uart_param_flags & UART_PARAM_FLAGS_USE_IRQ) {76/*enable rx interrupt*/77UART(IER) = IER_ERBFI;78}79
80return 0;81}
82
83int elvees_uart_has_symbol(struct uart *dev) {84return UART(LSR) & LSR_RDR;85}
86
87int elvees_uart_getc(struct uart *dev) {88return (char)UART(RBR);89}
90
91int elvees_uart_putc(struct uart *dev, int ch) {92while (!(UART(LSR) & LSR_THRE)) {93}94
95UART(THR) = ch;96
97return 0;98}
99