mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Oct 23 14:15:04 2013 +0100
Revision:
36:ab3ee77451e7
Child:
64:7b352733b00a
Synchronized with git revision 2404dc0092fa583d899df3d9021a4ddb4510b011

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 36:ab3ee77451e7 1 /* mbed Microcontroller Library
mbed_official 36:ab3ee77451e7 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 36:ab3ee77451e7 3 *
mbed_official 36:ab3ee77451e7 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 36:ab3ee77451e7 5 * you may not use this file except in compliance with the License.
mbed_official 36:ab3ee77451e7 6 * You may obtain a copy of the License at
mbed_official 36:ab3ee77451e7 7 *
mbed_official 36:ab3ee77451e7 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 36:ab3ee77451e7 9 *
mbed_official 36:ab3ee77451e7 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 36:ab3ee77451e7 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 36:ab3ee77451e7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 36:ab3ee77451e7 13 * See the License for the specific language governing permissions and
mbed_official 36:ab3ee77451e7 14 * limitations under the License.
mbed_official 36:ab3ee77451e7 15 */
mbed_official 36:ab3ee77451e7 16 #ifndef MBED_SERIALBASE_H
mbed_official 36:ab3ee77451e7 17 #define MBED_SERIALBASE_H
mbed_official 36:ab3ee77451e7 18
mbed_official 36:ab3ee77451e7 19 #include "platform.h"
mbed_official 36:ab3ee77451e7 20
mbed_official 36:ab3ee77451e7 21 #if DEVICE_SERIAL
mbed_official 36:ab3ee77451e7 22
mbed_official 36:ab3ee77451e7 23 #include "Stream.h"
mbed_official 36:ab3ee77451e7 24 #include "FunctionPointer.h"
mbed_official 36:ab3ee77451e7 25 #include "serial_api.h"
mbed_official 36:ab3ee77451e7 26
mbed_official 36:ab3ee77451e7 27 namespace mbed {
mbed_official 36:ab3ee77451e7 28
mbed_official 36:ab3ee77451e7 29 /** A base class for serial port implementations
mbed_official 36:ab3ee77451e7 30 * Can't be instantiated directly (use Serial or RawSerial)
mbed_official 36:ab3ee77451e7 31 */
mbed_official 36:ab3ee77451e7 32 class SerialBase {
mbed_official 36:ab3ee77451e7 33
mbed_official 36:ab3ee77451e7 34 public:
mbed_official 36:ab3ee77451e7 35 /** Set the baud rate of the serial port
mbed_official 36:ab3ee77451e7 36 *
mbed_official 36:ab3ee77451e7 37 * @param baudrate The baudrate of the serial port (default = 9600).
mbed_official 36:ab3ee77451e7 38 */
mbed_official 36:ab3ee77451e7 39 void baud(int baudrate);
mbed_official 36:ab3ee77451e7 40
mbed_official 36:ab3ee77451e7 41 enum Parity {
mbed_official 36:ab3ee77451e7 42 None = 0,
mbed_official 36:ab3ee77451e7 43 Odd,
mbed_official 36:ab3ee77451e7 44 Even,
mbed_official 36:ab3ee77451e7 45 Forced1,
mbed_official 36:ab3ee77451e7 46 Forced0
mbed_official 36:ab3ee77451e7 47 };
mbed_official 36:ab3ee77451e7 48
mbed_official 36:ab3ee77451e7 49 enum IrqType {
mbed_official 36:ab3ee77451e7 50 RxIrq = 0,
mbed_official 36:ab3ee77451e7 51 TxIrq
mbed_official 36:ab3ee77451e7 52 };
mbed_official 36:ab3ee77451e7 53
mbed_official 36:ab3ee77451e7 54 /** Set the transmission format used by the serial port
mbed_official 36:ab3ee77451e7 55 *
mbed_official 36:ab3ee77451e7 56 * @param bits The number of bits in a word (5-8; default = 8)
mbed_official 36:ab3ee77451e7 57 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
mbed_official 36:ab3ee77451e7 58 * @param stop The number of stop bits (1 or 2; default = 1)
mbed_official 36:ab3ee77451e7 59 */
mbed_official 36:ab3ee77451e7 60 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
mbed_official 36:ab3ee77451e7 61
mbed_official 36:ab3ee77451e7 62 /** Determine if there is a character available to read
mbed_official 36:ab3ee77451e7 63 *
mbed_official 36:ab3ee77451e7 64 * @returns
mbed_official 36:ab3ee77451e7 65 * 1 if there is a character available to read,
mbed_official 36:ab3ee77451e7 66 * 0 otherwise
mbed_official 36:ab3ee77451e7 67 */
mbed_official 36:ab3ee77451e7 68 int readable();
mbed_official 36:ab3ee77451e7 69
mbed_official 36:ab3ee77451e7 70 /** Determine if there is space available to write a character
mbed_official 36:ab3ee77451e7 71 *
mbed_official 36:ab3ee77451e7 72 * @returns
mbed_official 36:ab3ee77451e7 73 * 1 if there is space to write a character,
mbed_official 36:ab3ee77451e7 74 * 0 otherwise
mbed_official 36:ab3ee77451e7 75 */
mbed_official 36:ab3ee77451e7 76 int writeable();
mbed_official 36:ab3ee77451e7 77
mbed_official 36:ab3ee77451e7 78 /** Attach a function to call whenever a serial interrupt is generated
mbed_official 36:ab3ee77451e7 79 *
mbed_official 36:ab3ee77451e7 80 * @param fptr A pointer to a void function, or 0 to set as none
mbed_official 36:ab3ee77451e7 81 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
mbed_official 36:ab3ee77451e7 82 */
mbed_official 36:ab3ee77451e7 83 void attach(void (*fptr)(void), IrqType type=RxIrq);
mbed_official 36:ab3ee77451e7 84
mbed_official 36:ab3ee77451e7 85 /** Attach a member function to call whenever a serial interrupt is generated
mbed_official 36:ab3ee77451e7 86 *
mbed_official 36:ab3ee77451e7 87 * @param tptr pointer to the object to call the member function on
mbed_official 36:ab3ee77451e7 88 * @param mptr pointer to the member function to be called
mbed_official 36:ab3ee77451e7 89 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
mbed_official 36:ab3ee77451e7 90 */
mbed_official 36:ab3ee77451e7 91 template<typename T>
mbed_official 36:ab3ee77451e7 92 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
mbed_official 36:ab3ee77451e7 93 if((mptr != NULL) && (tptr != NULL)) {
mbed_official 36:ab3ee77451e7 94 _irq[type].attach(tptr, mptr);
mbed_official 36:ab3ee77451e7 95 serial_irq_set(&_serial, (SerialIrq)type, 1);
mbed_official 36:ab3ee77451e7 96 }
mbed_official 36:ab3ee77451e7 97 }
mbed_official 36:ab3ee77451e7 98
mbed_official 36:ab3ee77451e7 99 /** Generate a break condition on the serial line
mbed_official 36:ab3ee77451e7 100 */
mbed_official 36:ab3ee77451e7 101 void send_break();
mbed_official 36:ab3ee77451e7 102
mbed_official 36:ab3ee77451e7 103 static void _irq_handler(uint32_t id, SerialIrq irq_type);
mbed_official 36:ab3ee77451e7 104
mbed_official 36:ab3ee77451e7 105 protected:
mbed_official 36:ab3ee77451e7 106 SerialBase(PinName tx, PinName rx);
mbed_official 36:ab3ee77451e7 107
mbed_official 36:ab3ee77451e7 108 int _base_getc();
mbed_official 36:ab3ee77451e7 109 int _base_putc(int c);
mbed_official 36:ab3ee77451e7 110
mbed_official 36:ab3ee77451e7 111 serial_t _serial;
mbed_official 36:ab3ee77451e7 112 FunctionPointer _irq[2];
mbed_official 36:ab3ee77451e7 113 int _baud;
mbed_official 36:ab3ee77451e7 114 };
mbed_official 36:ab3ee77451e7 115
mbed_official 36:ab3ee77451e7 116 } // namespace mbed
mbed_official 36:ab3ee77451e7 117
mbed_official 36:ab3ee77451e7 118 #endif
mbed_official 36:ab3ee77451e7 119
mbed_official 36:ab3ee77451e7 120 #endif