I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhon309 0:ac8863619623 1 /* mbed Microcontroller Library
jhon309 0:ac8863619623 2 * Copyright (c) 2006-2013 ARM Limited
jhon309 0:ac8863619623 3 *
jhon309 0:ac8863619623 4 * Licensed under the Apache License, Version 2.0 (the "License");
jhon309 0:ac8863619623 5 * you may not use this file except in compliance with the License.
jhon309 0:ac8863619623 6 * You may obtain a copy of the License at
jhon309 0:ac8863619623 7 *
jhon309 0:ac8863619623 8 * http://www.apache.org/licenses/LICENSE-2.0
jhon309 0:ac8863619623 9 *
jhon309 0:ac8863619623 10 * Unless required by applicable law or agreed to in writing, software
jhon309 0:ac8863619623 11 * distributed under the License is distributed on an "AS IS" BASIS,
jhon309 0:ac8863619623 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jhon309 0:ac8863619623 13 * See the License for the specific language governing permissions and
jhon309 0:ac8863619623 14 * limitations under the License.
jhon309 0:ac8863619623 15 */
jhon309 0:ac8863619623 16 #ifndef MBED_I2C_SLAVE_H
jhon309 0:ac8863619623 17 #define MBED_I2C_SLAVE_H
jhon309 0:ac8863619623 18
jhon309 0:ac8863619623 19 #include "platform.h"
jhon309 0:ac8863619623 20
jhon309 0:ac8863619623 21 #if DEVICE_I2CSLAVE
jhon309 0:ac8863619623 22
jhon309 0:ac8863619623 23 #include "i2c_api.h"
jhon309 0:ac8863619623 24
jhon309 0:ac8863619623 25 namespace mbed {
jhon309 0:ac8863619623 26
jhon309 0:ac8863619623 27 /** An I2C Slave, used for communicating with an I2C Master device
jhon309 0:ac8863619623 28 *
jhon309 0:ac8863619623 29 * Example:
jhon309 0:ac8863619623 30 * @code
jhon309 0:ac8863619623 31 * // Simple I2C responder
jhon309 0:ac8863619623 32 * #include <mbed.h>
jhon309 0:ac8863619623 33 *
jhon309 0:ac8863619623 34 * I2CSlave slave(p9, p10);
jhon309 0:ac8863619623 35 *
jhon309 0:ac8863619623 36 * int main() {
jhon309 0:ac8863619623 37 * char buf[10];
jhon309 0:ac8863619623 38 * char msg[] = "Slave!";
jhon309 0:ac8863619623 39 *
jhon309 0:ac8863619623 40 * slave.address(0xA0);
jhon309 0:ac8863619623 41 * while (1) {
jhon309 0:ac8863619623 42 * int i = slave.receive();
jhon309 0:ac8863619623 43 * switch (i) {
jhon309 0:ac8863619623 44 * case I2CSlave::ReadAddressed:
jhon309 0:ac8863619623 45 * slave.write(msg, strlen(msg) + 1); // Includes null char
jhon309 0:ac8863619623 46 * break;
jhon309 0:ac8863619623 47 * case I2CSlave::WriteGeneral:
jhon309 0:ac8863619623 48 * slave.read(buf, 10);
jhon309 0:ac8863619623 49 * printf("Read G: %s\n", buf);
jhon309 0:ac8863619623 50 * break;
jhon309 0:ac8863619623 51 * case I2CSlave::WriteAddressed:
jhon309 0:ac8863619623 52 * slave.read(buf, 10);
jhon309 0:ac8863619623 53 * printf("Read A: %s\n", buf);
jhon309 0:ac8863619623 54 * break;
jhon309 0:ac8863619623 55 * }
jhon309 0:ac8863619623 56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
jhon309 0:ac8863619623 57 * }
jhon309 0:ac8863619623 58 * }
jhon309 0:ac8863619623 59 * @endcode
jhon309 0:ac8863619623 60 */
jhon309 0:ac8863619623 61 class I2CSlave {
jhon309 0:ac8863619623 62
jhon309 0:ac8863619623 63 public:
jhon309 0:ac8863619623 64 enum RxStatus {
jhon309 0:ac8863619623 65 NoData = 0,
jhon309 0:ac8863619623 66 ReadAddressed = 1,
jhon309 0:ac8863619623 67 WriteGeneral = 2,
jhon309 0:ac8863619623 68 WriteAddressed = 3
jhon309 0:ac8863619623 69 };
jhon309 0:ac8863619623 70
jhon309 0:ac8863619623 71 /** Create an I2C Slave interface, connected to the specified pins.
jhon309 0:ac8863619623 72 *
jhon309 0:ac8863619623 73 * @param sda I2C data line pin
jhon309 0:ac8863619623 74 * @param scl I2C clock line pin
jhon309 0:ac8863619623 75 */
jhon309 0:ac8863619623 76 I2CSlave(PinName sda, PinName scl);
jhon309 0:ac8863619623 77
jhon309 0:ac8863619623 78 /** Set the frequency of the I2C interface
jhon309 0:ac8863619623 79 *
jhon309 0:ac8863619623 80 * @param hz The bus frequency in hertz
jhon309 0:ac8863619623 81 */
jhon309 0:ac8863619623 82 void frequency(int hz);
jhon309 0:ac8863619623 83
jhon309 0:ac8863619623 84 /** Checks to see if this I2C Slave has been addressed.
jhon309 0:ac8863619623 85 *
jhon309 0:ac8863619623 86 * @returns
jhon309 0:ac8863619623 87 * A status indicating if the device has been addressed, and how
jhon309 0:ac8863619623 88 * - NoData - the slave has not been addressed
jhon309 0:ac8863619623 89 * - ReadAddressed - the master has requested a read from this slave
jhon309 0:ac8863619623 90 * - WriteAddressed - the master is writing to this slave
jhon309 0:ac8863619623 91 * - WriteGeneral - the master is writing to all slave
jhon309 0:ac8863619623 92 */
jhon309 0:ac8863619623 93 int receive(void);
jhon309 0:ac8863619623 94
jhon309 0:ac8863619623 95 /** Read from an I2C master.
jhon309 0:ac8863619623 96 *
jhon309 0:ac8863619623 97 * @param data pointer to the byte array to read data in to
jhon309 0:ac8863619623 98 * @param length maximum number of bytes to read
jhon309 0:ac8863619623 99 *
jhon309 0:ac8863619623 100 * @returns
jhon309 0:ac8863619623 101 * 0 on success,
jhon309 0:ac8863619623 102 * non-0 otherwise
jhon309 0:ac8863619623 103 */
jhon309 0:ac8863619623 104 int read(char *data, int length);
jhon309 0:ac8863619623 105
jhon309 0:ac8863619623 106 /** Read a single byte from an I2C master.
jhon309 0:ac8863619623 107 *
jhon309 0:ac8863619623 108 * @returns
jhon309 0:ac8863619623 109 * the byte read
jhon309 0:ac8863619623 110 */
jhon309 0:ac8863619623 111 int read(void);
jhon309 0:ac8863619623 112
jhon309 0:ac8863619623 113 /** Write to an I2C master.
jhon309 0:ac8863619623 114 *
jhon309 0:ac8863619623 115 * @param data pointer to the byte array to be transmitted
jhon309 0:ac8863619623 116 * @param length the number of bytes to transmite
jhon309 0:ac8863619623 117 *
jhon309 0:ac8863619623 118 * @returns
jhon309 0:ac8863619623 119 * 0 on success,
jhon309 0:ac8863619623 120 * non-0 otherwise
jhon309 0:ac8863619623 121 */
jhon309 0:ac8863619623 122 int write(const char *data, int length);
jhon309 0:ac8863619623 123
jhon309 0:ac8863619623 124 /** Write a single byte to an I2C master.
jhon309 0:ac8863619623 125 *
jhon309 0:ac8863619623 126 * @data the byte to write
jhon309 0:ac8863619623 127 *
jhon309 0:ac8863619623 128 * @returns
jhon309 0:ac8863619623 129 * '1' if an ACK was received,
jhon309 0:ac8863619623 130 * '0' otherwise
jhon309 0:ac8863619623 131 */
jhon309 0:ac8863619623 132 int write(int data);
jhon309 0:ac8863619623 133
jhon309 0:ac8863619623 134 /** Sets the I2C slave address.
jhon309 0:ac8863619623 135 *
jhon309 0:ac8863619623 136 * @param address The address to set for the slave (ignoring the least
jhon309 0:ac8863619623 137 * signifcant bit). If set to 0, the slave will only respond to the
jhon309 0:ac8863619623 138 * general call address.
jhon309 0:ac8863619623 139 */
jhon309 0:ac8863619623 140 void address(int address);
jhon309 0:ac8863619623 141
jhon309 0:ac8863619623 142 /** Reset the I2C slave back into the known ready receiving state.
jhon309 0:ac8863619623 143 */
jhon309 0:ac8863619623 144 void stop(void);
jhon309 0:ac8863619623 145
jhon309 0:ac8863619623 146 protected:
jhon309 0:ac8863619623 147 i2c_t _i2c;
jhon309 0:ac8863619623 148 };
jhon309 0:ac8863619623 149
jhon309 0:ac8863619623 150 } // namespace mbed
jhon309 0:ac8863619623 151
jhon309 0:ac8863619623 152 #endif
jhon309 0:ac8863619623 153
jhon309 0:ac8863619623 154 #endif