rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <stdarg.h> |
| 24 | #include <reg.h> |
| 25 | #include <debug.h> |
| 26 | #include <stdio.h> |
| 27 | #include <compiler.h> |
| 28 | #include <lib/cbuf.h> |
| 29 | #include <kernel/thread.h> |
| 30 | #include <platform/debug.h> |
| 31 | #include <arch/ops.h> |
| 32 | #include <arch/arm/cm.h> |
| 33 | #include <target/debugconfig.h> |
| 34 | |
| 35 | #include <platform/lpc.h> |
| 36 | |
| 37 | static cbuf_t debug_rx_buf; |
| 38 | |
| 39 | /* this code is only set up to handle UART0 as the debug uart */ |
| 40 | STATIC_ASSERT(DEBUG_UART == LPC_USART0); |
| 41 | |
| 42 | void lpc_debug_early_init(void) |
| 43 | { |
| 44 | /* Use main clock rate as base for UART baud rate divider */ |
| 45 | Chip_Clock_SetUARTBaseClockRate(Chip_Clock_GetMainClockRate(), false); |
| 46 | |
| 47 | /* Setup UART */ |
| 48 | Chip_UART_Init(DEBUG_UART); |
| 49 | Chip_UART_ConfigData(DEBUG_UART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1); |
| 50 | Chip_UART_SetBaud(DEBUG_UART, 115200); |
| 51 | Chip_UART_Enable(DEBUG_UART); |
| 52 | Chip_UART_TXEnable(DEBUG_UART); |
| 53 | } |
| 54 | |
| 55 | void lpc_debug_init(void) |
| 56 | { |
| 57 | cbuf_initialize(&debug_rx_buf, 16); |
| 58 | |
| 59 | /* enable uart interrupts */ |
| 60 | Chip_UART_IntEnable(DEBUG_UART, UART_INTEN_RXRDY); |
| 61 | |
| 62 | NVIC_EnableIRQ(UART0_IRQn); |
| 63 | } |
| 64 | |
| 65 | void lpc_UART0_irq(void) |
| 66 | { |
| 67 | arm_cm_irq_entry(); |
| 68 | |
| 69 | /* read the rx buffer until it's empty */ |
| 70 | while ((Chip_UART_GetStatus(DEBUG_UART) & UART_STAT_RXRDY) != 0) { |
| 71 | uint8_t c = Chip_UART_ReadByte(DEBUG_UART); |
| 72 | cbuf_write_char(&debug_rx_buf, c, false); |
| 73 | } |
| 74 | |
| 75 | arm_cm_irq_exit(true); |
| 76 | } |
| 77 | |
| 78 | void platform_dputc(char c) |
| 79 | { |
| 80 | if (c == '\n') { |
| 81 | platform_dputc('\r'); |
| 82 | } |
| 83 | |
| 84 | Chip_UART_SendBlocking(DEBUG_UART, &c, 1); |
| 85 | } |
| 86 | |
| 87 | int platform_dgetc(char *c, bool wait) |
| 88 | { |
| 89 | #if 1 |
| 90 | return cbuf_read_char(&debug_rx_buf, c, wait); |
| 91 | #else |
| 92 | uint8_t data; |
| 93 | |
| 94 | if (Chip_UART_Read(DEBUG_UART, &data, 1) == 1) { |
| 95 | *c = data; |
| 96 | return 1; |
| 97 | } |
| 98 | return -1; |
| 99 | #endif |
| 100 | } |
| 101 | |