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
Diff: I2CDriverTest01.h
- Revision:
- 3:967dde37e712
- Child:
- 4:eafa7efcd771
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CDriverTest01.h Fri Apr 19 21:33:29 2013 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" +#include "rtos.h" +#include "I2CMasterRtos.h" +#include "stdint.h" + +volatile int disco=0; +volatile osThreadId i2cDrvThrdID; + +void highPrioCallBck(void const *args) +{ + osSignalSet(i2cDrvThrdID, 1<<5); +} + +void highPrioThreadFun(void const *args) +{ + i2cDrvThrdID = Thread::gettid(); + I2CMasterRtos i2c(p28, p27); + i2c.frequency(400000); + const uint32_t adr = 0x70<<1; + + // trigger on srf08 ranging + const char regNcmd[2]= {0x00,0x51}; + i2c.write(adr, regNcmd, 2); + + while(true) { + 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); + + 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); + } +} + +int doit() +{ + Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal); + RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0); + + Thread::wait(100); + highPrioTicker.start(1); + + uint32_t cnt=0; + while (++cnt<5) { + const uint32_t dt=1e6; + uint32_t tStart = us_ticker_read(); + uint32_t tLast = tStart; + uint32_t tAct = tStart; + uint32_t tMe=0; + do { + tAct=us_ticker_read(); + if(tAct>tLast) { + if(tAct==tLast+1)++tMe; + } + tLast=tAct; + } while(tAct-tStart<dt); + printf("dc=%5.2f \n", 100.0*(float)tMe/dt); + disco=10; + while(disco>0); + Thread::wait(1000); + } + return 0; +} \ No newline at end of file