]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/SerialBase.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / common / SerialBase.cpp
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 #include "SerialBase.h"
17 #include "wait_api.h"
18
19 #if DEVICE_SERIAL
20
21 namespace mbed {
22
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);
26 }
27
28 void SerialBase::baud(int baudrate) {
29 serial_baud(&_serial, baudrate);
30 _baud = baudrate;
31 }
32
33 void SerialBase::format(int bits, Parity parity, int stop_bits) {
34 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
35 }
36
37 int SerialBase::readable() {
38 return serial_readable(&_serial);
39 }
40
41
42 int SerialBase::writeable() {
43 return serial_writable(&_serial);
44 }
45
46 void SerialBase::attach(void (*fptr)(void), IrqType type) {
47 if (fptr) {
48 _irq[type].attach(fptr);
49 serial_irq_set(&_serial, (SerialIrq)type, 1);
50 } else {
51 serial_irq_set(&_serial, (SerialIrq)type, 0);
52 }
53 }
54
55 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
56 SerialBase *handler = (SerialBase*)id;
57 handler->_irq[irq_type].call();
58 }
59
60 int SerialBase::_base_getc() {
61 return serial_getc(&_serial);
62 }
63
64 int SerialBase::_base_putc(int c) {
65 serial_putc(&_serial, c);
66 return c;
67 }
68
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);
81 }
82
83 #if DEVICE_SERIAL_FC
84 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
85 FlowControl flow_type = (FlowControl)type;
86 switch(type) {
87 case RTS:
88 serial_set_flow_control(&_serial, flow_type, flow1, NC);
89 break;
90
91 case CTS:
92 serial_set_flow_control(&_serial, flow_type, NC, flow1);
93 break;
94
95 case RTSCTS:
96 case Disabled:
97 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
98 break;
99
100 default:
101 break;
102 }
103 }
104 #endif
105
106 } // namespace mbed
107
108 #endif
Imprint / Impressum