1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "SerialBase.h"
23 SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) {
24 serial_init(&_serial, tx, rx);
25 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
28 void SerialBase::baud(int baudrate) {
29 serial_baud(&_serial, baudrate);
33 void SerialBase::format(int bits, Parity parity, int stop_bits) {
34 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
37 int SerialBase::readable() {
38 return serial_readable(&_serial);
42 int SerialBase::writeable() {
43 return serial_writable(&_serial);
46 void SerialBase::attach(void (*fptr)(void), IrqType type) {
48 _irq[type].attach(fptr);
49 serial_irq_set(&_serial, (SerialIrq)type, 1);
51 serial_irq_set(&_serial, (SerialIrq)type, 0);
55 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
56 SerialBase *handler = (SerialBase*)id;
57 handler->_irq[irq_type].call();
60 int SerialBase::_base_getc() {
61 return serial_getc(&_serial);
64 int SerialBase::_base_putc(int c) {
65 serial_putc(&_serial, c);
69 void SerialBase::send_break() {
70 // Wait for 1.5 frames before clearing the break condition
71 // This will have different effects on our platforms, but should
72 // ensure that we keep the break active for at least one frame.
73 // We consider a full frame (1 start bit + 8 data bits bits +
74 // 1 parity bit + 2 stop bits = 12 bits) for computation.
75 // One bit time (in us) = 1000000/_baud
76 // Twelve bits: 12000000/baud delay
77 // 1.5 frames: 18000000/baud delay
78 serial_break_set(&_serial);
79 wait_us(18000000/_baud);
80 serial_break_clear(&_serial);
84 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
85 FlowControl flow_type = (FlowControl)type;
88 serial_set_flow_control(&_serial, flow_type, flow1, NC);
92 serial_set_flow_control(&_serial, flow_type, NC, flow1);
97 serial_set_flow_control(&_serial, flow_type, flow1, flow2);