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
I2CDriverTest01.h@16:2c6432b37cce, 2014-05-17 (annotated)
- Committer:
- pHysiX
- Date:
- Sat May 17 11:56:46 2014 +0000
- Revision:
- 16:2c6432b37cce
- Parent:
- 14:352609d395c1
Modified to make 400kHz default
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 14:352609d395c1 | 1 | // A high prio thread reads at a rate of 1kHz from a SRF08 ultrasonic ranger |
humlet | 14:352609d395c1 | 2 | // data packets of different size, whereas the lower prio main thread measures the CPU time left. |
humlet | 14:352609d395c1 | 3 | |
humlet | 3:967dde37e712 | 4 | #include "mbed.h" |
humlet | 3:967dde37e712 | 5 | #include "rtos.h" |
humlet | 3:967dde37e712 | 6 | #include "I2CMasterRtos.h" |
humlet | 3:967dde37e712 | 7 | #include "stdint.h" |
humlet | 3:967dde37e712 | 8 | |
humlet | 4:eafa7efcd771 | 9 | volatile int g_disco=0; |
humlet | 14:352609d395c1 | 10 | volatile int g_len=2; |
humlet | 4:eafa7efcd771 | 11 | volatile int g_freq=100000; |
humlet | 3:967dde37e712 | 12 | |
humlet | 14:352609d395c1 | 13 | I2CMasterRtos i2c(p28, p27); |
humlet | 14:352609d395c1 | 14 | const uint32_t adr = 0x70<<1; |
humlet | 8:5be85bd4c5ba | 15 | |
humlet | 3:967dde37e712 | 16 | void highPrioCallBck(void const *args) |
humlet | 3:967dde37e712 | 17 | { |
humlet | 14:352609d395c1 | 18 | i2c.frequency(g_freq); |
humlet | 14:352609d395c1 | 19 | // read back srf08 echo times (1+16 words) |
humlet | 14:352609d395c1 | 20 | const char reg= 0x02; |
humlet | 14:352609d395c1 | 21 | char result[64]; |
humlet | 14:352609d395c1 | 22 | uint32_t t1=us_ticker_read(); |
humlet | 14:352609d395c1 | 23 | i2c.read(adr, reg, result, g_len); |
humlet | 14:352609d395c1 | 24 | uint32_t dt=us_ticker_read()-t1; |
humlet | 14:352609d395c1 | 25 | uint16_t dst=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1])); |
humlet | 14:352609d395c1 | 26 | if(--g_disco>0) printf("dst=%4dcm dt=%4dus\n",dst,dt); |
humlet | 3:967dde37e712 | 27 | } |
humlet | 3:967dde37e712 | 28 | |
humlet | 3:967dde37e712 | 29 | int doit() |
humlet | 3:967dde37e712 | 30 | { |
humlet | 3:967dde37e712 | 31 | RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0); |
humlet | 3:967dde37e712 | 32 | |
humlet | 14:352609d395c1 | 33 | Thread::wait(500); |
humlet | 14:352609d395c1 | 34 | // trigger srf08 ranging |
humlet | 14:352609d395c1 | 35 | const char regNcmd[2]= {0x00,0x54}; |
humlet | 14:352609d395c1 | 36 | i2c.write(adr, regNcmd, 2); |
humlet | 14:352609d395c1 | 37 | osDelay(200); |
humlet | 14:352609d395c1 | 38 | |
humlet | 3:967dde37e712 | 39 | highPrioTicker.start(1); |
humlet | 3:967dde37e712 | 40 | |
humlet | 6:5b98c902a659 | 41 | #if defined(TARGET_LPC1768) |
humlet | 4:eafa7efcd771 | 42 | const int nTest=7; |
humlet | 6:5b98c902a659 | 43 | const int freq[nTest]= {1e5, 1e5, 1e5, 4e5, 4e5, 4e5, 4e5}; |
humlet | 6:5b98c902a659 | 44 | const int len[nTest]= {1, 4, 6, 1, 6, 12, 25}; |
humlet | 6:5b98c902a659 | 45 | #elif defined(TARGET_LPC11U24) |
humlet | 6:5b98c902a659 | 46 | const int nTest=5; |
humlet | 6:5b98c902a659 | 47 | const int freq[nTest]= {1e5, 1e5, 4e5, 4e5, 4e5 }; |
humlet | 14:352609d395c1 | 48 | const int len[nTest]= {1, 6, 1, 6, 23}; |
humlet | 6:5b98c902a659 | 49 | #endif |
humlet | 6:5b98c902a659 | 50 | for(int i=0; i<nTest; ++i) { |
humlet | 4:eafa7efcd771 | 51 | g_freq = freq[i]; |
humlet | 4:eafa7efcd771 | 52 | g_len = len[i]; |
humlet | 4:eafa7efcd771 | 53 | printf("f=%d l=%d\n",g_freq,g_len); |
humlet | 4:eafa7efcd771 | 54 | Thread::wait(500); |
humlet | 3:967dde37e712 | 55 | const uint32_t dt=1e6; |
humlet | 3:967dde37e712 | 56 | uint32_t tStart = us_ticker_read(); |
humlet | 3:967dde37e712 | 57 | uint32_t tLast = tStart; |
humlet | 3:967dde37e712 | 58 | uint32_t tAct = tStart; |
humlet | 3:967dde37e712 | 59 | uint32_t tMe=0; |
humlet | 3:967dde37e712 | 60 | do { |
humlet | 3:967dde37e712 | 61 | tAct=us_ticker_read(); |
humlet | 14:352609d395c1 | 62 | #if defined(TARGET_LPC1768) |
humlet | 14:352609d395c1 | 63 | if(tAct==tLast+1)++tMe; |
humlet | 14:352609d395c1 | 64 | #elif defined(TARGET_LPC11U24) |
humlet | 14:352609d395c1 | 65 | uint32_t delta = tAct-tLast; |
humlet | 14:352609d395c1 | 66 | if(delta<=2)tMe+=delta; // on the 11U24 this loop takes a bit longer than 1µs (ISR ~3µs, task switch ~8µs) |
humlet | 14:352609d395c1 | 67 | #endif |
humlet | 3:967dde37e712 | 68 | tLast=tAct; |
humlet | 3:967dde37e712 | 69 | } while(tAct-tStart<dt); |
humlet | 3:967dde37e712 | 70 | printf("dc=%5.2f \n", 100.0*(float)tMe/dt); |
humlet | 14:352609d395c1 | 71 | g_disco=5; |
humlet | 4:eafa7efcd771 | 72 | while(g_disco>0); |
humlet | 3:967dde37e712 | 73 | } |
humlet | 3:967dde37e712 | 74 | return 0; |
humlet | 3:967dde37e712 | 75 | } |