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
Revision 12:6ddadcbbdca2, committed 2013-05-10
- Comitter:
- humlet
- Date:
- Fri May 10 07:34:24 2013 +0000
- Parent:
- 11:8c1d44595620
- Child:
- 13:530968937ccb
- Commit message:
- before removal of driver threads
Changed in this revision
I2CDriverTest04.h | Show annotated file Show diff for this revision Revisions of this file |
i2cRtos_api.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/I2CDriverTest04.h Thu May 09 20:52:07 2013 +0000 +++ b/I2CDriverTest04.h Fri May 10 07:34:24 2013 +0000 @@ -26,16 +26,16 @@ //printf("tst02\n"); config(i2c); + const char reg= 0x3a; + char result[64]; while(true) { //osci.write(0); i2c.frequency(g_freq); Thread::signal_wait(1<<5,osWaitForever); - // read back srf08 echo times (1+16 words) - const char reg= 0x3a; - char result[64]; + // read from MPU600's fifo + uint32_t t1=us_ticker_read(); int stat = i2c.read(i2cAdr, reg, result, g_len); - uint32_t dt=us_ticker_read()-t1; if(stat!=0) { //osci.write(1); @@ -53,7 +53,7 @@ Thread highPrioThread(highPrioThreadFun,0,osPriorityAboveNormal); RtosTimer highPrioTicker(highPrioCallBck, osTimerPeriodic, (void *)0); - Thread::wait(100); + Thread::wait(2000); highPrioTicker.start(1); #if defined(TARGET_LPC1768) @@ -92,10 +92,8 @@ void readModWrite(I2CMasterRtos& i2c, uint8_t reg, uint8_t dta) { - //printf("rmw01\n"); char rd1; int rStat1 = i2c.read(i2cAdr, reg, &rd1, 1); - //printf("rmw02\n"); char data[2]; data[0]=(char)reg; data[1]=(char)dta; @@ -103,7 +101,7 @@ int wStat = i2c.write(i2cAdr, data, 2); osDelay(100); int rStat2 = i2c.read(i2cAdr, reg, &rd2, 1); - printf("%2d%2d%2d %2x <- %2x => %2x -> %2x \n", rStat1, wStat, rStat2, reg, dta, rd1, rd2); + printf("(%3x%3x%3x) %2x <- %2x => %2x -> %2x \n", rStat1, wStat, rStat2, reg, dta, rd1, rd2); } static void config(I2CMasterRtos& i2c)
--- a/i2cRtos_api.c Thu May 09 20:52:07 2013 +0000 +++ b/i2cRtos_api.c Fri May 10 07:34:24 2013 +0000 @@ -10,8 +10,8 @@ #define I2C_STAT(x) (x->i2c->I2STAT) #define I2C_DAT(x) (x->i2c->I2DAT) -#include "gpio_api.h" -static gpio_t gpio[2]; // evillive +//#include "gpio_api.h" +//static gpio_t gpio[2]; // evillive enum I2cIsrCmd { readMst, @@ -67,7 +67,7 @@ static void i2cRtos_isr(uint32_t ch) { struct I2cIsrTransfer* tr=&(i2c_transfer[ch]); - if(tr->cmd==waitSI){ + if(tr->cmd==waitSI) { osSemaphoreRelease(isrIrqSem[ch].sem); NVIC_DisableIRQ(isrIrqSem[ch].irq); return; @@ -158,7 +158,7 @@ // wait for ISR finished -static void i2cRtos_wait_and_see(i2c_t *obj, int ch, uint32_t tmOut) //evillive +static inline void i2cRtos_wait_and_see(i2c_t *obj, int ch, uint32_t tmOut) //evillive { struct IsrIrqSem* iis = &(isrIrqSem[ch]); __disable_irq(); @@ -169,7 +169,8 @@ if(osSemaphoreWait(iis->sem, tmOut)!=1) NVIC_DisableIRQ(iis->irq); } -static inline void i2cRtos_waitSI(i2c_t *obj, uint32_t tmOut){ +static inline void i2cRtos_waitSI(i2c_t *obj, uint32_t tmOut) +{ int ch = i2c_get_channel(obj); i2c_transfer[ch].cmd = waitSI; i2cRtos_wait_and_see(obj, ch, tmOut); @@ -178,15 +179,15 @@ int i2cRtos_read(i2c_t *obj, int address, char *data, int length, int stop) { - gpio_write(&gpio[1], 1); + //gpio_write(&gpio[1], 1); int stat = i2c_start(obj); if ((stat != 0x10) && (stat != 0x08)) { i2c_stop(obj); return stat; } - gpio_write(&gpio[1], 0); + //gpio_write(&gpio[1], 0); int ch = i2c_get_channel(obj); - struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? + struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? tr->obj=obj; tr->cmd=readMst; tr->len=length; @@ -195,31 +196,36 @@ I2C_DAT(obj) = address | 0x01; i2cRtos_wait_and_see(obj, ch,2+(length>>2)); // timeout (2+len/4)ms stat = tr->stat; + //gpio_write(&gpio[1], 1); if(stat || stop) i2c_stop(obj); + //gpio_write(&gpio[1], 0); return stat; } int i2cRtos_write(i2c_t *obj, int address, const char *data, int length, int stop) { - gpio_write(&gpio[1], 1); + //gpio_write(&gpio[1], 1); int status = i2c_start(obj); if ((status != 0x10) && (status != 0x08)) { i2c_stop(obj); return status; } - gpio_write(&gpio[1], 0); + //gpio_write(&gpio[1], 0); + int ch = i2c_get_channel(obj); - struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? - tr->obj=obj; - tr->cmd=writeMst; - tr->len=length; - tr->cnt=-1; - tr->wData=data; + struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? + tr->obj = obj; + tr->cmd = writeMst; + tr->len = length; + tr->cnt = -1; + tr->wData = data; I2C_DAT(obj) = address & 0xfe; i2cRtos_wait_and_see(obj, ch, 2+(length>>2)); // timeout (2+len/4)ms - i2c_clear_SI(obj); // ... why? Also in official lib ... I guess this is the "write instead of start" bug + //i2c_clear_SI(obj); // ... why? Also in official lib ... I guess this is the "write instead of start" bug status = tr->stat; + //gpio_write(&gpio[1], 1); if(status || stop) i2c_stop(obj); + //gpio_write(&gpio[1], 0); return status; } @@ -260,7 +266,7 @@ int i2cRtos_slave_read(i2c_t *obj, char *data, int length) { int ch = i2c_get_channel(obj); - struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? + struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? tr->obj=obj; tr->cmd=readSlv; tr->len=length; @@ -280,7 +286,7 @@ return(0); } int ch = i2c_get_channel(obj); - struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? + struct I2cIsrTransfer* tr = &(i2c_transfer[ch]); // evilive fill it locally and then copy it in one go to (volatile) mem? tr->obj=obj; tr->cmd=writeSlv; tr->len=length; @@ -300,6 +306,7 @@ // setup semaphores and hook in ISRs void i2cRtos_init(i2c_t *obj, PinName sda, PinName scl) { + /* static int called=0; if(!called) { gpio_init(&gpio[0], p15, PIN_OUTPUT); @@ -308,7 +315,7 @@ called = 1; gpio_write(&gpio[0], 0); gpio_write(&gpio[1], 0); - + */ i2c_init(obj,sda,scl); uint32_t ch = i2c_get_channel(obj); #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)