Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ethaderu 3:78f223d34f36 1 /* mbed Microcontroller Library
ethaderu 3:78f223d34f36 2 * Copyright (c) 2006-2015 ARM Limited
ethaderu 3:78f223d34f36 3 *
ethaderu 3:78f223d34f36 4 * Licensed under the Apache License, Version 2.0 (the "License");
ethaderu 3:78f223d34f36 5 * you may not use this file except in compliance with the License.
ethaderu 3:78f223d34f36 6 * You may obtain a copy of the License at
ethaderu 3:78f223d34f36 7 *
ethaderu 3:78f223d34f36 8 * http://www.apache.org/licenses/LICENSE-2.0
ethaderu 3:78f223d34f36 9 *
ethaderu 3:78f223d34f36 10 * Unless required by applicable law or agreed to in writing, software
ethaderu 3:78f223d34f36 11 * distributed under the License is distributed on an "AS IS" BASIS,
ethaderu 3:78f223d34f36 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ethaderu 3:78f223d34f36 13 * See the License for the specific language governing permissions and
ethaderu 3:78f223d34f36 14 * limitations under the License.
ethaderu 3:78f223d34f36 15 */
ethaderu 3:78f223d34f36 16 #ifndef MBED_I2C_H
ethaderu 3:78f223d34f36 17 #define MBED_I2C_H
ethaderu 3:78f223d34f36 18
ethaderu 3:78f223d34f36 19 #include "platform.h"
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 #if DEVICE_I2C
ethaderu 3:78f223d34f36 22
ethaderu 3:78f223d34f36 23 #include "i2c_api.h"
ethaderu 3:78f223d34f36 24
ethaderu 3:78f223d34f36 25 #if DEVICE_I2C_ASYNCH
ethaderu 3:78f223d34f36 26 #include "CThunk.h"
ethaderu 3:78f223d34f36 27 #include "dma_api.h"
ethaderu 3:78f223d34f36 28 #include "FunctionPointer.h"
ethaderu 3:78f223d34f36 29 #endif
ethaderu 3:78f223d34f36 30
ethaderu 3:78f223d34f36 31 namespace mbed {
ethaderu 3:78f223d34f36 32
ethaderu 3:78f223d34f36 33 /** An I2C Master, used for communicating with I2C slave devices
ethaderu 3:78f223d34f36 34 *
ethaderu 3:78f223d34f36 35 * Example:
ethaderu 3:78f223d34f36 36 * @code
ethaderu 3:78f223d34f36 37 * // Read from I2C slave at address 0x62
ethaderu 3:78f223d34f36 38 *
ethaderu 3:78f223d34f36 39 * #include "mbed.h"
ethaderu 3:78f223d34f36 40 *
ethaderu 3:78f223d34f36 41 * I2C i2c(p28, p27);
ethaderu 3:78f223d34f36 42 *
ethaderu 3:78f223d34f36 43 * int main() {
ethaderu 3:78f223d34f36 44 * int address = 0x62;
ethaderu 3:78f223d34f36 45 * char data[2];
ethaderu 3:78f223d34f36 46 * i2c.read(address, data, 2);
ethaderu 3:78f223d34f36 47 * }
ethaderu 3:78f223d34f36 48 * @endcode
ethaderu 3:78f223d34f36 49 */
ethaderu 3:78f223d34f36 50 class I2C {
ethaderu 3:78f223d34f36 51
ethaderu 3:78f223d34f36 52 public:
ethaderu 3:78f223d34f36 53 enum RxStatus {
ethaderu 3:78f223d34f36 54 NoData,
ethaderu 3:78f223d34f36 55 MasterGeneralCall,
ethaderu 3:78f223d34f36 56 MasterWrite,
ethaderu 3:78f223d34f36 57 MasterRead
ethaderu 3:78f223d34f36 58 };
ethaderu 3:78f223d34f36 59
ethaderu 3:78f223d34f36 60 enum Acknowledge {
ethaderu 3:78f223d34f36 61 NoACK = 0,
ethaderu 3:78f223d34f36 62 ACK = 1
ethaderu 3:78f223d34f36 63 };
ethaderu 3:78f223d34f36 64
ethaderu 3:78f223d34f36 65 /** Create an I2C Master interface, connected to the specified pins
ethaderu 3:78f223d34f36 66 *
ethaderu 3:78f223d34f36 67 * @param sda I2C data line pin
ethaderu 3:78f223d34f36 68 * @param scl I2C clock line pin
ethaderu 3:78f223d34f36 69 */
ethaderu 3:78f223d34f36 70 I2C(PinName sda, PinName scl);
ethaderu 3:78f223d34f36 71
ethaderu 3:78f223d34f36 72 /** Set the frequency of the I2C interface
ethaderu 3:78f223d34f36 73 *
ethaderu 3:78f223d34f36 74 * @param hz The bus frequency in hertz
ethaderu 3:78f223d34f36 75 */
ethaderu 3:78f223d34f36 76 void frequency(int hz);
ethaderu 3:78f223d34f36 77
ethaderu 3:78f223d34f36 78 /** Read from an I2C slave
ethaderu 3:78f223d34f36 79 *
ethaderu 3:78f223d34f36 80 * Performs a complete read transaction. The bottom bit of
ethaderu 3:78f223d34f36 81 * the address is forced to 1 to indicate a read.
ethaderu 3:78f223d34f36 82 *
ethaderu 3:78f223d34f36 83 * @param address 8-bit I2C slave address [ addr | 1 ]
ethaderu 3:78f223d34f36 84 * @param data Pointer to the byte-array to read data in to
ethaderu 3:78f223d34f36 85 * @param length Number of bytes to read
ethaderu 3:78f223d34f36 86 * @param repeated Repeated start, true - don't send stop at end
ethaderu 3:78f223d34f36 87 *
ethaderu 3:78f223d34f36 88 * @returns
ethaderu 3:78f223d34f36 89 * 0 on success (ack),
ethaderu 3:78f223d34f36 90 * non-0 on failure (nack)
ethaderu 3:78f223d34f36 91 */
ethaderu 3:78f223d34f36 92 int read(int address, char *data, int length, bool repeated = false);
ethaderu 3:78f223d34f36 93
ethaderu 3:78f223d34f36 94 /** Read a single byte from the I2C bus
ethaderu 3:78f223d34f36 95 *
ethaderu 3:78f223d34f36 96 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
ethaderu 3:78f223d34f36 97 *
ethaderu 3:78f223d34f36 98 * @returns
ethaderu 3:78f223d34f36 99 * the byte read
ethaderu 3:78f223d34f36 100 */
ethaderu 3:78f223d34f36 101 int read(int ack);
ethaderu 3:78f223d34f36 102
ethaderu 3:78f223d34f36 103 /** Write to an I2C slave
ethaderu 3:78f223d34f36 104 *
ethaderu 3:78f223d34f36 105 * Performs a complete write transaction. The bottom bit of
ethaderu 3:78f223d34f36 106 * the address is forced to 0 to indicate a write.
ethaderu 3:78f223d34f36 107 *
ethaderu 3:78f223d34f36 108 * @param address 8-bit I2C slave address [ addr | 0 ]
ethaderu 3:78f223d34f36 109 * @param data Pointer to the byte-array data to send
ethaderu 3:78f223d34f36 110 * @param length Number of bytes to send
ethaderu 3:78f223d34f36 111 * @param repeated Repeated start, true - do not send stop at end
ethaderu 3:78f223d34f36 112 *
ethaderu 3:78f223d34f36 113 * @returns
ethaderu 3:78f223d34f36 114 * 0 on success (ack),
ethaderu 3:78f223d34f36 115 * non-0 on failure (nack)
ethaderu 3:78f223d34f36 116 */
ethaderu 3:78f223d34f36 117 int write(int address, const char *data, int length, bool repeated = false);
ethaderu 3:78f223d34f36 118
ethaderu 3:78f223d34f36 119 /** Write single byte out on the I2C bus
ethaderu 3:78f223d34f36 120 *
ethaderu 3:78f223d34f36 121 * @param data data to write out on bus
ethaderu 3:78f223d34f36 122 *
ethaderu 3:78f223d34f36 123 * @returns
ethaderu 3:78f223d34f36 124 * '1' if an ACK was received,
ethaderu 3:78f223d34f36 125 * '0' otherwise
ethaderu 3:78f223d34f36 126 */
ethaderu 3:78f223d34f36 127 int write(int data);
ethaderu 3:78f223d34f36 128
ethaderu 3:78f223d34f36 129 /** Creates a start condition on the I2C bus
ethaderu 3:78f223d34f36 130 */
ethaderu 3:78f223d34f36 131
ethaderu 3:78f223d34f36 132 void start(void);
ethaderu 3:78f223d34f36 133
ethaderu 3:78f223d34f36 134 /** Creates a stop condition on the I2C bus
ethaderu 3:78f223d34f36 135 */
ethaderu 3:78f223d34f36 136 void stop(void);
ethaderu 3:78f223d34f36 137
ethaderu 3:78f223d34f36 138 #if DEVICE_I2C_ASYNCH
ethaderu 3:78f223d34f36 139
ethaderu 3:78f223d34f36 140 /** Start non-blocking I2C transfer.
ethaderu 3:78f223d34f36 141 *
ethaderu 3:78f223d34f36 142 * @param address 8/10 bit I2c slave address
ethaderu 3:78f223d34f36 143 * @param tx_buffer The TX buffer with data to be transfered
ethaderu 3:78f223d34f36 144 * @param tx_length The length of TX buffer in bytes
ethaderu 3:78f223d34f36 145 * @param rx_buffer The RX buffer which is used for received data
ethaderu 3:78f223d34f36 146 * @param rx_length The length of RX buffer in bytes
ethaderu 3:78f223d34f36 147 * @param event The logical OR of events to modify
ethaderu 3:78f223d34f36 148 * @param callback The event callback function
ethaderu 3:78f223d34f36 149 * @param repeated Repeated start, true - do not send stop at end
ethaderu 3:78f223d34f36 150 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
ethaderu 3:78f223d34f36 151 */
ethaderu 3:78f223d34f36 152 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
ethaderu 3:78f223d34f36 153
ethaderu 3:78f223d34f36 154 /** Abort the on-going I2C transfer
ethaderu 3:78f223d34f36 155 */
ethaderu 3:78f223d34f36 156 void abort_transfer();
ethaderu 3:78f223d34f36 157 protected:
ethaderu 3:78f223d34f36 158 void irq_handler_asynch(void);
ethaderu 3:78f223d34f36 159 event_callback_t _callback;
ethaderu 3:78f223d34f36 160 CThunk<I2C> _irq;
ethaderu 3:78f223d34f36 161 DMAUsage _usage;
ethaderu 3:78f223d34f36 162 #endif
ethaderu 3:78f223d34f36 163
ethaderu 3:78f223d34f36 164 protected:
ethaderu 3:78f223d34f36 165 void aquire();
ethaderu 3:78f223d34f36 166
ethaderu 3:78f223d34f36 167 i2c_t _i2c;
ethaderu 3:78f223d34f36 168 static I2C *_owner;
ethaderu 3:78f223d34f36 169 int _hz;
ethaderu 3:78f223d34f36 170 };
ethaderu 3:78f223d34f36 171
ethaderu 3:78f223d34f36 172 } // namespace mbed
ethaderu 3:78f223d34f36 173
ethaderu 3:78f223d34f36 174 #endif
ethaderu 3:78f223d34f36 175
ethaderu 3:78f223d34f36 176 #endif