2015-07-07 04:26:34 +08:00
|
|
|
/** @file
|
2015-11-25 15:11:30 +08:00
|
|
|
* @brief Pipe UART driver
|
2015-06-02 03:35:02 +08:00
|
|
|
*
|
2015-11-25 15:11:30 +08:00
|
|
|
* A pipe UART driver allowing application to handle all aspects of received
|
2015-06-02 03:35:02 +08:00
|
|
|
* protocol data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-06-02 03:35:02 +08:00
|
|
|
*/
|
|
|
|
|
2016-12-05 04:59:37 +08:00
|
|
|
#include <kernel.h>
|
2015-06-02 03:35:02 +08:00
|
|
|
|
|
|
|
#include <board.h>
|
2015-10-15 14:48:03 +08:00
|
|
|
#include <uart.h>
|
2015-06-02 03:35:02 +08:00
|
|
|
|
2015-11-25 15:11:30 +08:00
|
|
|
#include <console/uart_pipe.h>
|
2015-06-02 03:35:02 +08:00
|
|
|
#include <misc/printk.h>
|
|
|
|
|
2015-12-02 00:42:20 +08:00
|
|
|
static struct device *uart_pipe_dev;
|
2015-06-02 03:35:02 +08:00
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
static u8_t *recv_buf;
|
2015-06-02 03:35:02 +08:00
|
|
|
static size_t recv_buf_len;
|
2015-11-25 15:11:30 +08:00
|
|
|
static uart_pipe_recv_cb app_cb;
|
2015-06-02 03:35:02 +08:00
|
|
|
static size_t recv_off;
|
|
|
|
|
2018-02-25 22:39:16 +08:00
|
|
|
static void uart_pipe_rx(struct device *dev)
|
2015-06-02 03:35:02 +08:00
|
|
|
{
|
2018-02-25 22:39:16 +08:00
|
|
|
/* As per the API, the interrupt may be an edge so keep
|
|
|
|
* reading from the FIFO until it's empty.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
int avail = recv_buf_len - recv_off;
|
|
|
|
int got;
|
|
|
|
|
|
|
|
got = uart_fifo_read(uart_pipe_dev, recv_buf + recv_off, avail);
|
|
|
|
if (got <= 0) {
|
|
|
|
break;
|
2015-06-02 03:35:02 +08:00
|
|
|
}
|
|
|
|
|
2015-11-25 15:11:30 +08:00
|
|
|
/*
|
|
|
|
* Call application callback with received data. Application
|
2015-06-02 03:35:02 +08:00
|
|
|
* may provide new buffer or alter data offset.
|
|
|
|
*/
|
2018-02-25 22:39:16 +08:00
|
|
|
recv_off += got;
|
2015-06-02 03:35:02 +08:00
|
|
|
recv_buf = app_cb(recv_buf, &recv_off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:39:16 +08:00
|
|
|
static void uart_pipe_isr(struct device *dev)
|
|
|
|
{
|
|
|
|
uart_irq_update(dev);
|
|
|
|
|
|
|
|
if (uart_irq_is_pending(dev)) {
|
|
|
|
if (uart_irq_rx_ready(dev)) {
|
|
|
|
uart_pipe_rx(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
int uart_pipe_send(const u8_t *data, int len)
|
2015-06-02 03:35:02 +08:00
|
|
|
{
|
2016-02-02 18:21:34 +08:00
|
|
|
while (len--) {
|
|
|
|
uart_poll_out(uart_pipe_dev, *data++);
|
2015-12-02 00:42:20 +08:00
|
|
|
}
|
2016-02-02 18:21:34 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-06-02 03:35:02 +08:00
|
|
|
}
|
|
|
|
|
2015-12-02 00:42:19 +08:00
|
|
|
static void uart_pipe_setup(struct device *uart)
|
2015-06-02 03:35:02 +08:00
|
|
|
{
|
2017-04-21 23:03:20 +08:00
|
|
|
u8_t c;
|
2016-03-24 20:51:26 +08:00
|
|
|
|
2015-06-02 03:35:02 +08:00
|
|
|
uart_irq_rx_disable(uart);
|
|
|
|
uart_irq_tx_disable(uart);
|
2016-01-08 16:46:14 +08:00
|
|
|
|
2015-06-02 03:35:02 +08:00
|
|
|
/* Drain the fifo */
|
2016-03-24 20:51:26 +08:00
|
|
|
while (uart_fifo_read(uart, &c, 1)) {
|
|
|
|
continue;
|
2015-06-02 03:35:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 02:14:50 +08:00
|
|
|
uart_irq_callback_set(uart, uart_pipe_isr);
|
|
|
|
|
2015-06-02 03:35:02 +08:00
|
|
|
uart_irq_rx_enable(uart);
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
void uart_pipe_register(u8_t *buf, size_t len, uart_pipe_recv_cb cb)
|
2015-06-02 03:35:02 +08:00
|
|
|
{
|
|
|
|
recv_buf = buf;
|
|
|
|
recv_buf_len = len;
|
|
|
|
app_cb = cb;
|
|
|
|
|
2015-12-02 00:42:20 +08:00
|
|
|
uart_pipe_dev = device_get_binding(CONFIG_UART_PIPE_ON_DEV_NAME);
|
|
|
|
|
|
|
|
if (uart_pipe_dev != NULL) {
|
|
|
|
uart_pipe_setup(uart_pipe_dev);
|
|
|
|
}
|
2015-06-02 03:35:02 +08:00
|
|
|
}
|