@@ -34,39 +34,93 @@ 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
+ * #if !DEVICE_I2CSLAVE
51
+ * #error [NOT_SUPPORTED] I2C Slave is not supported
52
+ * #endif
53
+ *
54
+ * I2CSlave slave(p3, p4);
55
+ *
56
+ * int main() {
57
+ *
58
+ * char buf[BUFFER_SIZE] = "ABCDE";
59
+ *
60
+ * slave.address(SLAVE_ADDR);
61
+ * while (1) {
62
+ * int i = slave.receive();
63
+ * switch (i) {
64
+ * case I2CSlave::ReadAddressed:
65
+ * // Write back the buffer from the master
66
+ * slave.write(buf, BUFFER_SIZE);
67
+ * printf("Written to master (addressed): %s\n", buf);
68
+ * break;
69
+ * case I2CSlave::WriteGeneral:
70
+ * slave.read(buf, BUFFER_SIZE);
71
+ * printf("Read from master (general): %s\n", buf);
72
+ * break;
73
+ * case I2CSlave::WriteAddressed:
74
+ * slave.read(buf, BUFFER_SIZE);
75
+ * printf("Read from master (addressed): %s\n", buf);
76
+ * break;
77
+ * }
78
+ * }
79
+ * }
80
+ *
81
+ * #else
82
+ *
83
+ *
84
+ *
85
+ * I2C master(p3, p4);
86
+ *
87
+ * static const char* to_send[] = { "abcde", "12345", "EFGHI" };
45
88
*
46
89
* int main() {
47
- * slave.address(SLAVE_ADDRESS);
48
- * while (1) {
49
- * int operation = slave.receive();
50
- * switch (operation) {
51
- * 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
- * }
58
- * break;
59
- * case I2CSlave::WriteGeneral:
60
- * int byte_read = slave.read();
61
- * printf("Read General: %c (%d)\n", byte_read, byte_read);
62
- * break;
63
- * case I2CSlave::WriteAddressed:
64
- * int byte_read = slave.read();
65
- * printf("Read Addressed: %c (%d)\n", byte_read, byte_read);
66
- * break;
67
- * }
68
- * }
90
+ *
91
+ * char buf[BUFFER_SIZE];
92
+ * int send_index = 0;
93
+
94
+ * while (1) {
95
+ * strcpy(buf, to_send[send_index]);
96
+ *
97
+ * // Write the new message to the buffer
98
+ * if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE)) {
99
+ * printf("Failed to write to slave!\n");
100
+ * } else {
101
+ * printf("Written to slave: %s\n", buf);
102
+ * }
103
+ *
104
+ * // Read what the slave has (should be identical)
105
+ * if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE)) {
106
+ * printf("Failed to read from slave!\n");
107
+ * } else {
108
+ * printf("Read from slave: %s\n", buf);
109
+ * }
110
+ *
111
+ * // Change the message we're writing to the slave
112
+ * send_index++;
113
+ * if (send_index > 2) {
114
+ * send_index = 0;
115
+ * }
116
+ *
117
+ * wait_us(500000); // Wait 0.5s
118
+ *
119
+ * }
69
120
* }
121
+ *
122
+ * #endif
123
+ *
70
124
* @endcode
71
125
*/
72
126
class I2CSlave {
0 commit comments