- fix F411 F334 systeminit when HSI used - portinout always read IDR regardless of port direction

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Aug 29 17:15:07 2014 +0100
Revision:
304:89b9c3a9a045
Parent:
300:55638feb26a4
Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea

Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/

1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.

2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.

I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.

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>
mbed_official 85:e1a8e879a6a9 18 #include <string.h>
mbed_official 227:7bd0639b8911 19 #include "mbed_assert.h"
mbed_official 85:e1a8e879a6a9 20
mbed_official 85:e1a8e879a6a9 21 #include "serial_api.h"
mbed_official 85:e1a8e879a6a9 22 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 23 #include "pinmap.h"
mbed_official 227:7bd0639b8911 24
mbed_official 85:e1a8e879a6a9 25 /******************************************************************************
mbed_official 85:e1a8e879a6a9 26 * INITIALIZATION
mbed_official 85:e1a8e879a6a9 27 ******************************************************************************/
mbed_official 85:e1a8e879a6a9 28 #define UART_NUM 1
mbed_official 85:e1a8e879a6a9 29
mbed_official 85:e1a8e879a6a9 30 static uint32_t serial_irq_ids[UART_NUM] = {0};
mbed_official 85:e1a8e879a6a9 31 static uart_irq_handler irq_handler;
mbed_official 300:55638feb26a4 32 static uint32_t acceptedSpeeds[16][2] = {{1200, UART_BAUDRATE_BAUDRATE_Baud1200},
mbed_official 300:55638feb26a4 33 {2400, UART_BAUDRATE_BAUDRATE_Baud2400},
mbed_official 300:55638feb26a4 34 {4800, UART_BAUDRATE_BAUDRATE_Baud4800},
mbed_official 300:55638feb26a4 35 {9600, UART_BAUDRATE_BAUDRATE_Baud9600},
mbed_official 300:55638feb26a4 36 {14400, UART_BAUDRATE_BAUDRATE_Baud14400},
mbed_official 300:55638feb26a4 37 {19200, UART_BAUDRATE_BAUDRATE_Baud19200},
mbed_official 300:55638feb26a4 38 {28800, UART_BAUDRATE_BAUDRATE_Baud28800},
mbed_official 300:55638feb26a4 39 {38400, UART_BAUDRATE_BAUDRATE_Baud38400},
mbed_official 300:55638feb26a4 40 {57600, UART_BAUDRATE_BAUDRATE_Baud57600},
mbed_official 300:55638feb26a4 41 {76800, UART_BAUDRATE_BAUDRATE_Baud76800},
mbed_official 300:55638feb26a4 42 {115200, UART_BAUDRATE_BAUDRATE_Baud115200},
mbed_official 300:55638feb26a4 43 {230400, UART_BAUDRATE_BAUDRATE_Baud230400},
mbed_official 300:55638feb26a4 44 {250000, UART_BAUDRATE_BAUDRATE_Baud250000},
mbed_official 300:55638feb26a4 45 {460800, UART_BAUDRATE_BAUDRATE_Baud460800},
mbed_official 300:55638feb26a4 46 {921600, UART_BAUDRATE_BAUDRATE_Baud921600},
mbed_official 300:55638feb26a4 47 {1000000, UART_BAUDRATE_BAUDRATE_Baud1M}};
mbed_official 85:e1a8e879a6a9 48
mbed_official 85:e1a8e879a6a9 49 int stdio_uart_inited = 0;
mbed_official 85:e1a8e879a6a9 50 serial_t stdio_uart;
mbed_official 85:e1a8e879a6a9 51
mbed_official 85:e1a8e879a6a9 52
mbed_official 85:e1a8e879a6a9 53 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 288:17565898c031 54 UARTName uart = UART_0;
mbed_official 300:55638feb26a4 55
mbed_official 227:7bd0639b8911 56 MBED_ASSERT((int)uart != NC);
mbed_official 300:55638feb26a4 57
mbed_official 85:e1a8e879a6a9 58 obj->uart = (NRF_UART_Type *)uart;
mbed_official 300:55638feb26a4 59
mbed_official 227:7bd0639b8911 60 //pin configurations --
mbed_official 227:7bd0639b8911 61 //outputs
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 85:e1a8e879a6a9 68 obj->uart->PSELRTS = RTS_PIN_NUMBER;
mbed_official 300:55638feb26a4 69 obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;
mbed_official 300:55638feb26a4 70
mbed_official 85:e1a8e879a6a9 71 //inputs
mbed_official 85:e1a8e879a6a9 72 obj->uart->PSELCTS = CTS_PIN_NUMBER;
mbed_official 300:55638feb26a4 73 obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;
mbed_official 300:55638feb26a4 74
mbed_official 300:55638feb26a4 75
mbed_official 85:e1a8e879a6a9 76 // set default baud rate and format
mbed_official 85:e1a8e879a6a9 77 serial_baud (obj, 9600);
mbed_official 85:e1a8e879a6a9 78 serial_format(obj, 8, ParityNone, 1);
mbed_official 300:55638feb26a4 79
mbed_official 300:55638feb26a4 80 obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 81 obj->uart->TASKS_STARTTX = 1;
mbed_official 85:e1a8e879a6a9 82 obj->uart->TASKS_STARTRX = 1;
mbed_official 300:55638feb26a4 83 obj->uart->EVENTS_RXDRDY = 0;
mbed_official 300:55638feb26a4 84
mbed_official 85:e1a8e879a6a9 85 obj->index = 0;
mbed_official 300:55638feb26a4 86
mbed_official 85:e1a8e879a6a9 87 // set rx/tx pins in PullUp mode
mbed_official 85:e1a8e879a6a9 88 pin_mode(tx, PullUp);
mbed_official 85:e1a8e879a6a9 89 pin_mode(rx, PullUp);
mbed_official 300:55638feb26a4 90
mbed_official 85:e1a8e879a6a9 91 if (uart == STDIO_UART) {
mbed_official 85:e1a8e879a6a9 92 stdio_uart_inited = 1;
mbed_official 85:e1a8e879a6a9 93 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 85:e1a8e879a6a9 94 }
mbed_official 85:e1a8e879a6a9 95 }
mbed_official 85:e1a8e879a6a9 96
mbed_official 300:55638feb26a4 97 void serial_free(serial_t *obj)
mbed_official 300:55638feb26a4 98 {
mbed_official 85:e1a8e879a6a9 99 serial_irq_ids[obj->index] = 0;
mbed_official 85:e1a8e879a6a9 100 }
mbed_official 85:e1a8e879a6a9 101
mbed_official 85:e1a8e879a6a9 102 // serial_baud
mbed_official 85:e1a8e879a6a9 103 // set the baud rate, taking in to account the current SystemFrequency
mbed_official 300:55638feb26a4 104 void serial_baud(serial_t *obj, int baudrate)
mbed_official 300:55638feb26a4 105 {
mbed_official 300:55638feb26a4 106 if (baudrate<=1200) {
mbed_official 85:e1a8e879a6a9 107 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
mbed_official 85:e1a8e879a6a9 108 return;
mbed_official 227:7bd0639b8911 109 }
mbed_official 300:55638feb26a4 110
mbed_official 300:55638feb26a4 111 for (int i = 1; i<16; i++) {
mbed_official 300:55638feb26a4 112 if (baudrate<acceptedSpeeds[i][0]) {
mbed_official 300:55638feb26a4 113 obj->uart->BAUDRATE = acceptedSpeeds[i - 1][1];
mbed_official 85:e1a8e879a6a9 114 return;
mbed_official 85:e1a8e879a6a9 115 }
mbed_official 85:e1a8e879a6a9 116 }
mbed_official 85:e1a8e879a6a9 117 obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
mbed_official 85:e1a8e879a6a9 118 }
mbed_official 85:e1a8e879a6a9 119
mbed_official 300:55638feb26a4 120 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
mbed_official 300:55638feb26a4 121 {
mbed_official 85:e1a8e879a6a9 122 // 0: 1 stop bits, 1: 2 stop bits
mbed_official 300:55638feb26a4 123 // int parity_enable, parity_select;
mbed_official 85:e1a8e879a6a9 124 switch (parity) {
mbed_official 227:7bd0639b8911 125 case ParityNone:
mbed_official 300:55638feb26a4 126 obj->uart->CONFIG = 0;
mbed_official 300:55638feb26a4 127 break;
mbed_official 85:e1a8e879a6a9 128 default:
mbed_official 300:55638feb26a4 129 obj->uart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos);
mbed_official 85:e1a8e879a6a9 130 return;
mbed_official 85:e1a8e879a6a9 131 }
mbed_official 85:e1a8e879a6a9 132 //no Flow Control
mbed_official 85:e1a8e879a6a9 133 }
mbed_official 85:e1a8e879a6a9 134
mbed_official 85:e1a8e879a6a9 135 //******************************************************************************
mbed_official 85:e1a8e879a6a9 136 // * INTERRUPT HANDLING
mbed_official 85:e1a8e879a6a9 137 //******************************************************************************
mbed_official 300:55638feb26a4 138 static inline void uart_irq(uint32_t iir, uint32_t index)
mbed_official 300:55638feb26a4 139 {
mbed_official 85:e1a8e879a6a9 140 SerialIrq irq_type;
mbed_official 85:e1a8e879a6a9 141 switch (iir) {
mbed_official 227:7bd0639b8911 142 case 1:
mbed_official 227:7bd0639b8911 143 irq_type = TxIrq;
mbed_official 300:55638feb26a4 144 break;
mbed_official 227:7bd0639b8911 145 case 2:
mbed_official 227:7bd0639b8911 146 irq_type = RxIrq;
mbed_official 300:55638feb26a4 147 break;
mbed_official 300:55638feb26a4 148
mbed_official 300:55638feb26a4 149 default:
mbed_official 300:55638feb26a4 150 return;
mbed_official 85:e1a8e879a6a9 151 }
mbed_official 300:55638feb26a4 152
mbed_official 300:55638feb26a4 153 if (serial_irq_ids[index] != 0) {
mbed_official 85:e1a8e879a6a9 154 irq_handler(serial_irq_ids[index], irq_type);
mbed_official 85:e1a8e879a6a9 155 }
mbed_official 85:e1a8e879a6a9 156 }
mbed_official 300:55638feb26a4 157
mbed_official 85:e1a8e879a6a9 158 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 159 extern "C" {
mbed_official 227:7bd0639b8911 160 #endif
mbed_official 85:e1a8e879a6a9 161 void UART0_IRQHandler()
mbed_official 85:e1a8e879a6a9 162 {
mbed_official 300:55638feb26a4 163 uint32_t irtype = 0;
mbed_official 300:55638feb26a4 164
mbed_official 300:55638feb26a4 165 if (NRF_UART0->EVENTS_TXDRDY) {
mbed_official 300:55638feb26a4 166 irtype = 1;
mbed_official 300:55638feb26a4 167 } else if (NRF_UART0->EVENTS_RXDRDY) {
mbed_official 300:55638feb26a4 168 irtype = 2;
mbed_official 85:e1a8e879a6a9 169 }
mbed_official 85:e1a8e879a6a9 170 uart_irq(irtype, 0);
mbed_official 85:e1a8e879a6a9 171 }
mbed_official 300:55638feb26a4 172
mbed_official 85:e1a8e879a6a9 173 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 174 }
mbed_official 227:7bd0639b8911 175 #endif
mbed_official 300:55638feb26a4 176 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
mbed_official 300:55638feb26a4 177 {
mbed_official 300:55638feb26a4 178 irq_handler = handler;
mbed_official 85:e1a8e879a6a9 179 serial_irq_ids[obj->index] = id;
mbed_official 85:e1a8e879a6a9 180 }
mbed_official 85:e1a8e879a6a9 181
mbed_official 300:55638feb26a4 182 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
mbed_official 300:55638feb26a4 183 {
mbed_official 85:e1a8e879a6a9 184 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 85:e1a8e879a6a9 185
mbed_official 85:e1a8e879a6a9 186 switch ((int)obj->uart) {
mbed_official 300:55638feb26a4 187 case UART_0:
mbed_official 300:55638feb26a4 188 irq_n = UART0_IRQn;
mbed_official 85:e1a8e879a6a9 189 break;
mbed_official 85:e1a8e879a6a9 190 }
mbed_official 300:55638feb26a4 191
mbed_official 85:e1a8e879a6a9 192 if (enable) {
mbed_official 85:e1a8e879a6a9 193 switch (irq) {
mbed_official 300:55638feb26a4 194 case RxIrq:
mbed_official 300:55638feb26a4 195 obj->uart->INTENSET |= (UART_INTENSET_RXDRDY_Msk);
mbed_official 300:55638feb26a4 196 break;
mbed_official 300:55638feb26a4 197 case TxIrq:
mbed_official 300:55638feb26a4 198 obj->uart->INTENSET |= (UART_INTENSET_TXDRDY_Msk);
mbed_official 300:55638feb26a4 199 break;
mbed_official 85:e1a8e879a6a9 200 }
mbed_official 85:e1a8e879a6a9 201 NVIC_SetPriority(irq_n, 3);
mbed_official 85:e1a8e879a6a9 202 NVIC_EnableIRQ(irq_n);
mbed_official 300:55638feb26a4 203 } else { // disable
mbed_official 85:e1a8e879a6a9 204 int all_disabled = 0;
mbed_official 85:e1a8e879a6a9 205 switch (irq) {
mbed_official 300:55638feb26a4 206 case RxIrq:
mbed_official 300:55638feb26a4 207 obj->uart->INTENSET &= ~(UART_INTENSET_RXDRDY_Msk);
mbed_official 300:55638feb26a4 208 all_disabled = (obj->uart->INTENSET & (UART_INTENSET_TXDRDY_Msk))==0;
mbed_official 300:55638feb26a4 209 break;
mbed_official 300:55638feb26a4 210 case TxIrq:
mbed_official 300:55638feb26a4 211 obj->uart->INTENSET &= ~(UART_INTENSET_TXDRDY_Msk);
mbed_official 300:55638feb26a4 212 all_disabled = (obj->uart->INTENSET & (UART_INTENSET_RXDRDY_Msk))==0;
mbed_official 300:55638feb26a4 213 break;
mbed_official 85:e1a8e879a6a9 214 }
mbed_official 300:55638feb26a4 215
mbed_official 300:55638feb26a4 216 if (all_disabled) {
mbed_official 85:e1a8e879a6a9 217 NVIC_DisableIRQ(irq_n);
mbed_official 85:e1a8e879a6a9 218 }
mbed_official 85:e1a8e879a6a9 219 }
mbed_official 85:e1a8e879a6a9 220 }
mbed_official 85:e1a8e879a6a9 221
mbed_official 85:e1a8e879a6a9 222 //******************************************************************************
mbed_official 85:e1a8e879a6a9 223 //* READ/WRITE
mbed_official 85:e1a8e879a6a9 224 //******************************************************************************
mbed_official 300:55638feb26a4 225 int serial_getc(serial_t *obj)
mbed_official 300:55638feb26a4 226 {
mbed_official 300:55638feb26a4 227 while (!serial_readable(obj)) {
mbed_official 300:55638feb26a4 228 }
mbed_official 300:55638feb26a4 229
mbed_official 85:e1a8e879a6a9 230 obj->uart->EVENTS_RXDRDY = 0;
mbed_official 300:55638feb26a4 231
mbed_official 85:e1a8e879a6a9 232 return (uint8_t)obj->uart->RXD;
mbed_official 85:e1a8e879a6a9 233 }
mbed_official 85:e1a8e879a6a9 234
mbed_official 300:55638feb26a4 235 void serial_putc(serial_t *obj, int c)
mbed_official 300:55638feb26a4 236 {
mbed_official 85:e1a8e879a6a9 237 obj->uart->TXD = (uint8_t)c;
mbed_official 300:55638feb26a4 238
mbed_official 300:55638feb26a4 239 while (!serial_writable(obj)) {
mbed_official 300:55638feb26a4 240 }
mbed_official 300:55638feb26a4 241
mbed_official 300:55638feb26a4 242 obj->uart->EVENTS_TXDRDY = 0;
mbed_official 85:e1a8e879a6a9 243 }
mbed_official 85:e1a8e879a6a9 244
mbed_official 300:55638feb26a4 245 int serial_readable(serial_t *obj)
mbed_official 300:55638feb26a4 246 {
mbed_official 85:e1a8e879a6a9 247 return (obj->uart->EVENTS_RXDRDY == 1);
mbed_official 85:e1a8e879a6a9 248 }
mbed_official 85:e1a8e879a6a9 249
mbed_official 300:55638feb26a4 250 int serial_writable(serial_t *obj)
mbed_official 300:55638feb26a4 251 {
mbed_official 85:e1a8e879a6a9 252 return (obj->uart->EVENTS_TXDRDY ==1);
mbed_official 85:e1a8e879a6a9 253 }
mbed_official 85:e1a8e879a6a9 254
mbed_official 300:55638feb26a4 255 void serial_break_set(serial_t *obj)
mbed_official 300:55638feb26a4 256 {
mbed_official 85:e1a8e879a6a9 257 obj->uart->TASKS_SUSPEND = 1;
mbed_official 85:e1a8e879a6a9 258 }
mbed_official 85:e1a8e879a6a9 259
mbed_official 300:55638feb26a4 260 void serial_break_clear(serial_t *obj)
mbed_official 300:55638feb26a4 261 {
mbed_official 85:e1a8e879a6a9 262 obj->uart->TASKS_STARTTX = 1;
mbed_official 85:e1a8e879a6a9 263 obj->uart->TASKS_STARTRX = 1;
mbed_official 85:e1a8e879a6a9 264 }