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 responder:
38  * @code
39  * #include <mbed.h>
40  *
41  * const int SLAVE_ADDRESS = 0xA0;
42  * const char message[] = "Slave!";
43  *
44  * I2CSlave slave(I2C_SDA, I2C_SCL);
45  *
46  * int main() {
47  * slave.address(SLAVE_ADDRESS);
48  * while (1) {
49  * int operation = slave.receive();
50  * switch (operation) {
51  * case I2CSlave::ReadAddressed:
52  * int status = slave.write(message, sizeof(message));
53  * if (status == 0) {
54  * printf("Written message: %s\n", message);
55  * } else {
56  * printf("Failed to write message.\n");
57  * }
58  * break;
59  * case I2CSlave::WriteGeneral:
60  * int byte_read = slave.read();
61  * printf("Read General: %c (%d)\n", byte_read, byte_read);
62  * break;
63  * case I2CSlave::WriteAddressed:
64  * int byte_read = slave.read();
65  * printf("Read Addressed: %c (%d)\n", byte_read, byte_read);
66  * break;
67  * }
68  * }
69  * }
70  * @endcode
71  */
72 class I2CSlave {
73 
74 public:
75  enum RxStatus {
76  NoData = 0,
77  ReadAddressed = 1,
78  WriteGeneral = 2,
79  WriteAddressed = 3
80  };
81 
82  /** Create an I2C Slave interface, connected to the specified pins.
83  *
84  * @param sda I2C data line pin.
85  * @param scl I2C clock line pin.
86  */
87  I2CSlave(PinName sda, PinName scl);
88 
89  /** Create an I2C Slave interface, connected to the specified pins.
90  *
91  * @param static_pinmap reference to structure which holds static pinmap.
92  */
93  I2CSlave(const i2c_pinmap_t &static_pinmap);
94  I2CSlave(const i2c_pinmap_t &&) = delete; // prevent passing of temporary objects
95 
96  /** Set the frequency of the I2C interface.
97  *
98  * @param hz The bus frequency in Hertz.
99  */
100  void frequency(int hz);
101 
102  /** Check if this I2C Slave has been addressed.
103  *
104  * @return A status indicating if the device has been addressed and how.
105  * @retval NoData The slave has not been addressed.
106  * @retval ReadAddressed The master has requested a read from this slave.
107  * @retval WriteAddressed The master is writing to this slave.
108  * @retval WriteGeneral The master is writing to all slave.
109  */
110  int receive(void);
111 
112  /** Read specified number of bytes from an I2C master.
113  *
114  * @param data Pointer to the buffer to read data into.
115  * @param length Number of bytes to read.
116  *
117  * @return Result of the operation.
118  * @retval 0 If the number of bytes read is equal to length requested.
119  * @retval nonzero On error or if the number of bytes read is less than requested.
120  */
121  int read(char *data, int length);
122 
123  /** Read a single byte from an I2C master.
124  *
125  * @return The byte read.
126  */
127  int read(void);
128 
129  /** Write to an I2C master.
130  *
131  * @param data Pointer to the buffer containing the data to be sent.
132  * @param length Number of bytes to send.
133  *
134  * @return
135  * @retval 0 If written all bytes successfully.
136  * @retval nonzero On error or if the number of bytes written is less than requested.
137  */
138  int write(const char *data, int length);
139 
140  /** Write a single byte to an I2C master.
141  *
142  * @param data Value to write.
143  *
144  * @return Result of the operation.
145  * @retval 0 If a NACK is received.
146  * @retval 1 If an ACK is received.
147  * @retval 2 On timeout.
148  */
149  int write(int data);
150 
151  /** Set the I2C slave address.
152  *
153  * @param address The address to set for the slave (least significant bit is ignored).
154  *
155  * @note If address is set to 0, the slave will only respond to the
156  * general call address.
157  */
158  void address(int address);
159 
160  /** Reset the I2C slave back into the known ready receiving state.
161  */
162  void stop(void);
163 
164 #if !defined(DOXYGEN_ONLY)
165 
166 protected:
167  /* Internal i2c object identifying the resources */
168  i2c_t _i2c;
169 
170 #endif //!defined(DOXYGEN_ONLY)
171 };
172 
173 /** @}*/
174 
175 } // namespace mbed
176 
177 #endif
178 
179 #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:72
int read(void)
Read a single byte from an I2C master.
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.