Modified version of the official mbed lib providing a RTOS enabled i2c-driver based on the official i2c-C-api.

Dependencies:   mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CDriverTest02.h Source File

I2CDriverTest02.h

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "I2CMasterRtos.h"
00004 #include "I2CSlaveRtos.h"
00005 
00006 const int freq = 400000;
00007 const int adr = 42;
00008 const int len=42;
00009 const char* mstMsg="We are mbed, resistance is futile";
00010 const char* slvMsg="Fine with me, let's get addicted";
00011 
00012 static void slvRxMsg(I2CSlaveRtos& slv)
00013 {
00014     char rxMsg[len];
00015     memset(rxMsg,0,len);
00016     if ( slv.receive() == I2CSlave::WriteAddressed) {
00017         slv.read(rxMsg, len);
00018         printf("thread %X received message as i2c slave: '%s'\n",Thread::gettid(),rxMsg);
00019     } else
00020         printf("Ouch slv rx failure\n");
00021 }
00022 
00023 static void slvTxMsg(I2CSlaveRtos& slv)
00024 {
00025     if ( slv.receive() == I2CSlave::ReadAddressed) {
00026         slv.write(slvMsg, len);
00027     } else
00028         printf("Ouch slv tx failure\n");
00029 }
00030 
00031 static void mstTxMsg(I2CMasterRtos& mst)
00032 {
00033     mst.write(adr,mstMsg,len);
00034 }
00035 
00036 static void mstRxMsg(I2CMasterRtos& mst)
00037 {
00038     char rxMsg[len];
00039     memset(rxMsg,0,len);
00040     mst.read(adr,rxMsg,len);
00041     printf("thread %X received message as i2c master: '%s'\n",Thread::gettid(),rxMsg);
00042 }
00043 
00044 static void channel1(void const *args)
00045 {
00046     I2CMasterRtos mst(p9,p10,freq);
00047     I2CSlaveRtos slv(p9,p10,freq,adr);
00048     while(1) {
00049         slvRxMsg(slv);
00050         slvTxMsg(slv);
00051         Thread::wait(100);
00052         mstTxMsg(mst);
00053         Thread::wait(100);
00054         mstRxMsg(mst);
00055     }
00056 }
00057 
00058 void channel2(void const *args)
00059 {
00060     I2CMasterRtos mst(p28,p27,freq);
00061     I2CSlaveRtos slv(p28,p27,freq,adr);
00062     while(1) {
00063         Thread::wait(100);
00064         mstTxMsg(mst);
00065         Thread::wait(100);
00066         mstRxMsg(mst);
00067         slvRxMsg(slv);
00068         slvTxMsg(slv);
00069     }
00070 }
00071 
00072 int doit()
00073 {
00074     Thread selftalk01(channel1,0,osPriorityAboveNormal);
00075     Thread selftalk02(channel2,0,osPriorityAboveNormal);
00076 
00077     Thread::wait(10000);
00078 
00079     return 0;
00080 }
00081