summaryrefslogtreecommitdiff
path: root/drivers/console/uart_pipe.c
blob: 2102ce5d43c7c34e3013f450ca530dcef8e4a80b (plain)
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
/** @file
 * @brief Pipe UART driver
 *
 * A pipe UART driver allowing application to handle all aspects of received
 * protocol data.
 */

/*
 * Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <nanokernel.h>

#include <board.h>
#include <uart.h>

#include <console/uart_pipe.h>
#include <misc/printk.h>

static struct device *uart_pipe_dev;

static uint8_t *recv_buf;
static size_t recv_buf_len;
static uart_pipe_recv_cb app_cb;
static size_t recv_off;

void uart_pipe_isr(void *unused)
{
	ARG_UNUSED(unused);

	while (uart_irq_update(uart_pipe_dev)
	       && uart_irq_is_pending(uart_pipe_dev)) {
		int rx;

		if (!uart_irq_rx_ready(uart_pipe_dev)) {
			continue;
		}

		rx = uart_fifo_read(uart_pipe_dev, recv_buf + recv_off,
				    recv_buf_len - recv_off);
		if (!rx) {
			continue;
		}

		/*
		 * Call application callback with received data. Application
		 * may provide new buffer or alter data offset.
		 */
		recv_off += rx;
		recv_buf = app_cb(recv_buf, &recv_off);
	}
}

int uart_pipe_send(const uint8_t *data, int len)
{
	if (uart_pipe_dev != NULL) {
		return uart_fifo_fill(uart_pipe_dev, data, len);
	} else {
		return 0;
	}
}

IRQ_CONNECT_STATIC(uart_pipe, CONFIG_UART_PIPE_IRQ,
		   CONFIG_UART_PIPE_IRQ_PRI, uart_pipe_isr, 0,
		   UART_IRQ_FLAGS);

static void uart_pipe_setup(struct device *uart)
{
	uart_irq_rx_disable(uart);
	uart_irq_tx_disable(uart);
	IRQ_CONFIG(uart_pipe, uart_irq_get(uart));
	irq_enable(uart_irq_get(uart));

	/* Drain the fifo */
	while (uart_irq_rx_ready(uart)) {
		unsigned char c;

		uart_fifo_read(uart, &c, 1);
	}

	uart_irq_rx_enable(uart);
}

void uart_pipe_register(uint8_t *buf, size_t len, uart_pipe_recv_cb cb)
{
	recv_buf = buf;
	recv_buf_len = len;
	app_cb = cb;

	uart_pipe_dev = device_get_binding(CONFIG_UART_PIPE_ON_DEV_NAME);

	if (uart_pipe_dev != NULL) {
		uart_pipe_setup(uart_pipe_dev);
	}
}