IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

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