i2c slave integrated

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDM_INTEGRATION by green rosh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers slave.cpp Source File

slave.cpp

00001 /*******************Below is an old version**********************************************************************************************/
00002 /* Added fault to hk structure and being sent through I2C. Need to verify the format */
00003 /*#include "slave.h"
00004 #include "HK.h"
00005  
00006 extern struct SensorData Sensor;
00007 I2CSlave slave(PTC9,PTC8);                       //configuring pins p27, p28 as I2Cslave
00008 Serial screen (USBTX,USBRX);
00009 void write_to_master(char send)                           //function to write data to master
00010         {
00011            int acknowledge; 
00012            int loopvariable4=1;
00013            while(loopvariable4)
00014            {
00015            acknowledge = slave.write(send);    //sending the byte to master
00016            if(acknowledge==1)
00017             {
00018               screen.printf(" acknowledge %d sent %u \n",acknowledge,send);
00019               loopvariable4=0;
00020             }
00021            } 
00022         }
00023         
00024  
00025  
00026  
00027 void FUNC_I2C_SLAVE_MAIN(int iterations)
00028 {
00029     
00030     screen.printf("\nSlave entered\n");
00031     slave.address(slave_address);                           //assigning slave address
00032     slave.stop();
00033     char Switch_Variable;
00034     
00035     
00036     int loopvariable1=1;
00037     int loopvariable2=0;
00038     int loopvariable3=1;
00039 //initialising dummy sensor data                         
00040     
00041     while(loopvariable1)
00042     { 
00043 //to read data from master       
00044     if(slave.receive()==WriteGeneral)                   //checking if slave is addressed to write
00045     {
00046       slave.stop();
00047       Switch_Variable=slave.read();                   //receiving data
00048       screen.printf("switch variable=%d\n",Switch_Variable);
00049       slave.stop();                          //reset slave to default receiving state
00050       loopvariable1=0;
00051 //to interpret and write data to master       
00052       switch(Switch_Variable)
00053       {
00054 
00055        
00056        case 1:   printf("\nEntered switch\n"); 
00057                    while(loopvariable3)
00058                   {
00059                      if(slave.receive()==ReadAddressed)             //check if slave is addressed to read 
00060                    {
00061                       while(loopvariable2<iterations)
00062                      {
00063                       if(loopvariable2%3==0)
00064                           {
00065                           screen.printf("\nvoltage%d\n",loopvariable2/3);
00066                           write_to_master(Sensor.Voltage[loopvariable2/3]);
00067                           }
00068                       else if(loopvariable2%3==1)
00069                           {   
00070                           screen.printf("\ncurrent%d\n",loopvariable2/3);
00071                           write_to_master(Sensor.Current[loopvariable2/3]);
00072                           }
00073                       else if(loopvariable2%3==2)
00074                           {   
00075                           screen.printf("\ntemp%d\n",loopvariable2/3);
00076                           write_to_master(Sensor.Temperature[loopvariable2/3]);
00077                           }         
00078                       loopvariable2++;
00079                        }
00080                        screen.printf("\nfault %c\n",Sensor.faultpoll);
00081                        write_to_master(Sensor.faultpoll);
00082                        screen.printf("\nfault %c\n",Sensor.faultir);
00083                        write_to_master(Sensor.faultir);
00084                        screen.printf("\npower mode %c\n",Sensor.power_mode);
00085                        write_to_master(Sensor.power_mode);
00086                        printf("\nExited i2c while loop\n");
00087                       slave.stop();    
00088                       loopvariable3=0;  
00089                     }//if(read addressed)
00090                     
00091                     }//while(loopvariable3)
00092                   
00093                   break;
00094          case 2 : screen.printf(" telecommand 2\n");
00095                   break;         
00096                  
00097         }//switch case ends
00098    }   
00099 }
00100    screen.printf("\nexited slave function\n");
00101 }
00102 */
00103 
00104 //******************The following is a new version********************************************************************************/
00105 #include "HK.h"
00106 #include "slave.h"
00107 
00108 #include "mbed.h"
00109 #include "rtos.h"
00110 #include "slave.h"
00111 
00112 void write_to_master(char);                    //function to write data to master
00113 
00114 I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
00115 
00116 Serial pcslave(USBTX, USBRX);
00117 
00118 
00119 
00120 //int temp;
00121  
00122 
00123 void FUNC_I2C_WRITE2CDMS(char *data,int length)
00124 {
00125        int slave_status = 1;
00126        
00127    
00128     while(slave_status)
00129     {                 
00130             slave.address(0x20);
00131             if(slave.receive()==1)
00132             {
00133                 slave_status=slave.write(data,length);
00134                
00135                 
00136             }
00137             else if(slave.receive()==3 || slave.receive()==2)
00138             {
00139                 slave_status=slave.read(data,length);
00140             }
00141             
00142     }
00143             printf("\ndone\n\r");
00144 
00145 }
00146 
00147 
00148 
00149 
00150 
00151       
00152    
00153    
00154 
00155 
00156 
00157 
00158