1 /* mbed Microcontroller Library - Serial
2 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
14 #include "PeripheralNames.h"
16 #include "FunctionPointer.h"
21 * A serial port (UART) for communication with other serial devices
23 * Can be used for Full Duplex communication, or Simplex by specifying
24 * one pin as NC (Not Connected)
27 * > // Print "Hello World" to the PC
31 * > Serial pc(USBTX, USBRX);
34 * > pc.printf("Hello World\n");
37 class Serial
: public Stream
{
41 /* Constructor: Serial
42 * Create a Serial port, connected to the specified transmit and receive pins
48 * Note: Either tx or rx may be specified as NC if unused
50 Serial(PinName tx
, PinName rx
, const char *name
= NULL
);
53 * Set the baud rate of the serial port
56 * baudrate - The baudrate of the serial port (default = 9600).
58 void baud(int baudrate
);
74 * Set the transmission format used by the Serial port
77 * bits - The number of bits in a word (5-8; default = 8)
78 * parity - The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
79 * stop - The number of stop bits (1 or 2; default = 1)
81 void format(int bits
= 8, Parity parity
= Serial::None
, int stop_bits
= 1);
83 #if 0 // Inhereted from Stream, for documentation only
89 * c - The character to write to the serial port
96 * Reads a character from the serial port. This will block until
97 * a character is available. To see if a character is available,
101 * returns - The character read from the serial port
106 * Write a formated string
109 * format - A printf-style format string, followed by the
110 * variables to use in formating the string.
112 int printf(const char* format
, ...);
115 * Read a formated string
118 * format - A scanf-style format string,
119 * followed by the pointers to variables to store the results.
121 int scanf(const char* format
, ...);
125 /* Function: readable
126 * Determine if there is a character available to read
129 * returns - 1 if there is a character available to read, else 0
133 /* Function: writeable
134 * Determine if there is space available to write a character
137 * returns - 1 if there is space to write a character, else 0
142 * Attach a function to call whenever a serial interrupt is generated
145 * fptr - A pointer to a void function, or 0 to set as none
146 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
148 void attach(void (*fptr
)(void), IrqType type
= RxIrq
);
151 * Attach a member function to call whenever a serial interrupt is generated
154 * tptr - pointer to the object to call the member function on
155 * mptr - pointer to the member function to be called
156 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
159 void attach(T
* tptr
, void (T::*mptr
)(void), IrqType type
= RxIrq
) {
160 if((mptr
!= NULL
) && (tptr
!= NULL
)) {
161 _irq
[type
].attach(tptr
, mptr
);
162 setup_interrupt(type
);
167 virtual const struct rpc_method
*get_rpc_methods();
168 static struct rpc_class
*get_rpc_class();
173 void setup_interrupt(IrqType type
);
174 void remove_interrupt(IrqType type
);
177 virtual int _putc(int c
);
180 FunctionPointer _irq
[2];