@@ -58,15 +58,15 @@ namespace mbed {
58
58
*
59
59
* All three of these APIs let you execute %I2C operations, but they work differently.
60
60
*
61
- * The I2C class is thread-safe, and uses a mutex to prevent multiple threads from using it at the same time.
62
- *
63
61
* <h1>Transaction-Based API</h1>
64
62
*
65
63
* The simplest API, which should be appropriate for most use cases, is the transaction-based API, which is
66
64
* accessed through the \link I2C::read(int address, char *data, int length, bool repeated) read() \endlink and the
67
65
* \link write(int address, const char *data, int length, bool repeated) write() \endlink functions. These functions
68
66
* execute an entire %I2C transaction (the start condition, address, data bytes, and stop condition) in a single
69
- * function call. The bytes to be read/written are passed in through an array, which requires that you know the
67
+ * function call.
68
+ *
69
+ * The bytes to be read/written are passed in through an array, which requires that you can predict the
70
70
* size of the data ahead of time. If this information is not known, you may want to use the single-byte API instead
71
71
* (see below).
72
72
*
@@ -127,11 +127,13 @@ namespace mbed {
127
127
* while (1) {
128
128
* // read and write takes the 8-bit version of the address.
129
129
* // set up configuration register (at 0x01)
130
+ * i2c.lock();
130
131
* i2c.start();
131
132
* I2C::Result result = i2c.write_byte(addr8bit); // Write address, LSBit low to indicate write
132
133
* i2c.write_byte(0x01);
133
134
* i2c.write_byte(0x00);
134
135
* i2c.stop();
136
+ * i2c.unlock();
135
137
*
136
138
* if(result != I2C::ACK)
137
139
* {
@@ -141,6 +143,7 @@ namespace mbed {
141
143
* ThisThread::sleep_for(500);
142
144
*
143
145
* // Set register to read
146
+ * i2c.lock();
144
147
* i2c.start();
145
148
* i2c.write_byte(addr8bit); // Write address
146
149
* i2c.write_byte(0x00);
@@ -151,8 +154,10 @@ namespace mbed {
151
154
*
152
155
* // Read the two byte temperature word
153
156
* uint16_t temperatureBinary = 0;
154
- * temperatureBinary |= static_cast<uint16_t>(i2c.read_byte()) << 8;
155
- * temperatureBinary |= static_cast<uint16_t>(i2c.read_byte());
157
+ * temperatureBinary |= static_cast<uint16_t>(i2c.read_byte(true)) << 8;
158
+ * temperatureBinary |= static_cast<uint16_t>(i2c.read_byte(false)); // send NACK to indicate last byte
159
+ * i2c.stop();
160
+ * i2c.unlock();
156
161
*
157
162
* float tmp = (float(temperatureBinary) / 256.0);
158
163
* printf("Temp = %.2f\n", tmp);
@@ -183,14 +188,16 @@ namespace mbed {
183
188
* addresses to I2C functions. See the documentation on each function for details.
184
189
*
185
190
* %I2C also has a <a href="https://www.i2c-bus.org/addressing/10-bit-addressing/">10-bit addressing mode</a>, where
186
- * the address is sent in two logical bytes on the bus. Some, but not all, Mbed targets support this mode -- refer
191
+ * the address is sent in two physical bytes on the bus. Some, but not all, Mbed targets support this mode -- refer
187
192
* to your MCU datasheet and your target's HAL code for details. For 10-bit addresses, use the same format to
188
193
* pass them to I2C functions -- shift them left by one and set the LSBit to indicate the read/write direction.
189
194
* On MCUs that do not natively support 10-bit addressing, you can emulate support by using the single-byte API
190
195
* to send two address bytes; see the linked page above for details.
191
196
*
192
197
* <h1>Other Info</h1>
193
198
*
199
+ * The I2C class is thread-safe, and uses a mutex to prevent multiple threads from using it at the same time.
200
+ *
194
201
* \warning Mbed OS requires that you only create one instance of the I2C class per physical %I2C bus on your chip.
195
202
* This means that if you have multiple sensors connected together on a bus, you must create one I2C object at the
196
203
* top level and pass it in to the drivers for each sensor. Violating this directive will cause undefined
@@ -247,7 +254,7 @@ class I2C : private NonCopyable<I2C> {
247
254
*/
248
255
void frequency (int hz);
249
256
250
- /* * Read from an I2C slave
257
+ /* * Read from an % I2C slave
251
258
*
252
259
* Performs a complete read transaction. The least significant bit of
253
260
* the address must be 1 to indicate a read.
@@ -262,7 +269,7 @@ class I2C : private NonCopyable<I2C> {
262
269
*/
263
270
Result read (int address, char *data, int length, bool repeated = false );
264
271
265
- /* * Write to an I2C slave
272
+ /* * Write to an % I2C slave
266
273
*
267
274
* Performs a complete write transaction. The least significant bit of
268
275
* the address must be 0 to indicate a write.
0 commit comments