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