Lab 1 Program C
Fork of mbed by
Serial.h@11:1c1ebd0324fa, 2009-08-28 (annotated)
- Committer:
- rolf.meyer@arm.com
- Date:
- Fri Aug 28 12:10:11 2009 +0000
- Revision:
- 11:1c1ebd0324fa
- Parent:
- 8:00a04e5cd407
- Child:
- 15:d1a9de3f4fe0
A shiny new version
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 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | * sford |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 5 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 6 | #ifndef MBED_SERIAL_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 7 | #define MBED_SERIAL_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 8 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 9 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 10 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 11 | #include "PeripheralNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 12 | #include "Stream.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 13 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 14 | namespace mbed { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 15 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 16 | /* Class: Serial |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 17 | * A serial port (UART) for communication with other serial devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 18 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | * Example: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 20 | * > // Print "Hello World" to the PC |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | * > #include "mbed.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 24 | * > Serial pc(USBTX, USBRX); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 26 | * > int main() { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | * > pc.printf("Hello World\n"); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 28 | * > } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 29 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 30 | class Serial : public Stream { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 31 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 32 | public: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 33 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 34 | /* Constructor: Serial |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 35 | * Create a Serial port, connected to the specified transmit and receive pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 36 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | * tx - Transmit pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | * rx - Receive pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 40 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | * Note: Either tx or rx may be specified as NOT_CONNECTED if unused |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 42 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | Serial(PinName tx, PinName rx, const char *name = NULL); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 44 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 45 | /* Function: baud |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | * Set the baud rate of the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 47 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | * baudrate - The baudrate of the serial port (default = 9600). |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 50 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | void baud(int baudrate); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 52 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | enum Parity { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 54 | None = 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | , Odd |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 56 | , Even |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 57 | , Forced1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | , Forced0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 60 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 61 | /* Function: format |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | * Set the transmission format used by the Serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 63 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 64 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | * bits - The number of bits in a word (5-8; default = 8) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 66 | * parity - The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 67 | * stop - The number of stop bits (1 or 2; default = 1) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 68 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 69 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 70 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 71 | #if 0 // Inhereted from Stream, for documentation only |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 73 | /* Function: putc |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 74 | * Write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 76 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 77 | * c - The character to write to the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 78 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 79 | int putc(int c); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 80 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | /* Function: getc |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | * Read a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 83 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 84 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 85 | * returns - The character read from the serial port |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 86 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 87 | int getc(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 88 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 89 | /* Function: printf |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | * Write a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 91 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 92 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 93 | * format - A printf-style format string, followed by the |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 94 | * variables to use in formating the string. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 95 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 96 | int printf(const char* format, ...); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 97 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 98 | /* Function: scanf |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 99 | * Read a formated string |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 100 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 101 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 102 | * format - A scanf-style format string, |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 103 | * followed by the pointers to variables to store the results. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 104 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 105 | int scanf(const char* format, ...); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 106 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 107 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 108 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 109 | /* Function: readable |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 110 | * Determine if there is a character available to read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 111 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 112 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 113 | * returns - 1 if there is a character available to read, else 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 114 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 115 | int readable(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 116 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 117 | /* Function: writeable |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 118 | * Determine if there is space available to write a character |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 119 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 120 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 121 | * returns - 1 if there is space to write a character, else 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 122 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 123 | int writeable(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 124 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 125 | #ifdef MBED_RPC |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 126 | virtual const struct rpc_method *get_rpc_methods(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 127 | static struct rpc_class *get_rpc_class(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 128 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 129 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 130 | protected: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 131 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 132 | virtual int _getc(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 133 | virtual int _putc(int c); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 134 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 135 | UARTName _uart; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 136 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 137 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 138 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 139 | } // namespace mbed |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 140 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 141 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 142 |