2 * Originally from mbed Microcontroller Library
3 * Copyright (c) 2006-2013 ARM Limited
4 * Copyright (c) 2014 Intel Corporation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 /** An I2C Slave, used for communicating with an I2C Master device
32 * // Simple I2C responder
35 * I2CSlave slave(p9, p10);
39 * char msg[] = "Slave!";
41 * slave.address(0xA0);
43 * int i = slave.receive();
45 * case I2CSlave::ReadAddressed:
46 * slave.write(msg, strlen(msg) + 1); // Includes null char
48 * case I2CSlave::WriteGeneral:
49 * slave.read(buf, 10);
50 * printf("Read G: %s\n", buf);
52 * case I2CSlave::WriteAddressed:
53 * slave.read(buf, 10);
54 * printf("Read A: %s\n", buf);
57 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
72 /** Create an I2C Slave interface, connected to the specified pins.
74 * @param sda I2C data line pin
75 * @param scl I2C clock line pin
77 I2CSlave(unsigned int sda, unsigned int scl);
79 /** Set the frequency of the I2C interface
81 * @param hz The bus frequency in hertz
83 void frequency(int hz);
85 /** Checks to see if this I2C Slave has been addressed.
88 * A status indicating if the device has been addressed, and how
89 * - NoData - the slave has not been addressed
90 * - ReadAddressed - the master has requested a read from this slave
91 * - WriteAddressed - the master is writing to this slave
92 * - WriteGeneral - the master is writing to all slave
96 /** Read from an I2C master.
98 * @param data pointer to the byte array to read data in to
99 * @param length maximum number of bytes to read
105 int read(char *data, int length);
107 /** Read a single byte from an I2C master.
114 /** Write to an I2C master.
116 * @param data pointer to the byte array to be transmitted
117 * @param length the number of bytes to transmite
123 int write(const char *data, int length);
125 /** Write a single byte to an I2C master.
127 * @data the byte to write
130 * '1' if an ACK was received,
135 /** Sets the I2C slave address.
137 * @param address The address to set for the slave (ignoring the least
138 * signifcant bit). If set to 0, the slave will only respond to the
139 * general call address.
141 void address(int address);
143 /** Reset the I2C slave back into the known ready receiving state.