mbed libraries for KL25Z
Serial.h@8:c14af7958ef5, 2012-11-09 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 09 11:33:53 2012 +0000
- Revision:
- 8:c14af7958ef5
- Parent:
- 7:73c5efe92a6c
- Child:
- 9:663789d7729f
SPI driver; ADC driver; DAC driver; microlib support; general bugfixing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 2:e9a661555b58 | 1 | /* mbed Microcontroller Library - Serial |
emilmont | 2:e9a661555b58 | 2 | * Copyright (c) 2007-2011 ARM Limited. All rights reserved. |
emilmont | 7:73c5efe92a6c | 3 | */ |
emilmont | 2:e9a661555b58 | 4 | #ifndef MBED_SERIAL_H |
emilmont | 2:e9a661555b58 | 5 | #define MBED_SERIAL_H |
emilmont | 2:e9a661555b58 | 6 | |
emilmont | 8:c14af7958ef5 | 7 | #include "platform.h" |
emilmont | 2:e9a661555b58 | 8 | |
emilmont | 2:e9a661555b58 | 9 | #if DEVICE_SERIAL |
emilmont | 2:e9a661555b58 | 10 | |
emilmont | 2:e9a661555b58 | 11 | #include "Stream.h" |
emilmont | 2:e9a661555b58 | 12 | #include "FunctionPointer.h" |
emilmont | 7:73c5efe92a6c | 13 | #include "serial_api.h" |
emilmont | 2:e9a661555b58 | 14 | |
emilmont | 2:e9a661555b58 | 15 | namespace mbed { |
emilmont | 2:e9a661555b58 | 16 | |
emilmont | 8:c14af7958ef5 | 17 | /** A serial port (UART) for communication with other serial devices |
emilmont | 2:e9a661555b58 | 18 | * |
emilmont | 2:e9a661555b58 | 19 | * Can be used for Full Duplex communication, or Simplex by specifying |
emilmont | 2:e9a661555b58 | 20 | * one pin as NC (Not Connected) |
emilmont | 2:e9a661555b58 | 21 | * |
emilmont | 2:e9a661555b58 | 22 | * Example: |
emilmont | 8:c14af7958ef5 | 23 | * @code |
emilmont | 8:c14af7958ef5 | 24 | * // Print "Hello World" to the PC |
emilmont | 8:c14af7958ef5 | 25 | * |
emilmont | 8:c14af7958ef5 | 26 | * #include "mbed.h" |
emilmont | 8:c14af7958ef5 | 27 | * |
emilmont | 8:c14af7958ef5 | 28 | * Serial pc(USBTX, USBRX); |
emilmont | 8:c14af7958ef5 | 29 | * |
emilmont | 8:c14af7958ef5 | 30 | * int main() { |
emilmont | 8:c14af7958ef5 | 31 | * pc.printf("Hello World\n"); |
emilmont | 8:c14af7958ef5 | 32 | * } |
emilmont | 8:c14af7958ef5 | 33 | * @endcode |
emilmont | 2:e9a661555b58 | 34 | */ |
emilmont | 2:e9a661555b58 | 35 | class Serial : public Stream { |
emilmont | 2:e9a661555b58 | 36 | |
emilmont | 2:e9a661555b58 | 37 | public: |
emilmont | 8:c14af7958ef5 | 38 | |
emilmont | 8:c14af7958ef5 | 39 | /** Create a Serial port, connected to the specified transmit and receive pins |
emilmont | 2:e9a661555b58 | 40 | * |
emilmont | 8:c14af7958ef5 | 41 | * @param tx Transmit pin |
emilmont | 8:c14af7958ef5 | 42 | * @param rx Receive pin |
emilmont | 2:e9a661555b58 | 43 | * |
emilmont | 8:c14af7958ef5 | 44 | * @note |
emilmont | 8:c14af7958ef5 | 45 | * Either tx or rx may be specified as NC if unused |
emilmont | 2:e9a661555b58 | 46 | */ |
emilmont | 8:c14af7958ef5 | 47 | Serial(PinName tx, PinName rx); |
emilmont | 2:e9a661555b58 | 48 | |
emilmont | 8:c14af7958ef5 | 49 | /** Set the baud rate of the serial port |
emilmont | 2:e9a661555b58 | 50 | * |
emilmont | 8:c14af7958ef5 | 51 | * @param baudrate The baudrate of the serial port (default = 9600). |
emilmont | 2:e9a661555b58 | 52 | */ |
emilmont | 2:e9a661555b58 | 53 | void baud(int baudrate); |
emilmont | 2:e9a661555b58 | 54 | |
emilmont | 8:c14af7958ef5 | 55 | /** Set the transmission format used by the Serial port |
emilmont | 2:e9a661555b58 | 56 | * |
emilmont | 8:c14af7958ef5 | 57 | * @param bits The number of bits in a word (5-8; default = 8) |
emilmont | 8:c14af7958ef5 | 58 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
emilmont | 8:c14af7958ef5 | 59 | * @param stop The number of stop bits (1 or 2; default = 1) |
emilmont | 2:e9a661555b58 | 60 | */ |
emilmont | 8:c14af7958ef5 | 61 | void format(int bits = 8, SerialParity parity=ParityNone, int stop_bits=1); |
emilmont | 2:e9a661555b58 | 62 | |
emilmont | 8:c14af7958ef5 | 63 | /** Determine if there is a character available to read |
emilmont | 2:e9a661555b58 | 64 | * |
emilmont | 8:c14af7958ef5 | 65 | * @returns |
emilmont | 8:c14af7958ef5 | 66 | * 1 if there is a character available to read, |
emilmont | 8:c14af7958ef5 | 67 | * 0 otherwise |
emilmont | 2:e9a661555b58 | 68 | */ |
emilmont | 2:e9a661555b58 | 69 | int readable(); |
emilmont | 2:e9a661555b58 | 70 | |
emilmont | 8:c14af7958ef5 | 71 | /** Determine if there is space available to write a character |
emilmont | 2:e9a661555b58 | 72 | * |
emilmont | 8:c14af7958ef5 | 73 | * @returns |
emilmont | 8:c14af7958ef5 | 74 | * 1 if there is space to write a character, |
emilmont | 8:c14af7958ef5 | 75 | * 0 otherwise |
emilmont | 2:e9a661555b58 | 76 | */ |
emilmont | 2:e9a661555b58 | 77 | int writeable(); |
emilmont | 2:e9a661555b58 | 78 | |
emilmont | 8:c14af7958ef5 | 79 | /** Attach a function to call whenever a serial interrupt is generated |
emilmont | 2:e9a661555b58 | 80 | * |
emilmont | 8:c14af7958ef5 | 81 | * @param fptr A pointer to a void function, or 0 to set as none |
emilmont | 8:c14af7958ef5 | 82 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
emilmont | 2:e9a661555b58 | 83 | */ |
emilmont | 7:73c5efe92a6c | 84 | void attach(void (*fptr)(void), SerialIrq type=RxIrq); |
emilmont | 2:e9a661555b58 | 85 | |
emilmont | 8:c14af7958ef5 | 86 | /** Attach a member function to call whenever a serial interrupt is generated |
emilmont | 2:e9a661555b58 | 87 | * |
emilmont | 8:c14af7958ef5 | 88 | * @param tptr pointer to the object to call the member function on |
emilmont | 8:c14af7958ef5 | 89 | * @param mptr pointer to the member function to be called |
emilmont | 8:c14af7958ef5 | 90 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
emilmont | 2:e9a661555b58 | 91 | */ |
emilmont | 2:e9a661555b58 | 92 | template<typename T> |
emilmont | 7:73c5efe92a6c | 93 | void attach(T* tptr, void (T::*mptr)(void), SerialIrq type=RxIrq) { |
emilmont | 2:e9a661555b58 | 94 | if((mptr != NULL) && (tptr != NULL)) { |
emilmont | 2:e9a661555b58 | 95 | _irq[type].attach(tptr, mptr); |
emilmont | 7:73c5efe92a6c | 96 | serial_irq_set(&_serial, type, 1); |
emilmont | 2:e9a661555b58 | 97 | } |
emilmont | 2:e9a661555b58 | 98 | } |
emilmont | 7:73c5efe92a6c | 99 | |
emilmont | 7:73c5efe92a6c | 100 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
emilmont | 2:e9a661555b58 | 101 | |
emilmont | 2:e9a661555b58 | 102 | protected: |
emilmont | 2:e9a661555b58 | 103 | virtual int _getc(); |
emilmont | 2:e9a661555b58 | 104 | virtual int _putc(int c); |
emilmont | 7:73c5efe92a6c | 105 | |
emilmont | 7:73c5efe92a6c | 106 | serial_object _serial; |
emilmont | 2:e9a661555b58 | 107 | FunctionPointer _irq[2]; |
emilmont | 2:e9a661555b58 | 108 | }; |
emilmont | 2:e9a661555b58 | 109 | |
emilmont | 2:e9a661555b58 | 110 | } // namespace mbed |
emilmont | 2:e9a661555b58 | 111 | |
emilmont | 2:e9a661555b58 | 112 | #endif |
emilmont | 2:e9a661555b58 | 113 | |
emilmont | 2:e9a661555b58 | 114 | #endif |