I2CSlave test program. I2CSlave library has some improper behavior, so this program is able to watch that. The workaround for this behavior, only for NXP device, is wrote in 'I2CSlave_mod_NXP.cpp'.

Dependencies:   mbed

First, write this program to the microcontroller which you want to check. Second, send I2C data to the microcontroller from the other one. Anything is enough for the transmission data, but the data length is made 8, 9, 10 and 11 bytes and confirm each reply.

Files at this revision

API Documentation at this revision

Comitter:
oks486
Date:
Fri Apr 15 08:16:34 2016 +0000
Parent:
0:7b78d1eee006
Commit message:
Added workaround for NXP device (confirmed only LPC1114)

Changed in this revision

I2CSlave_mod/I2CSlave_mod_NXP.cpp Show annotated file Show diff for this revision Revisions of this file
I2CSlave_mod/I2CSlave_mod_NXP.h 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
diff -r 7b78d1eee006 -r bd94bb3170b6 I2CSlave_mod/I2CSlave_mod_NXP.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CSlave_mod/I2CSlave_mod_NXP.cpp	Fri Apr 15 08:16:34 2016 +0000
@@ -0,0 +1,66 @@
+#include "I2CSlave_mod_NXP.h"
+
+#define I2C_CONSET(x)       (x->i2c->CONSET)
+#define I2C_CONCLR(x)       (x->i2c->CONCLR)
+#define I2C_DAT(x)          (x->i2c->DAT)
+#define I2C_STAT(x)         (x->i2c->STAT)
+
+static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
+    I2C_CONCLR(obj) = (start << 5)
+                    | (stop << 4)
+                    | (interrupt << 3)
+                    | (acknowledge << 2);
+}
+
+// Clear the Serial Interrupt (SI)
+static inline void i2c_clear_SI(i2c_t *obj) {
+    i2c_conclr(obj, 0, 0, 1, 0);
+}
+
+static inline int i2c_status(i2c_t *obj) {
+    return I2C_STAT(obj);
+}
+
+// Wait until the Serial Interrupt (SI) is set
+static int i2c_wait_SI(i2c_t *obj) {
+    int timeout = 0;
+    while (!(I2C_CONSET(obj) & (1 << 3))) {
+        timeout++;
+        if (timeout > 100000) return -1;
+    }
+    return 0;
+}
+
+
+I2CSlave_mod::I2CSlave_mod(PinName sda, PinName scl) : I2CSlave(sda, scl) {
+}
+
+
+int I2CSlave_mod::read(char *data, int length) {
+    return i2c_slave_read_mod(&_i2c, data, length) != length;
+}
+
+
+int I2CSlave_mod::i2c_slave_read_mod(i2c_t *obj, char *data, int length) {
+    int count = 0;
+    int status;
+    
+    do {
+        if((status == 0x80) || (status == 0x90)) {
+            data[count] = I2C_DAT(obj) & 0xFF;
+            count++;
+        }
+        i2c_clear_SI(obj);
+        i2c_wait_SI(obj);
+        status = i2c_status(obj);
+    } while (((status == 0x80) || (status == 0x90) ||
+            (status == 0x060) || (status == 0x70)) && (count < length));
+    
+    if(status != 0xA0) {
+        i2c_stop(obj);
+    }
+    
+    i2c_clear_SI(obj);
+    
+    return count;
+}
diff -r 7b78d1eee006 -r bd94bb3170b6 I2CSlave_mod/I2CSlave_mod_NXP.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CSlave_mod/I2CSlave_mod_NXP.h	Fri Apr 15 08:16:34 2016 +0000
@@ -0,0 +1,15 @@
+#ifndef I2C_SLAVE_NXP_MOD_H
+#define I2C_SLAVE_NXP_MOD_H
+
+#include "mbed.h"
+
+class I2CSlave_mod : public I2CSlave {
+public:
+    I2CSlave_mod(PinName sda, PinName scl);
+    int read(char *data, int length);       // hiding read function of parent class
+
+protected:
+    int i2c_slave_read_mod(i2c_t *obj, char *data, int length);
+};
+
+#endif
diff -r 7b78d1eee006 -r bd94bb3170b6 main.cpp
--- a/main.cpp	Fri Apr 15 06:12:00 2016 +0000
+++ b/main.cpp	Fri Apr 15 08:16:34 2016 +0000
@@ -1,6 +1,14 @@
 #include "mbed.h"
 
+//#define I2C_SLAVE_MOD
+
+#ifdef I2C_SLAVE_MOD
+#include "I2CSlave_mod_NXP.h"
+I2CSlave_mod slave(dp5, dp27);          // sda, scl
+#else
 I2CSlave slave(dp5, dp27);              // sda, scl
+#endif
+
 Serial serial(dp16, dp15);              // tx, rx
 DigitalOut led(LED1);                  // indicator