CDMS integrated with pyld b4 checking
Dependencies: SDFileSystem mbed-rtos mbed-src mbed
Fork of cdms_i2csdrtc by
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #define PYLD_ADD 0x48 00004 #define BAE_ADD 0x20 00005 /************************Include the required Header files for the code *******************************/ 00006 //#include "mbed.h" 00007 #include "string.h" 00008 00009 #include "rtos.h" 00010 #include "pin_config.h" 00011 //const int addr = 0x20; //slave address 00012 I2C master (PIN72,PIN71); //configure pins p27,p28 as I2C master 00013 Serial pc (USBTX,USBRX); 00014 DigitalOut interrupt(PIN4); 00015 InterruptIn data_ready(PIN39); 00016 InterruptIn data_ready_pyld(PIN40); //to be changed for i2c comm. between pyld and bae 00017 void store_data(char*); 00018 00019 int reset; 00020 Timer t; 00021 Timer t1; 00022 Timer t2; 00023 Timer t3; 00024 00025 typedef struct 00026 { 00027 char data; // To avoid dynamic memory allocation 00028 int length; 00029 }i2c_data; 00030 00031 //Mail<i2c_data,16> i2c_data_receive; 00032 Mail<i2c_data,16> i2c_data_send; 00033 00034 Thread * ptr_t_i2c; 00035 int switchflag = -1; // to switch between BAE and PYLD 00036 void FUNC_I2C_MASTER_FSLAVE(char * data,int length) 00037 { 00038 00039 00040 bool ack0 =true; 00041 00042 interrupt = 1; 00043 t1.start(); 00044 //wait_ms(20); 00045 if(switchflag == 0) 00046 ack0 = master.read(BAE_ADD|1,data,length); 00047 else if (switchflag==1) 00048 ack0 = master.read(PYLD_ADD|1,data,length); 00049 t1.stop(); 00050 store_data(data); 00051 00052 if(!ack0) 00053 { 00054 printf("\n master has read %s from slave\n\r",data); 00055 00056 } 00057 //master.stop(); 00058 printf("\n%d\n\r",t1.read_us()); 00059 t1.reset(); 00060 00061 interrupt=0; 00062 switchflag = -1; 00063 } 00064 00065 typedef struct { 00066 char Voltage[9]; 00067 char Current[5]; 00068 char Temperature[2]; 00069 char PanelTemperature[3];//read by the 4 thermistors on solar panels 00070 char BatteryTemperature; //to be populated 00071 char faultpoll; //polled faults 00072 char faultir; //interrupted faults 00073 char power_mode; //power modes 00074 char AngularSpeed[3]; 00075 char Bnewvalue[3]; 00076 00077 //float magnetometer,gyro=>to be addes 00078 } hk_data; 00079 hk_data decode_data; 00080 00081 /*void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae 00082 { 00083 for(int i=0;i<=7;i++) 00084 { 00085 decode_data.Voltage[i] = data_hk[i]; 00086 decode_data.Voltage[8] = '\0'; 00087 } 00088 for(int i=0;i<=3;i++) 00089 { 00090 decode_data.Current[i] = data_hk[8+i]; 00091 decode_data.Current[4] = '\0'; 00092 } 00093 decode_data.Temperature[0] = data_hk[12]; 00094 decode_data.Temperature[1] = '\0'; 00095 for(int i=0;i<=1;i++) 00096 { 00097 decode_data.PanelTemperature[i] = data_hk[13+i]; 00098 decode_data.PanelTemperature[2] = '\0'; 00099 } 00100 decode_data.BatteryTemperature = data_hk[15]; 00101 decode_data.faultpoll = data_hk[16]; 00102 decode_data.faultir = data_hk[17]; 00103 decode_data.power_mode = data_hk[18]; 00104 for(int i=0;i<=1;i++) 00105 { 00106 decode_data.AngularSpeed[i] = data_hk[19+i]; 00107 decode_data.AngularSpeed[2] = '\0'; 00108 } 00109 for(int i=0;i<=1;i++) 00110 { 00111 decode_data.Bnewvalue[i] = data_hk[21+i]; 00112 decode_data.Bnewvalue[2] = '\0'; 00113 } 00114 printf("\n voltage %s\n\r",decode_data.Voltage); 00115 printf("\n current %s\n\r",decode_data.Current); 00116 printf("\n faultpoll %c\n\r",decode_data.faultpoll); 00117 }*/ 00118 00119 00120 void T_I2C_MASTER_FSLAVE(void const *args) 00121 { 00122 char data_receive[25]; 00123 while(1) 00124 { 00125 Thread::signal_wait(0x1); 00126 00127 00128 FUNC_I2C_MASTER_FSLAVE(data_receive,25); 00129 /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); 00130 strcpy(i2c_data_r->data , data_receive); 00131 i2c_data_r->length = 25; 00132 i2c_data_receive.put(i2c_data_r);*/ 00133 printf("\n Data received from slave is %s\n\r",data_receive); 00134 //TC_DECODE(data_receive); 00135 } 00136 } 00137 00138 void FUNC_INT_BAE() 00139 { 00140 00141 switchflag = 0; 00142 ptr_t_i2c->signal_set(0x1); 00143 00144 } 00145 00146 void FUNC_INT_PYLD() 00147 { 00148 00149 switchflag = 1; 00150 ptr_t_i2c->signal_set(0x1); 00151 00152 } 00153 00154 char writedata; 00155 bool write2slave; 00156 bool master_status_write; 00157 void FUNC_MASTER_WRITE() 00158 { //wait(1); 00159 write2slave=true; 00160 00161 00162 00163 char data = pc.getc(); 00164 interrupt = 1; 00165 t.start(); 00166 t3.start(); 00167 wait_ms(20); 00168 i2c_data * i2c_data_s = i2c_data_send.alloc(); 00169 i2c_data_s->data = data; 00170 i2c_data_s->length = 1; 00171 i2c_data_send.put(i2c_data_s); 00172 master_status_write = true; 00173 00174 // interrupt = 1; 00175 00176 osEvent evt = i2c_data_send.get(); 00177 if (evt.status == osEventMail) 00178 { 00179 i2c_data *i2c_data_s = (i2c_data*)evt.value.p; 00180 writedata = i2c_data_s -> data; 00181 t.stop(); 00182 //t3.start(); 00183 if(data%2) 00184 master_status_write = (bool) master.write(BAE_ADD|0x00,&writedata,1); 00185 else 00186 master_status_write = (bool) master.write(PYLD_ADD|0x00,&writedata,1); 00187 t3.stop(); 00188 if(master_status_write==0) 00189 { 00190 printf("master has written %c to slave\n\r",writedata); 00191 write2slave=false; 00192 } 00193 i2c_data_send.free(i2c_data_s); 00194 printf("\n%d\n",t.read_us()); 00195 t.reset(); 00196 printf("\n%d\n",t3.read_us()); 00197 t3.reset(); 00198 } 00199 interrupt = 0; 00200 } 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 /********************************** Function Prototypes declaration ***********************************/ 00211 int FUNC_CDMS_hex2int(int); // Need to convert the RTC time values to integers 00212 void FUNC_CDMS_Gettime(void); // Function to get the time values from RTC registers 00213 void FUNC_CDMS_init_values(void); // Function to initialize the registers in the RTC 00214 00215 00216 /***************** Configure the SPI1 of the CDMS uc as the data bus for the RTC***********************/ 00217 SPI spi(PTE1,PTE3, PTE2); // mosi, miso, sclk 00218 DigitalOut cs(PTE29); // PTE29 is used for chip select 00219 00220 char ch; 00221 SDFileSystem sd(PTE1, PTE3, PTE2, PTE22, "sd"); // the pinout on the mbed Cool Components workshop board 00222 00223 char time_stamp[15]; 00224 00225 /***********************Initialization function of the RTC********************************************/ 00226 void FUNC_CDMS_init_values(void) 00227 { 00228 cs=0; 00229 spi.format(8,3); // Set the data bit with to be of 8 bits, 00230 // data tx mode is 3 00231 spi.frequency(1000000); // Set Data rate to be 1 MHz 00232 00233 cs = 0; // Set chip select pin to be 0: Activate RTC chip 00234 spi.write(0x80); // Send the address of the Seconds register 0x80 00235 spi.write(0x00); // Set seconds value to 0 00236 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00237 00238 cs=0; // Set chip select pin to be 0: Activate RTC chip 00239 spi.write(0x81); // Send the address of the Minutes register 0x81 00240 spi.write(0x00); // Set minutes value to 0 00241 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00242 00243 cs=0; // Set chip select pin to be 0: Activate RTC chip 00244 spi.write(0x82); // Send the address of the Hours register 0x82 00245 spi.write(0x00); // Set hours value to 0 00246 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00247 00248 cs=0; // Set chip select pin to be 0: Activate RTC chip 00249 spi.write(0x83); // Send the address of the Day register 0x83 00250 spi.write(0x01); // Set the day to 01 00251 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00252 00253 cs=0; // Set chip select pin to be 0: Activate RTC chip 00254 spi.write(0x84); // Send the address of the date register 0x84 00255 spi.write(0x01); // Set date of the month to 01 00256 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00257 00258 cs=0; // Set chip select pin to be 0: Activate RTC chip 00259 spi.write(0x85); // Send the address of the Month register 0x80 00260 spi.write(0x01); // Set month to 01 00261 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00262 00263 cs=0; // Set chip select pin to be 0: Activate RTC chip 00264 spi.write(0x86); // Send the address of the year register 0x80 00265 spi.write(0x00); // Set year to 00(2000) 00266 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00267 }// End of INIT Function 00268 00269 00270 /*********************************Function to read the RTC registers*********************************/ 00271 void FUNC_CDMS_Gettime() 00272 { 00273 00274 spi.format(8,3); // Set the data bit with to be of 8 bits, 00275 // data tx mode is 3 00276 spi.frequency(1000000); // Set Data rate to be 1 MHz 00277 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00278 00279 cs=0; // Set chip select pin to be 0: Activate RTC chip 00280 spi.write(0x00); // Sending address of seconds register 00281 int seconds = spi.write(0x00); // Read the value by sending dummy byte 00282 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00283 00284 cs=0; // Set chip select pin to be 0: Activate RTC chip 00285 spi.write(0x01); // Sending address of Minutes register 00286 int minutes =spi.write(0x01); // Read the value by sending dummy byte 00287 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00288 00289 cs=0; // Set chip select pin to be 0: Activate RTC chip 00290 spi.write(0x02); // Sending address of hours register 00291 int hours =spi.write(0x01); // Read the value by sending dummy byte 00292 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00293 00294 cs=0; // Set chip select pin to be 0: Activate RTC chip 00295 spi.write(0x03); // Sending address of day register 00296 int day =spi.write(0x01); // Read the value by sending dummy byte 00297 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00298 00299 cs=0; // Set chip select pin to be 0: Activate RTC chip 00300 spi.write(0x04); // Sending address of date register 00301 int date =spi.write(0x01); // Read the value by sending dummy byte 00302 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00303 00304 cs=0; // Set chip select pin to be 0: Activate RTC chip 00305 spi.write(0x05); // Sending address of month register 00306 int month =spi.write(0x01); // Read the value by sending dummy byte 00307 cs=1; // Set chip select pin to be 1: DeActivate RTC chip 00308 00309 cs=0; // Set chip select pin to be 0: Activate RTC chip 00310 spi.write(0x06); // Sending address of year register 00311 int year =spi.write(0x01); // Read the value by sending dummy byte 00312 cs = 1; // Set chip select pin to be 1: DeActivate RTC chip 00313 00314 // RTC sends in BCD format.. SO we convert the values generated by RTC to integers 00315 year = FUNC_CDMS_hex2int(year); 00316 month = FUNC_CDMS_hex2int(month); 00317 date = FUNC_CDMS_hex2int(date); 00318 day = FUNC_CDMS_hex2int(day); 00319 hours = FUNC_CDMS_hex2int(hours); 00320 minutes = FUNC_CDMS_hex2int(minutes); 00321 seconds = FUNC_CDMS_hex2int(seconds); 00322 00323 // Print the obtained Time stamp 00324 //printf("The time is :%d %d %d %d %d %d %d \n\r",seconds,minutes,hours,day,date,month,year); 00325 sprintf(time_stamp,"%02d%02d%02d%02d%02d%02d",year,month,date,hours,minutes,seconds); 00326 printf(" \n\r Received HK data from BAE"); 00327 printf("\n HK_data stored in %s.txt",time_stamp); 00328 //puts(time_stamp); 00329 //printf(".txt"); 00330 }//End of Read Function 00331 00332 /**************************Function to convert Hex values to Int values*****************************/ 00333 int FUNC_CDMS_hex2int(int a) 00334 { 00335 a=(a/16)*10+(a%16); //function to convert hex type to int type 00336 return a; 00337 }// End of convert function 00338 00339 00340 /******************************************END OF RTC CODE ****************************************/ 00341 00342 //char count = 10; 00343 void store_data(char *hkdata) 00344 { 00345 FUNC_CDMS_Gettime(); 00346 00347 //char hk_data[25]; 00348 // count++; 00349 //strcpy(hk_data,"hk_Data "); 00350 //strcat(hk_data,"!@@#"); 00351 // hk_data[10] = count; 00352 //storedata(hk_data); 00353 mkdir("/sd/hk", 0777); 00354 char add[20]; 00355 strcpy(add,"/sd/hk/"); 00356 strcat(add,time_stamp); 00357 strcat(add,".txt"); 00358 00359 FILE *fp = fopen(add, "w"); 00360 if(fp == NULL) { 00361 error("Could not open file for write\n"); 00362 } 00363 else 00364 { 00365 00366 fprintf(fp, "%s ",hkdata); 00367 fclose(fp); 00368 printf("\n File stored in SD card\n"); 00369 //printf("\n Reading from the file .... \n Data is %s\n",hk_data); 00370 00371 } 00372 } 00373 00374 00375 00376 //void create_file() 00377 //{ 00378 00379 /* 00380 fprintf(fp, "Hello fun SD Card World!\n"); 00381 fclose(fp); 00382 00383 fp = fopen("/sd/mydir/sdtest.txt", "r"); 00384 if(fp == NULL) { 00385 error("Could not open file for write\n"); 00386 } 00387 //fprintf(fp, "Hello fun SD Card World!"); 00388 // fprintf("The contents of %s file are :\n", "/sd/mydir/sdtest.txt"); 00389 while( ( ch = fgetc(fp) ) != '\n' ) 00390 { 00391 pc.printf("%c",ch); 00392 } 00393 //fprintf(fp); 00394 fclose(fp); 00395 00396 printf("Jai Mata Di..Goodbye World!\n");*/ 00397 //} 00398 00399 Ticker tx; 00400 00401 int main() 00402 { 00403 interrupt=0; 00404 ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); 00405 master.frequency(100000); 00406 printf("IITMSAT - CDMS\n"); 00407 data_ready.rise(&FUNC_INT_BAE); 00408 data_ready_pyld.rise(&FUNC_INT_PYLD); 00409 FUNC_CDMS_init_values(); 00410 //tx.attach(&store_data, 20); 00411 while(1) 00412 { 00413 FUNC_MASTER_WRITE(); 00414 } 00415 00416 }
Generated on Fri Jul 22 2022 22:02:58 by 1.7.2