sakthi priya amirtharaj
/
BAE_FRDM_INTEGRATION
i2c slave integrated
Fork of BAE_FRDM_INTEGRATION by
slave.cpp
- Committer:
- sakthipriya
- Date:
- 2014-12-15
- Revision:
- 9:a9de938283f9
- Parent:
- 8:667fbc82d634
File content as of revision 9:a9de938283f9:
/*******************Below is an old version**********************************************************************************************/ /* Added fault to hk structure and being sent through I2C. Need to verify the format */ /*#include "slave.h" #include "HK.h" extern struct SensorData Sensor; I2CSlave slave(PTC9,PTC8); //configuring pins p27, p28 as I2Cslave Serial screen (USBTX,USBRX); void write_to_master(char send) //function to write data to master { int acknowledge; int loopvariable4=1; while(loopvariable4) { acknowledge = slave.write(send); //sending the byte to master if(acknowledge==1) { screen.printf(" acknowledge %d sent %u \n",acknowledge,send); loopvariable4=0; } } } void FUNC_I2C_SLAVE_MAIN(int iterations) { screen.printf("\nSlave entered\n"); slave.address(slave_address); //assigning slave address slave.stop(); char Switch_Variable; int loopvariable1=1; int loopvariable2=0; int loopvariable3=1; //initialising dummy sensor data while(loopvariable1) { //to read data from master if(slave.receive()==WriteGeneral) //checking if slave is addressed to write { slave.stop(); Switch_Variable=slave.read(); //receiving data screen.printf("switch variable=%d\n",Switch_Variable); slave.stop(); //reset slave to default receiving state loopvariable1=0; //to interpret and write data to master switch(Switch_Variable) { case 1: printf("\nEntered switch\n"); while(loopvariable3) { if(slave.receive()==ReadAddressed) //check if slave is addressed to read { while(loopvariable2<iterations) { if(loopvariable2%3==0) { screen.printf("\nvoltage%d\n",loopvariable2/3); write_to_master(Sensor.Voltage[loopvariable2/3]); } else if(loopvariable2%3==1) { screen.printf("\ncurrent%d\n",loopvariable2/3); write_to_master(Sensor.Current[loopvariable2/3]); } else if(loopvariable2%3==2) { screen.printf("\ntemp%d\n",loopvariable2/3); write_to_master(Sensor.Temperature[loopvariable2/3]); } loopvariable2++; } screen.printf("\nfault %c\n",Sensor.faultpoll); write_to_master(Sensor.faultpoll); screen.printf("\nfault %c\n",Sensor.faultir); write_to_master(Sensor.faultir); screen.printf("\npower mode %c\n",Sensor.power_mode); write_to_master(Sensor.power_mode); printf("\nExited i2c while loop\n"); slave.stop(); loopvariable3=0; }//if(read addressed) }//while(loopvariable3) break; case 2 : screen.printf(" telecommand 2\n"); break; }//switch case ends } } screen.printf("\nexited slave function\n"); } */ //******************The following is a new version********************************************************************************/ #include "HK.h" #include "slave.h" #include "mbed.h" #include "rtos.h" #include "slave.h" void write_to_master(char); //function to write data to master I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave Serial pcslave(USBTX, USBRX); //int temp; void FUNC_I2C_WRITE2CDMS(char *data,int length) { int slave_status = 1; while(slave_status) { slave.address(0x20); if(slave.receive()==1) { slave_status=slave.write(data,length); } else if(slave.receive()==3 || slave.receive()==2) { slave_status=slave.read(data,length); } } printf("\ndone\n\r"); }