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.
Dependencies: mbed-rtos mbed-src
Diff: I2CDriverTest02.h
- Revision:
- 3:967dde37e712
- Child:
- 4:eafa7efcd771
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CDriverTest02.h Fri Apr 19 21:33:29 2013 +0000
@@ -0,0 +1,75 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "I2CMasterRtos.h"
+#include "I2CSlaveRtos.h"
+
+const int freq = 100000;
+const int adr = 42;
+const int len=42;
+const char* cMsg="We are mbed, resistance is futile";
+
+static void rxMsg(I2CSlaveRtos& slv)
+{
+ char rMsg[len];
+ memset(rMsg,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);
+ } else
+ printf("god damned nation");
+}
+
+static void txMsg(I2CMasterRtos& mst)
+{
+ mst.write(adr,cMsg,len);
+ printf("thread %d has sent the message\n",Thread::gettid());
+}
+
+static void channel1(void const *args)
+{
+ I2CMasterRtos mst(p9,p10,freq);
+ I2CSlaveRtos slv(p9,p10,freq,adr);
+ while(1) {
+ rxMsg(slv);
+ Thread::wait(100);
+ txMsg(mst);
+ Thread::wait(100);
+ }
+}
+
+void channel2(void const *args)
+{
+ I2CMasterRtos mst(p28,p27,freq);
+ I2CSlaveRtos slv(p28,p27,freq,adr);
+ while(1) {
+ txMsg(mst);
+ Thread::wait(100);
+ rxMsg(slv);
+ Thread::wait(100);
+ }
+}
+
+int doit()
+{
+ Thread selftalk01(channel1,0,osPriorityAboveNormal);
+ Thread selftalk02(channel2,0,osPriorityAboveNormal);
+
+ uint32_t cnt=0;
+ while (++cnt<5) {
+ const uint32_t dt=1e6;
+ uint32_t tStart = us_ticker_read();
+ uint32_t tLast = tStart;
+ uint32_t tAct = tStart;
+ uint32_t tMe=0;
+ do {
+ tAct=us_ticker_read();
+ if(tAct>tLast) {
+ if(tAct==tLast+1)++tMe;
+ }
+ tLast=tAct;
+ } while(tAct-tStart<dt);
+ printf("dc=%5.2f \n", 100.0*(float)tMe/dt);
+ }
+ return 0;
+}
+