Mistake on this page?
Report an issue in GitHub or email us
I2C.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_H
18 #define MBED_I2C_H
19 
20 #include "platform/platform.h"
21 #include "hal/gpio_api.h"
22 
23 #if DEVICE_I2C || defined(DOXYGEN_ONLY)
24 
25 #include "hal/i2c_api.h"
26 #include "platform/SingletonPtr.h"
27 #include "platform/PlatformMutex.h"
28 #include "platform/NonCopyable.h"
29 
30 #if DEVICE_I2C_ASYNCH
31 #include "platform/CThunk.h"
32 #include "hal/dma_api.h"
33 #include "platform/Callback.h"
34 #endif
35 
36 namespace mbed {
37 /** \defgroup drivers-public-api-i2c I2C
38  * \ingroup drivers-public-api
39  */
40 
41 /**
42  * \defgroup drivers_I2C I2C class
43  * \ingroup drivers-public-api-i2c
44  * @{
45  */
46 
47 /** An I2C Master, used for communicating with I2C slave devices
48  *
49  * @note Synchronization level: Thread safe
50  *
51  * Example:
52  * @code
53  * Read temperature from LM75BD
54  * #include "mbed.h"
55  * I2C i2c(I2C_SDA , I2C_SCL);
56  * const int addr7bit = 0x48; // 7-bit I2C address
57  * const int addr8bit = 0x48 << 1; // 8-bit I2C address, 0x90
58  *
59  * int main() {
60  * char cmd[2];
61  * while (1) {
62  * cmd[0] = 0x01;
63  * cmd[1] = 0x00;
64  *
65  * // read and write takes the 8-bit version of the address.
66  * // set up configuration register (at 0x01)
67  * i2c.write(addr8bit, cmd, 2);
68  *
69  * wait(0.5);
70  *
71  * // read temperature register
72  * cmd[0] = 0x00;
73  * i2c.write(addr8bit, cmd, 1);
74  * i2c.read( addr8bit, cmd, 2);
75  *
76  * float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
77  * printf("Temp = %.2f\n", tmp);
78  * }
79  * }
80  * @endcode
81  */
82 class I2C : private NonCopyable<I2C> {
83 
84 public:
85  enum RxStatus {
86  NoData,
87  MasterGeneralCall,
88  MasterWrite,
89  MasterRead
90  };
91 
92  enum Acknowledge {
93  NoACK = 0,
94  ACK = 1
95  };
96 
97  /** Create an I2C Master interface, connected to the specified pins
98  *
99  * @param sda I2C data line pin
100  * @param scl I2C clock line pin
101  */
102  I2C(PinName sda, PinName scl);
103 
104  virtual ~I2C();
105 
106  /** Set the frequency of the I2C interface
107  *
108  * @param hz The bus frequency in hertz
109  */
110  void frequency(int hz);
111 
112  /** Configure the timeout duration in microseconds for blocking transmission
113  *
114  * @param timeout Transmission timeout in microseconds.
115  *
116  * @note If no timeout is set the default timeout is used.
117  * Default timeout value is based on I2C frequency.
118  * Byte timeout is computed as triple amount of time it would take
119  * to send 10bit over I2C and is expressed by the formula:
120  * byte_timeout = 3 * (1/frequency * 10 * 1000000)
121  */
122  void timeout(uint32_t timeout);
123 
124  /** Read from an I2C slave
125  *
126  * Performs a complete read transaction. The bottom bit of
127  * the address is forced to 1 to indicate a read.
128  *
129  * @param address 8-bit I2C slave address [ addr | 1 ]
130  * @param data Pointer to the byte-array to read data in to
131  * @param length Number of bytes to read
132  * @param repeated Repeated start, true - don't send stop at end
133  * default value is false.
134  *
135  * @returns
136  * 0 on success (ack),
137  * nonzero on failure (nack)
138  */
139  int read(int address, char *data, int length, bool repeated = false);
140 
141  /** Read a single byte from the I2C bus
142  *
143  * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
144  *
145  * @returns
146  * the byte read
147  */
148  int read(int ack);
149 
150  /** Write to an I2C slave
151  *
152  * Performs a complete write transaction. The bottom bit of
153  * the address is forced to 0 to indicate a write.
154  *
155  * @param address 8-bit I2C slave address [ addr | 0 ]
156  * @param data Pointer to the byte-array data to send
157  * @param length Number of bytes to send
158  * @param repeated Repeated start, true - do not send stop at end
159  * default value is false.
160  *
161  * @returns
162  * 0 on success (ack),
163  * nonzero on failure (nack)
164  */
165  int write(int address, const char *data, int length, bool repeated = false);
166 
167  /** Write single byte out on the I2C bus
168  *
169  * @param data data to write out on bus
170  *
171  * @returns
172  * '0' - NAK was received
173  * '1' - ACK was received,
174  * '2' - timeout
175  */
176  int write(int data);
177 
178  /** Creates a start condition on the I2C bus
179  */
180  void start(void);
181 
182  /** Creates a stop condition on the I2C bus
183  */
184  void stop(void);
185 
186  /** Acquire exclusive access to this I2C bus
187  */
188  virtual void lock(void);
189 
190  /** Release exclusive access to this I2C bus
191  */
192  virtual void unlock(void);
193 
194 #if DEVICE_I2C_ASYNCH
195 
196  /** Start nonblocking I2C transfer.
197  *
198  * This function locks the deep sleep until any event has occurred
199  *
200  * @param address 8/10 bit I2C slave address
201  * @param tx_buffer The TX buffer with data to be transferred
202  * @param tx_length The length of TX buffer in bytes
203  * @param rx_buffer The RX buffer, which is used for received data
204  * @param rx_length The length of RX buffer in bytes
205  * @param event The logical OR of events to modify
206  * @param callback The event callback function
207  * @param repeated Repeated start, true - do not send stop at end
208  * default value is false.
209  *
210  * @returns Zero if the transfer has started, or -1 if I2C peripheral is busy
211  */
212  int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
213 
214  /** Abort the ongoing I2C transfer
215  */
216  void abort_transfer();
217 
218 #if !defined(DOXYGEN_ONLY)
219 protected:
220  /** Lock deep sleep only if it is not yet locked */
221  void lock_deep_sleep();
222 
223  /** Unlock deep sleep only if it has been locked */
224  void unlock_deep_sleep();
225 
226  static void irq_handler_asynch(i2c_t *obj, i2c_async_event_t *event, void *ctx);
227  event_callback_t _callback;
228  bool _deep_sleep_locked;
229  bool _async_transfer_ongoing;
230 #endif
231 #endif
232 
233 #if !defined(DOXYGEN_ONLY)
234 protected:
235  void aquire();
236 
237  i2c_t _i2c;
238  static I2C *_owner;
239  uint32_t _hz;
240  static SingletonPtr<PlatformMutex> _mutex;
241  PinName _sda;
242  PinName _scl;
243 
244 private:
245  /** Recover I2C bus, when stuck with SDA low
246  * @note : Initialization of I2C bus is required after this API.
247  *
248  * @param sda I2C data line pin
249  * @param scl I2C clock line pin
250  *
251  * @returns
252  * '0' - Successfully recovered
253  * 'I2C_ERROR_BUS_BUSY' - In case of failure
254  *
255  */
256  int recover(PinName sda, PinName scl);
257 #endif
258 };
259 
260 /** @}*/
261 
262 } // namespace mbed
263 
264 #endif
265 
266 #endif
void frequency(int hz)
Set the frequency of the I2C interface.
virtual void lock(void)
Acquire exclusive access to this I2C bus.
int write(int address, const char *data, int length, bool repeated=false)
Write to an I2C slave.
I2C HAL structure.
Definition: i2c_api.h:146
int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event=I2C_EVENT_TRANSFER_COMPLETE, bool repeated=false)
Start nonblocking I2C transfer.
An I2C Master, used for communicating with I2C slave devices.
Definition: I2C.h:82
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
void start(void)
Creates a start condition on the I2C bus.
Structure describing the status of async transfer.
Definition: i2c_api.h:127
void stop(void)
Creates a stop condition on the I2C bus.
void timeout(uint32_t timeout)
Configure the timeout duration in microseconds for blocking transmission.
void abort_transfer()
Abort the ongoing I2C transfer.
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:709
int read(int address, char *data, int length, bool repeated=false)
Read from an I2C slave.
virtual void unlock(void)
Release exclusive access to this I2C bus.
I2C(PinName sda, PinName scl)
Create an I2C Master interface, connected to the specified pins.
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.