summaryrefslogtreecommitdiff
path: root/src/libs/mynewt-nimble/apps/bttester/src/uart_pipe.c
blob: 1118d9af577471cd9c3d8c23ee37248c1ec1837c (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
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 "syscfg/syscfg.h"

#if MYNEWT_VAL(BTTESTER_PIPE_UART)

#include "os/mynewt.h"
#include "uart/uart.h"

#include "bttester_pipe.h"

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

struct uart_pipe_ring {
    uint8_t head;
    uint8_t tail;
    uint16_t size;
    uint8_t *buf;
};

static struct uart_dev *uart_dev;
static struct uart_pipe_ring cr_tx;
static uint8_t cr_tx_buf[MYNEWT_VAL(BTTESTER_BTP_DATA_SIZE_MAX)];
typedef void (*console_write_char)(struct uart_dev*, uint8_t);
static console_write_char write_char_cb;

static struct uart_pipe_ring cr_rx;
static uint8_t cr_rx_buf[MYNEWT_VAL(BTTESTER_BTP_DATA_SIZE_MAX)];
static volatile bool uart_console_rx_stalled;

struct os_event rx_ev;

static inline int
inc_and_wrap(int i, int max)
{
    return (i + 1) & (max - 1);
}

static void
uart_pipe_ring_add_char(struct uart_pipe_ring *cr, char ch)
{
    cr->buf[cr->head] = ch;
    cr->head = inc_and_wrap(cr->head, cr->size);
}

static uint8_t
uart_pipe_ring_pull_char(struct uart_pipe_ring *cr)
{
    uint8_t ch;

    ch = cr->buf[cr->tail];
    cr->tail = inc_and_wrap(cr->tail, cr->size);
    return ch;
}

static bool
uart_pipe_ring_is_full(const struct uart_pipe_ring *cr)
{
    return inc_and_wrap(cr->head, cr->size) == cr->tail;
}

static bool
uart_pipe_ring_is_empty(const struct uart_pipe_ring *cr)
{
    return cr->head == cr->tail;
}

static void
uart_pipe_queue_char(struct uart_dev *uart_dev, uint8_t ch)
{
    int sr;

    if ((uart_dev->ud_dev.od_flags & OS_DEV_F_STATUS_OPEN) == 0) {
        return;
    }

    OS_ENTER_CRITICAL(sr);
    while (uart_pipe_ring_is_full(&cr_tx)) {
        /* TX needs to drain */
        uart_start_tx(uart_dev);
        OS_EXIT_CRITICAL(sr);
        if (os_started()) {
            os_time_delay(1);
        }
        OS_ENTER_CRITICAL(sr);
    }
    uart_pipe_ring_add_char(&cr_tx, ch);
    OS_EXIT_CRITICAL(sr);
}

/*
 * Interrupts disabled when console_tx_char/console_rx_char are called.
 * Characters sent only in blocking mode.
 */
static int
uart_console_tx_char(void *arg)
{
    if (uart_pipe_ring_is_empty(&cr_tx)) {
        return -1;
    }
    return uart_pipe_ring_pull_char(&cr_tx);
}

/*
 * Interrupts disabled when console_tx_char/console_rx_char are called.
 */
static int
uart_console_rx_char(void *arg, uint8_t byte)
{
    if (uart_pipe_ring_is_full(&cr_rx)) {
        uart_console_rx_stalled = true;
        return -1;
    }

    uart_pipe_ring_add_char(&cr_rx, byte);

    if (!rx_ev.ev_queued) {
        os_eventq_put(os_eventq_dflt_get(), &rx_ev);
    }

    return 0;
}

static int
uart_pipe_handle_char(int key)
{
    recv_buf[recv_off] = (uint8_t) key;
    recv_off++;

    return 0;
}

static void
uart_console_rx_char_event(struct os_event *ev)
{
    static int b = -1;
    int sr;
    int ret;

    /* We may have unhandled character - try it first */
    if (b >= 0) {
        ret = uart_pipe_handle_char(b);
        if (ret < 0) {
            return;
        }
    }

    while (!uart_pipe_ring_is_empty(&cr_rx)) {
        OS_ENTER_CRITICAL(sr);
        b = uart_pipe_ring_pull_char(&cr_rx);
        OS_EXIT_CRITICAL(sr);

        /* If UART RX was stalled due to a full receive buffer, restart RX now
         * that we have removed a byte from the buffer.
         */
        if (uart_console_rx_stalled) {
            uart_console_rx_stalled = false;
            uart_start_rx(uart_dev);
        }

        ret = uart_pipe_handle_char(b);
        if (ret < 0) {
            return;
        }
    }

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

    b = -1;
}

int
bttester_pipe_send(const uint8_t *data, int len)
{
    int i;

    /* Assure that there is a write cb installed; this enables to debug
     * code that is faulting before the console was initialized.
     */
    if (!write_char_cb) {
        return -1;
    }

    for (i = 0; i < len; ++i) {
        write_char_cb(uart_dev, data[i]);
    }

    uart_start_tx(uart_dev);

    return 0;
}

int
bttester_pipe_send_buf(struct os_mbuf *buf)
{
	int i, len;
	struct os_mbuf *om;

	/* Assure that there is a write cb installed; this enables to debug
	 * code that is faulting before the console was initialized.
	 */
	if (!write_char_cb) {
		return -1;
	}

	for (om = buf; om; om = SLIST_NEXT(om, om_next)) {
		len = om->om_len;
		for (i = 0; i < len; ++i) {
			write_char_cb(uart_dev, om->om_data[i]);
		}
	}

	uart_start_tx(uart_dev);

	return 0;
}

int
bttester_pipe_init(void)
{
    struct uart_conf uc = {
            .uc_speed = MYNEWT_VAL(CONSOLE_UART_BAUD),
            .uc_databits = 8,
            .uc_stopbits = 1,
            .uc_parity = UART_PARITY_NONE,
            .uc_flow_ctl = MYNEWT_VAL(CONSOLE_UART_FLOW_CONTROL),
            .uc_tx_char = uart_console_tx_char,
            .uc_rx_char = uart_console_rx_char,
    };

    cr_tx.size = sizeof(cr_tx_buf);
    cr_tx.buf = cr_tx_buf;
    write_char_cb = uart_pipe_queue_char;

    cr_rx.size = sizeof(cr_rx_buf);
    cr_rx.buf = cr_rx_buf;

    rx_ev.ev_cb = uart_console_rx_char_event;

    if (!uart_dev) {
        uart_dev = (struct uart_dev *)os_dev_open(MYNEWT_VAL(CONSOLE_UART_DEV),
                                                  OS_TIMEOUT_NEVER, &uc);
        if (!uart_dev) {
            return -1;
        }
    }
    return 0;
}

void
bttester_pipe_register(uint8_t *buf, size_t len, bttester_pipe_recv_cb cb)
{
	recv_buf = buf;
	recv_buf_len = len;
	app_cb = cb;
}
#endif /* MYNEWT_VAL(BTTESTER_PIPE_UART) */