Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/api/SerialBase.h@10:22480ac31879, 2014-11-27 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 27 09:30:36 2014 +0000
- Revision:
- 10:22480ac31879
- Parent:
- 9:05f0b5a3a70a
change to new revision hardware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #ifndef MBED_SERIALBASE_H |
yihui | 9:05f0b5a3a70a | 17 | #define MBED_SERIALBASE_H |
yihui | 9:05f0b5a3a70a | 18 | |
yihui | 9:05f0b5a3a70a | 19 | #include "platform.h" |
yihui | 9:05f0b5a3a70a | 20 | |
yihui | 9:05f0b5a3a70a | 21 | #if DEVICE_SERIAL |
yihui | 9:05f0b5a3a70a | 22 | |
yihui | 9:05f0b5a3a70a | 23 | #include "Stream.h" |
yihui | 9:05f0b5a3a70a | 24 | #include "FunctionPointer.h" |
yihui | 9:05f0b5a3a70a | 25 | #include "serial_api.h" |
yihui | 9:05f0b5a3a70a | 26 | |
yihui | 9:05f0b5a3a70a | 27 | namespace mbed { |
yihui | 9:05f0b5a3a70a | 28 | |
yihui | 9:05f0b5a3a70a | 29 | /** A base class for serial port implementations |
yihui | 9:05f0b5a3a70a | 30 | * Can't be instantiated directly (use Serial or RawSerial) |
yihui | 9:05f0b5a3a70a | 31 | */ |
yihui | 9:05f0b5a3a70a | 32 | class SerialBase { |
yihui | 9:05f0b5a3a70a | 33 | |
yihui | 9:05f0b5a3a70a | 34 | public: |
yihui | 9:05f0b5a3a70a | 35 | /** Set the baud rate of the serial port |
yihui | 9:05f0b5a3a70a | 36 | * |
yihui | 9:05f0b5a3a70a | 37 | * @param baudrate The baudrate of the serial port (default = 9600). |
yihui | 9:05f0b5a3a70a | 38 | */ |
yihui | 9:05f0b5a3a70a | 39 | void baud(int baudrate); |
yihui | 9:05f0b5a3a70a | 40 | |
yihui | 9:05f0b5a3a70a | 41 | enum Parity { |
yihui | 9:05f0b5a3a70a | 42 | None = 0, |
yihui | 9:05f0b5a3a70a | 43 | Odd, |
yihui | 9:05f0b5a3a70a | 44 | Even, |
yihui | 9:05f0b5a3a70a | 45 | Forced1, |
yihui | 9:05f0b5a3a70a | 46 | Forced0 |
yihui | 9:05f0b5a3a70a | 47 | }; |
yihui | 9:05f0b5a3a70a | 48 | |
yihui | 9:05f0b5a3a70a | 49 | enum IrqType { |
yihui | 9:05f0b5a3a70a | 50 | RxIrq = 0, |
yihui | 9:05f0b5a3a70a | 51 | TxIrq |
yihui | 9:05f0b5a3a70a | 52 | }; |
yihui | 9:05f0b5a3a70a | 53 | |
yihui | 9:05f0b5a3a70a | 54 | enum Flow { |
yihui | 9:05f0b5a3a70a | 55 | Disabled = 0, |
yihui | 9:05f0b5a3a70a | 56 | RTS, |
yihui | 9:05f0b5a3a70a | 57 | CTS, |
yihui | 9:05f0b5a3a70a | 58 | RTSCTS |
yihui | 9:05f0b5a3a70a | 59 | }; |
yihui | 9:05f0b5a3a70a | 60 | |
yihui | 9:05f0b5a3a70a | 61 | /** Set the transmission format used by the serial port |
yihui | 9:05f0b5a3a70a | 62 | * |
yihui | 9:05f0b5a3a70a | 63 | * @param bits The number of bits in a word (5-8; default = 8) |
yihui | 9:05f0b5a3a70a | 64 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
yihui | 9:05f0b5a3a70a | 65 | * @param stop The number of stop bits (1 or 2; default = 1) |
yihui | 9:05f0b5a3a70a | 66 | */ |
yihui | 9:05f0b5a3a70a | 67 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
yihui | 9:05f0b5a3a70a | 68 | |
yihui | 9:05f0b5a3a70a | 69 | /** Determine if there is a character available to read |
yihui | 9:05f0b5a3a70a | 70 | * |
yihui | 9:05f0b5a3a70a | 71 | * @returns |
yihui | 9:05f0b5a3a70a | 72 | * 1 if there is a character available to read, |
yihui | 9:05f0b5a3a70a | 73 | * 0 otherwise |
yihui | 9:05f0b5a3a70a | 74 | */ |
yihui | 9:05f0b5a3a70a | 75 | int readable(); |
yihui | 9:05f0b5a3a70a | 76 | |
yihui | 9:05f0b5a3a70a | 77 | /** Determine if there is space available to write a character |
yihui | 9:05f0b5a3a70a | 78 | * |
yihui | 9:05f0b5a3a70a | 79 | * @returns |
yihui | 9:05f0b5a3a70a | 80 | * 1 if there is space to write a character, |
yihui | 9:05f0b5a3a70a | 81 | * 0 otherwise |
yihui | 9:05f0b5a3a70a | 82 | */ |
yihui | 9:05f0b5a3a70a | 83 | int writeable(); |
yihui | 9:05f0b5a3a70a | 84 | |
yihui | 9:05f0b5a3a70a | 85 | /** Attach a function to call whenever a serial interrupt is generated |
yihui | 9:05f0b5a3a70a | 86 | * |
yihui | 9:05f0b5a3a70a | 87 | * @param fptr A pointer to a void function, or 0 to set as none |
yihui | 9:05f0b5a3a70a | 88 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
yihui | 9:05f0b5a3a70a | 89 | */ |
yihui | 9:05f0b5a3a70a | 90 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
yihui | 9:05f0b5a3a70a | 91 | |
yihui | 9:05f0b5a3a70a | 92 | /** Attach a member function to call whenever a serial interrupt is generated |
yihui | 9:05f0b5a3a70a | 93 | * |
yihui | 9:05f0b5a3a70a | 94 | * @param tptr pointer to the object to call the member function on |
yihui | 9:05f0b5a3a70a | 95 | * @param mptr pointer to the member function to be called |
yihui | 9:05f0b5a3a70a | 96 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
yihui | 9:05f0b5a3a70a | 97 | */ |
yihui | 9:05f0b5a3a70a | 98 | template<typename T> |
yihui | 9:05f0b5a3a70a | 99 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
yihui | 9:05f0b5a3a70a | 100 | if((mptr != NULL) && (tptr != NULL)) { |
yihui | 9:05f0b5a3a70a | 101 | _irq[type].attach(tptr, mptr); |
yihui | 9:05f0b5a3a70a | 102 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
yihui | 9:05f0b5a3a70a | 103 | } |
yihui | 9:05f0b5a3a70a | 104 | } |
yihui | 9:05f0b5a3a70a | 105 | |
yihui | 9:05f0b5a3a70a | 106 | /** Generate a break condition on the serial line |
yihui | 9:05f0b5a3a70a | 107 | */ |
yihui | 9:05f0b5a3a70a | 108 | void send_break(); |
yihui | 9:05f0b5a3a70a | 109 | |
yihui | 9:05f0b5a3a70a | 110 | #if DEVICE_SERIAL_FC |
yihui | 9:05f0b5a3a70a | 111 | /** Set the flow control type on the serial port |
yihui | 9:05f0b5a3a70a | 112 | * |
yihui | 9:05f0b5a3a70a | 113 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
yihui | 9:05f0b5a3a70a | 114 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
yihui | 9:05f0b5a3a70a | 115 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
yihui | 9:05f0b5a3a70a | 116 | */ |
yihui | 9:05f0b5a3a70a | 117 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
yihui | 9:05f0b5a3a70a | 118 | #endif |
yihui | 9:05f0b5a3a70a | 119 | |
yihui | 9:05f0b5a3a70a | 120 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
yihui | 9:05f0b5a3a70a | 121 | |
yihui | 9:05f0b5a3a70a | 122 | protected: |
yihui | 9:05f0b5a3a70a | 123 | SerialBase(PinName tx, PinName rx); |
yihui | 9:05f0b5a3a70a | 124 | virtual ~SerialBase() { |
yihui | 9:05f0b5a3a70a | 125 | } |
yihui | 9:05f0b5a3a70a | 126 | |
yihui | 9:05f0b5a3a70a | 127 | int _base_getc(); |
yihui | 9:05f0b5a3a70a | 128 | int _base_putc(int c); |
yihui | 9:05f0b5a3a70a | 129 | |
yihui | 9:05f0b5a3a70a | 130 | serial_t _serial; |
yihui | 9:05f0b5a3a70a | 131 | FunctionPointer _irq[2]; |
yihui | 9:05f0b5a3a70a | 132 | int _baud; |
yihui | 9:05f0b5a3a70a | 133 | }; |
yihui | 9:05f0b5a3a70a | 134 | |
yihui | 9:05f0b5a3a70a | 135 | } // namespace mbed |
yihui | 9:05f0b5a3a70a | 136 | |
yihui | 9:05f0b5a3a70a | 137 | #endif |
yihui | 9:05f0b5a3a70a | 138 | |
yihui | 9:05f0b5a3a70a | 139 | #endif |