i2c working

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDMTESIN2 by Seeker of Truth ,

Committer:
raizel_varun
Date:
Thu Dec 11 07:34:17 2014 +0000
Revision:
8:69475d16d23d
Parent:
7:f06840d848e3
Child:
9:221d218f4690
Fault ready

Who changed what in which revision?

UserRevisionLine numberNew contents of line
greenroshks 0:8b0d43fe6c05 1 #include "mbed.h"
greenroshks 0:8b0d43fe6c05 2 #include "rtos.h"
greenroshks 0:8b0d43fe6c05 3 #include "HK.h"
greenroshks 0:8b0d43fe6c05 4 #include "slave.h"
greenroshks 0:8b0d43fe6c05 5 #include "beacon.h"
greenroshks 0:8b0d43fe6c05 6 #include "ACS.h"
sakthipriya 2:80b8a2e999f7 7 #include "fault.h"
greenroshks 0:8b0d43fe6c05 8
greenroshks 0:8b0d43fe6c05 9 Serial pc(USBTX, USBRX);
greenroshks 0:8b0d43fe6c05 10
greenroshks 0:8b0d43fe6c05 11
greenroshks 0:8b0d43fe6c05 12
sakthipriya 5:255b43e8e21a 13 Timer t; //To know the time of execution each thread
sakthipriya 4:8f6c24eca109 14 Timer t1;
sakthipriya 5:255b43e8e21a 15 //To know the time of entering of each thread
greenroshks 0:8b0d43fe6c05 16
greenroshks 0:8b0d43fe6c05 17 Thread *ptr_t_hk_acq;
greenroshks 0:8b0d43fe6c05 18 Thread *ptr_t_acs;
greenroshks 0:8b0d43fe6c05 19 Thread *ptr_t_acs_write2flash;
greenroshks 0:8b0d43fe6c05 20 Thread *ptr_t_bea;
greenroshks 0:8b0d43fe6c05 21 Thread *ptr_t_bea_telecommand;
greenroshks 0:8b0d43fe6c05 22 Thread *ptr_t_fault;
greenroshks 0:8b0d43fe6c05 23
greenroshks 0:8b0d43fe6c05 24
greenroshks 0:8b0d43fe6c05 25 //--------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 26 //TASK 2 : HK
greenroshks 0:8b0d43fe6c05 27 //--------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 28
greenroshks 0:8b0d43fe6c05 29
greenroshks 0:8b0d43fe6c05 30
greenroshks 0:8b0d43fe6c05 31 void t_hk_acq(void const *args)
greenroshks 0:8b0d43fe6c05 32 {
greenroshks 0:8b0d43fe6c05 33
greenroshks 0:8b0d43fe6c05 34 while(1)
greenroshks 0:8b0d43fe6c05 35 {
greenroshks 0:8b0d43fe6c05 36 Thread::signal_wait(0x2);
greenroshks 0:8b0d43fe6c05 37
greenroshks 0:8b0d43fe6c05 38 printf("\nTHIS IS HK %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 39 t.start();
greenroshks 0:8b0d43fe6c05 40
greenroshks 0:8b0d43fe6c05 41 FUNC_HK_MAIN(); //Collecting HK data
greenroshks 0:8b0d43fe6c05 42 //thread_2.signal_set(0x4);
greenroshks 0:8b0d43fe6c05 43 FUNC_I2C_SLAVE_MAIN(24); //Sending to CDMS via I2C
greenroshks 0:8b0d43fe6c05 44
greenroshks 0:8b0d43fe6c05 45 t.stop();
greenroshks 0:8b0d43fe6c05 46 printf("The time to execute hk_acq is %f seconds\n",t.read());
greenroshks 0:8b0d43fe6c05 47 t.reset();
greenroshks 0:8b0d43fe6c05 48 }
greenroshks 0:8b0d43fe6c05 49 }
greenroshks 0:8b0d43fe6c05 50
greenroshks 0:8b0d43fe6c05 51 //---------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 52 //TASK 1 : ACS
greenroshks 0:8b0d43fe6c05 53 //---------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 54 typedef struct {
greenroshks 0:8b0d43fe6c05 55 float mag_field;
greenroshks 0:8b0d43fe6c05 56 float omega;
greenroshks 0:8b0d43fe6c05 57 } sensor_data;
greenroshks 0:8b0d43fe6c05 58
greenroshks 0:8b0d43fe6c05 59 Mail <sensor_data, 16> q_acs;
greenroshks 0:8b0d43fe6c05 60
greenroshks 0:8b0d43fe6c05 61 void func_acs_readdata(sensor_data *ptr)
greenroshks 0:8b0d43fe6c05 62 {
greenroshks 0:8b0d43fe6c05 63 printf("Reading the data\n");
greenroshks 0:8b0d43fe6c05 64 ptr -> mag_field = 10;
greenroshks 0:8b0d43fe6c05 65 ptr -> omega = 3;
greenroshks 0:8b0d43fe6c05 66 }
greenroshks 0:8b0d43fe6c05 67
greenroshks 0:8b0d43fe6c05 68 void func_acs_ctrlalgo()
greenroshks 0:8b0d43fe6c05 69 {
greenroshks 0:8b0d43fe6c05 70 printf("Executing control algo\n");
greenroshks 0:8b0d43fe6c05 71 }
greenroshks 0:8b0d43fe6c05 72
greenroshks 0:8b0d43fe6c05 73
greenroshks 0:8b0d43fe6c05 74
greenroshks 0:8b0d43fe6c05 75 void func_acs_write2flash(sensor_data *ptr2)
greenroshks 0:8b0d43fe6c05 76 {
greenroshks 0:8b0d43fe6c05 77 printf("The magnetic field is %.2f T\n\r",ptr2->mag_field);
greenroshks 0:8b0d43fe6c05 78 printf("The angular velocity is %.2f rad/s\n\r",ptr2->omega);
greenroshks 0:8b0d43fe6c05 79 }
greenroshks 0:8b0d43fe6c05 80
sakthipriya 5:255b43e8e21a 81 int acs_pflag = 1;
greenroshks 0:8b0d43fe6c05 82 void t_acs(void const *args)
greenroshks 0:8b0d43fe6c05 83 {
greenroshks 0:8b0d43fe6c05 84 while(1)
greenroshks 0:8b0d43fe6c05 85 {
greenroshks 0:8b0d43fe6c05 86 Thread::signal_wait(0x1);
greenroshks 0:8b0d43fe6c05 87 printf("\nTHIS IS ACS %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 88 t.start();
greenroshks 0:8b0d43fe6c05 89 sensor_data *ptr = q_acs.alloc();
greenroshks 0:8b0d43fe6c05 90
greenroshks 0:8b0d43fe6c05 91 func_acs_readdata(ptr);
raizel_varun 7:f06840d848e3 92 //printf("\ngdhd\n");
greenroshks 0:8b0d43fe6c05 93 q_acs.put(ptr);
sakthipriya 5:255b43e8e21a 94 if(acs_pflag == 1)
sakthipriya 5:255b43e8e21a 95 {
greenroshks 0:8b0d43fe6c05 96 func_acs_ctrlalgo();
greenroshks 0:8b0d43fe6c05 97 FUNC_ACS_GENPWM(); //Generating PWM signal.
sakthipriya 5:255b43e8e21a 98 }
greenroshks 0:8b0d43fe6c05 99
greenroshks 0:8b0d43fe6c05 100 t.reset();
greenroshks 0:8b0d43fe6c05 101 }
greenroshks 0:8b0d43fe6c05 102 }
greenroshks 0:8b0d43fe6c05 103
greenroshks 0:8b0d43fe6c05 104 void t_acs_write2flash(void const *args)
greenroshks 0:8b0d43fe6c05 105 {
greenroshks 0:8b0d43fe6c05 106 while(1)
greenroshks 0:8b0d43fe6c05 107 {
greenroshks 0:8b0d43fe6c05 108 //printf("Writing in the flash\n");
greenroshks 0:8b0d43fe6c05 109 osEvent evt = q_acs.get();
greenroshks 0:8b0d43fe6c05 110 if(evt.status == osEventMail)
greenroshks 0:8b0d43fe6c05 111 {
greenroshks 0:8b0d43fe6c05 112 sensor_data *ptr = (sensor_data*)evt.value.p;
greenroshks 0:8b0d43fe6c05 113 func_acs_write2flash(ptr);
greenroshks 0:8b0d43fe6c05 114 q_acs.free(ptr);
greenroshks 0:8b0d43fe6c05 115 }
greenroshks 0:8b0d43fe6c05 116 printf("Writing acs data in the flash\n");
greenroshks 0:8b0d43fe6c05 117 }
greenroshks 0:8b0d43fe6c05 118 }
greenroshks 0:8b0d43fe6c05 119
greenroshks 0:8b0d43fe6c05 120
greenroshks 0:8b0d43fe6c05 121 //---------------------------------------------------BEACON--------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 122
greenroshks 0:8b0d43fe6c05 123 int beac_flag=0; //To receive telecommand from ground.
greenroshks 0:8b0d43fe6c05 124
greenroshks 0:8b0d43fe6c05 125
greenroshks 0:8b0d43fe6c05 126 void t_bea_telecommand(void const *args)
greenroshks 0:8b0d43fe6c05 127 {
greenroshks 0:8b0d43fe6c05 128 char c = pc.getc();
greenroshks 0:8b0d43fe6c05 129 if(c=='a')
greenroshks 0:8b0d43fe6c05 130 {
greenroshks 0:8b0d43fe6c05 131 printf("Telecommand detected\n");
greenroshks 0:8b0d43fe6c05 132 beac_flag=1;
greenroshks 0:8b0d43fe6c05 133 }
greenroshks 0:8b0d43fe6c05 134 }
greenroshks 0:8b0d43fe6c05 135
greenroshks 0:8b0d43fe6c05 136 void t_bea(void const *args)
greenroshks 0:8b0d43fe6c05 137 {
greenroshks 0:8b0d43fe6c05 138
greenroshks 0:8b0d43fe6c05 139 while(1)
greenroshks 0:8b0d43fe6c05 140 {
greenroshks 0:8b0d43fe6c05 141 Thread::signal_wait(0x3);
greenroshks 0:8b0d43fe6c05 142 printf("\nTHIS IS BEACON %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 143 t.start();
greenroshks 0:8b0d43fe6c05 144
greenroshks 0:8b0d43fe6c05 145
greenroshks 0:8b0d43fe6c05 146
greenroshks 0:8b0d43fe6c05 147 FUNC_BEA();
greenroshks 0:8b0d43fe6c05 148
greenroshks 0:8b0d43fe6c05 149
greenroshks 0:8b0d43fe6c05 150 if(beac_flag==1)
greenroshks 0:8b0d43fe6c05 151 {
greenroshks 0:8b0d43fe6c05 152 Thread::wait(600000);
greenroshks 0:8b0d43fe6c05 153 beac_flag = 0;
greenroshks 0:8b0d43fe6c05 154 }
greenroshks 0:8b0d43fe6c05 155
greenroshks 0:8b0d43fe6c05 156 printf("The time to execute beacon thread is %f seconds\n",t.read());
greenroshks 0:8b0d43fe6c05 157 t.reset();
greenroshks 0:8b0d43fe6c05 158 }
greenroshks 0:8b0d43fe6c05 159 }
greenroshks 0:8b0d43fe6c05 160
greenroshks 0:8b0d43fe6c05 161 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 162 //TASK 4 : FAULT MANAGEMENT
greenroshks 0:8b0d43fe6c05 163 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 164 //Dummy fault rectifier functions
greenroshks 0:8b0d43fe6c05 165
sakthipriya 2:80b8a2e999f7 166 /*Mail<int,16> faults;
greenroshks 0:8b0d43fe6c05 167
greenroshks 0:8b0d43fe6c05 168 void FUNC_FAULT_FUNCT1()
greenroshks 0:8b0d43fe6c05 169 {
greenroshks 0:8b0d43fe6c05 170 printf("\nFault 1 detected... \n");
greenroshks 0:8b0d43fe6c05 171 }
greenroshks 0:8b0d43fe6c05 172
greenroshks 0:8b0d43fe6c05 173 void FUNC_FAULT_FUNCT2()
greenroshks 0:8b0d43fe6c05 174 {
greenroshks 0:8b0d43fe6c05 175 printf("\nFault 2 detected...\n");
greenroshks 0:8b0d43fe6c05 176 }
greenroshks 0:8b0d43fe6c05 177
greenroshks 0:8b0d43fe6c05 178 void T_FAULT(void const *args)
greenroshks 0:8b0d43fe6c05 179 {
greenroshks 0:8b0d43fe6c05 180 while(1)
greenroshks 0:8b0d43fe6c05 181 {
greenroshks 0:8b0d43fe6c05 182 osEvent evt = faults.get();
greenroshks 0:8b0d43fe6c05 183 if(evt.status==osEventMail)
greenroshks 0:8b0d43fe6c05 184 {
greenroshks 0:8b0d43fe6c05 185 int *fault_id= (int *)evt.value.p;
greenroshks 0:8b0d43fe6c05 186 switch(*fault_id)
greenroshks 0:8b0d43fe6c05 187 {
greenroshks 0:8b0d43fe6c05 188 case 1: FUNC_FAULT_FUNCT1();
greenroshks 0:8b0d43fe6c05 189 break;
greenroshks 0:8b0d43fe6c05 190 case 2: FUNC_FAULT_FUNCT2();
greenroshks 0:8b0d43fe6c05 191 break;
greenroshks 0:8b0d43fe6c05 192 }
greenroshks 0:8b0d43fe6c05 193 faults.free(fault_id);
greenroshks 0:8b0d43fe6c05 194 }
greenroshks 0:8b0d43fe6c05 195 }
sakthipriya 2:80b8a2e999f7 196 }*/
sakthipriya 5:255b43e8e21a 197
sakthipriya 5:255b43e8e21a 198 extern SensorData Sensor;
raizel_varun 6:e65b1ab79f36 199
raizel_varun 6:e65b1ab79f36 200
sakthipriya 2:80b8a2e999f7 201 void T_FAULT(void const *args)
sakthipriya 4:8f6c24eca109 202 {
sakthipriya 5:255b43e8e21a 203 Sensor.power_mode='0';
sakthipriya 2:80b8a2e999f7 204 while(1)
sakthipriya 2:80b8a2e999f7 205 {
sakthipriya 2:80b8a2e999f7 206 Thread :: signal_wait(0x2);
sakthipriya 2:80b8a2e999f7 207 FAULTS();
sakthipriya 5:255b43e8e21a 208 POWER(Sensor.power_mode);
sakthipriya 5:255b43e8e21a 209 //Sensor.power_mode++; //testing ... should be removed
sakthipriya 2:80b8a2e999f7 210 }
greenroshks 0:8b0d43fe6c05 211 }
greenroshks 0:8b0d43fe6c05 212
greenroshks 0:8b0d43fe6c05 213 //------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 214 //SCHEDULER
greenroshks 0:8b0d43fe6c05 215 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 4:8f6c24eca109 216 int beacon_sc = 3;
greenroshks 0:8b0d43fe6c05 217 uint16_t schedcount=1;
greenroshks 0:8b0d43fe6c05 218 void t_sc(void const *args)
greenroshks 0:8b0d43fe6c05 219 {
greenroshks 0:8b0d43fe6c05 220
greenroshks 0:8b0d43fe6c05 221 printf("The value of i in scheduler is %d\n",schedcount);
greenroshks 0:8b0d43fe6c05 222 if(schedcount == 65532) //to reset the counter
greenroshks 0:8b0d43fe6c05 223 {
greenroshks 0:8b0d43fe6c05 224 schedcount = 0;
greenroshks 0:8b0d43fe6c05 225 }
greenroshks 0:8b0d43fe6c05 226
greenroshks 0:8b0d43fe6c05 227 if(schedcount%1==0)
greenroshks 0:8b0d43fe6c05 228 {
greenroshks 0:8b0d43fe6c05 229 ptr_t_acs -> signal_set(0x1);
greenroshks 0:8b0d43fe6c05 230 }
greenroshks 0:8b0d43fe6c05 231 if(schedcount%2==0)
greenroshks 0:8b0d43fe6c05 232 {
sakthipriya 3:02b45de29c0f 233 ptr_t_fault -> signal_set(0x2);
greenroshks 0:8b0d43fe6c05 234 ptr_t_hk_acq -> signal_set(0x2);
sakthipriya 2:80b8a2e999f7 235
greenroshks 0:8b0d43fe6c05 236 }
sakthipriya 4:8f6c24eca109 237 if(schedcount%beacon_sc==0)
greenroshks 0:8b0d43fe6c05 238 {
greenroshks 0:8b0d43fe6c05 239 if(beac_flag==0)
greenroshks 0:8b0d43fe6c05 240 {
greenroshks 0:8b0d43fe6c05 241
greenroshks 0:8b0d43fe6c05 242 ptr_t_bea -> signal_set(0x3);
greenroshks 0:8b0d43fe6c05 243 }
greenroshks 0:8b0d43fe6c05 244 }
greenroshks 0:8b0d43fe6c05 245 schedcount++;
greenroshks 0:8b0d43fe6c05 246 }
greenroshks 0:8b0d43fe6c05 247
greenroshks 0:8b0d43fe6c05 248 //---------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 249
greenroshks 0:8b0d43fe6c05 250 int main()
greenroshks 0:8b0d43fe6c05 251 {
greenroshks 0:8b0d43fe6c05 252 t1.start();
greenroshks 0:8b0d43fe6c05 253
greenroshks 0:8b0d43fe6c05 254 ptr_t_hk_acq = new Thread(t_hk_acq);
greenroshks 0:8b0d43fe6c05 255 ptr_t_acs = new Thread(t_acs);
greenroshks 0:8b0d43fe6c05 256 ptr_t_acs_write2flash = new Thread(t_acs_write2flash);
greenroshks 0:8b0d43fe6c05 257 ptr_t_bea = new Thread(t_bea);
greenroshks 0:8b0d43fe6c05 258 ptr_t_bea_telecommand = new Thread(t_bea_telecommand);
greenroshks 0:8b0d43fe6c05 259 ptr_t_fault = new Thread(T_FAULT);
greenroshks 0:8b0d43fe6c05 260 //ptr_t_sc = new Thread(t_sc);
raizel_varun 8:69475d16d23d 261
raizel_varun 8:69475d16d23d 262 interrupt_fault();
raizel_varun 8:69475d16d23d 263
greenroshks 0:8b0d43fe6c05 264 ptr_t_fault -> set_priority(osPriorityRealtime);
greenroshks 0:8b0d43fe6c05 265 ptr_t_acs->set_priority(osPriorityHigh);
greenroshks 0:8b0d43fe6c05 266 ptr_t_hk_acq->set_priority(osPriorityNormal);
greenroshks 0:8b0d43fe6c05 267 ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal);
greenroshks 0:8b0d43fe6c05 268 ptr_t_bea->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 269 ptr_t_bea_telecommand->set_priority(osPriorityIdle);
greenroshks 0:8b0d43fe6c05 270 //ptr_t_sc->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 271
greenroshks 0:8b0d43fe6c05 272
greenroshks 0:8b0d43fe6c05 273 // ----------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 274 printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority());
greenroshks 0:8b0d43fe6c05 275 printf("\n T_ACS priority is %d",ptr_t_acs->get_priority());
greenroshks 0:8b0d43fe6c05 276 printf("\n T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
greenroshks 0:8b0d43fe6c05 277 printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority());
greenroshks 0:8b0d43fe6c05 278 printf("\n T_BEA priority is %d",ptr_t_bea->get_priority());
greenroshks 0:8b0d43fe6c05 279 RtosTimer t_sc_timer(t_sc,osTimerPeriodic);
greenroshks 0:8b0d43fe6c05 280 t_sc_timer.start(10000);
greenroshks 0:8b0d43fe6c05 281 printf("\n%f\n",t1.read());
greenroshks 0:8b0d43fe6c05 282
greenroshks 0:8b0d43fe6c05 283 while(1)
raizel_varun 7:f06840d848e3 284 {
greenroshks 0:8b0d43fe6c05 285 Thread::wait(10000);
greenroshks 0:8b0d43fe6c05 286 ;
greenroshks 0:8b0d43fe6c05 287 }
greenroshks 0:8b0d43fe6c05 288
greenroshks 0:8b0d43fe6c05 289 }