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.
quadv3/mbed/Serial.h@1:ac68f0368a77, 2013-07-23 (annotated)
- Committer:
- Anaesthetix
- Date:
- Tue Jul 23 14:01:42 2013 +0000
- Revision:
- 1:ac68f0368a77
- Parent:
- 0:978110f7f027
Other accelerometer added
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Anaesthetix | 0:978110f7f027 | 1 | /* mbed Microcontroller Library - Serial |
| Anaesthetix | 0:978110f7f027 | 2 | * Copyright (c) 2007-2011 ARM Limited. All rights reserved. |
| Anaesthetix | 0:978110f7f027 | 3 | */ |
| Anaesthetix | 0:978110f7f027 | 4 | |
| Anaesthetix | 0:978110f7f027 | 5 | #ifndef MBED_SERIAL_H |
| Anaesthetix | 0:978110f7f027 | 6 | #define MBED_SERIAL_H |
| Anaesthetix | 0:978110f7f027 | 7 | |
| Anaesthetix | 0:978110f7f027 | 8 | #include "device.h" |
| Anaesthetix | 0:978110f7f027 | 9 | |
| Anaesthetix | 0:978110f7f027 | 10 | #if DEVICE_SERIAL |
| Anaesthetix | 0:978110f7f027 | 11 | |
| Anaesthetix | 0:978110f7f027 | 12 | #include "platform.h" |
| Anaesthetix | 0:978110f7f027 | 13 | #include "PinNames.h" |
| Anaesthetix | 0:978110f7f027 | 14 | #include "PeripheralNames.h" |
| Anaesthetix | 0:978110f7f027 | 15 | #include "Stream.h" |
| Anaesthetix | 0:978110f7f027 | 16 | #include "FunctionPointer.h" |
| Anaesthetix | 0:978110f7f027 | 17 | |
| Anaesthetix | 0:978110f7f027 | 18 | namespace mbed { |
| Anaesthetix | 0:978110f7f027 | 19 | |
| Anaesthetix | 0:978110f7f027 | 20 | /* Class: Serial |
| Anaesthetix | 0:978110f7f027 | 21 | * A serial port (UART) for communication with other serial devices |
| Anaesthetix | 0:978110f7f027 | 22 | * |
| Anaesthetix | 0:978110f7f027 | 23 | * Can be used for Full Duplex communication, or Simplex by specifying |
| Anaesthetix | 0:978110f7f027 | 24 | * one pin as NC (Not Connected) |
| Anaesthetix | 0:978110f7f027 | 25 | * |
| Anaesthetix | 0:978110f7f027 | 26 | * Example: |
| Anaesthetix | 0:978110f7f027 | 27 | * > // Print "Hello World" to the PC |
| Anaesthetix | 0:978110f7f027 | 28 | * > |
| Anaesthetix | 0:978110f7f027 | 29 | * > #include "mbed.h" |
| Anaesthetix | 0:978110f7f027 | 30 | * > |
| Anaesthetix | 0:978110f7f027 | 31 | * > Serial pc(USBTX, USBRX); |
| Anaesthetix | 0:978110f7f027 | 32 | * > |
| Anaesthetix | 0:978110f7f027 | 33 | * > int main() { |
| Anaesthetix | 0:978110f7f027 | 34 | * > pc.printf("Hello World\n"); |
| Anaesthetix | 0:978110f7f027 | 35 | * > } |
| Anaesthetix | 0:978110f7f027 | 36 | */ |
| Anaesthetix | 0:978110f7f027 | 37 | class Serial : public Stream { |
| Anaesthetix | 0:978110f7f027 | 38 | |
| Anaesthetix | 0:978110f7f027 | 39 | public: |
| Anaesthetix | 0:978110f7f027 | 40 | |
| Anaesthetix | 0:978110f7f027 | 41 | /* Constructor: Serial |
| Anaesthetix | 0:978110f7f027 | 42 | * Create a Serial port, connected to the specified transmit and receive pins |
| Anaesthetix | 0:978110f7f027 | 43 | * |
| Anaesthetix | 0:978110f7f027 | 44 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 45 | * tx - Transmit pin |
| Anaesthetix | 0:978110f7f027 | 46 | * rx - Receive pin |
| Anaesthetix | 0:978110f7f027 | 47 | * |
| Anaesthetix | 0:978110f7f027 | 48 | * Note: Either tx or rx may be specified as NC if unused |
| Anaesthetix | 0:978110f7f027 | 49 | */ |
| Anaesthetix | 0:978110f7f027 | 50 | Serial(PinName tx, PinName rx, const char *name = NULL); |
| Anaesthetix | 0:978110f7f027 | 51 | |
| Anaesthetix | 0:978110f7f027 | 52 | /* Function: baud |
| Anaesthetix | 0:978110f7f027 | 53 | * Set the baud rate of the serial port |
| Anaesthetix | 0:978110f7f027 | 54 | * |
| Anaesthetix | 0:978110f7f027 | 55 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 56 | * baudrate - The baudrate of the serial port (default = 9600). |
| Anaesthetix | 0:978110f7f027 | 57 | */ |
| Anaesthetix | 0:978110f7f027 | 58 | void baud(int baudrate); |
| Anaesthetix | 0:978110f7f027 | 59 | |
| Anaesthetix | 0:978110f7f027 | 60 | enum Parity { |
| Anaesthetix | 0:978110f7f027 | 61 | None = 0 |
| Anaesthetix | 0:978110f7f027 | 62 | , Odd |
| Anaesthetix | 0:978110f7f027 | 63 | , Even |
| Anaesthetix | 0:978110f7f027 | 64 | , Forced1 |
| Anaesthetix | 0:978110f7f027 | 65 | , Forced0 |
| Anaesthetix | 0:978110f7f027 | 66 | }; |
| Anaesthetix | 0:978110f7f027 | 67 | |
| Anaesthetix | 0:978110f7f027 | 68 | enum IrqType { |
| Anaesthetix | 0:978110f7f027 | 69 | RxIrq = 0 |
| Anaesthetix | 0:978110f7f027 | 70 | , TxIrq |
| Anaesthetix | 0:978110f7f027 | 71 | }; |
| Anaesthetix | 0:978110f7f027 | 72 | |
| Anaesthetix | 0:978110f7f027 | 73 | /* Function: format |
| Anaesthetix | 0:978110f7f027 | 74 | * Set the transmission format used by the Serial port |
| Anaesthetix | 0:978110f7f027 | 75 | * |
| Anaesthetix | 0:978110f7f027 | 76 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 77 | * bits - The number of bits in a word (5-8; default = 8) |
| Anaesthetix | 0:978110f7f027 | 78 | * parity - The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
| Anaesthetix | 0:978110f7f027 | 79 | * stop - The number of stop bits (1 or 2; default = 1) |
| Anaesthetix | 0:978110f7f027 | 80 | */ |
| Anaesthetix | 0:978110f7f027 | 81 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); |
| Anaesthetix | 0:978110f7f027 | 82 | |
| Anaesthetix | 0:978110f7f027 | 83 | #if 0 // Inhereted from Stream, for documentation only |
| Anaesthetix | 0:978110f7f027 | 84 | |
| Anaesthetix | 0:978110f7f027 | 85 | /* Function: putc |
| Anaesthetix | 0:978110f7f027 | 86 | * Write a character |
| Anaesthetix | 0:978110f7f027 | 87 | * |
| Anaesthetix | 0:978110f7f027 | 88 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 89 | * c - The character to write to the serial port |
| Anaesthetix | 0:978110f7f027 | 90 | */ |
| Anaesthetix | 0:978110f7f027 | 91 | int putc(int c); |
| Anaesthetix | 0:978110f7f027 | 92 | |
| Anaesthetix | 0:978110f7f027 | 93 | /* Function: getc |
| Anaesthetix | 0:978110f7f027 | 94 | * Read a character |
| Anaesthetix | 0:978110f7f027 | 95 | * |
| Anaesthetix | 0:978110f7f027 | 96 | * Reads a character from the serial port. This will block until |
| Anaesthetix | 0:978110f7f027 | 97 | * a character is available. To see if a character is available, |
| Anaesthetix | 0:978110f7f027 | 98 | * see <readable> |
| Anaesthetix | 0:978110f7f027 | 99 | * |
| Anaesthetix | 0:978110f7f027 | 100 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 101 | * returns - The character read from the serial port |
| Anaesthetix | 0:978110f7f027 | 102 | */ |
| Anaesthetix | 0:978110f7f027 | 103 | int getc(); |
| Anaesthetix | 0:978110f7f027 | 104 | |
| Anaesthetix | 0:978110f7f027 | 105 | /* Function: printf |
| Anaesthetix | 0:978110f7f027 | 106 | * Write a formated string |
| Anaesthetix | 0:978110f7f027 | 107 | * |
| Anaesthetix | 0:978110f7f027 | 108 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 109 | * format - A printf-style format string, followed by the |
| Anaesthetix | 0:978110f7f027 | 110 | * variables to use in formating the string. |
| Anaesthetix | 0:978110f7f027 | 111 | */ |
| Anaesthetix | 0:978110f7f027 | 112 | int printf(const char* format, ...); |
| Anaesthetix | 0:978110f7f027 | 113 | |
| Anaesthetix | 0:978110f7f027 | 114 | /* Function: scanf |
| Anaesthetix | 0:978110f7f027 | 115 | * Read a formated string |
| Anaesthetix | 0:978110f7f027 | 116 | * |
| Anaesthetix | 0:978110f7f027 | 117 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 118 | * format - A scanf-style format string, |
| Anaesthetix | 0:978110f7f027 | 119 | * followed by the pointers to variables to store the results. |
| Anaesthetix | 0:978110f7f027 | 120 | */ |
| Anaesthetix | 0:978110f7f027 | 121 | int scanf(const char* format, ...); |
| Anaesthetix | 0:978110f7f027 | 122 | |
| Anaesthetix | 0:978110f7f027 | 123 | #endif |
| Anaesthetix | 0:978110f7f027 | 124 | |
| Anaesthetix | 0:978110f7f027 | 125 | /* Function: readable |
| Anaesthetix | 0:978110f7f027 | 126 | * Determine if there is a character available to read |
| Anaesthetix | 0:978110f7f027 | 127 | * |
| Anaesthetix | 0:978110f7f027 | 128 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 129 | * returns - 1 if there is a character available to read, else 0 |
| Anaesthetix | 0:978110f7f027 | 130 | */ |
| Anaesthetix | 0:978110f7f027 | 131 | int readable(); |
| Anaesthetix | 0:978110f7f027 | 132 | |
| Anaesthetix | 0:978110f7f027 | 133 | /* Function: writeable |
| Anaesthetix | 0:978110f7f027 | 134 | * Determine if there is space available to write a character |
| Anaesthetix | 0:978110f7f027 | 135 | * |
| Anaesthetix | 0:978110f7f027 | 136 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 137 | * returns - 1 if there is space to write a character, else 0 |
| Anaesthetix | 0:978110f7f027 | 138 | */ |
| Anaesthetix | 0:978110f7f027 | 139 | int writeable(); |
| Anaesthetix | 0:978110f7f027 | 140 | |
| Anaesthetix | 0:978110f7f027 | 141 | /* Function: attach |
| Anaesthetix | 0:978110f7f027 | 142 | * Attach a function to call whenever a serial interrupt is generated |
| Anaesthetix | 0:978110f7f027 | 143 | * |
| Anaesthetix | 0:978110f7f027 | 144 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 145 | * fptr - A pointer to a void function, or 0 to set as none |
| Anaesthetix | 0:978110f7f027 | 146 | * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
| Anaesthetix | 0:978110f7f027 | 147 | */ |
| Anaesthetix | 0:978110f7f027 | 148 | void attach(void (*fptr)(void), IrqType type = RxIrq); |
| Anaesthetix | 0:978110f7f027 | 149 | |
| Anaesthetix | 0:978110f7f027 | 150 | /* Function: attach |
| Anaesthetix | 0:978110f7f027 | 151 | * Attach a member function to call whenever a serial interrupt is generated |
| Anaesthetix | 0:978110f7f027 | 152 | * |
| Anaesthetix | 0:978110f7f027 | 153 | * Variables: |
| Anaesthetix | 0:978110f7f027 | 154 | * tptr - pointer to the object to call the member function on |
| Anaesthetix | 0:978110f7f027 | 155 | * mptr - pointer to the member function to be called |
| Anaesthetix | 0:978110f7f027 | 156 | * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
| Anaesthetix | 0:978110f7f027 | 157 | */ |
| Anaesthetix | 0:978110f7f027 | 158 | template<typename T> |
| Anaesthetix | 0:978110f7f027 | 159 | void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) { |
| Anaesthetix | 0:978110f7f027 | 160 | if((mptr != NULL) && (tptr != NULL)) { |
| Anaesthetix | 0:978110f7f027 | 161 | _irq[type].attach(tptr, mptr); |
| Anaesthetix | 0:978110f7f027 | 162 | setup_interrupt(type); |
| Anaesthetix | 0:978110f7f027 | 163 | } |
| Anaesthetix | 0:978110f7f027 | 164 | } |
| Anaesthetix | 0:978110f7f027 | 165 | |
| Anaesthetix | 0:978110f7f027 | 166 | #ifdef MBED_RPC |
| Anaesthetix | 0:978110f7f027 | 167 | virtual const struct rpc_method *get_rpc_methods(); |
| Anaesthetix | 0:978110f7f027 | 168 | static struct rpc_class *get_rpc_class(); |
| Anaesthetix | 0:978110f7f027 | 169 | #endif |
| Anaesthetix | 0:978110f7f027 | 170 | |
| Anaesthetix | 0:978110f7f027 | 171 | protected: |
| Anaesthetix | 0:978110f7f027 | 172 | |
| Anaesthetix | 0:978110f7f027 | 173 | void setup_interrupt(IrqType type); |
| Anaesthetix | 0:978110f7f027 | 174 | void remove_interrupt(IrqType type); |
| Anaesthetix | 0:978110f7f027 | 175 | |
| Anaesthetix | 0:978110f7f027 | 176 | virtual int _getc(); |
| Anaesthetix | 0:978110f7f027 | 177 | virtual int _putc(int c); |
| Anaesthetix | 0:978110f7f027 | 178 | |
| Anaesthetix | 0:978110f7f027 | 179 | UARTName _uart; |
| Anaesthetix | 0:978110f7f027 | 180 | FunctionPointer _irq[2]; |
| Anaesthetix | 0:978110f7f027 | 181 | int _uidx; |
| Anaesthetix | 0:978110f7f027 | 182 | |
| Anaesthetix | 0:978110f7f027 | 183 | }; |
| Anaesthetix | 0:978110f7f027 | 184 | |
| Anaesthetix | 0:978110f7f027 | 185 | } // namespace mbed |
| Anaesthetix | 0:978110f7f027 | 186 | |
| Anaesthetix | 0:978110f7f027 | 187 | #endif |
| Anaesthetix | 0:978110f7f027 | 188 | |
| Anaesthetix | 0:978110f7f027 | 189 | #endif |