I2C master and slave testing example

Dependencies:   mbed-src

Files at this revision

API Documentation at this revision

Comitter:
Yunfeng
Date:
Fri Aug 14 15:00:28 2015 +0000
Commit message:
initial commit for I2C master and slave test

Changed in this revision

blinky_test.cpp Show annotated file Show diff for this revision Revisions of this file
fw_config.h Show annotated file Show diff for this revision Revisions of this file
i2c_3200.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0c4c8f8e3e04 blinky_test.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blinky_test.cpp	Fri Aug 14 15:00:28 2015 +0000
@@ -0,0 +1,85 @@
+#include "fw_config.h"
+#if SEL_BLINKY_TEST == 1
+/*
+#include "mbed.h"
+
+DigitalOut pin01  (P0_1);
+DigitalOut pin023 (P0_23);
+DigitalOut pin021 (P0_21);
+DigitalOut pin022 (P0_22);
+DigitalOut pin115 (P1_15);
+DigitalOut pin018 (P0_18);
+DigitalOut pin019 (P0_19);
+DigitalOut pin119 (P1_19);
+DigitalOut pin020 (P0_20);
+DigitalOut pin07  (P0_7);
+DigitalOut pin02  (P0_2);
+DigitalOut pin04  (P0_4);
+DigitalOut pin05  (P0_5);
+
+
+DigitalOut pin08  (P0_8);
+DigitalOut pin09 (P0_9);
+DigitalOut pin010 (P0_10);
+DigitalOut pin011 (P0_11);
+DigitalOut pin012 (P0_12);
+DigitalOut pin013 (P0_13);
+DigitalOut pin014 (P0_14);
+DigitalOut pin015 (P0_15);
+DigitalOut pin016 (P0_16);
+DigitalOut pin017 (P0_17);
+
+void blinky_test() {
+        while(1) {
+        pin01 = 1;
+        pin023 = 1;
+        pin021 = 1;
+        pin022 = 1;
+        pin115 = 1;
+        pin018 = 1;
+        pin019 = 1;
+        pin119 = 1;
+        pin020 = 1;
+        pin07 = 1;
+        pin02 = 1;
+        pin04 = 1;
+        pin05 = 1;
+        pin08 = 1;
+        pin09 = 1;
+        pin010 = 1;
+        pin011 = 1;
+        pin012 = 1;
+        pin013 = 1;
+        pin014 = 1;
+        pin015 = 1;
+        pin016 = 1;
+        pin017 = 1;
+        wait (3);
+        pin01 = 0;
+        pin023 = 0;
+        pin021 = 0;
+        pin022 = 0;
+        pin115 = 0;
+        pin018 = 0;
+        pin019 = 0;
+        pin119 = 0;
+        pin020 = 0;
+        pin07 = 0;
+        pin02 = 0;
+        pin04 = 0;
+        pin05 = 0;
+        pin08 = 0;
+        pin09 = 0;
+        pin010 = 0;
+        pin011 = 0;
+        pin012 = 0;
+        pin013 = 0;
+        pin014 = 0;
+        pin015 = 0;
+        pin016 = 0;
+        pin017 = 0;
+        wait (3);
+    }
+}
+*/
+#endif
\ No newline at end of file
diff -r 000000000000 -r 0c4c8f8e3e04 fw_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fw_config.h	Fri Aug 14 15:00:28 2015 +0000
@@ -0,0 +1,8 @@
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+#define SEL_BLINKY_TEST 0
+#define SEL_I2C_3200_TEST 1
+#define SEL_I2C_SLAVE_MODE 0
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 0c4c8f8e3e04 i2c_3200.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2c_3200.cpp	Fri Aug 14 15:00:28 2015 +0000
@@ -0,0 +1,107 @@
+#include "fw_config.h"
+#if SEL_I2C_3200_TEST == 1
+
+#include "mbed.h"
+#if SEL_I2C_SLAVE_MODE == 1
+
+#else 
+
+#endif
+
+DigitalOut pin07  (P0_20);
+DigitalOut pin12  (P0_21);
+DigitalOut pin21  (P0_11);
+
+#if SEL_I2C_SLAVE_MODE == 1
+void i2c_3200 () {
+    
+    I2CSlave slave(P0_5 , P0_4);
+    // indicate enter
+    volatile int d = 0;
+    for (int i = 0; i < 5; i++) {
+        d = d > 0? 0:1;
+        pin07 = d;
+        pin12 = d;
+        pin21 = d;
+        wait(1);
+    }
+    pin07 = 1;
+    pin12 = 1;
+    pin21 = 1;
+    //while (1);
+    
+    
+    slave.address(0x60);
+    char buf[2];
+    
+    while (1) {
+        int i = slave.receive();
+         switch (i) {
+             case I2CSlave::ReadAddressed:
+                 buf[1] = 'Z';
+                 slave.write(buf, 1); // Includes null char
+                 pin07 = 0;
+                 pin12 = 1;
+                 pin21 = 1;
+                 break;
+             case I2CSlave::WriteGeneral:
+                 slave.read(buf, 10);
+                 pin12 = 0;
+                 pin07 = 1;
+                 pin21 = 1;
+                 printf("Read G: \n");
+                 break;
+             case I2CSlave::WriteAddressed:
+                 slave.read(buf, 1);
+                 pin21 = 0;
+                 pin07 = 1;
+                 pin12 = 1;
+                 if (buf[0] != 0xaa) {
+                     pin07 = 0;
+                 }
+                 wait(0.3);
+                 pin21 = 1;
+                 printf("Read A: %x, %x\n", buf[0], buf[1]);
+                 break;
+         }
+         //for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
+    }
+}
+#else
+void i2c_3200_master () {
+    volatile int d = 0;
+    for (int i = 0; i < 10; i++) {
+        d = d > 0? 0:1;
+        pin07 = d;
+        pin12 = d;
+        pin21 = d;
+        wait(1);
+    }
+    pin07 = 1;
+    pin12 = 1;
+    pin21 = 1;
+    I2C master(P0_5 , P0_4);
+    char cmd[2];
+    int err;
+    master.frequency(400000);
+    while (1) {
+        cmd[0] = 0xAA;
+        //cmd[1] = 0x01;
+        err = master.write(0x60, cmd, 1);
+        if (err)  pin07 = 0;
+        wait(0.5);
+ 
+        cmd[0] = 0xAA;
+        //cmd[1] = 0x80;
+        err = master.write(0x60, cmd, 1);
+        if (err) pin12 = 0;
+        wait(0.5);
+        //slave.read(addr, cmd, 2);
+ 
+        //float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
+        //printf("Temp = %.2f\n", tmp);
+    }
+}    
+#endif
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 0c4c8f8e3e04 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 14 15:00:28 2015 +0000
@@ -0,0 +1,30 @@
+#include "fw_config.h"
+#include "mbed.h"
+
+
+
+#if SEL_BLINKY_TEST == 1
+void blinky_test();
+#endif
+
+#if SEL_I2C_3200_TEST == 1
+#if SEL_I2C_SLAVE_MODE == 1
+void i2c_3200();
+#else
+void i2c_3200_master();
+#endif
+#endif
+
+
+int main() {
+#if SEL_I2C_3200_TEST == 1
+#if SEL_I2C_SLAVE_MODE == 1
+    i2c_3200();
+#else
+    i2c_3200_master ();
+#endif
+    while (1) {}
+#endif
+}
+
+
diff -r 000000000000 -r 0c4c8f8e3e04 mbed-src.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Fri Aug 14 15:00:28 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#421d040916fe