@@ -34,39 +34,95 @@ namespace mbed {
34
34
*
35
35
* @note Synchronization level: Not protected
36
36
*
37
- * Example Simple I2C responder :
37
+ * Example Simple I2C slave and master (requires two Mbed-boards) :
38
38
* @code
39
39
* #include <mbed.h>
40
+ * #include <mbed_wait_api.h>
41
+ * #include <string.h>
40
42
*
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
43
44
*
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);
45
57
*
46
58
* int main() {
47
- * slave.address(SLAVE_ADDRESS);
59
+ *
60
+ * char buf[BUFFER_SIZE] = "ABCDE";
61
+ *
62
+ * slave.address(SLAVE_ADDR);
48
63
* while (1) {
49
- * int operation = slave.receive();
50
- * switch (operation ) {
64
+ * int i = slave.receive();
65
+ * switch (i ) {
51
66
* 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);
58
70
* break;
71
+ *
59
72
* 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 );
62
75
* break;
76
+ *
63
77
* 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 );
66
80
* break;
67
81
* }
68
82
* }
69
83
* }
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
+ *
70
126
* @endcode
71
127
*/
72
128
class I2CSlave {
@@ -116,7 +172,7 @@ class I2CSlave {
116
172
*
117
173
* @return Result of the operation.
118
174
* @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.
120
176
*/
121
177
int read (char *data, int length);
122
178
0 commit comments