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