sakthi priya amirtharaj
/
slave_working1_1
i2c slave working version 1.1
Fork of slave_i2c3 by
Diff: main.cpp
- Revision:
- 4:133b49d51b18
- Parent:
- 3:5e68dd4bbb70
- Child:
- 5:ce0bb4174ca4
diff -r 5e68dd4bbb70 -r 133b49d51b18 main.cpp --- a/main.cpp Wed Dec 10 05:21:50 2014 +0000 +++ b/main.cpp Wed Dec 10 10:33:56 2014 +0000 @@ -1,23 +1,34 @@ #include "mbed.h" +#include "rtos.h" void write_to_master(char); //function to write data to master - +Thread * ptr_t_i2c; I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave Serial pc (USBTX,USBRX); InterruptIn interrupt(D9); -void FUNC_I2C_WRITE2CDMS(char * data,int length) + +typedef struct { + char data; // To avoid dynamic memory allocation + int length; +}i2c_data; +Mail<i2c_data,16> i2c_data_receive; +Mail<i2c_data,16> i2c_data_send; + +void FUNC_I2C_WRITE2CDMS(char *data_receive,char *data_send, int length=1) +{ + int slave_status = 1; while(slave_status) { slave.address(0x20); if(slave.receive()==1) { - slave_status=slave.write(data,length); + slave_status=slave.write(data_send,length); // printf("\n In the while loop\n"); - /* if(!slave_status) + /*if(!slave_status) { printf("slave wrote %c to master\n\r",*data); @@ -27,7 +38,8 @@ else if(slave.receive()==3 || slave.receive()==2) { - slave_status=slave.read(data,length); + //i2c_data *i2c_data_r = i2c_data_receive.alloc(); + slave_status=slave.read(data_receive,length); /*if(!slave_status) { printf("1 read %c from master\n\r",*data); @@ -36,47 +48,58 @@ //slave.stop(); } printf("2 done"); + } -/*void FUNC_I2C_READFCDMS() - +void T_I2C_BAE(void const * args) { - printf("\nEntered slave code\n"); - //read_loop=true; - char *data_rec = new char; - int slave_status =1; - - slave.address(0x20); - - - - printf("\nentered slave while loop\n"); - //if(slave.receive()==3) - while(slave_status==1) - slave_status=slave.read(data_rec,1); - - if(slave_status==0){printf("1 read %c from master\n\r",*data_rec);} - // read_loop = false;} - - printf("\n done \n \r"); - - delete data_rec; - + char data_send,data_receive; + data_send = 'a'; + while(1) + { + Thread::signal_wait(0x1); + FUNC_I2C_WRITE2CDMS(&data_receive,&data_send); + //printf("\n Data received from CDMS is %c\n",data_receive); + i2c_data * i2c_data_r = i2c_data_receive.alloc(); + i2c_data_r->data = data_receive; + i2c_data_r->length = 1; + i2c_data_receive.put(i2c_data_r); + } } -*/ + +void FUNC_INT() +{ + // char * data = new char; + //*data = 'a'; + ptr_t_i2c->signal_set(0x1); + //delete data; +} + + + void main() { printf("\nStarted slave... waiting for master\n"); - char * data = new char; - *data = 'a'; - + ptr_t_i2c = new Thread(T_I2C_BAE); + char data='a'; + interrupt.rise(&FUNC_INT); while(1) { //*(data_send)=pc.getc(); - FUNC_I2C_WRITE2CDMS(data,1); - printf("\n The data send or received by slave is %c\n",*data); - (*data)++; + //FUNC_I2C_WRITE2CDMS(&data,1); + //printf("\n The data send or received by slave is %c\n",data); + //(data)++; //wait(1); + osEvent evt = i2c_data_receive.get(); + if (evt.status == osEventMail) + { + i2c_data *i2c_data_r = (i2c_data*)evt.value.p; + printf("\nData read from CDMS is %c\n",i2c_data_r->data); + + i2c_data_receive.free(i2c_data_r); + //delete i2c_data_r; + } + } } \ No newline at end of file