Lab 1 Program C
Fork of mbed by
Serial.h@43:aff670d0d510, 2012-10-24 (annotated)
- Committer:
- screamer
- Date:
- Wed Oct 24 10:44:49 2012 +0000
- Revision:
- 43:aff670d0d510
- Parent:
- 27:7110ebee3484
Conversion of the classes documentation to Doxygen format
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 | |
screamer | 43:aff670d0d510 | 20 | /** A serial port (UART) for communication with other serial devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * |
screamer | 43:aff670d0d510 | 22 | * Can be used for Full Duplex communication, or Simplex by specifying |
screamer | 43:aff670d0d510 | 23 | * one pin as NC (Not Connected) |
emilmont | 27:7110ebee3484 | 24 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * Example: |
screamer | 43:aff670d0d510 | 26 | * @code |
screamer | 43:aff670d0d510 | 27 | * // Print "Hello World" to the PC |
screamer | 43:aff670d0d510 | 28 | * |
screamer | 43:aff670d0d510 | 29 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 30 | * |
screamer | 43:aff670d0d510 | 31 | * Serial pc(USBTX, USBRX); |
screamer | 43:aff670d0d510 | 32 | * |
screamer | 43:aff670d0d510 | 33 | * int main() { |
screamer | 43:aff670d0d510 | 34 | * pc.printf("Hello World\n"); |
screamer | 43:aff670d0d510 | 35 | * } |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 42 | /** Create a Serial port, connected to the specified transmit and receive pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | * |
screamer | 43:aff670d0d510 | 44 | * @param tx Transmit pin |
screamer | 43:aff670d0d510 | 45 | * @param rx Receive pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | * |
screamer | 43:aff670d0d510 | 47 | * @note |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 52 | /** Set the baud rate of the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | * |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 71 | /** Set the transmission format used by the Serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | * |
screamer | 43:aff670d0d510 | 73 | * @param bits The number of bits in a word (5-8; default = 8) |
screamer | 43:aff670d0d510 | 74 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 81 | /** Write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | * |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 87 | /** Reads a character from the serial port. This will block until |
screamer | 43:aff670d0d510 | 88 | * a character is available. To see if a character is available, |
screamer | 43:aff670d0d510 | 89 | * see readable() |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | * |
screamer | 43:aff670d0d510 | 91 | * @returns |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 96 | /** Write a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 97 | * |
screamer | 43:aff670d0d510 | 98 | * @param format A printf-style format string, followed by the |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 103 | /** Read a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 104 | * |
screamer | 43:aff670d0d510 | 105 | * @param format A scanf-style format string, |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 112 | /** Determine if there is a character available to read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 113 | * |
screamer | 43:aff670d0d510 | 114 | * @returns |
screamer | 43:aff670d0d510 | 115 | * 1 if there is a character available to read, |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 120 | /** Determine if there is space available to write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 121 | * |
screamer | 43:aff670d0d510 | 122 | * @returns |
screamer | 43:aff670d0d510 | 123 | * 1 if there is space to write a character, |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 128 | /** Attach a function to call whenever a serial interrupt is generated |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 129 | * |
screamer | 43:aff670d0d510 | 130 | * @param fptr A pointer to a void function, or 0 to set as none |
screamer | 43:aff670d0d510 | 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 | |
screamer | 43:aff670d0d510 | 135 | /** Attach a member function to call whenever a serial interrupt is generated |
simon.ford@mbed.co.uk | 15:d1a9de3f4fe0 | 136 | * |
screamer | 43:aff670d0d510 | 137 | * @param tptr pointer to the object to call the member function on |
screamer | 43:aff670d0d510 | 138 | * @param mptr pointer to the member function to be called |
screamer | 43:aff670d0d510 | 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 |