GOPA KUMAR K C / Mbed 2 deprecated BAE_RTOS_test_2

Dependencies:   mbed-rtos mbed

Fork of BAE_RTOS_test_1 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[25];
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 %c \n",data_receive);
00185             if(strcmp(data_receive,"h"))
00186             {   strcpy(data_send, "ababababababababababababg");
00187                 length=25;
00188                 write_ack=slave.write(data_send,length);
00189                 if(write_ack == 0)
00190                 printf("\n\rData sent to CDMS is %s \n",data_send);     
00191                 slave.stop();    
00192                 }
00193             slave.stop();
00194         }   
00195         printf("\n \r %d %d\n",t.read_us(),t1.read_us());
00196         t.reset();
00197         t1.reset(); 
00198         }
00199         
00200  //         if(cmd_flag == 1)
00201 //        {
00202 //            t.stop();
00203 //            if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
00204 //            {
00205 //                
00206 //                t1.start();
00207 //                read_ack=slave.read(&mstr_cmd,1);
00208 //                t1.stop();
00209 //                if(read_ack == 0)
00210 //                {
00211 //                    printf("\n\r Data received from CDMS is %c \n",mstr_cmd);
00212 //                    switch(mstr_cmd)
00213 //                    {
00214 //                        case 's':
00215 //                        length = 11;
00216 //                        cmd_flag = 0;
00217 //                        break;
00218 //                    
00219 //                        case 'l':
00220 //                        length = 135;
00221 //                        cmd_flag = 0;
00222 //                        break;
00223 //                    
00224 //                        case 'h':
00225 //                        length = 25;
00226 //                        cmd_flag = 0;
00227 //                        FCTN_I2C_WRITE(hk_data,length );
00228 //                        break;
00229 //                    
00230 //                        default:
00231 //                        printf("\n\r invalid command \n");
00232 //                    //cmd_err = 0;
00233 //                        cmd_flag = 1;
00234 //                    }
00235 //                }
00236 //                    else
00237 //                    cmd_flag = 1;   
00238 //            }   
00239 //            else
00240 //                cmd_flag = 1;
00241 //        }
00242 //        printf("\n \r %d %d\n",t.read_us(),t1.read_us());
00243 //        t.reset();
00244 //        t1.reset();
00245 //       
00246 //       
00247 //    }
00248 //}
00249 //
00250 //void FCTN_I2C_READ(char *data, int length )
00251 //{
00252 //    t1.start();
00253 //    read_ack=slave.read(data,length);
00254 //    t1.stop();
00255 //    if(read_ack == 0)
00256 //        printf("\n\rData received from CDMS is %s \n",data);
00257 //    else
00258 //        printf("\n\r data not received \n");
00259 //}
00260 //
00261 //void FCTN_I2C_WRITE(char *data,int length)
00262 //{
00263 //    t1.start();
00264 //    write_ack=slave.write(data,length);
00265 //    t1.stop();       
00266 //    if(write_ack == 0)
00267 //        printf("\n\rData sent to CDMS is %s \n",data);
00268 //    else 
00269 //        printf("\n\r data not sent\n");
00270 }
00271 
00272 
00273 void FCTN_ISR_I2C()
00274 {
00275      ptr_t_i2c->signal_set(0x4); 
00276      t.start();
00277 }
00278 //void FCTN_HK_DATA_CATENATE()
00279 //{
00280 //        strcpy(hk_data,"hk_Data");
00281 //        strcat(hk_data,SensorQuantised.Voltage);
00282 //        strcat(hk_data,SensorQuantised.Current); 
00283 //        strcat(hk_data,SensorQuantised.PanelTemperature);
00284 //        strcat(hk_data,SensorQuantised.AngularSpeed);
00285 //        strcat(hk_data,SensorQuantised.Bnewvalue);
00286 //        char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode};
00287 //        /*strcat(hk_data,sfaultpoll);
00288 //        strcat(hk_data,sfaultir);
00289 //        strcat(hk_data,spower_mode);*/
00290 //        strcat(hk_data,fdata);
00291 //        printf("\n\r hk data being sent is %s ",hk_data);
00292 //}
00293 
00294 //----------------------------------------------------------------------------BAE_INIT-------------------------------------------------------------
00295 
00296 void FCTN_BAE_INIT()
00297 {
00298     slave.address(0x20);                              // setting slave address for BAE for I2C communication
00299     FCTN_ACS_INIT();                                      // Initializing mnm blue
00300     //FCTN_BAE_HK_INIT();
00301     //FCTN_BEA_INIT();
00302 }
00303 
00304 
00305 //---------------------------------------------------------------------------------------------------------------------------------------------------
00306 //TASK : Main
00307 //---------------------------------------------------------------------------------------------------------------------------------------------------
00308 
00309 
00310 
00311 int main(){
00312     ptr_t_hk = new Thread(T_HK);
00313     ptr_t_acs = new Thread(T_ACS);                    
00314     ptr_t_bea = new Thread(T_BEA);
00315     ptr_t_i2c= new Thread(T_I2C_SLAVE);
00316     
00317     ptr_t_acs->set_priority(osPriorityAboveNormal);
00318     ptr_t_hk->set_priority(osPriorityAboveNormal);
00319     ptr_t_bea->set_priority(osPriorityAboveNormal);
00320     ptr_t_i2c->set_priority(osPriorityRealtime);
00321 
00322     RtosTimer t_sc_timer(T_SC,osTimerPeriodic);               // Initiating the scheduler thread
00323     t_sc_timer.start(10000);
00324     printf("\n\r BAE ACTIVATED\n");
00325     FCTN_BAE_INIT();
00326     //strcpy(data_send,"sakthi");
00327     slave.address(addr); 
00328     irpt_4m_mstr.enable_irq();
00329     irpt_4m_mstr.rise(&FCTN_ISR_I2C);
00330         
00331    
00332     while(1)                                                   //required to prevent main from terminating
00333     {   
00334         ;
00335     }
00336 }