summaryrefslogtreecommitdiff
path: root/ports/risc-v32/gnu/example_build/cva6_ariane/uart.c
blob: c92c67e61ae5b67f086f8531de30fc9f90692b4d (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
/*******************************************************************************
 * Copyright (c) 2020 Thales.
 * Copyright 2019-2020 Microchip FPGA Embedded Systems Solutions.
 *
 * SPDX-License-Identifier: MIT
 *
 */
// Additional contributions by:
//         Sebastien Jacq - sjthales on github.com
//         Anjali Gedam - anjaliigedam on github.com
//
// Description: Driver 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
// =========================================================================== //


#include "uart.h"

//#include "../plic/plic.h"
//#include "../hw_platform.h"
#define FPGA_UART_0_FREQUENCY 50000000
#define FPGA_UART_0_BASE (UART_TypeDef *)0x10000000
#define STDIO_BAUD_RATE  UART_115200_BAUD

/*******************************************************************************
 * Defines
 */
#define TX_COMPLETE                     0u
#define TX_FIFO_SIZE                    16u

#define FCR_TRIG_LEVEL_MASK             0xC0u

#define IIRF_MASK                       0x0Fu

#define INVALID_INTERRUPT               0u
#define INVALID_IRQ_HANDLER             ((uart_irq_handler_t) 0)
#define NULL_HANDLER                    ((uart_irq_handler_t) 0)

#define UART_DATA_READY             ((uint8_t) 0x01)


/*******************************************************************************
 * Possible values for Interrupt Identification Register Field.
 */
#define IIRF_MODEM_STATUS               0x00u
#define IIRF_THRE                       0x02u
//#define IIRF_MMI                        0x03u
#define IIRF_RX_DATA                    0x04u
#define IIRF_RX_LINE_STATUS             0x06u
#define IIRF_DATA_TIMEOUT               0x0Cu

 
//#define PLIC_DEMO 1
// Source/portable/GCC/RISC-V/portASM.S:290:freertos_risc_v_application_interrupt_handler (weak function : User application can add there own override)

uart_instance_t g_uart_0 = { .hw_reg = FPGA_UART_0_BASE };

/*******************************************************************************
 * Global initialization for all modes
 */
static void global_init
(
    uart_instance_t * this_uart,
    uint32_t baud_rate,
    uint8_t line_config
)
{
    
    /* disable interrupts */
    this_uart->hw_reg->IER = 0u;

    /* FIFO configuration */
    this_uart->hw_reg->FCR = 0u;

    /* clear receiver FIFO */
    this_uart->hw_reg->FCR = FIFO_RX_TRIGGER_LEVEL_14_MASK | CLEAR_RX_FIFO_MASK | CLEAR_TX_FIFO_MASK | RXRDY_TXRDYN_EN_MASK;

    /* clear transmitter FIFO */
    this_uart->hw_reg->FCR |= CLEAR_TX_FIFO_MASK;

    /* set default READY mode : Mode 0*/
    /* enable RXRDYN and TXRDYN pins. The earlier FCR write to set the TX FIFO
     * trigger level inadvertently disabled the FCR_RXRDY_TXRDYN_EN bit. */
    this_uart->hw_reg->FCR |= RXRDY_TXRDYN_EN_MASK;

    this_uart->hw_reg->MCR = 0u;


    
    /* 
     * Configure baud rate divisors. This uses the fractional baud rate divisor
     * where possible to provide the most accurate baud rat possible.
     */
    config_baud_divisors(this_uart, baud_rate);

    /* set the line control register (bit length, stop bits, parity) */
    this_uart->hw_reg->LCR = line_config;

    /* Instance setup */
    this_uart->baudrate = baud_rate;
    this_uart->lineconfig = line_config;
    this_uart->tx_buff_size = TX_COMPLETE;
    this_uart->tx_buffer = (const uint8_t*)0;
    this_uart->tx_idx = 0u;

    /* Default handlers for MSS UART interrupts */
    this_uart->rx_handler       = NULL_HANDLER;
    this_uart->tx_handler       = NULL_HANDLER;
    this_uart->linests_handler  = NULL_HANDLER;
    this_uart->modemsts_handler = NULL_HANDLER;

    /* Initialize the sticky status */
    this_uart->status = 0u;
}

/*******************************************************************************
 * Public Functions
 *******************************************************************************/
/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void 
UART_init
(
    uart_instance_t* this_uart, 
    uint32_t baud_rate,
    uint8_t line_config
)
{
    /* Perform generic initialization */
    global_init(this_uart, baud_rate, line_config);


    /* set default tx handler for automated TX using interrupt in USART mode */
    this_uart->tx_handler = default_tx_handler;
}



/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_polled_tx
(
    uart_instance_t * this_uart,
    const uint8_t * pbuff,
    uint32_t tx_size
)
{
    uint32_t char_idx = 0u;
   // uint32_t size_sent;
    uint8_t status;
    //uint32_t temp_tx_size = tx_size;

    //ASSERT(pbuff != ( (uint8_t*)0));
    //ASSERT(tx_size > 0u);

    if ((pbuff != ((uint8_t*)0)) && (tx_size > 0u))
    {
        /* Remain in this loop until the entire input buffer
         * has been transferred to the UART.
         */
        do
        {
            /* Wait until TX FIFO is empty. */
            do
            {
                status = this_uart->hw_reg->LSR;
               // this_uart->status |= status;
            }while (0u == (status & UART_THRE));


            /* Check if TX FIFO is empty. */
           // if (status & UART_THRE)
            //{
               // uint32_t fill_size = TX_FIFO_SIZE;

                /* Calculate the number of bytes to transmit. */
                //if (temp_tx_size < TX_FIFO_SIZE)
                //{
                //    fill_size = temp_tx_size;
                //}

                /* Fill the TX FIFO with the calculated the number of bytes. */
                //for (size_sent = 0u; size_sent < fill_size; ++size_sent)
                //{
                    /* Send next character in the buffer. */
                    this_uart->hw_reg->THR = pbuff[char_idx];
                    char_idx++;
                //}

                /* Calculate the number of bytes remaining(not transmitted yet)*/
                //temp_tx_size -= size_sent;
            //}
        }while (char_idx < tx_size);
    }
}


/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_polled_tx_string
(
    uart_instance_t * this_uart,
    const uint8_t * p_sz_string
)
{
    uint32_t char_idx = 0u;
    //uint32_t fill_size;
    uint8_t data_byte;
    volatile uint8_t status;

    //ASSERT(p_sz_string != ((uint8_t*)0));

    if (p_sz_string != ((uint8_t*)0))
    {
        /* Get the first data byte from the input buffer */
        data_byte = p_sz_string[char_idx];

        /* First check for the NULL terminator byte.
         * Then remain in this loop until the entire string in the input buffer
         * has been transferred to the UART.
         */
        while (0u != data_byte)
        {
            /* Wait until TX FIFO is empty. */
            do
            {
                status = this_uart->hw_reg->LSR;
               // this_uart->status |= status;
            }while (0u == (status & UART_THRE));

		
            /* Send bytes from the input buffer until the TX FIFO is full
             * or we reach the NULL terminator byte.
             */
            //fill_size = 0u;

           // while ((0u != data_byte) && (fill_size < TX_FIFO_SIZE))
            //{
                /* Send the data byte */
                this_uart->hw_reg->THR = data_byte;
                //++fill_size;
                char_idx++;
                /* Get the next data byte from the input buffer */
                data_byte = p_sz_string[char_idx];
            //}
        }
    }
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_irq_tx
(
    uart_instance_t * this_uart,
    const uint8_t * pbuff,
    uint32_t tx_size
)
{
    //ASSERT(pbuff != ((uint8_t*)0));
    //ASSERT(tx_size > 0u);

    if ((tx_size > 0u) && (pbuff != ((uint8_t*)0)))
    {
        /*Initialize the transmit info for the UART instance with the arguments*/
        this_uart->tx_buffer = pbuff;
        this_uart->tx_buff_size = tx_size;
        this_uart->tx_idx = 0u;

        /* assign default handler for data transfer */
        this_uart->tx_handler = default_tx_handler;

        /* enables TX interrupt */
        this_uart->hw_reg->IER |= ETBEI_MASK;
        enable_irq(this_uart);
    }
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
int8_t
UART_tx_complete
(
    uart_instance_t * this_uart
)
{
    int8_t ret_value = 0;
    uint8_t status = 0u;

    /* Read the Line Status Register and update the sticky record. */
    status = this_uart->hw_reg->LSR;
    this_uart->status |= status;

    if ((TX_COMPLETE == this_uart->tx_buff_size) &&
       ((status & UART_TEMT) != 0u))
    {
        ret_value = (int8_t)1;
    }

    return ret_value;
}


/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
size_t
UART_get_rx
(
    uart_instance_t * this_uart,
    uint8_t * rx_buff,
    size_t buff_size
)
{
    size_t rx_size = 0u;
    uint8_t status = 0u;

    //ASSERT(rx_buff != ((uint8_t*)0));
    //ASSERT(buff_size > 0u);
    //printf("UART_get_rx() called buff_size = %d\n", buff_size);

    if ((rx_buff != (uint8_t*)0) && (buff_size > 0u))
    {
        status = this_uart->hw_reg->LSR;
        this_uart->status |= status;
        //printf("UART_get_rx() LSR status = %d\n", status);
        
        while (((status & UART_DATA_READY) != 0u) && (rx_size < buff_size))
        {
            rx_buff[rx_size] = this_uart->hw_reg->RBR;
            ++rx_size;
            status = this_uart->hw_reg->LSR;
            //printf("1 UART_get_rx() LSR status = %d\n", status);
            this_uart->status |= status;
            //printf("2 UART_get_rx() LSR status = %d\n", status);
        
        }
    }

    return rx_size;
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_enable_irq
(
    uart_instance_t * this_uart,
    uart_irq_t irq_mask
)
{
    //ASSERT(UART_INVALID_IRQ > irq_mask);

    enable_irq(this_uart);

    if (UART_INVALID_IRQ > irq_mask)
    {
        /* irq_mask encoding: 1- enable
         * bit 0 - Receive Data Available Interrupt
         * bit 1 - Transmitter Holding  Register Empty Interrupt
         * bit 2 - Receiver Line Status Interrupt
         * bit 3 - Modem Status Interrupt
         */
        this_uart->hw_reg->IER |= ((uint8_t)(((uint32_t)irq_mask &
                                                            (uint32_t)IIRF_MASK)));

    }
}


/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_set_rx_handler
(
    uart_instance_t *       this_uart,
    uart_irq_handler_t      handler,
    uart_rx_trig_level_t    trigger_level
)
{
    //ASSERT(handler != INVALID_IRQ_HANDLER );
    //ASSERT(trigger_level < UART_FIFO_INVALID_TRIG_LEVEL);
#ifdef PLIC_DEMO 
    PLIC_init();
#endif
    
    if ((handler != INVALID_IRQ_HANDLER) &&
       (trigger_level < UART_FIFO_INVALID_TRIG_LEVEL))
    {
        //printf("UART DEBUG1\n");
        this_uart->rx_handler = handler;

        /* Set the receive interrupt trigger level. */
        this_uart->hw_reg->FCR = (this_uart->hw_reg->FCR &
                                 (uint8_t)(~((uint8_t)FCR_TRIG_LEVEL_MASK))) |
                                 (uint8_t)trigger_level;

        /* Enable receive interrupt. */
        this_uart->hw_reg->IER |= ERBFI_MASK;
        //printf("UART Enable IRQ\n");
        enable_irq(this_uart);
    }
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_set_tx_handler
(
    uart_instance_t * this_uart,
    uart_irq_handler_t handler
)
{
    //ASSERT(handler != INVALID_IRQ_HANDLER);

    if (handler != INVALID_IRQ_HANDLER)
    {
        this_uart->tx_handler = handler;

        /* Make TX buffer info invalid */
        this_uart->tx_buffer = (const uint8_t*)0;
        this_uart->tx_buff_size = 0u;

        /* Enable transmitter holding register Empty interrupt. */
        this_uart->hw_reg->IER |= ETBEI_MASK;
        enable_irq(this_uart);
    }
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_set_modemstatus_handler
(
    uart_instance_t * this_uart,
    uart_irq_handler_t handler
)
{
    //ASSERT(handler != INVALID_IRQ_HANDLER);

    if (handler != INVALID_IRQ_HANDLER)
    {
        this_uart->modemsts_handler = handler;

        /* Enable modem status interrupt. */
        this_uart->hw_reg->IER |= EDSSI_MASK;
        enable_irq(this_uart);
    }
}


/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
size_t
UART_fill_tx_fifo
(
    uart_instance_t * this_uart,
    const uint8_t * tx_buffer,
    size_t tx_size
)
{
    uint8_t status = 0u;
    uint32_t size_sent = 0u;

    //ASSERT(tx_buffer != ( (uint8_t*)0));
    //ASSERT(tx_size > 0);

    /* Fill the UART's Tx FIFO until the FIFO is full or the complete input
     * buffer has been written. */
    if ((tx_buffer != ((uint8_t*)0)) && (tx_size > 0u))
    {
        status = this_uart->hw_reg->LSR;
        this_uart->status |= status;

        if (status & UART_THRE)
        {
            uint32_t fill_size = TX_FIFO_SIZE;

            if (tx_size < TX_FIFO_SIZE)
            {
                fill_size = tx_size;
            }

            /* Fill up FIFO */
            for (size_sent = 0u; size_sent < fill_size; size_sent++)
            {
                /* Send next character in the buffer. */
                this_uart->hw_reg->THR = tx_buffer[size_sent];
            }
        }
    }

    return size_sent;
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
uint8_t
UART_get_rx_status
(
    uart_instance_t * this_uart
)
{
    uint8_t status = UART_INVALID_PARAM;

    /*
     * Extract UART receive error status.
     * Bit 1 - Overflow error status
     * Bit 2 - Parity error status
     * Bit 3 - Frame error status
     * Bit 4 - Break interrupt indicator
     * Bit 7 - FIFO data error status
     */
    this_uart->status |= (this_uart->hw_reg->LSR);
    status = (this_uart->status & STATUS_ERROR_MASK);
    
    //printf("UART_get_rx_status = %d", status);
    //printf("UART_get_rx_LSR = %d", this_uart->hw_reg->LSR);


    /* Clear the sticky status after reading */
    this_uart->status = 0u;

    return status;
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
uint8_t
UART_get_modem_status
(
    const uart_instance_t * this_uart
)
{
    uint8_t status = UART_INVALID_PARAM;

    /*
     * Extract UART modem status and place in lower bits of "status".
     * Bit 0 - Delta Clear to Send Indicator
     * Bit 1 - Delta Clear to Receive Indicator
     * Bit 2 - Trailing edge of Ring Indicator detector
     * Bit 3 - Delta Data Carrier Detect indicator
     * Bit 4 - Clear To Send
     * Bit 5 - Data Set Ready
     * Bit 6 - Ring Indicator
     * Bit 7 - Data Carrier Detect
     */
    status = this_uart->hw_reg->MSR;
    //printf("UART_get_modem_status = %d", status);
    
    return status;
}


/***************************************************************************//**
 * UART_get_tx_status.
 * See uart.h for details of how to use this function.
 */
uint8_t
UART_get_tx_status
(
    uart_instance_t * this_uart
)
{
    uint8_t status = UART_TX_BUSY;

    /* Read the Line Status Register and update the sticky record. */
    status = this_uart->hw_reg->LSR;
    this_uart->status |= status;
    
    /*
     * Extract the transmit status bits from the UART's Line Status Register.
     * Bit 5 - Transmitter Holding Register/FIFO Empty (THRE) status. 
               (If = 1, TX FIFO is empty)
     * Bit 6 - Transmitter Empty (TEMT) status. 
               (If = 1, both TX FIFO and shift register are empty)
     */
    status &= (UART_THRE | UART_TEMT);

    return status;
}

/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_set_break
(
    uart_instance_t * this_uart
)
{
    /* set break character on Tx line */
    this_uart->hw_reg->LCR |= SB_MASK;
}


/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
void
UART_clear_break
(
    uart_instance_t * this_uart
)
{
    /* remove break character from Tx line */
    this_uart->hw_reg->LCR &= ~SB_MASK;
}

/***************************************************************************//**
 * Configure baud divisors using fractional baud rate if possible.
 */
static void
config_baud_divisors
(
    uart_instance_t * this_uart,
    uint32_t baudrate
)
{
    uint32_t baud_value;
    uint32_t baud_value_by_64;
    uint32_t baud_value_by_128;
//    uint32_t fractional_baud_value;
    /* Note that we may need to set this as 64 bits for high frequencies */
    uint32_t pclk_freq;

    this_uart->baudrate = baudrate;

    /* Use the system clock value from hw_platform.h */
    pclk_freq = FPGA_UART_0_FREQUENCY;

    /*
     * Compute baud value based on requested baud rate and PCLK frequency.
     * The baud value is computed using the following equation:
     *      baud_value = PCLK_Frequency / (baud_rate * 16)
     */
    baud_value_by_128 = (uint32_t)((8UL * pclk_freq) / baudrate);
    baud_value_by_64 = baud_value_by_128 / 2u;
    baud_value = baud_value_by_64 / 64u;
//    fractional_baud_value = baud_value_by_64 - (baud_value * 64u);
//    fractional_baud_value += (baud_value_by_128 - (baud_value * 128u))
 //                            - (fractional_baud_value * 2u);

    /* //ASSERT if integer baud value fits in 16-bit. */
    //ASSERT(baud_value <= UINT16_MAX);

    if (baud_value <= (uint32_t)UINT16_MAX)
    {
        
        /*
        * Use Fractional baud rate divisors
        */
        /* set divisor latch */
        this_uart->hw_reg->LCR = DLAB_MASK;

        /* msb of baud value */
        this_uart->hw_reg->DLM = (uint8_t)(baud_value >> 8);
        /* lsb of baud value */
        this_uart->hw_reg->DLL = (uint8_t)baud_value;

        /* reset divisor latch */
        this_uart->hw_reg->LCR = 0;
	    }
}

/***************************************************************************//**
 * Interrupt service routine triggered by any MSS UART interrupt. This routine
 * will call the handler function appropriate to the interrupt from the
 * handlers previously registered with the driver through calls to the
 * UART_set_*_handler() functions, or it will call the default_tx_handler()
 * function in response to transmit interrupts if UART_irq_tx() is used to
 * transmit data.
 */
static void __attribute__ ((unused))
uart_isr
(
    uart_instance_t * this_uart
)
{
    //printf("uart_isr called\n");
    uint8_t iirf;

    iirf = this_uart->hw_reg->IIR & IIRF_MASK;

    switch (iirf)
    {
        case IIRF_MODEM_STATUS:  /* Modem status interrupt */
        {
            //ASSERT(NULL_HANDLER != this_uart->modemsts_handler);
            if (NULL_HANDLER != this_uart->modemsts_handler)
            {
               (*(this_uart->modemsts_handler))(this_uart);
            }
        }
        break;

        case IIRF_THRE: /* Transmitter Holding Register Empty */
        {
            //ASSERT(NULL_HANDLER != this_uart->tx_handler);
            if (NULL_HANDLER != this_uart->tx_handler)
            {
                (*(this_uart->tx_handler))(this_uart);
            }
        }
        break;

        case IIRF_RX_DATA:      /* Received Data Available */
        case IIRF_DATA_TIMEOUT: /* Received Data Timed-out */
        {
            //ASSERT(NULL_HANDLER != this_uart->rx_handler);
            if (NULL_HANDLER != this_uart->rx_handler)
            {
                (*(this_uart->rx_handler))(this_uart);
            }
        }
        break;

        case IIRF_RX_LINE_STATUS:  /* Line Status Interrupt */
        {
            //ASSERT(NULL_HANDLER != this_uart->linests_handler);
            if (NULL_HANDLER != this_uart->linests_handler)
            {
               (*(this_uart->linests_handler))(this_uart);
            }
        }
        break;
        
        default:
        {
            //ASSERT(INVALID_INTERRUPT); /*Alternative case has been considered*/
        }
        break;
    }
}



/***************************************************************************//**
 * See uart.h for details of how to use this function.
 */
static void
default_tx_handler
(
    uart_instance_t * this_uart
)
{
    uint8_t status;

    //ASSERT(( (uint8_t*)0 ) != this_uart->tx_buffer);
    //ASSERT(0u < this_uart->tx_buff_size);

    if ((((uint8_t*)0 ) != this_uart->tx_buffer) &&
       (0u < this_uart->tx_buff_size))
    {
        /* Read the Line Status Register and update the sticky record. */
        status = this_uart->hw_reg->LSR;
        this_uart->status |= status;

        /*
         * This function should only be called as a result of a THRE interrupt.
         * Verify that this is true before proceeding to transmit data.
         */
        if (status & UART_THRE)
        {
            uint32_t cnt;
            uint32_t fill_size = TX_FIFO_SIZE;
            uint32_t tx_remain = this_uart->tx_buff_size - this_uart->tx_idx;

            /* Calculate the number of bytes to transmit. */
            if (tx_remain < TX_FIFO_SIZE)
            {
                fill_size = tx_remain;
            }

            /* Fill the TX FIFO with the calculated the number of bytes. */
            for (cnt = 0u; cnt < fill_size; ++cnt)
            {
                /* Send next character in the buffer. */
                this_uart->hw_reg->THR = this_uart->tx_buffer[this_uart->tx_idx];
                ++this_uart->tx_idx;
            }
        }

        /* Flag Tx as complete if all data has been pushed into the Tx FIFO. */
        if (this_uart->tx_idx == this_uart->tx_buff_size)
        {
            this_uart->tx_buff_size = TX_COMPLETE;

            /* disables TX interrupt */
            this_uart->hw_reg->IER &= ~ETBEI_MASK;
        }
    }
}


static void __attribute__ ((unused))
enable_irq
(
    const uart_instance_t * this_uart
)
{

#ifdef PLIC_DEMO
   PLIC_IRQn_Type plic_num = 0;
#endif

    if (&g_uart_0 == this_uart )
    {
        //printf("enable_irq() UART assign plic_num = %d\n",plic_num);
#ifdef PLIC_DEMO
        plic_num = UART_0_PLIC_IRQHandler;
#endif

    }
    else
    {
        //printf("ASSERT(0) called\n");
        //ASSERT(0); /*Alternative case has been considered*/
    }

#ifdef PLIC_DEMO
    /* Enable UART instance interrupt in PLIC. */
    PLIC_EnableIRQ(plic_num);
#endif
}

static void __attribute__ ((unused))
disable_irq
(
    const uart_instance_t * this_uart
)
{
#ifdef PLIC_DEMO
    PLIC_IRQn_Type plic_num = 0;
#endif

    if (&g_uart_0 == this_uart )
    {
#ifdef PLIC_DEMO
        plic_num = UART_0_PLIC_IRQHandler;
#endif
    }
    else
    {
        //ASSERT(0); /*Alternative case has been considered*/
    }

    /* Disable UART instance interrupt in PLIC. */
#ifdef PLIC_DEMO
    PLIC_DisableIRQ(plic_num);
#endif
}


///////////////////////////////////////////////////////////////////////////////////

static uart_instance_t * const gp_my_uart = &g_uart_0;

int uart_init(void)
{
    UART_init(gp_my_uart, STDIO_BAUD_RATE, ( UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT ) );

    return 0;
}

int uart_putc(int ch)
{
    UART_polled_tx(gp_my_uart, (const uint8_t *)&ch, 1);
}

int uart_puts(const char* str)
{
    UART_polled_tx_string(gp_my_uart, str);
}