mbed.h library with any bug fixes AV finds.

Dependents:   micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more

Committer:
aravindsv
Date:
Mon Nov 02 02:26:59 2015 +0000
Revision:
0:ba7650f404af
Reduced HSE_STARTUP_TIMEOUT to 500 ms, fixed some compiler warnings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aravindsv 0:ba7650f404af 1 /* mbed Microcontroller Library
aravindsv 0:ba7650f404af 2 * Copyright (c) 2006-2013 ARM Limited
aravindsv 0:ba7650f404af 3 *
aravindsv 0:ba7650f404af 4 * Licensed under the Apache License, Version 2.0 (the "License");
aravindsv 0:ba7650f404af 5 * you may not use this file except in compliance with the License.
aravindsv 0:ba7650f404af 6 * You may obtain a copy of the License at
aravindsv 0:ba7650f404af 7 *
aravindsv 0:ba7650f404af 8 * http://www.apache.org/licenses/LICENSE-2.0
aravindsv 0:ba7650f404af 9 *
aravindsv 0:ba7650f404af 10 * Unless required by applicable law or agreed to in writing, software
aravindsv 0:ba7650f404af 11 * distributed under the License is distributed on an "AS IS" BASIS,
aravindsv 0:ba7650f404af 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
aravindsv 0:ba7650f404af 13 * See the License for the specific language governing permissions and
aravindsv 0:ba7650f404af 14 * limitations under the License.
aravindsv 0:ba7650f404af 15 */
aravindsv 0:ba7650f404af 16 #include "SerialBase.h"
aravindsv 0:ba7650f404af 17 #include "wait_api.h"
aravindsv 0:ba7650f404af 18
aravindsv 0:ba7650f404af 19 #if DEVICE_SERIAL
aravindsv 0:ba7650f404af 20
aravindsv 0:ba7650f404af 21 namespace mbed {
aravindsv 0:ba7650f404af 22
aravindsv 0:ba7650f404af 23 SerialBase::SerialBase(PinName tx, PinName rx) :
aravindsv 0:ba7650f404af 24 #if DEVICE_SERIAL_ASYNCH
aravindsv 0:ba7650f404af 25 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
aravindsv 0:ba7650f404af 26 _rx_usage(DMA_USAGE_NEVER),
aravindsv 0:ba7650f404af 27 #endif
aravindsv 0:ba7650f404af 28 _serial(), _baud(9600) {
aravindsv 0:ba7650f404af 29 serial_init(&_serial, tx, rx);
aravindsv 0:ba7650f404af 30 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
aravindsv 0:ba7650f404af 31 }
aravindsv 0:ba7650f404af 32
aravindsv 0:ba7650f404af 33 void SerialBase::baud(int baudrate) {
aravindsv 0:ba7650f404af 34 serial_baud(&_serial, baudrate);
aravindsv 0:ba7650f404af 35 _baud = baudrate;
aravindsv 0:ba7650f404af 36 }
aravindsv 0:ba7650f404af 37
aravindsv 0:ba7650f404af 38 void SerialBase::format(int bits, Parity parity, int stop_bits) {
aravindsv 0:ba7650f404af 39 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
aravindsv 0:ba7650f404af 40 }
aravindsv 0:ba7650f404af 41
aravindsv 0:ba7650f404af 42 int SerialBase::readable() {
aravindsv 0:ba7650f404af 43 return serial_readable(&_serial);
aravindsv 0:ba7650f404af 44 }
aravindsv 0:ba7650f404af 45
aravindsv 0:ba7650f404af 46
aravindsv 0:ba7650f404af 47 int SerialBase::writeable() {
aravindsv 0:ba7650f404af 48 return serial_writable(&_serial);
aravindsv 0:ba7650f404af 49 }
aravindsv 0:ba7650f404af 50
aravindsv 0:ba7650f404af 51 void SerialBase::attach(void (*fptr)(void), IrqType type) {
aravindsv 0:ba7650f404af 52 if (fptr) {
aravindsv 0:ba7650f404af 53 _irq[type].attach(fptr);
aravindsv 0:ba7650f404af 54 serial_irq_set(&_serial, (SerialIrq)type, 1);
aravindsv 0:ba7650f404af 55 } else {
aravindsv 0:ba7650f404af 56 serial_irq_set(&_serial, (SerialIrq)type, 0);
aravindsv 0:ba7650f404af 57 }
aravindsv 0:ba7650f404af 58 }
aravindsv 0:ba7650f404af 59
aravindsv 0:ba7650f404af 60 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
aravindsv 0:ba7650f404af 61 SerialBase *handler = (SerialBase*)id;
aravindsv 0:ba7650f404af 62 handler->_irq[irq_type].call();
aravindsv 0:ba7650f404af 63 }
aravindsv 0:ba7650f404af 64
aravindsv 0:ba7650f404af 65 int SerialBase::_base_getc() {
aravindsv 0:ba7650f404af 66 return serial_getc(&_serial);
aravindsv 0:ba7650f404af 67 }
aravindsv 0:ba7650f404af 68
aravindsv 0:ba7650f404af 69 int SerialBase::_base_putc(int c) {
aravindsv 0:ba7650f404af 70 serial_putc(&_serial, c);
aravindsv 0:ba7650f404af 71 return c;
aravindsv 0:ba7650f404af 72 }
aravindsv 0:ba7650f404af 73
aravindsv 0:ba7650f404af 74 void SerialBase::send_break() {
aravindsv 0:ba7650f404af 75 // Wait for 1.5 frames before clearing the break condition
aravindsv 0:ba7650f404af 76 // This will have different effects on our platforms, but should
aravindsv 0:ba7650f404af 77 // ensure that we keep the break active for at least one frame.
aravindsv 0:ba7650f404af 78 // We consider a full frame (1 start bit + 8 data bits bits +
aravindsv 0:ba7650f404af 79 // 1 parity bit + 2 stop bits = 12 bits) for computation.
aravindsv 0:ba7650f404af 80 // One bit time (in us) = 1000000/_baud
aravindsv 0:ba7650f404af 81 // Twelve bits: 12000000/baud delay
aravindsv 0:ba7650f404af 82 // 1.5 frames: 18000000/baud delay
aravindsv 0:ba7650f404af 83 serial_break_set(&_serial);
aravindsv 0:ba7650f404af 84 wait_us(18000000/_baud);
aravindsv 0:ba7650f404af 85 serial_break_clear(&_serial);
aravindsv 0:ba7650f404af 86 }
aravindsv 0:ba7650f404af 87
aravindsv 0:ba7650f404af 88 #if DEVICE_SERIAL_FC
aravindsv 0:ba7650f404af 89 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
aravindsv 0:ba7650f404af 90 FlowControl flow_type = (FlowControl)type;
aravindsv 0:ba7650f404af 91 switch(type) {
aravindsv 0:ba7650f404af 92 case RTS:
aravindsv 0:ba7650f404af 93 serial_set_flow_control(&_serial, flow_type, flow1, NC);
aravindsv 0:ba7650f404af 94 break;
aravindsv 0:ba7650f404af 95
aravindsv 0:ba7650f404af 96 case CTS:
aravindsv 0:ba7650f404af 97 serial_set_flow_control(&_serial, flow_type, NC, flow1);
aravindsv 0:ba7650f404af 98 break;
aravindsv 0:ba7650f404af 99
aravindsv 0:ba7650f404af 100 case RTSCTS:
aravindsv 0:ba7650f404af 101 case Disabled:
aravindsv 0:ba7650f404af 102 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
aravindsv 0:ba7650f404af 103 break;
aravindsv 0:ba7650f404af 104
aravindsv 0:ba7650f404af 105 default:
aravindsv 0:ba7650f404af 106 break;
aravindsv 0:ba7650f404af 107 }
aravindsv 0:ba7650f404af 108 }
aravindsv 0:ba7650f404af 109 #endif
aravindsv 0:ba7650f404af 110
aravindsv 0:ba7650f404af 111 #if DEVICE_SERIAL_ASYNCH
aravindsv 0:ba7650f404af 112
aravindsv 0:ba7650f404af 113 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event)
aravindsv 0:ba7650f404af 114 {
aravindsv 0:ba7650f404af 115 if (serial_tx_active(&_serial)) {
aravindsv 0:ba7650f404af 116 return -1; // transaction ongoing
aravindsv 0:ba7650f404af 117 }
aravindsv 0:ba7650f404af 118 start_write((void *)buffer, length, 8, callback, event);
aravindsv 0:ba7650f404af 119 return 0;
aravindsv 0:ba7650f404af 120 }
aravindsv 0:ba7650f404af 121
aravindsv 0:ba7650f404af 122 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event)
aravindsv 0:ba7650f404af 123 {
aravindsv 0:ba7650f404af 124 if (serial_tx_active(&_serial)) {
aravindsv 0:ba7650f404af 125 return -1; // transaction ongoing
aravindsv 0:ba7650f404af 126 }
aravindsv 0:ba7650f404af 127 start_write((void *)buffer, length, 16, callback, event);
aravindsv 0:ba7650f404af 128 return 0;
aravindsv 0:ba7650f404af 129 }
aravindsv 0:ba7650f404af 130
aravindsv 0:ba7650f404af 131 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event)
aravindsv 0:ba7650f404af 132 {
aravindsv 0:ba7650f404af 133 _tx_callback = callback;
aravindsv 0:ba7650f404af 134
aravindsv 0:ba7650f404af 135 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
aravindsv 0:ba7650f404af 136 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
aravindsv 0:ba7650f404af 137 }
aravindsv 0:ba7650f404af 138
aravindsv 0:ba7650f404af 139 void SerialBase::abort_write(void)
aravindsv 0:ba7650f404af 140 {
aravindsv 0:ba7650f404af 141 serial_tx_abort_asynch(&_serial);
aravindsv 0:ba7650f404af 142 }
aravindsv 0:ba7650f404af 143
aravindsv 0:ba7650f404af 144 void SerialBase::abort_read(void)
aravindsv 0:ba7650f404af 145 {
aravindsv 0:ba7650f404af 146 serial_rx_abort_asynch(&_serial);
aravindsv 0:ba7650f404af 147 }
aravindsv 0:ba7650f404af 148
aravindsv 0:ba7650f404af 149 int SerialBase::set_dma_usage_tx(DMAUsage usage)
aravindsv 0:ba7650f404af 150 {
aravindsv 0:ba7650f404af 151 if (serial_tx_active(&_serial)) {
aravindsv 0:ba7650f404af 152 return -1;
aravindsv 0:ba7650f404af 153 }
aravindsv 0:ba7650f404af 154 _tx_usage = usage;
aravindsv 0:ba7650f404af 155 return 0;
aravindsv 0:ba7650f404af 156 }
aravindsv 0:ba7650f404af 157
aravindsv 0:ba7650f404af 158 int SerialBase::set_dma_usage_rx(DMAUsage usage)
aravindsv 0:ba7650f404af 159 {
aravindsv 0:ba7650f404af 160 if (serial_tx_active(&_serial)) {
aravindsv 0:ba7650f404af 161 return -1;
aravindsv 0:ba7650f404af 162 }
aravindsv 0:ba7650f404af 163 _rx_usage = usage;
aravindsv 0:ba7650f404af 164 return 0;
aravindsv 0:ba7650f404af 165 }
aravindsv 0:ba7650f404af 166
aravindsv 0:ba7650f404af 167 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
aravindsv 0:ba7650f404af 168 {
aravindsv 0:ba7650f404af 169 if (serial_rx_active(&_serial)) {
aravindsv 0:ba7650f404af 170 return -1; // transaction ongoing
aravindsv 0:ba7650f404af 171 }
aravindsv 0:ba7650f404af 172 start_read((void*)buffer, length, 8, callback, event, char_match);
aravindsv 0:ba7650f404af 173 return 0;
aravindsv 0:ba7650f404af 174 }
aravindsv 0:ba7650f404af 175
aravindsv 0:ba7650f404af 176
aravindsv 0:ba7650f404af 177 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
aravindsv 0:ba7650f404af 178 {
aravindsv 0:ba7650f404af 179 if (serial_rx_active(&_serial)) {
aravindsv 0:ba7650f404af 180 return -1; // transaction ongoing
aravindsv 0:ba7650f404af 181 }
aravindsv 0:ba7650f404af 182 start_read((void*)buffer, length, 16, callback, event, char_match);
aravindsv 0:ba7650f404af 183 return 0;
aravindsv 0:ba7650f404af 184 }
aravindsv 0:ba7650f404af 185
aravindsv 0:ba7650f404af 186
aravindsv 0:ba7650f404af 187 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match)
aravindsv 0:ba7650f404af 188 {
aravindsv 0:ba7650f404af 189 _rx_callback = callback;
aravindsv 0:ba7650f404af 190 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
aravindsv 0:ba7650f404af 191 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
aravindsv 0:ba7650f404af 192 }
aravindsv 0:ba7650f404af 193
aravindsv 0:ba7650f404af 194 void SerialBase::interrupt_handler_asynch(void)
aravindsv 0:ba7650f404af 195 {
aravindsv 0:ba7650f404af 196 int event = serial_irq_handler_asynch(&_serial);
aravindsv 0:ba7650f404af 197 int rx_event = event & SERIAL_EVENT_RX_MASK;
aravindsv 0:ba7650f404af 198 if (_rx_callback && rx_event) {
aravindsv 0:ba7650f404af 199 _rx_callback.call(rx_event);
aravindsv 0:ba7650f404af 200 }
aravindsv 0:ba7650f404af 201
aravindsv 0:ba7650f404af 202 int tx_event = event & SERIAL_EVENT_TX_MASK;
aravindsv 0:ba7650f404af 203 if (_tx_callback && tx_event) {
aravindsv 0:ba7650f404af 204 _tx_callback.call(tx_event);
aravindsv 0:ba7650f404af 205 }
aravindsv 0:ba7650f404af 206 }
aravindsv 0:ba7650f404af 207
aravindsv 0:ba7650f404af 208 #endif
aravindsv 0:ba7650f404af 209
aravindsv 0:ba7650f404af 210 } // namespace mbed
aravindsv 0:ba7650f404af 211
aravindsv 0:ba7650f404af 212 #endif