mbed
Dependents: DHTSensor_Test K64F_eCompass_OneNET_JW
I2CSlave.h@0:2cc6bb4d7fea, 2015-02-25 (annotated)
- 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?
User | Revision | Line number | New 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_SLAVE_H |
mbotkinl | 0:2cc6bb4d7fea | 17 | #define MBED_I2C_SLAVE_H |
mbotkinl | 0:2cc6bb4d7fea | 18 | |
mbotkinl | 0:2cc6bb4d7fea | 19 | #include "platform.h" |
mbotkinl | 0:2cc6bb4d7fea | 20 | |
mbotkinl | 0:2cc6bb4d7fea | 21 | #if DEVICE_I2CSLAVE |
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 Slave, used for communicating with an I2C Master device |
mbotkinl | 0:2cc6bb4d7fea | 28 | * |
mbotkinl | 0:2cc6bb4d7fea | 29 | * Example: |
mbotkinl | 0:2cc6bb4d7fea | 30 | * @code |
mbotkinl | 0:2cc6bb4d7fea | 31 | * // Simple I2C responder |
mbotkinl | 0:2cc6bb4d7fea | 32 | * #include <mbed.h> |
mbotkinl | 0:2cc6bb4d7fea | 33 | * |
mbotkinl | 0:2cc6bb4d7fea | 34 | * I2CSlave slave(p9, p10); |
mbotkinl | 0:2cc6bb4d7fea | 35 | * |
mbotkinl | 0:2cc6bb4d7fea | 36 | * int main() { |
mbotkinl | 0:2cc6bb4d7fea | 37 | * char buf[10]; |
mbotkinl | 0:2cc6bb4d7fea | 38 | * char msg[] = "Slave!"; |
mbotkinl | 0:2cc6bb4d7fea | 39 | * |
mbotkinl | 0:2cc6bb4d7fea | 40 | * slave.address(0xA0); |
mbotkinl | 0:2cc6bb4d7fea | 41 | * while (1) { |
mbotkinl | 0:2cc6bb4d7fea | 42 | * int i = slave.receive(); |
mbotkinl | 0:2cc6bb4d7fea | 43 | * switch (i) { |
mbotkinl | 0:2cc6bb4d7fea | 44 | * case I2CSlave::ReadAddressed: |
mbotkinl | 0:2cc6bb4d7fea | 45 | * slave.write(msg, strlen(msg) + 1); // Includes null char |
mbotkinl | 0:2cc6bb4d7fea | 46 | * break; |
mbotkinl | 0:2cc6bb4d7fea | 47 | * case I2CSlave::WriteGeneral: |
mbotkinl | 0:2cc6bb4d7fea | 48 | * slave.read(buf, 10); |
mbotkinl | 0:2cc6bb4d7fea | 49 | * printf("Read G: %s\n", buf); |
mbotkinl | 0:2cc6bb4d7fea | 50 | * break; |
mbotkinl | 0:2cc6bb4d7fea | 51 | * case I2CSlave::WriteAddressed: |
mbotkinl | 0:2cc6bb4d7fea | 52 | * slave.read(buf, 10); |
mbotkinl | 0:2cc6bb4d7fea | 53 | * printf("Read A: %s\n", buf); |
mbotkinl | 0:2cc6bb4d7fea | 54 | * break; |
mbotkinl | 0:2cc6bb4d7fea | 55 | * } |
mbotkinl | 0:2cc6bb4d7fea | 56 | * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer |
mbotkinl | 0:2cc6bb4d7fea | 57 | * } |
mbotkinl | 0:2cc6bb4d7fea | 58 | * } |
mbotkinl | 0:2cc6bb4d7fea | 59 | * @endcode |
mbotkinl | 0:2cc6bb4d7fea | 60 | */ |
mbotkinl | 0:2cc6bb4d7fea | 61 | class I2CSlave { |
mbotkinl | 0:2cc6bb4d7fea | 62 | |
mbotkinl | 0:2cc6bb4d7fea | 63 | public: |
mbotkinl | 0:2cc6bb4d7fea | 64 | enum RxStatus { |
mbotkinl | 0:2cc6bb4d7fea | 65 | NoData = 0, |
mbotkinl | 0:2cc6bb4d7fea | 66 | ReadAddressed = 1, |
mbotkinl | 0:2cc6bb4d7fea | 67 | WriteGeneral = 2, |
mbotkinl | 0:2cc6bb4d7fea | 68 | WriteAddressed = 3 |
mbotkinl | 0:2cc6bb4d7fea | 69 | }; |
mbotkinl | 0:2cc6bb4d7fea | 70 | |
mbotkinl | 0:2cc6bb4d7fea | 71 | /** Create an I2C Slave interface, connected to the specified pins. |
mbotkinl | 0:2cc6bb4d7fea | 72 | * |
mbotkinl | 0:2cc6bb4d7fea | 73 | * @param sda I2C data line pin |
mbotkinl | 0:2cc6bb4d7fea | 74 | * @param scl I2C clock line pin |
mbotkinl | 0:2cc6bb4d7fea | 75 | */ |
mbotkinl | 0:2cc6bb4d7fea | 76 | I2CSlave(PinName sda, PinName scl); |
mbotkinl | 0:2cc6bb4d7fea | 77 | |
mbotkinl | 0:2cc6bb4d7fea | 78 | /** Set the frequency of the I2C interface |
mbotkinl | 0:2cc6bb4d7fea | 79 | * |
mbotkinl | 0:2cc6bb4d7fea | 80 | * @param hz The bus frequency in hertz |
mbotkinl | 0:2cc6bb4d7fea | 81 | */ |
mbotkinl | 0:2cc6bb4d7fea | 82 | void frequency(int hz); |
mbotkinl | 0:2cc6bb4d7fea | 83 | |
mbotkinl | 0:2cc6bb4d7fea | 84 | /** Checks to see if this I2C Slave has been addressed. |
mbotkinl | 0:2cc6bb4d7fea | 85 | * |
mbotkinl | 0:2cc6bb4d7fea | 86 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 87 | * A status indicating if the device has been addressed, and how |
mbotkinl | 0:2cc6bb4d7fea | 88 | * - NoData - the slave has not been addressed |
mbotkinl | 0:2cc6bb4d7fea | 89 | * - ReadAddressed - the master has requested a read from this slave |
mbotkinl | 0:2cc6bb4d7fea | 90 | * - WriteAddressed - the master is writing to this slave |
mbotkinl | 0:2cc6bb4d7fea | 91 | * - WriteGeneral - the master is writing to all slave |
mbotkinl | 0:2cc6bb4d7fea | 92 | */ |
mbotkinl | 0:2cc6bb4d7fea | 93 | int receive(void); |
mbotkinl | 0:2cc6bb4d7fea | 94 | |
mbotkinl | 0:2cc6bb4d7fea | 95 | /** Read from an I2C master. |
mbotkinl | 0:2cc6bb4d7fea | 96 | * |
mbotkinl | 0:2cc6bb4d7fea | 97 | * @param data pointer to the byte array to read data in to |
mbotkinl | 0:2cc6bb4d7fea | 98 | * @param length maximum number of bytes to read |
mbotkinl | 0:2cc6bb4d7fea | 99 | * |
mbotkinl | 0:2cc6bb4d7fea | 100 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 101 | * 0 on success, |
mbotkinl | 0:2cc6bb4d7fea | 102 | * non-0 otherwise |
mbotkinl | 0:2cc6bb4d7fea | 103 | */ |
mbotkinl | 0:2cc6bb4d7fea | 104 | int read(char *data, int length); |
mbotkinl | 0:2cc6bb4d7fea | 105 | |
mbotkinl | 0:2cc6bb4d7fea | 106 | /** Read a single byte from an I2C master. |
mbotkinl | 0:2cc6bb4d7fea | 107 | * |
mbotkinl | 0:2cc6bb4d7fea | 108 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 109 | * the byte read |
mbotkinl | 0:2cc6bb4d7fea | 110 | */ |
mbotkinl | 0:2cc6bb4d7fea | 111 | int read(void); |
mbotkinl | 0:2cc6bb4d7fea | 112 | |
mbotkinl | 0:2cc6bb4d7fea | 113 | /** Write to an I2C master. |
mbotkinl | 0:2cc6bb4d7fea | 114 | * |
mbotkinl | 0:2cc6bb4d7fea | 115 | * @param data pointer to the byte array to be transmitted |
mbotkinl | 0:2cc6bb4d7fea | 116 | * @param length the number of bytes to transmite |
mbotkinl | 0:2cc6bb4d7fea | 117 | * |
mbotkinl | 0:2cc6bb4d7fea | 118 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 119 | * 0 on success, |
mbotkinl | 0:2cc6bb4d7fea | 120 | * non-0 otherwise |
mbotkinl | 0:2cc6bb4d7fea | 121 | */ |
mbotkinl | 0:2cc6bb4d7fea | 122 | int write(const char *data, int length); |
mbotkinl | 0:2cc6bb4d7fea | 123 | |
mbotkinl | 0:2cc6bb4d7fea | 124 | /** Write a single byte to an I2C master. |
mbotkinl | 0:2cc6bb4d7fea | 125 | * |
mbotkinl | 0:2cc6bb4d7fea | 126 | * @data the byte to write |
mbotkinl | 0:2cc6bb4d7fea | 127 | * |
mbotkinl | 0:2cc6bb4d7fea | 128 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 129 | * '1' if an ACK was received, |
mbotkinl | 0:2cc6bb4d7fea | 130 | * '0' otherwise |
mbotkinl | 0:2cc6bb4d7fea | 131 | */ |
mbotkinl | 0:2cc6bb4d7fea | 132 | int write(int data); |
mbotkinl | 0:2cc6bb4d7fea | 133 | |
mbotkinl | 0:2cc6bb4d7fea | 134 | /** Sets the I2C slave address. |
mbotkinl | 0:2cc6bb4d7fea | 135 | * |
mbotkinl | 0:2cc6bb4d7fea | 136 | * @param address The address to set for the slave (ignoring the least |
mbotkinl | 0:2cc6bb4d7fea | 137 | * signifcant bit). If set to 0, the slave will only respond to the |
mbotkinl | 0:2cc6bb4d7fea | 138 | * general call address. |
mbotkinl | 0:2cc6bb4d7fea | 139 | */ |
mbotkinl | 0:2cc6bb4d7fea | 140 | void address(int address); |
mbotkinl | 0:2cc6bb4d7fea | 141 | |
mbotkinl | 0:2cc6bb4d7fea | 142 | /** Reset the I2C slave back into the known ready receiving state. |
mbotkinl | 0:2cc6bb4d7fea | 143 | */ |
mbotkinl | 0:2cc6bb4d7fea | 144 | void stop(void); |
mbotkinl | 0:2cc6bb4d7fea | 145 | |
mbotkinl | 0:2cc6bb4d7fea | 146 | protected: |
mbotkinl | 0:2cc6bb4d7fea | 147 | i2c_t _i2c; |
mbotkinl | 0:2cc6bb4d7fea | 148 | }; |
mbotkinl | 0:2cc6bb4d7fea | 149 | |
mbotkinl | 0:2cc6bb4d7fea | 150 | } // namespace mbed |
mbotkinl | 0:2cc6bb4d7fea | 151 | |
mbotkinl | 0:2cc6bb4d7fea | 152 | #endif |
mbotkinl | 0:2cc6bb4d7fea | 153 | |
mbotkinl | 0:2cc6bb4d7fea | 154 | #endif |