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