1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #ifndef MBED_I2C_SLAVE_H
17 #define MBED_I2C_SLAVE_H
27 /** An I2C Slave, used for communicating with an I2C Master device
31 * // Simple I2C responder
34 * I2CSlave slave(p9, p10);
38 * char msg[] = "Slave!";
40 * slave.address(0xA0);
42 * int i = slave.receive();
44 * case I2CSlave::ReadAddressed:
45 * slave.write(msg, strlen(msg) + 1); // Includes null char
47 * case I2CSlave::WriteGeneral:
48 * slave.read(buf, 10);
49 * printf("Read G: %s\n", buf);
51 * case I2CSlave::WriteAddressed:
52 * slave.read(buf, 10);
53 * printf("Read A: %s\n", buf);
56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
71 /** Create an I2C Slave interface, connected to the specified pins.
73 * @param sda I2C data line pin
74 * @param scl I2C clock line pin
76 I2CSlave(PinName sda, PinName scl);
78 /** Set the frequency of the I2C interface
80 * @param hz The bus frequency in hertz
82 void frequency(int hz);
84 /** Checks to see if this I2C Slave has been addressed.
87 * A status indicating if the device has been addressed, and how
88 * - NoData - the slave has not been addressed
89 * - ReadAddressed - the master has requested a read from this slave
90 * - WriteAddressed - the master is writing to this slave
91 * - WriteGeneral - the master is writing to all slave
95 /** Read from an I2C master.
97 * @param data pointer to the byte array to read data in to
98 * @param length maximum number of bytes to read
104 int read(char *data, int length);
106 /** Read a single byte from an I2C master.
113 /** Write to an I2C master.
115 * @param data pointer to the byte array to be transmitted
116 * @param length the number of bytes to transmite
122 int write(const char *data, int length);
124 /** Write a single byte to an I2C master.
126 * @data the byte to write
129 * '1' if an ACK was received,
134 /** Sets the I2C slave address.
136 * @param address The address to set for the slave (ignoring the least
137 * signifcant bit). If set to 0, the slave will only respond to the
138 * general call address.
140 void address(int address);
142 /** Reset the I2C slave back into the known ready receiving state.