forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_liteuart.c
331 lines (282 loc) · 7.63 KB
/
uart_liteuart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT litex_uart0
#include <zephyr/kernel.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/init.h>
#include <zephyr/irq.h>
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/types.h>
#define UART_RXTX_ADDR DT_INST_REG_ADDR_BY_NAME(0, rxtx)
#define UART_TXFULL_ADDR DT_INST_REG_ADDR_BY_NAME(0, txfull)
#define UART_RXEMPTY_ADDR DT_INST_REG_ADDR_BY_NAME(0, rxempty)
#define UART_EV_STATUS_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_status)
#define UART_EV_PENDING_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_pending)
#define UART_EV_ENABLE_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_enable)
#define UART_TXEMPTY_ADDR DT_INST_REG_ADDR_BY_NAME(0, txempty)
#define UART_RXFULL_ADDR DT_INST_REG_ADDR_BY_NAME(0, rxfull)
#define UART_EV_TX (1 << 0)
#define UART_EV_RX (1 << 1)
#define UART_IRQ DT_INST_IRQN(0)
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
typedef void (*irq_cfg_func_t)(void);
#endif
struct uart_liteuart_device_config {
uint32_t port;
uint32_t sys_clk_freq;
uint32_t baud_rate;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
irq_cfg_func_t cfg_func;
#endif
};
struct uart_liteuart_data {
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_user_data_t callback;
void *cb_data;
#endif
};
/**
* @brief Output a character in polled mode.
*
* Writes data to tx register. Waits for space if transmitter is full.
*
* @param dev UART device struct
* @param c Character to send
*/
static void uart_liteuart_poll_out(const struct device *dev, unsigned char c)
{
/* wait for space */
while (litex_read8(UART_TXFULL_ADDR)) {
}
litex_write8(c, UART_RXTX_ADDR);
}
/**
* @brief Poll the device for input.
*
* @param dev UART device struct
* @param c Pointer to character
*
* @return 0 if a character arrived, -1 if the input buffer if empty.
*/
static int uart_liteuart_poll_in(const struct device *dev, unsigned char *c)
{
if (!litex_read8(UART_RXEMPTY_ADDR)) {
*c = litex_read8(UART_RXTX_ADDR);
/* refresh UART_RXEMPTY by writing UART_EV_RX
* to UART_EV_PENDING
*/
litex_write8(UART_EV_RX, UART_EV_PENDING_ADDR);
return 0;
} else {
return -1;
}
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/**
* @brief Enable TX interrupt in event register
*
* @param dev UART device struct
*/
static void uart_liteuart_irq_tx_enable(const struct device *dev)
{
uint8_t enable = litex_read8(UART_EV_ENABLE_ADDR);
litex_write8(enable | UART_EV_TX, UART_EV_ENABLE_ADDR);
}
/**
* @brief Disable TX interrupt in event register
*
* @param dev UART device struct
*/
static void uart_liteuart_irq_tx_disable(const struct device *dev)
{
uint8_t enable = litex_read8(UART_EV_ENABLE_ADDR);
litex_write8(enable & ~(UART_EV_TX), UART_EV_ENABLE_ADDR);
}
/**
* @brief Enable RX interrupt in event register
*
* @param dev UART device struct
*/
static void uart_liteuart_irq_rx_enable(const struct device *dev)
{
uint8_t enable = litex_read8(UART_EV_ENABLE_ADDR);
litex_write8(enable | UART_EV_RX, UART_EV_ENABLE_ADDR);
}
/**
* @brief Disable RX interrupt in event register
*
* @param dev UART device struct
*/
static void uart_liteuart_irq_rx_disable(const struct device *dev)
{
uint8_t enable = litex_read8(UART_EV_ENABLE_ADDR);
litex_write8(enable & ~(UART_EV_RX), UART_EV_ENABLE_ADDR);
}
/**
* @brief Check if Tx IRQ has been raised and UART is ready to accept new data
*
* @param dev UART device struct
*
* @return 1 if an IRQ has been raised, 0 otherwise
*/
static int uart_liteuart_irq_tx_ready(const struct device *dev)
{
uint8_t val = litex_read8(UART_TXFULL_ADDR);
return !val;
}
/**
* @brief Check if Rx IRQ has been raised and there's data to be read from UART
*
* @param dev UART device struct
*
* @return 1 if an IRQ has been raised, 0 otherwise
*/
static int uart_liteuart_irq_rx_ready(const struct device *dev)
{
uint8_t pending;
pending = litex_read8(UART_EV_PENDING_ADDR);
if (pending & UART_EV_RX) {
return 1;
} else {
return 0;
}
}
/**
* @brief Fill FIFO with data
*
* @param dev UART device struct
* @param tx_data Data to transmit
* @param size Number of bytes to send
*
* @return Number of bytes sent
*/
static int uart_liteuart_fifo_fill(const struct device *dev,
const uint8_t *tx_data, int size)
{
int i;
for (i = 0; i < size && !litex_read8(UART_TXFULL_ADDR); i++) {
litex_write8(tx_data[i], UART_RXTX_ADDR);
}
return i;
}
/**
* @brief Read data from FIFO
*
* @param dev UART device struct
* @param rxData Data container
* @param size Container size
*
* @return Number of bytes read
*/
static int uart_liteuart_fifo_read(const struct device *dev,
uint8_t *rx_data, const int size)
{
int i;
for (i = 0; i < size && !litex_read8(UART_RXEMPTY_ADDR); i++) {
rx_data[i] = litex_read8(UART_RXTX_ADDR);
/* refresh UART_RXEMPTY by writing UART_EV_RX
* to UART_EV_PENDING
*/
litex_write8(UART_EV_RX, UART_EV_PENDING_ADDR);
}
return i;
}
static void uart_liteuart_irq_err(const struct device *dev)
{
ARG_UNUSED(dev);
}
/**
* @brief Check if any IRQ is pending
*
* @param dev UART device struct
*
* @return 1 if an IRQ is pending, 0 otherwise
*/
static int uart_liteuart_irq_is_pending(const struct device *dev)
{
uint8_t pending;
pending = litex_read8(UART_EV_PENDING_ADDR);
if (pending & (UART_EV_TX | UART_EV_RX)) {
return 1;
} else {
return 0;
}
}
static int uart_liteuart_irq_update(const struct device *dev)
{
return 1;
}
/**
* @brief Set the callback function pointer for IRQ.
*
* @param dev UART device struct
* @param cb Callback function pointer.
*/
static void uart_liteuart_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb,
void *cb_data)
{
struct uart_liteuart_data *data;
data = dev->data;
data->callback = cb;
data->cb_data = cb_data;
}
static void liteuart_uart_irq_handler(const struct device *dev)
{
struct uart_liteuart_data *data = dev->data;
unsigned int key = irq_lock();
if (data->callback) {
data->callback(dev, data->cb_data);
}
/* clear events */
litex_write8(UART_EV_TX | UART_EV_RX, UART_EV_PENDING_ADDR);
irq_unlock(key);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_driver_api uart_liteuart_driver_api = {
.poll_in = uart_liteuart_poll_in,
.poll_out = uart_liteuart_poll_out,
.err_check = NULL,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = uart_liteuart_fifo_fill,
.fifo_read = uart_liteuart_fifo_read,
.irq_tx_enable = uart_liteuart_irq_tx_enable,
.irq_tx_disable = uart_liteuart_irq_tx_disable,
.irq_tx_ready = uart_liteuart_irq_tx_ready,
.irq_rx_enable = uart_liteuart_irq_rx_enable,
.irq_rx_disable = uart_liteuart_irq_rx_disable,
.irq_rx_ready = uart_liteuart_irq_rx_ready,
.irq_err_enable = uart_liteuart_irq_err,
.irq_err_disable = uart_liteuart_irq_err,
.irq_is_pending = uart_liteuart_irq_is_pending,
.irq_update = uart_liteuart_irq_update,
.irq_callback_set = uart_liteuart_irq_callback_set
#endif
};
static struct uart_liteuart_data uart_liteuart_data_0;
static int uart_liteuart_init(const struct device *dev);
static const struct uart_liteuart_device_config uart_liteuart_dev_cfg_0 = {
.port = UART_RXTX_ADDR,
.baud_rate = DT_INST_PROP(0, current_speed)
};
DEVICE_DT_INST_DEFINE(0,
uart_liteuart_init,
NULL,
&uart_liteuart_data_0, &uart_liteuart_dev_cfg_0,
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY,
(void *)&uart_liteuart_driver_api);
static int uart_liteuart_init(const struct device *dev)
{
litex_write8(UART_EV_TX | UART_EV_RX, UART_EV_PENDING_ADDR);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
IRQ_CONNECT(UART_IRQ, DT_INST_IRQ(0, priority),
liteuart_uart_irq_handler, DEVICE_DT_INST_GET(0),
0);
irq_enable(UART_IRQ);
#endif
return 0;
}