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 I2CDriverTest04.h Source File

I2CDriverTest04.h

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "I2CMasterRtos.h"
00004 #include "stdint.h"
00005 
00006 volatile int g_disco=0;
00007 volatile int g_len=0;
00008 volatile int g_freq=100000;
00009 const uint32_t i2cAdr = 0x68<<1;
00010 volatile osThreadId i2cDrvThrdID;
00011 
00012 static void config(I2CMasterRtos& i2c);
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     
00024     config(i2c);
00025 
00026     while(true) {
00027         i2c.frequency(g_freq);
00028         Thread::signal_wait(1<<5,osWaitForever);
00029         // read back srf08 echo times (1+16 words)
00030         const char reg= 0x3a;
00031         char result[64];
00032         uint32_t t1=us_ticker_read();
00033         i2c.read(i2cAdr, reg, result, g_len);
00034         uint32_t dt=us_ticker_read()-t1;
00035         uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
00036 
00037         if(--g_disco>0) printf("tm=%8d dt=%4dus\n",tm,dt);
00038     }
00039 }
00040 
00041 int doit()
00042 {
00043     Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal);
00044     RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0);
00045 
00046     Thread::wait(100);
00047     highPrioTicker.start(1);
00048     
00049 #if defined(TARGET_LPC1768)
00050     const int nTest=7;
00051     const int freq[nTest]=  {1e5,   1e5,    1e5,   4e5,    4e5,    4e5,    4e5};
00052     const int len[nTest]=   {1,     4,      6,      1,     6,     12,      30};
00053 #elif defined(TARGET_LPC11U24)
00054     const int nTest=5;
00055     const int freq[nTest]=  {1e5,   1e5,    4e5,   4e5,    4e5    };
00056     const int len[nTest]=   {1,     4,      1,      6,     16};
00057 #endif
00058     for(int i=0; i<nTest; ++i) {
00059         g_freq = freq[i];
00060         g_len = len[i];
00061         printf("f=%d l=%d\n",g_freq,g_len);
00062         Thread::wait(500);
00063         const uint32_t dt=1e6;
00064         uint32_t tStart = us_ticker_read();
00065         uint32_t tLast = tStart;
00066         uint32_t tAct = tStart;
00067         uint32_t tMe=0;
00068         do {
00069             tAct=us_ticker_read();
00070             if(tAct>tLast) {
00071                 if(tAct==tLast+1)++tMe;
00072                 //tLast=tAct;
00073             }
00074             tLast=tAct;
00075         } while(tAct-tStart<dt);
00076         printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
00077         g_disco=10;
00078         while(g_disco>0);
00079     }
00080     return 0;
00081 }
00082 
00083 void readModWrite(I2CMasterRtos& i2c, uint8_t reg, uint8_t dta)
00084 {   
00085     char rd1;
00086     int rStat1 = i2c.read(i2cAdr, reg, &rd1, 1);
00087     char data[2];
00088     data[0]=(char)reg;
00089     data[1]=(char)dta;
00090     char rd2;
00091     int wStat = i2c.write(i2cAdr, data, 2);
00092     osDelay(100);
00093     int rStat2 = i2c.read(i2cAdr, reg, &rd2, 1);
00094     printf("%2d%2d%2d  %2x <- %2x  => %2x -> %2x \n", rStat1, wStat, rStat2, reg, dta, rd1, rd2);
00095 }
00096 
00097 static void config(I2CMasterRtos& i2c)
00098 {
00099     uint8_t ncfg=32;
00100     uint8_t regs[ncfg];
00101     uint8_t vals[ncfg];
00102     int cnt=0;
00103     regs[cnt]=0x6b;
00104     vals[cnt++]=(1<<7); // pwr 1 reg //: device reset
00105     regs[cnt]=0x6b;
00106     vals[cnt++]=1; // pwr 1 reg // clock from x gyro all pwr sav modes off
00107     regs[cnt]=0x19;
00108     vals[cnt++]=0;  // sample rate divider reg  // sapmle rate = gyro rate / (1+x)
00109     regs[cnt]=0x1a;
00110     vals[cnt++]=0;// conf  reg // no ext frame sync / no dig low pass set to 1 => 8kHz Sampling 
00111     regs[cnt]=0x1b;
00112     vals[cnt++]=0;// gyro conf  reg // no test mode and gyro range 250°/s
00113     regs[cnt]=0x1c;
00114     vals[cnt++]=0;// accl conf  reg // no test mode and accl range 2g
00115     regs[cnt]=0x23;
00116     vals[cnt++]=0xf<<3;// fifo conf  reg // accl + all gyro -> fifo
00117     regs[cnt]=0x6a;
00118     vals[cnt++]=(1<<2); // pwr 1 reg // fifo reset
00119     regs[cnt]=0x6a;
00120     vals[cnt++]=(1<<6); // pwr 1 reg // fifo on
00121 
00122     for(int i=0; i<cnt; i++)
00123         readModWrite(i2c, regs[i], vals[i]);
00124 }