sakthi priya amirtharaj / Mbed 2 deprecated CDMS_work_always

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 const int addr = 0x20;                     //slave address 
00005 I2C master (PIN72,PIN71);                      //configure pins p27,p28 as I2C master
00006 Serial pc (USBTX,USBRX);
00007 DigitalOut interrupt(PIN4);
00008 InterruptIn data_ready(PIN39);
00009 
00010 int reset;
00011 Timer t;
00012 Timer t1;
00013 Timer t2;
00014 Timer t3;
00015 
00016 typedef struct
00017 {
00018     char data;          // To avoid dynamic memory allocation
00019     int length;
00020 }i2c_data;
00021 
00022 //Mail<i2c_data,16> i2c_data_receive;
00023 Mail<i2c_data,16> i2c_data_send;
00024 
00025 Thread * ptr_t_i2c;
00026 void FUNC_I2C_MASTER_FSLAVE(char * data,int length)
00027 {
00028     
00029             
00030             bool ack0 =true; 
00031             
00032             interrupt = 1;
00033             t1.start();
00034             //wait_ms(20);
00035             ack0 =  master.read(addr|1,data,length);
00036             t1.stop();
00037             
00038             
00039             if(!ack0)
00040             {
00041                     printf("\n master has read %s from slave\n\r",data);
00042                     
00043             }
00044                 //master.stop();
00045             printf("\n%d\n\r",t1.read_us());
00046             t1.reset();
00047             
00048             interrupt=0;
00049            
00050 }
00051 
00052 typedef struct {
00053     char Voltage[9];
00054     char Current[5];
00055     char Temperature[2];
00056     char PanelTemperature[3];//read by the 4 thermistors on solar panels
00057     char BatteryTemperature;    //to be populated   
00058     char faultpoll;             //polled faults
00059     char faultir;               //interrupted faults
00060     char power_mode;            //power modes
00061     char AngularSpeed[3];
00062     char Bnewvalue[3];
00063     
00064     //float magnetometer,gyro=>to be addes
00065 } hk_data;
00066 hk_data decode_data;
00067 
00068 /*void TC_DECODE(char *data_hk)                  //getting the structure back from hk data sent by bae
00069 {
00070     for(int i=0;i<=7;i++)
00071     {
00072     decode_data.Voltage[i] = data_hk[i];
00073     decode_data.Voltage[8] = '\0';
00074     }
00075     for(int i=0;i<=3;i++)
00076     {
00077     decode_data.Current[i] = data_hk[8+i];
00078     decode_data.Current[4] = '\0';
00079     }
00080     decode_data.Temperature[0] = data_hk[12];
00081     decode_data.Temperature[1] = '\0'; 
00082     for(int i=0;i<=1;i++)
00083     {
00084     decode_data.PanelTemperature[i] = data_hk[13+i];
00085     decode_data.PanelTemperature[2] = '\0';
00086     }
00087     decode_data.BatteryTemperature = data_hk[15];
00088     decode_data.faultpoll = data_hk[16];
00089     decode_data.faultir = data_hk[17];
00090     decode_data.power_mode = data_hk[18];
00091     for(int i=0;i<=1;i++)
00092     {
00093     decode_data.AngularSpeed[i] = data_hk[19+i];
00094     decode_data.AngularSpeed[2] = '\0';
00095     }
00096     for(int i=0;i<=1;i++)
00097     {
00098     decode_data.Bnewvalue[i] = data_hk[21+i];
00099     decode_data.Bnewvalue[2] = '\0';
00100     }
00101     printf("\n voltage %s\n\r",decode_data.Voltage);
00102     printf("\n current  %s\n\r",decode_data.Current);
00103     printf("\n faultpoll %c\n\r",decode_data.faultpoll);
00104 }*/
00105 
00106             
00107 void T_I2C_MASTER_FSLAVE(void const *args)
00108 {
00109     char data_receive[25];
00110     while(1)
00111     {
00112         Thread::signal_wait(0x1);
00113         
00114         
00115         FUNC_I2C_MASTER_FSLAVE(data_receive,25);
00116         /*i2c_data * i2c_data_r = i2c_data_receive.alloc();
00117         strcpy(i2c_data_r->data , data_receive);
00118         i2c_data_r->length = 25;
00119         i2c_data_receive.put(i2c_data_r);*/
00120         printf("\n Data received from slave is %s\n\r",data_receive);
00121         //TC_DECODE(data_receive);
00122     }
00123 }
00124 
00125 void FUNC_INT()
00126 {
00127   
00128    ptr_t_i2c->signal_set(0x1);
00129      
00130 }
00131 
00132 char writedata;
00133 bool write2slave;
00134 bool master_status_write;
00135 void FUNC_MASTER_WRITE()
00136 {   //wait(1);
00137     write2slave=true;
00138    
00139     
00140      
00141     char data = pc.getc();
00142     interrupt = 1;
00143     t.start();
00144     t3.start();
00145     wait_ms(20);
00146     i2c_data * i2c_data_s = i2c_data_send.alloc();
00147     i2c_data_s->data = data;
00148     i2c_data_s->length = 1;
00149     i2c_data_send.put(i2c_data_s); 
00150     master_status_write = true;
00151     
00152    // interrupt = 1; 
00153 
00154     osEvent evt = i2c_data_send.get();
00155             if (evt.status == osEventMail) 
00156             {
00157                 i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
00158                 writedata = i2c_data_s -> data;
00159                 t.stop();
00160                 //t3.start();         
00161                 master_status_write = (bool) master.write(addr|0x00,&writedata,1);
00162                 t3.stop();
00163                 if(master_status_write==0)
00164                     {
00165                     printf("master has written %c to slave\n\r",writedata);
00166                     write2slave=false;
00167                     }
00168                 i2c_data_send.free(i2c_data_s);
00169                 printf("\n%d\n",t.read_us());
00170                 t.reset();
00171                 printf("\n%d\n",t3.read_us());
00172                 t3.reset();
00173             }
00174     interrupt = 0;
00175 }
00176 
00177     
00178 
00179 int main()
00180 {   
00181     interrupt=0;
00182     ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE);
00183     data_ready.rise(&FUNC_INT);
00184     printf("\nstarted master\n"); 
00185      master.frequency(100000);      
00186     while(1)
00187     { //Thread::wait(9000);
00188     //interrupt = 1;
00189       
00190       FUNC_MASTER_WRITE(); 
00191       //interrupt = 0; 
00192       ;
00193     }
00194 }