initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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