mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
api/SerialBase.h@213:cc557cb04877, 2014-05-26 (annotated)
- Committer:
- mbed_official
- Date:
- Mon May 26 18:30:07 2014 +0100
- Revision:
- 213:cc557cb04877
- Parent:
- 212:34d62c0b2af6
Synchronized with git revision 3cec47a2d5a7be8cf25757494272b60c1d8538d1
Full URL: https://github.com/mbedmicro/mbed/commit/3cec47a2d5a7be8cf25757494272b60c1d8538d1/
Who changed what in which revision?
User | Revision | Line number | New 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 | 64:7b352733b00a | 54 | enum Flow { |
mbed_official | 64:7b352733b00a | 55 | Disabled = 0, |
mbed_official | 64:7b352733b00a | 56 | RTS, |
mbed_official | 64:7b352733b00a | 57 | CTS, |
mbed_official | 64:7b352733b00a | 58 | RTSCTS |
mbed_official | 64:7b352733b00a | 59 | }; |
mbed_official | 64:7b352733b00a | 60 | |
mbed_official | 36:ab3ee77451e7 | 61 | /** Set the transmission format used by the serial port |
mbed_official | 36:ab3ee77451e7 | 62 | * |
mbed_official | 36:ab3ee77451e7 | 63 | * @param bits The number of bits in a word (5-8; default = 8) |
mbed_official | 36:ab3ee77451e7 | 64 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
mbed_official | 36:ab3ee77451e7 | 65 | * @param stop The number of stop bits (1 or 2; default = 1) |
mbed_official | 36:ab3ee77451e7 | 66 | */ |
mbed_official | 36:ab3ee77451e7 | 67 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
mbed_official | 36:ab3ee77451e7 | 68 | |
mbed_official | 36:ab3ee77451e7 | 69 | /** Determine if there is a character available to read |
mbed_official | 36:ab3ee77451e7 | 70 | * |
mbed_official | 36:ab3ee77451e7 | 71 | * @returns |
mbed_official | 36:ab3ee77451e7 | 72 | * 1 if there is a character available to read, |
mbed_official | 36:ab3ee77451e7 | 73 | * 0 otherwise |
mbed_official | 36:ab3ee77451e7 | 74 | */ |
mbed_official | 36:ab3ee77451e7 | 75 | int readable(); |
mbed_official | 36:ab3ee77451e7 | 76 | |
mbed_official | 36:ab3ee77451e7 | 77 | /** Determine if there is space available to write a character |
mbed_official | 36:ab3ee77451e7 | 78 | * |
mbed_official | 36:ab3ee77451e7 | 79 | * @returns |
mbed_official | 36:ab3ee77451e7 | 80 | * 1 if there is space to write a character, |
mbed_official | 36:ab3ee77451e7 | 81 | * 0 otherwise |
mbed_official | 36:ab3ee77451e7 | 82 | */ |
mbed_official | 36:ab3ee77451e7 | 83 | int writeable(); |
mbed_official | 36:ab3ee77451e7 | 84 | |
mbed_official | 36:ab3ee77451e7 | 85 | /** Attach a function to call whenever a serial interrupt is generated |
mbed_official | 36:ab3ee77451e7 | 86 | * |
mbed_official | 36:ab3ee77451e7 | 87 | * @param fptr A pointer to a void function, or 0 to set as none |
mbed_official | 36:ab3ee77451e7 | 88 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
mbed_official | 36:ab3ee77451e7 | 89 | */ |
mbed_official | 36:ab3ee77451e7 | 90 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
mbed_official | 36:ab3ee77451e7 | 91 | |
mbed_official | 36:ab3ee77451e7 | 92 | /** Attach a member function to call whenever a serial interrupt is generated |
mbed_official | 36:ab3ee77451e7 | 93 | * |
mbed_official | 36:ab3ee77451e7 | 94 | * @param tptr pointer to the object to call the member function on |
mbed_official | 36:ab3ee77451e7 | 95 | * @param mptr pointer to the member function to be called |
mbed_official | 36:ab3ee77451e7 | 96 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
mbed_official | 36:ab3ee77451e7 | 97 | */ |
mbed_official | 36:ab3ee77451e7 | 98 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 99 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
mbed_official | 36:ab3ee77451e7 | 100 | if((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 36:ab3ee77451e7 | 101 | _irq[type].attach(tptr, mptr); |
mbed_official | 36:ab3ee77451e7 | 102 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
mbed_official | 36:ab3ee77451e7 | 103 | } |
mbed_official | 36:ab3ee77451e7 | 104 | } |
mbed_official | 36:ab3ee77451e7 | 105 | |
mbed_official | 36:ab3ee77451e7 | 106 | /** Generate a break condition on the serial line |
mbed_official | 36:ab3ee77451e7 | 107 | */ |
mbed_official | 36:ab3ee77451e7 | 108 | void send_break(); |
mbed_official | 212:34d62c0b2af6 | 109 | |
mbed_official | 64:7b352733b00a | 110 | #if DEVICE_SERIAL_FC |
mbed_official | 64:7b352733b00a | 111 | /** Set the flow control type on the serial port |
mbed_official | 64:7b352733b00a | 112 | * |
mbed_official | 212:34d62c0b2af6 | 113 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
mbed_official | 64:7b352733b00a | 114 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
mbed_official | 64:7b352733b00a | 115 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
mbed_official | 64:7b352733b00a | 116 | */ |
mbed_official | 64:7b352733b00a | 117 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
mbed_official | 64:7b352733b00a | 118 | #endif |
mbed_official | 36:ab3ee77451e7 | 119 | |
mbed_official | 36:ab3ee77451e7 | 120 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
mbed_official | 36:ab3ee77451e7 | 121 | |
mbed_official | 36:ab3ee77451e7 | 122 | protected: |
mbed_official | 36:ab3ee77451e7 | 123 | SerialBase(PinName tx, PinName rx); |
mbed_official | 213:cc557cb04877 | 124 | virtual ~SerialBase() { |
mbed_official | 213:cc557cb04877 | 125 | } |
mbed_official | 212:34d62c0b2af6 | 126 | |
mbed_official | 36:ab3ee77451e7 | 127 | int _base_getc(); |
mbed_official | 36:ab3ee77451e7 | 128 | int _base_putc(int c); |
mbed_official | 36:ab3ee77451e7 | 129 | |
mbed_official | 36:ab3ee77451e7 | 130 | serial_t _serial; |
mbed_official | 36:ab3ee77451e7 | 131 | FunctionPointer _irq[2]; |
mbed_official | 36:ab3ee77451e7 | 132 | int _baud; |
mbed_official | 36:ab3ee77451e7 | 133 | }; |
mbed_official | 36:ab3ee77451e7 | 134 | |
mbed_official | 36:ab3ee77451e7 | 135 | } // namespace mbed |
mbed_official | 36:ab3ee77451e7 | 136 | |
mbed_official | 36:ab3ee77451e7 | 137 | #endif |
mbed_official | 36:ab3ee77451e7 | 138 | |
mbed_official | 36:ab3ee77451e7 | 139 | #endif |