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

Committer:
humlet
Date:
Tue Apr 30 19:12:57 2013 +0000
Revision:
8:5be85bd4c5ba
Parent:
6:5b98c902a659
Child:
14:352609d395c1
alpha2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
humlet 3:967dde37e712 1 #include "mbed.h"
humlet 3:967dde37e712 2 #include "rtos.h"
humlet 3:967dde37e712 3 #include "I2CMasterRtos.h"
humlet 3:967dde37e712 4 #include "stdint.h"
humlet 8:5be85bd4c5ba 5 #include "DigitalOut.h"
humlet 3:967dde37e712 6
humlet 4:eafa7efcd771 7 volatile int g_disco=0;
humlet 4:eafa7efcd771 8 volatile int g_len=0;
humlet 4:eafa7efcd771 9 volatile int g_freq=100000;
humlet 3:967dde37e712 10 volatile osThreadId i2cDrvThrdID;
humlet 3:967dde37e712 11
humlet 8:5be85bd4c5ba 12 DigitalOut osci(p12);
humlet 8:5be85bd4c5ba 13
humlet 3:967dde37e712 14 void highPrioCallBck(void const *args)
humlet 3:967dde37e712 15 {
humlet 3:967dde37e712 16 osSignalSet(i2cDrvThrdID, 1<<5);
humlet 3:967dde37e712 17 }
humlet 3:967dde37e712 18
humlet 3:967dde37e712 19 void highPrioThreadFun(void const *args)
humlet 3:967dde37e712 20 {
humlet 3:967dde37e712 21 i2cDrvThrdID = Thread::gettid();
humlet 3:967dde37e712 22 I2CMasterRtos i2c(p28, p27);
humlet 3:967dde37e712 23 const uint32_t adr = 0x70<<1;
humlet 3:967dde37e712 24
humlet 3:967dde37e712 25 // trigger on srf08 ranging
humlet 3:967dde37e712 26 const char regNcmd[2]= {0x00,0x51};
humlet 3:967dde37e712 27 i2c.write(adr, regNcmd, 2);
humlet 8:5be85bd4c5ba 28 osDelay(200);
humlet 3:967dde37e712 29 while(true) {
humlet 4:eafa7efcd771 30 i2c.frequency(g_freq);
humlet 3:967dde37e712 31 Thread::signal_wait(1<<5,osWaitForever);
humlet 3:967dde37e712 32 // read back srf08 echo times (1+16 words)
humlet 3:967dde37e712 33 const char reg= 0x02;
humlet 3:967dde37e712 34 char result[64];
humlet 3:967dde37e712 35 uint32_t t1=us_ticker_read();
humlet 8:5be85bd4c5ba 36 i2c.read(adr, reg, result, g_len);
humlet 3:967dde37e712 37 uint32_t dt=us_ticker_read()-t1;
humlet 3:967dde37e712 38 uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
humlet 6:5b98c902a659 39
humlet 4:eafa7efcd771 40 if(--g_disco>0) printf("tm=%4dus dt=%4dus\n",tm,dt);
humlet 3:967dde37e712 41 }
humlet 3:967dde37e712 42 }
humlet 3:967dde37e712 43
humlet 3:967dde37e712 44 int doit()
humlet 3:967dde37e712 45 {
humlet 3:967dde37e712 46 Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal);
humlet 3:967dde37e712 47 RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0);
humlet 3:967dde37e712 48
humlet 3:967dde37e712 49 Thread::wait(100);
humlet 3:967dde37e712 50 highPrioTicker.start(1);
humlet 3:967dde37e712 51
humlet 6:5b98c902a659 52 #if defined(TARGET_LPC1768)
humlet 4:eafa7efcd771 53 const int nTest=7;
humlet 6:5b98c902a659 54 const int freq[nTest]= {1e5, 1e5, 1e5, 4e5, 4e5, 4e5, 4e5};
humlet 6:5b98c902a659 55 const int len[nTest]= {1, 4, 6, 1, 6, 12, 25};
humlet 6:5b98c902a659 56 #elif defined(TARGET_LPC11U24)
humlet 6:5b98c902a659 57 const int nTest=5;
humlet 6:5b98c902a659 58 const int freq[nTest]= {1e5, 1e5, 4e5, 4e5, 4e5 };
humlet 8:5be85bd4c5ba 59 const int len[nTest]= {1, 4, 1, 6, 16};
humlet 6:5b98c902a659 60 #endif
humlet 6:5b98c902a659 61 for(int i=0; i<nTest; ++i) {
humlet 4:eafa7efcd771 62 g_freq = freq[i];
humlet 4:eafa7efcd771 63 g_len = len[i];
humlet 4:eafa7efcd771 64 printf("f=%d l=%d\n",g_freq,g_len);
humlet 4:eafa7efcd771 65 Thread::wait(500);
humlet 3:967dde37e712 66 const uint32_t dt=1e6;
humlet 3:967dde37e712 67 uint32_t tStart = us_ticker_read();
humlet 3:967dde37e712 68 uint32_t tLast = tStart;
humlet 3:967dde37e712 69 uint32_t tAct = tStart;
humlet 3:967dde37e712 70 uint32_t tMe=0;
humlet 3:967dde37e712 71 do {
humlet 3:967dde37e712 72 tAct=us_ticker_read();
humlet 3:967dde37e712 73 if(tAct>tLast) {
humlet 3:967dde37e712 74 if(tAct==tLast+1)++tMe;
humlet 8:5be85bd4c5ba 75 osci.write(tAct&1);
humlet 3:967dde37e712 76 }
humlet 3:967dde37e712 77 tLast=tAct;
humlet 3:967dde37e712 78 } while(tAct-tStart<dt);
humlet 3:967dde37e712 79 printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
humlet 4:eafa7efcd771 80 g_disco=10;
humlet 4:eafa7efcd771 81 while(g_disco>0);
humlet 3:967dde37e712 82 }
humlet 3:967dde37e712 83 return 0;
humlet 3:967dde37e712 84 }