forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockUART.cpp
More file actions
480 lines (398 loc) · 9.19 KB
/
MockUART.cpp
File metadata and controls
480 lines (398 loc) · 9.19 KB
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
470
471
472
473
474
475
476
477
478
479
480
/*
MockUART.cpp - esp8266 UART HAL EMULATION
Copyright (c) 2019 Clemens Kirchgatterer. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
This UART driver is directly derived from the ESP8266 UART HAL driver
Copyright (c) 2014 Ivan Grokhotkov. It provides the same API as the
original driver and was striped from all HW dependent interfaces.
UART0 writes got to stdout, while UART1 writes got to stderr. The user
is responsible for feeding the RX FIFO new data by calling uart_new_data().
*/
#include <unistd.h> // write
#include <sys/time.h> // gettimeofday
#include <time.h> // localtime
#include "Arduino.h"
#include "uart.h"
//#define UART_DISCARD_NEWEST
extern "C" {
static int s_uart_debug_nr = UART1;
static uart_t *UART[2] = { NULL, NULL };
struct uart_rx_buffer_
{
size_t size;
size_t rpos;
size_t wpos;
uint8_t * buffer;
};
struct uart_
{
int uart_nr;
int baud_rate;
bool rx_enabled;
bool tx_enabled;
bool rx_overrun;
struct uart_rx_buffer_ * rx_buffer;
};
bool serial_timestamp = false;
// write one byte to the emulated UART
static void
uart_do_write_char(const int uart_nr, char c)
{
static bool w = false;
if (uart_nr >= UART0 && uart_nr <= UART1)
{
if (serial_timestamp && (c == '\n' || c == '\r'))
{
if (w)
{
FILE* out = uart_nr == UART0? stdout: stderr;
timeval tv;
gettimeofday(&tv, nullptr);
const tm* tm = localtime(&tv.tv_sec);
fprintf(out, "\r\n%d:%02d:%02d.%06d: ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec);
fflush(out);
w = false;
}
}
else
{
write(uart_nr + 1, &c, 1);
w = true;
}
}
}
// write a new byte into the RX FIFO buffer
static void
uart_handle_data(uart_t* uart, uint8_t data)
{
struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer;
size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size;
if(nextPos == rx_buffer->rpos)
{
uart->rx_overrun = true;
#ifdef UART_DISCARD_NEWEST
return;
#else
if (++rx_buffer->rpos == rx_buffer->size)
rx_buffer->rpos = 0;
#endif
}
rx_buffer->buffer[rx_buffer->wpos] = data;
rx_buffer->wpos = nextPos;
}
// insert a new byte into the RX FIFO nuffer
void
uart_new_data(const int uart_nr, uint8_t data)
{
uart_t* uart = UART[uart_nr];
if(uart == NULL || !uart->rx_enabled) {
return;
}
uart_handle_data(uart, data);
}
static size_t
uart_rx_available_unsafe(const struct uart_rx_buffer_ * rx_buffer)
{
size_t ret = rx_buffer->wpos - rx_buffer->rpos;
if(rx_buffer->wpos < rx_buffer->rpos)
ret = (rx_buffer->wpos + rx_buffer->size) - rx_buffer->rpos;
return ret;
}
// taking data straight from fifo, only needed in uart_resize_rx_buffer()
static int
uart_read_char_unsafe(uart_t* uart)
{
if (uart_rx_available_unsafe(uart->rx_buffer))
{
// take oldest sw data
int ret = uart->rx_buffer->buffer[uart->rx_buffer->rpos];
uart->rx_buffer->rpos = (uart->rx_buffer->rpos + 1) % uart->rx_buffer->size;
return ret;
}
// unavailable
return -1;
}
/**********************************************************/
/************ UART API FUNCTIONS **************************/
/**********************************************************/
size_t
uart_rx_available(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return 0;
return uart_rx_available_unsafe(uart->rx_buffer);
}
int
uart_peek_char(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return -1;
if (!uart_rx_available_unsafe(uart->rx_buffer))
return -1;
return uart->rx_buffer->buffer[uart->rx_buffer->rpos];
}
int
uart_read_char(uart_t* uart)
{
uint8_t ret;
return uart_read(uart, (char*)&ret, 1) ? ret : -1;
}
size_t
uart_read(uart_t* uart, char* userbuffer, size_t usersize)
{
if(uart == NULL || !uart->rx_enabled)
return 0;
size_t ret = 0;
while (ret < usersize && uart_rx_available_unsafe(uart->rx_buffer))
{
// pour sw buffer to user's buffer
// get largest linear length from sw buffer
size_t chunk = uart->rx_buffer->rpos < uart->rx_buffer->wpos ?
uart->rx_buffer->wpos - uart->rx_buffer->rpos :
uart->rx_buffer->size - uart->rx_buffer->rpos;
if (ret + chunk > usersize)
chunk = usersize - ret;
memcpy(userbuffer + ret, uart->rx_buffer->buffer + uart->rx_buffer->rpos, chunk);
uart->rx_buffer->rpos = (uart->rx_buffer->rpos + chunk) % uart->rx_buffer->size;
ret += chunk;
}
return ret;
}
size_t
uart_resize_rx_buffer(uart_t* uart, size_t new_size)
{
if(uart == NULL || !uart->rx_enabled)
return 0;
if(uart->rx_buffer->size == new_size)
return uart->rx_buffer->size;
uint8_t * new_buf = (uint8_t*)malloc(new_size);
if(!new_buf)
return uart->rx_buffer->size;
size_t new_wpos = 0;
// if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1
while(uart_rx_available_unsafe(uart->rx_buffer) && new_wpos < new_size)
new_buf[new_wpos++] = uart_read_char_unsafe(uart);
if (new_wpos == new_size)
new_wpos = 0;
uint8_t * old_buf = uart->rx_buffer->buffer;
uart->rx_buffer->rpos = 0;
uart->rx_buffer->wpos = new_wpos;
uart->rx_buffer->size = new_size;
uart->rx_buffer->buffer = new_buf;
free(old_buf);
return uart->rx_buffer->size;
}
size_t
uart_get_rx_buffer_size(uart_t* uart)
{
return uart && uart->rx_enabled ? uart->rx_buffer->size : 0;
}
size_t
uart_write_char(uart_t* uart, char c)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
uart_do_write_char(uart->uart_nr, c);
return 1;
}
size_t
uart_write(uart_t* uart, const char* buf, size_t size)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
size_t ret = size;
const int uart_nr = uart->uart_nr;
while (size--)
uart_do_write_char(uart_nr, *buf++);
return ret;
}
size_t
uart_tx_free(uart_t* uart)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
return UART_TX_FIFO_SIZE;
}
void
uart_wait_tx_empty(uart_t* uart)
{
(void) uart;
}
void
uart_flush(uart_t* uart)
{
if(uart == NULL)
return;
if(uart->rx_enabled)
{
uart->rx_buffer->rpos = 0;
uart->rx_buffer->wpos = 0;
}
}
void
uart_set_baudrate(uart_t* uart, int baud_rate)
{
if(uart == NULL)
return;
uart->baud_rate = baud_rate;
}
int
uart_get_baudrate(uart_t* uart)
{
if(uart == NULL)
return 0;
return uart->baud_rate;
}
uint8_t
uart_get_bit_length(const int uart_nr)
{
uint8_t width = ((uart_nr % 16) >> 2) + 5;
uint8_t parity = (uart_nr >> 5) + 1;
uint8_t stop = uart_nr % 4;
return (width + parity + stop + 1);
}
uart_t*
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
{
(void) config;
(void) tx_pin;
uart_t* uart = (uart_t*) malloc(sizeof(uart_t));
if(uart == NULL)
return NULL;
uart->uart_nr = uart_nr;
uart->rx_overrun = false;
switch(uart->uart_nr)
{
case UART0:
uart->rx_enabled = (mode != UART_TX_ONLY);
uart->tx_enabled = (mode != UART_RX_ONLY);
if(uart->rx_enabled)
{
struct uart_rx_buffer_ * rx_buffer = (struct uart_rx_buffer_ *)malloc(sizeof(struct uart_rx_buffer_));
if(rx_buffer == NULL)
{
free(uart);
return NULL;
}
rx_buffer->size = rx_size;//var this
rx_buffer->rpos = 0;
rx_buffer->wpos = 0;
rx_buffer->buffer = (uint8_t *)malloc(rx_buffer->size);
if(rx_buffer->buffer == NULL)
{
free(rx_buffer);
free(uart);
return NULL;
}
uart->rx_buffer = rx_buffer;
}
break;
case UART1:
// Note: uart_interrupt_handler does not support RX on UART 1.
uart->rx_enabled = false;
uart->tx_enabled = (mode != UART_RX_ONLY);
break;
case UART_NO:
default:
// big fail!
free(uart);
return NULL;
}
uart_set_baudrate(uart, baudrate);
UART[uart_nr] = uart;
return uart;
}
void
uart_uninit(uart_t* uart)
{
if(uart == NULL)
return;
if(uart->rx_enabled) {
free(uart->rx_buffer->buffer);
free(uart->rx_buffer);
}
free(uart);
}
void
uart_swap(uart_t* uart, int tx_pin)
{
(void) uart;
(void) tx_pin;
}
void
uart_set_tx(uart_t* uart, int tx_pin)
{
(void) uart;
(void) tx_pin;
}
void
uart_set_pins(uart_t* uart, int tx, int rx)
{
(void) uart;
(void) tx;
(void) rx;
}
bool
uart_tx_enabled(uart_t* uart)
{
if(uart == NULL)
return false;
return uart->tx_enabled;
}
bool
uart_rx_enabled(uart_t* uart)
{
if(uart == NULL)
return false;
return uart->rx_enabled;
}
bool
uart_has_overrun(uart_t* uart)
{
if(uart == NULL || !uart->rx_overrun)
return false;
// clear flag
uart->rx_overrun = false;
return true;
}
bool
uart_has_rx_error(uart_t* uart)
{
(void) uart;
return false;
}
void
uart_set_debug(int uart_nr)
{
(void)uart_nr;
}
int
uart_get_debug()
{
return s_uart_debug_nr;
}
void
uart_start_detect_baudrate(int uart_nr)
{
(void) uart_nr;
}
int
uart_detect_baudrate(int uart_nr)
{
(void) uart_nr;
return 115200;
}
};