summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c
blob: 8e3e8c34c72d12c8441668204d2335e74dd560ae (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* mbed Microcontroller Library
 * Copyright (c) 2013 Nordic Semiconductor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// math.h required for floating point operations for baud rate calculation
//#include <math.h>
#include <string.h>
#include "mbed_assert.h"

#include "serial_api.h"
#include "cmsis.h"
#include "pinmap.h"

/******************************************************************************
 * INITIALIZATION
 ******************************************************************************/
#define UART_NUM    1

static uint32_t serial_irq_ids[UART_NUM] = {0};
static uart_irq_handler irq_handler;
static uint32_t acceptedSpeeds[17][2] = {{1200, UART_BAUDRATE_BAUDRATE_Baud1200},
                                         {2400, UART_BAUDRATE_BAUDRATE_Baud2400},
                                         {4800, UART_BAUDRATE_BAUDRATE_Baud4800},
                                         {9600, UART_BAUDRATE_BAUDRATE_Baud9600},
                                         {14400, UART_BAUDRATE_BAUDRATE_Baud14400},
                                         {19200, UART_BAUDRATE_BAUDRATE_Baud19200},
                                         {28800, UART_BAUDRATE_BAUDRATE_Baud28800},
                                         {31250, (0x00800000UL) /* 31250 baud */},
                                         {38400, UART_BAUDRATE_BAUDRATE_Baud38400},
                                         {57600, UART_BAUDRATE_BAUDRATE_Baud57600},
                                         {76800, UART_BAUDRATE_BAUDRATE_Baud76800},
                                         {115200, UART_BAUDRATE_BAUDRATE_Baud115200},
                                         {230400, UART_BAUDRATE_BAUDRATE_Baud230400},
                                         {250000, UART_BAUDRATE_BAUDRATE_Baud250000},
                                         {460800, UART_BAUDRATE_BAUDRATE_Baud460800},
                                         {921600, UART_BAUDRATE_BAUDRATE_Baud921600},
                                         {1000000, UART_BAUDRATE_BAUDRATE_Baud1M}};

int stdio_uart_inited = 0;
serial_t stdio_uart;


void serial_init(serial_t *obj, PinName tx, PinName rx) {
    UARTName uart = UART_0;
    obj->uart = (NRF_UART_Type *)uart;

    //pin configurations --
    NRF_GPIO->DIR |= (1 << tx); //TX_PIN_NUMBER);
    NRF_GPIO->DIR |= (1 << RTS_PIN_NUMBER);

    NRF_GPIO->DIR &= ~(1 << rx); //RX_PIN_NUMBER);
    NRF_GPIO->DIR &= ~(1 << CTS_PIN_NUMBER);


    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);

    obj->uart->ENABLE        = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
    obj->uart->TASKS_STARTTX = 1;
    obj->uart->TASKS_STARTRX = 1;
    obj->uart->EVENTS_RXDRDY = 0;
    // dummy write needed or TXDRDY trails write rather than leads write.
    //  pins are disconnected so nothing is physically transmitted on the wire
    obj->uart->TXD = 0;

    obj->index = 0;
    
    obj->uart->PSELRTS = RTS_PIN_NUMBER;
    obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;
    obj->uart->PSELCTS = CTS_PIN_NUMBER;
    obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;

    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }

    if (uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}

void serial_free(serial_t *obj)
{
    serial_irq_ids[obj->index] = 0;
}

// serial_baud
// set the baud rate, taking in to account the current SystemFrequency
void serial_baud(serial_t *obj, int baudrate)
{
    if (baudrate<=1200) {
        obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
        return;
    }

    for (int i = 1; i<17; i++) {
        if (baudrate<acceptedSpeeds[i][0]) {
            obj->uart->BAUDRATE = acceptedSpeeds[i - 1][1];
            return;
        }
    }
    obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
}

void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
{
    // 0: 1 stop bits, 1: 2 stop bits
    // int parity_enable, parity_select;
    switch (parity) {
        case ParityNone:
            obj->uart->CONFIG = 0;
            break;
        default:
            obj->uart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos);
            return;
    }
    //no Flow Control
}

//******************************************************************************
// * INTERRUPT HANDLING
//******************************************************************************
static inline void uart_irq(uint32_t iir, uint32_t index)
{
    SerialIrq irq_type;
    switch (iir) {
        case 1:
            irq_type = TxIrq;
            break;
        case 2:
            irq_type = RxIrq;
            break;

        default:
            return;
    }

    if (serial_irq_ids[index] != 0) {
        irq_handler(serial_irq_ids[index], irq_type);
    }
}

#ifdef __cplusplus
extern "C" {
#endif
void UART0_IRQHandler()
{
    uint32_t irtype = 0;

    if((NRF_UART0->INTENSET & 0x80) && NRF_UART0->EVENTS_TXDRDY) {
        irtype = 1;
    } else if((NRF_UART0->INTENSET & 0x04) && NRF_UART0->EVENTS_RXDRDY) {
        irtype = 2;
    }
    uart_irq(irtype, 0);
}

#ifdef __cplusplus
}
#endif
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
{
    irq_handler                = handler;
    serial_irq_ids[obj->index] = id;
}

void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
{
    IRQn_Type irq_n = (IRQn_Type)0;

    switch ((int)obj->uart) {
        case UART_0:
            irq_n = UART0_IRQn;
            break;
    }

    if (enable) {
        switch (irq) {
            case RxIrq:
                obj->uart->INTENSET = (UART_INTENSET_RXDRDY_Msk);
                break;
            case TxIrq:
                obj->uart->INTENSET = (UART_INTENSET_TXDRDY_Msk);
                break;
        }
        NVIC_SetPriority(irq_n, 3);
        NVIC_EnableIRQ(irq_n);
    } else { // disable
        // maseked writes to INTENSET dont disable and masked writes to
        //  INTENCLR seemed to clear the entire register, not bits.
        //  Added INTEN to memory map and seems to allow set and clearing of specific bits as desired
        int all_disabled = 0;
        switch (irq) {
            case RxIrq:
                obj->uart->INTENCLR = (UART_INTENCLR_RXDRDY_Msk);
                all_disabled        =  (obj->uart->INTENCLR & (UART_INTENCLR_TXDRDY_Msk)) == 0;
                break;
            case TxIrq:
                obj->uart->INTENCLR = (UART_INTENCLR_TXDRDY_Msk);
                all_disabled        =  (obj->uart->INTENCLR & (UART_INTENCLR_RXDRDY_Msk)) == 0;
                break;
        }

        if (all_disabled) {
            NVIC_DisableIRQ(irq_n);
        }
    }
}

//******************************************************************************
//* READ/WRITE
//******************************************************************************
int serial_getc(serial_t *obj)
{
    while (!serial_readable(obj)) {
    }

    obj->uart->EVENTS_RXDRDY = 0;

    return (uint8_t)obj->uart->RXD;
}

void serial_putc(serial_t *obj, int c)
{
    while (!serial_writable(obj)) {
    }

    obj->uart->EVENTS_TXDRDY = 0;
    obj->uart->TXD = (uint8_t)c;
}

int serial_readable(serial_t *obj)
{
    return (obj->uart->EVENTS_RXDRDY == 1);
}

int serial_writable(serial_t *obj)
{
    return (obj->uart->EVENTS_TXDRDY == 1);
}

void serial_break_set(serial_t *obj)
{
    obj->uart->TASKS_SUSPEND = 1;
}

void serial_break_clear(serial_t *obj)
{
    obj->uart->TASKS_STARTTX = 1;
    obj->uart->TASKS_STARTRX = 1;
}

void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
{

    if (type == FlowControlRTSCTS || type == FlowControlRTS) {
        NRF_GPIO->DIR |= (1<<rxflow);
        pin_mode(rxflow, PullUp);
        obj->uart->PSELRTS = rxflow;

        obj->uart->CONFIG |= 0x01; // Enable HWFC
    }

    if (type == FlowControlRTSCTS || type == FlowControlCTS) {
        NRF_GPIO->DIR &= ~(1<<txflow);
        pin_mode(txflow, PullUp);
        obj->uart->PSELCTS = txflow;

        obj->uart->CONFIG |= 0x01; // Enable HWFC;
    }

    if (type == FlowControlNone) {
        obj->uart->PSELRTS = 0xFFFFFFFF; // Disable RTS
        obj->uart->PSELCTS = 0xFFFFFFFF; // Disable CTS

        obj->uart->CONFIG &= ~0x01; // Enable HWFC;
    }
}

void serial_clear(serial_t *obj) {
}