mbed library sources
common/SerialBase.cpp@0:0a673c671a56, 2016-07-27 (annotated)
- Committer:
- ebrus
- Date:
- Wed Jul 27 18:35:32 2016 +0000
- Revision:
- 0:0a673c671a56
4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ebrus | 0:0a673c671a56 | 1 | /* mbed Microcontroller Library |
ebrus | 0:0a673c671a56 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ebrus | 0:0a673c671a56 | 3 | * |
ebrus | 0:0a673c671a56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ebrus | 0:0a673c671a56 | 5 | * you may not use this file except in compliance with the License. |
ebrus | 0:0a673c671a56 | 6 | * You may obtain a copy of the License at |
ebrus | 0:0a673c671a56 | 7 | * |
ebrus | 0:0a673c671a56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ebrus | 0:0a673c671a56 | 9 | * |
ebrus | 0:0a673c671a56 | 10 | * Unless required by applicable law or agreed to in writing, software |
ebrus | 0:0a673c671a56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ebrus | 0:0a673c671a56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ebrus | 0:0a673c671a56 | 13 | * See the License for the specific language governing permissions and |
ebrus | 0:0a673c671a56 | 14 | * limitations under the License. |
ebrus | 0:0a673c671a56 | 15 | */ |
ebrus | 0:0a673c671a56 | 16 | #include "SerialBase.h" |
ebrus | 0:0a673c671a56 | 17 | #include "wait_api.h" |
ebrus | 0:0a673c671a56 | 18 | |
ebrus | 0:0a673c671a56 | 19 | #if DEVICE_SERIAL |
ebrus | 0:0a673c671a56 | 20 | |
ebrus | 0:0a673c671a56 | 21 | namespace mbed { |
ebrus | 0:0a673c671a56 | 22 | |
ebrus | 0:0a673c671a56 | 23 | SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) { |
ebrus | 0:0a673c671a56 | 24 | serial_init(&_serial, tx, rx); |
ebrus | 0:0a673c671a56 | 25 | serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); |
ebrus | 0:0a673c671a56 | 26 | } |
ebrus | 0:0a673c671a56 | 27 | |
ebrus | 0:0a673c671a56 | 28 | void SerialBase::baud(int baudrate) { |
ebrus | 0:0a673c671a56 | 29 | serial_baud(&_serial, baudrate); |
ebrus | 0:0a673c671a56 | 30 | _baud = baudrate; |
ebrus | 0:0a673c671a56 | 31 | } |
ebrus | 0:0a673c671a56 | 32 | |
ebrus | 0:0a673c671a56 | 33 | void SerialBase::format(int bits, Parity parity, int stop_bits) { |
ebrus | 0:0a673c671a56 | 34 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
ebrus | 0:0a673c671a56 | 35 | } |
ebrus | 0:0a673c671a56 | 36 | |
ebrus | 0:0a673c671a56 | 37 | int SerialBase::readable() { |
ebrus | 0:0a673c671a56 | 38 | return serial_readable(&_serial); |
ebrus | 0:0a673c671a56 | 39 | } |
ebrus | 0:0a673c671a56 | 40 | |
ebrus | 0:0a673c671a56 | 41 | |
ebrus | 0:0a673c671a56 | 42 | int SerialBase::writeable() { |
ebrus | 0:0a673c671a56 | 43 | return serial_writable(&_serial); |
ebrus | 0:0a673c671a56 | 44 | } |
ebrus | 0:0a673c671a56 | 45 | |
ebrus | 0:0a673c671a56 | 46 | void SerialBase::attach(void (*fptr)(void), IrqType type) { |
ebrus | 0:0a673c671a56 | 47 | if (fptr) { |
ebrus | 0:0a673c671a56 | 48 | _irq[type].attach(fptr); |
ebrus | 0:0a673c671a56 | 49 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
ebrus | 0:0a673c671a56 | 50 | } else { |
ebrus | 0:0a673c671a56 | 51 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
ebrus | 0:0a673c671a56 | 52 | } |
ebrus | 0:0a673c671a56 | 53 | } |
ebrus | 0:0a673c671a56 | 54 | |
ebrus | 0:0a673c671a56 | 55 | void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { |
ebrus | 0:0a673c671a56 | 56 | SerialBase *handler = (SerialBase*)id; |
ebrus | 0:0a673c671a56 | 57 | handler->_irq[irq_type].call(); |
ebrus | 0:0a673c671a56 | 58 | } |
ebrus | 0:0a673c671a56 | 59 | |
ebrus | 0:0a673c671a56 | 60 | int SerialBase::_base_getc() { |
ebrus | 0:0a673c671a56 | 61 | return serial_getc(&_serial); |
ebrus | 0:0a673c671a56 | 62 | } |
ebrus | 0:0a673c671a56 | 63 | |
ebrus | 0:0a673c671a56 | 64 | int SerialBase::_base_putc(int c) { |
ebrus | 0:0a673c671a56 | 65 | serial_putc(&_serial, c); |
ebrus | 0:0a673c671a56 | 66 | return c; |
ebrus | 0:0a673c671a56 | 67 | } |
ebrus | 0:0a673c671a56 | 68 | |
ebrus | 0:0a673c671a56 | 69 | void SerialBase::send_break() { |
ebrus | 0:0a673c671a56 | 70 | // Wait for 1.5 frames before clearing the break condition |
ebrus | 0:0a673c671a56 | 71 | // This will have different effects on our platforms, but should |
ebrus | 0:0a673c671a56 | 72 | // ensure that we keep the break active for at least one frame. |
ebrus | 0:0a673c671a56 | 73 | // We consider a full frame (1 start bit + 8 data bits bits + |
ebrus | 0:0a673c671a56 | 74 | // 1 parity bit + 2 stop bits = 12 bits) for computation. |
ebrus | 0:0a673c671a56 | 75 | // One bit time (in us) = 1000000/_baud |
ebrus | 0:0a673c671a56 | 76 | // Twelve bits: 12000000/baud delay |
ebrus | 0:0a673c671a56 | 77 | // 1.5 frames: 18000000/baud delay |
ebrus | 0:0a673c671a56 | 78 | serial_break_set(&_serial); |
ebrus | 0:0a673c671a56 | 79 | wait_us(18000000/_baud); |
ebrus | 0:0a673c671a56 | 80 | serial_break_clear(&_serial); |
ebrus | 0:0a673c671a56 | 81 | } |
ebrus | 0:0a673c671a56 | 82 | |
ebrus | 0:0a673c671a56 | 83 | #if DEVICE_SERIAL_FC |
ebrus | 0:0a673c671a56 | 84 | void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { |
ebrus | 0:0a673c671a56 | 85 | FlowControl flow_type = (FlowControl)type; |
ebrus | 0:0a673c671a56 | 86 | switch(type) { |
ebrus | 0:0a673c671a56 | 87 | case RTS: |
ebrus | 0:0a673c671a56 | 88 | serial_set_flow_control(&_serial, flow_type, flow1, NC); |
ebrus | 0:0a673c671a56 | 89 | break; |
ebrus | 0:0a673c671a56 | 90 | |
ebrus | 0:0a673c671a56 | 91 | case CTS: |
ebrus | 0:0a673c671a56 | 92 | serial_set_flow_control(&_serial, flow_type, NC, flow1); |
ebrus | 0:0a673c671a56 | 93 | break; |
ebrus | 0:0a673c671a56 | 94 | |
ebrus | 0:0a673c671a56 | 95 | case RTSCTS: |
ebrus | 0:0a673c671a56 | 96 | case Disabled: |
ebrus | 0:0a673c671a56 | 97 | serial_set_flow_control(&_serial, flow_type, flow1, flow2); |
ebrus | 0:0a673c671a56 | 98 | break; |
ebrus | 0:0a673c671a56 | 99 | |
ebrus | 0:0a673c671a56 | 100 | default: |
ebrus | 0:0a673c671a56 | 101 | break; |
ebrus | 0:0a673c671a56 | 102 | } |
ebrus | 0:0a673c671a56 | 103 | } |
ebrus | 0:0a673c671a56 | 104 | #endif |
ebrus | 0:0a673c671a56 | 105 | |
ebrus | 0:0a673c671a56 | 106 | } // namespace mbed |
ebrus | 0:0a673c671a56 | 107 | |
ebrus | 0:0a673c671a56 | 108 | #endif |