customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

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