summaryrefslogtreecommitdiff
path: root/ports/risc-v32/gnu/example_build/cva6_ariane/uart.h
blob: 0a835727d90c82686951d4fae9702e44db363e16 (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
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026 Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

#ifndef RISCV_UART_H
#define RISCV_UART_H

#define UART0 		0x10000000L
#define UART0_IRQ 	10

#define puts(str) uart_puts(str"\n\r")
int uart_init(void);
int uart_putc(int ch);
int uart_puts(const char* str);
#endif


/*******************************************************************************
 * Copyright (c) 2020 Thales.
 * Copyright 2019-2020 Microchip FPGA Embedded Systems Solutions.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 *
 */
// Additional contributions by:
//         Sebastien Jacq - sjthales on github.com
//         Anjali Gedam - anjaliigedam on github.com
//
// Description: Driver header for UART Ip of the CVA6 platform
//
// =========================================================================== //
// Revisions  :
// Date        Version  Author       Description
// 2020-10-06  0.1      S.Jacq       modification of the Test for CVA6 softcore
// 2021-07-12  0.2      Anjali G     modification for the CVA6 FPGA
// =========================================================================== //

#ifndef __UART1_H_
#define __UART1_H_ 1

#include <stddef.h>
#include <stdint.h>


/***************************************************************************//**
  Baud rates
  ==========
  The following definitions are used to specify standard baud rates as a
  parameter to the UART_init() function.
  
  | Constant             | Description      |
  |----------------------|------------------|
  | UART_110_BAUD    |    110 baud rate |
  | UART_300_BAUD    |    300 baud rate |
  | UART_600_BAUD    |    600 baud rate |
  | UART_1200_BAUD   |   1200 baud rate |
  | UART_2400_BAUD   |   2400 baud rate |
  | UART_4800_BAUD   |   4800 baud rate |
  | UART_9600_BAUD   |   9600 baud rate |
  | UART_19200_BAUD  |  19200 baud rate |
  | UART_38400_BAUD  |  38400 baud rate |
  | UART_57600_BAUD  |  57600 baud rate |
  | UART_115200_BAUD | 115200 baud rate |
  | UART_230400_BAUD | 230400 baud rate |
  | UART_460800_BAUD | 460800 baud rate |
  | UART_921600_BAUD | 921600 baud rate |
  
 */
#define UART_110_BAUD       110U
#define UART_300_BAUD       300U
#define UART_600_BAUD       600U
#define UART_1200_BAUD      1200U
#define UART_2400_BAUD      2400U
#define UART_4800_BAUD      4800U
#define UART_9600_BAUD      9600U
#define UART_19200_BAUD     19200U
#define UART_38400_BAUD     38400U
#define UART_57600_BAUD     57600U
#define UART_115200_BAUD    115200U
#define UART_230400_BAUD    230400U
#define UART_460800_BAUD    460800U
#define UART_921600_BAUD    921600U

/***************************************************************************//**
  Data Bits Length
  ================
  The following defines are used to build the value of the UART_init()
  function line_config parameter.
  
  | Constant             | Description                |
  |----------------------|----------------------------|
  | UART_DATA_5_BITS | 5 bits of data transmitted |
  | UART_DATA_6_BITS | 6 bits of data transmitted |
  | UART_DATA_7_BITS | 7 bits of data transmitted |
  | UART_DATA_8_BITS | 8 bits of data transmitted |
  
 */
#define UART_DATA_5_BITS     ((uint8_t) 0x00)
#define UART_DATA_6_BITS     ((uint8_t) 0x01)
#define UART_DATA_7_BITS     ((uint8_t) 0x02)
#define UART_DATA_8_BITS     ((uint8_t) 0x03)

/***************************************************************************//**
  Parity
  ======
  The following defines are used to build the value of the UART_init()
  function line_config parameter.
  
  | Constant                | Description              |
  |-------------------------|--------------------------|
  | UART_NO_PARITY      | No parity                |
  | UART_ODD_PARITY     | Odd Parity               |
  | UART_EVEN_PARITY    | Even parity              |
  | UART_STICK_PARITY_0 | Stick parity bit to zero |
  | UART_STICK_PARITY_1 | Stick parity bit to one  |
 */
#define UART_NO_PARITY           ((uint8_t) 0x00)
#define UART_ODD_PARITY          ((uint8_t) 0x08)
#define UART_EVEN_PARITY         ((uint8_t) 0x18)
#define UART_STICK_PARITY_0      ((uint8_t) 0x38)
#define UART_STICK_PARITY_1      ((uint8_t) 0x28)

/***************************************************************************//**
  Number of Stop Bits
  ===================
  The following defines are used to build the value of the UART_init()
  function line_config parameter.
  
  | Constant                  | Description              |
  |---------------------------|--------------------------|
  | UART_ONE_STOP_BIT     | One stop bit             |
  | UART_ONEHALF_STOP_BIT | One and a half stop bits |
  | UART_TWO_STOP_BITS    | Two stop bits            |
  
 */
#define UART_ONE_STOP_BIT        ((uint8_t) 0x00)
#define UART_ONEHALF_STOP_BIT    ((uint8_t) 0x04)
#define UART_TWO_STOP_BITS       ((uint8_t) 0x04)

/***************************************************************************//**
  Receiver Error Status
  =====================
  The following defines are used to determine the UART receiver error type.
  These bit mask constants are used with the return value of the
  UART_get_rx_status() function to find out if any errors occurred while
  receiving data.
  
  
  | Constant               | Description                                |
  |------------------------|--------------------------------------------|
  | UART_NO_ERROR      | No error bit mask (0x00)                   |
  | UART_OVERUN_ERROR  | Overrun error bit mask (0x02)              |
  | UART_PARITY_ERROR  | Parity error bit mask (0x04)               |
  | UART_FRAMING_ERROR | Framing error bit mask (0x08)              |
  | UART_BREAK_ERROR   | Break error bit mask (0x10)                |
  | UART_FIFO_ERROR    | FIFO error bit mask (0x80)                 |
  | UART_INVALID_PARAM | Invalid function parameter bit mask (0xFF) |
 */
#define UART_INVALID_PARAM    ((uint8_t)0xFF)
#define UART_NO_ERROR         ((uint8_t)0x00 )
#define UART_OVERUN_ERROR     ((uint8_t)0x02)
#define UART_PARITY_ERROR     ((uint8_t)0x04)
#define UART_FRAMING_ERROR    ((uint8_t)0x08)
#define UART_BREAK_ERROR      ((uint8_t)0x10)
#define UART_FIFO_ERROR       ((uint8_t)0x80)


/*******************************************************************************
 * Receiver error status mask.
 */
#define STATUS_ERROR_MASK    ( UART_OVERUN_ERROR   | UART_PARITY_ERROR | \
                               UART_FRAMING_ERROR  | UART_BREAK_ERROR  | \
                               UART_FIFO_ERROR)

/***************************************************************************//**
  Transmitter Status
  ==================
  The following definitions are used to determine the UART transmitter status.
  These bit mask constants are used with the return value of the
  UART_get_tx_status() function to find out the status of the transmitter.
    
  | Constant         | Description                                        |
  |------------------|----------------------------------------------------|
  | UART_TX_BUSY | Transmitter busy (0x00)                            |
  | UART_THRE    | Transmitter holding register empty bit mask (0x20) |
  | UART_TEMT    | Transmitter empty bit mask (0x40)                  |
  
 */
#define UART_TX_BUSY          ((uint8_t) 0x00)
#define UART_THRE             ((uint8_t) 0x20)
#define UART_TEMT             ((uint8_t) 0x40)

/***************************************************************************//**
  Modem Status
  ============
  The following defines are used to determine the modem status. These bit
  mask constants are used with the return value of the
  UART_get_modem_status() function to find out the modem status of
  the UART.
  
  | Constant      | Description                                     |
  |---------------|-------------------------------------------------|
  | UART_DCTS | Delta clear to send bit mask (0x01)             |
  | UART_DDSR | Delta data set ready bit mask (0x02)            |
  | UART_TERI | Trailing edge of ring indicator bit mask (0x04) |
  | UART_DDCD | Delta data carrier detect bit mask (0x08)       |
  | UART_CTS  | Clear to send bit mask (0x10)                   |
  | UART_DSR  | Data set ready bit mask (0x20)                  |
  | UART_RI   | Ring indicator bit mask (0x40)                  |
  | UART_DCD  | Data carrier detect bit mask (0x80)             |
 */
#define UART_DCTS             ((uint8_t) 0x01)
#define UART_DDSR             ((uint8_t) 0x02)
#define UART_TERI             ((uint8_t) 0x04)
#define UART_DDCD             ((uint8_t) 0x08)
#define UART_CTS              ((uint8_t) 0x10)
#define UART_DSR              ((uint8_t) 0x20)
#define UART_RI               ((uint8_t) 0x40)
#define UART_DCD              ((uint8_t) 0x80)

/***************************************************************************//**
  This typedef specifies the irq_mask parameter for the UART_enable_irq()
  and UART_disable_irq() functions. The driver defines a set of bit masks
  that are used to build the value of the irq_mask parameter. A bitwise OR of
  these bit masks is used to enable or disable multipleUART interrupts.
 */
typedef uint16_t uart_irq_t;

/***************************************************************************//**
  UART Interrupts
  =====================
  The following defines specify the interrupt masks to enable and disable
  UART interrupts. They are used to build the value of the irq_mask parameter
  for the UART_enable_irq() and UART_disable_irq() functions. A bitwise
  OR of these constants is used to enable or disable multiple interrupts.
  
  
  | Constant           | Description                                                   |
  |--------------------|---------------------------------------------------------------|
  | UART_RBF_IRQ   | Receive Data Available Interrupt bit mask (0x001)             |
  | UART_TBE_IRQ   | Transmitter Holding Register Empty interrupt bit mask (0x002) |
  | UART_LS_IRQ    | Receiver Line Status interrupt bit mask (0x004)               |
  | UART_MS_IRQ    | Modem Status interrupt bit mask (0x008)                       |

 */
#define UART_RBF_IRQ        0x001
#define UART_TBE_IRQ        0x002
#define UART_LS_IRQ         0x004
#define UART_MS_IRQ         0x008
//#define UART_RTO_IRQ        0x010
//#define UART_NACK_IRQ       0x020
//#define UART_PIDPE_IRQ      0x040
//#define UART_LINB_IRQ       0x080
//#define UART_LINS_IRQ       0x100
#define UART_INVALID_IRQ    UINT16_MAX

/***************************************************************************//**
  This enumeration specifies the receiver FIFO trigger level. This is the number
  of bytes that must be received before the UART generates a receive data
  available interrupt. It provides the allowed values for the
  UART_set_rx_handler() function trigger_level parameter.
 */
typedef enum {
    UART_FIFO_SINGLE_BYTE    = 0x00,
    UART_FIFO_FOUR_BYTES     = 0x40,
    UART_FIFO_EIGHT_BYTES    = 0x80,
    UART_FIFO_FOURTEEN_BYTES = 0xC0,
    UART_FIFO_INVALID_TRIG_LEVEL

} uart_rx_trig_level_t;




/***************************************************************************//**
  UART instance type.
  This is type definition for UART instance. You need to create and
  maintain a record of this type. This holds all data regarding the UART
  instance
 */
typedef struct  uart_instance uart_instance_t;

/***************************************************************************//**
  Interrupt handler prototype.
  This typedef specifies the function prototype for UART interrupt handlers.
  All interrupt handlers registered with the UART driver must be of this type.
  The interrupt handlers are registered with the driver through the
  UART_set_rx_handler(), UART_set_tx_handler(),
  UART_set_rxstatus_handler(), and UART_set_modemstatus_handler()
  functions.
  The this_uart parameter is a pointer to either g_uart0 or g_uart1 to
  identify the UART to associate with the handler function.
 */
typedef void (*uart_irq_handler_t)( uart_instance_t * this_uart );


/*******************************************************************************
 Register Bit definitions
 */

/* Line Control register bit definitions */
#define SB                          6u                  /* Set break */
#define DLAB                        7u                  /* Divisor latch access bit */

/* Line Control register bit masks */
#define SB_MASK                     (0x01u << SB)        /* Set break */
#define DLAB_MASK                   (0x01u << DLAB)      /* Divisor latch access bit */

/* FIFO Control register bit definitions */
#define ENABLE_FIFO                 0u                  /* Enable FIFO */
#define CLEAR_RX_FIFO               1u                  /* Clear receiver FIFO */
#define CLEAR_TX_FIFO               2u                  /* Clear transmitter FIFO */
#define DMA_MODE                    3u                  /* DMA mode */
#define FIFO64_ENABLE               5u                  /* FIFO64 enable */
#define RDYMODE                     3u                  /* Mode 0 or Mode 1 for TXRDY and RXRDY */
#define FIFO_RX_TRIGGER             6u                  /* FIFO RX trigger */

/* FIFO Control register bit MASKS */
#define RXRDY_TXRDYN_EN_MASK           (0x01u << 0u)      /* Enable TXRDY and RXRDY signals */
#define CLEAR_RX_FIFO_MASK             (0x01u << 1u)      /* Clear receiver FIFO */
#define CLEAR_TX_FIFO_MASK             (0x01u << 2u)      /* Clear transmitter FIFO */
#define DMA_MODE_MASK                  (0x01u << 3u)      /* DMA mode */
#define FIFO64_ENABLE_MASK             (0x01u << 5u)      /* FIFO64 enable */
#define RDYMODE_MASK                 (0x01u << 3u)      /* Mode 0 or Mode 1 for TXRDY and RXRDY */

#define FIFO_RX_TRIGGER_LEVEL_4_MASK   (0x01u << 6u)
#define FIFO_RX_TRIGGER_LEVEL_8_MASK   (0x02u << 6u)
#define FIFO_RX_TRIGGER_LEVEL_14_MASK   (0x03u << 6u)

#define FIFO64_RX_TRIGGER_LEVEL_16_MASK   (0x01u << 6u)
#define FIFO64_RX_TRIGGER_LEVEL_32_MASK   (0x02u << 6u)
#define FIFO64_RX_TRIGGER_LEVEL_56_MASK   (0x03u << 6u)

/* Modem Control register bit definitions */
//#define LOOP                        4u                  /* Local loopback */
//#define RLOOP                       5u                  /* Remote loopback */
//#define ECHO                        6u                  /* Automatic echo */

/* Modem Control register bit MASKS */
//#define LOOP_MASK                   (0x01u << 4u)        /* Local loopback */
//#define RLOOP_MASK                  (0x01u << 5u)        /* Remote loopback & Automatic echo*/
//#define ECHO_MASK                   (0x01u << 6u)        /* Automatic echo */

/* Line Status register bit definitions   */
#define DR                          0u                  /* Data ready */
#define THRE                        5u                  /* Transmitter holding register empty */
#define TEMT                        6u                  /* Transmitter empty */

/* Line Status register bit MASKS   */
#define DR_MASK                     (0x01u << 0u)        /* Data ready */
#define THRE_MASK                   (0x01u << 5u)        /* Transmitter holding register empty */
#define TEMT_MASK                   (0x01u << 6u)        /* Transmitter empty */

/* Interrupt Enable register bit definitions */
#define ERBFI                       0u                  /* Enable receiver buffer full interrupt */
#define ETBEI                       1u                  /* Enable transmitter buffer empty interrupt */
#define ELSI                        2u                  /* Enable line status interrupt */
#define EDSSI                       3u                  /* Enable modem status interrupt */

/* Interrupt Enable register bit MASKS */
#define ERBFI_MASK                  (0x01u << 0u)      /* Enable receiver buffer full interrupt */
#define ETBEI_MASK                  (0x01u << 1u)      /* Enable transmitter buffer empty interrupt */
#define ELSI_MASK                   (0x01u << 2u)      /* Enable line status interrupt */
#define EDSSI_MASK                  (0x01u << 3u)      /* Enable modem status interrupt */

/* Multimode register 0 bit definitions */
#define ELIN                        3u                  /* Enable LIN header detection */
#define ETTG                        5u                  /* Enable transmitter time guard */
#define ERTO                        6u                  /* Enable receiver time-out */
#define EFBR                        7u                  /* Enable fractional baud rate mode */

/* Multimode register 0 bit MASKS */
#define ELIN_MASK                   (0x01u << 3u)      /* Enable LIN header detection */
#define ETTG_MASK                   (0x01u << 5u)      /* Enable transmitter time guard */
#define ERTO_MASK                   (0x01u << 6u)      /* Enable receiver time-out */
#define EFBR_MASK                   (0x01u << 7u)      /* Enable fractional baud rate mode */

/* Multimode register 1 bit definitions */
#define E_MSB_RX                    0u                  /* MSB / LSB first for receiver */
#define E_MSB_TX                    1u                  /* MSB / LSB first for transmitter */
#define EIRD                        2u                  /* Enable IrDA modem */
#define EIRX                        3u                  /* Input polarity for IrDA modem */
#define EITX                        4u                  /* Output polarity for IrDA modem */
#define EITP                        5u                  /* Output pulse width for IrDA modem */

/* Multimode register 1 bit MASKS */
#define E_MSB_RX_MASK               (0x01u << 0u)      /* MSB / LSB first for receiver */
#define E_MSB_TX_MASK               (0x01u << 1u)      /* MSB / LSB first for transmitter */
#define EIRD_MASK                   (0x01u << 2u)      /* Enable IrDA modem */
#define EIRX_MASK                   (0x01u << 3u)      /* Input polarity for IrDA modem */
#define EITX_MASK                   (0x01u << 4u)      /* Output polarity for IrDA modem */
#define EITP_MASK                   (0x01u << 5u)      /* Output pulse width for IrDA modem */

/* Multimode register 2 bit definitions */
//#define EERR                        0u                  /* Enable ERR / NACK during stop time */
//#define EAFM                        1u                  /* Enable 9-bit address flag mode */
//#define EAFC                        2u                  /* Enable address flag clear */
//#define ESWM                        3u                  /* Enable single wire half-duplex mode */

/* Multimode register 2 bit MASKS */
//#define EERR_MASK                   (0x01u << 0u)      /* Enable ERR / NACK during stop time */
//#define EAFM_MASK                   (0x01u << 1u)      /* Enable 9-bit address flag mode */
//#define EAFC_MASK                   (0x01u << 2u)      /* Enable address flag clear */
//#define ESWM_MASK                   (0x01u << 3u)      /* Enable single wire half-duplex mode */

/* Multimode Interrupt Enable register and
   Multimode Interrupt Identification register definitions */
//#define ERTOI                       0u                  /* Enable receiver timeout interrupt */
//#define ENACKI                      1u                  /* Enable NACK / ERR interrupt */
//#define EPID_PEI                    2u                  /* Enable PID parity error interrupt */
//#define ELINBI                      3u                  /* Enable LIN break interrupt */
//#define ELINSI                      4u                  /* Enable LIN sync detection interrupt */

/* Multimode Interrupt Enable register and
   Multimode Interrupt Identification register MASKS */
//#define ERTOI_MASK                  (0x01u << 0u)      /* Enable receiver timeout interrupt */
//#define ENACKI_MASK                 (0x01u << 1u)      /* Enable NACK / ERR interrupt */
//#define EPID_PEI_MASK               (0x01u << 2u)      /* Enable PID parity error interrupt */
//#define ELINBI_MASK                 (0x01u << 3u)      /* Enable LIN break interrupt */
//#define ELINSI_MASK                 (0x01u << 4u)      /* Enable LIN sync detection interrupt */

typedef struct
{
    union
    {
        volatile const  uint8_t    RBR;
        volatile uint8_t    THR;
        volatile uint8_t    DLL;
             uint32_t   RESERVED0;
    };

    union
    {
        volatile uint8_t  DLM;
        volatile uint8_t  IER;
             uint32_t RESERVED1;
    };

    union
    {
        volatile uint8_t  IIR;
        volatile uint8_t  FCR;
             uint32_t RESERVED2;
    };

    volatile uint8_t  LCR;
         uint8_t  RESERVED3[3];

    volatile uint8_t  MCR;
         uint8_t  RESERVED4[3];

    volatile const  uint8_t  LSR;
         uint8_t  RESERVED5[3];

    volatile const  uint8_t  MSR;
         uint8_t  RESERVED6[3];

    volatile uint8_t  SR;
         uint8_t  RESERVED7[7];

} UART_TypeDef;


/***************************************************************************//**
  uart_instance.
  There is one instance of this structure for each instance of the
  microprocessor subsystem's UARTs. Instances of this structure are used to
  identify a specific UART. A pointer to an initialized instance of the
  uart_instance_t structure is passed as the first parameter to
  UART driver functions to identify which UART should perform the
  requested operation.
 */
struct uart_instance{
    /* CMSIS related defines identifying the UART hardware. */
    UART_TypeDef *      hw_reg;     /*!< Pointer to UART registers. */
    uint32_t            baudrate;   /*!< Operating baud rate. */
    uint8_t             lineconfig; /*!< Line configuration parameters. */
    uint8_t             status;     /*!< Sticky line status. */

    /* transmit related info (used with interrupt driven transmit): */
    const uint8_t * tx_buffer;          /*!< Pointer to transmit buffer. */
    uint32_t        tx_buff_size;       /*!< Transmit buffer size. */
    uint32_t        tx_idx;             /*!< Index within transmit buffer of next byte to transmit.*/

    /* line status interrupt handler:*/
    uart_irq_handler_t linests_handler;   /*!< Pointer to user registered line status handler. */
    /* receive interrupt handler:*/
    uart_irq_handler_t rx_handler;        /*!< Pointer to user registered receiver handler. */
    /* transmit interrupt handler:*/
    uart_irq_handler_t tx_handler;        /*!< Pointer to user registered transmit handler. */
    /* modem status interrupt handler:*/
    uart_irq_handler_t modemsts_handler;  /*!< Pointer to user registered modem status handler. */



};

extern uart_instance_t g_uart_0;

static void default_tx_handler(uart_instance_t * this_uart);
static void enable_irq(const uart_instance_t * this_uart);

static void disable_irq(const uart_instance_t * this_uart);

static void config_baud_divisors
(
    uart_instance_t * this_uart,
    uint32_t baudrate    
);

/***************************************************************************//**
  The UART_init() function initializes and configures the
  UART with the configuration passed as a parameter. The configuration
  parameters are the baud_rate which is used to generate the baud value and the
  line_config which is used to specify the line configuration (bit length,
  stop bits and parity).
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @param baud_rate
    The baud_rate parameter specifies the baud rate. It can be specified for
    common baud rates using the following defines:
    - UART_110_BAUD
    - UART_300_BAUD
    - UART_600_BAUD
    - UART_1200_BAUD
    - UART_2400_BAUD
    - UART_4800_BAUD
    - UART_9600_BAUD
    - UART_19200_BAUD
    - UART_38400_BAUD
    - UART_57600_BAUD
    - UART_115200_BAUD
    - UART_230400_BAUD
    - UART_460800_BAUD
    - UART_921600_BAUD
    
    Alternatively, any nonstandard baud rate can be specified by simply passing
    the actual required baud rate as the value for this parameter.
  @param line_config
    The line_config parameter is the line configuration specifying the bit length,
    number of stop bits and parity settings.
    
    This is a bitwise OR of one value from each of the following groups of
    allowed values:
    
    One of the following to specify the transmit/receive data bit length:
     - UART_DATA_5_BITS
     - UART_DATA_6_BITS,
     - UART_DATA_7_BITS
     - UART_DATA_8_BITS
     
    One of the following to specify the parity setting:
     - UART_NO_PARITY
     - UART_EVEN_PARITY
     - UART_ODD_PARITY
     - UART_STICK_PARITY_0
     - UART_STICK_PARITY_1
        
    One of the following to specify the number of stop bits:
     - UART_ONE_STOP_BIT
     - UART_ONEHALF_STOP_BIT
     - UART_TWO_STOP_BITS
  @return
    This function does not return a value.
  Example:
  @code
  #include "uart.h"
  int main(void)
  {
    UART_init(&g_uart0_lo,
             UART_57600_BAUD,
             UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                   
     return(0);
  }
  @endcode
 */
void
UART_init_ariane
(
    uart_instance_t* this_uart,
    uint32_t baud_rate,
    uint8_t line_config
);

/***************************************************************************//**
  The function UART_polled_tx() is used to transmit data. It transfers the
  contents of the transmitter data buffer, passed as a function parameter, into
  the UART's hardware transmitter FIFO. It returns when the full content of the
  transmit data buffer has been transferred to the UART's transmit FIFO. It is
  safe to release or reuse the memory used as the transmitter data buffer once
  this function returns.
  Note:     This function reads the UART's line status register (LSR) to poll
  for the active state of the transmitter holding register empty (THRE) bit
  before transferring data from the data buffer to the transmitter FIFO. It
  transfers data to the transmitter FIFO in blocks of 16 bytes or less and
  allows the FIFO to empty before transferring the next block of data.
  Note:     The actual transmission over the serial connection will still be
  in progress when this function returns. Use the UART_get_tx_status()
  function if you need to know when the transmitter is empty.
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function. 
  @param pbuff
    The pbuff parameter is a pointer to a buffer containing the data to
    be transmitted.
  @param tx_size
    The tx_size parameter specifies the size, in bytes, of the data to
    be transmitted.
  @return
    This function does not return a value.
  Example:
  @code
  #include "uart.h"
  int main(void)
  {
     uint8_t message[12] = "Hello World";
     UART_init(&g_uart0_lo,
              UART_57600_BAUD,
              SS_UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
     UART_polled_tx(&g_uart0_lo, message, sizeof(message));
     
     return(0);
  }
  @endcode
 */
void
UART_polled_tx
(
    uart_instance_t * this_uart,
    const uint8_t * pbuff,
    uint32_t tx_size
);

/***************************************************************************//**
  The function UART_polled_tx_string() is used to transmit a NULL ('\0')
  terminated string. It transfers the text string, from the buffer starting at
  the address pointed to by p_sz_string into the UART's hardware transmitter
  FIFO. It returns when the complete string has been transferred to the UART's
  transmit FIFO. It is safe to release or reuse the memory used as the string
  buffer once this function returns.
  Note:     This function reads the UART's line status register (LSR) to poll
  for the active state of the transmitter holding register empty (THRE) bit
  before transferring data from the data buffer to the transmitter FIFO. It
  transfers data to the transmitter FIFO in blocks of 16 bytes or less and
  allows the FIFO to empty before transferring the next block of data.
  Note:     The actual transmission over the serial connection will still be
  in progress when this function returns. Use the UART_get_tx_status()
  function if you need to know when the transmitter is empty.
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @param p_sz_string
    The p_sz_string parameter is a pointer to a buffer containing the NULL
    ('\0') terminated string to be transmitted.
  @return
    This function does not return a value.
  Example:
  @code
  #include "uart.h"
  int main(void)
  {
     uint8_t message[12] = "Hello World";
     UART_init(&g_uart0_lo,
             UART_57600_BAUD,
             UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                   
     UART_polled_tx_string(&g_uart0_lo, message);
     
     return(0);
  }
  @endcode
 */
void
UART_polled_tx_string
(
    uart_instance_t * this_uart,
    const uint8_t * p_sz_string
);

/***************************************************************************//**
  The function UART_irq_tx() is used to initiate an interrupt-driven
  transmit. It returns immediately after making a note of the transmit buffer
  location and enabling transmit interrupts both at the UART and the PolarFire
  SoC Core Complex PLIC level. This function takes a pointer via the pbuff
  parameter to a memory buffer containing the data to transmit. The memory
  buffer specified through this pointer must remain allocated and contain the
  data to transmit until the transmit completion has been detected through calls
  to function UART_tx_complete(). The actual transmission over the serial 
  connection is still in progress until calls to the UART_tx_complete() 
  function indicate transmit completion.
  Note:     The UART_irq_tx() function enables both the transmit holding
  register empty (THRE) interrupt in the UART and the UART instance
  interrupt in the PolarFire SoC Core Complex PLIC as part of its implementation.
  Note:     The UART_irq_tx() function assigns an internal default transmit
  interrupt handler function to the UART's THRE interrupt. This interrupt
  handler overrides any custom interrupt handler that you may have previously
  registered using the UART_set_tx_handler() function.
  Note:     The UART_irq_tx() function's default transmit interrupt
  handler disables the UART's THRE interrupt when all of the data has
  been transferred to the UART's transmit FIFO.
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @param pbuff
    The pbuff parameter is a pointer to a buffer containing the data
    to be transmitted.
  @param tx_size
    The tx_size parameter specifies the size, in bytes, of the data
    to be transmitted.
  @return
    This function does not return a value.
  Example:
  @code
  #include "uart.h"
  int main(void)
  {
     uint8_t tx_buff[10] = "abcdefghi";
     
     UART_init(&g_uart0_lo,
              UART_57600_BAUD,
              UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                   
     UART_irq_tx(&g_uart0_lo, tx_buff, sizeof(tx_buff));
     
     while(0 == UART_tx_complete(&g_uart0_lo))
     {
         ;
     }
     return(0);
  }
  @endcode
 */
void
UART_irq_tx
(
    uart_instance_t * this_uart,
    const uint8_t * pbuff,
    uint32_t tx_size
);

/***************************************************************************//**
  The UART_tx_complete() function is used to find out if the
  interrupt-driven transmit previously initiated through a call to
  UART_irq_tx() is complete. This is typically used to find out when it is
  safe to reuse or release the memory buffer holding transmit data.
  Note:     The transfer of all of the data from the memory buffer to the UART's
  transmit FIFO and the actual transmission over the serial connection are both
  complete when a call to the UART_tx_complete() function indicates transmit
  completion.
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @return
    This function return a non-zero value if transmit has completed, otherwise
    it returns zero.
  Example:
    See the UART_irq_tx() function for an example that uses the
    UART_tx_complete() function.
 */
int8_t
UART_tx_complete
(
   uart_instance_t * this_uart
);

/***************************************************************************//**
  The UART_get_rx() function reads the content of the UART receiver's FIFO
  and stores it in the receive buffer that is passed via the rx_buff function
  parameter. It copies either the full contents of the FIFO into the receive
  buffer, or just enough data from the FIFO to fill the receive buffer,
  dependent upon the size of the receive buffer passed by the buff_size
  parameter. The UART_get_rx() function returns the number of bytes copied
  into the receive buffer .This function is non-blocking and will return 0
  immediately if no data has been received.
  Note:     The UART_get_rx() function reads and accumulates the receiver
  status of the UART instance before reading each byte from the receiver's
  data register/FIFO. This allows the driver to maintain a sticky record of any
  receiver errors that occur as the UART receives each data byte; receiver
  errors would otherwise be lost after each read from the receiver's data
  register. A call to the UART_get_rx_status() function returns any receiver
  errors accumulated during the execution of the UART_get_rx() function.
  Note:     If you need to read the error status for each byte received, set
  the buff_size to 1 and read the receive line error status for each byte
  using the UART_get_rx_status() function.
  The UART_get_rx() function can be used in polled mode, where it is called
  at regular intervals to find out if any data has been received, or in
  interrupt driven-mode, where it is called as part of a receive handler that is
  called by the driver as a result of data being received.
  Note:     In interrupt driven mode you should call the UART_get_rx()
  function as part of the receive handler function that you register with
  the UART driver through a call to UART_set_rx_handler().
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @param rx_buff
    The rx_buff parameter is a pointer to a buffer where the received
    data is copied.
  @param buff_size
    The buff_size parameter specifies the size of the receive buffer in bytes.
  @return
    This function returns the number of bytes that were copied into the
    rx_buff buffer. It returns 0 if no data has been received.
  Polled mode example:
  @code
   int main( void )
   {
      uint8_t rx_buff[RX_BUFF_SIZE];
      uint32_t rx_idx  = 0;
      UART_init(&g_uart0_lo,
             UART_57600_BAUD,
             UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                    
      while(1)
      {
          rx_size = UART_get_rx(&g_uart0_lo, rx_buff, sizeof(rx_buff));
          if(rx_size > 0)
          {
              process_rx_data(rx_buff, rx_size);
          }
          task_a();
          task_b();
      }
      return 0;
   }
  @endcode
  Interrupt driven example:
  @code
   int main( void )
   {
      UART_init(&g_uart1,
              UART_57600_BAUD,
              UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                    
      UART_set_rx_handler(&g_uart1,
                              uart1_rx_handler,
                              UART_FIFO_SINGLE_BYTE);
      
      while(1)
      {
          task_a();
          task_b();
      }
      return 0;
   }
   void uart1_rx_handler(uart_instance_t * this_uart)
   {
      uint8_t rx_buff[RX_BUFF_SIZE];
      uint32_t rx_idx  = 0;
      rx_size = UART_get_rx(this_uart, rx_buff, sizeof(rx_buff));
      process_rx_data(rx_buff, rx_size);
   }
  @endcode
 */
size_t
UART_get_rx
(
   uart_instance_t * this_uart,
   uint8_t * rx_buff,
   size_t buff_size
);

/***************************************************************************//**
  The UART_set_rx_handler() function is used to register a receive handler
  function that is called by the driver when a UART receive data available (RDA)
  interrupt occurs. You must create and register the receive handler function
  to suit your application and it must include a call to the UART_get_rx()
  function to actually read the received data.
  Note:     The UART_set_rx_handler() function enables both the RDA
  interrupt in the UART instance. It also enables the corresponding 
  UART instance interrupt in the PolarFire SoC Core Complex PLIC  as part
  of its implementation.
  Note:     You can disable the RDA interrupt when required by calling the 
  UART_disable_irq() function. This is your choice and is dependent upon
  your application.
  
  @param this_uart
    The this_uart parameter is a pointer to an uart_instance_t
    structure identifying the UART hardware block that will perform
    the requested function.
  @param handler
    The handler parameter is a pointer to a receive interrupt handler function
    provided by your application that will be called as a result of a UART RDA
    interrupt. This handler function must be of type uart_irq_handler_t.
  @param trigger_level
    The trigger_level parameter is the receive FIFO trigger level. This
    specifies the number of bytes that must be received before the UART
    triggers an RDA interrupt.
  @return
    This function does not return a value.
  Example:
  @code
  #include "uart.h"
  #define RX_BUFF_SIZE    64
  uint8_t g_rx_buff[RX_BUFF_SIZE];
  void uart0_rx_handler(uart_instance_t * this_uart)
  {
      UART_get_rx(this_uart, &g_rx_buff[g_rx_idx], sizeof(g_rx_buff));
  }
  int main(void)
  {
      UART_init(&g_uart0_lo,
             UART_57600_BAUD,
             UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
                    
      UART_set_rx_handler(&g_uart0_lo,
                              uart0_rx_handler,
                              UART_FIFO_SINGLE_BYTE);
                              
      while(1)
      {
         ;
      }
      return(0);
   }
  @endcode
 */
void
UART_set_rx_handler
(
    uart_instance_t *       this_uart,
    uart_irq_handler_t          handler,
    uart_rx_trig_level_t    trigger_level
);


#endif /*__UART_H_*/