mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 // math.h required for floating point operations for baud rate calculation
elessair 0:f269e3021894 17 #include "mbed_assert.h"
elessair 0:f269e3021894 18 #include <math.h>
elessair 0:f269e3021894 19 #include <string.h>
elessair 0:f269e3021894 20 #include <stdlib.h>
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include "serial_api.h"
elessair 0:f269e3021894 23 #include "cmsis.h"
elessair 0:f269e3021894 24 #include "pinmap.h"
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 /******************************************************************************
elessair 0:f269e3021894 27 * INITIALIZATION
elessair 0:f269e3021894 28 ******************************************************************************/
elessair 0:f269e3021894 29 #define UART_NUM 1
elessair 0:f269e3021894 30
elessair 0:f269e3021894 31 static const PinMap PinMap_UART_TX[] = {
elessair 0:f269e3021894 32 {P0_19, UART_0, 1},
elessair 0:f269e3021894 33 {P1_13, UART_0, 3},
elessair 0:f269e3021894 34 {P1_27, UART_0, 2},
elessair 0:f269e3021894 35 { NC , NC , 0}
elessair 0:f269e3021894 36 };
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 static const PinMap PinMap_UART_RX[] = {
elessair 0:f269e3021894 39 {P0_18, UART_0, 1},
elessair 0:f269e3021894 40 {P1_14, UART_0, 3},
elessair 0:f269e3021894 41 {P1_26, UART_0, 2},
elessair 0:f269e3021894 42 {NC , NC , 0}
elessair 0:f269e3021894 43 };
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 static uint32_t serial_irq_ids[UART_NUM] = {0};
elessair 0:f269e3021894 46 static uart_irq_handler irq_handler;
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 int stdio_uart_inited = 0;
elessair 0:f269e3021894 49 serial_t stdio_uart;
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 void serial_init(serial_t *obj, PinName tx, PinName rx) {
elessair 0:f269e3021894 52 int is_stdio_uart = 0;
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 // determine the UART to use
elessair 0:f269e3021894 55 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
elessair 0:f269e3021894 56 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
elessair 0:f269e3021894 57 UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
elessair 0:f269e3021894 58 MBED_ASSERT((int)uart != NC);
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 obj->uart = (LPC_USART_Type *)uart;
elessair 0:f269e3021894 61 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 // [TODO] Consider more elegant approach
elessair 0:f269e3021894 64 // disconnect USBTX/RX mapping mux, for case when switching ports
elessair 0:f269e3021894 65 //pin_function(USBTX, 0);
elessair 0:f269e3021894 66 //pin_function(USBRX, 0);
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 // enable fifos and default rx trigger level
elessair 0:f269e3021894 69 obj->uart->FCR = 1 << 0 // FIFO Enable - 0 = Disables, 1 = Enabled
elessair 0:f269e3021894 70 | 0 << 1 // Rx Fifo Reset
elessair 0:f269e3021894 71 | 0 << 2 // Tx Fifo Reset
elessair 0:f269e3021894 72 | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 // disable irqs
elessair 0:f269e3021894 75 obj->uart->IER = 0 << 0 // Rx Data available irq enable
elessair 0:f269e3021894 76 | 0 << 1 // Tx Fifo empty irq enable
elessair 0:f269e3021894 77 | 0 << 2; // Rx Line Status irq enable
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 // set default baud rate and format
elessair 0:f269e3021894 80 serial_baud (obj, 9600);
elessair 0:f269e3021894 81 serial_format(obj, 8, ParityNone, 1);
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 // pinout the chosen uart
elessair 0:f269e3021894 84 pinmap_pinout(tx, PinMap_UART_TX);
elessair 0:f269e3021894 85 pinmap_pinout(rx, PinMap_UART_RX);
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 // set rx/tx pins in PullUp mode
elessair 0:f269e3021894 88 if (tx != NC) {
elessair 0:f269e3021894 89 pin_mode(tx, PullUp);
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91 if (rx != NC) {
elessair 0:f269e3021894 92 pin_mode(rx, PullUp);
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 switch (uart) {
elessair 0:f269e3021894 96 case UART_0: obj->index = 0; break;
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 if (is_stdio_uart) {
elessair 0:f269e3021894 102 stdio_uart_inited = 1;
elessair 0:f269e3021894 103 memcpy(&stdio_uart, obj, sizeof(serial_t));
elessair 0:f269e3021894 104 }
elessair 0:f269e3021894 105 }
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 void serial_free(serial_t *obj) {
elessair 0:f269e3021894 108 serial_irq_ids[obj->index] = 0;
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 // serial_baud
elessair 0:f269e3021894 112 // set the baud rate, taking in to account the current SystemFrequency
elessair 0:f269e3021894 113 void serial_baud(serial_t *obj, int baudrate) {
elessair 0:f269e3021894 114 LPC_SYSCON->UARTCLKDIV = 0x1;
elessair 0:f269e3021894 115 uint32_t PCLK = SystemCoreClock;
elessair 0:f269e3021894 116 // First we check to see if the basic divide with no DivAddVal/MulVal
elessair 0:f269e3021894 117 // ratio gives us an integer result. If it does, we set DivAddVal = 0,
elessair 0:f269e3021894 118 // MulVal = 1. Otherwise, we search the valid ratio value range to find
elessair 0:f269e3021894 119 // the closest match. This could be more elegant, using search methods
elessair 0:f269e3021894 120 // and/or lookup tables, but the brute force method is not that much
elessair 0:f269e3021894 121 // slower, and is more maintainable.
elessair 0:f269e3021894 122 uint16_t DL = PCLK / (16 * baudrate);
elessair 0:f269e3021894 123
elessair 0:f269e3021894 124 uint8_t DivAddVal = 0;
elessair 0:f269e3021894 125 uint8_t MulVal = 1;
elessair 0:f269e3021894 126 int hit = 0;
elessair 0:f269e3021894 127 uint16_t dlv;
elessair 0:f269e3021894 128 uint8_t mv, dav;
elessair 0:f269e3021894 129 if ((PCLK % (16 * baudrate)) != 0) { // Checking for zero remainder
elessair 0:f269e3021894 130 int err_best = baudrate, b;
elessair 0:f269e3021894 131 for (mv = 1; mv < 16 && !hit; mv++)
elessair 0:f269e3021894 132 {
elessair 0:f269e3021894 133 for (dav = 0; dav < mv; dav++)
elessair 0:f269e3021894 134 {
elessair 0:f269e3021894 135 // baudrate = PCLK / (16 * dlv * (1 + (DivAdd / Mul))
elessair 0:f269e3021894 136 // solving for dlv, we get dlv = mul * PCLK / (16 * baudrate * (divadd + mul))
elessair 0:f269e3021894 137 // mul has 4 bits, PCLK has 27 so we have 1 bit headroom which can be used for rounding
elessair 0:f269e3021894 138 // for many values of mul and PCLK we have 2 or more bits of headroom which can be used to improve precision
elessair 0:f269e3021894 139 // note: X / 32 doesn't round correctly. Instead, we use ((X / 16) + 1) / 2 for correct rounding
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 if ((mv * PCLK * 2) & 0x80000000) // 1 bit headroom
elessair 0:f269e3021894 142 dlv = ((((2 * mv * PCLK) / (baudrate * (dav + mv))) / 16) + 1) / 2;
elessair 0:f269e3021894 143 else // 2 bits headroom, use more precision
elessair 0:f269e3021894 144 dlv = ((((4 * mv * PCLK) / (baudrate * (dav + mv))) / 32) + 1) / 2;
elessair 0:f269e3021894 145
elessair 0:f269e3021894 146 // datasheet says if DLL==DLM==0, then 1 is used instead since divide by zero is ungood
elessair 0:f269e3021894 147 if (dlv == 0)
elessair 0:f269e3021894 148 dlv = 1;
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 // datasheet says if dav > 0 then DL must be >= 2
elessair 0:f269e3021894 151 if ((dav > 0) && (dlv < 2))
elessair 0:f269e3021894 152 dlv = 2;
elessair 0:f269e3021894 153
elessair 0:f269e3021894 154 // integer rearrangement of the baudrate equation (with rounding)
elessair 0:f269e3021894 155 b = ((PCLK * mv / (dlv * (dav + mv) * 8)) + 1) / 2;
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 // check to see how we went
elessair 0:f269e3021894 158 b = abs(b - baudrate);
elessair 0:f269e3021894 159 if (b < err_best)
elessair 0:f269e3021894 160 {
elessair 0:f269e3021894 161 err_best = b;
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163 DL = dlv;
elessair 0:f269e3021894 164 MulVal = mv;
elessair 0:f269e3021894 165 DivAddVal = dav;
elessair 0:f269e3021894 166
elessair 0:f269e3021894 167 if (b == baudrate)
elessair 0:f269e3021894 168 {
elessair 0:f269e3021894 169 hit = 1;
elessair 0:f269e3021894 170 break;
elessair 0:f269e3021894 171 }
elessair 0:f269e3021894 172 }
elessair 0:f269e3021894 173 }
elessair 0:f269e3021894 174 }
elessair 0:f269e3021894 175 }
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 // set LCR[DLAB] to enable writing to divider registers
elessair 0:f269e3021894 178 obj->uart->LCR |= (1 << 7);
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 // set divider values
elessair 0:f269e3021894 181 obj->uart->DLM = (DL >> 8) & 0xFF;
elessair 0:f269e3021894 182 obj->uart->DLL = (DL >> 0) & 0xFF;
elessair 0:f269e3021894 183 obj->uart->FDR = (uint32_t) DivAddVal << 0
elessair 0:f269e3021894 184 | (uint32_t) MulVal << 4;
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 // clear LCR[DLAB]
elessair 0:f269e3021894 187 obj->uart->LCR &= ~(1 << 7);
elessair 0:f269e3021894 188 }
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
elessair 0:f269e3021894 191 MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
elessair 0:f269e3021894 192 MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
elessair 0:f269e3021894 193 MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) ||
elessair 0:f269e3021894 194 (parity == ParityForced1) || (parity == ParityForced0));
elessair 0:f269e3021894 195
elessair 0:f269e3021894 196 stop_bits -= 1;
elessair 0:f269e3021894 197 data_bits -= 5;
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 int parity_enable, parity_select;
elessair 0:f269e3021894 200 switch (parity) {
elessair 0:f269e3021894 201 case ParityNone: parity_enable = 0; parity_select = 0; break;
elessair 0:f269e3021894 202 case ParityOdd : parity_enable = 1; parity_select = 0; break;
elessair 0:f269e3021894 203 case ParityEven: parity_enable = 1; parity_select = 1; break;
elessair 0:f269e3021894 204 case ParityForced1: parity_enable = 1; parity_select = 2; break;
elessair 0:f269e3021894 205 case ParityForced0: parity_enable = 1; parity_select = 3; break;
elessair 0:f269e3021894 206 default:
elessair 0:f269e3021894 207 break;
elessair 0:f269e3021894 208 }
elessair 0:f269e3021894 209
elessair 0:f269e3021894 210 obj->uart->LCR = data_bits << 0
elessair 0:f269e3021894 211 | stop_bits << 2
elessair 0:f269e3021894 212 | parity_enable << 3
elessair 0:f269e3021894 213 | parity_select << 4;
elessair 0:f269e3021894 214 }
elessair 0:f269e3021894 215
elessair 0:f269e3021894 216 /******************************************************************************
elessair 0:f269e3021894 217 * INTERRUPTS HANDLING
elessair 0:f269e3021894 218 ******************************************************************************/
elessair 0:f269e3021894 219 static inline void uart_irq(uint32_t iir, uint32_t index) {
elessair 0:f269e3021894 220 // [Chapter 14] LPC17xx UART0/2/3: UARTn Interrupt Handling
elessair 0:f269e3021894 221 SerialIrq irq_type;
elessair 0:f269e3021894 222 switch (iir) {
elessair 0:f269e3021894 223 case 1: irq_type = TxIrq; break;
elessair 0:f269e3021894 224 case 2: irq_type = RxIrq; break;
elessair 0:f269e3021894 225 default: return;
elessair 0:f269e3021894 226 }
elessair 0:f269e3021894 227
elessair 0:f269e3021894 228 if (serial_irq_ids[index] != 0)
elessair 0:f269e3021894 229 irq_handler(serial_irq_ids[index], irq_type);
elessair 0:f269e3021894 230 }
elessair 0:f269e3021894 231
elessair 0:f269e3021894 232 void uart0_irq() {uart_irq((LPC_USART->IIR >> 1) & 0x7, 0);}
elessair 0:f269e3021894 233
elessair 0:f269e3021894 234 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
elessair 0:f269e3021894 235 irq_handler = handler;
elessair 0:f269e3021894 236 serial_irq_ids[obj->index] = id;
elessair 0:f269e3021894 237 }
elessair 0:f269e3021894 238
elessair 0:f269e3021894 239 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
elessair 0:f269e3021894 240 IRQn_Type irq_n = (IRQn_Type)0;
elessair 0:f269e3021894 241 uint32_t vector = 0;
elessair 0:f269e3021894 242 switch ((int)obj->uart) {
elessair 0:f269e3021894 243 case UART_0: irq_n=USART_IRQn ; vector = (uint32_t)&uart0_irq; break;
elessair 0:f269e3021894 244 }
elessair 0:f269e3021894 245
elessair 0:f269e3021894 246 if (enable) {
elessair 0:f269e3021894 247 obj->uart->IER |= 1 << irq;
elessair 0:f269e3021894 248 NVIC_SetVector(irq_n, vector);
elessair 0:f269e3021894 249 NVIC_EnableIRQ(irq_n);
elessair 0:f269e3021894 250 } else { // disable
elessair 0:f269e3021894 251 int all_disabled = 0;
elessair 0:f269e3021894 252 SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
elessair 0:f269e3021894 253
elessair 0:f269e3021894 254 obj->uart->IER &= ~(1 << irq);
elessair 0:f269e3021894 255 all_disabled = (obj->uart->IER & (1 << other_irq)) == 0;
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 if (all_disabled)
elessair 0:f269e3021894 258 NVIC_DisableIRQ(irq_n);
elessair 0:f269e3021894 259 }
elessair 0:f269e3021894 260 }
elessair 0:f269e3021894 261
elessair 0:f269e3021894 262 /******************************************************************************
elessair 0:f269e3021894 263 * READ/WRITE
elessair 0:f269e3021894 264 ******************************************************************************/
elessair 0:f269e3021894 265 int serial_getc(serial_t *obj) {
elessair 0:f269e3021894 266 while (!serial_readable(obj));
elessair 0:f269e3021894 267 return obj->uart->RBR;
elessair 0:f269e3021894 268 }
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 void serial_putc(serial_t *obj, int c) {
elessair 0:f269e3021894 271 while (!serial_writable(obj));
elessair 0:f269e3021894 272 obj->uart->THR = c;
elessair 0:f269e3021894 273 }
elessair 0:f269e3021894 274
elessair 0:f269e3021894 275 int serial_readable(serial_t *obj) {
elessair 0:f269e3021894 276 return obj->uart->LSR & 0x01;
elessair 0:f269e3021894 277 }
elessair 0:f269e3021894 278
elessair 0:f269e3021894 279 int serial_writable(serial_t *obj) {
elessair 0:f269e3021894 280 return obj->uart->LSR & 0x20;
elessair 0:f269e3021894 281 }
elessair 0:f269e3021894 282
elessair 0:f269e3021894 283 void serial_clear(serial_t *obj) {
elessair 0:f269e3021894 284 obj->uart->FCR = 1 << 1 // rx FIFO reset
elessair 0:f269e3021894 285 | 1 << 2 // tx FIFO reset
elessair 0:f269e3021894 286 | 0 << 6; // interrupt depth
elessair 0:f269e3021894 287 }
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 void serial_pinout_tx(PinName tx) {
elessair 0:f269e3021894 290 pinmap_pinout(tx, PinMap_UART_TX);
elessair 0:f269e3021894 291 }
elessair 0:f269e3021894 292
elessair 0:f269e3021894 293 void serial_break_set(serial_t *obj) {
elessair 0:f269e3021894 294 obj->uart->LCR |= (1 << 6);
elessair 0:f269e3021894 295 }
elessair 0:f269e3021894 296
elessair 0:f269e3021894 297 void serial_break_clear(serial_t *obj) {
elessair 0:f269e3021894 298 obj->uart->LCR &= ~(1 << 6);
elessair 0:f269e3021894 299 }
elessair 0:f269e3021894 300