Modified version of the official mbed lib providing a RTOS enabled i2c-driver based on the official i2c-C-api.

Dependencies:   mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CDriverTest01.h Source File

I2CDriverTest01.h

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "I2CMasterRtos.h"
00004 #include "stdint.h"
00005 #include "DigitalOut.h"
00006 
00007 volatile int g_disco=0;
00008 volatile int g_len=0;
00009 volatile int g_freq=100000;
00010 volatile osThreadId i2cDrvThrdID;
00011 
00012 DigitalOut osci(p12);
00013 
00014 void highPrioCallBck(void const *args)
00015 {
00016     osSignalSet(i2cDrvThrdID, 1<<5);
00017 }
00018 
00019 void highPrioThreadFun(void const *args)
00020 {
00021     i2cDrvThrdID = Thread::gettid();
00022     I2CMasterRtos i2c(p28, p27);
00023     const uint32_t adr = 0x70<<1;
00024 
00025     // trigger on srf08 ranging
00026     const char regNcmd[2]= {0x00,0x51};
00027     i2c.write(adr, regNcmd, 2);
00028     osDelay(200);
00029     while(true) {
00030         i2c.frequency(g_freq);
00031         Thread::signal_wait(1<<5,osWaitForever);
00032         // read back srf08 echo times (1+16 words)
00033         const char reg= 0x02;
00034         char result[64];
00035         uint32_t t1=us_ticker_read();
00036         i2c.read(adr, reg, result, g_len);
00037         uint32_t dt=us_ticker_read()-t1;
00038         uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
00039 
00040         if(--g_disco>0) printf("tm=%4dus dt=%4dus\n",tm,dt);
00041     }
00042 }
00043 
00044 int doit()
00045 {
00046     Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal);
00047     RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0);
00048 
00049     Thread::wait(100);
00050     highPrioTicker.start(1);
00051 
00052 #if defined(TARGET_LPC1768)
00053     const int nTest=7;
00054     const int freq[nTest]=  {1e5,   1e5,    1e5,   4e5,    4e5,    4e5,    4e5};
00055     const int len[nTest]=   {1,     4,      6,      1,     6,     12,      25};
00056 #elif defined(TARGET_LPC11U24)
00057     const int nTest=5;
00058     const int freq[nTest]=  {1e5,   1e5,    4e5,   4e5,    4e5    };
00059     const int len[nTest]=   {1,     4,      1,      6,     16};
00060 #endif
00061     for(int i=0; i<nTest; ++i) {
00062         g_freq = freq[i];
00063         g_len = len[i];
00064         printf("f=%d l=%d\n",g_freq,g_len);
00065         Thread::wait(500);
00066         const uint32_t dt=1e6;
00067         uint32_t tStart = us_ticker_read();
00068         uint32_t tLast = tStart;
00069         uint32_t tAct = tStart;
00070         uint32_t tMe=0;
00071         do {
00072             tAct=us_ticker_read();
00073             if(tAct>tLast) {
00074                 if(tAct==tLast+1)++tMe;
00075                 osci.write(tAct&1);
00076             }
00077             tLast=tAct;
00078         } while(tAct-tStart<dt);
00079         printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
00080         g_disco=10;
00081         while(g_disco>0);
00082     }
00083     return 0;
00084 }