Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
Serial.h@43:e2ed12d17f06, 2012-10-26 (annotated)
- Committer:
- emilmont
- Date:
- Fri Oct 26 17:40:46 2012 +0100
- Revision:
- 43:e2ed12d17f06
- Parent:
- 27:7110ebee3484
- Child:
- 44:24d45a770a51
Update documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rolf.meyer@arm.com | 11:1c1ebd0324fa | 1 | /* mbed Microcontroller Library - Serial |
emilmont | 27:7110ebee3484 | 2 | * Copyright (c) 2007-2011 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 5 | #ifndef MBED_SERIAL_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 6 | #define MBED_SERIAL_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 7 | |
emilmont | 27:7110ebee3484 | 8 | #include "device.h" |
emilmont | 27:7110ebee3484 | 9 | |
emilmont | 27:7110ebee3484 | 10 | #if DEVICE_SERIAL |
emilmont | 27:7110ebee3484 | 11 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 12 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 13 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 14 | #include "PeripheralNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 15 | #include "Stream.h" |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 16 | #include "FunctionPointer.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 17 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 18 | namespace mbed { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | |
emilmont | 43:e2ed12d17f06 | 20 | /** A serial port (UART) for communication with other serial devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * |
emilmont | 43:e2ed12d17f06 | 22 | * Can be used for Full Duplex communication, or Simplex by specifying |
emilmont | 43:e2ed12d17f06 | 23 | * one pin as NC (Not Connected) |
emilmont | 27:7110ebee3484 | 24 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * Example: |
emilmont | 43:e2ed12d17f06 | 26 | * @code |
emilmont | 43:e2ed12d17f06 | 27 | * // Print "Hello World" to the PC |
emilmont | 43:e2ed12d17f06 | 28 | * |
emilmont | 43:e2ed12d17f06 | 29 | * #include "mbed.h" |
emilmont | 43:e2ed12d17f06 | 30 | * |
emilmont | 43:e2ed12d17f06 | 31 | * Serial pc(USBTX, USBRX); |
emilmont | 43:e2ed12d17f06 | 32 | * |
emilmont | 43:e2ed12d17f06 | 33 | * int main() { |
emilmont | 43:e2ed12d17f06 | 34 | * pc.printf("Hello World\n"); |
emilmont | 43:e2ed12d17f06 | 35 | * } |
emilmont | 43:e2ed12d17f06 | 36 | * @endcode |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | class Serial : public Stream { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 40 | public: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | |
emilmont | 43:e2ed12d17f06 | 42 | /** Create a Serial port, connected to the specified transmit and receive pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | * |
emilmont | 43:e2ed12d17f06 | 44 | * @param tx Transmit pin |
emilmont | 43:e2ed12d17f06 | 45 | * @param rx Receive pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | * |
emilmont | 43:e2ed12d17f06 | 47 | * @note |
emilmont | 43:e2ed12d17f06 | 48 | * Either tx or rx may be specified as NC if unused |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 50 | Serial(PinName tx, PinName rx, const char *name = NULL); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | |
emilmont | 43:e2ed12d17f06 | 52 | /** Set the baud rate of the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | * |
emilmont | 43:e2ed12d17f06 | 54 | * @param baudrate The baudrate of the serial port (default = 9600). |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 56 | void baud(int baudrate); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 57 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | enum Parity { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | None = 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 60 | , Odd |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 61 | , Even |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | , Forced1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 63 | , Forced0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 64 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | |
simon | 21:3944f1e2fa4f | 66 | enum IrqType { |
simon | 21:3944f1e2fa4f | 67 | RxIrq = 0 |
simon | 21:3944f1e2fa4f | 68 | , TxIrq |
simon | 21:3944f1e2fa4f | 69 | }; |
simon | 21:3944f1e2fa4f | 70 | |
emilmont | 43:e2ed12d17f06 | 71 | /** Set the transmission format used by the Serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | * |
emilmont | 43:e2ed12d17f06 | 73 | * @param bits The number of bits in a word (5-8; default = 8) |
emilmont | 43:e2ed12d17f06 | 74 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
emilmont | 43:e2ed12d17f06 | 75 | * @param stop The number of stop bits (1 or 2; default = 1) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 76 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 77 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 78 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 79 | #if 0 // Inhereted from Stream, for documentation only |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 80 | |
emilmont | 43:e2ed12d17f06 | 81 | /** Write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | * |
emilmont | 43:e2ed12d17f06 | 83 | * @param c The character to write to the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 84 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 85 | int putc(int c); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 86 | |
emilmont | 43:e2ed12d17f06 | 87 | /** Reads a character from the serial port. This will block until |
emilmont | 43:e2ed12d17f06 | 88 | * a character is available. To see if a character is available, |
emilmont | 43:e2ed12d17f06 | 89 | * see readable() |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | * |
emilmont | 43:e2ed12d17f06 | 91 | * @returns |
emilmont | 43:e2ed12d17f06 | 92 | * The character read from the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 93 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 94 | int getc(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 95 | |
emilmont | 43:e2ed12d17f06 | 96 | /** Write a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 97 | * |
emilmont | 43:e2ed12d17f06 | 98 | * @param format A printf-style format string, followed by the |
emilmont | 43:e2ed12d17f06 | 99 | * variables to use in formating the string. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 100 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 101 | int printf(const char* format, ...); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 102 | |
emilmont | 43:e2ed12d17f06 | 103 | /** Read a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 104 | * |
emilmont | 43:e2ed12d17f06 | 105 | * @param format A scanf-style format string, |
emilmont | 43:e2ed12d17f06 | 106 | * followed by the pointers to variables to store the results. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 107 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 108 | int scanf(const char* format, ...); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 109 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 110 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 111 | |
emilmont | 43:e2ed12d17f06 | 112 | /** Determine if there is a character available to read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 113 | * |
emilmont | 43:e2ed12d17f06 | 114 | * @returns |
emilmont | 43:e2ed12d17f06 | 115 | * 1 if there is a character available to read, |
emilmont | 43:e2ed12d17f06 | 116 | * 0 otherwise |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 117 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 118 | int readable(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 119 | |
emilmont | 43:e2ed12d17f06 | 120 | /** Determine if there is space available to write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 121 | * |
emilmont | 43:e2ed12d17f06 | 122 | * @returns |
emilmont | 43:e2ed12d17f06 | 123 | * 1 if there is space to write a character, |
emilmont | 43:e2ed12d17f06 | 124 | * 0 otherwise |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 125 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 126 | int writeable(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 127 | |
emilmont | 43:e2ed12d17f06 | 128 | /** Attach a function to call whenever a serial interrupt is generated |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 129 | * |
emilmont | 43:e2ed12d17f06 | 130 | * @param fptr A pointer to a void function, or 0 to set as none |
emilmont | 43:e2ed12d17f06 | 131 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 132 | */ |
simon | 21:3944f1e2fa4f | 133 | void attach(void (*fptr)(void), IrqType type = RxIrq); |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 134 | |
emilmont | 43:e2ed12d17f06 | 135 | /** Attach a member function to call whenever a serial interrupt is generated |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 136 | * |
emilmont | 43:e2ed12d17f06 | 137 | * @param tptr pointer to the object to call the member function on |
emilmont | 43:e2ed12d17f06 | 138 | * @param mptr pointer to the member function to be called |
emilmont | 43:e2ed12d17f06 | 139 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 140 | */ |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 141 | template<typename T> |
simon | 21:3944f1e2fa4f | 142 | void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) { |
simon | 21:3944f1e2fa4f | 143 | if((mptr != NULL) && (tptr != NULL)) { |
simon | 21:3944f1e2fa4f | 144 | _irq[type].attach(tptr, mptr); |
simon | 21:3944f1e2fa4f | 145 | setup_interrupt(type); |
simon | 21:3944f1e2fa4f | 146 | } |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 147 | } |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 148 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 149 | #ifdef MBED_RPC |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 150 | virtual const struct rpc_method *get_rpc_methods(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 151 | static struct rpc_class *get_rpc_class(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 152 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 153 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 154 | protected: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 155 | |
simon | 21:3944f1e2fa4f | 156 | void setup_interrupt(IrqType type); |
simon | 21:3944f1e2fa4f | 157 | void remove_interrupt(IrqType type); |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 158 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 159 | virtual int _getc(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 160 | virtual int _putc(int c); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 161 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 162 | UARTName _uart; |
simon | 21:3944f1e2fa4f | 163 | FunctionPointer _irq[2]; |
simon | 21:3944f1e2fa4f | 164 | int _uidx; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 165 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 166 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 167 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 168 | } // namespace mbed |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 169 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 170 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 171 | |
emilmont | 27:7110ebee3484 | 172 | #endif |