DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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