Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright (c) 2021, Thomas Stranger
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * This is not a real serial driver. It is used to instantiate struct
 * devices for the "vnd,serial" devicetree compatible used in test code.
 */

#include <stdbool.h>

#include <zephyr/sys/__assert.h>
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/uart/serial_test.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/ring_buffer.h>

LOG_MODULE_REGISTER(mock_serial, CONFIG_LOG_DEFAULT_LEVEL);

#define DT_DRV_COMPAT vnd_serial
struct serial_vnd_data {
#ifdef CONFIG_RING_BUFFER
	struct ring_buf *written;
	struct ring_buf *read_queue;
#endif
	serial_vnd_write_cb_t callback;
	void *callback_data;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	uart_irq_callback_user_data_t irq_isr;
	bool irq_rx_enabled;
	bool irq_tx_enabled;
#endif
#ifdef CONFIG_UART_ASYNC_API
	uart_callback_t async_cb;
	void *async_cb_user_data;
	uint8_t *read_buf;
	size_t read_size;
	size_t read_position;
#endif
};

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static bool is_irq_rx_pending(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	return !ring_buf_is_empty(data->read_queue);
}

static bool is_irq_tx_pending(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	return ring_buf_space_get(data->written) != 0;
}

static void irq_process(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	for (;;) {
		bool rx_rdy = is_irq_rx_pending(dev);
		bool tx_rdy = is_irq_tx_pending(dev);
		bool rx_int = rx_rdy && data->irq_rx_enabled;
		bool tx_int = tx_rdy && data->irq_tx_enabled;

		LOG_DBG("rx_rdy %d tx_rdy %d", rx_rdy, tx_rdy);
		LOG_DBG("rx_int %d tx_int %d", rx_int, tx_int);

		if (!(rx_int || tx_int)) {
			break;
		}

		LOG_DBG("isr");
		if (!data->irq_isr) {
			LOG_ERR("no isr registered");
			break;
		}
		data->irq_isr(dev, NULL);
	};
}

static void irq_rx_enable(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	data->irq_rx_enabled = true;
	LOG_DBG("rx enabled");
	irq_process(dev);
}

static void irq_rx_disable(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	data->irq_rx_enabled = false;
	LOG_DBG("rx disabled");
}

static int irq_rx_ready(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;
	bool ready = !ring_buf_is_empty(data->read_queue);

	LOG_DBG("rx ready: %d", ready);
	return ready;
}

static void irq_tx_enable(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	LOG_DBG("tx enabled");
	data->irq_tx_enabled = true;
	irq_process(dev);
}

static void irq_tx_disable(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	data->irq_tx_enabled = false;
	LOG_DBG("tx disabled");
}

static int irq_tx_ready(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;
	bool ready = (ring_buf_space_get(data->written) != 0);

	LOG_DBG("tx ready: %d", ready);
	return ready;
}

static void irq_callback_set(const struct device *dev, uart_irq_callback_user_data_t cb,
			     void *user_data)
{
	struct serial_vnd_data *data = dev->data;

	/* Not implemented. Ok because `user_data` is always NULL in the current
	 * implementation of core UART API.
	 */
	__ASSERT_NO_MSG(user_data == NULL);

#if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS) && defined(CONFIG_UART_ASYNC_API)
	if (data->read_buf) {
		LOG_ERR("Setting callback to NULL while asynchronous API is in use.");
	}
	data->async_cb = NULL;
	data->async_cb_user_data = NULL;
#endif

	data->irq_isr = cb;
	LOG_DBG("callback set");
}

static int fifo_fill(const struct device *dev, const uint8_t *tx_data, int size)
{
	struct serial_vnd_data *data = dev->data;
	uint32_t write_len = ring_buf_put(data->written, tx_data, size);

	if (data->callback) {
		data->callback(dev, data->callback_data);
	}
	return write_len;
}

static int fifo_read(const struct device *dev, uint8_t *rx_data, const int size)
{
	struct serial_vnd_data *data = dev->data;
	int read_len = ring_buf_get(data->read_queue, rx_data, size);

	LOG_HEXDUMP_DBG(rx_data, read_len, "");
	return read_len;
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static int serial_vnd_poll_in(const struct device *dev, unsigned char *c)
{
#ifdef CONFIG_RING_BUFFER
	struct serial_vnd_data *data = dev->data;
	uint32_t bytes_read;

	if (data == NULL || data->read_queue == NULL) {
		return -ENOTSUP;
	}
	bytes_read = ring_buf_get(data->read_queue, c, 1);
	if (bytes_read == 1) {
		return 0;
	}
	return -1;
#else
	return -ENOTSUP;
#endif
}

static void serial_vnd_poll_out(const struct device *dev, unsigned char c)
{
	struct serial_vnd_data *data = dev->data;

#ifdef CONFIG_RING_BUFFER
	if (data == NULL || data->written == NULL) {
		return;
	}
	ring_buf_put(data->written, &c, 1);
#endif
	if (data->callback) {
		data->callback(dev, data->callback_data);
	}
}

#ifdef CONFIG_UART_ASYNC_API
static void async_rx_run(const struct device *dev);
#endif

#ifdef CONFIG_RING_BUFFER
int serial_vnd_queue_in_data(const struct device *dev, const unsigned char *c, uint32_t size)
{
	struct serial_vnd_data *data = dev->data;
	int write_size;

	if (data == NULL || data->read_queue == NULL) {
		return -ENOTSUP;
	}
	write_size = ring_buf_put(data->read_queue, c, size);

	LOG_DBG("size %u write_size %u", size, write_size);
	LOG_HEXDUMP_DBG(c, write_size, "");

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	if (write_size > 0) {
		irq_process(dev);
	}
#endif

#ifdef CONFIG_UART_ASYNC_API
	async_rx_run(dev);
#endif

	return write_size;
}

uint32_t serial_vnd_out_data_size_get(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;

	if (data == NULL || data->written == NULL) {
		return -ENOTSUP;
	}
	return ring_buf_size_get(data->written);
}

uint32_t serial_vnd_read_out_data(const struct device *dev, unsigned char *out_data, uint32_t size)
{
	struct serial_vnd_data *data = dev->data;

	if (data == NULL || data->written == NULL) {
		return -ENOTSUP;
	}
	return ring_buf_get(data->written, out_data, size);
}

uint32_t serial_vnd_peek_out_data(const struct device *dev, unsigned char *out_data, uint32_t size)
{
	struct serial_vnd_data *data = dev->data;

	if (data == NULL || data->written == NULL) {
		return -ENOTSUP;
	}
	return ring_buf_peek(data->written, out_data, size);
}
#endif

void serial_vnd_set_callback(const struct device *dev, serial_vnd_write_cb_t callback,
			     void *user_data)
{
	struct serial_vnd_data *data = dev->data;

	if (data == NULL) {
		return;
	}
	data->callback = callback;
	data->callback_data = user_data;
}

static int serial_vnd_err_check(const struct device *dev)
{
	return -ENOTSUP;
}

#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
static int serial_vnd_configure(const struct device *dev, const struct uart_config *cfg)
{
	return -ENOTSUP;
}

static int serial_vnd_config_get(const struct device *dev, struct uart_config *cfg)
{
	return -ENOTSUP;
}
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */

#ifdef CONFIG_UART_ASYNC_API
static int serial_vnd_callback_set(const struct device *dev, uart_callback_t callback,
				   void *user_data)
{
	struct serial_vnd_data *data = dev->data;

	if (data == NULL) {
		return -ENOTSUP;
	}

#if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS) && defined(CONFIG_UART_INTERRUPT_DRIVEN)
	data->irq_isr = NULL;
#endif

	if (callback == NULL && data->read_buf) {
		LOG_ERR("Setting callback to NULL while asynchronous API is in use.");
	}

	data->async_cb = callback;
	data->async_cb_user_data = user_data;

	return 0;
}

static int serial_vnd_api_tx(const struct device *dev, const uint8_t *tx_data, size_t len,
			     int32_t timeout)
{
	struct serial_vnd_data *data = dev->data;
	struct uart_event evt;
	uint32_t write_len;

	if (data == NULL) {
		return -ENOTSUP;
	}

	if (data->async_cb == NULL) {
		return -EINVAL;
	}

	write_len = ring_buf_put(data->written, tx_data, len);
	if (data->callback) {
		data->callback(dev, data->callback_data);
	}

	__ASSERT(write_len == len, "Ring buffer full. Async wait not implemented.");

	evt = (struct uart_event){
		.type = UART_TX_DONE,
		.data.tx.buf = tx_data,
		.data.tx.len = len,
	};
	data->async_cb(dev, &evt, data->async_cb_user_data);

	return 0;
}

static void async_rx_run(const struct device *dev)
{
	struct serial_vnd_data *data = dev->data;
	struct uart_event evt;
	uint32_t read_len;
	uint32_t read_remaining;

	if (!data->read_buf) {
		return;
	}

	__ASSERT_NO_MSG(data->async_cb);

	read_remaining = data->read_size - data->read_position;

	read_len = ring_buf_get(data->read_queue, &data->read_buf[data->read_position],
				read_remaining);

	if (read_len != 0) {
		evt = (struct uart_event){
			.type = UART_RX_RDY,
			.data.rx.buf = data->read_buf,
			.data.rx.len = read_len,
			.data.rx.offset = data->read_position,
		};
		data->async_cb(dev, &evt, data->async_cb_user_data);
	}

	data->read_position += read_len;

	if (data->read_position == data->read_size) {
		data->read_buf = NULL;
		evt = (struct uart_event){
			.type = UART_RX_DISABLED,
		};
		data->async_cb(dev, &evt, data->async_cb_user_data);
	}
}

static int serial_vnd_rx_enable(const struct device *dev, uint8_t *read_buf, size_t read_size,
				int32_t timeout)
{
	struct serial_vnd_data *data = dev->data;

	LOG_WRN("read_size %zd", read_size);

	if (data == NULL) {
		return -ENOTSUP;
	}

	if (data->async_cb == NULL) {
		return -EINVAL;
	}

	__ASSERT(timeout == SYS_FOREVER_MS, "Async timeout not implemented.");

	data->read_buf = read_buf;
	data->read_size = read_size;
	data->read_position = 0;

	async_rx_run(dev);

	return 0;
}
#endif /* CONFIG_UART_ASYNC_API */

static const struct uart_driver_api serial_vnd_api = {
	.poll_in = serial_vnd_poll_in,
	.poll_out = serial_vnd_poll_out,
	.err_check = serial_vnd_err_check,
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
	.configure = serial_vnd_configure,
	.config_get = serial_vnd_config_get,
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_callback_set = irq_callback_set,
	.irq_rx_enable = irq_rx_enable,
	.irq_rx_disable = irq_rx_disable,
	.irq_rx_ready = irq_rx_ready,
	.irq_tx_enable = irq_tx_enable,
	.irq_tx_disable = irq_tx_disable,
	.irq_tx_ready = irq_tx_ready,
	.fifo_read = fifo_read,
	.fifo_fill = fifo_fill,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#ifdef CONFIG_UART_ASYNC_API
	.callback_set = serial_vnd_callback_set,
	.tx = serial_vnd_api_tx,
	.rx_enable = serial_vnd_rx_enable,
#endif /* CONFIG_UART_ASYNC_API */
};

#define VND_SERIAL_DATA_BUFFER(n)                                                                  \
	RING_BUF_DECLARE(written_data_##n, DT_INST_PROP(n, buffer_size));                          \
	RING_BUF_DECLARE(read_queue_##n, DT_INST_PROP(n, buffer_size));                            \
	static struct serial_vnd_data serial_vnd_data_##n = {                                      \
		.written = &written_data_##n,                                                      \
		.read_queue = &read_queue_##n,                                                     \
	};
#define VND_SERIAL_DATA(n) static struct serial_vnd_data serial_vnd_data_##n = {};
#define VND_SERIAL_INIT(n)                                                                         \
	COND_CODE_1(DT_INST_NODE_HAS_PROP(n, buffer_size), (VND_SERIAL_DATA_BUFFER(n)),            \
		    (VND_SERIAL_DATA(n)))                                                          \
	DEVICE_DT_INST_DEFINE(n, NULL, NULL, &serial_vnd_data_##n, NULL, POST_KERNEL,              \
			      CONFIG_SERIAL_INIT_PRIORITY, &serial_vnd_api);

DT_INST_FOREACH_STATUS_OKAY(VND_SERIAL_INIT)