To fix the hang problem
Dependencies: FreescaleIAP SimpleDMA mbed-rtos mbed
Fork of CDMS_CODE by
CDMS_HK.h
- Committer:
- chaithanyarss
- Date:
- 2016-07-21
- Revision:
- 269:24c50959b0a4
- Parent:
- 268:ded5306a1fd1
- Child:
- 270:9dacc1d8459a
File content as of revision 269:24c50959b0a4:
void FCTN_CDMS_HK_MAIN(); void FCTN_CDMS_HK(); void VERIFY_COMRX(); void VERIFY_RTC(); void CDMS_HK_SD(); void HANDLE_HW_FAULTS(); void HANDLE_HW_FAULT_SD(); void HANDLE_HW_FAULT_BAE(); void HANDLE_HW_FAULT_PL(); void FUNC_CDMS_GPIO_STATUS(); void minMaxHkData(); void COLLECT_CDMS_RAM(); extern uint8_t beacon_array[134]; AnalogIn TempInput(PIN27); // Input from Current Multiplexer AnalogIn CDMS_temp_sensor(PIN53); AnalogIn COMRX_RSSI_volatge(PIN70); DigitalOut SelectLinec3 (PIN79); // MSB of Select Lines DigitalOut SelectLinec2 (PIN78); DigitalOut SelectLinec1 (PIN77); DigitalOut SelectLinec0 (PIN76); // LSB of Select Lines Convolution CDMS_HEALTH; Convolution BAE_HEALTH; unsigned char CDMS_HK_FRAME[134] = {0}; char BAE_HK[134] = {0}; uint8_t convoluted_CDMS_HK[270]; uint8_t interleave_CDMS_HK[288]; uint8_t CDMS_HEALTH_FINAL[512] = {0}; uint8_t convoluted_BAE_HK[270]; uint8_t interleave_BAE_HK[288]; uint8_t BAE_HEALTH_FINAL[512] = {0}; unsigned char BAE_HK_FRAME[134] = {0}; void FCTN_CDMS_HK_MAIN(void const *args) { uint8_t sd_stat = 0; while(1) { gHK_THREAD->signal_wait(HK_SIGNAL); gPC.printf("\n\nEntering HK thread\n"); gMutex.lock(); CDMS_HK_MAIN_STATUS = 0x01; CDMS_HK_MAIN_COUNTER++; FCTN_CDMS_HK(); RSSI_volatge = COMRX_RSSI_volatge.read() * 3.3; VERIFY_COMRX(); VERIFY_RTC(); HANDLE_HW_FAULTS(); FUNC_CDMS_GPIO_STATUS(); //yet to be done uint8_t CDMS_quant[20]; CDMS_quant[1]= (uint8_t)quant_data.CDMS_temp_quant; CDMS_quant[2]= (uint8_t)RSSI_volatge; for(int i=0; i<16; i++) { CDMS_quant[i+4]= (uint8_t)quant_data.temp_quant[i]; } minMaxHkData(); CDMS_HEALTH_DATA[1] = GPIO_STATUS; //Reading GPIO Pins CDMS_HEALTH_DATA[0] = GPIO_STATUS >> 8; COLLECT_CDMS_RAM(); for(int i = 0;i<84;i++) CDMS_HEALTH_DATA[2+i] = CDMS_RAM[i]; //Reading RAM parameters for(int i = 0;i<20;i++) //Collecting Data from Temp sensors CDMS_HEALTH_DATA[86+i] = CDMS_quant[i]; // Here: Have to FIT flash data. uint64_t time = FCTN_CDMS_RD_RTC() >> 7; //Reading Time from RTC for(int i = 124; i<128; i++) CDMS_HEALTH_DATA[i] = time >> i*8; gPC.printf("0x%x\n",time); FCTN_SD_MNGR(); //Adding FSC & TMID to TM frame CDMS_HK_FRAME[0] = 0x20; CDMS_HK_FRAME[1] = FSC_CURRENT[4]+1; CDMS_HK_FRAME[2] = (FSC_CURRENT[4]+1) >> 8; CDMS_HK_FRAME[3] = (FSC_CURRENT[4]+1) >> 16; for(int i = 0; i<128; i++) /*Adding actual CDMS Health data to TM frame*/ CDMS_HK_FRAME[4+i] = CDMS_HEALTH_DATA[i]; uint16_t crc = crc16_gen(CDMS_HK_FRAME,132); /*Adding CRC to TM frame*/ CDMS_HK_FRAME[133] = crc; CDMS_HK_FRAME[132] = crc >> 8; exor(CDMS_HK_FRAME); CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME , convoluted_CDMS_HK); CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME + 67, convoluted_CDMS_HK + 135); interleave(convoluted_CDMS_HK , interleave_CDMS_HK); interleave(convoluted_CDMS_HK +135, interleave_CDMS_HK + 144); for(int i=0; i<288; i++) CDMS_HEALTH_FINAL[i] = interleave_CDMS_HK[i]; sd_stat = SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4); if(sd_stat) { gPC.puts("sd write failure"); break; } gPC.printf("Completed CDMS HK\t"); /*---------------------------------- BAE HK --------------------------------------------*/ BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134); gPC.printf("Entering BAE HK\t"); if(BAE_HK_I2C == 0) { crc = crc16_gen((unsigned char *)BAE_HK,132); if(crc == ((uint16_t)BAE_HK[132] << 8) | (uint16_t)BAE_HK[133]){ TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7; /*for(int i = 0; i<15; i++) gPC.printf("\r 0x%02X\n",BAE_HK[i]);*/ for(int i = 0; i<4; i++) BAE_HK[i] = time >> i; BAE_HK_FRAME[0] = 0x28; BAE_HK_FRAME[1] = FSC_CURRENT[5]+1; BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8; BAE_HK_FRAME[3] = (FSC_CURRENT[5]+1) >> 16; for(int i = 0; i<128; i++) /*Adding actual CDMS Health data to TM frame*/ BAE_HK_FRAME[4+i] = BAE_HK[i]; crc = crc16_gen(BAE_HK_FRAME,132); /*Adding CRC to TM frame*/ BAE_HK_FRAME[133] = crc; BAE_HK_FRAME[132] = crc >> 8; exor(BAE_HK_FRAME); BAE_HEALTH.convolutionEncode(BAE_HK_FRAME , convoluted_BAE_HK); BAE_HEALTH.convolutionEncode(BAE_HK_FRAME + 67, convoluted_BAE_HK + 135); interleave(convoluted_BAE_HK , interleave_BAE_HK); interleave(convoluted_BAE_HK +135, interleave_BAE_HK + 144); for(int i=0; i<288; i++) BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i]; sd_stat = SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5); if(sd_stat) { gPC.puts("sd write failure"); break; } } } else { gPC.printf("BAE HK data not recieved through I2C\t"); for(int i = 0; i<134; i++) BAE_HK[i] = 0; } gPC.printf("Completed BAE HK\n"); /*----------------------------------Beacon message--------------------------------------*/ // Add HK bits beacon_array[0] = 0x00; beacon_array[1] = time >> 32; beacon_array[2] = time >> 24; beacon_array[3] = time >> 16; beacon_array[4] = time >> 8; beacon_array[5] = time; beacon_array[6] = SD_FAULTCOUNT >> 8; beacon_array[7] = SD_FAULTCOUNT; beacon_array[8] = RTC_FAULTCOUNT >> 8; beacon_array[9] = RTC_FAULTCOUNT; beacon_array[10] = (((SD_STATUS == DEVICE_DISABLED || SD_STATUS == DEVICE_OC_FAULT)?1:0)<<7)|(RTC_STATUS <<6)|(COM_RX_STATUS<<3)|(V_C_PGOOD<<2)|(COMRX_OC_FAULT<<1)|(COMTX_OC_FAULT); beacon_array[11] = (EPS_V_C_EN_STATUS <<7)|(EPS_V_D_EN_STATUS); beacon_array[12] = CDMS_HK_MAIN_COUNTER >>8; beacon_array[13] = CDMS_HK_MAIN_COUNTER; beacon_array[14] = PL_MAIN_COUNTER >>8; beacon_array[15] = PL_MAIN_COUNTER; beacon_array[16] = PL_RCV_SC_DATA_COUNTER >>8; beacon_array[17] = PL_RCV_SC_DATA_COUNTER; beacon_array[18] = TIME_LATEST_SPI_SPEED >>24; beacon_array[19] = TIME_LATEST_SPI_SPEED >>16; beacon_array[20] = TIME_LATEST_SPI_SPEED >>8; beacon_array[21] = TIME_LATEST_SPI_SPEED; beacon_array[22] = (uint8_t)RSSI_volatge; // Add SC bits crc = crc16_gen(beacon_array,132); beacon_array[132] = crc; beacon_array[133] = crc >> 8; bool y; y = FCTN_I2C_WRITE((char *)beacon_array,134); if(y == 0) gPC.printf("long Bcn sent\n\r"); else gPC.printf("long Bcn not sent\r\n"); gPC.printf("\rCompleted Beacon\n"); gMutex.unlock(); } } int quantiz(float start,float step,float x) { int y=(x-start)/step; if(y<=0)y=0; if(y>=255)y=255; return y; } char saveMin(char x,char y) { return (y<x)?y:x; } char saveMax(char x,char y) { return (y>x)?y:x; } void minMaxHkData() { if(firstCount==true) { for (int i = 0; i < 16; ++i) { min_max_data.temp_min[i] = quant_data.temp_quant[i]; min_max_data.temp_max[i] = quant_data.temp_quant[i]; } min_max_data.CDMS_temp_min=quant_data.CDMS_temp_quant; min_max_data.CDMS_temp_max=quant_data.CDMS_temp_quant; } else { for (int i = 0; i < 16; ++i) { min_max_data.temp_min[i] = saveMin(min_max_data.temp_min[i],quant_data.temp_quant[i]); min_max_data.temp_max[i] = saveMax(min_max_data.temp_max[i],quant_data.temp_quant[i]); } min_max_data.CDMS_temp_min = saveMin(min_max_data.CDMS_temp_min,quant_data.CDMS_temp_quant); min_max_data.CDMS_temp_max = saveMax(min_max_data.CDMS_temp_max,quant_data.CDMS_temp_quant); } firstCount=false; } void FCTN_CDMS_HK() { int Iteration=0; SelectLinec0=0; SelectLinec1=0; SelectLinec2=0; SelectLinec3=0; for(Iteration=0; Iteration<16; Iteration++) { actual_data.temp_actual[Iteration]=TempInput.read(); SelectLinec0=!(SelectLinec0); if(Iteration%2==1) SelectLinec1=!(SelectLinec1); if(Iteration%4==3) SelectLinec2=!(SelectLinec2); if(Iteration%8==7) SelectLinec3=!(SelectLinec3); } actual_data.CDMS_temp_actual=(-90.7*3.3*CDMS_temp_sensor.read())+190.1543; for(Iteration=0; Iteration<16; Iteration++) { if(Iteration<14) { actual_data.temp_actual[Iteration]=actual_data.temp_actual[Iteration]*3.3; int resistance; resistance=24000*actual_data.temp_actual[Iteration]/(3.3-actual_data.temp_actual[Iteration]); if(actual_data.temp_actual[Iteration]>1.47) { actual_data.temp_actual[Iteration]=3694/log(24.032242*resistance); } else { actual_data.temp_actual[Iteration]=3365.4/log(7.60573*resistance); } } else actual_data.temp_actual[Iteration]=(-90.7*3.3*actual_data.temp_actual[Iteration])+190.1543; } for(Iteration=0; Iteration<16; Iteration++) { if(Iteration<14) { quant_data.temp_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.temp_actual[Iteration]); } else quant_data.temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.temp_actual[Iteration]); } quant_data.CDMS_temp_quant=quantiz(tstart,tstep,actual_data.CDMS_temp_actual); minMaxHkData(); } void FUNC_CDMS_GPIO_STATUS() //Polls the status of Input GPIO PINS { //V_A_PGOOD //TRZ EN GPIO_STATUS=(V_A_PGOOD)?(GPIO_STATUS)||((uint16_t)(0x1<<15)):(GPIO_STATUS)&(~((uint16_t)(0x1<<15))); //V_B_PGOOD_1 //3V3BPGOOD //$ GPIO_STATUS=(V_B_PGOOD_1)?(GPIO_STATUS)||((uint16_t)(0x1<<14)):(GPIO_STATUS)&(~((uint16_t)(0x1<<14))); //V_B_PGOOD_2 //3V3BEN //$ GPIO_STATUS=(V_B_PGOOD_2)?(GPIO_STATUS)||((uint16_t)(0x1<<13)):(GPIO_STATUS)&(~((uint16_t)(0x1<<13))); //V_C_PGOOD //3V3CPGOOD //$ GPIO_STATUS=(V_C_PGOOD)?(GPIO_STATUS)||((uint16_t)(0x1<<12)):(GPIO_STATUS)&(~((uint16_t)(0x1<<12))); //COMRX_OC_FAULT //$ GPIO_STATUS=(COMRX_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<11)):(GPIO_STATUS)&(~((uint16_t)(0x1<<11))); // COMTX_OC_FAULT //$ GPIO_STATUS=(COMTX_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<10)):(GPIO_STATUS)&(~((uint16_t)(0x1<<10))); //BAE_OC_FAULT //$ GPIO_STATUS=(BAE_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<9)):(GPIO_STATUS)&(~((uint16_t)(0x1<<9))); //PL_GPIO_1_STATUS //$ GPIO_STATUS=(PL_GPIO_1_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<8)):(GPIO_STATUS)&(~((uint16_t)(0x1<<8))); //PL_GPIO_2_STATUS //$ GPIO_STATUS=(PL_GPIO_2_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<7)):(GPIO_STATUS)&(~((uint16_t)(0x1<<7))); //PL_GPIO_3_STATUS //$ GPIO_STATUS=(PL_GPIO_3_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<6)):(GPIO_STATUS)&(~((uint16_t)(0x1<<6))); //PL_BEE_SW_OC_FAULT //to be verified GPIO_STATUS=(PL_BEE_SW_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<5)):(GPIO_STATUS)&(~((uint16_t)(0x1<<5))); //PL_EPS_LATCH_SW_OC_FAULT // to be verified GPIO_STATUS=(PL_EPS_LATCH_SW_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<4)):(GPIO_STATUS)&(~((uint16_t)(0x1<<4))); //EPS_V_C_EN_STATUS GPIO_STATUS=(EPS_V_C_EN_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<3)):(GPIO_STATUS)&(~((uint16_t)(0x1<<3))); //EPS_V_D_EN_STATUS GPIO_STATUS=(EPS_V_D_EN_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<2)):(GPIO_STATUS)&(~((uint16_t)(0x1<<2))); } void VERIFY_COMRX() { //COMRX_OC_FAULT //$ if(PIN68==0 && RSSI_volatge > 0.4) { COMRX_STATUS = COMRX_ALIVE; } else { RESET_COMRX(); COMRX_RESET_COUNTER++; if(PIN68==0 && RSSI_volatge > 0.4) COMRX_STATUS = COMRX_ALIVE; else COMRX_STATUS = COMRX_DEAD; } } void VERIFY_RTC() { SPI_mutex.lock(); gCS_RTC=1; gCS_RTC=0; spi.write(0x0F); if(spi.write(0x00) & 0x04 == 0x04) { RTC_STATUS = 0x01; RESET_RTC(); RTC_FAULTCOUNT++; } gCS_RTC=1; SPI_mutex.unlock(); } void HANDLE_HW_FAULTS() { HANDLE_HW_FAULT_SD(); HANDLE_HW_FAULT_BAE(); HANDLE_HW_FAULT_PL(); } void HANDLE_HW_FAULT_SD() { if(SD_STATUS != DEVICE_DISABLED) { if(SD_STATUS == DEVICE_OC_FAULT) SD_SW_EN_DS = 0; //powering on SD if(SD_OC_FAULT == 0) { SD_SW_EN_DS = 1; //switching off SD card SD_FAULTCOUNT++; SD_STATUS = (SD_FAULTCOUNT == 3) ? DEVICE_DISABLED :DEVICE_OC_FAULT; } else { SD_STATUS = DEVICE_POWERED; SD_FAULTCOUNT = 0; } } } void HANDLE_HW_FAULT_BAE() { if(BAE_STATUS != DEVICE_DISABLED) { if(BAE_STATUS == DEVICE_OC_FAULT) BAE_SW_EN_DS = 0; //Power ON BAE if(BAE_OC_FAULT == 0) { // If OC Fault BAE_SW_EN_DS = 1; //Switch OFF BAE BAE_FAULTCOUNT++; BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT; } else { BAE_STATUS = DEVICE_POWERED; BAE_FAULTCOUNT = 0; } } } void HANDLE_HW_FAULT_PL() { if(PL_STATUS != DEVICE_DISABLED) { if(PL_STATUS == DEVICE_OC_FAULT){ PYLD_DFF_CLK = 0; PYLD_DFF = 1; // Switching ON PL wait_us(1); PYLD_DFF_CLK = 1; wait_us(1); PYLD_DFF_CLK = 0; wait_us(1); } if(PL_BEE_SW_OC_FAULT == 0) { // if OC Fault PYLD_DFF_CLK = 0; PYLD_DFF = 0; //Switching OFF PL wait_us(1); PYLD_DFF_CLK = 1; wait_us(1); PYLD_DFF_CLK = 0; wait_us(1); PL_FAULTCOUNT++; PL_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT; } else { if(PL_STATUS == DEVICE_OC_FAULT){ PYLD_DFF_CLK = 0; PYLD_DFF = 0; //Switching OFF PL wait_us(1); PYLD_DFF_CLK = 1; wait_us(1); PYLD_DFF_CLK = 0; wait_us(1); } PL_STATUS = DEVICE_ENABLED; PL_FAULTCOUNT = 0; } } } void COLLECT_CDMS_RAM() { CDMS_RAM[0] = ((PL_INIT_STATUS<<7)&0x80)|((PL_MAIN_STATUS<<6)&0x40)|((PL_LOW_POWER<<5)&0x20)|((PL_STATE<<3)&0x18)|(PL_STATUS&0x07); CDMS_RAM[1] = ((PL_RCV_SC_DATA_STATUS<<7)&0x80)|((COM_SESSION<<6)&0x40)|((COM_RX<<5)&0x20)|((RF_SW_STATUS<<4)&0x10)|((COM_TX<<3)&0x08)|((COM_TX_STATUS<<2)&0x04)|((COM_MNG_TMTC<<1)&0x02)|(EN_CDMS_HK&0x01); CDMS_RAM[2] = ((EN_PL<<7)&0x80)|((EN_RCV_SC<<6)&0x40)|((CDMS_INIT_STATUS<<5)&0x20)|((CDMS_HK_MAIN_STATUS<<4)&0x10)|((CDMS_HK_STATUS<<2)&0x0C)|((COM_RX_STATUS<<1)&0x02)|(CDMS_RTC_BL&0x01); CDMS_RAM[3] = CDMS_I2C_ERR_SPEED_COUNTER >> 8; CDMS_RAM[4] = CDMS_I2C_ERR_SPEED_COUNTER; CDMS_RAM[5] = CDMS_I2C_ERR_BAE_COUNTER >> 8; CDMS_RAM[6] = CDMS_I2C_ERR_BAE_COUNTER; CDMS_RAM[7] = CDMS_HK_MAIN_COUNTER >> 8; CDMS_RAM[8] = CDMS_HK_MAIN_COUNTER; CDMS_RAM[9] = PL_MAIN_COUNTER >> 8; CDMS_RAM[10] = PL_MAIN_COUNTER; CDMS_RAM[11] = PL_RCV_SC_DATA_COUNTER >> 8; CDMS_RAM[12] = PL_RCV_SC_DATA_COUNTER; CDMS_RAM[13] = COMRX_RESET_COUNTER >> 8; CDMS_RAM[14] = COMRX_RESET_COUNTER; CDMS_RAM[15] = CDMS_WR_SD_FAULT_COUNTER >> 8; CDMS_RAM[16] = CDMS_WR_SD_FAULT_COUNTER; CDMS_RAM[17] = SD_LIB_WRITES >> 8; CDMS_RAM[18] = SD_LIB_WRITES; for(int i = 0; i<4; i++) CDMS_RAM[19+i] = TIME_LATEST_RTC >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[23+i] = TIME_LATEST_I2C_BAE >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[27+i] = TIME_LATEST_I2C_SPEED >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[31+i] = TIME_LATEST_SD_WR >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[35+i] = TIME_LATEST_SD_RD >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[39+i] = TIME_LATEST_SPI_SPEED >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[43+i] = FSC_CURRENT[1] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[47+i] = FSC_LAST[1] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[51+i] = FSC_CURRENT[2] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[55+i] = FSC_LAST[2] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[59+i] = FSC_CURRENT[3] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[63+i] = FSC_LAST[3] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[67+i] = FSC_CURRENT[4] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[71+i] = FSC_LAST[4] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[75+i] = FSC_CURRENT[5] >> i*8; for(int i = 0; i<4; i++) CDMS_RAM[79+i] = FSC_LAST[5] >> i*8; CDMS_RAM[83] = 0x00; }