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