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