I2CRTOS Driver by Helmut Schmücker. Removed included mbed-rtos library to prevent multiple definition. Make sure to include mbed-rtos library in your program!

Fork of I2cRtosDriver by Helmut Schmücker

Files at this revision

API Documentation at this revision

Comitter:
humlet
Date:
Tue Apr 30 19:12:57 2013 +0000
Parent:
7:04824382eafb
Child:
9:65aae53a34de
Commit message:
alpha2

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
I2CDriverTest04.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
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- a/I2CDriver.cpp	Sun Apr 28 15:08:04 2013 +0000
+++ b/I2CDriver.cpp	Tue Apr 30 19:12:57 2013 +0000
@@ -2,12 +2,15 @@
 #include "i2c_api.h"
 #include "error.h"
 
-#include "DigitalOut.h"
-
 using namespace mbed;
 using namespace rtos;
 
-DigitalOut osci(p12);
+extern "C"{
+osSemaphoreId i2cIsrDrvSem_1;
+osSemaphoreDef(i2cIsrDrvSem_1); 
+osSemaphoreId i2cIsrDrvSem_2;
+osSemaphoreDef(i2cIsrDrvSem_2); 
+}
 
 #define DRV_USR_SIG (1<<6)
 
@@ -19,23 +22,19 @@
 #if defined(TARGET_LPC1768)
 void I2CDriver::channel_0_ISR()
 {
-    osci.write(1);
-    osSignalSet( s_channels[0]->driver, I2C_ISR_DRV_SIG);
+    osSemaphoreRelease(i2cIsrDrvSem_1);
     NVIC_DisableIRQ(I2C1_IRQn);
-    osci.write(0);
 }
 #endif
 
 void I2CDriver::channel_1_ISR()
 {
-    osci.write(1);
-    osSignalSet( s_channels[1]->driver, I2C_ISR_DRV_SIG);
+    osSemaphoreRelease(i2cIsrDrvSem_2);
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
     NVIC_DisableIRQ(I2C2_IRQn);
 #elif defined(TARGET_LPC11U24)
     NVIC_DisableIRQ(I2C_IRQn);
 #endif
-    osci.write(0);
 }
 
 
@@ -50,8 +49,16 @@
         if (sda==c_sdas[1] && scl==c_scls[1]) channel=1; //I2C_2 or I2C
         else error("I2CDriver: Invalid I2C pinns selected");
 
-    if(s_channels[channel]==0)
+    if(s_channels[channel]==0){
         new Thread(threadFun,(void *)channel,osPriorityRealtime,256);
+        if(channel==0){
+            i2cIsrDrvSem_1 = osSemaphoreCreate(osSemaphore(i2cIsrDrvSem_1), 1);
+            osSemaphoreWait(i2cIsrDrvSem_1,osWaitForever);
+        }else{
+            i2cIsrDrvSem_2 = osSemaphoreCreate(osSemaphore(i2cIsrDrvSem_2), 1);
+            osSemaphoreWait(i2cIsrDrvSem_2,osWaitForever);
+        }
+    }
     m_channel = s_channels[channel];
 }
 
--- a/I2CDriverTest01.h	Sun Apr 28 15:08:04 2013 +0000
+++ b/I2CDriverTest01.h	Tue Apr 30 19:12:57 2013 +0000
@@ -2,12 +2,15 @@
 #include "rtos.h"
 #include "I2CMasterRtos.h"
 #include "stdint.h"
+#include "DigitalOut.h"
 
 volatile int g_disco=0;
 volatile int g_len=0;
 volatile int g_freq=100000;
 volatile osThreadId i2cDrvThrdID;
 
+DigitalOut osci(p12);
+
 void highPrioCallBck(void const *args)
 {
     osSignalSet(i2cDrvThrdID, 1<<5);
@@ -22,7 +25,7 @@
     // trigger on srf08 ranging
     const char regNcmd[2]= {0x00,0x51};
     i2c.write(adr, regNcmd, 2);
-
+    osDelay(200);
     while(true) {
         i2c.frequency(g_freq);
         Thread::signal_wait(1<<5,osWaitForever);
@@ -30,7 +33,7 @@
         const char reg= 0x02;
         char result[64];
         uint32_t t1=us_ticker_read();
-        i2c.read(adr, reg, result, g_len, 1);
+        i2c.read(adr, reg, result, g_len);
         uint32_t dt=us_ticker_read()-t1;
         uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
 
@@ -53,7 +56,7 @@
 #elif defined(TARGET_LPC11U24)
     const int nTest=5;
     const int freq[nTest]=  {1e5,   1e5,    4e5,   4e5,    4e5    };
-    const int len[nTest]=   {1,     4,      1,      6,     12};
+    const int len[nTest]=   {1,     4,      1,      6,     16};
 #endif
     for(int i=0; i<nTest; ++i) {
         g_freq = freq[i];
@@ -69,6 +72,7 @@
             tAct=us_ticker_read();
             if(tAct>tLast) {
                 if(tAct==tLast+1)++tMe;
+                osci.write(tAct&1);
             }
             tLast=tAct;
         } while(tAct-tStart<dt);
--- a/I2CDriverTest04.h	Sun Apr 28 15:08:04 2013 +0000
+++ b/I2CDriverTest04.h	Tue Apr 30 19:12:57 2013 +0000
@@ -49,11 +49,11 @@
 #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,      29};
+    const int len[nTest]=   {1,     4,      6,      1,     6,     12,      30};
 #elif defined(TARGET_LPC11U24)
     const int nTest=5;
     const int freq[nTest]=  {1e5,   1e5,    4e5,   4e5,    4e5    };
-    const int len[nTest]=   {1,     4,      1,      6,     12};
+    const int len[nTest]=   {1,     4,      1,      6,     16};
 #endif
     for(int i=0; i<nTest; ++i) {
         g_freq = freq[i];
@@ -69,6 +69,7 @@
             tAct=us_ticker_read();
             if(tAct>tLast) {
                 if(tAct==tLast+1)++tMe;
+                //tLast=tAct;
             }
             tLast=tAct;
         } while(tAct-tStart<dt);
--- a/mbed-NXP.lib	Sun Apr 28 15:08:04 2013 +0000
+++ b/mbed-NXP.lib	Tue Apr 30 19:12:57 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/humlet/code/mbed-NXP/#5e7f2f37d75b
+http://mbed.org/users/humlet/code/mbed-NXP/#692b566d83bf
--- a/mbed-src.lib	Sun Apr 28 15:08:04 2013 +0000
+++ b/mbed-src.lib	Tue Apr 30 19:12:57 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/humlet/code/mbed-src/#28ed2abfeb85
+http://mbed.org/users/humlet/code/mbed-src/#49923059ddc8