Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-RtosI2cDriver by
Diff: I2CDriverTest02.h
- Revision:
- 4:eafa7efcd771
- Parent:
- 3:967dde37e712
- Child:
- 6:5b98c902a659
--- a/I2CDriverTest02.h Fri Apr 19 21:33:29 2013 +0000
+++ b/I2CDriverTest02.h Sat Apr 20 09:52:09 2013 +0000
@@ -3,26 +3,44 @@
#include "I2CMasterRtos.h"
#include "I2CSlaveRtos.h"
-const int freq = 100000;
+const int freq = 400000;
const int adr = 42;
const int len=42;
-const char* cMsg="We are mbed, resistance is futile";
+const char* mstMsg="We are mbed, resistance is futile";
+const char* slvMsg="Fine with me, let's get addicted";
-static void rxMsg(I2CSlaveRtos& slv)
+static void slvRxMsg(I2CSlaveRtos& slv)
{
- char rMsg[len];
- memset(rMsg,0,len);
+ char rxMsg[len];
+ memset(rxMsg,0,len);
if ( slv.receive() == I2CSlave::WriteAddressed) {
- slv.read(rMsg, len); // have to add 1 byte for the address (bug report!)
- printf("thread %d received message '%s'\n",Thread::gettid(),rxMsg);
+ slv.read(rxMsg, len);
+ //rMsg[len-1]=0;
+ printf("thread %X received message as i2c slave: '%s'\n",Thread::gettid(),rxMsg);
} else
- printf("god damned nation");
+ printf("Ouch slv rx failure\n");
}
-static void txMsg(I2CMasterRtos& mst)
+static void slvTxMsg(I2CSlaveRtos& slv)
+{
+ if ( slv.receive() == I2CSlave::ReadAddressed) {
+ slv.write(slvMsg, len);
+ } else
+ printf("Ouch slv tx failure\n");
+}
+
+static void mstTxMsg(I2CMasterRtos& mst)
{
- mst.write(adr,cMsg,len);
- printf("thread %d has sent the message\n",Thread::gettid());
+ mst.write(adr,mstMsg,len);
+ //printf("thread %X has sent the message\n",Thread::gettid());
+}
+
+static void mstRxMsg(I2CMasterRtos& mst)
+{
+ char rxMsg[len];
+ memset(rxMsg,0,len);
+ mst.read(adr,rxMsg,len);
+ printf("thread %X received message as i2c master: '%s'\n",Thread::gettid(),rxMsg);
}
static void channel1(void const *args)
@@ -30,10 +48,12 @@
I2CMasterRtos mst(p9,p10,freq);
I2CSlaveRtos slv(p9,p10,freq,adr);
while(1) {
- rxMsg(slv);
+ slvRxMsg(slv);
+ slvTxMsg(slv);
Thread::wait(100);
- txMsg(mst);
+ mstTxMsg(mst);
Thread::wait(100);
+ mstRxMsg(mst);
}
}
@@ -42,10 +62,12 @@
I2CMasterRtos mst(p28,p27,freq);
I2CSlaveRtos slv(p28,p27,freq,adr);
while(1) {
- txMsg(mst);
+ Thread::wait(100);
+ mstTxMsg(mst);
Thread::wait(100);
- rxMsg(slv);
- Thread::wait(100);
+ mstRxMsg(mst);
+ slvRxMsg(slv);
+ slvTxMsg(slv);
}
}
