Modified version of the mbed library for use with the Nucleo boards.
Dependents: EEPROMWrite Full-Project
Fork of mbed-src by
common/Serial.cpp@13:0645d8841f51, 2013-08-05 (annotated)
- Committer:
- bogdanm
- Date:
- Mon Aug 05 14:12:34 2013 +0300
- Revision:
- 13:0645d8841f51
- Parent:
- 10:3bc89ef62ce7
- Child:
- 15:4892fe388435
Update mbed sources to revision 64
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #include "Serial.h" |
bogdanm | 13:0645d8841f51 | 17 | #include "wait_api.h" |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #if DEVICE_SERIAL |
bogdanm | 13:0645d8841f51 | 20 | |
bogdanm | 13:0645d8841f51 | 21 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 22 | |
bogdanm | 13:0645d8841f51 | 23 | Serial::Serial(PinName tx, PinName rx, const char *name) : Stream(name) { |
bogdanm | 13:0645d8841f51 | 24 | serial_init(&_serial, tx, rx); |
bogdanm | 13:0645d8841f51 | 25 | _baud = 9600; |
bogdanm | 13:0645d8841f51 | 26 | serial_irq_handler(&_serial, Serial::_irq_handler, (uint32_t)this); |
bogdanm | 13:0645d8841f51 | 27 | } |
bogdanm | 13:0645d8841f51 | 28 | |
bogdanm | 13:0645d8841f51 | 29 | void Serial::baud(int baudrate) { |
bogdanm | 13:0645d8841f51 | 30 | serial_baud(&_serial, baudrate); |
bogdanm | 13:0645d8841f51 | 31 | _baud = baudrate; |
bogdanm | 13:0645d8841f51 | 32 | } |
bogdanm | 13:0645d8841f51 | 33 | |
bogdanm | 13:0645d8841f51 | 34 | void Serial::format(int bits, Parity parity, int stop_bits) { |
bogdanm | 13:0645d8841f51 | 35 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
bogdanm | 13:0645d8841f51 | 36 | } |
bogdanm | 13:0645d8841f51 | 37 | |
bogdanm | 13:0645d8841f51 | 38 | int Serial::readable() { |
bogdanm | 13:0645d8841f51 | 39 | return serial_readable(&_serial); |
bogdanm | 13:0645d8841f51 | 40 | } |
bogdanm | 13:0645d8841f51 | 41 | |
bogdanm | 13:0645d8841f51 | 42 | |
bogdanm | 13:0645d8841f51 | 43 | int Serial::writeable() { |
bogdanm | 13:0645d8841f51 | 44 | return serial_writable(&_serial); |
bogdanm | 13:0645d8841f51 | 45 | } |
bogdanm | 13:0645d8841f51 | 46 | |
bogdanm | 13:0645d8841f51 | 47 | void Serial::attach(void (*fptr)(void), IrqType type) { |
bogdanm | 13:0645d8841f51 | 48 | if (fptr) { |
bogdanm | 13:0645d8841f51 | 49 | _irq[type].attach(fptr); |
bogdanm | 13:0645d8841f51 | 50 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
bogdanm | 13:0645d8841f51 | 51 | } else { |
bogdanm | 13:0645d8841f51 | 52 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
bogdanm | 13:0645d8841f51 | 53 | } |
bogdanm | 13:0645d8841f51 | 54 | } |
bogdanm | 13:0645d8841f51 | 55 | |
bogdanm | 13:0645d8841f51 | 56 | |
bogdanm | 13:0645d8841f51 | 57 | void Serial::_irq_handler(uint32_t id, SerialIrq irq_type) { |
bogdanm | 13:0645d8841f51 | 58 | Serial *handler = (Serial*)id; |
bogdanm | 13:0645d8841f51 | 59 | handler->_irq[irq_type].call(); |
bogdanm | 13:0645d8841f51 | 60 | } |
bogdanm | 13:0645d8841f51 | 61 | |
bogdanm | 13:0645d8841f51 | 62 | int Serial::_getc() { |
bogdanm | 13:0645d8841f51 | 63 | return serial_getc(&_serial); |
bogdanm | 13:0645d8841f51 | 64 | } |
bogdanm | 13:0645d8841f51 | 65 | |
bogdanm | 13:0645d8841f51 | 66 | int Serial::_putc(int c) { |
bogdanm | 13:0645d8841f51 | 67 | serial_putc(&_serial, c); |
bogdanm | 13:0645d8841f51 | 68 | return c; |
bogdanm | 13:0645d8841f51 | 69 | } |
bogdanm | 13:0645d8841f51 | 70 | |
bogdanm | 13:0645d8841f51 | 71 | void Serial::send_break() { |
bogdanm | 13:0645d8841f51 | 72 | // Wait for 1.5 frames before clearing the break condition |
bogdanm | 13:0645d8841f51 | 73 | // This will have different effects on our platforms, but should |
bogdanm | 13:0645d8841f51 | 74 | // ensure that we keep the break active for at least one frame. |
bogdanm | 13:0645d8841f51 | 75 | // We consider a full frame (1 start bit + 8 data bits bits + |
bogdanm | 13:0645d8841f51 | 76 | // 1 parity bit + 2 stop bits = 12 bits) for computation. |
bogdanm | 13:0645d8841f51 | 77 | // One bit time (in us) = 1000000/_baud |
bogdanm | 13:0645d8841f51 | 78 | // Twelve bits: 12000000/baud delay |
bogdanm | 13:0645d8841f51 | 79 | // 1.5 frames: 18000000/baud delay |
bogdanm | 13:0645d8841f51 | 80 | serial_break_set(&_serial); |
bogdanm | 13:0645d8841f51 | 81 | wait_us(18000000/_baud); |
bogdanm | 13:0645d8841f51 | 82 | serial_break_clear(&_serial); |
bogdanm | 13:0645d8841f51 | 83 | } |
bogdanm | 13:0645d8841f51 | 84 | |
bogdanm | 13:0645d8841f51 | 85 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 86 | |
bogdanm | 13:0645d8841f51 | 87 | #endif |