embox

Форк
0
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  0x00
16
#define THR  0x00
17
#define DLL  0x00
18
#define DLH  0x04
19
#define IER  0x04
20
#define IIR  0x08
21
#define FCR  0x08
22
#define LCR  0x0C
23
#define MCR  0x10
24
#define LSR  0x14
25
#define MSR  0x18
26
#define SCR  0x1C
27
#define SRBR 0x30
28
#define STHR 0x30
29
#define USR  0x7C
30
#define TFL  0x80
31
#define RFL  0x84
32
#define SRR  0x88
33
#define SRTS 0x8C
34
#define SBCR 0x90
35
#define SFE  0x98
36
#define SRT  0x9C
37
#define STET 0xA0
38
#define HTX  0XA4
39

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

51
static inline void  elvees_uart_setup_baud_rate(struct uart *dev, int baudrate_bps) {
52
	uint32_t src_clock_hz = 48000000;
53
    uint32_t divisor = (uint32_t) (src_clock_hz / (baudrate_bps * 16));
54
    uint32_t tmp_lcr;
55

56
    tmp_lcr = UART(LCR);
57
    UART(LCR) = tmp_lcr | LCR_DLAB;
58

59
    UART(DLH) =  (divisor >> 8) & 0xFF;
60

61
    UART(DLL)= divisor & 0xFF;
62

63
    UART(LCR) = tmp_lcr;
64
}
65

66
int elvees_uart_setup_common(struct uart *dev, const struct uart_params *params) {
67

68
	UART(SRR) = 1; /* soft reset */
69
	UART(FCR) = 0; /* without fifo */
70
	UART(LCR) = LCR_DLS_8BITS; /* 8 bit */
71

72
	elvees_uart_setup_baud_rate(dev, params->baud_rate);
73

74
	/*enable rx interrupt*/
75
	if (params->uart_param_flags & UART_PARAM_FLAGS_USE_IRQ) {
76
		/*enable rx interrupt*/
77
		UART(IER) = IER_ERBFI;
78
	}
79

80
	return 0;
81
}
82

83
int elvees_uart_has_symbol(struct uart *dev) {
84
	return UART(LSR) & LSR_RDR;
85
}
86

87
int elvees_uart_getc(struct uart *dev) {
88
	return (char)UART(RBR);
89
}
90

91
int elvees_uart_putc(struct uart *dev, int ch) {
92
	while (!(UART(LSR) & LSR_THRE)) {
93
	}
94

95
	UART(THR) = ch;
96

97
	return 0;
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.