Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/soc/uart.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2019-2022 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <soc/uart.h>
19
#include <soc/clock.h>
20
#include <soc/timer.h>
21
#include <soc/t210.h>
22
23
/* UART A, B, C, D and E. */
24
static const u16 _uart_base_offsets[5] = { 0, 0x40, 0x200, 0x300, 0x400 };
25
26
void uart_init(u32 idx, u32 baud, u32 mode)
27
{
28
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
29
30
// Make sure no data is being sent.
31
if (!(mode & (UART_MCR_CTS_EN | UART_MCR_DTR)))
32
uart_wait_xfer(idx, UART_TX_IDLE);
33
34
// Set clock.
35
bool clk_type = clock_uart_use_src_div(idx, baud);
36
37
// 2 STOP bits for rates > 1M. (Reduced efficiency but less errors on high baudrates).
38
u32 uart_lcr_stop = baud > 1000000 ? UART_LCR_STOP : 0;
39
40
// Misc settings.
41
u32 div = clk_type ? ((8 * baud + 408000000) / (16 * baud)) : 1; // DIV_ROUND_CLOSEST.
42
uart->UART_IER_DLAB = 0; // Disable interrupts.
43
uart->UART_LCR = UART_LCR_DLAB | UART_LCR_WORD_LENGTH_8; // Enable DLAB & set 8n1 mode.
44
uart->UART_THR_DLAB = (u8)div; // Divisor latch LSB.
45
uart->UART_IER_DLAB = (u8)(div >> 8); // Divisor latch MSB.
46
47
// Disable DLAB and set STOP bits setting if applicable.
48
uart->UART_LCR = uart_lcr_stop | UART_LCR_WORD_LENGTH_8;
49
(void)uart->UART_SPR;
50
51
// Enable fifo.
52
uart->UART_IIR_FCR = UART_IIR_FCR_EN_FIFO;
53
(void)uart->UART_SPR;
54
usleep(20);
55
56
// Disable hardware flow control.
57
uart->UART_MCR = 0;
58
usleep(96);
59
60
// Clear tx/rx fifos.
61
uart->UART_IIR_FCR = UART_IIR_FCR_EN_FIFO | UART_IIR_FCR_TX_CLR | UART_IIR_FCR_RX_CLR;
62
63
// Set hardware flow control.
64
uart->UART_MCR = mode;
65
66
// Wait 3 symbols for baudrate change.
67
usleep(3 * ((baud + 999999) / baud));
68
uart_wait_xfer(idx, UART_TX_IDLE | UART_RX_RDYR);
69
}
70
71
void uart_wait_xfer(u32 idx, u32 which)
72
{
73
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
74
if (UART_TX_IDLE & which)
75
{
76
while (!(uart->UART_LSR & UART_LSR_TMTY))
77
;
78
}
79
if (UART_RX_RDYR & which)
80
{
81
while (uart->UART_LSR & UART_LSR_RDR)
82
(void)uart->UART_THR_DLAB;
83
}
84
}
85
86
void uart_send(u32 idx, const u8 *buf, u32 len)
87
{
88
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
89
90
for (u32 i = 0; i != len; i++)
91
{
92
while (!(uart->UART_LSR & UART_LSR_THRE))
93
;
94
uart->UART_THR_DLAB = buf[i];
95
}
96
}
97
98
u32 uart_recv(u32 idx, u8 *buf, u32 len)
99
{
100
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
101
bool manual_mode = uart->UART_MCR & UART_MCR_RTS;
102
u32 timeout = get_tmr_us() + 250;
103
u32 i;
104
105
if (manual_mode)
106
uart->UART_MCR &= ~UART_MCR_RTS;
107
108
for (i = 0; ; i++)
109
{
110
if (len && len <= i)
111
break;
112
113
while (!(uart->UART_LSR & UART_LSR_RDR))
114
if (timeout < get_tmr_us())
115
goto out;
116
117
buf[i] = uart->UART_THR_DLAB;
118
timeout = get_tmr_us() + 250;
119
}
120
121
out:
122
if (manual_mode)
123
uart->UART_MCR |= UART_MCR_RTS;
124
125
return i;
126
}
127
128
void uart_invert(u32 idx, bool enable, u32 invert_mask)
129
{
130
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
131
132
if (enable)
133
uart->UART_IRDA_CSR |= invert_mask;
134
else
135
uart->UART_IRDA_CSR &= ~invert_mask;
136
(void)uart->UART_SPR;
137
}
138
139
void uart_set_mode(u32 idx, u32 mode)
140
{
141
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
142
143
uart->UART_MCR = mode;
144
(void)uart->UART_SPR;
145
}
146
147
u32 uart_get_IIR(u32 idx)
148
{
149
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
150
151
u32 iir = uart->UART_IIR_FCR & UART_IIR_INT_MASK;
152
153
if (iir & UART_IIR_NO_INT)
154
return 0;
155
else
156
return ((iir >> 1) + 1); // Return encoded interrupt.
157
}
158
159
void uart_set_IIR(u32 idx)
160
{
161
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
162
163
uart->UART_IER_DLAB &= ~UART_IER_DLAB_IE_EORD;
164
(void)uart->UART_SPR;
165
uart->UART_IER_DLAB |= UART_IER_DLAB_IE_EORD;
166
(void)uart->UART_SPR;
167
}
168
169
void uart_empty_fifo(u32 idx, u32 which)
170
{
171
uart_t *uart = (uart_t *)(UART_BASE + (u32)_uart_base_offsets[idx]);
172
173
uart->UART_MCR = 0;
174
(void)uart->UART_SPR;
175
usleep(96);
176
177
uart->UART_IIR_FCR = UART_IIR_FCR_EN_FIFO | which;
178
(void)uart->UART_SPR;
179
usleep(18);
180
u32 tries = 0;
181
182
if (UART_IIR_FCR_TX_CLR & which)
183
{
184
while (tries < 10 && !(uart->UART_LSR & UART_LSR_TMTY))
185
{
186
tries++;
187
usleep(100);
188
}
189
tries = 0;
190
}
191
192
if (UART_IIR_FCR_RX_CLR & which)
193
{
194
while (tries < 10 && (uart->UART_LSR & UART_LSR_RDR))
195
{
196
tries++;
197
usleep(100);
198
}
199
}
200
}
201
202
#ifdef DEBUG_UART_PORT
203
#include <stdarg.h>
204
#include <string.h>
205
206
#include <utils/sprintf.h>
207
208
void uart_printf(const char *fmt, ...)
209
{
210
va_list ap;
211
212
//! NOTE: Anything more and it will hang. Heap usage is out of the question.
213
char text[256];
214
215
va_start(ap, fmt);
216
s_vprintf(text, fmt, ap);
217
va_end(ap);
218
219
uart_send(DEBUG_UART_PORT, (u8 *)text, strlen(text));
220
}
221
#endif
222
223