@@ -34,39 +34,95 @@ namespace mbed {
3434 *
3535 * @note Synchronization level: Not protected
3636 *
37- * Example Simple I2C responder :
37+ * Example Simple I2C slave and master (requires two Mbed-boards) :
3838 * @code
3939 * #include <mbed.h>
40+ * #include <mbed_wait_api.h>
41+ * #include <string.h>
4042 *
41- * const int SLAVE_ADDRESS = 0xA0;
42- * const char message[] = "Slave!";
43+ * #define BUILD_I2C_SLAVE 1 // Build for slave or master of this example
4344 *
44- * I2CSlave slave(I2C_SDA, I2C_SCL);
45+ * #define SLAVE_ADDR 0xA0
46+ * #define BUFFER_SIZE 6
47+ *
48+ * #if BUILD_I2C_SLAVE
49+ *
50+ * // Slave side of the example
51+ *
52+ * #if !DEVICE_I2CSLAVE
53+ * #error [NOT_SUPPORTED] I2C Slave is not supported
54+ * #endif
55+ *
56+ * I2CSlave slave(p3, p4);
4557 *
4658 * int main() {
47- * slave.address(SLAVE_ADDRESS);
59+ *
60+ * char buf[BUFFER_SIZE] = "ABCDE";
61+ *
62+ * slave.address(SLAVE_ADDR);
4863 * while (1) {
49- * int operation = slave.receive();
50- * switch (operation ) {
64+ * int i = slave.receive();
65+ * switch (i ) {
5166 * case I2CSlave::ReadAddressed:
52- * int status = slave.write(message, sizeof(message));
53- * if (status == 0) {
54- * printf("Written message: %s\n", message);
55- * } else {
56- * printf("Failed to write message.\n");
57- * }
67+ * // Write back the buffer from the master
68+ * slave.write(buf, BUFFER_SIZE);
69+ * printf("Written to master (addressed): %s\n", buf);
5870 * break;
71+ *
5972 * case I2CSlave::WriteGeneral:
60- * int byte_read = slave.read();
61- * printf("Read General: %c (%d) \n", byte_read, byte_read );
73+ * slave.read(buf, BUFFER_SIZE );
74+ * printf("Read from master (general): %s \n", buf );
6275 * break;
76+ *
6377 * case I2CSlave::WriteAddressed:
64- * int byte_read = slave.read();
65- * printf("Read Addressed: %c (%d) \n", byte_read, byte_read );
78+ * slave.read(buf, BUFFER_SIZE );
79+ * printf("Read from master (addressed): %s \n", buf );
6680 * break;
6781 * }
6882 * }
6983 * }
84+ *
85+ * #else
86+ *
87+ * // Master side of the example
88+ *
89+ * I2C master(p3, p4);
90+ *
91+ * static const char* to_send[] = { "abcde", "12345", "EFGHI" };
92+ *
93+ * int main() {
94+ * char buf[BUFFER_SIZE];
95+ * int send_index = 0;
96+ *
97+ * while (1) {
98+ * strcpy(buf, to_send[send_index]);
99+ *
100+ * // Write the new message to the slave
101+ * if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE)) {
102+ * printf("Failed to write to slave!\n");
103+ * } else {
104+ * printf("Written to slave: %s\n", buf);
105+ * }
106+ *
107+ * // Read what the slave has (should be identical)
108+ * if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE)) {
109+ * printf("Failed to read from slave!\n");
110+ * } else {
111+ * printf("Read from slave: %s\n", buf);
112+ * }
113+ *
114+ * // Change the message we're writing to the slave
115+ * send_index++;
116+ * if (send_index > 2) {
117+ * send_index = 0;
118+ * }
119+ *
120+ * wait_us(500000); // Wait 0.5s
121+ * }
122+ * }
123+ *
124+ * #endif
125+ *
70126 * @endcode
71127 */
72128class I2CSlave {
@@ -116,7 +172,7 @@ class I2CSlave {
116172 *
117173 * @return Result of the operation.
118174 * @retval 0 If the number of bytes read is equal to length requested.
119- * @retval nonzero On error or if the number of bytes read is less than requested .
175+ * @retval the number of bytes read.
120176 */
121177 int read (char *data, int length);
122178
0 commit comments