i2c slave working version 1.0

Dependencies:   mbed-rtos mbed

Fork of freeslave_write2master_working by Seeker of Truth ,

Committer:
greenroshks
Date:
Wed Dec 10 10:33:56 2014 +0000
Revision:
4:133b49d51b18
Parent:
3:5e68dd4bbb70
Child:
5:ce0bb4174ca4
slave code updated with threads and mail boxes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
viswachaitanya 0:24e80ff2c7b1 1 #include "mbed.h"
greenroshks 4:133b49d51b18 2 #include "rtos.h"
viswachaitanya 0:24e80ff2c7b1 3
viswachaitanya 0:24e80ff2c7b1 4 void write_to_master(char); //function to write data to master
greenroshks 4:133b49d51b18 5 Thread * ptr_t_i2c;
viswachaitanya 0:24e80ff2c7b1 6 I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave
viswachaitanya 0:24e80ff2c7b1 7 Serial pc (USBTX,USBRX);
greenroshks 2:458f9e603814 8 InterruptIn interrupt(D9);
greenroshks 4:133b49d51b18 9
greenroshks 4:133b49d51b18 10 typedef struct
greenroshks 2:458f9e603814 11 {
greenroshks 4:133b49d51b18 12 char data; // To avoid dynamic memory allocation
greenroshks 4:133b49d51b18 13 int length;
greenroshks 4:133b49d51b18 14 }i2c_data;
viswachaitanya 0:24e80ff2c7b1 15
greenroshks 4:133b49d51b18 16 Mail<i2c_data,16> i2c_data_receive;
greenroshks 4:133b49d51b18 17 Mail<i2c_data,16> i2c_data_send;
greenroshks 4:133b49d51b18 18
greenroshks 4:133b49d51b18 19 void FUNC_I2C_WRITE2CDMS(char *data_receive,char *data_send, int length=1)
greenroshks 4:133b49d51b18 20 {
greenroshks 4:133b49d51b18 21
greenroshks 2:458f9e603814 22 int slave_status = 1;
greenroshks 2:458f9e603814 23 while(slave_status)
greenroshks 2:458f9e603814 24 {
viswachaitanya 0:24e80ff2c7b1 25 slave.address(0x20);
greenroshks 2:458f9e603814 26 if(slave.receive()==1)
greenroshks 2:458f9e603814 27 {
greenroshks 4:133b49d51b18 28 slave_status=slave.write(data_send,length);
greenroshks 3:5e68dd4bbb70 29 // printf("\n In the while loop\n");
greenroshks 2:458f9e603814 30
greenroshks 4:133b49d51b18 31 /*if(!slave_status)
greenroshks 2:458f9e603814 32 {
greenroshks 3:5e68dd4bbb70 33 printf("slave wrote %c to master\n\r",*data);
greenroshks 2:458f9e603814 34
greenroshks 3:5e68dd4bbb70 35 }*/
greenroshks 2:458f9e603814 36
viswachaitanya 0:24e80ff2c7b1 37 }
greenroshks 3:5e68dd4bbb70 38 else
greenroshks 3:5e68dd4bbb70 39 if(slave.receive()==3 || slave.receive()==2)
greenroshks 3:5e68dd4bbb70 40 {
greenroshks 4:133b49d51b18 41 //i2c_data *i2c_data_r = i2c_data_receive.alloc();
greenroshks 4:133b49d51b18 42 slave_status=slave.read(data_receive,length);
greenroshks 3:5e68dd4bbb70 43 /*if(!slave_status)
greenroshks 3:5e68dd4bbb70 44 {
greenroshks 3:5e68dd4bbb70 45 printf("1 read %c from master\n\r",*data);
greenroshks 3:5e68dd4bbb70 46 }*/
greenroshks 3:5e68dd4bbb70 47 }
greenroshks 2:458f9e603814 48 //slave.stop();
greenroshks 2:458f9e603814 49 }
viswachaitanya 0:24e80ff2c7b1 50 printf("2 done");
greenroshks 4:133b49d51b18 51
greenroshks 2:458f9e603814 52 }
greenroshks 2:458f9e603814 53
greenroshks 4:133b49d51b18 54 void T_I2C_BAE(void const * args)
greenroshks 2:458f9e603814 55 {
greenroshks 4:133b49d51b18 56 char data_send,data_receive;
greenroshks 4:133b49d51b18 57 data_send = 'a';
greenroshks 4:133b49d51b18 58 while(1)
greenroshks 4:133b49d51b18 59 {
greenroshks 4:133b49d51b18 60 Thread::signal_wait(0x1);
greenroshks 4:133b49d51b18 61 FUNC_I2C_WRITE2CDMS(&data_receive,&data_send);
greenroshks 4:133b49d51b18 62 //printf("\n Data received from CDMS is %c\n",data_receive);
greenroshks 4:133b49d51b18 63 i2c_data * i2c_data_r = i2c_data_receive.alloc();
greenroshks 4:133b49d51b18 64 i2c_data_r->data = data_receive;
greenroshks 4:133b49d51b18 65 i2c_data_r->length = 1;
greenroshks 4:133b49d51b18 66 i2c_data_receive.put(i2c_data_r);
greenroshks 4:133b49d51b18 67 }
greenroshks 2:458f9e603814 68 }
greenroshks 4:133b49d51b18 69
greenroshks 4:133b49d51b18 70 void FUNC_INT()
greenroshks 4:133b49d51b18 71 {
greenroshks 4:133b49d51b18 72 // char * data = new char;
greenroshks 4:133b49d51b18 73 //*data = 'a';
greenroshks 4:133b49d51b18 74 ptr_t_i2c->signal_set(0x1);
greenroshks 4:133b49d51b18 75 //delete data;
greenroshks 4:133b49d51b18 76 }
greenroshks 4:133b49d51b18 77
greenroshks 4:133b49d51b18 78
greenroshks 4:133b49d51b18 79
greenroshks 2:458f9e603814 80 void main()
greenroshks 2:458f9e603814 81 {
greenroshks 2:458f9e603814 82 printf("\nStarted slave... waiting for master\n");
greenroshks 4:133b49d51b18 83 ptr_t_i2c = new Thread(T_I2C_BAE);
greenroshks 4:133b49d51b18 84 char data='a';
greenroshks 4:133b49d51b18 85 interrupt.rise(&FUNC_INT);
greenroshks 2:458f9e603814 86 while(1)
greenroshks 2:458f9e603814 87 {
greenroshks 2:458f9e603814 88 //*(data_send)=pc.getc();
greenroshks 2:458f9e603814 89
greenroshks 4:133b49d51b18 90 //FUNC_I2C_WRITE2CDMS(&data,1);
greenroshks 4:133b49d51b18 91 //printf("\n The data send or received by slave is %c\n",data);
greenroshks 4:133b49d51b18 92 //(data)++;
greenroshks 2:458f9e603814 93 //wait(1);
greenroshks 4:133b49d51b18 94 osEvent evt = i2c_data_receive.get();
greenroshks 4:133b49d51b18 95 if (evt.status == osEventMail)
greenroshks 4:133b49d51b18 96 {
greenroshks 4:133b49d51b18 97 i2c_data *i2c_data_r = (i2c_data*)evt.value.p;
greenroshks 4:133b49d51b18 98 printf("\nData read from CDMS is %c\n",i2c_data_r->data);
greenroshks 4:133b49d51b18 99
greenroshks 4:133b49d51b18 100 i2c_data_receive.free(i2c_data_r);
greenroshks 4:133b49d51b18 101 //delete i2c_data_r;
greenroshks 4:133b49d51b18 102 }
greenroshks 4:133b49d51b18 103
greenroshks 2:458f9e603814 104 }
greenroshks 2:458f9e603814 105 }