mbed library sources for airmote

Fork of mbed-src by mbed official

Committer:
zskdan
Date:
Tue Nov 24 14:02:46 2015 +0000
Revision:
625:88d3fa07e462
Parent:
589:6c996abb70b0
remove unused service

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 104:a6a92e2e5a92 2 * Copyright (c) 2013 Nordic Semiconductor
mbed_official 85:e1a8e879a6a9 3 *
mbed_official 85:e1a8e879a6a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 85:e1a8e879a6a9 5 * you may not use this file except in compliance with the License.
mbed_official 85:e1a8e879a6a9 6 * You may obtain a copy of the License at
mbed_official 85:e1a8e879a6a9 7 *
mbed_official 85:e1a8e879a6a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 85:e1a8e879a6a9 9 *
mbed_official 85:e1a8e879a6a9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 85:e1a8e879a6a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 85:e1a8e879a6a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 85:e1a8e879a6a9 13 * See the License for the specific language governing permissions and
mbed_official 85:e1a8e879a6a9 14 * limitations under the License.
mbed_official 85:e1a8e879a6a9 15 */
mbed_official 85:e1a8e879a6a9 16 // math.h required for floating point operations for baud rate calculation
mbed_official 85:e1a8e879a6a9 17 //#include <math.h>
zskdan 625:88d3fa07e462 18 #if 0
mbed_official 85:e1a8e879a6a9 19 #include <string.h>
mbed_official 227:7bd0639b8911 20 #include "mbed_assert.h"
mbed_official 85:e1a8e879a6a9 21
mbed_official 85:e1a8e879a6a9 22 #include "serial_api.h"
mbed_official 85:e1a8e879a6a9 23 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 24 #include "pinmap.h"
mbed_official 227:7bd0639b8911 25
mbed_official 85:e1a8e879a6a9 26 /******************************************************************************
mbed_official 85:e1a8e879a6a9 27 * INITIALIZATION
mbed_official 85:e1a8e879a6a9 28 ******************************************************************************/
mbed_official 85:e1a8e879a6a9 29 #define UART_NUM 1
mbed_official 85:e1a8e879a6a9 30
mbed_official 85:e1a8e879a6a9 31 static uint32_t serial_irq_ids[UART_NUM] = {0};
mbed_official 85:e1a8e879a6a9 32 static uart_irq_handler irq_handler;
mbed_official 589:6c996abb70b0 33 static const int acceptedSpeeds[17][2] = {
mbed_official 589:6c996abb70b0 34 {1200, UART_BAUDRATE_BAUDRATE_Baud1200},
mbed_official 589:6c996abb70b0 35 {2400, UART_BAUDRATE_BAUDRATE_Baud2400},
mbed_official 589:6c996abb70b0 36 {4800, UART_BAUDRATE_BAUDRATE_Baud4800},
mbed_official 589:6c996abb70b0 37 {9600, UART_BAUDRATE_BAUDRATE_Baud9600},
mbed_official 589:6c996abb70b0 38 {14400, UART_BAUDRATE_BAUDRATE_Baud14400},
mbed_official 589:6c996abb70b0 39 {19200, UART_BAUDRATE_BAUDRATE_Baud19200},
mbed_official 589:6c996abb70b0 40 {28800, UART_BAUDRATE_BAUDRATE_Baud28800},
mbed_official 589:6c996abb70b0 41 {31250, (0x00800000UL) /* 31250 baud */},
mbed_official 589:6c996abb70b0 42 {38400, UART_BAUDRATE_BAUDRATE_Baud38400},
mbed_official 589:6c996abb70b0 43 {57600, UART_BAUDRATE_BAUDRATE_Baud57600},
mbed_official 589:6c996abb70b0 44 {76800, UART_BAUDRATE_BAUDRATE_Baud76800},
mbed_official 589:6c996abb70b0 45 {115200, UART_BAUDRATE_BAUDRATE_Baud115200},
mbed_official 589:6c996abb70b0 46 {230400, UART_BAUDRATE_BAUDRATE_Baud230400},
mbed_official 589:6c996abb70b0 47 {250000, UART_BAUDRATE_BAUDRATE_Baud250000},
mbed_official 589:6c996abb70b0 48 {460800, UART_BAUDRATE_BAUDRATE_Baud460800},
mbed_official 589:6c996abb70b0 49 {921600, UART_BAUDRATE_BAUDRATE_Baud921600},
mbed_official 589:6c996abb70b0 50 {1000000, UART_BAUDRATE_BAUDRATE_Baud1M}
mbed_official 589:6c996abb70b0 51 };
mbed_official 85:e1a8e879a6a9 52
mbed_official 85:e1a8e879a6a9 53 int stdio_uart_inited = 0;
mbed_official 85:e1a8e879a6a9 54 serial_t stdio_uart;
mbed_official 85:e1a8e879a6a9 55
mbed_official 85:e1a8e879a6a9 56
mbed_official 85:e1a8e879a6a9 57 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 288:17565898c031 58 UARTName uart = UART_0;
mbed_official 85:e1a8e879a6a9 59 obj->uart = (NRF_UART_Type *)uart;
mbed_official 300:55638feb26a4 60
mbed_official 227:7bd0639b8911 61 //pin configurations --
mbed_official 300:55638feb26a4 62 NRF_GPIO->DIR |= (1 << tx); //TX_PIN_NUMBER);
mbed_official 300:55638feb26a4 63 NRF_GPIO->DIR |= (1 << RTS_PIN_NUMBER);
mbed_official 85:e1a8e879a6a9 64
mbed_official 300:55638feb26a4 65 NRF_GPIO->DIR &= ~(1 << rx); //RX_PIN_NUMBER);
mbed_official 300:55638feb26a4 66 NRF_GPIO->DIR &= ~(1 << CTS_PIN_NUMBER);
mbed_official 300:55638feb26a4 67
mbed_official 300:55638feb26a4 68
mbed_official 85:e1a8e879a6a9 69 // set default baud rate and format
mbed_official 85:e1a8e879a6a9 70 serial_baud (obj, 9600);
mbed_official 85:e1a8e879a6a9 71 serial_format(obj, 8, ParityNone, 1);
mbed_official 300:55638feb26a4 72
mbed_official 300:55638feb26a4 73 obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 74 obj->uart->TASKS_STARTTX = 1;
mbed_official 85:e1a8e879a6a9 75 obj->uart->TASKS_STARTRX = 1;
mbed_official 300:55638feb26a4 76 obj->uart->EVENTS_RXDRDY = 0;
mbed_official 433:73d4f6083dd2 77 // dummy write needed or TXDRDY trails write rather than leads write.
mbed_official 433:73d4f6083dd2 78 // pins are disconnected so nothing is physically transmitted on the wire
mbed_official 433:73d4f6083dd2 79 obj->uart->TXD = 0;
mbed_official 300:55638feb26a4 80
mbed_official 85:e1a8e879a6a9 81 obj->index = 0;
mbed_official 519:ec58d8604398 82
mbed_official 433:73d4f6083dd2 83 obj->uart->PSELRTS = RTS_PIN_NUMBER;
mbed_official 433:73d4f6083dd2 84 obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;
mbed_official 433:73d4f6083dd2 85 obj->uart->PSELCTS = CTS_PIN_NUMBER;
mbed_official 433:73d4f6083dd2 86 obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;
mbed_official 300:55638feb26a4 87
mbed_official 85:e1a8e879a6a9 88 // set rx/tx pins in PullUp mode
mbed_official 339:40bd4701f3e2 89 if (tx != NC) {
mbed_official 339:40bd4701f3e2 90 pin_mode(tx, PullUp);
mbed_official 339:40bd4701f3e2 91 }
mbed_official 339:40bd4701f3e2 92 if (rx != NC) {
mbed_official 339:40bd4701f3e2 93 pin_mode(rx, PullUp);
mbed_official 339:40bd4701f3e2 94 }
mbed_official 300:55638feb26a4 95
mbed_official 85:e1a8e879a6a9 96 if (uart == STDIO_UART) {
mbed_official 85:e1a8e879a6a9 97 stdio_uart_inited = 1;
mbed_official 85:e1a8e879a6a9 98 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 85:e1a8e879a6a9 99 }
mbed_official 85:e1a8e879a6a9 100 }
mbed_official 85:e1a8e879a6a9 101
mbed_official 300:55638feb26a4 102 void serial_free(serial_t *obj)
mbed_official 300:55638feb26a4 103 {
mbed_official 85:e1a8e879a6a9 104 serial_irq_ids[obj->index] = 0;
mbed_official 85:e1a8e879a6a9 105 }
mbed_official 85:e1a8e879a6a9 106
mbed_official 85:e1a8e879a6a9 107 // serial_baud
mbed_official 85:e1a8e879a6a9 108 // set the baud rate, taking in to account the current SystemFrequency
mbed_official 300:55638feb26a4 109 void serial_baud(serial_t *obj, int baudrate)
mbed_official 300:55638feb26a4 110 {
mbed_official 300:55638feb26a4 111 if (baudrate<=1200) {
mbed_official 85:e1a8e879a6a9 112 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
mbed_official 85:e1a8e879a6a9 113 return;
mbed_official 227:7bd0639b8911 114 }
mbed_official 300:55638feb26a4 115
mbed_official 407:bbbab616ce8f 116 for (int i = 1; i<17; i++) {
mbed_official 300:55638feb26a4 117 if (baudrate<acceptedSpeeds[i][0]) {
mbed_official 300:55638feb26a4 118 obj->uart->BAUDRATE = acceptedSpeeds[i - 1][1];
mbed_official 85:e1a8e879a6a9 119 return;
mbed_official 85:e1a8e879a6a9 120 }
mbed_official 85:e1a8e879a6a9 121 }
mbed_official 85:e1a8e879a6a9 122 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
mbed_official 85:e1a8e879a6a9 123 }
mbed_official 85:e1a8e879a6a9 124
mbed_official 300:55638feb26a4 125 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
mbed_official 300:55638feb26a4 126 {
mbed_official 85:e1a8e879a6a9 127 // 0: 1 stop bits, 1: 2 stop bits
mbed_official 300:55638feb26a4 128 // int parity_enable, parity_select;
mbed_official 85:e1a8e879a6a9 129 switch (parity) {
mbed_official 227:7bd0639b8911 130 case ParityNone:
mbed_official 300:55638feb26a4 131 obj->uart->CONFIG = 0;
mbed_official 300:55638feb26a4 132 break;
mbed_official 85:e1a8e879a6a9 133 default:
mbed_official 300:55638feb26a4 134 obj->uart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos);
mbed_official 85:e1a8e879a6a9 135 return;
mbed_official 85:e1a8e879a6a9 136 }
mbed_official 85:e1a8e879a6a9 137 //no Flow Control
mbed_official 85:e1a8e879a6a9 138 }
mbed_official 85:e1a8e879a6a9 139
mbed_official 85:e1a8e879a6a9 140 //******************************************************************************
mbed_official 85:e1a8e879a6a9 141 // * INTERRUPT HANDLING
mbed_official 85:e1a8e879a6a9 142 //******************************************************************************
mbed_official 300:55638feb26a4 143 static inline void uart_irq(uint32_t iir, uint32_t index)
mbed_official 300:55638feb26a4 144 {
mbed_official 85:e1a8e879a6a9 145 SerialIrq irq_type;
mbed_official 85:e1a8e879a6a9 146 switch (iir) {
mbed_official 227:7bd0639b8911 147 case 1:
mbed_official 227:7bd0639b8911 148 irq_type = TxIrq;
mbed_official 300:55638feb26a4 149 break;
mbed_official 227:7bd0639b8911 150 case 2:
mbed_official 227:7bd0639b8911 151 irq_type = RxIrq;
mbed_official 300:55638feb26a4 152 break;
mbed_official 300:55638feb26a4 153
mbed_official 300:55638feb26a4 154 default:
mbed_official 300:55638feb26a4 155 return;
mbed_official 85:e1a8e879a6a9 156 }
mbed_official 300:55638feb26a4 157
mbed_official 300:55638feb26a4 158 if (serial_irq_ids[index] != 0) {
mbed_official 85:e1a8e879a6a9 159 irq_handler(serial_irq_ids[index], irq_type);
mbed_official 85:e1a8e879a6a9 160 }
mbed_official 85:e1a8e879a6a9 161 }
mbed_official 300:55638feb26a4 162
mbed_official 85:e1a8e879a6a9 163 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 164 extern "C" {
mbed_official 227:7bd0639b8911 165 #endif
mbed_official 85:e1a8e879a6a9 166 void UART0_IRQHandler()
mbed_official 85:e1a8e879a6a9 167 {
mbed_official 519:ec58d8604398 168 uint32_t irtype = 0;
mbed_official 300:55638feb26a4 169
mbed_official 519:ec58d8604398 170 if((NRF_UART0->INTENSET & 0x80) && NRF_UART0->EVENTS_TXDRDY) {
mbed_official 519:ec58d8604398 171 irtype = 1;
mbed_official 519:ec58d8604398 172 } else if((NRF_UART0->INTENSET & 0x04) && NRF_UART0->EVENTS_RXDRDY) {
mbed_official 519:ec58d8604398 173 irtype = 2;
mbed_official 85:e1a8e879a6a9 174 }
mbed_official 519:ec58d8604398 175 uart_irq(irtype, 0);
mbed_official 85:e1a8e879a6a9 176 }
mbed_official 300:55638feb26a4 177
mbed_official 85:e1a8e879a6a9 178 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 179 }
mbed_official 227:7bd0639b8911 180 #endif
mbed_official 300:55638feb26a4 181 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
mbed_official 300:55638feb26a4 182 {
mbed_official 300:55638feb26a4 183 irq_handler = handler;
mbed_official 85:e1a8e879a6a9 184 serial_irq_ids[obj->index] = id;
mbed_official 85:e1a8e879a6a9 185 }
mbed_official 85:e1a8e879a6a9 186
mbed_official 300:55638feb26a4 187 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
mbed_official 300:55638feb26a4 188 {
mbed_official 85:e1a8e879a6a9 189 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 85:e1a8e879a6a9 190
mbed_official 85:e1a8e879a6a9 191 switch ((int)obj->uart) {
mbed_official 300:55638feb26a4 192 case UART_0:
mbed_official 300:55638feb26a4 193 irq_n = UART0_IRQn;
mbed_official 85:e1a8e879a6a9 194 break;
mbed_official 85:e1a8e879a6a9 195 }
mbed_official 300:55638feb26a4 196
mbed_official 85:e1a8e879a6a9 197 if (enable) {
mbed_official 85:e1a8e879a6a9 198 switch (irq) {
mbed_official 300:55638feb26a4 199 case RxIrq:
mbed_official 501:36015dec7d16 200 obj->uart->INTENSET = (UART_INTENSET_RXDRDY_Msk);
mbed_official 300:55638feb26a4 201 break;
mbed_official 300:55638feb26a4 202 case TxIrq:
mbed_official 501:36015dec7d16 203 obj->uart->INTENSET = (UART_INTENSET_TXDRDY_Msk);
mbed_official 300:55638feb26a4 204 break;
mbed_official 85:e1a8e879a6a9 205 }
mbed_official 85:e1a8e879a6a9 206 NVIC_SetPriority(irq_n, 3);
mbed_official 85:e1a8e879a6a9 207 NVIC_EnableIRQ(irq_n);
mbed_official 300:55638feb26a4 208 } else { // disable
mbed_official 433:73d4f6083dd2 209 // maseked writes to INTENSET dont disable and masked writes to
mbed_official 433:73d4f6083dd2 210 // INTENCLR seemed to clear the entire register, not bits.
mbed_official 433:73d4f6083dd2 211 // Added INTEN to memory map and seems to allow set and clearing of specific bits as desired
mbed_official 85:e1a8e879a6a9 212 int all_disabled = 0;
mbed_official 85:e1a8e879a6a9 213 switch (irq) {
mbed_official 300:55638feb26a4 214 case RxIrq:
mbed_official 501:36015dec7d16 215 obj->uart->INTENCLR = (UART_INTENCLR_RXDRDY_Msk);
mbed_official 501:36015dec7d16 216 all_disabled = (obj->uart->INTENCLR & (UART_INTENCLR_TXDRDY_Msk)) == 0;
mbed_official 300:55638feb26a4 217 break;
mbed_official 300:55638feb26a4 218 case TxIrq:
mbed_official 501:36015dec7d16 219 obj->uart->INTENCLR = (UART_INTENCLR_TXDRDY_Msk);
mbed_official 501:36015dec7d16 220 all_disabled = (obj->uart->INTENCLR & (UART_INTENCLR_RXDRDY_Msk)) == 0;
mbed_official 300:55638feb26a4 221 break;
mbed_official 85:e1a8e879a6a9 222 }
mbed_official 300:55638feb26a4 223
mbed_official 300:55638feb26a4 224 if (all_disabled) {
mbed_official 85:e1a8e879a6a9 225 NVIC_DisableIRQ(irq_n);
mbed_official 85:e1a8e879a6a9 226 }
mbed_official 85:e1a8e879a6a9 227 }
mbed_official 85:e1a8e879a6a9 228 }
mbed_official 85:e1a8e879a6a9 229
mbed_official 85:e1a8e879a6a9 230 //******************************************************************************
mbed_official 85:e1a8e879a6a9 231 //* READ/WRITE
mbed_official 85:e1a8e879a6a9 232 //******************************************************************************
mbed_official 300:55638feb26a4 233 int serial_getc(serial_t *obj)
mbed_official 300:55638feb26a4 234 {
mbed_official 300:55638feb26a4 235 while (!serial_readable(obj)) {
mbed_official 300:55638feb26a4 236 }
mbed_official 300:55638feb26a4 237
mbed_official 85:e1a8e879a6a9 238 obj->uart->EVENTS_RXDRDY = 0;
mbed_official 300:55638feb26a4 239
mbed_official 85:e1a8e879a6a9 240 return (uint8_t)obj->uart->RXD;
mbed_official 85:e1a8e879a6a9 241 }
mbed_official 85:e1a8e879a6a9 242
mbed_official 300:55638feb26a4 243 void serial_putc(serial_t *obj, int c)
mbed_official 300:55638feb26a4 244 {
mbed_official 519:ec58d8604398 245 while (!serial_writable(obj)) {
mbed_official 519:ec58d8604398 246 }
mbed_official 300:55638feb26a4 247
mbed_official 519:ec58d8604398 248 obj->uart->EVENTS_TXDRDY = 0;
mbed_official 519:ec58d8604398 249 obj->uart->TXD = (uint8_t)c;
mbed_official 85:e1a8e879a6a9 250 }
mbed_official 85:e1a8e879a6a9 251
mbed_official 300:55638feb26a4 252 int serial_readable(serial_t *obj)
mbed_official 300:55638feb26a4 253 {
mbed_official 85:e1a8e879a6a9 254 return (obj->uart->EVENTS_RXDRDY == 1);
mbed_official 85:e1a8e879a6a9 255 }
mbed_official 85:e1a8e879a6a9 256
mbed_official 300:55638feb26a4 257 int serial_writable(serial_t *obj)
mbed_official 300:55638feb26a4 258 {
mbed_official 433:73d4f6083dd2 259 return (obj->uart->EVENTS_TXDRDY == 1);
mbed_official 85:e1a8e879a6a9 260 }
mbed_official 85:e1a8e879a6a9 261
mbed_official 300:55638feb26a4 262 void serial_break_set(serial_t *obj)
mbed_official 300:55638feb26a4 263 {
mbed_official 85:e1a8e879a6a9 264 obj->uart->TASKS_SUSPEND = 1;
mbed_official 85:e1a8e879a6a9 265 }
mbed_official 85:e1a8e879a6a9 266
mbed_official 300:55638feb26a4 267 void serial_break_clear(serial_t *obj)
mbed_official 300:55638feb26a4 268 {
mbed_official 85:e1a8e879a6a9 269 obj->uart->TASKS_STARTTX = 1;
mbed_official 85:e1a8e879a6a9 270 obj->uart->TASKS_STARTRX = 1;
mbed_official 85:e1a8e879a6a9 271 }
mbed_official 340:28d1f895c6fe 272
mbed_official 340:28d1f895c6fe 273 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
mbed_official 340:28d1f895c6fe 274 {
mbed_official 340:28d1f895c6fe 275
mbed_official 340:28d1f895c6fe 276 if (type == FlowControlRTSCTS || type == FlowControlRTS) {
mbed_official 340:28d1f895c6fe 277 NRF_GPIO->DIR |= (1<<rxflow);
mbed_official 340:28d1f895c6fe 278 pin_mode(rxflow, PullUp);
mbed_official 340:28d1f895c6fe 279 obj->uart->PSELRTS = rxflow;
mbed_official 340:28d1f895c6fe 280
mbed_official 340:28d1f895c6fe 281 obj->uart->CONFIG |= 0x01; // Enable HWFC
mbed_official 340:28d1f895c6fe 282 }
mbed_official 340:28d1f895c6fe 283
mbed_official 340:28d1f895c6fe 284 if (type == FlowControlRTSCTS || type == FlowControlCTS) {
mbed_official 340:28d1f895c6fe 285 NRF_GPIO->DIR &= ~(1<<txflow);
mbed_official 340:28d1f895c6fe 286 pin_mode(txflow, PullUp);
mbed_official 340:28d1f895c6fe 287 obj->uart->PSELCTS = txflow;
mbed_official 340:28d1f895c6fe 288
mbed_official 340:28d1f895c6fe 289 obj->uart->CONFIG |= 0x01; // Enable HWFC;
mbed_official 340:28d1f895c6fe 290 }
mbed_official 340:28d1f895c6fe 291
mbed_official 340:28d1f895c6fe 292 if (type == FlowControlNone) {
mbed_official 340:28d1f895c6fe 293 obj->uart->PSELRTS = 0xFFFFFFFF; // Disable RTS
mbed_official 340:28d1f895c6fe 294 obj->uart->PSELCTS = 0xFFFFFFFF; // Disable CTS
mbed_official 340:28d1f895c6fe 295
mbed_official 340:28d1f895c6fe 296 obj->uart->CONFIG &= ~0x01; // Enable HWFC;
mbed_official 340:28d1f895c6fe 297 }
mbed_official 340:28d1f895c6fe 298 }
mbed_official 340:28d1f895c6fe 299
mbed_official 340:28d1f895c6fe 300 void serial_clear(serial_t *obj) {
mbed_official 340:28d1f895c6fe 301 }
zskdan 625:88d3fa07e462 302 #endif