I2C BAE standalone hardware testing

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE_1 by Team Fox

Committer:
sakthipriya
Date:
Thu Dec 24 19:15:43 2015 +0000
Revision:
0:7b4c00e3912f
Child:
1:446a959e36ce
EPS CONOPS BTRY GAUGE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sakthipriya 0:7b4c00e3912f 1 #include "mbed.h"
sakthipriya 0:7b4c00e3912f 2 #include "rtos.h"
sakthipriya 0:7b4c00e3912f 3 #include "pin_config.h"
sakthipriya 0:7b4c00e3912f 4 #include "ACS.h"
sakthipriya 0:7b4c00e3912f 5 #include "EPS.h"
sakthipriya 0:7b4c00e3912f 6 #include "BCN.h"
sakthipriya 0:7b4c00e3912f 7 #include "TCTM.h"
sakthipriya 0:7b4c00e3912f 8
sakthipriya 0:7b4c00e3912f 9 #define tm_len 134
sakthipriya 0:7b4c00e3912f 10 #define tc_len 135
sakthipriya 0:7b4c00e3912f 11 #define bae_data_len 150
sakthipriya 0:7b4c00e3912f 12 #define batt_heat_low 20
sakthipriya 0:7b4c00e3912f 13 //***************************************************** flags *************************************************************//
sakthipriya 0:7b4c00e3912f 14 uint32_t BAE_STATUS = 0x00000000;
sakthipriya 0:7b4c00e3912f 15 uint32_t BAE_ENABLE = 0xFFFFFFFF;
sakthipriya 0:7b4c00e3912f 16
sakthipriya 0:7b4c00e3912f 17 //.........acs...............//
sakthipriya 0:7b4c00e3912f 18 char ACS_INIT_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 19 char ACS_DATA_ACQ_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 20 char ACS_ATS_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 21 char ACS_MAIN_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 22 char ACS_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 23
sakthipriya 0:7b4c00e3912f 24 char ACS_ATS_ENABLE = 'q';
sakthipriya 0:7b4c00e3912f 25 char ACS_DATA_ACQ_ENABLE = 'q';
sakthipriya 0:7b4c00e3912f 26 char ACS_STATE = 'q';
sakthipriya 0:7b4c00e3912f 27
sakthipriya 0:7b4c00e3912f 28 //.....................eps...................//
sakthipriya 0:7b4c00e3912f 29 //eps init
sakthipriya 0:7b4c00e3912f 30 char EPS_INIT_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 31 char EPS_BATTERY_GAUGE_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 32 //eps main
sakthipriya 0:7b4c00e3912f 33 char EPS_MAIN_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 34 char EPS_BATTERY_TEMP_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 35 char EPS_STATUS = 'q';
sakthipriya 0:7b4c00e3912f 36
sakthipriya 0:7b4c00e3912f 37 char EPS_BATTERY_HEAT_ENABLE = 'q';
sakthipriya 0:7b4c00e3912f 38
sakthipriya 0:7b4c00e3912f 39 //.......................global variables..................................................................// new hk structure- everything has to changed based on this
sakthipriya 0:7b4c00e3912f 40 char BAE_data[bae_data_len];
sakthipriya 0:7b4c00e3912f 41
sakthipriya 0:7b4c00e3912f 42
sakthipriya 0:7b4c00e3912f 43 //*************************************Global declarations************************************************//
sakthipriya 0:7b4c00e3912f 44 const int addr = 0x20; //slave address
sakthipriya 0:7b4c00e3912f 45
sakthipriya 0:7b4c00e3912f 46 Timer t_rfsilence;
sakthipriya 0:7b4c00e3912f 47 Timer t_start;
sakthipriya 0:7b4c00e3912f 48 Serial pc(USBTX, USBRX);
sakthipriya 0:7b4c00e3912f 49 int power_flag_dummy=2;
sakthipriya 0:7b4c00e3912f 50 float data[6];
sakthipriya 0:7b4c00e3912f 51
sakthipriya 0:7b4c00e3912f 52 extern float moment[3];
sakthipriya 0:7b4c00e3912f 53 extern SensorData Sensor;
sakthipriya 0:7b4c00e3912f 54 extern uint8_t BCN_FEN;
sakthipriya 0:7b4c00e3912f 55
sakthipriya 0:7b4c00e3912f 56 bool write_ack = 1;
sakthipriya 0:7b4c00e3912f 57 bool read_ack = 1;
sakthipriya 0:7b4c00e3912f 58 char telecommand[tc_len];
sakthipriya 0:7b4c00e3912f 59 char telemetry[tm_len];
sakthipriya 0:7b4c00e3912f 60 char data_send_flag = 'h';
sakthipriya 0:7b4c00e3912f 61
sakthipriya 0:7b4c00e3912f 62 //*****************************************************Assigning pins******************************************************//
sakthipriya 0:7b4c00e3912f 63 DigitalOut gpo1(PTC0); // enable of att sens2 switch
sakthipriya 0:7b4c00e3912f 64 DigitalOut gpo2(PTC16); // enable of att sens switch
sakthipriya 0:7b4c00e3912f 65 InterruptIn irpt_4m_mstr(PIN38); //I2c interrupt from CDMS
sakthipriya 0:7b4c00e3912f 66 DigitalOut irpt_2_mstr(PIN4); //I2C interrupt to CDMS
sakthipriya 0:7b4c00e3912f 67 I2CSlave slave (PIN1,PIN2);
sakthipriya 0:7b4c00e3912f 68 DigitalOut batt_heat(PIN96);
sakthipriya 0:7b4c00e3912f 69
sakthipriya 0:7b4c00e3912f 70 //gpo1 = 0;
sakthipriya 0:7b4c00e3912f 71 PwmOut PWM1(PIN93); //x //Functions used to generate PWM signal
sakthipriya 0:7b4c00e3912f 72 PwmOut PWM2(PIN94); //y
sakthipriya 0:7b4c00e3912f 73 PwmOut PWM3(PIN95); //z //PWM output comes from pins p6
sakthipriya 0:7b4c00e3912f 74
sakthipriya 0:7b4c00e3912f 75
sakthipriya 0:7b4c00e3912f 76 /*****************************************************************Threads USed***********************************************************************************/
sakthipriya 0:7b4c00e3912f 77 Thread *ptr_t_acs;
sakthipriya 0:7b4c00e3912f 78 Thread *ptr_t_eps;
sakthipriya 0:7b4c00e3912f 79 Thread *ptr_t_bcn;
sakthipriya 0:7b4c00e3912f 80 Thread *ptr_t_i2c;
sakthipriya 0:7b4c00e3912f 81
sakthipriya 0:7b4c00e3912f 82 /*********************************************************FCTN HEADERS***********************************************************************************/
sakthipriya 0:7b4c00e3912f 83
sakthipriya 0:7b4c00e3912f 84 void FCTN_ISR_I2C();
sakthipriya 0:7b4c00e3912f 85 void FCTN_TM();
sakthipriya 0:7b4c00e3912f 86
sakthipriya 0:7b4c00e3912f 87 //*******************************************ACS THREAD**************************************************//
sakthipriya 0:7b4c00e3912f 88
sakthipriya 0:7b4c00e3912f 89 void T_ACS(void const *args)
sakthipriya 0:7b4c00e3912f 90 {
sakthipriya 0:7b4c00e3912f 91 float b1[3]={-23.376,-37.56,14.739}, omega1[3]={-1.52,2.746,0.7629}, moment1[3]= {1.0498,-1.0535,1.3246};
sakthipriya 0:7b4c00e3912f 92 //b1[3] = {22, 22,10};
sakthipriya 0:7b4c00e3912f 93 //omega1[3] = {2.1,3.0,1.5};
sakthipriya 0:7b4c00e3912f 94 // gpo1 = 0; // att sens2 switch is disabled
sakthipriya 0:7b4c00e3912f 95 // gpo2 = 0; // att sens switch is disabled
sakthipriya 0:7b4c00e3912f 96
sakthipriya 0:7b4c00e3912f 97 while(1)
sakthipriya 0:7b4c00e3912f 98 {
sakthipriya 0:7b4c00e3912f 99
sakthipriya 0:7b4c00e3912f 100 Thread::signal_wait(0x1);
sakthipriya 0:7b4c00e3912f 101 ACS_MAIN_STATUS = 's'; //set ACS_MAIN_STATUS flag
sakthipriya 0:7b4c00e3912f 102 PWM1 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 103 PWM2 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 104 PWM3 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 105 pc.printf("\n\rEntered ACS %f\n",t_start.read());
sakthipriya 0:7b4c00e3912f 106
sakthipriya 0:7b4c00e3912f 107 if(ACS_DATA_ACQ_ENABLE == 'e')// check if ACS_DATA_ACQ_ENABLE = 1?
sakthipriya 0:7b4c00e3912f 108 {
sakthipriya 0:7b4c00e3912f 109 FLAG();
sakthipriya 0:7b4c00e3912f 110 FCTN_ATS_DATA_ACQ(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
sakthipriya 0:7b4c00e3912f 111 pc.printf("gyro values\n\r"); //printing the angular velocity and magnetic field values
sakthipriya 0:7b4c00e3912f 112 for(int i=0; i<3; i++)
sakthipriya 0:7b4c00e3912f 113 {
sakthipriya 0:7b4c00e3912f 114 pc.printf("%f\n\r",data[i]);
sakthipriya 0:7b4c00e3912f 115 }
sakthipriya 0:7b4c00e3912f 116 pc.printf("mag values\n\r");
sakthipriya 0:7b4c00e3912f 117 for(int i=3; i<6; i++)
sakthipriya 0:7b4c00e3912f 118 {
sakthipriya 0:7b4c00e3912f 119 pc.printf("%f\n\r",data[i]);
sakthipriya 0:7b4c00e3912f 120
sakthipriya 0:7b4c00e3912f 121 for(int i=0;i<3;i++)
sakthipriya 0:7b4c00e3912f 122 {
sakthipriya 0:7b4c00e3912f 123 omega1[i]= data[i];
sakthipriya 0:7b4c00e3912f 124 b1[i] = data[i+3];
sakthipriya 0:7b4c00e3912f 125 }
sakthipriya 0:7b4c00e3912f 126 }
sakthipriya 0:7b4c00e3912f 127 }//if ACS_DATA_ACQ_ENABLE = 1
sakthipriya 0:7b4c00e3912f 128 else
sakthipriya 0:7b4c00e3912f 129 {
sakthipriya 0:7b4c00e3912f 130 // Z axis actuation is the only final solution,
sakthipriya 0:7b4c00e3912f 131 }
sakthipriya 0:7b4c00e3912f 132 if(ACS_STATE == '0') // check ACS_STATE = ACS_CONTROL_OFF?
sakthipriya 0:7b4c00e3912f 133 {
sakthipriya 0:7b4c00e3912f 134 printf("\n\r acs control off\n");
sakthipriya 0:7b4c00e3912f 135 FLAG();
sakthipriya 0:7b4c00e3912f 136 ACS_STATUS = '0'; // set ACS_STATUS = ACS_CONTROL_OFF
sakthipriya 0:7b4c00e3912f 137 PWM1 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 138 PWM2 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 139 PWM3 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 140 }
sakthipriya 0:7b4c00e3912f 141 else
sakthipriya 0:7b4c00e3912f 142 {
sakthipriya 0:7b4c00e3912f 143 if(Sensor.power_mode>1)
sakthipriya 0:7b4c00e3912f 144
sakthipriya 0:7b4c00e3912f 145 {
sakthipriya 0:7b4c00e3912f 146 if(ACS_STATE == '2') // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY
sakthipriya 0:7b4c00e3912f 147 {
sakthipriya 0:7b4c00e3912f 148 FLAG();
sakthipriya 0:7b4c00e3912f 149 printf("\n\r z axis moment only\n");
sakthipriya 0:7b4c00e3912f 150 ACS_STATUS = '2'; // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY
sakthipriya 0:7b4c00e3912f 151 // FCTN_ACS_CNTRLALGO(b1, omega1);
sakthipriya 0:7b4c00e3912f 152 moment[0] = 0;
sakthipriya 0:7b4c00e3912f 153 moment[1] = 0;
sakthipriya 0:7b4c00e3912f 154 moment[2] =1.3;// is a dummy value
sakthipriya 0:7b4c00e3912f 155 FCTN_ACS_GENPWM_MAIN(moment) ;
sakthipriya 0:7b4c00e3912f 156 }
sakthipriya 0:7b4c00e3912f 157 else
sakthipriya 0:7b4c00e3912f 158 {
sakthipriya 0:7b4c00e3912f 159 if(ACS_STATE == '3') // check ACS_STATE = ACS_DATA_ACQ_FAILURE
sakthipriya 0:7b4c00e3912f 160 {
sakthipriya 0:7b4c00e3912f 161 FLAG();
sakthipriya 0:7b4c00e3912f 162 printf("\n\r acs data failure ");
sakthipriya 0:7b4c00e3912f 163 ACS_STATUS = '3'; // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
sakthipriya 0:7b4c00e3912f 164 PWM1 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 165 PWM2 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 166 PWM3 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 167 }
sakthipriya 0:7b4c00e3912f 168 else
sakthipriya 0:7b4c00e3912f 169 {
sakthipriya 0:7b4c00e3912f 170 if(ACS_STATE == '4') // check ACS_STATE = ACS_NOMINAL_ONLY
sakthipriya 0:7b4c00e3912f 171 {
sakthipriya 0:7b4c00e3912f 172 FLAG();
sakthipriya 0:7b4c00e3912f 173 printf("\n\r nominal");
sakthipriya 0:7b4c00e3912f 174 ACS_STATUS = '4'; // set ACS_STATUS = ACS_NOMINAL_ONLY
sakthipriya 0:7b4c00e3912f 175 FCTN_ACS_CNTRLALGO(b1,omega1);
sakthipriya 0:7b4c00e3912f 176 printf("\n\r moment values returned by control algo \n");
sakthipriya 0:7b4c00e3912f 177 for(int i=0; i<3; i++)
sakthipriya 0:7b4c00e3912f 178 {
sakthipriya 0:7b4c00e3912f 179 printf("%f\t",moment[i]);
sakthipriya 0:7b4c00e3912f 180 }
sakthipriya 0:7b4c00e3912f 181 FCTN_ACS_GENPWM_MAIN(moment) ;
sakthipriya 0:7b4c00e3912f 182 }
sakthipriya 0:7b4c00e3912f 183 else
sakthipriya 0:7b4c00e3912f 184 {
sakthipriya 0:7b4c00e3912f 185 if(ACS_STATE == '5') // check ACS_STATE = ACS_AUTO_CONTROL
sakthipriya 0:7b4c00e3912f 186 {
sakthipriya 0:7b4c00e3912f 187 FLAG();
sakthipriya 0:7b4c00e3912f 188 printf("\n\r auto control");
sakthipriya 0:7b4c00e3912f 189 ACS_STATUS = '5'; // set ACS_STATUS = ACS_AUTO_CONTROL
sakthipriya 0:7b4c00e3912f 190 //FCTN_ACS_AUTOCTRL_LOGIC // gotta include this code
sakthipriya 0:7b4c00e3912f 191 }
sakthipriya 0:7b4c00e3912f 192 else
sakthipriya 0:7b4c00e3912f 193 {
sakthipriya 0:7b4c00e3912f 194 if(ACS_STATE == '6') // check ACS_STATE = ACS_DETUMBLING_ONLY
sakthipriya 0:7b4c00e3912f 195 {
sakthipriya 0:7b4c00e3912f 196 FLAG();
sakthipriya 0:7b4c00e3912f 197 printf("\n\r Entered detumbling \n");
sakthipriya 0:7b4c00e3912f 198 ACS_STATUS = '6'; // set ACS_STATUS = ACS_DETUMBLING_ONLY
sakthipriya 0:7b4c00e3912f 199 FCTN_ACS_CNTRLALGO(b1,omega1); // detumbling code has to be included
sakthipriya 0:7b4c00e3912f 200 FCTN_ACS_GENPWM_MAIN(moment) ;
sakthipriya 0:7b4c00e3912f 201 }
sakthipriya 0:7b4c00e3912f 202 else
sakthipriya 0:7b4c00e3912f 203 {
sakthipriya 0:7b4c00e3912f 204 FLAG();
sakthipriya 0:7b4c00e3912f 205 printf("\n\r invalid state");
sakthipriya 0:7b4c00e3912f 206 ACS_STATUS = '7' ; // set ACS_STATUS = INVALID STATE
sakthipriya 0:7b4c00e3912f 207 PWM1 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 208 PWM2 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 209 PWM3 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 210 }//else of invalid
sakthipriya 0:7b4c00e3912f 211 }//else of autocontrol
sakthipriya 0:7b4c00e3912f 212 }//else of nominal
sakthipriya 0:7b4c00e3912f 213 }//else of data acg failure
sakthipriya 0:7b4c00e3912f 214
sakthipriya 0:7b4c00e3912f 215 }//else fo z axis moment only
sakthipriya 0:7b4c00e3912f 216 }//if power >2
sakthipriya 0:7b4c00e3912f 217 else
sakthipriya 0:7b4c00e3912f 218 {
sakthipriya 0:7b4c00e3912f 219 FLAG();
sakthipriya 0:7b4c00e3912f 220 printf("\n\r low power");
sakthipriya 0:7b4c00e3912f 221 ACS_STATUS = '1'; // set ACS_STATUS = ACS_LOW_POWER
sakthipriya 0:7b4c00e3912f 222 PWM1 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 223 PWM2 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 224 PWM3 = 0; //clear pwm pins
sakthipriya 0:7b4c00e3912f 225 }
sakthipriya 0:7b4c00e3912f 226 } //else for acs control off
sakthipriya 0:7b4c00e3912f 227
sakthipriya 0:7b4c00e3912f 228
sakthipriya 0:7b4c00e3912f 229
sakthipriya 0:7b4c00e3912f 230 ACS_MAIN_STATUS = 'c'; //clear ACS_MAIN_STATUS flag
sakthipriya 0:7b4c00e3912f 231 }//while ends
sakthipriya 0:7b4c00e3912f 232
sakthipriya 0:7b4c00e3912f 233
sakthipriya 0:7b4c00e3912f 234 }
sakthipriya 0:7b4c00e3912f 235 //***************************************************EPS THREAD***********************************************//
sakthipriya 0:7b4c00e3912f 236
sakthipriya 0:7b4c00e3912f 237 void T_EPS(void const *args)
sakthipriya 0:7b4c00e3912f 238 {
sakthipriya 0:7b4c00e3912f 239 while(1)
sakthipriya 0:7b4c00e3912f 240 {
sakthipriya 0:7b4c00e3912f 241 Thread::signal_wait(0x2);
sakthipriya 0:7b4c00e3912f 242 pc.printf("\n\rEntered EPS %f\n",t_start.read());
sakthipriya 0:7b4c00e3912f 243 EPS_MAIN_STATUS = 's'; // Set EPS main status
sakthipriya 0:7b4c00e3912f 244 //FCTN_READ_HK();
sakthipriya 0:7b4c00e3912f 245 //FCTN_APPEND_HKDATA();
sakthipriya 0:7b4c00e3912f 246 FCTN_BATTERYGAUGE_MAIN(actual_data.Batt_gauge_actual);
sakthipriya 0:7b4c00e3912f 247 if (actual_data.Battery_gauge_actual[1] == 200) //data not received
sakthipriya 0:7b4c00e3912f 248 {
sakthipriya 0:7b4c00e3912f 249 actual_data.power_mode = 1;
sakthipriya 0:7b4c00e3912f 250 EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 251
sakthipriya 0:7b4c00e3912f 252 }
sakthipriya 0:7b4c00e3912f 253 else
sakthipriya 0:7b4c00e3912f 254 {
sakthipriya 0:7b4c00e3912f 255 FCTN_EPS_POWERMODE(actual_data.Battery_gauge_actual[1]); //updating power level
sakthipriya 0:7b4c00e3912f 256 EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 257 }
sakthipriya 0:7b4c00e3912f 258 /* if( Temperature data received)
sakthipriya 0:7b4c00e3912f 259 {
sakthipriya 0:7b4c00e3912f 260 FCTN_BATT_TEMP_SENSOR_MAIN();
sakthipriya 0:7b4c00e3912f 261 EPS_BATTERY_TEMP_STATUS = 's'; //set EPS_BATTERY_TEMP_STATUS
sakthipriya 0:7b4c00e3912f 262 if(EPS_BATTERY_HEAT_ENABLE = 'e')
sakthipriya 0:7b4c00e3912f 263 {
sakthipriya 0:7b4c00e3912f 264 if(actual_data.Batt_temp_actual[0] < batt_heat_low)
sakthipriya 0:7b4c00e3912f 265 {
sakthipriya 0:7b4c00e3912f 266 batt_heat = 1; //turn on battery heater
sakthipriya 0:7b4c00e3912f 267 }
sakthipriya 0:7b4c00e3912f 268 else
sakthipriya 0:7b4c00e3912f 269 {
sakthipriya 0:7b4c00e3912f 270 batt_heat = 0; //turn off battery heater
sakthipriya 0:7b4c00e3912f 271 }
sakthipriya 0:7b4c00e3912f 272
sakthipriya 0:7b4c00e3912f 273 }
sakthipriya 0:7b4c00e3912f 274 else if(EPS_BATTERY_HEAT_ENABLE = 'd)
sakthipriya 0:7b4c00e3912f 275 {
sakthipriya 0:7b4c00e3912f 276 EPS_STATUS = EPS_BATTERY_HEATER_DISABLED;
sakthipriya 0:7b4c00e3912f 277 }
sakthipriya 0:7b4c00e3912f 278
sakthipriya 0:7b4c00e3912f 279 }
sakthipriya 0:7b4c00e3912f 280 else
sakthipriya 0:7b4c00e3912f 281 {
sakthipriya 0:7b4c00e3912f 282 Set battery temp to XX
sakthipriya 0:7b4c00e3912f 283 EPS_BATTERY_TEMP_STATUS = 'c'; //clear EPS_BATTERY_TEMP_STATUS
sakthipriya 0:7b4c00e3912f 284 EPS_STATUS = EPS_ERR_BATTERY_TEMP;
sakthipriya 0:7b4c00e3912f 285 }
sakthipriya 0:7b4c00e3912f 286
sakthipriya 0:7b4c00e3912f 287 EPS_MAIN_STATUS = 'c'; // clear EPS main status */
sakthipriya 0:7b4c00e3912f 288
sakthipriya 0:7b4c00e3912f 289 }
sakthipriya 0:7b4c00e3912f 290
sakthipriya 0:7b4c00e3912f 291 }
sakthipriya 0:7b4c00e3912f 292
sakthipriya 0:7b4c00e3912f 293 //**************************************************BCN THREAD*******************************************************************//
sakthipriya 0:7b4c00e3912f 294
sakthipriya 0:7b4c00e3912f 295 void T_BCN(void const *args)
sakthipriya 0:7b4c00e3912f 296 {
sakthipriya 0:7b4c00e3912f 297 while(1)
sakthipriya 0:7b4c00e3912f 298 {
sakthipriya 0:7b4c00e3912f 299 Thread::signal_wait(0x3);
sakthipriya 0:7b4c00e3912f 300 pc.printf("\n\rEntered BCN %f\n",t_start.read());
sakthipriya 0:7b4c00e3912f 301
sakthipriya 0:7b4c00e3912f 302 P_BCN_TX_MAIN();
sakthipriya 0:7b4c00e3912f 303 }
sakthipriya 0:7b4c00e3912f 304
sakthipriya 0:7b4c00e3912f 305 }
sakthipriya 0:7b4c00e3912f 306
sakthipriya 0:7b4c00e3912f 307 //**************************************************TCTM THREAD*******************************************************************//
sakthipriya 0:7b4c00e3912f 308
sakthipriya 0:7b4c00e3912f 309 void T_TC(void const * args)
sakthipriya 0:7b4c00e3912f 310 {
sakthipriya 0:7b4c00e3912f 311 while(1)
sakthipriya 0:7b4c00e3912f 312 {
sakthipriya 0:7b4c00e3912f 313 Thread::signal_wait(0x4);
sakthipriya 0:7b4c00e3912f 314 wait_us(200); // can be between 38 to 15700
sakthipriya 0:7b4c00e3912f 315 if( slave.receive() == 0)
sakthipriya 0:7b4c00e3912f 316 slave.stop();
sakthipriya 0:7b4c00e3912f 317 else if( slave.receive() == 1) // slave writes to master
sakthipriya 0:7b4c00e3912f 318 {
sakthipriya 0:7b4c00e3912f 319 write_ack=slave.write(telemetry,tm_len);
sakthipriya 0:7b4c00e3912f 320 }
sakthipriya 0:7b4c00e3912f 321 else if( slave.receive()==3 || slave.receive()==2) // slave read
sakthipriya 0:7b4c00e3912f 322 {
sakthipriya 0:7b4c00e3912f 323 read_ack=slave.read(telecommand,tc_len);
sakthipriya 0:7b4c00e3912f 324 pc.printf("\n\rTELECOMMAND received from CDMS is %s \n",telecommand);
sakthipriya 0:7b4c00e3912f 325 pc.printf("\n\r Executing Telecommand \n");
sakthipriya 0:7b4c00e3912f 326 FCTN_TC_DECODE((uint8_t*) telecommand);
sakthipriya 0:7b4c00e3912f 327 FCTN_TM();
sakthipriya 0:7b4c00e3912f 328 }
sakthipriya 0:7b4c00e3912f 329
sakthipriya 0:7b4c00e3912f 330 }
sakthipriya 0:7b4c00e3912f 331 }
sakthipriya 0:7b4c00e3912f 332
sakthipriya 0:7b4c00e3912f 333 void FCTN_TM()
sakthipriya 0:7b4c00e3912f 334 {
sakthipriya 0:7b4c00e3912f 335 irpt_2_mstr = 0;
sakthipriya 0:7b4c00e3912f 336 pc.printf("\n\r Telemetry Generation \n");
sakthipriya 0:7b4c00e3912f 337 irpt_2_mstr = 1;
sakthipriya 0:7b4c00e3912f 338 }
sakthipriya 0:7b4c00e3912f 339
sakthipriya 0:7b4c00e3912f 340
sakthipriya 0:7b4c00e3912f 341 //******************************************************* I2C *******************************************************************//
sakthipriya 0:7b4c00e3912f 342
sakthipriya 0:7b4c00e3912f 343 void FCTN_I2C_ISR()
sakthipriya 0:7b4c00e3912f 344 {
sakthipriya 0:7b4c00e3912f 345 ptr_t_i2c->signal_set(0x4);
sakthipriya 0:7b4c00e3912f 346 }
sakthipriya 0:7b4c00e3912f 347
sakthipriya 0:7b4c00e3912f 348
sakthipriya 0:7b4c00e3912f 349 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:7b4c00e3912f 350 //SCHEDULER
sakthipriya 0:7b4c00e3912f 351 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:7b4c00e3912f 352 uint8_t schedcount=1;
sakthipriya 0:7b4c00e3912f 353 void T_SC(void const *args)
sakthipriya 0:7b4c00e3912f 354 {
sakthipriya 0:7b4c00e3912f 355 printf("\n\r in scheduler");
sakthipriya 0:7b4c00e3912f 356
sakthipriya 0:7b4c00e3912f 357 if(schedcount == 7) //to reset the counter
sakthipriya 0:7b4c00e3912f 358 {
sakthipriya 0:7b4c00e3912f 359 schedcount = 1;
sakthipriya 0:7b4c00e3912f 360 }
sakthipriya 0:7b4c00e3912f 361 if(schedcount%1==0)
sakthipriya 0:7b4c00e3912f 362 {
sakthipriya 0:7b4c00e3912f 363 ptr_t_acs -> signal_set(0x1);
sakthipriya 0:7b4c00e3912f 364 }
sakthipriya 0:7b4c00e3912f 365
sakthipriya 0:7b4c00e3912f 366 if(schedcount%2==0)
sakthipriya 0:7b4c00e3912f 367 {
sakthipriya 0:7b4c00e3912f 368 // ptr_t_eps -> signal_set(0x2);
sakthipriya 0:7b4c00e3912f 369
sakthipriya 0:7b4c00e3912f 370 }
sakthipriya 0:7b4c00e3912f 371 if(schedcount%3==0)
sakthipriya 0:7b4c00e3912f 372 {
sakthipriya 0:7b4c00e3912f 373 //ptr_t_bcn -> signal_set(0x3);
sakthipriya 0:7b4c00e3912f 374 }
sakthipriya 0:7b4c00e3912f 375 schedcount++;
sakthipriya 0:7b4c00e3912f 376 printf("\n\r exited scheduler");
sakthipriya 0:7b4c00e3912f 377 }
sakthipriya 0:7b4c00e3912f 378
sakthipriya 0:7b4c00e3912f 379 Timer t_flag;
sakthipriya 0:7b4c00e3912f 380 void FLAG()
sakthipriya 0:7b4c00e3912f 381 {
sakthipriya 0:7b4c00e3912f 382
sakthipriya 0:7b4c00e3912f 383 //.............acs..................//
sakthipriya 0:7b4c00e3912f 384 if(ACS_INIT_STATUS == 's')
sakthipriya 0:7b4c00e3912f 385 BAE_STATUS = BAE_STATUS | 0x00000080; //set ACS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 386 else if(ACS_INIT_STATUS == 'c')
sakthipriya 0:7b4c00e3912f 387 BAE_STATUS &= 0xFFFFFF7F; //clear ACS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 388
sakthipriya 0:7b4c00e3912f 389 if(ACS_DATA_ACQ_STATUS == 's')
sakthipriya 0:7b4c00e3912f 390 BAE_STATUS =BAE_STATUS | 0x00000100; //set ACS_DATA_ACQ_STATUS flag
sakthipriya 0:7b4c00e3912f 391 else if(ACS_DATA_ACQ_STATUS == 'c')
sakthipriya 0:7b4c00e3912f 392 BAE_STATUS &= 0xFFFFFEFF; //clear ACS_DATA_ACQ_STATUS flag
sakthipriya 0:7b4c00e3912f 393
sakthipriya 0:7b4c00e3912f 394 if(ACS_ATS_ENABLE == 'e')
sakthipriya 0:7b4c00e3912f 395 BAE_ENABLE |= 0x00000004;
sakthipriya 0:7b4c00e3912f 396 else if(ACS_ATS_ENABLE == 'd')
sakthipriya 0:7b4c00e3912f 397 BAE_ENABLE = BAE_ENABLE &0xFFFFFFFB | 0x00000004;
sakthipriya 0:7b4c00e3912f 398
sakthipriya 0:7b4c00e3912f 399 if(ACS_DATA_ACQ_STATUS == 'f')
sakthipriya 0:7b4c00e3912f 400 BAE_STATUS |= 0x00000200;
sakthipriya 0:7b4c00e3912f 401
sakthipriya 0:7b4c00e3912f 402 if(ACS_MAIN_STATUS == 's')
sakthipriya 0:7b4c00e3912f 403 BAE_STATUS = (BAE_STATUS | 0x00001000); //set ACS_MAIN_STATUS flag
sakthipriya 0:7b4c00e3912f 404 else if(ACS_MAIN_STATUS == 'c')
sakthipriya 0:7b4c00e3912f 405 BAE_STATUS &= 0xFFFFEFFF; //clear ACS_MAIN_STATUS flag
sakthipriya 0:7b4c00e3912f 406
sakthipriya 0:7b4c00e3912f 407 if(ACS_STATUS == '0')
sakthipriya 0:7b4c00e3912f 408 BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF); // set ACS_STATUS = ACS_CONTROL_OFF
sakthipriya 0:7b4c00e3912f 409 else if(ACS_STATUS == '1')
sakthipriya 0:7b4c00e3912f 410 BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x00002000; // set ACS_STATUS = ACS_LOW_POWER
sakthipriya 0:7b4c00e3912f 411 else if(ACS_STATUS == '2')
sakthipriya 0:7b4c00e3912f 412 BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF)| 0x00004000; // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY
sakthipriya 0:7b4c00e3912f 413 else if(ACS_STATUS == '3')
sakthipriya 0:7b4c00e3912f 414 BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00006000; // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
sakthipriya 0:7b4c00e3912f 415 else if(ACS_STATUS == '4')
sakthipriya 0:7b4c00e3912f 416 BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00008000; // set ACS_STATUS = ACS_NOMINAL_ONLY
sakthipriya 0:7b4c00e3912f 417 else if(ACS_STATUS == '5')
sakthipriya 0:7b4c00e3912f 418 BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000A000; // set ACS_STATUS = ACS_AUTO_CONTROL
sakthipriya 0:7b4c00e3912f 419 else if(ACS_STATUS == '6')
sakthipriya 0:7b4c00e3912f 420 BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000C000; // set ACS_STATUS = ACS_DETUMBLING_ONLY
sakthipriya 0:7b4c00e3912f 421 else
sakthipriya 0:7b4c00e3912f 422 BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000E000; // set ACS_STATUS = INVALID STATE
sakthipriya 0:7b4c00e3912f 423
sakthipriya 0:7b4c00e3912f 424 if(ACS_STATE == '0')
sakthipriya 0:7b4c00e3912f 425 BAE_ENABLE = (BAE_ENABLE & 0xFFFFFF8F); //ACS_STATE = ACS_CONTROL_OFF
sakthipriya 0:7b4c00e3912f 426 else if(ACS_STATE == '2')
sakthipriya 0:7b4c00e3912f 427 BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000020); // ACS_STATE = ACS_ZAXIS_MOMENT_ONLY
sakthipriya 0:7b4c00e3912f 428 else if(ACS_STATE == '3')
sakthipriya 0:7b4c00e3912f 429 BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000030); // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
sakthipriya 0:7b4c00e3912f 430 else if(ACS_STATE == '4')
sakthipriya 0:7b4c00e3912f 431 BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000040); // ACS_STATE = ACS_NOMINAL_ONLY
sakthipriya 0:7b4c00e3912f 432 else if(ACS_STATE == '5')
sakthipriya 0:7b4c00e3912f 433 BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000050); // ACS_STATE = ACS_AUTO_CONTROL
sakthipriya 0:7b4c00e3912f 434 else if(ACS_STATE == '6')
sakthipriya 0:7b4c00e3912f 435 BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000060); //ACS_STATE = ACS_DETUMBLING_CONTROL
sakthipriya 0:7b4c00e3912f 436
sakthipriya 0:7b4c00e3912f 437 //...............eps......................//
sakthipriya 0:7b4c00e3912f 438 if(EPS_INIT_STATUS == 's')
sakthipriya 0:7b4c00e3912f 439 BAE_STATUS |= 0x00010000; //set EPS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 440 else if(EPS_INIT_STATUS == 'c')
sakthipriya 0:7b4c00e3912f 441 BAE_STATUS &= 0xFFFEFFFF; //clear EPS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 442 if(EPS_BATTERY_GAUGE_STATUS == 'c')
sakthipriya 0:7b4c00e3912f 443 BAE_STATUS &= 0xFFFDFFFF; //clear EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 444 else if(EPS_BATTERY_GAUGE_STATUS == 's')
sakthipriya 0:7b4c00e3912f 445 BAE_STATUS |= 0x00020000; //set EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 446
sakthipriya 0:7b4c00e3912f 447
sakthipriya 0:7b4c00e3912f 448 pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE);
sakthipriya 0:7b4c00e3912f 449 }
sakthipriya 0:7b4c00e3912f 450
sakthipriya 0:7b4c00e3912f 451 void FCTN_BAE_INIT()
sakthipriya 0:7b4c00e3912f 452 {
sakthipriya 0:7b4c00e3912f 453 printf("\n\r Initialising BAE ");
sakthipriya 0:7b4c00e3912f 454 FCTN_ACS_INIT();
sakthipriya 0:7b4c00e3912f 455 FCTN_EPS_INIT();
sakthipriya 0:7b4c00e3912f 456 //P_BCN_INIT();
sakthipriya 0:7b4c00e3912f 457 FLAG();
sakthipriya 0:7b4c00e3912f 458 }
sakthipriya 0:7b4c00e3912f 459
sakthipriya 0:7b4c00e3912f 460 int main()
sakthipriya 0:7b4c00e3912f 461 {
sakthipriya 0:7b4c00e3912f 462 pc.printf("\n\r BAE Activated. Testing Version 1.1 \n");
sakthipriya 0:7b4c00e3912f 463
sakthipriya 0:7b4c00e3912f 464 /* if (BCN_FEN == 0) //dummy implementation
sakthipriya 0:7b4c00e3912f 465 {
sakthipriya 0:7b4c00e3912f 466 pc.printf("\n\r RF silence ");
sakthipriya 0:7b4c00e3912f 467 P_BCN_FEN();
sakthipriya 0:7b4c00e3912f 468 t_rfsilence.start();//Start the timer for RF_Silence
sakthipriya 0:7b4c00e3912f 469 while(t_rfsilence.read() < RF_SILENCE_TIME);
sakthipriya 0:7b4c00e3912f 470 }
sakthipriya 0:7b4c00e3912f 471 */
sakthipriya 0:7b4c00e3912f 472 ACS_STATE = '4';
sakthipriya 0:7b4c00e3912f 473 //ACS_INIT_STATUS = 'c';
sakthipriya 0:7b4c00e3912f 474 //ACS_DATA_ACQ_STATUS = 'c';
sakthipriya 0:7b4c00e3912f 475 gpo1 = 0;
sakthipriya 0:7b4c00e3912f 476 FLAG();
sakthipriya 0:7b4c00e3912f 477 FCTN_BAE_INIT();
sakthipriya 0:7b4c00e3912f 478 ACS_ATS_ENABLE = 'e';
sakthipriya 0:7b4c00e3912f 479 ACS_DATA_ACQ_ENABLE = 'e';
sakthipriya 0:7b4c00e3912f 480
sakthipriya 0:7b4c00e3912f 481 //...i2c..
sakthipriya 0:7b4c00e3912f 482 strcpy(telemetry,"This is telemetry THis is sample telemetry. ffffffffffffffffffffffffffffff end");
sakthipriya 0:7b4c00e3912f 483 slave.address(addr);
sakthipriya 0:7b4c00e3912f 484 irpt_2_mstr = 0;
sakthipriya 0:7b4c00e3912f 485
sakthipriya 0:7b4c00e3912f 486 ptr_t_i2c = new Thread(T_TC);
sakthipriya 0:7b4c00e3912f 487 ptr_t_i2c->set_priority(osPriorityHigh);
sakthipriya 0:7b4c00e3912f 488 ptr_t_acs = new Thread(T_ACS);
sakthipriya 0:7b4c00e3912f 489 ptr_t_acs->set_priority(osPriorityAboveNormal);
sakthipriya 0:7b4c00e3912f 490 ptr_t_eps = new Thread(T_EPS);
sakthipriya 0:7b4c00e3912f 491 ptr_t_eps->set_priority(osPriorityAboveNormal);
sakthipriya 0:7b4c00e3912f 492 ptr_t_bcn = new Thread(T_BCN);
sakthipriya 0:7b4c00e3912f 493 ptr_t_bcn->set_priority(osPriorityAboveNormal);
sakthipriya 0:7b4c00e3912f 494
sakthipriya 0:7b4c00e3912f 495 irpt_4m_mstr.enable_irq();
sakthipriya 0:7b4c00e3912f 496 irpt_4m_mstr.rise(&FCTN_I2C_ISR);
sakthipriya 0:7b4c00e3912f 497 RtosTimer t_sc_timer(T_SC,osTimerPeriodic); // Initiating the scheduler thread
sakthipriya 0:7b4c00e3912f 498 t_sc_timer.start(10000);
sakthipriya 0:7b4c00e3912f 499 t_start.start();
sakthipriya 0:7b4c00e3912f 500 pc.printf("\n\rStarted scheduler %f\n\r",t_start.read());
sakthipriya 0:7b4c00e3912f 501
sakthipriya 0:7b4c00e3912f 502 gpo1 = 0; // att sens2 switch is enabled
sakthipriya 0:7b4c00e3912f 503 //FCTN_BAE_INIT();
sakthipriya 0:7b4c00e3912f 504 while(1); //required to prevent main from terminating
sakthipriya 0:7b4c00e3912f 505 }