mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialBase.cpp Source File

SerialBase.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "SerialBase.h"
00017 #include "wait_api.h"
00018 
00019 #if DEVICE_SERIAL
00020 
00021 namespace mbed {
00022 
00023 SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) {
00024     serial_init(&_serial, tx, rx);
00025     serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
00026 }
00027 
00028 void SerialBase::baud(int baudrate) {
00029     serial_baud(&_serial, baudrate);
00030     _baud = baudrate;
00031 }
00032 
00033 void SerialBase::format(int bits, Parity parity, int stop_bits) {
00034     serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
00035 }
00036 
00037 int SerialBase::readable() {
00038     return serial_readable(&_serial);
00039 }
00040 
00041 
00042 int SerialBase::writeable() {
00043     return serial_writable(&_serial);
00044 }
00045 
00046 void SerialBase::attach(void (*fptr)(void), IrqType type) {
00047     if (fptr) {
00048         _irq[type].attach(fptr);
00049         serial_irq_set(&_serial, (SerialIrq)type, 1);
00050     } else {
00051         serial_irq_set(&_serial, (SerialIrq)type, 0);
00052     }
00053 }
00054 
00055 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
00056     SerialBase *handler = (SerialBase*)id;
00057     handler->_irq[irq_type].call();
00058 }
00059 
00060 int SerialBase::_base_getc() {
00061     return serial_getc(&_serial);
00062 }
00063 
00064 int SerialBase::_base_getc_irq() {
00065     return serial_getc_irq(&_serial);
00066 }
00067 
00068 int SerialBase::_base_putc(int c) {
00069     serial_putc(&_serial, c);
00070     return c;
00071 }
00072 
00073 void SerialBase::send_break() {
00074   // Wait for 1.5 frames before clearing the break condition
00075   // This will have different effects on our platforms, but should
00076   // ensure that we keep the break active for at least one frame.
00077   // We consider a full frame (1 start bit + 8 data bits bits +
00078   // 1 parity bit + 2 stop bits = 12 bits) for computation.
00079   // One bit time (in us) = 1000000/_baud
00080   // Twelve bits: 12000000/baud delay
00081   // 1.5 frames: 18000000/baud delay
00082   serial_break_set(&_serial);
00083   wait_us(18000000/_baud);
00084   serial_break_clear(&_serial);
00085 }
00086 
00087 #if DEVICE_SERIAL_FC
00088 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
00089     FlowControl flow_type = (FlowControl)type;
00090     switch(type) {
00091         case RTS:
00092             serial_set_flow_control(&_serial, flow_type, flow1, NC);
00093             break;
00094 
00095         case CTS:
00096             serial_set_flow_control(&_serial, flow_type, NC, flow1);
00097             break;
00098 
00099         case RTSCTS:
00100         case Disabled:
00101             serial_set_flow_control(&_serial, flow_type, flow1, flow2);
00102             break;
00103 
00104         default:
00105             break;
00106     }
00107 }
00108 #endif
00109 
00110 } // namespace mbed
00111 
00112 #endif