aboutsummaryrefslogtreecommitdiff
path: root/platform/nrf52xxx/uart.c
blob: 8e1f1d9ded4a5ca616b374b9f3cab9b104304db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2015 Eric Holland
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <stdarg.h>
#include <reg.h>
#include <debug.h>
#include <stdio.h>
#include <assert.h>
#include <err.h>
#include <lib/cbuf.h>
#include <arch/arm/cm.h>
#include <arch/ops.h>
#include <dev/uart.h>
#include <dev/gpio.h>
#include <kernel/thread.h>
#include <platform/debug.h>
#include <platform/gpio.h>
#include <target/debugconfig.h>
#include <target/gpioconfig.h>

#define RXBUF_SIZE 16

//cbuf_t uart0_rx_buf;



void uart_init_early(void)
{
#ifdef ENABLE_UART0

#ifdef  UART0_TX_PIN
    gpio_config(UART0_TX_PIN,GPIO_OUTPUT);
    NRF_UART0->PSELTXD =   UART0_TX_PIN;
#endif
#ifdef  UART0_RX_PIN
    gpio_config(UART0_RX_PIN,GPIO_INPUT);
    NRF_UART0->PSELRXD =   UART0_RX_PIN;
#endif

    NRF_UART0->BAUDRATE =   UART_BAUDRATE_BAUDRATE_Baud115200   <<  UART_BAUDRATE_BAUDRATE_Pos;
    NRF_UART0->CONFIG   =   UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos | \
                            UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos;
    NVIC_DisableIRQ(UARTE0_UART0_IRQn);
    NRF_UART0->ENABLE   =   UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos;
    NRF_UART0->TXD      = 'E';
    NRF_UART0->TASKS_STARTTX=1;
    NRF_UART0->TASKS_STARTRX=1;
#endif //ENABLE_UART0
}

void uart_init(void)
{
#ifdef ENABLE_UART0
//    cbuf_initialize(&uart0_rx_buf, RXBUF_SIZE);
//    NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos;
    NRF_UART0->EVENTS_RXDRDY = 0;
//    NVIC_EnableIRQ(UART0_IRQn);
    char c = NRF_UART0->RXD;
    (void)c;
#endif //ENABLE_UART0
}

void nrf52_UARTE0_UART0_IRQ(void)
{
//  char c;
    arm_cm_irq_entry();
    /*
        bool resched = false;
        while ( NRF_UART0->EVENTS_RXDRDY > 0 ) {
            NRF_UART0->EVENTS_RXDRDY = 0;
            c = NRF_UART0->RXD;
            if (!cbuf_space_avail(&uart0_rx_buf)) {
                break;
            }
            cbuf_write_char(&uart0_rx_buf, c, false);
            resched = true;
        }
    */
    arm_cm_irq_exit(false);
}

int uart_putc(int port, char c)
{
    while (NRF_UART0->EVENTS_TXDRDY == 0);
    NRF_UART0->EVENTS_TXDRDY = 0;
    NRF_UART0->TXD = c;
    return 1;
}

int uart_getc(int port, bool wait)
{
    do {
        if (NRF_UART0->EVENTS_RXDRDY > 0) {
            NRF_UART0->EVENTS_RXDRDY=0;
            return NRF_UART0->RXD;
        }
    } while (wait);
    return -1;
}

void uart_flush_tx(int port) {}

void uart_flush_rx(int port) {}

void uart_init_port(int port, uint baud)
{
    // TODO - later
    PANIC_UNIMPLEMENTED;
}