1 /*
2  * Copyright (c) 2012 Ian McKellar
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <stdarg.h>
9 #include <lk/reg.h>
10 #include <lk/debug.h>
11 #include <stdio.h>
12 #include <lib/cbuf.h>
13 #include <kernel/thread.h>
14 #include <platform/debug.h>
15 #include <arch/ops.h>
16 #include <target/debugconfig.h>
17 #include <arch/arm/cm.h>
18 
19 #include "ti_driverlib.h"
20 
21 #include "inc/hw_memmap.h"
22 #include "inc/hw_types.h"
23 
24 #define DEBUG_UART UART0_BASE
25 
26 static cbuf_t debug_rx_buf;
27 
stellaris_uart0_irq(void)28 void stellaris_uart0_irq(void) {
29     arm_cm_irq_entry();
30 
31     //
32     // Get the interrrupt status.
33     //
34     unsigned long ulStatus = UARTIntStatus(DEBUG_UART, true);
35 
36     //
37     // Clear the asserted interrupts.
38     //
39     UARTIntClear(DEBUG_UART, ulStatus);
40 
41     //
42     // Loop while there are characters in the receive FIFO.
43     //
44     bool resched = false;
45     while (UARTCharsAvail(DEBUG_UART)) {
46         //
47         // Read the next character from the UART and write it back to the UART.
48         //
49         unsigned char c = UARTCharGetNonBlocking(DEBUG_UART);
50         cbuf_write_char(&debug_rx_buf, c, false);
51 
52         resched = true;
53     }
54 
55     arm_cm_irq_exit(resched);
56 }
57 
stellaris_debug_early_init(void)58 void stellaris_debug_early_init(void) {
59     SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
60 
61     /* we only support UART0 right now */
62     STATIC_ASSERT(DEBUG_UART == UART0_BASE);
63 
64     if (DEBUG_UART == UART0_BASE) {
65 #if defined(PART_LM4F120H5QR)
66         /* Set GPIO A0 and A1 as UART pins. */
67         GPIOPinConfigure(GPIO_PA0_U0RX);
68         GPIOPinConfigure(GPIO_PA1_U0TX);
69         GPIOPinTypeUART(GPIO_PORTA_AHB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
70 #endif
71     }
72 
73     UARTConfigSetExpClk(DEBUG_UART, SysCtlClockGet(), 115200, UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE);
74 
75     UARTEnable(DEBUG_UART);
76 }
77 
stellaris_debug_init(void)78 void stellaris_debug_init(void) {
79     cbuf_initialize(&debug_rx_buf, 16);
80 
81     /* Enable the UART interrupt. */
82     UARTIntEnable(DEBUG_UART, UART_INT_RX | UART_INT_RT);
83 
84     NVIC_EnableIRQ(INT_UART0 - 16);
85 
86 }
87 
platform_dputc(char c)88 void platform_dputc(char c) {
89     if (c == '\n') {
90         platform_dputc('\r');
91     }
92 
93     UARTCharPut(DEBUG_UART, c);
94 }
95 
platform_dgetc(char * c,bool wait)96 int platform_dgetc(char *c, bool wait) {
97     return cbuf_read_char(&debug_rx_buf, c, wait);
98 }
99 
100