Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.
Dependents: 1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB
Fork of mbed by
SerialBase.h@84:0b3ab51c8877, 2014-05-19 (annotated)
- Committer:
- bogdanm
- Date:
- Mon May 19 18:14:09 2014 +0100
- Revision:
- 84:0b3ab51c8877
- Parent:
- 75:dc225afb6914
- Child:
- 85:024bf7f99721
Release 84 of the mbed library
Main changes:
- added LPC11U68 to the official build
- Bug fixes and new features for ST Nucleo boards
- I2C fixes for Freescale targets
- Added nRF51822 exporters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 68:f37f3b9c9f0b | 1 | /* mbed Microcontroller Library |
bogdanm | 68:f37f3b9c9f0b | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 68:f37f3b9c9f0b | 3 | * |
bogdanm | 68:f37f3b9c9f0b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 68:f37f3b9c9f0b | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 68:f37f3b9c9f0b | 6 | * You may obtain a copy of the License at |
bogdanm | 68:f37f3b9c9f0b | 7 | * |
bogdanm | 68:f37f3b9c9f0b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 68:f37f3b9c9f0b | 9 | * |
bogdanm | 68:f37f3b9c9f0b | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 68:f37f3b9c9f0b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 68:f37f3b9c9f0b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 68:f37f3b9c9f0b | 13 | * See the License for the specific language governing permissions and |
bogdanm | 68:f37f3b9c9f0b | 14 | * limitations under the License. |
bogdanm | 68:f37f3b9c9f0b | 15 | */ |
bogdanm | 68:f37f3b9c9f0b | 16 | #ifndef MBED_SERIALBASE_H |
bogdanm | 68:f37f3b9c9f0b | 17 | #define MBED_SERIALBASE_H |
bogdanm | 68:f37f3b9c9f0b | 18 | |
bogdanm | 68:f37f3b9c9f0b | 19 | #include "platform.h" |
bogdanm | 68:f37f3b9c9f0b | 20 | |
bogdanm | 68:f37f3b9c9f0b | 21 | #if DEVICE_SERIAL |
bogdanm | 68:f37f3b9c9f0b | 22 | |
bogdanm | 68:f37f3b9c9f0b | 23 | #include "Stream.h" |
bogdanm | 68:f37f3b9c9f0b | 24 | #include "FunctionPointer.h" |
bogdanm | 68:f37f3b9c9f0b | 25 | #include "serial_api.h" |
bogdanm | 68:f37f3b9c9f0b | 26 | |
bogdanm | 68:f37f3b9c9f0b | 27 | namespace mbed { |
bogdanm | 68:f37f3b9c9f0b | 28 | |
bogdanm | 68:f37f3b9c9f0b | 29 | /** A base class for serial port implementations |
bogdanm | 68:f37f3b9c9f0b | 30 | * Can't be instantiated directly (use Serial or RawSerial) |
bogdanm | 68:f37f3b9c9f0b | 31 | */ |
bogdanm | 68:f37f3b9c9f0b | 32 | class SerialBase { |
bogdanm | 68:f37f3b9c9f0b | 33 | |
bogdanm | 68:f37f3b9c9f0b | 34 | public: |
bogdanm | 68:f37f3b9c9f0b | 35 | /** Set the baud rate of the serial port |
bogdanm | 68:f37f3b9c9f0b | 36 | * |
bogdanm | 68:f37f3b9c9f0b | 37 | * @param baudrate The baudrate of the serial port (default = 9600). |
bogdanm | 68:f37f3b9c9f0b | 38 | */ |
bogdanm | 68:f37f3b9c9f0b | 39 | void baud(int baudrate); |
bogdanm | 68:f37f3b9c9f0b | 40 | |
bogdanm | 68:f37f3b9c9f0b | 41 | enum Parity { |
bogdanm | 68:f37f3b9c9f0b | 42 | None = 0, |
bogdanm | 68:f37f3b9c9f0b | 43 | Odd, |
bogdanm | 68:f37f3b9c9f0b | 44 | Even, |
bogdanm | 68:f37f3b9c9f0b | 45 | Forced1, |
bogdanm | 68:f37f3b9c9f0b | 46 | Forced0 |
bogdanm | 68:f37f3b9c9f0b | 47 | }; |
bogdanm | 68:f37f3b9c9f0b | 48 | |
bogdanm | 68:f37f3b9c9f0b | 49 | enum IrqType { |
bogdanm | 68:f37f3b9c9f0b | 50 | RxIrq = 0, |
bogdanm | 68:f37f3b9c9f0b | 51 | TxIrq |
bogdanm | 68:f37f3b9c9f0b | 52 | }; |
bogdanm | 68:f37f3b9c9f0b | 53 | |
bogdanm | 75:dc225afb6914 | 54 | enum Flow { |
bogdanm | 75:dc225afb6914 | 55 | Disabled = 0, |
bogdanm | 75:dc225afb6914 | 56 | RTS, |
bogdanm | 75:dc225afb6914 | 57 | CTS, |
bogdanm | 75:dc225afb6914 | 58 | RTSCTS |
bogdanm | 75:dc225afb6914 | 59 | }; |
bogdanm | 75:dc225afb6914 | 60 | |
bogdanm | 68:f37f3b9c9f0b | 61 | /** Set the transmission format used by the serial port |
bogdanm | 68:f37f3b9c9f0b | 62 | * |
bogdanm | 68:f37f3b9c9f0b | 63 | * @param bits The number of bits in a word (5-8; default = 8) |
bogdanm | 68:f37f3b9c9f0b | 64 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
bogdanm | 68:f37f3b9c9f0b | 65 | * @param stop The number of stop bits (1 or 2; default = 1) |
bogdanm | 68:f37f3b9c9f0b | 66 | */ |
bogdanm | 68:f37f3b9c9f0b | 67 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
bogdanm | 68:f37f3b9c9f0b | 68 | |
bogdanm | 68:f37f3b9c9f0b | 69 | /** Determine if there is a character available to read |
bogdanm | 68:f37f3b9c9f0b | 70 | * |
bogdanm | 68:f37f3b9c9f0b | 71 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 72 | * 1 if there is a character available to read, |
bogdanm | 68:f37f3b9c9f0b | 73 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 74 | */ |
bogdanm | 68:f37f3b9c9f0b | 75 | int readable(); |
bogdanm | 68:f37f3b9c9f0b | 76 | |
bogdanm | 68:f37f3b9c9f0b | 77 | /** Determine if there is space available to write a character |
bogdanm | 68:f37f3b9c9f0b | 78 | * |
bogdanm | 68:f37f3b9c9f0b | 79 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 80 | * 1 if there is space to write a character, |
bogdanm | 68:f37f3b9c9f0b | 81 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 82 | */ |
bogdanm | 68:f37f3b9c9f0b | 83 | int writeable(); |
bogdanm | 68:f37f3b9c9f0b | 84 | |
bogdanm | 68:f37f3b9c9f0b | 85 | /** Attach a function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 86 | * |
bogdanm | 68:f37f3b9c9f0b | 87 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 68:f37f3b9c9f0b | 88 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 89 | */ |
bogdanm | 68:f37f3b9c9f0b | 90 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
bogdanm | 68:f37f3b9c9f0b | 91 | |
bogdanm | 68:f37f3b9c9f0b | 92 | /** Attach a member function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 93 | * |
bogdanm | 68:f37f3b9c9f0b | 94 | * @param tptr pointer to the object to call the member function on |
bogdanm | 68:f37f3b9c9f0b | 95 | * @param mptr pointer to the member function to be called |
bogdanm | 68:f37f3b9c9f0b | 96 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 97 | */ |
bogdanm | 68:f37f3b9c9f0b | 98 | template<typename T> |
bogdanm | 68:f37f3b9c9f0b | 99 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
bogdanm | 68:f37f3b9c9f0b | 100 | if((mptr != NULL) && (tptr != NULL)) { |
bogdanm | 68:f37f3b9c9f0b | 101 | _irq[type].attach(tptr, mptr); |
bogdanm | 68:f37f3b9c9f0b | 102 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
bogdanm | 68:f37f3b9c9f0b | 103 | } |
bogdanm | 68:f37f3b9c9f0b | 104 | } |
bogdanm | 68:f37f3b9c9f0b | 105 | |
bogdanm | 68:f37f3b9c9f0b | 106 | /** Generate a break condition on the serial line |
bogdanm | 68:f37f3b9c9f0b | 107 | */ |
bogdanm | 68:f37f3b9c9f0b | 108 | void send_break(); |
bogdanm | 75:dc225afb6914 | 109 | |
bogdanm | 75:dc225afb6914 | 110 | #if DEVICE_SERIAL_FC |
bogdanm | 75:dc225afb6914 | 111 | /** Set the flow control type on the serial port |
bogdanm | 75:dc225afb6914 | 112 | * |
bogdanm | 75:dc225afb6914 | 113 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
bogdanm | 75:dc225afb6914 | 114 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
bogdanm | 75:dc225afb6914 | 115 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
bogdanm | 75:dc225afb6914 | 116 | */ |
bogdanm | 75:dc225afb6914 | 117 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
bogdanm | 75:dc225afb6914 | 118 | #endif |
bogdanm | 68:f37f3b9c9f0b | 119 | |
bogdanm | 68:f37f3b9c9f0b | 120 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
bogdanm | 68:f37f3b9c9f0b | 121 | |
bogdanm | 68:f37f3b9c9f0b | 122 | protected: |
bogdanm | 68:f37f3b9c9f0b | 123 | SerialBase(PinName tx, PinName rx); |
bogdanm | 68:f37f3b9c9f0b | 124 | |
bogdanm | 68:f37f3b9c9f0b | 125 | int _base_getc(); |
bogdanm | 68:f37f3b9c9f0b | 126 | int _base_putc(int c); |
bogdanm | 68:f37f3b9c9f0b | 127 | |
bogdanm | 68:f37f3b9c9f0b | 128 | serial_t _serial; |
bogdanm | 68:f37f3b9c9f0b | 129 | FunctionPointer _irq[2]; |
bogdanm | 68:f37f3b9c9f0b | 130 | int _baud; |
bogdanm | 68:f37f3b9c9f0b | 131 | }; |
bogdanm | 68:f37f3b9c9f0b | 132 | |
bogdanm | 68:f37f3b9c9f0b | 133 | } // namespace mbed |
bogdanm | 68:f37f3b9c9f0b | 134 | |
bogdanm | 68:f37f3b9c9f0b | 135 | #endif |
bogdanm | 68:f37f3b9c9f0b | 136 | |
bogdanm | 68:f37f3b9c9f0b | 137 | #endif |