25
25
namespace mbed {
26
26
/* * \addtogroup drivers */
27
27
28
- /* * An I2C Slave, used for communicating with an I2C Master device
28
+ /* * An I2C Slave, used for communicating with an I2C Master device.
29
29
*
30
30
* @note Synchronization level: Not protected
31
31
*
32
- * Example:
32
+ * Example Simple I2C responder :
33
33
* @code
34
- * // Simple I2C responder
35
34
* #include <mbed.h>
36
35
*
37
- * I2CSlave slave(p9, p10);
36
+ * const int SLAVE_ADDRESS = 0xA0;
37
+ * const char message[] = "Slave!";
38
38
*
39
- * int main() {
40
- * char buf[10];
41
- * char msg[] = "Slave!";
39
+ * I2CSlave slave(I2C_SDA, I2C_SCL);
42
40
*
43
- * slave.address(0xA0);
41
+ * int main() {
42
+ * slave.address(SLAVE_ADDRESS);
44
43
* while (1) {
45
- * int i = slave.receive();
46
- * switch (i ) {
44
+ * int operation = slave.receive();
45
+ * switch (operation ) {
47
46
* case I2CSlave::ReadAddressed:
48
- * slave.write(msg, strlen(msg) + 1); // Includes null char
47
+ * int status = slave.write(message, sizeof(message));
48
+ * if (status == 0) {
49
+ * printf("Written message: %s\n", message);
50
+ * } else {
51
+ * printf("Failed to write message.\n");
52
+ * }
49
53
* break;
50
54
* case I2CSlave::WriteGeneral:
51
- * slave.read(buf, 10 );
52
- * printf("Read G : %s \n", buf );
55
+ * int byte_read = slave.read();
56
+ * printf("Read General : %c (%d) \n", byte_read, byte_read );
53
57
* break;
54
58
* case I2CSlave::WriteAddressed:
55
- * slave.read(buf, 10 );
56
- * printf("Read A : %s \n", buf );
59
+ * int byte_read = slave.read();
60
+ * printf("Read Addressed : %c (%d) \n", byte_read, byte_read );
57
61
* break;
58
62
* }
59
- * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
60
63
* }
61
64
* }
62
65
* @endcode
@@ -74,71 +77,71 @@ class I2CSlave {
74
77
75
78
/* * Create an I2C Slave interface, connected to the specified pins.
76
79
*
77
- * @param sda I2C data line pin
78
- * @param scl I2C clock line pin
80
+ * @param sda I2C data line pin.
81
+ * @param scl I2C clock line pin.
79
82
*/
80
83
I2CSlave (PinName sda, PinName scl);
81
84
82
- /* * Set the frequency of the I2C interface
85
+ /* * Set the frequency of the I2C interface.
83
86
*
84
- * @param hz The bus frequency in hertz
87
+ * @param hz The bus frequency in Hertz.
85
88
*/
86
89
void frequency (int hz);
87
90
88
- /* * Checks to see if this I2C Slave has been addressed.
91
+ /* * Check if this I2C Slave has been addressed.
89
92
*
90
- * @returns
91
- * A status indicating if the device has been addressed, and how
92
- * - NoData - the slave has not been addressed
93
- * - ReadAddressed - the master has requested a read from this slave
94
- * - WriteAddressed - the master is writing to this slave
95
- * - WriteGeneral - the master is writing to all slave
93
+ * @return A status indicating if the device has been addressed and how.
94
+ * @retval NoData The slave has not been addressed.
95
+ * @retval ReadAddressed The master has requested a read from this slave.
96
+ * @retval WriteAddressed The master is writing to this slave.
97
+ * @retval WriteGeneral The master is writing to all slave.
96
98
*/
97
99
int receive (void );
98
100
99
- /* * Read from an I2C master.
101
+ /* * Read specified number of bytes from an I2C master.
100
102
*
101
- * @param data pointer to the byte array to read data in to
102
- * @param length maximum number of bytes to read
103
+ * @param data Pointer to the buffer to read data into.
104
+ * @param length Number of bytes to read.
103
105
*
104
- * @returns
105
- * 0 on success,
106
- * non-0 otherwise
106
+ * @return Result of the operation.
107
+ * @retval 0 If the number of bytes read is equal to length requested.
108
+ * @retval nonzero On error or if the number of bytes read is less than requested.
107
109
*/
108
110
int read (char *data, int length);
109
111
110
112
/* * Read a single byte from an I2C master.
111
113
*
112
- * @returns
113
- * the byte read
114
+ * @return The byte read.
114
115
*/
115
116
int read (void );
116
117
117
118
/* * Write to an I2C master.
118
119
*
119
- * @param data pointer to the byte array to be transmitted
120
- * @param length the number of bytes to transmite
120
+ * @param data Pointer to the buffer containing the data to be sent.
121
+ * @param length Number of bytes to send.
121
122
*
122
- * @returns
123
- * 0 on success,
124
- * non-0 otherwise
123
+ * @return
124
+ * @retval 0 If written all bytes successfully.
125
+ * @retval nonzero On error or if the number of bytes written is less than requested.
125
126
*/
126
127
int write (const char *data, int length);
127
128
128
129
/* * Write a single byte to an I2C master.
129
130
*
130
- * @param data the byte to write
131
+ * @param data Value to write.
131
132
*
132
- * @returns
133
- * '1' if an ACK was received,
134
- * '0' otherwise
133
+ * @return Result of the operation.
134
+ * @retval 0 If a NACK is received.
135
+ * @retval 1 If an ACK is received.
136
+ * @retval 2 On timeout.
135
137
*/
136
138
int write (int data);
137
139
138
- /* * Sets the I2C slave address.
140
+ /* * Set the I2C slave address.
141
+ *
142
+ * @param address The address to set for the slave (least significant bit is ignored).
139
143
*
140
- * @param address The address to set for the slave (ignoring the least
141
- * signifcant bit). If set to 0, the slave will only respond to the
144
+ * @note If address is set to 0, the slave will only respond to the
142
145
* general call address.
143
146
*/
144
147
void address (int address);
@@ -147,8 +150,13 @@ class I2CSlave {
147
150
*/
148
151
void stop (void );
149
152
153
+ #if !defined(DOXYGEN_ONLY)
154
+
150
155
protected:
156
+ /* Internal i2c object identifying the resources */
151
157
i2c_t _i2c;
158
+
159
+ #endif // !defined(DOXYGEN_ONLY)
152
160
};
153
161
154
162
} // namespace mbed
0 commit comments