I2C Slave

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jimmy4399
Date:
Wed Jul 24 03:30:47 2019 +0000
Commit message:
I2C Slave

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b7bc22a927d4 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 24 03:30:47 2019 +0000
@@ -0,0 +1,40 @@
+#include <mbed.h>
+
+#if !DEVICE_I2CSLAVE
+  #error [NOT_SUPPORTED] I2C Slave is not supported
+#endif
+
+#define SDA P0_11
+#define SCL P0_10
+I2CSlave slave(SDA, SCL);
+
+#define usbtx P0_7
+#define usbrx P0_0
+Serial pc(usbtx,usbrx);
+
+int main() {
+   
+   pc.printf("Ready\n");
+   
+   char buf[10];
+   char msg[] = "Slave!";
+
+   slave.address(0xA0);
+   while (1) {
+       int i = slave.receive();
+       switch (i) {
+           case I2CSlave::ReadAddressed:
+               slave.write(msg, strlen(msg) + 1); // Includes null char
+               break;
+           case I2CSlave::WriteGeneral:
+               slave.read(buf, 10);
+               pc.printf("Read G: %s\n", buf);
+               break;
+           case I2CSlave::WriteAddressed:
+               slave.read(buf, 10);
+               pc.printf("Read A: %s\n", buf);
+               break;
+       }
+       for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
+   }
+}
\ No newline at end of file
diff -r 000000000000 -r b7bc22a927d4 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jul 24 03:30:47 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file