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 #include "platform/NonCopyable.h"
26 
27 namespace mbed {
28 /**
29  * \defgroup drivers_I2CSlave I2CSlave class
30  * \ingroup drivers-public-api-i2c
31  * @{
32  */
33 
34 /** An I2C Slave, used for communicating with an I2C Master device.
35  *
36  * @note Synchronization level: Not protected
37  *
38  * Example Simple I2C responder:
39  * @code
40  * #include <mbed.h>
41  *
42  * const int SLAVE_ADDRESS = 0xA0;
43  * const char message[] = "Slave!";
44  *
45  * I2CSlave slave(I2C_SDA, I2C_SCL);
46  *
47  * int main() {
48  * slave.address(SLAVE_ADDRESS);
49  * while (1) {
50  * int operation = slave.receive();
51  * switch (operation) {
52  * case I2CSlave::ReadAddressed:
53  * int status = slave.write(message, sizeof(message));
54  * if (status == 0) {
55  * printf("Written message: %s\n", message);
56  * } else {
57  * printf("Failed to write message.\n");
58  * }
59  * break;
60  * case I2CSlave::WriteGeneral:
61  * int byte_read = slave.read();
62  * printf("Read General: %c (%d)\n", byte_read, byte_read);
63  * break;
64  * case I2CSlave::WriteAddressed:
65  * int byte_read = slave.read();
66  * printf("Read Addressed: %c (%d)\n", byte_read, byte_read);
67  * break;
68  * }
69  * }
70  * }
71  * @endcode
72  */
73 class I2CSlave : private NonCopyable<I2CSlave> {
74 
75 public:
76  enum RxStatus {
77  NoData = 0,
78  ReadAddressed = 1,
79  WriteGeneral = 2,
80  WriteAddressed = 3
81  };
82 
83  /** Create an I2C Slave interface, connected to the specified pins.
84  *
85  * @param sda I2C data line pin.
86  * @param scl I2C clock line pin.
87  */
88  I2CSlave(PinName sda, PinName scl);
89 
90  virtual ~I2CSlave();
91 
92  /** Set the frequency of the I2C interface.
93  *
94  * @param hz The bus frequency in Hertz.
95  */
96  void frequency(int hz);
97 
98  /** Configure the timeout duration in microseconds for blocking transmission
99  *
100  * @param timeout Transmission timeout in microseconds.
101  *
102  * @note If no timeout is set the default timeout is used.
103  * Default timeout value is based on I2C frequency.
104  * Byte timeout is computed as triple amount of time it would take
105  * to send 10bit over I2C and is expressed by the formula:
106  * byte_timeout = 3 * (1/frequency * 10 * 1000000)
107  */
108  void timeout(uint32_t timeout);
109 
110  /** Check if this I2C Slave has been addressed.
111  *
112  * @return A status indicating if the device has been addressed and how.
113  * @retval NoData The slave has not been addressed.
114  * @retval ReadAddressed The master has requested a read from this slave.
115  * @retval WriteAddressed The master is writing to this slave.
116  * @retval WriteGeneral The master is writing to all slave.
117  */
118  int receive(void);
119 
120  /** Read specified number of bytes from an I2C master.
121  *
122  * @param data Pointer to the buffer to read data into.
123  * @param length Number of bytes to read.
124  *
125  * @return Result of the operation.
126  * @retval 0 If the number of bytes read is equal to length requested.
127  * @retval nonzero On error or if the number of bytes read is less than requested.
128  */
129  int read(char *data, int length);
130 
131  /** Read a single byte from an I2C master.
132  *
133  * @return The byte read.
134  */
135  int read(void);
136 
137  /** Write to an I2C master.
138  *
139  * @param data Pointer to the buffer containing the data to be sent.
140  * @param length Number of bytes to send.
141  *
142  * @return
143  * @retval 0 If written all bytes successfully.
144  * @retval nonzero On error or if the number of bytes written is less than requested.
145  */
146  int write(const char *data, int length);
147 
148  /** Write a single byte to an I2C master.
149  *
150  * @param data Value to write.
151  *
152  * @return Result of the operation.
153  * @retval 0 If a NACK is received.
154  * @retval 1 If an ACK is received.
155  * @retval 2 On timeout.
156  */
157  int write(int data);
158 
159  /** Set the I2C slave address.
160  *
161  * @param address The address to set for the slave (least significant bit is ignored).
162  *
163  * @note If address is set to 0, the slave will only respond to the
164  * general call address.
165  */
166  void address(int address);
167 
168  /** Reset the I2C slave back into the known ready receiving state.
169  */
170  void stop(void);
171 
172 #if !defined(DOXYGEN_ONLY)
173 
174 protected:
175  PinName _sda;
176  PinName _scl;
177  /* Internal i2c object identifying the resources */
178  i2c_t _i2c;
179 
180 #endif //!defined(DOXYGEN_ONLY)
181 };
182 
183 /** @}*/
184 
185 } // namespace mbed
186 
187 #endif
188 
189 #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.
I2C HAL structure.
Definition: i2c_api.h:146
int write(const char *data, int length)
Write to an I2C master.
void stop(void)
Reset the I2C slave back into the known ready receiving state.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
An I2C Slave, used for communicating with an I2C Master device.
Definition: I2CSlave.h:73
void timeout(uint32_t timeout)
Configure the timeout duration in microseconds for blocking transmission.
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.