mbed library sources

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/Serial.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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