mbed library sources

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Parent:
9:0ce32e54c9a7
Child:
13:0645d8841f51
Unify mbed library sources

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
emilmont 10:3bc89ef62ce7 16 #ifndef MBED_SERIAL_H
emilmont 10:3bc89ef62ce7 17 #define MBED_SERIAL_H
emilmont 10:3bc89ef62ce7 18
emilmont 10:3bc89ef62ce7 19 #include "platform.h"
emilmont 10:3bc89ef62ce7 20
emilmont 10:3bc89ef62ce7 21 #if DEVICE_SERIAL
emilmont 10:3bc89ef62ce7 22
emilmont 10:3bc89ef62ce7 23 #include "Stream.h"
emilmont 10:3bc89ef62ce7 24 #include "FunctionPointer.h"
emilmont 10:3bc89ef62ce7 25 #include "serial_api.h"
emilmont 10:3bc89ef62ce7 26
emilmont 10:3bc89ef62ce7 27 namespace mbed {
emilmont 10:3bc89ef62ce7 28
emilmont 10:3bc89ef62ce7 29 /** A serial port (UART) for communication with other serial devices
emilmont 10:3bc89ef62ce7 30 *
emilmont 10:3bc89ef62ce7 31 * Can be used for Full Duplex communication, or Simplex by specifying
emilmont 10:3bc89ef62ce7 32 * one pin as NC (Not Connected)
emilmont 10:3bc89ef62ce7 33 *
emilmont 10:3bc89ef62ce7 34 * Example:
emilmont 10:3bc89ef62ce7 35 * @code
emilmont 10:3bc89ef62ce7 36 * // Print "Hello World" to the PC
emilmont 10:3bc89ef62ce7 37 *
emilmont 10:3bc89ef62ce7 38 * #include "mbed.h"
emilmont 10:3bc89ef62ce7 39 *
emilmont 10:3bc89ef62ce7 40 * Serial pc(USBTX, USBRX);
emilmont 10:3bc89ef62ce7 41 *
emilmont 10:3bc89ef62ce7 42 * int main() {
emilmont 10:3bc89ef62ce7 43 * pc.printf("Hello World\n");
emilmont 10:3bc89ef62ce7 44 * }
emilmont 10:3bc89ef62ce7 45 * @endcode
emilmont 10:3bc89ef62ce7 46 */
emilmont 10:3bc89ef62ce7 47 class Serial : public Stream {
emilmont 10:3bc89ef62ce7 48
emilmont 10:3bc89ef62ce7 49 public:
emilmont 10:3bc89ef62ce7 50 /** Create a Serial port, connected to the specified transmit and receive pins
emilmont 10:3bc89ef62ce7 51 *
emilmont 10:3bc89ef62ce7 52 * @param tx Transmit pin
emilmont 10:3bc89ef62ce7 53 * @param rx Receive pin
emilmont 10:3bc89ef62ce7 54 *
emilmont 10:3bc89ef62ce7 55 * @note
emilmont 10:3bc89ef62ce7 56 * Either tx or rx may be specified as NC if unused
emilmont 10:3bc89ef62ce7 57 */
emilmont 10:3bc89ef62ce7 58 Serial(PinName tx, PinName rx, const char *name=NULL);
emilmont 10:3bc89ef62ce7 59
emilmont 10:3bc89ef62ce7 60 /** Set the baud rate of the serial port
emilmont 10:3bc89ef62ce7 61 *
emilmont 10:3bc89ef62ce7 62 * @param baudrate The baudrate of the serial port (default = 9600).
emilmont 10:3bc89ef62ce7 63 */
emilmont 10:3bc89ef62ce7 64 void baud(int baudrate);
emilmont 10:3bc89ef62ce7 65
emilmont 10:3bc89ef62ce7 66 enum Parity {
emilmont 10:3bc89ef62ce7 67 None = 0,
emilmont 10:3bc89ef62ce7 68 Odd,
emilmont 10:3bc89ef62ce7 69 Even,
emilmont 10:3bc89ef62ce7 70 Forced1,
emilmont 10:3bc89ef62ce7 71 Forced0
emilmont 10:3bc89ef62ce7 72 };
emilmont 10:3bc89ef62ce7 73
emilmont 10:3bc89ef62ce7 74 enum IrqType {
emilmont 10:3bc89ef62ce7 75 RxIrq = 0,
emilmont 10:3bc89ef62ce7 76 TxIrq
emilmont 10:3bc89ef62ce7 77 };
emilmont 10:3bc89ef62ce7 78
emilmont 10:3bc89ef62ce7 79 /** Set the transmission format used by the Serial port
emilmont 10:3bc89ef62ce7 80 *
emilmont 10:3bc89ef62ce7 81 * @param bits The number of bits in a word (5-8; default = 8)
emilmont 10:3bc89ef62ce7 82 * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
emilmont 10:3bc89ef62ce7 83 * @param stop The number of stop bits (1 or 2; default = 1)
emilmont 10:3bc89ef62ce7 84 */
emilmont 10:3bc89ef62ce7 85 void format(int bits = 8, Parity parity=Serial::None, int stop_bits=1);
emilmont 10:3bc89ef62ce7 86
emilmont 10:3bc89ef62ce7 87 /** Determine if there is a character available to read
emilmont 10:3bc89ef62ce7 88 *
emilmont 10:3bc89ef62ce7 89 * @returns
emilmont 10:3bc89ef62ce7 90 * 1 if there is a character available to read,
emilmont 10:3bc89ef62ce7 91 * 0 otherwise
emilmont 10:3bc89ef62ce7 92 */
emilmont 10:3bc89ef62ce7 93 int readable();
emilmont 10:3bc89ef62ce7 94
emilmont 10:3bc89ef62ce7 95 /** Determine if there is space available to write a character
emilmont 10:3bc89ef62ce7 96 *
emilmont 10:3bc89ef62ce7 97 * @returns
emilmont 10:3bc89ef62ce7 98 * 1 if there is space to write a character,
emilmont 10:3bc89ef62ce7 99 * 0 otherwise
emilmont 10:3bc89ef62ce7 100 */
emilmont 10:3bc89ef62ce7 101 int writeable();
emilmont 10:3bc89ef62ce7 102
emilmont 10:3bc89ef62ce7 103 /** Attach a function to call whenever a serial interrupt is generated
emilmont 10:3bc89ef62ce7 104 *
emilmont 10:3bc89ef62ce7 105 * @param fptr A pointer to a void function, or 0 to set as none
emilmont 10:3bc89ef62ce7 106 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 10:3bc89ef62ce7 107 */
emilmont 10:3bc89ef62ce7 108 void attach(void (*fptr)(void), IrqType type=RxIrq);
emilmont 10:3bc89ef62ce7 109
emilmont 10:3bc89ef62ce7 110 /** Attach a member function to call whenever a serial interrupt is generated
emilmont 10:3bc89ef62ce7 111 *
emilmont 10:3bc89ef62ce7 112 * @param tptr pointer to the object to call the member function on
emilmont 10:3bc89ef62ce7 113 * @param mptr pointer to the member function to be called
emilmont 10:3bc89ef62ce7 114 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 10:3bc89ef62ce7 115 */
emilmont 10:3bc89ef62ce7 116 template<typename T>
emilmont 10:3bc89ef62ce7 117 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
emilmont 10:3bc89ef62ce7 118 if((mptr != NULL) && (tptr != NULL)) {
emilmont 10:3bc89ef62ce7 119 _irq[type].attach(tptr, mptr);
emilmont 10:3bc89ef62ce7 120 serial_irq_set(&_serial, (SerialIrq)type, 1);
emilmont 10:3bc89ef62ce7 121 }
emilmont 10:3bc89ef62ce7 122 }
emilmont 10:3bc89ef62ce7 123
emilmont 10:3bc89ef62ce7 124 static void _irq_handler(uint32_t id, SerialIrq irq_type);
emilmont 10:3bc89ef62ce7 125
emilmont 10:3bc89ef62ce7 126 protected:
emilmont 10:3bc89ef62ce7 127 virtual int _getc();
emilmont 10:3bc89ef62ce7 128 virtual int _putc(int c);
emilmont 10:3bc89ef62ce7 129
emilmont 10:3bc89ef62ce7 130 serial_t _serial;
emilmont 10:3bc89ef62ce7 131 FunctionPointer _irq[2];
emilmont 10:3bc89ef62ce7 132 };
emilmont 10:3bc89ef62ce7 133
emilmont 10:3bc89ef62ce7 134 } // namespace mbed
emilmont 10:3bc89ef62ce7 135
emilmont 10:3bc89ef62ce7 136 #endif
emilmont 10:3bc89ef62ce7 137
emilmont 10:3bc89ef62ce7 138 #endif