Skip to content

Commit 19c9108

Browse files
Naveen KajeNaveen Kaje
authored andcommitted
I2C.h: Update documentation, example and formatting
Change the example to a more elaborate implementation. Fix formatting and update documentation.
1 parent acabbeb commit 19c9108

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

drivers/I2C.h

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,32 @@ namespace mbed {
4141
*
4242
* Example:
4343
* @code
44-
* // Read from I2C slave at address 0x62
45-
*
44+
* Read temperature from LM75BD
4645
* #include "mbed.h"
47-
*
48-
* I2C i2c(p28, p27);
46+
* I2C i2c(I2C_SDA , I2C_SCL);
47+
* const int addr7bit = 0x48; // 7 bit I2C address
48+
* const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90
4949
*
5050
* int main() {
51-
* int address = 0x62;
52-
* char data[2];
53-
* i2c.read(address, data, 2);
51+
* char cmd[2];
52+
* while (1) {
53+
* cmd[0] = 0x01;
54+
* cmd[1] = 0x00;
55+
*
56+
* // read and write takes the 8 bit version of the address.
57+
* // set up configuration register (at 0x01)
58+
* i2c.write(addr8bit, cmd, 2);
59+
*
60+
* wait(0.5);
61+
*
62+
* // read temperature register
63+
* cmd[0] = 0x00;
64+
* i2c.write(addr8bit, cmd, 1);
65+
* i2c.read( addr8bit, cmd, 2);
66+
*
67+
* float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
68+
* printf("Temp = %.2f\n", tmp);
69+
* }
5470
* }
5571
* @endcode
5672
* @ingroup drivers
@@ -92,10 +108,11 @@ class I2C : private NonCopyable<I2C> {
92108
* @param data Pointer to the byte-array to read data in to
93109
* @param length Number of bytes to read
94110
* @param repeated Repeated start, true - don't send stop at end
111+
* default value is false.
95112
*
96113
* @returns
97114
* 0 on success (ack),
98-
* non-0 on failure (nack)
115+
* non-zero on failure (nack)
99116
*/
100117
int read(int address, char *data, int length, bool repeated = false);
101118

@@ -117,10 +134,11 @@ class I2C : private NonCopyable<I2C> {
117134
* @param data Pointer to the byte-array data to send
118135
* @param length Number of bytes to send
119136
* @param repeated Repeated start, true - do not send stop at end
137+
* default value is false.
120138
*
121139
* @returns
122140
* 0 on success (ack),
123-
* non-0 on failure (nack)
141+
* non-zero on failure (nack)
124142
*/
125143
int write(int address, const char *data, int length, bool repeated = false);
126144

@@ -137,7 +155,6 @@ class I2C : private NonCopyable<I2C> {
137155

138156
/** Creates a start condition on the I2C bus
139157
*/
140-
141158
void start(void);
142159

143160
/** Creates a stop condition on the I2C bus
@@ -171,7 +188,9 @@ class I2C : private NonCopyable<I2C> {
171188
* @param event The logical OR of events to modify
172189
* @param callback The event callback function
173190
* @param repeated Repeated start, true - do not send stop at end
174-
* @return Zero if the transfer has started, or -1 if I2C peripheral is busy
191+
* default value is false.
192+
*
193+
* @returns Zero if the transfer has started, or -1 if I2C peripheral is busy
175194
*/
176195
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);
177196

@@ -212,7 +231,7 @@ class I2C : private NonCopyable<I2C> {
212231
* @param sda I2C data line pin
213232
* @param scl I2C clock line pin
214233
*
215-
* @returns:
234+
* @returns
216235
* '0' - Successfully recovered
217236
* 'I2C_ERROR_BUS_BUSY' - In case of failure
218237
*

0 commit comments

Comments
 (0)