rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Eric Holland |
| 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 <assert.h> |
| 28 | #include <err.h> |
| 29 | #include <lib/cbuf.h> |
| 30 | #include <arch/arm/cm.h> |
| 31 | #include <arch/ops.h> |
| 32 | #include <dev/uart.h> |
| 33 | #include <dev/gpio.h> |
| 34 | #include <kernel/thread.h> |
| 35 | #include <platform/debug.h> |
| 36 | #include <platform/gpio.h> |
| 37 | #include <target/debugconfig.h> |
| 38 | #include <target/gpioconfig.h> |
| 39 | |
| 40 | #define RXBUF_SIZE 16 |
| 41 | |
| 42 | //cbuf_t uart0_rx_buf; |
| 43 | |
| 44 | |
| 45 | |
| 46 | void uart_init_early(void) |
| 47 | { |
| 48 | #ifdef ENABLE_UART0 |
| 49 | |
| 50 | #ifdef UART0_TX_PIN |
| 51 | gpio_config(UART0_TX_PIN,GPIO_OUTPUT); |
| 52 | NRF_UART0->PSELTXD = UART0_TX_PIN; |
| 53 | #endif |
| 54 | #ifdef UART0_RX_PIN |
| 55 | gpio_config(UART0_RX_PIN,GPIO_INPUT); |
| 56 | NRF_UART0->PSELRXD = UART0_RX_PIN; |
| 57 | #endif |
| 58 | |
| 59 | NRF_UART0->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud115200 << UART_BAUDRATE_BAUDRATE_Pos; |
| 60 | NRF_UART0->CONFIG = UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos | \ |
| 61 | UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos; |
| 62 | NVIC_DisableIRQ(UART0_IRQn); |
| 63 | NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos; |
| 64 | NRF_UART0->TXD = 'E'; |
| 65 | NRF_UART0->TASKS_STARTTX=1; |
| 66 | NRF_UART0->TASKS_STARTRX=1; |
| 67 | #endif //ENABLE_UART0 |
| 68 | } |
| 69 | |
| 70 | void uart_init(void) |
| 71 | { |
| 72 | #ifdef ENABLE_UART0 |
| 73 | // cbuf_initialize(&uart0_rx_buf, RXBUF_SIZE); |
| 74 | // NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos; |
| 75 | NRF_UART0->EVENTS_RXDRDY = 0; |
| 76 | // NVIC_EnableIRQ(UART0_IRQn); |
| 77 | char c = NRF_UART0->RXD; |
| 78 | (void)c; |
| 79 | #endif //ENABLE_UART0 |
| 80 | } |
| 81 | |
| 82 | void nrf51_UART0_IRQ(void) |
| 83 | { |
| 84 | // char c; |
| 85 | arm_cm_irq_entry(); |
| 86 | /* |
| 87 | bool resched = false; |
| 88 | while ( NRF_UART0->EVENTS_RXDRDY > 0 ) { |
| 89 | NRF_UART0->EVENTS_RXDRDY = 0; |
| 90 | c = NRF_UART0->RXD; |
| 91 | if (!cbuf_space_avail(&uart0_rx_buf)) { |
| 92 | break; |
| 93 | } |
| 94 | cbuf_write_char(&uart0_rx_buf, c, false); |
| 95 | resched = true; |
| 96 | } |
| 97 | */ |
| 98 | arm_cm_irq_exit(false); |
| 99 | } |
| 100 | |
| 101 | int uart_putc(int port, char c) |
| 102 | { |
| 103 | while (NRF_UART0->EVENTS_TXDRDY == 0); |
| 104 | NRF_UART0->TXD = c; |
| 105 | NRF_UART0->EVENTS_TXDRDY = 0; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | int uart_getc(int port, bool wait) |
| 110 | { |
| 111 | do { |
| 112 | if (NRF_UART0->EVENTS_RXDRDY > 0) { |
| 113 | NRF_UART0->EVENTS_RXDRDY=0; |
| 114 | return NRF_UART0->RXD; |
| 115 | } |
| 116 | } while (wait); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | void uart_flush_tx(int port) {} |
| 121 | |
| 122 | void uart_flush_rx(int port) {} |
| 123 | |
| 124 | void uart_init_port(int port, uint baud) |
| 125 | { |
| 126 | // TODO - later |
| 127 | PANIC_UNIMPLEMENTED; |
| 128 | } |