-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
sim_uart.c
795 lines (671 loc) · 20.1 KB
/
sim_uart.c
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/****************************************************************************
* arch/sim/src/sim/sim_uart.c
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/serial/serial.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/serial/uart_ram.h>
#include <nuttx/wqueue.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include "sim_internal.h"
#if defined(USE_DEVCONSOLE) || CONFIG_SIM_UART_NUMBER > 0
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define SIM_UART_WORK_DELAY USEC2TICK(1000)
#ifndef CONFIG_SIM_UART_BUFFER_SIZE
#define CONFIG_SIM_UART_BUFFER_SIZE 256
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct tty_priv_s
{
/* tty-port path name */
const char *path;
/* The file descriptor. It is returned by open */
int fd;
/* TX interrupt enable or not */
bool txint;
/* RX interrupt enable or not */
bool rxint;
/* Work queue for transmit */
struct work_s worker;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int tty_setup(struct uart_dev_s *dev);
static void tty_shutdown(struct uart_dev_s *dev);
static int tty_attach(struct uart_dev_s *dev);
static void tty_detach(struct uart_dev_s *dev);
static int tty_ioctl(struct file *filep, int cmd,
unsigned long arg);
static int tty_receive(struct uart_dev_s *dev, uint32_t *status);
static void tty_rxint(struct uart_dev_s *dev, bool enable);
static bool tty_rxavailable(struct uart_dev_s *dev);
static bool tty_rxflowcontrol(struct uart_dev_s *dev,
unsigned int nbuffered, bool upper);
#ifdef CONFIG_SIM_UART_DMA
static void tty_dmatxavail(struct uart_dev_s *dev);
static void tty_dmasend(struct uart_dev_s *dev);
static void tty_dmarxfree(struct uart_dev_s *dev);
static void tty_dmareceive(struct uart_dev_s *dev);
#endif
static void tty_send(struct uart_dev_s *dev, int ch);
static void tty_txint(struct uart_dev_s *dev, bool enable);
static bool tty_txready(struct uart_dev_s *dev);
static bool tty_txempty(struct uart_dev_s *dev);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct uart_ops_s g_tty_ops =
{
.setup = tty_setup,
.shutdown = tty_shutdown,
.attach = tty_attach,
.detach = tty_detach,
.ioctl = tty_ioctl,
.receive = tty_receive,
.rxint = tty_rxint,
.rxavailable = tty_rxavailable,
.rxflowcontrol = tty_rxflowcontrol,
#ifdef CONFIG_SIM_UART_DMA
.dmatxavail = tty_dmatxavail,
.dmasend = tty_dmasend,
.dmarxfree = tty_dmarxfree,
.dmareceive = tty_dmareceive,
#endif
.send = tty_send,
.txint = tty_txint,
.txready = tty_txready,
.txempty = tty_txempty,
};
#endif
#ifdef USE_DEVCONSOLE
static char g_console_rxbuf[CONFIG_SIM_UART_BUFFER_SIZE];
static char g_console_txbuf[CONFIG_SIM_UART_BUFFER_SIZE];
#endif
#ifdef CONFIG_SIM_UART0_NAME
static char g_tty0_rxbuf[CONFIG_SIM_UART_BUFFER_SIZE];
static char g_tty0_txbuf[CONFIG_SIM_UART_BUFFER_SIZE];
#endif
#ifdef CONFIG_SIM_UART1_NAME
static char g_tty1_rxbuf[CONFIG_SIM_UART_BUFFER_SIZE];
static char g_tty1_txbuf[CONFIG_SIM_UART_BUFFER_SIZE];
#endif
#ifdef CONFIG_SIM_UART2_NAME
static char g_tty2_rxbuf[CONFIG_SIM_UART_BUFFER_SIZE];
static char g_tty2_txbuf[CONFIG_SIM_UART_BUFFER_SIZE];
#endif
#ifdef CONFIG_SIM_UART3_NAME
static char g_tty3_rxbuf[CONFIG_SIM_UART_BUFFER_SIZE];
static char g_tty3_txbuf[CONFIG_SIM_UART_BUFFER_SIZE];
#endif
#ifdef USE_DEVCONSOLE
static struct tty_priv_s g_console_priv =
{
.path = "console",
.fd = 0,
};
static struct uart_dev_s g_console_dev =
{
.isconsole = true,
.ops = &g_tty_ops,
.priv = &g_console_priv,
.xmit =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_console_txbuf,
},
.recv =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_console_rxbuf,
},
};
#endif
#ifdef CONFIG_SIM_UART0_NAME
static struct tty_priv_s g_tty0_priv =
{
.path = CONFIG_SIM_UART0_NAME,
.fd = -1,
};
static struct uart_dev_s g_tty0_dev =
{
.ops = &g_tty_ops,
.priv = &g_tty0_priv,
.xmit =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty0_txbuf,
},
.recv =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty0_rxbuf,
},
};
#endif
#ifdef CONFIG_SIM_UART1_NAME
static struct tty_priv_s g_tty1_priv =
{
.path = CONFIG_SIM_UART1_NAME,
.fd = -1,
};
static struct uart_dev_s g_tty1_dev =
{
.ops = &g_tty_ops,
.priv = &g_tty1_priv,
.xmit =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty1_txbuf,
},
.recv =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty1_rxbuf,
},
};
#endif
#ifdef CONFIG_SIM_UART2_NAME
static struct tty_priv_s g_tty2_priv =
{
.path = CONFIG_SIM_UART2_NAME,
.fd = -1,
};
static struct uart_dev_s g_tty2_dev =
{
.ops = &g_tty_ops,
.priv = &g_tty2_priv,
.xmit =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty2_txbuf,
},
.recv =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty2_rxbuf,
},
};
#endif
#ifdef CONFIG_SIM_UART3_NAME
static struct tty_priv_s g_tty3_priv =
{
.path = CONFIG_SIM_UART3_NAME,
.fd = -1,
};
static struct uart_dev_s g_tty3_dev =
{
.ops = &g_tty_ops,
.priv = &g_tty3_priv,
.xmit =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty3_txbuf,
},
.recv =
{
.size = CONFIG_SIM_UART_BUFFER_SIZE,
.buffer = g_tty3_rxbuf,
},
};
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
#if defined(USE_DEVCONSOLE) || CONFIG_SIM_UART_NUMBER > 0
/****************************************************************************
* Name: uart_nputs
*
* Description:
* Loop to write data to the UART until all the data is sent
*
****************************************************************************/
static void uart_nputs(int fd, const char *buf, size_t size)
{
while (size > 0)
{
int ret = host_uart_puts(fd, buf, size);
if (ret < 0)
{
continue;
}
buf += ret;
size -= ret;
}
}
/****************************************************************************
* Name: tty_setup
*
* Description:
* Configure the UART baud, bits, parity, fifos, etc. This
* method is called the first time that the serial port is
* opened.
*
****************************************************************************/
static int tty_setup(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
priv->fd = host_uart_open(priv->path);
return priv->fd;
}
/****************************************************************************
* Name: tty_shutdown
*
* Description:
* Disable the UART. This method is called when the serial
* port is closed
*
****************************************************************************/
static void tty_shutdown(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
/* close file Description and reset fd */
host_uart_close(priv->fd);
priv->fd = -1;
}
/****************************************************************************
* Name: tty_attach
*
* Description:
* Configure the UART to operation in interrupt driven mode. This method
* is called when the serial port is opened. Normally, this is just after
* the setup() method is called, however, the serial console may operate in
* a non-interrupt driven mode during the boot phase.
*
* RX and TX interrupts are not enabled when by the attach method (unless
* the hardware supports multiple levels of interrupt enabling). The RX
* and TX interrupts are not enabled until the txint() and rxint() methods
* are called.
*
****************************************************************************/
static int tty_attach(struct uart_dev_s *dev)
{
return OK;
}
/****************************************************************************
* Name: tty_detach
*
* Description:
* Detach UART interrupts. This method is called when the serial port is
* closed normally just before the shutdown method is called. The
* exception is the serial console which is never shutdown.
*
****************************************************************************/
static void tty_detach(struct uart_dev_s *dev)
{
}
/****************************************************************************
* Name: tty_ioctl
*
* Description:
* All ioctl calls will be routed through this method
*
****************************************************************************/
static int tty_ioctl(struct file *filep, int cmd, unsigned long arg)
{
#ifdef CONFIG_SERIAL_TERMIOS
struct inode *inode = filep->f_inode;
struct uart_dev_s *dev = inode->i_private;
struct tty_priv_s *priv = dev->priv;
struct termios *termiosp = (struct termios *)(uintptr_t)arg;
switch (cmd)
{
case TCGETS:
DEBUGASSERT(termiosp != NULL);
return host_uart_getcflag(priv->fd, &termiosp->c_cflag);
case TCSETS:
DEBUGASSERT(termiosp != NULL);
return host_uart_setcflag(priv->fd, termiosp->c_cflag);
default:
break;
}
#endif
return -ENOTTY;
}
/****************************************************************************
* Name: tty_receive
*
* Description:
* Called (usually) from the interrupt level to receive one
* character from the UART. Error bits associated with the
* receipt are provided in the return 'status'.
*
****************************************************************************/
static int tty_receive(struct uart_dev_s *dev, uint32_t *status)
{
struct tty_priv_s *priv = dev->priv;
char ch = 0;
*status = 0;
host_uart_gets(priv->fd, &ch, 1);
return ch;
}
/****************************************************************************
* Name: tty_work
*
* Description:
* Notify DMA that there is data to be transferred in the TX buffer
*
****************************************************************************/
static void tty_work(void *arg)
{
struct uart_dev_s *dev = arg;
struct tty_priv_s *priv = dev->priv;
if (priv->fd < 0)
{
return;
}
if (priv->txint)
{
#ifdef CONFIG_SIM_UART_DMA
uart_dmatxavail(dev);
#else
uart_xmitchars(dev);
#endif
}
if (priv->rxint)
{
#ifdef CONFIG_SIM_UART_DMA
uart_dmarxfree(dev);
#else
uart_recvchars(dev);
#endif
}
work_queue(HPWORK, &priv->worker,
tty_work, dev, SIM_UART_WORK_DELAY);
}
/****************************************************************************
* Name: tty_rxint
*
* Description:
* Call to enable or disable RX interrupts
*
****************************************************************************/
static void tty_rxint(struct uart_dev_s *dev, bool enable)
{
struct tty_priv_s *priv = dev->priv;
priv->rxint = enable;
if (enable)
{
work_queue(HPWORK, &priv->worker, tty_work, dev, 0);
}
}
/****************************************************************************
* Name: tty_rxavailable
*
* Description:
* Return true if the receive fifo is not empty
*
****************************************************************************/
static bool tty_rxavailable(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
return host_uart_checkin(priv->fd);
}
/****************************************************************************
* Name: tty_rxflowcontrol
*
* Description:
* Return true if UART activated RX flow control to block more incoming
* data.
*
****************************************************************************/
static bool tty_rxflowcontrol(struct uart_dev_s *dev,
unsigned int nbuffered, bool upper)
{
struct uart_buffer_s *rxbuf = &dev->recv;
if (nbuffered == rxbuf->size)
{
return true;
}
return false;
}
#ifdef CONFIG_SIM_UART_DMA
/****************************************************************************
* Name: tty_dmatxavail
*
* Description:
* Notify DMA that there is data to be transferred in the TX buffer
*
****************************************************************************/
static void tty_dmatxavail(struct uart_dev_s *dev)
{
if (uart_txready(dev))
{
uart_xmitchars_dma(dev);
}
}
/****************************************************************************
* Name: tty_dmasend
*
* Description:
* Start transfer bytes from the TX circular buffer using DMA
*
****************************************************************************/
static void tty_dmasend(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
struct uart_dmaxfer_s *xfer = &dev->dmatx;
int fd = dev->isconsole ? 1 : priv->fd;
int ret;
xfer->nbytes = 0;
ret = host_uart_puts(fd, xfer->buffer, xfer->length);
if (ret > 0)
{
xfer->nbytes = ret;
if (ret == xfer->length && xfer->nlength > 0)
{
ret = host_uart_puts(fd, xfer->nbuffer, xfer->nlength);
if (ret > 0)
{
xfer->nbytes += ret;
}
}
}
uart_xmitchars_done(dev);
}
/****************************************************************************
* Name: tty_dmarxfree
*
* Description:
* Notify DMA that there is free space in the RX buffer
*
****************************************************************************/
static void tty_dmarxfree(struct uart_dev_s *dev)
{
if (uart_rxavailable(dev))
{
uart_recvchars_dma(dev);
}
}
/****************************************************************************
* Name: tty_dmareceive
*
* Description:
* Start receive bytes from the RX circular buffer using DMA
*
****************************************************************************/
static void tty_dmareceive(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
struct uart_dmaxfer_s *xfer = &dev->dmarx;
int ret;
xfer->nbytes = 0;
ret = host_uart_gets(priv->fd, xfer->buffer, xfer->length);
if (ret > 0)
{
xfer->nbytes = ret;
if (ret == xfer->length && xfer->nlength > 0)
{
ret = host_uart_gets(priv->fd, xfer->nbuffer, xfer->nlength);
if (ret > 0)
{
xfer->nbytes += ret;
}
}
}
uart_recvchars_done(dev);
}
#endif
/****************************************************************************
* Name: tty_send
*
* Description:
* This method will send one byte on the UART
*
****************************************************************************/
static void tty_send(struct uart_dev_s *dev, int ch)
{
struct tty_priv_s *priv = dev->priv;
char c = ch;
uart_nputs(dev->isconsole ? 1 : priv->fd, &c, 1);
}
/****************************************************************************
* Name: tty_txint
*
* Description:
* Call to enable or disable TX interrupts
*
****************************************************************************/
static void tty_txint(struct uart_dev_s *dev, bool enable)
{
struct tty_priv_s *priv = dev->priv;
priv->txint = enable;
if (enable)
{
work_queue(HPWORK, &priv->worker, tty_work, dev, 0);
}
}
/****************************************************************************
* Name: uart_txready
*
* Description:
* Return true if the tranmsit fifo is not full
*
****************************************************************************/
static bool tty_txready(struct uart_dev_s *dev)
{
struct tty_priv_s *priv = dev->priv;
return host_uart_checkout(dev->isconsole ? 1 : priv->fd);
}
/****************************************************************************
* Name: tty_txempty
*
* Description:
* Return true if the transmit fifo is empty
*
****************************************************************************/
static bool tty_txempty(struct uart_dev_s *dev)
{
return true;
}
#endif
#ifdef CONFIG_SIM_RAM_UART
static int sim_uartram_register(const char *devname, bool slave)
{
char name[NAME_MAX];
struct uart_rambuf_s *shmem;
strlcpy(name, strrchr(devname, '/') + 1, NAME_MAX);
shmem = host_allocshmem(name, sizeof(struct uart_rambuf_s) * 2);
DEBUGASSERT(shmem);
memset(shmem, 0, sizeof(struct uart_rambuf_s) * 2);
return uart_ram_register(devname, shmem, slave);
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sim_uartinit
****************************************************************************/
void sim_uartinit(void)
{
#ifdef CONFIG_SIM_RAM_UART0
# ifdef CONFIG_SIM_RAM_UART0_SLAVE
sim_uartram_register("/dev/ttyVS0", true);
# else
sim_uartram_register("/dev/ttyVS0", false);
# endif
#endif
#ifdef CONFIG_SIM_RAM_UART1
# ifdef CONFIG_SIM_RAM_UART1_SLAVE
sim_uartram_register("/dev/ttyVS1", true);
# else
sim_uartram_register("/dev/ttyVS1", false);
# endif
#endif
#ifdef CONFIG_SIM_RAM_UART2
# ifdef CONFIG_SIM_RAM_UART2_SLAVE
sim_uartram_register("/dev/ttyVS2", true);
# else
sim_uartram_register("/dev/ttyVS2", false);
# endif
#endif
#ifdef USE_DEVCONSOLE
/* Start the simulated UART device */
host_uart_start();
/* Register console device */
uart_register("/dev/console", &g_console_dev);
#endif
#ifdef CONFIG_SIM_UART0_NAME
uart_register(CONFIG_SIM_UART0_NAME, &g_tty0_dev);
#endif
#ifdef CONFIG_SIM_UART1_NAME
uart_register(CONFIG_SIM_UART1_NAME, &g_tty1_dev);
#endif
#ifdef CONFIG_SIM_UART2_NAME
uart_register(CONFIG_SIM_UART2_NAME, &g_tty2_dev);
#endif
#ifdef CONFIG_SIM_UART3_NAME
uart_register(CONFIG_SIM_UART3_NAME, &g_tty3_dev);
#endif
}
/****************************************************************************
* Name: up_nputs
*
* Description:
* This is a low-level helper function used to support debug.
*
****************************************************************************/
void up_nputs(const char *str, size_t len)
{
#ifdef USE_DEVCONSOLE
uart_nputs(1, str, len);
#endif
}
/****************************************************************************
* Name: up_putc
****************************************************************************/
void up_putc(int ch)
{
#ifdef USE_DEVCONSOLE
char c = ch;
up_nputs(&c, 1);
#endif
}