Mistake on this page?
Report an issue in GitHub or email us
I2CSlave.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_I2C_SLAVE_H
18 #define MBED_I2C_SLAVE_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_I2CSLAVE || defined(DOXYGEN_ONLY)
23 
24 #include "hal/i2c_api.h"
25 
26 namespace mbed {
27 /**
28  * \defgroup drivers_I2CSlave I2CSlave class
29  * \ingroup drivers-public-api-i2c
30  * @{
31  */
32 
33 /** An I2C Slave, used for communicating with an I2C Master device.
34  *
35  * @note Synchronization level: Not protected
36  *
37  * Example Simple I2C slave and master (requires two Mbed-boards):
38  * @code
39  * #include <mbed.h>
40  * #include <mbed_wait_api.h>
41  * #include <string.h>
42  *
43  * #define BUILD_I2C_SLAVE 1 // Build for slave or master of this example
44  *
45  * #define SLAVE_ADDR 0xA0
46  * #define BUFFER_SIZE 6
47  *
48  * #if BUILD_I2C_SLAVE
49  *
50  * // Slave side of the example
51  *
52  * #if !DEVICE_I2CSLAVE
53  * #error [NOT_SUPPORTED] I2C Slave is not supported
54  * #endif
55  *
56  * I2CSlave slave(I2C_SDA, I2C_SCL);
57  *
58  * int main() {
59  *
60  * char buf[BUFFER_SIZE] = "ABCDE";
61  *
62  * slave.address(SLAVE_ADDR);
63  * while (1) {
64  * int i = slave.receive();
65  * switch (i) {
66  * case I2CSlave::ReadAddressed:
67  * // Write back the buffer from the master
68  * slave.write(buf, BUFFER_SIZE);
69  * printf("Written to master (addressed): %s\n", buf);
70  * break;
71  *
72  * case I2CSlave::WriteGeneral:
73  * slave.read(buf, BUFFER_SIZE);
74  * printf("Read from master (general): %s\n", buf);
75  * break;
76  *
77  * case I2CSlave::WriteAddressed:
78  * slave.read(buf, BUFFER_SIZE);
79  * printf("Read from master (addressed): %s\n", buf);
80  * break;
81  * }
82  * }
83  * }
84  *
85  * #else
86  *
87  * // Master side of the example
88  *
89  * I2C master(I2C_SDA, I2C_SCL);
90  *
91  * static const char* to_send[] = { "abcde", "12345", "EFGHI" };
92  *
93  * int main() {
94  * char buf[BUFFER_SIZE];
95  * int send_index = 0;
96  *
97  * while (1) {
98  * strcpy(buf, to_send[send_index]);
99  *
100  * // Write the new message to the slave
101  * if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE)) {
102  * printf("Failed to write to slave!\n");
103  * } else {
104  * printf("Written to slave: %s\n", buf);
105  * }
106  *
107  * // Read what the slave has (should be identical)
108  * if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE)) {
109  * printf("Failed to read from slave!\n");
110  * } else {
111  * printf("Read from slave: %s\n", buf);
112  * }
113  *
114  * // Change the message we're writing to the slave
115  * send_index++;
116  * if (send_index > 2) {
117  * send_index = 0;
118  * }
119  *
120  * wait_us(500000); // Wait 0.5s
121  * }
122  * }
123  *
124  * #endif
125  *
126  * @endcode
127  */
128 class I2CSlave {
129 
130 public:
131  enum RxStatus {
132  NoData = 0,
133  ReadAddressed = 1,
134  WriteGeneral = 2,
135  WriteAddressed = 3
136  };
137 
138  /** Create an I2C Slave interface, connected to the specified pins.
139  *
140  * @param sda I2C data line pin.
141  * @param scl I2C clock line pin.
142  */
143  I2CSlave(PinName sda, PinName scl);
144 
145  /** Create an I2C Slave interface, connected to the specified pins.
146  *
147  * @param static_pinmap reference to structure which holds static pinmap.
148  */
149  I2CSlave(const i2c_pinmap_t &static_pinmap);
150  I2CSlave(const i2c_pinmap_t &&) = delete; // prevent passing of temporary objects
151 
152  /** Set the frequency of the I2C interface.
153  *
154  * @param hz The bus frequency in Hertz.
155  */
156  void frequency(int hz);
157 
158  /** Check if this I2C Slave has been addressed.
159  *
160  * @return A status indicating if the device has been addressed and how.
161  * @retval NoData The slave has not been addressed.
162  * @retval ReadAddressed The master has requested a read from this slave.
163  * @retval WriteAddressed The master is writing to this slave.
164  * @retval WriteGeneral The master is writing to all slave.
165  */
166  int receive(void);
167 
168  /** Read specified number of bytes from an I2C master.
169  *
170  * @param data Pointer to the buffer to read data into.
171  * @param length Number of bytes to read.
172  *
173  * @return Result of the operation.
174  * @retval 0 If the number of bytes read is equal to length requested.
175  * @retval nonzero On error or if the number of bytes read is less than requested.
176  */
177  int read(char *data, int length);
178 
179  /** Read a single byte from an I2C master.
180  *
181  * @return The byte read.
182  */
183  int read(void);
184 
185  /** Write to an I2C master.
186  *
187  * @param data Pointer to the buffer containing the data to be sent.
188  * @param length Number of bytes to send.
189  *
190  * @return
191  * @retval 0 If written all bytes successfully.
192  * @retval nonzero On error or if the number of bytes written is less than requested.
193  */
194  int write(const char *data, int length);
195 
196  /** Write a single byte to an I2C master.
197  *
198  * @param data Value to write.
199  *
200  * @return Result of the operation.
201  * @retval 0 If a NACK is received.
202  * @retval 1 If an ACK is received.
203  * @retval 2 On timeout.
204  */
205  int write(int data);
206 
207  /** Set the I2C slave address.
208  *
209  * @param address The address to set for the slave (least significant bit is ignored).
210  *
211  * @note If address is set to 0, the slave will only respond to the
212  * general call address.
213  */
214  void address(int address);
215 
216  /** Reset the I2C slave back into the known ready receiving state.
217  */
218  void stop(void);
219 
220 #if !defined(DOXYGEN_ONLY)
221 
222 protected:
223  /* Internal i2c object identifying the resources */
224  i2c_t _i2c;
225 
226 #endif //!defined(DOXYGEN_ONLY)
227 };
228 
229 /** @}*/
230 
231 } // namespace mbed
232 
233 #endif
234 
235 #endif
I2CSlave(PinName sda, PinName scl)
Create an I2C Slave interface, connected to the specified pins.
void frequency(int hz)
Set the frequency of the I2C interface.
void address(int address)
Set the I2C slave address.
int write(const char *data, int length)
Write to an I2C master.
Asynch I2C HAL structure.
Definition: i2c_api.h:49
void stop(void)
Reset the I2C slave back into the known ready receiving state.
An I2C Slave, used for communicating with an I2C Master device.
Definition: I2CSlave.h:128
int read(void)
Read a single byte from an I2C master.
Definition: ATHandler.h:46
int receive(void)
Check if this I2C Slave has been addressed.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.