Helmut Schmücker / I2cRtosDriver

Dependencies:   mbed-rtos

Fork of mbed-RtosI2cDriver by Helmut Schmücker

Revision:
14:352609d395c1
Parent:
13:530968937ccb
--- a/I2CDriverTest04.h	Fri May 10 20:38:35 2013 +0000
+++ b/I2CDriverTest04.h	Sun May 19 11:21:16 2013 +0000
@@ -1,65 +1,57 @@
+// A high prio thread reads at a rate of 1kHz from a MPU6050 gyro/acc meter's FIFO
+// data packets of different size, whereas the lower prio ain thread the CPU time left.
+
 #include "mbed.h"
 #include "rtos.h"
 #include "I2CMasterRtos.h"
 #include "stdint.h"
 
-//DigitalOut osci(p16);
+//#include "DigitalOut.h"
+//DigitalOut osci(p8);
 
 volatile int g_disco=0;
 volatile int g_len=0;
 volatile int g_freq=100000;
 const uint32_t i2cAdr = 0x68<<1;
-volatile osThreadId i2cDrvThrdID;
 
 static void config(I2CMasterRtos& i2c);
 
+I2CMasterRtos i2c(p28, p27);
+
 void highPrioCallBck(void const *args)
 {
-    osSignalSet(i2cDrvThrdID, 1<<5);
-}
-
-void highPrioThreadFun(void const *args)
-{
-    //printf("tst01\n");
-    i2cDrvThrdID = Thread::gettid();
-    I2CMasterRtos i2c(p28, p27);
-    //printf("tst02\n");
-    config(i2c);
+    //I2CDriver::osci2.write(0);
+    const char reg= 0x74;
+    static char result[64];
+    //I2CDriver::osci2.write(1);
+    // read from MPU600's fifo
+    i2c.frequency(g_freq);
+    uint32_t t1=us_ticker_read();
+    //I2CDriver::osci2.write(0);
+    int stat = i2c.read(i2cAdr, reg, result, g_len);
+    uint32_t dt=us_ticker_read()-t1;
+    if(stat!=0) {
+        printf("\n%x %d %d %d\n",stat,g_freq,g_len,dt);
+        exit(0);
+    }
+    int16_t val=((static_cast<int16_t>(result[0])<<8)|static_cast<int16_t>(result[1]));
 
-    const char reg= 0x3a;
-    char result[64];
-    while(true) {
-        //osci.write(0);
-        i2c.frequency(g_freq);
-        Thread::signal_wait(1<<5,osWaitForever);
-        // read from MPU600's fifo
-
-        uint32_t t1=us_ticker_read();
-        int stat = i2c.read(i2cAdr, reg, result, g_len);
-        uint32_t dt=us_ticker_read()-t1;
-        if(stat!=0) {
-            //osci.write(1);
-            printf("\n%x %d %d %d\n",stat,g_freq,g_len,dt);
-            exit(0);
-        }
-        uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
-
-        if(--g_disco>0) printf("tm=%8d dt=%4dus\n",tm,dt);
-    }
+    if(--g_disco>0)printf("val=%8d dt=%4dus\n",val,dt);
 }
 
 int doit()
 {
-    Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal);
+    config(i2c);
+    
     RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0);
 
-    Thread::wait(2000);
+    Thread::wait(500);
     highPrioTicker.start(1);
 
 #if defined(TARGET_LPC1768)
     const int nTest=7;
     const int freq[nTest]=  {1e5,   1e5,    1e5,   4e5,    4e5,    4e5,    4e5};
-    const int len[nTest]=   {1,     4,      6,      1,     6,     12,      36};
+    const int len[nTest]=   {1,     4,      7,      1,     6,     12,      36};
 #elif defined(TARGET_LPC11U24)
     const int nTest=5;
     const int freq[nTest]=  {1e5,   1e5,    4e5,   4e5,    4e5    };
@@ -70,21 +62,27 @@
         g_len = len[i];
         printf("f=%d l=%d\n",g_freq,g_len);
         Thread::wait(500);
+        //highPrioTicker.start(1);
         const uint32_t dt=1e6;
         uint32_t tStart = us_ticker_read();
         uint32_t tLast = tStart;
         uint32_t tAct = tStart;
         uint32_t tMe=0;
-        do {
+        do {  // loop an count consecutive µs ticker edges
+            //osci.write(!osci.read());
             tAct=us_ticker_read();
-            if(tAct>tLast) {
-                if(tAct==tLast+1)++tMe;
-                //tLast=tAct;
-            }
+            #if defined(TARGET_LPC1768)
+            if(tAct==tLast+1)++tMe;
+            #elif defined(TARGET_LPC11U24)
+            uint32_t delta = tAct-tLast;
+            if(delta<=2)tMe+=delta;  // on the 11U24 this loop takes a bit longer than 1µs (ISR ~3µs, task switch ~8µs)
+            #endif
             tLast=tAct;
         } while(tAct-tStart<dt);
+        //highPrioTicker.stop();
+        // and calculate the duty cycle from this measurement
         printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
-        g_disco=10;
+        g_disco=5;
         while(g_disco>0);
     }
     return 0;