mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Jul 02 16:15:09 2015 +0100
Revision:
580:3c14cb9b87c5
Synchronized with git revision 213caf296f26963a7bea129b8ec4f33bbd1e6588

Full URL: https://github.com/mbedmicro/mbed/commit/213caf296f26963a7bea129b8ec4f33bbd1e6588/

commit of mps2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 580:3c14cb9b87c5 1 /* mbed Microcontroller Library
mbed_official 580:3c14cb9b87c5 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 580:3c14cb9b87c5 3 *
mbed_official 580:3c14cb9b87c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 580:3c14cb9b87c5 5 * you may not use this file except in compliance with the License.
mbed_official 580:3c14cb9b87c5 6 * You may obtain a copy of the License at
mbed_official 580:3c14cb9b87c5 7 *
mbed_official 580:3c14cb9b87c5 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 580:3c14cb9b87c5 9 *
mbed_official 580:3c14cb9b87c5 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 580:3c14cb9b87c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 580:3c14cb9b87c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 580:3c14cb9b87c5 13 * See the License for the specific language governing permissions and
mbed_official 580:3c14cb9b87c5 14 * limitations under the License.
mbed_official 580:3c14cb9b87c5 15 */
mbed_official 580:3c14cb9b87c5 16 // math.h required for floating point operations for baud rate calculation
mbed_official 580:3c14cb9b87c5 17 #include <math.h>
mbed_official 580:3c14cb9b87c5 18 #include <stdio.h>
mbed_official 580:3c14cb9b87c5 19 #include <string.h>
mbed_official 580:3c14cb9b87c5 20 #include <stdlib.h>
mbed_official 580:3c14cb9b87c5 21
mbed_official 580:3c14cb9b87c5 22 #include "serial_api.h"
mbed_official 580:3c14cb9b87c5 23 #include "cmsis.h"
mbed_official 580:3c14cb9b87c5 24 #include "pinmap.h"
mbed_official 580:3c14cb9b87c5 25 #include "mbed_error.h"
mbed_official 580:3c14cb9b87c5 26 #include "gpio_api.h"
mbed_official 580:3c14cb9b87c5 27
mbed_official 580:3c14cb9b87c5 28 /******************************************************************************
mbed_official 580:3c14cb9b87c5 29 * INITIALIZATION
mbed_official 580:3c14cb9b87c5 30 ******************************************************************************/
mbed_official 580:3c14cb9b87c5 31
mbed_official 580:3c14cb9b87c5 32 static const PinMap PinMap_UART_TX[] = {
mbed_official 580:3c14cb9b87c5 33 {USBTX , UART_0, 0},
mbed_official 580:3c14cb9b87c5 34 {UART_TX1, UART_1, 0},
mbed_official 580:3c14cb9b87c5 35 {NC , NC , 0}
mbed_official 580:3c14cb9b87c5 36 };
mbed_official 580:3c14cb9b87c5 37
mbed_official 580:3c14cb9b87c5 38 static const PinMap PinMap_UART_RX[] = {
mbed_official 580:3c14cb9b87c5 39 {USBRX , UART_0, 0},
mbed_official 580:3c14cb9b87c5 40 {UART_RX1, UART_1, 0},
mbed_official 580:3c14cb9b87c5 41 {NC , NC , 0}
mbed_official 580:3c14cb9b87c5 42 };
mbed_official 580:3c14cb9b87c5 43
mbed_official 580:3c14cb9b87c5 44 #define UART_NUM 3
mbed_official 580:3c14cb9b87c5 45
mbed_official 580:3c14cb9b87c5 46 static uart_irq_handler irq_handler;
mbed_official 580:3c14cb9b87c5 47
mbed_official 580:3c14cb9b87c5 48 int stdio_uart_inited = 0;
mbed_official 580:3c14cb9b87c5 49 serial_t stdio_uart;
mbed_official 580:3c14cb9b87c5 50
mbed_official 580:3c14cb9b87c5 51 struct serial_global_data_s {
mbed_official 580:3c14cb9b87c5 52 uint32_t serial_irq_id;
mbed_official 580:3c14cb9b87c5 53 gpio_t sw_rts, sw_cts;
mbed_official 580:3c14cb9b87c5 54 uint8_t count, rx_irq_set_flow, rx_irq_set_api;
mbed_official 580:3c14cb9b87c5 55 };
mbed_official 580:3c14cb9b87c5 56
mbed_official 580:3c14cb9b87c5 57 static struct serial_global_data_s uart_data[UART_NUM];
mbed_official 580:3c14cb9b87c5 58
mbed_official 580:3c14cb9b87c5 59 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 580:3c14cb9b87c5 60 int is_stdio_uart = 0;
mbed_official 580:3c14cb9b87c5 61
mbed_official 580:3c14cb9b87c5 62 // determine the UART to use
mbed_official 580:3c14cb9b87c5 63 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 580:3c14cb9b87c5 64 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 580:3c14cb9b87c5 65 UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 580:3c14cb9b87c5 66 if ((int)uart == NC) {
mbed_official 580:3c14cb9b87c5 67 error("Serial pinout mapping failed");
mbed_official 580:3c14cb9b87c5 68 }
mbed_official 580:3c14cb9b87c5 69
mbed_official 580:3c14cb9b87c5 70 obj->uart = (CMSDK_UART_TypeDef *)uart;
mbed_official 580:3c14cb9b87c5 71 //set baud rate and enable Uart in normarl mode (RX and TX enabled)
mbed_official 580:3c14cb9b87c5 72 switch (uart) {
mbed_official 580:3c14cb9b87c5 73 case UART_0: CMSDK_UART0->CTRL = 0; // Disable UART when changing configuration
mbed_official 580:3c14cb9b87c5 74 CMSDK_UART0->CTRL = 0x3; // Normal mode
mbed_official 580:3c14cb9b87c5 75 break;
mbed_official 580:3c14cb9b87c5 76 case UART_1: CMSDK_UART1->CTRL = 0; // Disable UART when changing configuration
mbed_official 580:3c14cb9b87c5 77 CMSDK_UART1->CTRL = 0x3; // Normal mode
mbed_official 580:3c14cb9b87c5 78 break;
mbed_official 580:3c14cb9b87c5 79 }
mbed_official 580:3c14cb9b87c5 80
mbed_official 580:3c14cb9b87c5 81 // set default baud rate and format
mbed_official 580:3c14cb9b87c5 82 serial_baud (obj, 9600);
mbed_official 580:3c14cb9b87c5 83
mbed_official 580:3c14cb9b87c5 84 // pinout the chosen uart
mbed_official 580:3c14cb9b87c5 85 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 580:3c14cb9b87c5 86 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 580:3c14cb9b87c5 87
mbed_official 580:3c14cb9b87c5 88 switch (uart) {
mbed_official 580:3c14cb9b87c5 89 case UART_0: obj->index = 0; break;
mbed_official 580:3c14cb9b87c5 90 case UART_1: obj->index = 1; break;
mbed_official 580:3c14cb9b87c5 91 }
mbed_official 580:3c14cb9b87c5 92 uart_data[obj->index].sw_rts.pin = NC;
mbed_official 580:3c14cb9b87c5 93 uart_data[obj->index].sw_cts.pin = NC;
mbed_official 580:3c14cb9b87c5 94 serial_set_flow_control(obj, FlowControlNone, NC, NC);
mbed_official 580:3c14cb9b87c5 95
mbed_official 580:3c14cb9b87c5 96 is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
mbed_official 580:3c14cb9b87c5 97
mbed_official 580:3c14cb9b87c5 98 if (is_stdio_uart) {
mbed_official 580:3c14cb9b87c5 99 stdio_uart_inited = 1;
mbed_official 580:3c14cb9b87c5 100 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 580:3c14cb9b87c5 101 }
mbed_official 580:3c14cb9b87c5 102 }
mbed_official 580:3c14cb9b87c5 103
mbed_official 580:3c14cb9b87c5 104 void serial_free(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 105 }
mbed_official 580:3c14cb9b87c5 106
mbed_official 580:3c14cb9b87c5 107 // serial_baud
mbed_official 580:3c14cb9b87c5 108 // set the baud rate, taking in to account the current SystemFrequency
mbed_official 580:3c14cb9b87c5 109 void serial_baud(serial_t *obj, int baudrate) {
mbed_official 580:3c14cb9b87c5 110 // The MPS2 has a simple divider to control the baud rate. The formula is:
mbed_official 580:3c14cb9b87c5 111 //
mbed_official 580:3c14cb9b87c5 112 // Baudrate = PCLK / BAUDDIV
mbed_official 580:3c14cb9b87c5 113 //
mbed_official 580:3c14cb9b87c5 114 // PCLK = 25 Mhz
mbed_official 580:3c14cb9b87c5 115 // so for a desired baud rate of 9600
mbed_official 580:3c14cb9b87c5 116 // 25000000 / 9600 = 2604
mbed_official 580:3c14cb9b87c5 117 //
mbed_official 580:3c14cb9b87c5 118 //check to see if minimum baud value entered
mbed_official 580:3c14cb9b87c5 119 int baudrate_div = 0;
mbed_official 580:3c14cb9b87c5 120 baudrate_div = 25000000 / baudrate;
mbed_official 580:3c14cb9b87c5 121 if(baudrate >= 16){
mbed_official 580:3c14cb9b87c5 122 switch ((int)obj->uart) {
mbed_official 580:3c14cb9b87c5 123 case UART_0: CMSDK_UART0->BAUDDIV = baudrate_div; break;
mbed_official 580:3c14cb9b87c5 124 case UART_1: CMSDK_UART1->BAUDDIV = baudrate_div; break;
mbed_official 580:3c14cb9b87c5 125 default: error("serial_baud"); break;
mbed_official 580:3c14cb9b87c5 126 }
mbed_official 580:3c14cb9b87c5 127 } else {
mbed_official 580:3c14cb9b87c5 128 error("serial_baud");
mbed_official 580:3c14cb9b87c5 129 }
mbed_official 580:3c14cb9b87c5 130
mbed_official 580:3c14cb9b87c5 131 }
mbed_official 580:3c14cb9b87c5 132
mbed_official 580:3c14cb9b87c5 133 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
mbed_official 580:3c14cb9b87c5 134 }
mbed_official 580:3c14cb9b87c5 135
mbed_official 580:3c14cb9b87c5 136 /******************************************************************************
mbed_official 580:3c14cb9b87c5 137 * INTERRUPTS HANDLING
mbed_official 580:3c14cb9b87c5 138 ******************************************************************************/
mbed_official 580:3c14cb9b87c5 139 static inline void uart_irq(uint32_t intstatus, uint32_t index, CMSDK_UART_TypeDef *puart) {
mbed_official 580:3c14cb9b87c5 140 SerialIrq irq_type;
mbed_official 580:3c14cb9b87c5 141 switch (intstatus) {
mbed_official 580:3c14cb9b87c5 142 case 1: irq_type = TxIrq; break;
mbed_official 580:3c14cb9b87c5 143 case 2: irq_type = RxIrq; break;
mbed_official 580:3c14cb9b87c5 144 default: return;
mbed_official 580:3c14cb9b87c5 145 }
mbed_official 580:3c14cb9b87c5 146 if ((RxIrq == irq_type) && (NC != uart_data[index].sw_rts.pin)) {
mbed_official 580:3c14cb9b87c5 147 gpio_write(&uart_data[index].sw_rts, 1);
mbed_official 580:3c14cb9b87c5 148 // Disable interrupt if it wasn't enabled by other part of the application
mbed_official 580:3c14cb9b87c5 149 if (!uart_data[index].rx_irq_set_api)
mbed_official 580:3c14cb9b87c5 150 puart->CTRL &= ~(1 << RxIrq);
mbed_official 580:3c14cb9b87c5 151 }
mbed_official 580:3c14cb9b87c5 152 if (uart_data[index].serial_irq_id != 0)
mbed_official 580:3c14cb9b87c5 153 if ((irq_type != RxIrq) || (uart_data[index].rx_irq_set_api))
mbed_official 580:3c14cb9b87c5 154 irq_handler(uart_data[index].serial_irq_id, irq_type);
mbed_official 580:3c14cb9b87c5 155 }
mbed_official 580:3c14cb9b87c5 156
mbed_official 580:3c14cb9b87c5 157 void uart0_irq() {uart_irq(CMSDK_UART0->INTSTATUS & 0x3, 0, (CMSDK_UART_TypeDef*)CMSDK_UART0);}
mbed_official 580:3c14cb9b87c5 158 void uart1_irq() {uart_irq(CMSDK_UART1->INTSTATUS & 0x3, 1, (CMSDK_UART_TypeDef*)CMSDK_UART1);}
mbed_official 580:3c14cb9b87c5 159
mbed_official 580:3c14cb9b87c5 160 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
mbed_official 580:3c14cb9b87c5 161 irq_handler = handler;
mbed_official 580:3c14cb9b87c5 162 uart_data[obj->index].serial_irq_id = id;
mbed_official 580:3c14cb9b87c5 163 }
mbed_official 580:3c14cb9b87c5 164
mbed_official 580:3c14cb9b87c5 165 static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 580:3c14cb9b87c5 166 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 580:3c14cb9b87c5 167 uint32_t vector = 0;
mbed_official 580:3c14cb9b87c5 168 switch ((int)obj->uart) {
mbed_official 580:3c14cb9b87c5 169 case UART_0: irq_n=((irq>> 2) ? UARTRX0_IRQn : UARTTX0_IRQn); vector = (uint32_t)&uart0_irq; break;
mbed_official 580:3c14cb9b87c5 170 case UART_1: irq_n=((irq>> 2) ? UARTRX1_IRQn : UARTTX1_IRQn); vector = (uint32_t)&uart1_irq; break;
mbed_official 580:3c14cb9b87c5 171 }
mbed_official 580:3c14cb9b87c5 172
mbed_official 580:3c14cb9b87c5 173 if (enable) {
mbed_official 580:3c14cb9b87c5 174 obj->uart->CTRL |= 1 << irq;
mbed_official 580:3c14cb9b87c5 175 NVIC_SetVector(irq_n, vector);
mbed_official 580:3c14cb9b87c5 176 NVIC_EnableIRQ(irq_n);
mbed_official 580:3c14cb9b87c5 177 } else if ((TxIrq == irq) || (uart_data[obj->index].rx_irq_set_api + uart_data[obj->index].rx_irq_set_flow == 0)) { // disable
mbed_official 580:3c14cb9b87c5 178 int all_disabled = 0;
mbed_official 580:3c14cb9b87c5 179 SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
mbed_official 580:3c14cb9b87c5 180 obj->uart->CTRL &= ~(1 << irq);
mbed_official 580:3c14cb9b87c5 181 all_disabled = (obj->uart->CTRL & (1 << other_irq)) == 0;
mbed_official 580:3c14cb9b87c5 182 if (all_disabled)
mbed_official 580:3c14cb9b87c5 183 NVIC_DisableIRQ(irq_n);
mbed_official 580:3c14cb9b87c5 184 }
mbed_official 580:3c14cb9b87c5 185 }
mbed_official 580:3c14cb9b87c5 186
mbed_official 580:3c14cb9b87c5 187 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 580:3c14cb9b87c5 188 if (RxIrq == irq)
mbed_official 580:3c14cb9b87c5 189 uart_data[obj->index].rx_irq_set_api = enable;
mbed_official 580:3c14cb9b87c5 190 serial_irq_set_internal(obj, irq, enable);
mbed_official 580:3c14cb9b87c5 191 }
mbed_official 580:3c14cb9b87c5 192
mbed_official 580:3c14cb9b87c5 193 /******************************************************************************
mbed_official 580:3c14cb9b87c5 194 * READ/WRITE
mbed_official 580:3c14cb9b87c5 195 ******************************************************************************/
mbed_official 580:3c14cb9b87c5 196 int serial_getc(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 197 while (serial_readable(obj) == 0);
mbed_official 580:3c14cb9b87c5 198 int data = obj->uart->DATA;
mbed_official 580:3c14cb9b87c5 199 return data;
mbed_official 580:3c14cb9b87c5 200 }
mbed_official 580:3c14cb9b87c5 201
mbed_official 580:3c14cb9b87c5 202 void serial_putc(serial_t *obj, int c) {
mbed_official 580:3c14cb9b87c5 203 while (serial_writable(obj));
mbed_official 580:3c14cb9b87c5 204 obj->uart->DATA = c;
mbed_official 580:3c14cb9b87c5 205 }
mbed_official 580:3c14cb9b87c5 206
mbed_official 580:3c14cb9b87c5 207 int serial_readable(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 208 return obj->uart->STATE & 2;
mbed_official 580:3c14cb9b87c5 209 }
mbed_official 580:3c14cb9b87c5 210
mbed_official 580:3c14cb9b87c5 211 int serial_writable(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 212 return obj->uart->STATE & 1;
mbed_official 580:3c14cb9b87c5 213 }
mbed_official 580:3c14cb9b87c5 214
mbed_official 580:3c14cb9b87c5 215 void serial_clear(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 216 obj->uart->DATA = 0x00;
mbed_official 580:3c14cb9b87c5 217 }
mbed_official 580:3c14cb9b87c5 218
mbed_official 580:3c14cb9b87c5 219 void serial_pinout_tx(PinName tx) {
mbed_official 580:3c14cb9b87c5 220 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 580:3c14cb9b87c5 221 }
mbed_official 580:3c14cb9b87c5 222
mbed_official 580:3c14cb9b87c5 223 void serial_break_set(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 224 }
mbed_official 580:3c14cb9b87c5 225
mbed_official 580:3c14cb9b87c5 226 void serial_break_clear(serial_t *obj) {
mbed_official 580:3c14cb9b87c5 227 }
mbed_official 580:3c14cb9b87c5 228 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) {
mbed_official 580:3c14cb9b87c5 229 }
mbed_official 580:3c14cb9b87c5 230