mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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