mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Nov 20 17:24:08 2012 +0000
Revision:
0:fd0d7bdfcdc2
Child:
2:143cac498751
mbed sources

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:fd0d7bdfcdc2 1 /* mbed Microcontroller Library
mbed_official 0:fd0d7bdfcdc2 2 * Copyright (c) 2006-2012 ARM Limited
mbed_official 0:fd0d7bdfcdc2 3 *
mbed_official 0:fd0d7bdfcdc2 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbed_official 0:fd0d7bdfcdc2 5 * of this software and associated documentation files (the "Software"), to deal
mbed_official 0:fd0d7bdfcdc2 6 * in the Software without restriction, including without limitation the rights
mbed_official 0:fd0d7bdfcdc2 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed_official 0:fd0d7bdfcdc2 8 * copies of the Software, and to permit persons to whom the Software is
mbed_official 0:fd0d7bdfcdc2 9 * furnished to do so, subject to the following conditions:
mbed_official 0:fd0d7bdfcdc2 10 *
mbed_official 0:fd0d7bdfcdc2 11 * The above copyright notice and this permission notice shall be included in
mbed_official 0:fd0d7bdfcdc2 12 * all copies or substantial portions of the Software.
mbed_official 0:fd0d7bdfcdc2 13 *
mbed_official 0:fd0d7bdfcdc2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed_official 0:fd0d7bdfcdc2 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed_official 0:fd0d7bdfcdc2 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed_official 0:fd0d7bdfcdc2 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed_official 0:fd0d7bdfcdc2 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:fd0d7bdfcdc2 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mbed_official 0:fd0d7bdfcdc2 20 * SOFTWARE.
mbed_official 0:fd0d7bdfcdc2 21 */
mbed_official 0:fd0d7bdfcdc2 22 #ifndef MBED_SERIAL_H
mbed_official 0:fd0d7bdfcdc2 23 #define MBED_SERIAL_H
mbed_official 0:fd0d7bdfcdc2 24
mbed_official 0:fd0d7bdfcdc2 25 #include "platform.h"
mbed_official 0:fd0d7bdfcdc2 26
mbed_official 0:fd0d7bdfcdc2 27 #if DEVICE_SERIAL
mbed_official 0:fd0d7bdfcdc2 28
mbed_official 0:fd0d7bdfcdc2 29 #include "Stream.h"
mbed_official 0:fd0d7bdfcdc2 30 #include "FunctionPointer.h"
mbed_official 0:fd0d7bdfcdc2 31 #include "serial_api.h"
mbed_official 0:fd0d7bdfcdc2 32
mbed_official 0:fd0d7bdfcdc2 33 namespace mbed {
mbed_official 0:fd0d7bdfcdc2 34
mbed_official 0:fd0d7bdfcdc2 35 /** A serial port (UART) for communication with other serial devices
mbed_official 0:fd0d7bdfcdc2 36 *
mbed_official 0:fd0d7bdfcdc2 37 * Can be used for Full Duplex communication, or Simplex by specifying
mbed_official 0:fd0d7bdfcdc2 38 * one pin as NC (Not Connected)
mbed_official 0:fd0d7bdfcdc2 39 *
mbed_official 0:fd0d7bdfcdc2 40 * Example:
mbed_official 0:fd0d7bdfcdc2 41 * @code
mbed_official 0:fd0d7bdfcdc2 42 * // Print "Hello World" to the PC
mbed_official 0:fd0d7bdfcdc2 43 *
mbed_official 0:fd0d7bdfcdc2 44 * #include "mbed.h"
mbed_official 0:fd0d7bdfcdc2 45 *
mbed_official 0:fd0d7bdfcdc2 46 * Serial pc(USBTX, USBRX);
mbed_official 0:fd0d7bdfcdc2 47 *
mbed_official 0:fd0d7bdfcdc2 48 * int main() {
mbed_official 0:fd0d7bdfcdc2 49 * pc.printf("Hello World\n");
mbed_official 0:fd0d7bdfcdc2 50 * }
mbed_official 0:fd0d7bdfcdc2 51 * @endcode
mbed_official 0:fd0d7bdfcdc2 52 */
mbed_official 0:fd0d7bdfcdc2 53 class Serial : public Stream {
mbed_official 0:fd0d7bdfcdc2 54
mbed_official 0:fd0d7bdfcdc2 55 public:
mbed_official 0:fd0d7bdfcdc2 56
mbed_official 0:fd0d7bdfcdc2 57 /** Create a Serial port, connected to the specified transmit and receive pins
mbed_official 0:fd0d7bdfcdc2 58 *
mbed_official 0:fd0d7bdfcdc2 59 * @param tx Transmit pin
mbed_official 0:fd0d7bdfcdc2 60 * @param rx Receive pin
mbed_official 0:fd0d7bdfcdc2 61 *
mbed_official 0:fd0d7bdfcdc2 62 * @note
mbed_official 0:fd0d7bdfcdc2 63 * Either tx or rx may be specified as NC if unused
mbed_official 0:fd0d7bdfcdc2 64 */
mbed_official 0:fd0d7bdfcdc2 65 Serial(PinName tx, PinName rx) {
mbed_official 0:fd0d7bdfcdc2 66 serial_init(&_serial, tx, rx);
mbed_official 0:fd0d7bdfcdc2 67 serial_irq_handler(&_serial, Serial::_irq_handler, (uint32_t)this);
mbed_official 0:fd0d7bdfcdc2 68 }
mbed_official 0:fd0d7bdfcdc2 69
mbed_official 0:fd0d7bdfcdc2 70 /** Set the baud rate of the serial port
mbed_official 0:fd0d7bdfcdc2 71 *
mbed_official 0:fd0d7bdfcdc2 72 * @param baudrate The baudrate of the serial port (default = 9600).
mbed_official 0:fd0d7bdfcdc2 73 */
mbed_official 0:fd0d7bdfcdc2 74 void baud(int baudrate) {
mbed_official 0:fd0d7bdfcdc2 75 serial_baud(&_serial, baudrate);
mbed_official 0:fd0d7bdfcdc2 76 }
mbed_official 0:fd0d7bdfcdc2 77
mbed_official 0:fd0d7bdfcdc2 78 /** Set the transmission format used by the Serial port
mbed_official 0:fd0d7bdfcdc2 79 *
mbed_official 0:fd0d7bdfcdc2 80 * @param bits The number of bits in a word (5-8; default = 8)
mbed_official 0:fd0d7bdfcdc2 81 * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
mbed_official 0:fd0d7bdfcdc2 82 * @param stop The number of stop bits (1 or 2; default = 1)
mbed_official 0:fd0d7bdfcdc2 83 */
mbed_official 0:fd0d7bdfcdc2 84 void format(int bits = 8, SerialParity parity=ParityNone, int stop_bits=1) {
mbed_official 0:fd0d7bdfcdc2 85 serial_format(&_serial, bits, parity, stop_bits);
mbed_official 0:fd0d7bdfcdc2 86 }
mbed_official 0:fd0d7bdfcdc2 87
mbed_official 0:fd0d7bdfcdc2 88 /** Determine if there is a character available to read
mbed_official 0:fd0d7bdfcdc2 89 *
mbed_official 0:fd0d7bdfcdc2 90 * @returns
mbed_official 0:fd0d7bdfcdc2 91 * 1 if there is a character available to read,
mbed_official 0:fd0d7bdfcdc2 92 * 0 otherwise
mbed_official 0:fd0d7bdfcdc2 93 */
mbed_official 0:fd0d7bdfcdc2 94 int readable() {
mbed_official 0:fd0d7bdfcdc2 95 return serial_readable(&_serial);
mbed_official 0:fd0d7bdfcdc2 96 }
mbed_official 0:fd0d7bdfcdc2 97
mbed_official 0:fd0d7bdfcdc2 98 /** Determine if there is space available to write a character
mbed_official 0:fd0d7bdfcdc2 99 *
mbed_official 0:fd0d7bdfcdc2 100 * @returns
mbed_official 0:fd0d7bdfcdc2 101 * 1 if there is space to write a character,
mbed_official 0:fd0d7bdfcdc2 102 * 0 otherwise
mbed_official 0:fd0d7bdfcdc2 103 */
mbed_official 0:fd0d7bdfcdc2 104 int writeable() {
mbed_official 0:fd0d7bdfcdc2 105 return serial_writable(&_serial);
mbed_official 0:fd0d7bdfcdc2 106 }
mbed_official 0:fd0d7bdfcdc2 107
mbed_official 0:fd0d7bdfcdc2 108 /** Attach a function to call whenever a serial interrupt is generated
mbed_official 0:fd0d7bdfcdc2 109 *
mbed_official 0:fd0d7bdfcdc2 110 * @param fptr A pointer to a void function, or 0 to set as none
mbed_official 0:fd0d7bdfcdc2 111 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
mbed_official 0:fd0d7bdfcdc2 112 */
mbed_official 0:fd0d7bdfcdc2 113 void attach(void (*fptr)(void), SerialIrq type=RxIrq) {
mbed_official 0:fd0d7bdfcdc2 114 if (fptr) {
mbed_official 0:fd0d7bdfcdc2 115 _irq[type].attach(fptr);
mbed_official 0:fd0d7bdfcdc2 116 serial_irq_set(&_serial, type, 1);
mbed_official 0:fd0d7bdfcdc2 117 } else {
mbed_official 0:fd0d7bdfcdc2 118 serial_irq_set(&_serial, type, 0);
mbed_official 0:fd0d7bdfcdc2 119 }
mbed_official 0:fd0d7bdfcdc2 120 }
mbed_official 0:fd0d7bdfcdc2 121
mbed_official 0:fd0d7bdfcdc2 122 /** Attach a member function to call whenever a serial interrupt is generated
mbed_official 0:fd0d7bdfcdc2 123 *
mbed_official 0:fd0d7bdfcdc2 124 * @param tptr pointer to the object to call the member function on
mbed_official 0:fd0d7bdfcdc2 125 * @param mptr pointer to the member function to be called
mbed_official 0:fd0d7bdfcdc2 126 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
mbed_official 0:fd0d7bdfcdc2 127 */
mbed_official 0:fd0d7bdfcdc2 128 template<typename T>
mbed_official 0:fd0d7bdfcdc2 129 void attach(T* tptr, void (T::*mptr)(void), SerialIrq type=RxIrq) {
mbed_official 0:fd0d7bdfcdc2 130 if((mptr != NULL) && (tptr != NULL)) {
mbed_official 0:fd0d7bdfcdc2 131 _irq[type].attach(tptr, mptr);
mbed_official 0:fd0d7bdfcdc2 132 serial_irq_set(&_serial, type, 1);
mbed_official 0:fd0d7bdfcdc2 133 }
mbed_official 0:fd0d7bdfcdc2 134 }
mbed_official 0:fd0d7bdfcdc2 135
mbed_official 0:fd0d7bdfcdc2 136 static void _irq_handler(uint32_t id, SerialIrq irq_type) {
mbed_official 0:fd0d7bdfcdc2 137 Serial *handler = (Serial*)id;
mbed_official 0:fd0d7bdfcdc2 138 handler->_irq[irq_type].call();
mbed_official 0:fd0d7bdfcdc2 139 }
mbed_official 0:fd0d7bdfcdc2 140
mbed_official 0:fd0d7bdfcdc2 141 protected:
mbed_official 0:fd0d7bdfcdc2 142 virtual int _getc() {
mbed_official 0:fd0d7bdfcdc2 143 return serial_getc(&_serial);
mbed_official 0:fd0d7bdfcdc2 144 }
mbed_official 0:fd0d7bdfcdc2 145
mbed_official 0:fd0d7bdfcdc2 146 virtual int _putc(int c) {
mbed_official 0:fd0d7bdfcdc2 147 serial_putc(&_serial, c);
mbed_official 0:fd0d7bdfcdc2 148 return c;
mbed_official 0:fd0d7bdfcdc2 149 }
mbed_official 0:fd0d7bdfcdc2 150
mbed_official 0:fd0d7bdfcdc2 151 serial_t _serial;
mbed_official 0:fd0d7bdfcdc2 152 FunctionPointer _irq[2];
mbed_official 0:fd0d7bdfcdc2 153 };
mbed_official 0:fd0d7bdfcdc2 154
mbed_official 0:fd0d7bdfcdc2 155 } // namespace mbed
mbed_official 0:fd0d7bdfcdc2 156
mbed_official 0:fd0d7bdfcdc2 157 #endif
mbed_official 0:fd0d7bdfcdc2 158
mbed_official 0:fd0d7bdfcdc2 159 #endif