Helmut Schmücker / I2cRtosDriver

Dependencies:   mbed-rtos

Fork of mbed-RtosI2cDriver by Helmut Schmücker

Committer:
humlet
Date:
Fri May 10 20:38:35 2013 +0000
Revision:
13:530968937ccb
Parent:
7:04824382eafb
Child:
14:352609d395c1
happyhappyjoyjoy

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 13:530968937ccb 4 #include "I2CSlaveRtos.h"
humlet 7:04824382eafb 5
humlet 13:530968937ccb 6 const int freq = 400000;
humlet 13:530968937ccb 7 const int adr = 42<<1;
humlet 13:530968937ccb 8 const int len=34;
humlet 13:530968937ccb 9 const char mstMsg[len]="We are mbed, resistance is futile";
humlet 13:530968937ccb 10 const char slvMsg[len]="Fine with me, let's get addicted ";
humlet 7:04824382eafb 11
humlet 13:530968937ccb 12 static void slvRxMsg(I2CSlaveRtos& slv)
humlet 13:530968937ccb 13 {
humlet 13:530968937ccb 14 char rxMsg[len];
humlet 13:530968937ccb 15 memset(rxMsg,0,len);
humlet 13:530968937ccb 16 if ( slv.receive() == I2CSlave::WriteAddressed ) {
humlet 13:530968937ccb 17 int cnt=0;
humlet 13:530968937ccb 18 while(cnt<len) rxMsg[cnt++]=slv.read();
humlet 13:530968937ccb 19 slv.stop(); // stop sretching low level of scl
humlet 13:530968937ccb 20 printf("thread %x received message (sz=%d) as i2c slave: '%s'\n",Thread::gettid(),cnt,rxMsg);
humlet 13:530968937ccb 21 } else
humlet 13:530968937ccb 22 printf("Ouch slv rx failure\n");
humlet 13:530968937ccb 23 }
humlet 7:04824382eafb 24
humlet 13:530968937ccb 25 static void slvTxMsg(I2CSlaveRtos& slv)
humlet 13:530968937ccb 26 {
humlet 13:530968937ccb 27 if ( slv.receive()==I2CSlave::ReadAddressed) {
humlet 13:530968937ccb 28 int cnt=0;
humlet 13:530968937ccb 29 while(cnt<len && slv.write(slvMsg[cnt++]));
humlet 13:530968937ccb 30 slv.stop(); // stop sretching low level of scl
humlet 13:530968937ccb 31 } else
humlet 13:530968937ccb 32 printf("Ouch slv tx failure\n");
humlet 13:530968937ccb 33 }
humlet 7:04824382eafb 34
humlet 13:530968937ccb 35 static void mstTxMsg(I2CMasterRtos& mst)
humlet 7:04824382eafb 36 {
humlet 13:530968937ccb 37 mst.start();
humlet 13:530968937ccb 38 if(!mst.write(adr & 0xfe))printf("adr+W not acked\n");
humlet 13:530968937ccb 39 int cnt=0;
humlet 13:530968937ccb 40 while(cnt<len && mst.write(mstMsg[cnt++]));
humlet 13:530968937ccb 41 // give the slave a chance to stop stretching scl to low, otherwise we will busy wait for the stop forever
humlet 13:530968937ccb 42 while(!mst.stop())Thread::wait(1);
humlet 7:04824382eafb 43 }
humlet 7:04824382eafb 44
humlet 13:530968937ccb 45 static void mstRxMsg(I2CMasterRtos& mst)
humlet 7:04824382eafb 46 {
humlet 13:530968937ccb 47 char rxMsg[len];
humlet 13:530968937ccb 48 memset(rxMsg,0,len);
humlet 7:04824382eafb 49
humlet 13:530968937ccb 50 mst.lock(); // no special reason, just a test
humlet 13:530968937ccb 51 mst.start();
humlet 13:530968937ccb 52 if(!mst.write(adr | 0x01))printf("adr+R not acked\n");
humlet 13:530968937ccb 53 int cnt=0;
humlet 13:530968937ccb 54 while(cnt<len-1) rxMsg[cnt++]=mst.read(1);
humlet 13:530968937ccb 55 mst.unlock();
humlet 13:530968937ccb 56 rxMsg[cnt++]=mst.read(0);
humlet 13:530968937ccb 57 // give the slave a chance to stop stretching scl to low, otherwise we will busy wait for the stop forever
humlet 13:530968937ccb 58 while(!mst.stop())Thread::wait(1);
humlet 13:530968937ccb 59 printf("thread %x received message (sz=%d) as i2c master: '%s'\n",Thread::gettid(),cnt,rxMsg);
humlet 7:04824382eafb 60 }
humlet 7:04824382eafb 61
humlet 13:530968937ccb 62 static void channel1(void const *args)
humlet 13:530968937ccb 63 {
humlet 13:530968937ccb 64 I2CMasterRtos mst(p9,p10,freq);
humlet 13:530968937ccb 65 I2CSlaveRtos slv(p9,p10,freq,adr);
humlet 13:530968937ccb 66 while(1) {
humlet 13:530968937ccb 67 slvRxMsg(slv);
humlet 13:530968937ccb 68 slvTxMsg(slv);
humlet 13:530968937ccb 69 Thread::wait(100);
humlet 13:530968937ccb 70 mstTxMsg(mst);
humlet 13:530968937ccb 71 Thread::wait(100);
humlet 13:530968937ccb 72 mstRxMsg(mst);
humlet 13:530968937ccb 73 }
humlet 13:530968937ccb 74 }
humlet 13:530968937ccb 75
humlet 13:530968937ccb 76 void channel2(void const *args)
humlet 13:530968937ccb 77 {
humlet 13:530968937ccb 78 I2CMasterRtos mst(p28,p27,freq);
humlet 13:530968937ccb 79 I2CSlaveRtos slv(p28,p27,freq,adr);
humlet 13:530968937ccb 80 while(1) {
humlet 13:530968937ccb 81 Thread::wait(100);
humlet 13:530968937ccb 82 mstTxMsg(mst);
humlet 13:530968937ccb 83 Thread::wait(100);
humlet 13:530968937ccb 84 mstRxMsg(mst);
humlet 13:530968937ccb 85 slvRxMsg(slv);
humlet 13:530968937ccb 86 slvTxMsg(slv);
humlet 13:530968937ccb 87 }
humlet 13:530968937ccb 88 }
humlet 7:04824382eafb 89
humlet 7:04824382eafb 90 int doit()
humlet 7:04824382eafb 91 {
humlet 13:530968937ccb 92 Thread selftalk01(channel1,0);
humlet 13:530968937ccb 93 Thread selftalk02(channel2,0);
humlet 13:530968937ccb 94 Thread::wait(5000);
humlet 7:04824382eafb 95 return 0;
humlet 7:04824382eafb 96 }
humlet 7:04824382eafb 97