Mbed OS Reference
Loading...
Searching...
No Matches
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
26namespace 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 */
128class I2CSlave {
129
130public:
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 /**
169 * @brief Read bytes transmitted to this MCU from an I2C master.
170 *
171 * Call this function only once \c receive() returns WriteAddressed or WriteGeneral.
172 *
173 * @param data Pointer to the buffer to read data into.
174 * @param length Maximum number of bytes to read.
175 *
176 * @return The number of bytes read
177 */
178 int read(char *data, int length);
179
180 /** Read a single byte from an I2C master.
181 *
182 * @return The byte read.
183 */
184 int read(void);
185
186 /**
187 * @brief Write to an I2C master which is addressing this slave device.
188 *
189 * Call this function only once \c receive() returns ReadAddressed.
190 * This will send the given data bytes to the master and then send a NACK to end the read transaction.
191 *
192 * @param data Pointer to the buffer containing the data to be sent.
193 * @param length Number of bytes to send.
194 *
195 * @return
196 * @retval 0 If written all bytes successfully.
197 * @retval nonzero On error or if the number of bytes written is less than requested.
198 */
199 int write(const char *data, int length);
200
201 /** Write a single byte to an I2C master.
202 *
203 * @param data Value to write.
204 *
205 * @return Result of the operation.
206 * @retval 0 If a NACK is received.
207 * @retval 1 If an ACK is received.
208 * @retval 2 On timeout.
209 */
210 int write(int data);
211
212 /** Set the I2C slave address.
213 *
214 * @param address The 8-bit address to set for the slave (least significant bit is ignored).
215 *
216 * @note If address is set to 0, the slave will only respond to the
217 * general call address.
218 */
219 void address(int address);
220
221 /** Reset the I2C slave back into the known ready receiving state.
222 */
223 void stop(void);
224
225#if !defined(DOXYGEN_ONLY)
226
227protected:
228 /* Internal i2c object identifying the resources */
229 i2c_t _i2c;
230
231#endif //!defined(DOXYGEN_ONLY)
232};
233
234/** @}*/
235
236} // namespace mbed
237
238#endif
239
240#endif
An I2C Slave, used for communicating with an I2C Master device.
Definition: I2CSlave.h:128
I2CSlave(const i2c_pinmap_t &static_pinmap)
Create an I2C Slave interface, connected to the specified pins.
int write(int data)
Write a single byte to an I2C master.
int receive(void)
Check if this I2C Slave has been addressed.
int read(char *data, int length)
Read bytes transmitted to this MCU from an I2C master.
I2CSlave(PinName sda, PinName scl)
Create an I2C Slave interface, connected to the specified pins.
void address(int address)
Set the I2C slave address.
void stop(void)
Reset the I2C slave back into the known ready receiving state.
void frequency(int hz)
Set the frequency of the I2C interface.
int write(const char *data, int length)
Write to an I2C master which is addressing this slave device.
int read(void)
Read a single byte from an I2C master.
Asynch I2C HAL structure.
Definition: i2c_api.h:75