mbed library sources: Modified to operate FRDM-KL25Z at 48MHz from internal 32kHz oscillator (nothing else changed).

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Serial.cpp Source File

Serial.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "Serial.h"
00017 #include "wait_api.h"
00018 
00019 #if DEVICE_SERIAL
00020 
00021 namespace mbed {
00022 
00023 Serial::Serial(PinName tx, PinName rx, const char *name) : Stream(name) {
00024     serial_init(&_serial, tx, rx);
00025     _baud = 9600;
00026     serial_irq_handler(&_serial, Serial::_irq_handler, (uint32_t)this);
00027 }
00028 
00029 void Serial::baud(int baudrate) {
00030     serial_baud(&_serial, baudrate);
00031     _baud = baudrate;
00032 }
00033 
00034 void Serial::format(int bits, Parity parity, int stop_bits) {
00035     serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
00036 }
00037 
00038 int Serial::readable() {
00039     return serial_readable(&_serial);
00040 }
00041 
00042 
00043 int Serial::writeable() {
00044     return serial_writable(&_serial);
00045 }
00046 
00047 pFunctionPointer_t Serial::attach(void (*fptr)(void), IrqType type) {
00048     pFunctionPointer_t pf = NULL;
00049     _irq[type].clear();
00050     if (fptr) {
00051         pf = _irq[type].add(fptr);
00052         serial_irq_set(&_serial, (SerialIrq)type, 1);
00053     } else {
00054         serial_irq_set(&_serial, (SerialIrq)type, 0);
00055     }
00056     return pf;
00057 }
00058 
00059 pFunctionPointer_t Serial::add_handler_helper(void (*fptr)(void), IrqType type, bool front) {
00060     if (NULL == fptr)
00061         return NULL;
00062     pFunctionPointer_t pf = front ? _irq[type].add_front(fptr) : _irq[type].add(fptr);
00063     serial_irq_set(&_serial, (SerialIrq)type, 1);
00064     return pf;
00065 }
00066 
00067 bool Serial::remove_handler(pFunctionPointer_t pf, IrqType type) {
00068     bool res = _irq[type].remove(pf);
00069     if (res && _irq[type].size() == 0)
00070         serial_irq_set(&_serial, (SerialIrq)type, 0);
00071     return res;
00072 }
00073 
00074 void Serial::_irq_handler(uint32_t id, SerialIrq irq_type) {
00075     Serial *handler = (Serial*)id;
00076     handler->_irq[irq_type].call();
00077 }
00078 
00079 int Serial::_getc() {
00080     return serial_getc(&_serial);
00081 }
00082 
00083 int Serial::_putc(int c) {
00084     serial_putc(&_serial, c);
00085     return c;
00086 }
00087 
00088 void Serial::send_break() {
00089   // Wait for 1.5 frames before clearing the break condition
00090   // This will have different effects on our platforms, but should
00091   // ensure that we keep the break active for at least one frame.
00092   // We consider a full frame (1 start bit + 8 data bits bits + 
00093   // 1 parity bit + 2 stop bits = 12 bits) for computation.
00094   // One bit time (in us) = 1000000/_baud
00095   // Twelve bits: 12000000/baud delay
00096   // 1.5 frames: 18000000/baud delay
00097   serial_break_set(&_serial);
00098   wait_us(18000000/_baud);
00099   serial_break_clear(&_serial);
00100 }
00101 
00102 } // namespace mbed
00103 
00104 #endif