mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
api/Serial.h@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 | #ifndef MBED_SERIAL_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_SERIAL_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "platform.h" |
bogdanm | 13:0645d8841f51 | 20 | |
bogdanm | 13:0645d8841f51 | 21 | #if DEVICE_SERIAL |
bogdanm | 13:0645d8841f51 | 22 | |
bogdanm | 13:0645d8841f51 | 23 | #include "Stream.h" |
bogdanm | 13:0645d8841f51 | 24 | #include "FunctionPointer.h" |
bogdanm | 13:0645d8841f51 | 25 | #include "serial_api.h" |
bogdanm | 13:0645d8841f51 | 26 | |
bogdanm | 13:0645d8841f51 | 27 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 28 | |
bogdanm | 13:0645d8841f51 | 29 | /** A serial port (UART) for communication with other serial devices |
bogdanm | 13:0645d8841f51 | 30 | * |
bogdanm | 13:0645d8841f51 | 31 | * Can be used for Full Duplex communication, or Simplex by specifying |
bogdanm | 13:0645d8841f51 | 32 | * one pin as NC (Not Connected) |
bogdanm | 13:0645d8841f51 | 33 | * |
bogdanm | 13:0645d8841f51 | 34 | * Example: |
bogdanm | 13:0645d8841f51 | 35 | * @code |
bogdanm | 13:0645d8841f51 | 36 | * // Print "Hello World" to the PC |
bogdanm | 13:0645d8841f51 | 37 | * |
bogdanm | 13:0645d8841f51 | 38 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 39 | * |
bogdanm | 13:0645d8841f51 | 40 | * Serial pc(USBTX, USBRX); |
bogdanm | 13:0645d8841f51 | 41 | * |
bogdanm | 13:0645d8841f51 | 42 | * int main() { |
bogdanm | 13:0645d8841f51 | 43 | * pc.printf("Hello World\n"); |
bogdanm | 13:0645d8841f51 | 44 | * } |
bogdanm | 13:0645d8841f51 | 45 | * @endcode |
bogdanm | 13:0645d8841f51 | 46 | */ |
bogdanm | 13:0645d8841f51 | 47 | class Serial : public Stream { |
bogdanm | 13:0645d8841f51 | 48 | |
bogdanm | 13:0645d8841f51 | 49 | public: |
bogdanm | 13:0645d8841f51 | 50 | /** Create a Serial port, connected to the specified transmit and receive pins |
bogdanm | 13:0645d8841f51 | 51 | * |
bogdanm | 13:0645d8841f51 | 52 | * @param tx Transmit pin |
bogdanm | 13:0645d8841f51 | 53 | * @param rx Receive pin |
bogdanm | 13:0645d8841f51 | 54 | * |
bogdanm | 13:0645d8841f51 | 55 | * @note |
bogdanm | 13:0645d8841f51 | 56 | * Either tx or rx may be specified as NC if unused |
bogdanm | 13:0645d8841f51 | 57 | */ |
bogdanm | 13:0645d8841f51 | 58 | Serial(PinName tx, PinName rx, const char *name=NULL); |
bogdanm | 13:0645d8841f51 | 59 | |
bogdanm | 13:0645d8841f51 | 60 | /** Set the baud rate of the serial port |
bogdanm | 13:0645d8841f51 | 61 | * |
bogdanm | 13:0645d8841f51 | 62 | * @param baudrate The baudrate of the serial port (default = 9600). |
bogdanm | 13:0645d8841f51 | 63 | */ |
bogdanm | 13:0645d8841f51 | 64 | void baud(int baudrate); |
bogdanm | 13:0645d8841f51 | 65 | |
bogdanm | 13:0645d8841f51 | 66 | enum Parity { |
bogdanm | 13:0645d8841f51 | 67 | None = 0, |
bogdanm | 13:0645d8841f51 | 68 | Odd, |
bogdanm | 13:0645d8841f51 | 69 | Even, |
bogdanm | 13:0645d8841f51 | 70 | Forced1, |
bogdanm | 13:0645d8841f51 | 71 | Forced0 |
bogdanm | 13:0645d8841f51 | 72 | }; |
bogdanm | 13:0645d8841f51 | 73 | |
bogdanm | 13:0645d8841f51 | 74 | enum IrqType { |
bogdanm | 13:0645d8841f51 | 75 | RxIrq = 0, |
bogdanm | 13:0645d8841f51 | 76 | TxIrq |
bogdanm | 13:0645d8841f51 | 77 | }; |
bogdanm | 13:0645d8841f51 | 78 | |
bogdanm | 13:0645d8841f51 | 79 | /** Set the transmission format used by the Serial port |
bogdanm | 13:0645d8841f51 | 80 | * |
bogdanm | 13:0645d8841f51 | 81 | * @param bits The number of bits in a word (5-8; default = 8) |
bogdanm | 13:0645d8841f51 | 82 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
bogdanm | 13:0645d8841f51 | 83 | * @param stop The number of stop bits (1 or 2; default = 1) |
bogdanm | 13:0645d8841f51 | 84 | */ |
bogdanm | 13:0645d8841f51 | 85 | void format(int bits = 8, Parity parity=Serial::None, int stop_bits=1); |
bogdanm | 13:0645d8841f51 | 86 | |
bogdanm | 13:0645d8841f51 | 87 | /** Determine if there is a character available to read |
bogdanm | 13:0645d8841f51 | 88 | * |
bogdanm | 13:0645d8841f51 | 89 | * @returns |
bogdanm | 13:0645d8841f51 | 90 | * 1 if there is a character available to read, |
bogdanm | 13:0645d8841f51 | 91 | * 0 otherwise |
bogdanm | 13:0645d8841f51 | 92 | */ |
bogdanm | 13:0645d8841f51 | 93 | int readable(); |
bogdanm | 13:0645d8841f51 | 94 | |
bogdanm | 13:0645d8841f51 | 95 | /** Determine if there is space available to write a character |
bogdanm | 13:0645d8841f51 | 96 | * |
bogdanm | 13:0645d8841f51 | 97 | * @returns |
bogdanm | 13:0645d8841f51 | 98 | * 1 if there is space to write a character, |
bogdanm | 13:0645d8841f51 | 99 | * 0 otherwise |
bogdanm | 13:0645d8841f51 | 100 | */ |
bogdanm | 13:0645d8841f51 | 101 | int writeable(); |
bogdanm | 13:0645d8841f51 | 102 | |
bogdanm | 13:0645d8841f51 | 103 | /** Attach a function to call whenever a serial interrupt is generated |
bogdanm | 13:0645d8841f51 | 104 | * |
bogdanm | 13:0645d8841f51 | 105 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 13:0645d8841f51 | 106 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 13:0645d8841f51 | 107 | */ |
bogdanm | 13:0645d8841f51 | 108 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
bogdanm | 13:0645d8841f51 | 109 | |
bogdanm | 13:0645d8841f51 | 110 | /** Attach a member function to call whenever a serial interrupt is generated |
bogdanm | 13:0645d8841f51 | 111 | * |
bogdanm | 13:0645d8841f51 | 112 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 113 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 114 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 13:0645d8841f51 | 115 | */ |
bogdanm | 13:0645d8841f51 | 116 | template<typename T> |
bogdanm | 13:0645d8841f51 | 117 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
bogdanm | 13:0645d8841f51 | 118 | if((mptr != NULL) && (tptr != NULL)) { |
bogdanm | 13:0645d8841f51 | 119 | _irq[type].attach(tptr, mptr); |
bogdanm | 13:0645d8841f51 | 120 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
bogdanm | 13:0645d8841f51 | 121 | } |
bogdanm | 13:0645d8841f51 | 122 | } |
bogdanm | 13:0645d8841f51 | 123 | |
bogdanm | 13:0645d8841f51 | 124 | /** Generate a break condition on the serial line |
bogdanm | 13:0645d8841f51 | 125 | */ |
bogdanm | 13:0645d8841f51 | 126 | void send_break(); |
bogdanm | 13:0645d8841f51 | 127 | |
bogdanm | 13:0645d8841f51 | 128 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
bogdanm | 13:0645d8841f51 | 129 | |
bogdanm | 13:0645d8841f51 | 130 | protected: |
bogdanm | 13:0645d8841f51 | 131 | virtual int _getc(); |
bogdanm | 13:0645d8841f51 | 132 | virtual int _putc(int c); |
bogdanm | 13:0645d8841f51 | 133 | |
bogdanm | 13:0645d8841f51 | 134 | serial_t _serial; |
bogdanm | 13:0645d8841f51 | 135 | FunctionPointer _irq[2]; |
bogdanm | 13:0645d8841f51 | 136 | int _baud; |
bogdanm | 13:0645d8841f51 | 137 | }; |
bogdanm | 13:0645d8841f51 | 138 | |
bogdanm | 13:0645d8841f51 | 139 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 140 | |
bogdanm | 13:0645d8841f51 | 141 | #endif |
bogdanm | 13:0645d8841f51 | 142 | |
bogdanm | 13:0645d8841f51 | 143 | #endif |