rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2012 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 <lib/cbuf.h> |
| 28 | #include <kernel/thread.h> |
| 29 | #include <platform/debug.h> |
| 30 | #include <arch/ops.h> |
| 31 | #include <target/debugconfig.h> |
| 32 | #include <arch/arm/cm.h> |
| 33 | |
| 34 | #include <uart/uart.h> |
| 35 | #include <pmc/pmc.h> |
| 36 | #include <pio/pio.h> |
| 37 | |
| 38 | static cbuf_t debug_rx_buf; |
| 39 | |
| 40 | void sam3_uart_irq(void) |
| 41 | { |
| 42 | arm_cm_irq_entry(); |
| 43 | |
| 44 | bool resched = false; |
| 45 | unsigned char c; |
| 46 | if (uart_read(UART, &c) == 0) { |
| 47 | cbuf_write_char(&debug_rx_buf, c, false); |
| 48 | resched = true; |
| 49 | } |
| 50 | |
| 51 | arm_cm_irq_exit(resched); |
| 52 | } |
| 53 | |
| 54 | void sam_debug_early_init(void) |
| 55 | { |
| 56 | pmc_enable_periph_clk(ID_UART); |
| 57 | pmc_enable_periph_clk(ID_PIOA); |
| 58 | |
| 59 | pio_set_peripheral(PIOA, PIO_PERIPH_A, PIO_PA8); |
| 60 | pio_set_peripheral(PIOA, PIO_PERIPH_A, PIO_PA9); |
| 61 | |
| 62 | sam_uart_opt_t opt; |
| 63 | |
| 64 | opt.ul_mck = 84000000; |
| 65 | opt.ul_baudrate = 115200; |
| 66 | opt.ul_mode = UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL; |
| 67 | |
| 68 | NVIC_DisableIRQ(UART_IRQn); |
| 69 | |
| 70 | uart_init(UART, &opt); |
| 71 | |
| 72 | uart_enable(UART); |
| 73 | } |
| 74 | |
| 75 | void sam_debug_init(void) |
| 76 | { |
| 77 | cbuf_initialize(&debug_rx_buf, 16); |
| 78 | NVIC_EnableIRQ(UART_IRQn); |
| 79 | uart_enable_interrupt(UART, UART_IER_RXRDY); |
| 80 | } |
| 81 | |
| 82 | void platform_dputc(char c) |
| 83 | { |
| 84 | if (c == '\n') { |
| 85 | platform_dputc('\r'); |
| 86 | } |
| 87 | |
| 88 | while (!uart_is_tx_ready(UART)) |
| 89 | ; |
| 90 | uart_write(UART, c); |
| 91 | } |
| 92 | |
| 93 | int platform_dgetc(char *c, bool wait) |
| 94 | { |
| 95 | return cbuf_read_char(&debug_rx_buf, c, wait); |
| 96 | } |
| 97 | |