Skip to content

Commit b90b13b

Browse files
author
Jamie Smith
authored
Fix a few mistakes in the I2C docs. (#66)
1 parent b010ca5 commit b90b13b

File tree

1 file changed

+15
-8
lines changed
  • drivers/include/drivers

1 file changed

+15
-8
lines changed

drivers/include/drivers/I2C.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ namespace mbed {
5858
*
5959
* All three of these APIs let you execute %I2C operations, but they work differently.
6060
*
61-
* The I2C class is thread-safe, and uses a mutex to prevent multiple threads from using it at the same time.
62-
*
6361
* <h1>Transaction-Based API</h1>
6462
*
6563
* The simplest API, which should be appropriate for most use cases, is the transaction-based API, which is
6664
* accessed through the \link I2C::read(int address, char *data, int length, bool repeated) read() \endlink and the
6765
* \link write(int address, const char *data, int length, bool repeated) write() \endlink functions. These functions
6866
* 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
7070
* size of the data ahead of time. If this information is not known, you may want to use the single-byte API instead
7171
* (see below).
7272
*
@@ -127,11 +127,13 @@ namespace mbed {
127127
* while (1) {
128128
* // read and write takes the 8-bit version of the address.
129129
* // set up configuration register (at 0x01)
130+
* i2c.lock();
130131
* i2c.start();
131132
* I2C::Result result = i2c.write_byte(addr8bit); // Write address, LSBit low to indicate write
132133
* i2c.write_byte(0x01);
133134
* i2c.write_byte(0x00);
134135
* i2c.stop();
136+
* i2c.unlock();
135137
*
136138
* if(result != I2C::ACK)
137139
* {
@@ -141,6 +143,7 @@ namespace mbed {
141143
* ThisThread::sleep_for(500);
142144
*
143145
* // Set register to read
146+
* i2c.lock();
144147
* i2c.start();
145148
* i2c.write_byte(addr8bit); // Write address
146149
* i2c.write_byte(0x00);
@@ -151,8 +154,10 @@ namespace mbed {
151154
*
152155
* // Read the two byte temperature word
153156
* 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();
156161
*
157162
* float tmp = (float(temperatureBinary) / 256.0);
158163
* printf("Temp = %.2f\n", tmp);
@@ -183,14 +188,16 @@ namespace mbed {
183188
* addresses to I2C functions. See the documentation on each function for details.
184189
*
185190
* %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
187192
* to your MCU datasheet and your target's HAL code for details. For 10-bit addresses, use the same format to
188193
* pass them to I2C functions -- shift them left by one and set the LSBit to indicate the read/write direction.
189194
* On MCUs that do not natively support 10-bit addressing, you can emulate support by using the single-byte API
190195
* to send two address bytes; see the linked page above for details.
191196
*
192197
* <h1>Other Info</h1>
193198
*
199+
* The I2C class is thread-safe, and uses a mutex to prevent multiple threads from using it at the same time.
200+
*
194201
* \warning Mbed OS requires that you only create one instance of the I2C class per physical %I2C bus on your chip.
195202
* This means that if you have multiple sensors connected together on a bus, you must create one I2C object at the
196203
* 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> {
247254
*/
248255
void frequency(int hz);
249256

250-
/** Read from an I2C slave
257+
/** Read from an %I2C slave
251258
*
252259
* Performs a complete read transaction. The least significant bit of
253260
* the address must be 1 to indicate a read.
@@ -262,7 +269,7 @@ class I2C : private NonCopyable<I2C> {
262269
*/
263270
Result read(int address, char *data, int length, bool repeated = false);
264271

265-
/** Write to an I2C slave
272+
/** Write to an %I2C slave
266273
*
267274
* Performs a complete write transaction. The least significant bit of
268275
* the address must be 0 to indicate a write.

0 commit comments

Comments
 (0)