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