4-10-2015 BAE_RTOS_TEST ACS_DATA_ACQ and I2C working

Dependencies:   mbed-rtos mbed

Fork of BAE_RTOS_TEST1 by GOPA KUMAR K C

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 #include "pin_config.h"
00004 //#include "HK.h"
00005 #include "ACS.h"
00006 //#include "beacon.h"
00007 
00008 Serial pc(USBTX, USBRX);
00009 InterruptIn irpt_4m_mstr(PIN38);                                      //I2c interrupt from CDMS
00010 DigitalOut irpt_2_mstr(PIN4);                                        //I2C interrupt to CDMS
00011 I2CSlave slave (PIN1,PIN2);
00012 const int addr = 0x20;                                            //slave address 
00013 Thread *ptr_t_i2c;
00014 Timer t;                  // time taken from isr to reach i2c function
00015 Timer t1;
00016 Timer t_exec;                                                        //To know the time of execution each thread
00017 
00018 Timer t_start;                                                       //To know the time of entering  of each thread
00019 Timer t_i2c_start;                                                       //To check the time sync in i2c communication
00020 Timer t_i2c_exec;                                                       //To know the time taken by i2c read/write function
00021 
00022 Thread *ptr_t_hk;
00023 Thread *ptr_t_acs;
00024 Thread *ptr_t_bea;
00025 
00026 /**************************************************************global variables*********************************************************************************/
00027 char hk_data[25];
00028 
00029 /**************************************************************funtion header**********************************************************************************/
00030 
00031 void FCTN_HK_DATA_CATENATE();   
00032  
00033 
00034 //---------------------------------------------------------------------------------------------------------------------------------------------------
00035 //TASK : HK
00036 //---------------------------------------------------------------------------------------------------------------------------------------------------
00037 //extern SensorDataQuantised SensorQuantised;
00038 void T_HK(void const *args)
00039 {
00040    while(1){
00041         Thread::signal_wait(0x2);
00042         //SensorQuantised.power_mode='3';                          //default power mode(dummy)
00043 //        printf("\n\rTHIS IS HK    %f\n\r",t_start.read());
00044 //        t_exec.start();
00045 //        FCTN_HK_MAIN();                                             //Collecting HK data
00046 //        FCTN_HK_DATA_CATENATE();                                      //sending HK data to CDMS
00047 //        t_exec.stop();
00048 //        //printf("The time to execute hk_acq is %f seconds\n\r",t_exec.read());
00049 //        t_exec.reset();
00050     printf("\n\rTHIS IS HK    %f\n\r",t_start.read());
00051    }
00052     
00053     }
00054 //---------------------------------------------------------------------------------------------------------------------------------------------------
00055 //TASK : ACS data
00056 //---------------------------------------------------------------------------------------------------------------------------------------------------
00057 
00058 void T_ACS(void const *args){
00059        
00060     float mag_field1[3];
00061     float omega1[3];
00062     //float tauc1[3];
00063     //float moment[3];
00064     //float *mnm_data;
00065     while(1)
00066     {
00067         Thread::signal_wait(0x1);
00068         printf("\n\rEntered ACS   %f\n",t_start.read());
00069         FCTN_ATS_DATA_ACQ(omega1,mag_field1); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
00070         printf("\n\rmnm gyro values\n"); //printing the angular velocity and magnetic field values
00071         for(int i=0; i<3; i++) 
00072         {
00073             printf("%f\t",omega1[i]);
00074         }
00075         printf("\n\r mnm mag values\n");
00076         for(int i=0; i<3; i++) 
00077         {
00078             printf("%f\t",mag_field1[i]);
00079         }
00080         t_exec.reset();
00081 
00082     }
00083 }
00084 //---------------------------------------------------------------------------------------------------------------------------------------------------
00085 //TASK : Beacon
00086 //---------------------------------------------------------------------------------------------------------------------------------------------------
00087 int beac_flag=0;
00088 void T_BEA(void const *args){
00089        while(1){
00090        Thread::signal_wait(0x3);
00091         printf("\n\rTHIS IS BEACON    %f\n\r",t_start.read());
00092 //        t_exec.start();
00093 //        FCTN_BEA_MAIN();
00094 //        if(beac_flag==1)
00095 //        {
00096 //            Thread::wait(600000);
00097 //            beac_flag = 0;
00098 //        }
00099 //        printf("The time to execute beacon thread is %f seconds\n\r",t_exec.read());
00100 //        t_exec.reset();
00101     }
00102 }
00103 
00104 
00105 
00106 //extern SensorDataQuantised SensorQuantised;
00107 
00108 
00109 //---------------------------------------------------------------------------------------------------------------------------------------------------
00110 //TASK : Scheduler
00111 //---------------------------------------------------------------------------------------------------------------------------------------------------
00112 
00113 uint8_t schedcount=1;
00114 void T_SC(void const *args)
00115 {    
00116     
00117    
00118     if(schedcount == 4)                         //to reset the counter
00119     {
00120         schedcount = 1;
00121     }
00122      pc.printf("\n\rThe value of i in scheduler is %d\n\r",schedcount);
00123     if(schedcount%1==0)
00124     {
00125         ptr_t_acs -> signal_set(0x1);
00126     }
00127     
00128     if(schedcount%2==0)
00129     {
00130         ptr_t_hk -> signal_set(0x2);
00131         
00132     }
00133     if(schedcount%3==0)
00134     {
00135     
00136            ptr_t_bea -> signal_set(0x3);
00137         
00138     }
00139     schedcount++;
00140 }
00141 //---------------------------------------------------------------------------------------------------------------------------------------------------
00142 //TASK : i2c data
00143 //---------------------------------------------------------------------------------------------------------------------------------------------------
00144 //void FCTN_I2C_READ(char *data,int length);
00145 //void FCTN_I2C_WRITE(char *data,int length);
00146 
00147 
00148 bool write_ack = 1;
00149 bool read_ack = 1;
00150 char data_send[10];
00151 char data_receive[10];
00152 char short_tc[10];
00153 char long_tc[134];
00154 char mstr_cmd = '0';
00155 bool cmd_flag = 1;
00156 int length=10;
00157 
00158 void T_I2C_SLAVE(void const * args)
00159 {
00160      while(1)
00161     {
00162         cmd_flag = 1;
00163         Thread::signal_wait(0x4);
00164         wait_us(100);                                               // can be between 38 to 15700
00165         //printf("\n\r check 1\n");
00166         t.stop();
00167         if( slave.receive() == 0) 
00168             slave.stop();                  
00169         if( slave.receive() == 1)                                     // slave writes to master
00170         {
00171             t1.start();
00172             write_ack=slave.write(data_send,length);       
00173             t1.stop(); 
00174             if(write_ack == 0)
00175             printf("\n\rData sent to CDMS is %s \n",data_send);     
00176             slave.stop();    
00177         }
00178         if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
00179         {
00180             t1.start();
00181             read_ack=slave.read(data_receive,length);
00182             t1.stop();
00183             if(read_ack == 0)
00184             printf("\n\r Data received from CDMS is %s \n",data_receive);
00185             slave.stop();
00186         }   
00187         printf("\n \r %d %d\n",t.read_us(),t1.read_us());
00188         t.reset();
00189         t1.reset(); 
00190         }
00191         
00192  //         if(cmd_flag == 1)
00193 //        {
00194 //            t.stop();
00195 //            if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
00196 //            {
00197 //                
00198 //                t1.start();
00199 //                read_ack=slave.read(&mstr_cmd,1);
00200 //                t1.stop();
00201 //                if(read_ack == 0)
00202 //                {
00203 //                    printf("\n\r Data received from CDMS is %c \n",mstr_cmd);
00204 //                    switch(mstr_cmd)
00205 //                    {
00206 //                        case 's':
00207 //                        length = 11;
00208 //                        cmd_flag = 0;
00209 //                        break;
00210 //                    
00211 //                        case 'l':
00212 //                        length = 135;
00213 //                        cmd_flag = 0;
00214 //                        break;
00215 //                    
00216 //                        case 'h':
00217 //                        length = 25;
00218 //                        cmd_flag = 0;
00219 //                        FCTN_I2C_WRITE(hk_data,length );
00220 //                        break;
00221 //                    
00222 //                        default:
00223 //                        printf("\n\r invalid command \n");
00224 //                    //cmd_err = 0;
00225 //                        cmd_flag = 1;
00226 //                    }
00227 //                }
00228 //                    else
00229 //                    cmd_flag = 1;   
00230 //            }   
00231 //            else
00232 //                cmd_flag = 1;
00233 //        }
00234 //        printf("\n \r %d %d\n",t.read_us(),t1.read_us());
00235 //        t.reset();
00236 //        t1.reset();
00237 //       
00238 //       
00239 //    }
00240 //}
00241 //
00242 //void FCTN_I2C_READ(char *data, int length )
00243 //{
00244 //    t1.start();
00245 //    read_ack=slave.read(data,length);
00246 //    t1.stop();
00247 //    if(read_ack == 0)
00248 //        printf("\n\rData received from CDMS is %s \n",data);
00249 //    else
00250 //        printf("\n\r data not received \n");
00251 //}
00252 //
00253 //void FCTN_I2C_WRITE(char *data,int length)
00254 //{
00255 //    t1.start();
00256 //    write_ack=slave.write(data,length);
00257 //    t1.stop();       
00258 //    if(write_ack == 0)
00259 //        printf("\n\rData sent to CDMS is %s \n",data);
00260 //    else 
00261 //        printf("\n\r data not sent\n");
00262 }
00263 
00264 
00265 void FCTN_ISR_I2C()
00266 {
00267      ptr_t_i2c->signal_set(0x4); 
00268      t.start();
00269 }
00270 //void FCTN_HK_DATA_CATENATE()
00271 //{
00272 //        strcpy(hk_data,"hk_Data");
00273 //        strcat(hk_data,SensorQuantised.Voltage);
00274 //        strcat(hk_data,SensorQuantised.Current); 
00275 //        strcat(hk_data,SensorQuantised.PanelTemperature);
00276 //        strcat(hk_data,SensorQuantised.AngularSpeed);
00277 //        strcat(hk_data,SensorQuantised.Bnewvalue);
00278 //        char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode};
00279 //        /*strcat(hk_data,sfaultpoll);
00280 //        strcat(hk_data,sfaultir);
00281 //        strcat(hk_data,spower_mode);*/
00282 //        strcat(hk_data,fdata);
00283 //        printf("\n\r hk data being sent is %s ",hk_data);
00284 //}
00285 
00286 //----------------------------------------------------------------------------BAE_INIT-------------------------------------------------------------
00287 
00288 void FCTN_BAE_INIT()
00289 {
00290     slave.address(0x20);                              // setting slave address for BAE for I2C communication
00291     FCTN_ACS_INIT();                                      // Initializing mnm blue
00292     //FCTN_BAE_HK_INIT();
00293     //FCTN_BEA_INIT();
00294 }
00295 
00296 
00297 //---------------------------------------------------------------------------------------------------------------------------------------------------
00298 //TASK : Main
00299 //---------------------------------------------------------------------------------------------------------------------------------------------------
00300 
00301 
00302 
00303 int main(){
00304     ptr_t_hk = new Thread(T_HK);
00305     ptr_t_acs = new Thread(T_ACS);                    
00306     ptr_t_bea = new Thread(T_BEA);
00307     ptr_t_i2c= new Thread(T_I2C_SLAVE);
00308     
00309     ptr_t_acs->set_priority(osPriorityAboveNormal);
00310     ptr_t_hk->set_priority(osPriorityAboveNormal);
00311     ptr_t_bea->set_priority(osPriorityAboveNormal);
00312     ptr_t_i2c->set_priority(osPriorityRealtime);
00313 
00314     RtosTimer t_sc_timer(T_SC,osTimerPeriodic);               // Initiating the scheduler thread
00315     t_sc_timer.start(10000);
00316     printf("\n\r BAE ACTIVATED\n");
00317     FCTN_BAE_INIT();
00318     //strcpy(data_send,"sakthi");
00319     slave.address(addr); 
00320     irpt_4m_mstr.enable_irq();
00321     irpt_4m_mstr.rise(&FCTN_ISR_I2C);
00322         
00323    
00324     while(1)                                                   //required to prevent main from terminating
00325     {   
00326         ;
00327     }
00328 }