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_I2C_H
mbotkinl 0:2cc6bb4d7fea 17 #define MBED_I2C_H
mbotkinl 0:2cc6bb4d7fea 18
mbotkinl 0:2cc6bb4d7fea 19 #include "platform.h"
mbotkinl 0:2cc6bb4d7fea 20
mbotkinl 0:2cc6bb4d7fea 21 #if DEVICE_I2C
mbotkinl 0:2cc6bb4d7fea 22
mbotkinl 0:2cc6bb4d7fea 23 #include "i2c_api.h"
mbotkinl 0:2cc6bb4d7fea 24
mbotkinl 0:2cc6bb4d7fea 25 namespace mbed {
mbotkinl 0:2cc6bb4d7fea 26
mbotkinl 0:2cc6bb4d7fea 27 /** An I2C Master, used for communicating with I2C slave devices
mbotkinl 0:2cc6bb4d7fea 28 *
mbotkinl 0:2cc6bb4d7fea 29 * Example:
mbotkinl 0:2cc6bb4d7fea 30 * @code
mbotkinl 0:2cc6bb4d7fea 31 * // Read from I2C slave at address 0x62
mbotkinl 0:2cc6bb4d7fea 32 *
mbotkinl 0:2cc6bb4d7fea 33 * #include "mbed.h"
mbotkinl 0:2cc6bb4d7fea 34 *
mbotkinl 0:2cc6bb4d7fea 35 * I2C i2c(p28, p27);
mbotkinl 0:2cc6bb4d7fea 36 *
mbotkinl 0:2cc6bb4d7fea 37 * int main() {
mbotkinl 0:2cc6bb4d7fea 38 * int address = 0x62;
mbotkinl 0:2cc6bb4d7fea 39 * char data[2];
mbotkinl 0:2cc6bb4d7fea 40 * i2c.read(address, data, 2);
mbotkinl 0:2cc6bb4d7fea 41 * }
mbotkinl 0:2cc6bb4d7fea 42 * @endcode
mbotkinl 0:2cc6bb4d7fea 43 */
mbotkinl 0:2cc6bb4d7fea 44 class I2C {
mbotkinl 0:2cc6bb4d7fea 45
mbotkinl 0:2cc6bb4d7fea 46 public:
mbotkinl 0:2cc6bb4d7fea 47 enum RxStatus {
mbotkinl 0:2cc6bb4d7fea 48 NoData,
mbotkinl 0:2cc6bb4d7fea 49 MasterGeneralCall,
mbotkinl 0:2cc6bb4d7fea 50 MasterWrite,
mbotkinl 0:2cc6bb4d7fea 51 MasterRead
mbotkinl 0:2cc6bb4d7fea 52 };
mbotkinl 0:2cc6bb4d7fea 53
mbotkinl 0:2cc6bb4d7fea 54 enum Acknowledge {
mbotkinl 0:2cc6bb4d7fea 55 NoACK = 0,
mbotkinl 0:2cc6bb4d7fea 56 ACK = 1
mbotkinl 0:2cc6bb4d7fea 57 };
mbotkinl 0:2cc6bb4d7fea 58
mbotkinl 0:2cc6bb4d7fea 59 /** Create an I2C Master interface, connected to the specified pins
mbotkinl 0:2cc6bb4d7fea 60 *
mbotkinl 0:2cc6bb4d7fea 61 * @param sda I2C data line pin
mbotkinl 0:2cc6bb4d7fea 62 * @param scl I2C clock line pin
mbotkinl 0:2cc6bb4d7fea 63 */
mbotkinl 0:2cc6bb4d7fea 64 I2C(PinName sda, PinName scl);
mbotkinl 0:2cc6bb4d7fea 65
mbotkinl 0:2cc6bb4d7fea 66 /** Set the frequency of the I2C interface
mbotkinl 0:2cc6bb4d7fea 67 *
mbotkinl 0:2cc6bb4d7fea 68 * @param hz The bus frequency in hertz
mbotkinl 0:2cc6bb4d7fea 69 */
mbotkinl 0:2cc6bb4d7fea 70 void frequency(int hz);
mbotkinl 0:2cc6bb4d7fea 71
mbotkinl 0:2cc6bb4d7fea 72 /** Read from an I2C slave
mbotkinl 0:2cc6bb4d7fea 73 *
mbotkinl 0:2cc6bb4d7fea 74 * Performs a complete read transaction. The bottom bit of
mbotkinl 0:2cc6bb4d7fea 75 * the address is forced to 1 to indicate a read.
mbotkinl 0:2cc6bb4d7fea 76 *
mbotkinl 0:2cc6bb4d7fea 77 * @param address 8-bit I2C slave address [ addr | 1 ]
mbotkinl 0:2cc6bb4d7fea 78 * @param data Pointer to the byte-array to read data in to
mbotkinl 0:2cc6bb4d7fea 79 * @param length Number of bytes to read
mbotkinl 0:2cc6bb4d7fea 80 * @param repeated Repeated start, true - don't send stop at end
mbotkinl 0:2cc6bb4d7fea 81 *
mbotkinl 0:2cc6bb4d7fea 82 * @returns
mbotkinl 0:2cc6bb4d7fea 83 * 0 on success (ack),
mbotkinl 0:2cc6bb4d7fea 84 * non-0 on failure (nack)
mbotkinl 0:2cc6bb4d7fea 85 */
mbotkinl 0:2cc6bb4d7fea 86 int read(int address, char *data, int length, bool repeated = false);
mbotkinl 0:2cc6bb4d7fea 87
mbotkinl 0:2cc6bb4d7fea 88 /** Read a single byte from the I2C bus
mbotkinl 0:2cc6bb4d7fea 89 *
mbotkinl 0:2cc6bb4d7fea 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
mbotkinl 0:2cc6bb4d7fea 91 *
mbotkinl 0:2cc6bb4d7fea 92 * @returns
mbotkinl 0:2cc6bb4d7fea 93 * the byte read
mbotkinl 0:2cc6bb4d7fea 94 */
mbotkinl 0:2cc6bb4d7fea 95 int read(int ack);
mbotkinl 0:2cc6bb4d7fea 96
mbotkinl 0:2cc6bb4d7fea 97 /** Write to an I2C slave
mbotkinl 0:2cc6bb4d7fea 98 *
mbotkinl 0:2cc6bb4d7fea 99 * Performs a complete write transaction. The bottom bit of
mbotkinl 0:2cc6bb4d7fea 100 * the address is forced to 0 to indicate a write.
mbotkinl 0:2cc6bb4d7fea 101 *
mbotkinl 0:2cc6bb4d7fea 102 * @param address 8-bit I2C slave address [ addr | 0 ]
mbotkinl 0:2cc6bb4d7fea 103 * @param data Pointer to the byte-array data to send
mbotkinl 0:2cc6bb4d7fea 104 * @param length Number of bytes to send
mbotkinl 0:2cc6bb4d7fea 105 * @param repeated Repeated start, true - do not send stop at end
mbotkinl 0:2cc6bb4d7fea 106 *
mbotkinl 0:2cc6bb4d7fea 107 * @returns
mbotkinl 0:2cc6bb4d7fea 108 * 0 on success (ack),
mbotkinl 0:2cc6bb4d7fea 109 * non-0 on failure (nack)
mbotkinl 0:2cc6bb4d7fea 110 */
mbotkinl 0:2cc6bb4d7fea 111 int write(int address, const char *data, int length, bool repeated = false);
mbotkinl 0:2cc6bb4d7fea 112
mbotkinl 0:2cc6bb4d7fea 113 /** Write single byte out on the I2C bus
mbotkinl 0:2cc6bb4d7fea 114 *
mbotkinl 0:2cc6bb4d7fea 115 * @param data data to write out on bus
mbotkinl 0:2cc6bb4d7fea 116 *
mbotkinl 0:2cc6bb4d7fea 117 * @returns
mbotkinl 0:2cc6bb4d7fea 118 * '1' if an ACK was received,
mbotkinl 0:2cc6bb4d7fea 119 * '0' otherwise
mbotkinl 0:2cc6bb4d7fea 120 */
mbotkinl 0:2cc6bb4d7fea 121 int write(int data);
mbotkinl 0:2cc6bb4d7fea 122
mbotkinl 0:2cc6bb4d7fea 123 /** Creates a start condition on the I2C bus
mbotkinl 0:2cc6bb4d7fea 124 */
mbotkinl 0:2cc6bb4d7fea 125
mbotkinl 0:2cc6bb4d7fea 126 void start(void);
mbotkinl 0:2cc6bb4d7fea 127
mbotkinl 0:2cc6bb4d7fea 128 /** Creates a stop condition on the I2C bus
mbotkinl 0:2cc6bb4d7fea 129 */
mbotkinl 0:2cc6bb4d7fea 130 void stop(void);
mbotkinl 0:2cc6bb4d7fea 131
mbotkinl 0:2cc6bb4d7fea 132 protected:
mbotkinl 0:2cc6bb4d7fea 133 void aquire();
mbotkinl 0:2cc6bb4d7fea 134
mbotkinl 0:2cc6bb4d7fea 135 i2c_t _i2c;
mbotkinl 0:2cc6bb4d7fea 136 static I2C *_owner;
mbotkinl 0:2cc6bb4d7fea 137 int _hz;
mbotkinl 0:2cc6bb4d7fea 138 };
mbotkinl 0:2cc6bb4d7fea 139
mbotkinl 0:2cc6bb4d7fea 140 } // namespace mbed
mbotkinl 0:2cc6bb4d7fea 141
mbotkinl 0:2cc6bb4d7fea 142 #endif
mbotkinl 0:2cc6bb4d7fea 143
mbotkinl 0:2cc6bb4d7fea 144 #endif