Ian Hua / I2cRtosDriver

Fork of I2cRtosDriver by Helmut Schmücker

Committer:
humlet
Date:
Thu May 09 20:52:07 2013 +0000
Revision:
11:8c1d44595620
Parent:
10:e3d6c92ff222
Child:
12:6ddadcbbdca2
seem to work without  tweaked mbed lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
humlet 7:04824382eafb 1 #include "mbed.h"
humlet 7:04824382eafb 2 #include "rtos.h"
humlet 7:04824382eafb 3 #include "I2CMasterRtos.h"
humlet 7:04824382eafb 4 #include "stdint.h"
humlet 7:04824382eafb 5
humlet 11:8c1d44595620 6 //DigitalOut osci(p16);
humlet 10:e3d6c92ff222 7
humlet 7:04824382eafb 8 volatile int g_disco=0;
humlet 7:04824382eafb 9 volatile int g_len=0;
humlet 7:04824382eafb 10 volatile int g_freq=100000;
humlet 7:04824382eafb 11 const uint32_t i2cAdr = 0x68<<1;
humlet 7:04824382eafb 12 volatile osThreadId i2cDrvThrdID;
humlet 7:04824382eafb 13
humlet 7:04824382eafb 14 static void config(I2CMasterRtos& i2c);
humlet 7:04824382eafb 15
humlet 7:04824382eafb 16 void highPrioCallBck(void const *args)
humlet 7:04824382eafb 17 {
humlet 7:04824382eafb 18 osSignalSet(i2cDrvThrdID, 1<<5);
humlet 7:04824382eafb 19 }
humlet 7:04824382eafb 20
humlet 7:04824382eafb 21 void highPrioThreadFun(void const *args)
humlet 7:04824382eafb 22 {
humlet 10:e3d6c92ff222 23 //printf("tst01\n");
humlet 7:04824382eafb 24 i2cDrvThrdID = Thread::gettid();
humlet 7:04824382eafb 25 I2CMasterRtos i2c(p28, p27);
humlet 10:e3d6c92ff222 26 //printf("tst02\n");
humlet 7:04824382eafb 27 config(i2c);
humlet 7:04824382eafb 28
humlet 7:04824382eafb 29 while(true) {
humlet 11:8c1d44595620 30 //osci.write(0);
humlet 7:04824382eafb 31 i2c.frequency(g_freq);
humlet 7:04824382eafb 32 Thread::signal_wait(1<<5,osWaitForever);
humlet 7:04824382eafb 33 // read back srf08 echo times (1+16 words)
humlet 7:04824382eafb 34 const char reg= 0x3a;
humlet 7:04824382eafb 35 char result[64];
humlet 7:04824382eafb 36 uint32_t t1=us_ticker_read();
humlet 10:e3d6c92ff222 37 int stat = i2c.read(i2cAdr, reg, result, g_len);
humlet 10:e3d6c92ff222 38
humlet 7:04824382eafb 39 uint32_t dt=us_ticker_read()-t1;
humlet 10:e3d6c92ff222 40 if(stat!=0) {
humlet 11:8c1d44595620 41 //osci.write(1);
humlet 10:e3d6c92ff222 42 printf("\n%x %d %d %d\n",stat,g_freq,g_len,dt);
humlet 10:e3d6c92ff222 43 exit(0);
humlet 10:e3d6c92ff222 44 }
humlet 7:04824382eafb 45 uint16_t tm=((static_cast<uint16_t>(result[0])<<8)|static_cast<uint16_t>(result[1]));
humlet 7:04824382eafb 46
humlet 7:04824382eafb 47 if(--g_disco>0) printf("tm=%8d dt=%4dus\n",tm,dt);
humlet 7:04824382eafb 48 }
humlet 7:04824382eafb 49 }
humlet 7:04824382eafb 50
humlet 7:04824382eafb 51 int doit()
humlet 7:04824382eafb 52 {
humlet 7:04824382eafb 53 Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal);
humlet 7:04824382eafb 54 RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0);
humlet 7:04824382eafb 55
humlet 7:04824382eafb 56 Thread::wait(100);
humlet 7:04824382eafb 57 highPrioTicker.start(1);
humlet 10:e3d6c92ff222 58
humlet 7:04824382eafb 59 #if defined(TARGET_LPC1768)
humlet 7:04824382eafb 60 const int nTest=7;
humlet 7:04824382eafb 61 const int freq[nTest]= {1e5, 1e5, 1e5, 4e5, 4e5, 4e5, 4e5};
humlet 10:e3d6c92ff222 62 const int len[nTest]= {1, 4, 6, 1, 6, 12, 36};
humlet 7:04824382eafb 63 #elif defined(TARGET_LPC11U24)
humlet 7:04824382eafb 64 const int nTest=5;
humlet 7:04824382eafb 65 const int freq[nTest]= {1e5, 1e5, 4e5, 4e5, 4e5 };
humlet 8:5be85bd4c5ba 66 const int len[nTest]= {1, 4, 1, 6, 16};
humlet 7:04824382eafb 67 #endif
humlet 7:04824382eafb 68 for(int i=0; i<nTest; ++i) {
humlet 7:04824382eafb 69 g_freq = freq[i];
humlet 7:04824382eafb 70 g_len = len[i];
humlet 7:04824382eafb 71 printf("f=%d l=%d\n",g_freq,g_len);
humlet 7:04824382eafb 72 Thread::wait(500);
humlet 7:04824382eafb 73 const uint32_t dt=1e6;
humlet 7:04824382eafb 74 uint32_t tStart = us_ticker_read();
humlet 7:04824382eafb 75 uint32_t tLast = tStart;
humlet 7:04824382eafb 76 uint32_t tAct = tStart;
humlet 7:04824382eafb 77 uint32_t tMe=0;
humlet 7:04824382eafb 78 do {
humlet 7:04824382eafb 79 tAct=us_ticker_read();
humlet 7:04824382eafb 80 if(tAct>tLast) {
humlet 7:04824382eafb 81 if(tAct==tLast+1)++tMe;
humlet 8:5be85bd4c5ba 82 //tLast=tAct;
humlet 7:04824382eafb 83 }
humlet 7:04824382eafb 84 tLast=tAct;
humlet 7:04824382eafb 85 } while(tAct-tStart<dt);
humlet 7:04824382eafb 86 printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
humlet 7:04824382eafb 87 g_disco=10;
humlet 7:04824382eafb 88 while(g_disco>0);
humlet 7:04824382eafb 89 }
humlet 7:04824382eafb 90 return 0;
humlet 7:04824382eafb 91 }
humlet 7:04824382eafb 92
humlet 7:04824382eafb 93 void readModWrite(I2CMasterRtos& i2c, uint8_t reg, uint8_t dta)
humlet 10:e3d6c92ff222 94 {
humlet 10:e3d6c92ff222 95 //printf("rmw01\n");
humlet 7:04824382eafb 96 char rd1;
humlet 7:04824382eafb 97 int rStat1 = i2c.read(i2cAdr, reg, &rd1, 1);
humlet 10:e3d6c92ff222 98 //printf("rmw02\n");
humlet 7:04824382eafb 99 char data[2];
humlet 7:04824382eafb 100 data[0]=(char)reg;
humlet 7:04824382eafb 101 data[1]=(char)dta;
humlet 7:04824382eafb 102 char rd2;
humlet 7:04824382eafb 103 int wStat = i2c.write(i2cAdr, data, 2);
humlet 7:04824382eafb 104 osDelay(100);
humlet 7:04824382eafb 105 int rStat2 = i2c.read(i2cAdr, reg, &rd2, 1);
humlet 7:04824382eafb 106 printf("%2d%2d%2d %2x <- %2x => %2x -> %2x \n", rStat1, wStat, rStat2, reg, dta, rd1, rd2);
humlet 7:04824382eafb 107 }
humlet 7:04824382eafb 108
humlet 7:04824382eafb 109 static void config(I2CMasterRtos& i2c)
humlet 7:04824382eafb 110 {
humlet 7:04824382eafb 111 uint8_t ncfg=32;
humlet 7:04824382eafb 112 uint8_t regs[ncfg];
humlet 7:04824382eafb 113 uint8_t vals[ncfg];
humlet 7:04824382eafb 114 int cnt=0;
humlet 7:04824382eafb 115 regs[cnt]=0x6b;
humlet 7:04824382eafb 116 vals[cnt++]=(1<<7); // pwr 1 reg //: device reset
humlet 7:04824382eafb 117 regs[cnt]=0x6b;
humlet 7:04824382eafb 118 vals[cnt++]=1; // pwr 1 reg // clock from x gyro all pwr sav modes off
humlet 7:04824382eafb 119 regs[cnt]=0x19;
humlet 7:04824382eafb 120 vals[cnt++]=0; // sample rate divider reg // sapmle rate = gyro rate / (1+x)
humlet 7:04824382eafb 121 regs[cnt]=0x1a;
humlet 10:e3d6c92ff222 122 vals[cnt++]=0;// conf reg // no ext frame sync / no dig low pass set to 1 => 8kHz Sampling
humlet 7:04824382eafb 123 regs[cnt]=0x1b;
humlet 7:04824382eafb 124 vals[cnt++]=0;// gyro conf reg // no test mode and gyro range 250°/s
humlet 7:04824382eafb 125 regs[cnt]=0x1c;
humlet 7:04824382eafb 126 vals[cnt++]=0;// accl conf reg // no test mode and accl range 2g
humlet 7:04824382eafb 127 regs[cnt]=0x23;
humlet 7:04824382eafb 128 vals[cnt++]=0xf<<3;// fifo conf reg // accl + all gyro -> fifo
humlet 7:04824382eafb 129 regs[cnt]=0x6a;
humlet 7:04824382eafb 130 vals[cnt++]=(1<<2); // pwr 1 reg // fifo reset
humlet 7:04824382eafb 131 regs[cnt]=0x6a;
humlet 7:04824382eafb 132 vals[cnt++]=(1<<6); // pwr 1 reg // fifo on
humlet 7:04824382eafb 133
humlet 7:04824382eafb 134 for(int i=0; i<cnt; i++)
humlet 7:04824382eafb 135 readModWrite(i2c, regs[i], vals[i]);
humlet 7:04824382eafb 136 }