Helmut Schmücker / mbed-RtosI2cDriver

Dependencies:   mbed-rtos mbed-src

Files at this revision

API Documentation at this revision

Comitter:
humlet
Date:
Sat Apr 20 09:52:09 2013 +0000
Parent:
3:967dde37e712
Child:
5:8a418c89e515
Commit message:
running

Changed in this revision

I2CDriver.cpp Show annotated file Show diff for this revision Revisions of this file
I2CDriverTest01.h Show annotated file Show diff for this revision Revisions of this file
I2CDriverTest02.h Show annotated file Show diff for this revision Revisions of this file
I2CSlaveRtos.h Show annotated file Show diff for this revision Revisions of this file
mbed-NXP.lib Show annotated file Show diff for this revision Revisions of this file
--- a/I2CDriver.cpp	Fri Apr 19 21:33:29 2013 +0000
+++ b/I2CDriver.cpp	Sat Apr 20 09:52:09 2013 +0000
@@ -126,8 +126,9 @@
                 break;
             case RECEIVE:
                 tr.ret = i2c_slave_receive_rtos(&i2c, tr.tmout);
+                break;
             default:
-                error("call 911");
+                error("call 911\n");
         }
         // inform the caller
         osSignalSet( channel.transfer.caller, DRV_USR_SIG);
--- a/I2CDriverTest01.h	Fri Apr 19 21:33:29 2013 +0000
+++ b/I2CDriverTest01.h	Sat Apr 20 09:52:09 2013 +0000
@@ -3,7 +3,9 @@
 #include "I2CMasterRtos.h"
 #include "stdint.h"
 
-volatile int disco=0;
+volatile int g_disco=0;
+volatile int g_len=0;
+volatile int g_freq=100000;
 volatile osThreadId i2cDrvThrdID;
 
 void highPrioCallBck(void const *args)
@@ -15,7 +17,6 @@
 {
     i2cDrvThrdID = Thread::gettid();
     I2CMasterRtos i2c(p28, p27);
-    i2c.frequency(400000);
     const uint32_t adr = 0x70<<1;
 
     // trigger on srf08 ranging
@@ -23,17 +24,17 @@
     i2c.write(adr, regNcmd, 2);
 
     while(true) {
+        i2c.frequency(g_freq);
         Thread::signal_wait(1<<5,osWaitForever);
         // read back srf08 echo times (1+16 words)
         const char reg= 0x02;
         char result[64];
         uint32_t t1=us_ticker_read();
-        i2c.read(adr, reg, result, 25, 1);
-
+        i2c.read(adr, reg, result, g_len, 1);
         uint32_t dt=us_ticker_read()-t1;
         uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
         
-        if(--disco>0) printf("tm=%4dus dt=%4dus\n",tm,dt);
+        if(--g_disco>0) printf("tm=%4dus dt=%4dus\n",tm,dt);
     }
 }
 
@@ -45,8 +46,15 @@
     Thread::wait(100);
     highPrioTicker.start(1);
 
-    uint32_t cnt=0;
-    while (++cnt<5) {
+    const int nTest=7;
+    const int freq[nTest]=  {4e5,   4e5,    4e5,    4e5,    4e5,    1e5,    1e5};
+    const int len[nTest]=   {1,     4,      9,      16,     25,     1,      6};
+    
+    for(int i=0; i<nTest; ++i){
+        g_freq = freq[i];
+        g_len = len[i];
+        printf("f=%d l=%d\n",g_freq,g_len);
+        Thread::wait(500);
         const uint32_t dt=1e6;
         uint32_t tStart = us_ticker_read();
         uint32_t tLast = tStart;
@@ -60,9 +68,8 @@
             tLast=tAct;
         } while(tAct-tStart<dt);
         printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
-        disco=10;
-        while(disco>0);
-        Thread::wait(1000);
+        g_disco=10;
+        while(g_disco>0);
     }
     return 0;
 }
\ No newline at end of file
--- a/I2CDriverTest02.h	Fri Apr 19 21:33:29 2013 +0000
+++ b/I2CDriverTest02.h	Sat Apr 20 09:52:09 2013 +0000
@@ -3,26 +3,44 @@
 #include "I2CMasterRtos.h"
 #include "I2CSlaveRtos.h"
 
-const int freq = 100000;
+const int freq = 400000;
 const int adr = 42;
 const int len=42;
-const char* cMsg="We are mbed, resistance is futile";
+const char* mstMsg="We are mbed, resistance is futile";
+const char* slvMsg="Fine with me, let's get addicted";
 
-static void rxMsg(I2CSlaveRtos& slv)
+static void slvRxMsg(I2CSlaveRtos& slv)
 {
-    char rMsg[len];
-    memset(rMsg,0,len);
+    char rxMsg[len];
+    memset(rxMsg,0,len);
     if ( slv.receive() == I2CSlave::WriteAddressed) {
-        slv.read(rMsg, len); // have to add 1 byte for the address (bug report!)
-        printf("thread %d received message '%s'\n",Thread::gettid(),rxMsg);
+        slv.read(rxMsg, len);
+        //rMsg[len-1]=0;
+        printf("thread %X received message as i2c slave: '%s'\n",Thread::gettid(),rxMsg);
     } else
-        printf("god damned nation");
+        printf("Ouch slv rx failure\n");
 }
 
-static void txMsg(I2CMasterRtos& mst)
+static void slvTxMsg(I2CSlaveRtos& slv)
+{
+    if ( slv.receive() == I2CSlave::ReadAddressed) {
+        slv.write(slvMsg, len);
+    } else
+        printf("Ouch slv tx failure\n");
+}
+
+static void mstTxMsg(I2CMasterRtos& mst)
 {
-    mst.write(adr,cMsg,len);
-    printf("thread %d has sent the message\n",Thread::gettid());
+    mst.write(adr,mstMsg,len);
+    //printf("thread %X has sent the message\n",Thread::gettid());
+}
+
+static void mstRxMsg(I2CMasterRtos& mst)
+{
+    char rxMsg[len];
+    memset(rxMsg,0,len);
+    mst.read(adr,rxMsg,len);
+    printf("thread %X received message as i2c master: '%s'\n",Thread::gettid(),rxMsg);
 }
 
 static void channel1(void const *args)
@@ -30,10 +48,12 @@
     I2CMasterRtos mst(p9,p10,freq);
     I2CSlaveRtos slv(p9,p10,freq,adr);
     while(1) {
-        rxMsg(slv);
+        slvRxMsg(slv);
+        slvTxMsg(slv);
         Thread::wait(100);
-        txMsg(mst);
+        mstTxMsg(mst);
         Thread::wait(100);
+        mstRxMsg(mst);
     }
 }
 
@@ -42,10 +62,12 @@
     I2CMasterRtos mst(p28,p27,freq);
     I2CSlaveRtos slv(p28,p27,freq,adr);
     while(1) {
-        txMsg(mst);
+        Thread::wait(100);
+        mstTxMsg(mst);
         Thread::wait(100);
-        rxMsg(slv);
-        Thread::wait(100);
+        mstRxMsg(mst);
+        slvRxMsg(slv);
+        slvTxMsg(slv);
     }
 }
 
--- a/I2CSlaveRtos.h	Fri Apr 19 21:33:29 2013 +0000
+++ b/I2CSlaveRtos.h	Sat Apr 20 09:52:09 2013 +0000
@@ -46,8 +46,8 @@
      *  - WriteAddressed    - the master is writing to this slave
      *  - WriteGeneral      - the master is writing to all slave
      */
-    int receive(void) {
-        return m_drv.receiveSlave();
+    int receive(uint32_t timeout_ms=osWaitForever) {
+        return m_drv.receiveSlave(timeout_ms);
     }
 
     /** Read from an I2C master.
--- a/mbed-NXP.lib	Fri Apr 19 21:33:29 2013 +0000
+++ b/mbed-NXP.lib	Sat Apr 20 09:52:09 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-NXP/#b7a4ba1b2dab
+http://mbed.org/users/mbed_official/code/mbed-NXP/#bd882383c33b