/************************************************************************* $Archive: /PACS/OnBoard/DetSim.c $ $Revision: 1.11 $ $Date: 2009/04/23 13:51:12 $ $Author: amazy $ $Log: DetSim.c,v $ Revision 1.11 2009/04/23 13:51:12 amazy 6.029 * * 31 3/14/06 4:31p Pacs Egse * Version 6.001 * Cleaned and commented *************************************************************************/ #ifdef SIMULATOR #include "virtuosoSim.h" #else // SIMULATOR #include "v_macro.h" #include "node1.h" #include "k_struct.h" #include "smcs\rawdef.h" #endif #include "error.h" #include "det.h" #include "links.h" #include "u_res.h" #include "commands.h" #include "params.h" //**************************************************************** // LOCAL TYPES DEFINITION //**************************************************************** /* STRUCT : DetectorSimulatorCommandParam ************************************** AUTHOR : AMazy USE : Used to pass 2 parameters to the trigger command "Start Detector Simulator" NOTE : the memory is not organized the same way for the simulator (PC) and for the DSP due to little and big-endian issues */ #ifdef SIMULATOR typedef struct { int Period : 28; int Detector : 4; } DetectorSimulatorCommandParam; #else //SIMULATOR typedef struct { int Detector : 4; int Period : 28; } DetectorSimulatorCommandParam; #endif //SIMULATOR //**************************************************************** // LOCAL PARAMETERS //**************************************************************** static K_TIMER* pDetectorSimulatorTimer = NULL; #ifdef SIMULATOR //Defines the semaphores to be signaled to simulate the reception of a readout static int aSemaphores[3] = { 7, //BlueSpec 8, //RedSpec 9, //Bol }; #endif //SIMULATOR //**************************************************************** // MACRO //**************************************************************** //macro to signal an error in Detector Simulator Task #define SetError(newError) \ {\ SetGlobalError(newError);\ LOCK_WRITE_RES_PARAMS;\ SET_BITS(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_ERROR_STATUS | K_BMASK_TASK_STATUS_ERROR_CODE, K_BMASK_TASK_STATUS_TASK_IN_ERROR + newError); \ UNLOCK_WRITE_RES_PARAMS;\ }\ //**************************************************************** // FUNCTION DECLARATION //**************************************************************** void InitializeDetSim(); void DetSim(); // command functions BOOL CommandStartDetectorSimulator(CommandParameter param); BOOL CommandStopDetectorSimulator(CommandParameter paramNotUsed); //**************************************************************** // FUNCTION IMPLEMENTATION //**************************************************************** /* FUNCTION : void InitializeDetSim() ********************************** AUTHOR : AMazy USE : Initialize Detector Simulator task. */ void InitializeDetSim() { LOCK_WRITE_RES_PARAMS; // initialize task status gpDetectorSimulator->TaskStatus = K_BMASK_TASK_STATUS_ALIVE | K_BMASK_TASK_STATUS_NO_ERROR_IN_TASK; gpDetectorSimulator->Period = 0; UNLOCK_WRITE_RES_PARAMS; pDetectorSimulatorTimer = KS_LowTimerGet(); if (pDetectorSimulatorTimer == NULL) { SetError(ERR_DET_SIMULATOR_COULD_NOT_CREATE_TIMER); } }; /* TASK_FUNCTION : DetSim() ************************ AUTHOR : AMazy This function implements the detector simulator task */ void DetSim() { int i=0; InitializeDetSim(); while (1) { // wait for the end of the period KS_SemaTestW(SEMA_TIMER_DETECTOR_SIMULATOR); #ifdef SIMULATOR if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BLUE_DEC_SIM_ACTIVE)) { KS_SemaSignal(aSemaphores[0]); } if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_RED_DEC_SIM_ACTIVE)) { KS_SemaSignal(aSemaphores[1]); } if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BOL_SIM_ACTIVE)) { KS_SemaSignal(aSemaphores[2]); for (i = 0; i < 4; ++i) //for BOLC, we need to simulate the reception of 5 packet for each readout { KS_TaskSleep(gpDetectorSimulator->Period/5); KS_SemaSignal(aSemaphores[2]); } } #else// SIMULATOR // signal an event to simulate the reception of the readout // (task waiting on the link spacewire is freed). if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BLUE_DEC_SIM_ACTIVE)) { KS_EventSignal(gaEventsSmcsReceiveCompleted[CHANNEL_BLUE_DEC_ID]); } if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_RED_DEC_SIM_ACTIVE)) { KS_EventSignal(gaEventsSmcsReceiveCompleted[CHANNEL_RED_DEC_ID]); } if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BOL_SIM_ACTIVE)) { KS_EventSignal(gaEventsSmcsReceiveCompleted[CHANNEL_BOL_ID]); for (i = 0; i < 4; ++i) //for BOLC, we need to simulate the reception of 5 packet for each readout { KS_TaskSleep(gpDetectorSimulator->Period/5); KS_EventSignal(gaEventsSmcsReceiveCompleted[CHANNEL_BOL_ID]); } } #endif //SIMULATOR }; } /* CMD_FUNCTION : BOOL CommandStartDetectorSimulator(CommandParameter param) ************************************************************************* AUTHOR : Amazy USE : Start the Detector Simulator. PARAMS : the 4 MSBits contains the id of the detector the 28 LSBits contains the period (in ms) */ BOOL CommandStartDetectorSimulator(CommandParameter param) { DetectorSimulatorCommandParam par; int option = 0; par = *((DetectorSimulatorCommandParam*)¶m); // if a simulation is already active, generate an error. if (gpDetectorSimulator->TaskStatus & K_BMASK_TASK_STATUS_DET_SIM_SIMULATED_DETECTOR) { SequencerSendNack(0, COULD_NOT_EXECUTE_COMMAND); SetSequencerError(ERR_SEQUENCER_COULD_NOT_EXECUTE_COMMAND); return FALSE; } // check period if (par.Period < 4) // period too short to simulate, use real timings { SET_BITS(option, K_BMASK_SIMULATION, K_BMASK_REAL_TIMING + K_BMASK_SIMULATED_READOUTS); } else { SET_BITS(option, K_BMASK_SIMULATION, K_BMASK_SIMULATED_TIMING + K_BMASK_SIMULATED_READOUTS); } //check the selected detector if (TEST_ONE_BIT(par.Detector, BLUE_SPEC)) { LOCK_WRITE_RES_PARAMS; SET_BITS(gpBlueDecRec->TaskStatus, K_BMASK_SIMULATION, option); SET_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BLUE_DEC_SIM_ACTIVE); UNLOCK_WRITE_RES_PARAMS; } if (TEST_ONE_BIT(par.Detector, RED_SPEC)) { LOCK_WRITE_RES_PARAMS; SET_BITS(gpRedDecRec->TaskStatus, K_BMASK_SIMULATION, option); SET_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_RED_DEC_SIM_ACTIVE); UNLOCK_WRITE_RES_PARAMS; } if (TEST_ONE_BIT(par.Detector, BOL)) { if (par.Period < 25) // the detector simulator si not able to work faster than 40Hz for BOLC { SequencerSendNack(param.Int, INVALID_PARAMETER); SetSequencerError(ERR_SEQUENCER_INVALID_PARAMETERS); return FALSE; } LOCK_WRITE_RES_PARAMS; SET_BITS(gpBolRec->TaskStatus, K_BMASK_SIMULATION, option); SET_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_BOL_SIM_ACTIVE); UNLOCK_WRITE_RES_PARAMS; } if ((par.Detector & (BLUE_SPEC | RED_SPEC | BOL)) == 0) { SequencerSendNack(param.Int, INVALID_PARAMETER); SetSequencerError(ERR_SEQUENCER_INVALID_PARAMETERS); return FALSE; } gpDetectorSimulator->Period = par.Period; //create the timer for triggering the simulator if (par.Period >= 4) { // start the timer SET_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_SIMULATING_TIMING); KS_LowTimerStart(pDetectorSimulatorTimer, par.Period, par.Period, SEMA_TIMER_DETECTOR_SIMULATOR); } SequencerSendAck(DMC_START_DET_SIMULATOR); return FALSE; }; /* CMD_FUNCTION : BOOL CommandStopDetectorSimulator(CommandParameter paramNotUsed) ******************************************************************************* AUTHOR : Amazy USE : Stop the Detector Simulator. */ BOOL CommandStopDetectorSimulator(CommandParameter paramNotUsed) { // stops the old timer if there were one. if (TEST_ONE_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_SIMULATING_TIMING)) { KS_LowTimerStop(pDetectorSimulatorTimer); } LOCK_WRITE_RES_PARAMS; // reset all the options of all the tasks related to the Dectector SET_BITS(gpBlueDecRec->TaskStatus, K_BMASK_SIMULATION, K_BMASK_REAL_TIMING + K_BMASK_REAL_READOUTS); SET_BITS(gpRedDecRec->TaskStatus, K_BMASK_SIMULATION, K_BMASK_REAL_TIMING + K_BMASK_REAL_READOUTS); SET_BITS(gpBolRec->TaskStatus, K_BMASK_SIMULATION, K_BMASK_REAL_TIMING + K_BMASK_REAL_READOUTS); CLEAR_BIT(gpDetectorSimulator->TaskStatus, K_BMASK_TASK_STATUS_DET_SIM_SIMULATED_DETECTOR | K_BMASK_TASK_STATUS_DET_SIM_SIMULATING_TIMING); gpDetectorSimulator->Period = 0; UNLOCK_WRITE_RES_PARAMS; SequencerSendAck(DMC_STOP_DET_SIMULATOR); return FALSE; };