\hypertarget{THS_8c}{
\section{THS.c File Reference}
\label{THS_8c}\index{THS.c@{THS.c}}
}
Time Hack Services for CPU.  


{\tt \#include $<$PBS/PBS.h$>$}\par
{\tt \#include $<$PBS/WCT.h$>$}\par
{\tt \#include $<$PBS/TASK.h$>$}\par
{\tt \#include $<$PBS/TMR.h$>$}\par
{\tt \#include $<$PBS/LSU.h$>$}\par
{\tt \#include $<$PBS/MBA.h$>$}\par
{\tt \#include $<$PBS/FPA.h$>$}\par
{\tt \#include $<$PBS/BSWP.ih$>$}\par
{\tt \#include $<$LCBD/LCBD.h$>$}\par
{\tt \#include $<$MSG/MSG\_\-pubdefs.h$>$}\par
{\tt \#include $<$THS/THS\_\-msgs.h$>$}\par
{\tt \#include $<$THS\_\-prvdefs.h$>$}\par
{\tt \#include $<$stdio.h$>$}\par
\subsection*{Functions}
\begin{CompactItemize}
\item 
unsigned int \hyperlink{THS_8c_0b19bbf6d1e49dea5b87257e11becc5e}{THS\_\-init} (int mask)
\begin{CompactList}\small\item\em Inits the TimeHackService module. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_ca69fee554e97202f9fc6458f44bf02b}{THS\_\-simulatedPPS} ()
\begin{CompactList}\small\item\em Return the state of the pulse-per-second simulation. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_166e8adeda51ce2487c895488f3a2450}{THS\_\-simulatedTT} ()
\begin{CompactList}\small\item\em Return the state of the timetone simulation. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_4b801a1f8fc468fd5f1f8fb2c3fb67ee}{THS\_\-wait} ()
\begin{CompactList}\small\item\em Wait for TimeHackService module to be fully operational. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_74d3678b7bf6f28e8871dc67b7e0fe52}{THS\_\-timetone} (unsigned int absTimeSec, unsigned short saFlags)
\begin{CompactList}\small\item\em Provide absTime for next entry in table (on next PPS). \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_6876c237f3e829e2a8a1a053d51a43a1}{THS\_\-getLastPpsAbsTimeSec} ()
\begin{CompactList}\small\item\em Get the absolute time value at last PPS. \item\end{CompactList}\item 
void \hyperlink{THS_8c_017318158fc77ad5c9a79a84c7341828}{THS\_\-stats} ()
\begin{CompactList}\small\item\em Print some THS statistics. \item\end{CompactList}\item 
void \hyperlink{THS_8c_2af9f421a5521535c03a81e0b22c772b}{THS\_\-setPpsWarnEvery} (unsigned int warnPpsEvery)
\begin{CompactList}\small\item\em Set how often get warning when no PPS. \item\end{CompactList}\item 
void \hyperlink{THS_8c_f8770d8bccc08c0cc4b31ff0f38ed775}{THS\_\-setTimetoneWarnEvery} (unsigned int warnTimetoneEvery)
\begin{CompactList}\small\item\em Set how often get warning when no timetone. \item\end{CompactList}\item 
void \hyperlink{THS_8c_54ae5e4a93554a58f9ade65b5d420cc6}{THS\_\-setGemUpdateMsgs} (int maxMsgs)
\begin{CompactList}\small\item\em set size of memory pool for gemUpdate msgs, must be called before THS\_\-init \item\end{CompactList}\item 
void \hyperlink{THS_8c_707fa3168ba32b79fd64b905db1d3edc}{THS\_\-setSlaveSend} (\hyperlink{THS_8h_08a270711b7a13fb61a9653dcf8fa734}{THS\_\-SendSlaveRtn} rtn, void $\ast$arg, int lsmID)
\begin{CompactList}\small\item\em stash away routine (from LCM) to send msg to EPU \item\end{CompactList}\item 
void \hyperlink{THS_8c_a83407ad24896c1490086323d916c4ab}{THS\_\-sendSlaveGemSetTableMsg} (unsigned int absTime, \hyperlink{union__THS__GemTableFlags}{THS\_\-GemTableFlags} flags, unsigned int gemStrobe, LSU\_\-factors Kgem)
\begin{CompactList}\small\item\em send gmHack from SIU to EPU (this runs on SIU) \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_991719838cf8691f8893ce0ccd50fa3b}{THS\_\-slaveProcessMsg} (int func, void $\ast$payload, unsigned int len)
\begin{CompactList}\small\item\em routine called from ITC dispatch in LSM to process each message it receives (this routine is run on EPU when it receives message) \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Time Hack Services for CPU. 

\begin{Desc}
\item[Author:]Ed Bacho -- \href{mailto:ebacho@slac.stanford.edu}{\tt ebacho@slac.stanford.edu} This is the controlling module for the Time Hack Services. It does the following:\begin{itemize}
\item Setup Pid and TImers on Rad750 to interrupt on PPS\item Call THS\_\-gemXXX routines to control time hack for 20 MHz clock on gem/gasu\item Call THS\_\-cpuXxx routines to control time hack for Cpu CLock\end{itemize}
\end{Desc}
This module handles calculation of absolute time using the \char`\"{}time hack\char`\"{}. The absolute (or true) time is provided once a second through a combination of a 1PPS (1 pulse per sec) hardware pulse and a time tone message (\char`\"{}at the tone the time will be\char`\"{}) that preceeds it. The time provided in the time tone message from the scapecraft is in microSec since 1/1/2001.

This module only truely works on the Rad750 which has the PPS input thru the PID registers. The mv2304 version can be sort-of simulated by setting up a task to provide PPS (see THS\_\-test.c)

Inputs:\begin{itemize}
\item time tone message every sec (500-800 mSec before pulse) giving next absTime\item HW 1PPS pulse every sec from GASU (although originates from spacecraft)\end{itemize}


Output:\begin{itemize}
\item can obtain corrected absolute time thru WCT\_\-get()\item can obtain corrected absolute time of event thru THS\_\-gemAbsTimeEvent \end{itemize}


\subsection{Function Documentation}
\hypertarget{THS_8c_6876c237f3e829e2a8a1a053d51a43a1}{
\index{THS.c@{THS.c}!THS\_\-getLastPpsAbsTimeSec@{THS\_\-getLastPpsAbsTimeSec}}
\index{THS\_\-getLastPpsAbsTimeSec@{THS\_\-getLastPpsAbsTimeSec}!THS.c@{THS.c}}
\subsubsection[{THS\_\-getLastPpsAbsTimeSec}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-getLastPpsAbsTimeSec ()}}
\label{THS_8c_6876c237f3e829e2a8a1a053d51a43a1}


Get the absolute time value at last PPS. 

\begin{Desc}
\item[Returns:]absolute time value \end{Desc}
\hypertarget{THS_8c_0b19bbf6d1e49dea5b87257e11becc5e}{
\index{THS.c@{THS.c}!THS\_\-init@{THS\_\-init}}
\index{THS\_\-init@{THS\_\-init}!THS.c@{THS.c}}
\subsubsection[{THS\_\-init}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-init (int {\em mask})}}
\label{THS_8c_0b19bbf6d1e49dea5b87257e11becc5e}


Inits the TimeHackService module. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em mask}]time hack options, 1=CPU time hack, 2=Gem Time Hack, 4=CpuIsSiu \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]status This routine does basic init for THS and requires a few modules to be init (PBS,MSG, ??). It setup the Pid TImers for the PPS interrupt and prepares to handle timeones. Howver, it takes several seconds for time hack to be operational and THS\_\-start() can be used to determine if it is operational. \end{Desc}


References THS\_\-CPU\_\-HACK, THS\_\-cpuInit(), THS\_\-GEM\_\-HACK, THS\_\-gemInit(), and THS\_\-SIU\_\-HACK.\hypertarget{THS_8c_a83407ad24896c1490086323d916c4ab}{
\index{THS.c@{THS.c}!THS\_\-sendSlaveGemSetTableMsg@{THS\_\-sendSlaveGemSetTableMsg}}
\index{THS\_\-sendSlaveGemSetTableMsg@{THS\_\-sendSlaveGemSetTableMsg}!THS.c@{THS.c}}
\subsubsection[{THS\_\-sendSlaveGemSetTableMsg}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-sendSlaveGemSetTableMsg (unsigned int {\em absTime}, \/  {\bf THS\_\-GemTableFlags} {\em flags}, \/  unsigned int {\em gemStrobe}, \/  LSU\_\-factors {\em Kgem})}}
\label{THS_8c_a83407ad24896c1490086323d916c4ab}


send gmHack from SIU to EPU (this runs on SIU) 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em absTime}]- time in sec since 1/1/2001 from tt message \item[{\em flags}]- flags from tt message \item[{\em gemStrobe}]- contents of gemStrobe reg with clk on last PPS and clk counter \item[{\em Kgem}]- multiplcation constants for calcs \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}
\hypertarget{THS_8c_54ae5e4a93554a58f9ade65b5d420cc6}{
\index{THS.c@{THS.c}!THS\_\-setGemUpdateMsgs@{THS\_\-setGemUpdateMsgs}}
\index{THS\_\-setGemUpdateMsgs@{THS\_\-setGemUpdateMsgs}!THS.c@{THS.c}}
\subsubsection[{THS\_\-setGemUpdateMsgs}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-setGemUpdateMsgs (int {\em maxMsgs})}}
\label{THS_8c_54ae5e4a93554a58f9ade65b5d420cc6}


set size of memory pool for gemUpdate msgs, must be called before THS\_\-init 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em maxMsgs}]number of gem table update messages in memory pool \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}
\hypertarget{THS_8c_2af9f421a5521535c03a81e0b22c772b}{
\index{THS.c@{THS.c}!THS\_\-setPpsWarnEvery@{THS\_\-setPpsWarnEvery}}
\index{THS\_\-setPpsWarnEvery@{THS\_\-setPpsWarnEvery}!THS.c@{THS.c}}
\subsubsection[{THS\_\-setPpsWarnEvery}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-setPpsWarnEvery (unsigned int {\em warnPpsEvery})}}
\label{THS_8c_2af9f421a5521535c03a81e0b22c772b}


Set how often get warning when no PPS. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em warnPpsEvery}]0=warn once when no pps detect, n=warn every N sec \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}
\hypertarget{THS_8c_707fa3168ba32b79fd64b905db1d3edc}{
\index{THS.c@{THS.c}!THS\_\-setSlaveSend@{THS\_\-setSlaveSend}}
\index{THS\_\-setSlaveSend@{THS\_\-setSlaveSend}!THS.c@{THS.c}}
\subsubsection[{THS\_\-setSlaveSend}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-setSlaveSend ({\bf THS\_\-SendSlaveRtn} {\em rtn}, \/  void $\ast$ {\em arg}, \/  int {\em lsmID})}}
\label{THS_8c_707fa3168ba32b79fd64b905db1d3edc}


stash away routine (from LCM) to send msg to EPU 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rtn}]- pointer to function to use to send off message to EPU \item[{\em arg}]- 1st arg passed into rtn \item[{\em lsmID}]- 2nd arg passed into rtn \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none Given routine should be called as: (rtn)(arg, lsmId, int nid, void $\ast$payload, int payloadLen, void$\ast$ payloadFreeRtn, void $\ast$payloadFreeRtnArg); \end{Desc}
\hypertarget{THS_8c_f8770d8bccc08c0cc4b31ff0f38ed775}{
\index{THS.c@{THS.c}!THS\_\-setTimetoneWarnEvery@{THS\_\-setTimetoneWarnEvery}}
\index{THS\_\-setTimetoneWarnEvery@{THS\_\-setTimetoneWarnEvery}!THS.c@{THS.c}}
\subsubsection[{THS\_\-setTimetoneWarnEvery}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-setTimetoneWarnEvery (unsigned int {\em warnTimetoneEvery})}}
\label{THS_8c_f8770d8bccc08c0cc4b31ff0f38ed775}


Set how often get warning when no timetone. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em warnTimetoneEvery}]0=warn once when no tt detect, n=warn every N sec \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}
\hypertarget{THS_8c_ca69fee554e97202f9fc6458f44bf02b}{
\index{THS.c@{THS.c}!THS\_\-simulatedPPS@{THS\_\-simulatedPPS}}
\index{THS\_\-simulatedPPS@{THS\_\-simulatedPPS}!THS.c@{THS.c}}
\subsubsection[{THS\_\-simulatedPPS}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-simulatedPPS (void)}}
\label{THS_8c_ca69fee554e97202f9fc6458f44bf02b}


Return the state of the pulse-per-second simulation. 

\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em 0}]Pulse-per-second is not being simulated \item[{\em 1}]Pulse-per-second is being simulated \end{description}
\end{Desc}
\hypertarget{THS_8c_166e8adeda51ce2487c895488f3a2450}{
\index{THS.c@{THS.c}!THS\_\-simulatedTT@{THS\_\-simulatedTT}}
\index{THS\_\-simulatedTT@{THS\_\-simulatedTT}!THS.c@{THS.c}}
\subsubsection[{THS\_\-simulatedTT}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-simulatedTT (void)}}
\label{THS_8c_166e8adeda51ce2487c895488f3a2450}


Return the state of the timetone simulation. 

\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em 0}]Timetone is not being simulated \item[{\em 1}]Timetone is being simulated \end{description}
\end{Desc}
\hypertarget{THS_8c_991719838cf8691f8893ce0ccd50fa3b}{
\index{THS.c@{THS.c}!THS\_\-slaveProcessMsg@{THS\_\-slaveProcessMsg}}
\index{THS\_\-slaveProcessMsg@{THS\_\-slaveProcessMsg}!THS.c@{THS.c}}
\subsubsection[{THS\_\-slaveProcessMsg}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-slaveProcessMsg (int {\em func}, \/  void $\ast$ {\em payload}, \/  unsigned int {\em len})}}
\label{THS_8c_991719838cf8691f8893ce0ccd50fa3b}


routine called from ITC dispatch in LSM to process each message it receives (this routine is run on EPU when it receives message) 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em func}]func code for message \item[{\em payload}]- buffer with received message \item[{\em len}]- length of given buffer \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}


References THS\_\-FSW\_\-FLAG\_\-CPU\_\-PPS\_\-SIM, THS\_\-FSW\_\-FLAG\_\-TT\_\-SIM, THS\_\-gemSetTable(), and THS\_\-timetone().\hypertarget{THS_8c_017318158fc77ad5c9a79a84c7341828}{
\index{THS.c@{THS.c}!THS\_\-stats@{THS\_\-stats}}
\index{THS\_\-stats@{THS\_\-stats}!THS.c@{THS.c}}
\subsubsection[{THS\_\-stats}]{\setlength{\rightskip}{0pt plus 5cm}void THS\_\-stats ()}}
\label{THS_8c_017318158fc77ad5c9a79a84c7341828}


Print some THS statistics. 

\begin{Desc}
\item[Returns:]none \end{Desc}


References THS\_\-CPU\_\-HACK, THS\_\-GEM\_\-HACK, THS\_\-gemCalcClkDiff(), THS\_\-gemGetTableInfo(), and THS\_\-prevTimtoneFromEvent().\hypertarget{THS_8c_74d3678b7bf6f28e8871dc67b7e0fe52}{
\index{THS.c@{THS.c}!THS\_\-timetone@{THS\_\-timetone}}
\index{THS\_\-timetone@{THS\_\-timetone}!THS.c@{THS.c}}
\subsubsection[{THS\_\-timetone}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-timetone (unsigned int {\em absTimeSec}, \/  unsigned short {\em saFlags})}}
\label{THS_8c_74d3678b7bf6f28e8871dc67b7e0fe52}


Provide absTime for next entry in table (on next PPS). 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em absTimeSec}]- absolute time for next PPS pulse in secs since 1/1/2001 \item[{\em saFlags}]- flags from Spectrum Astro (i.e. in Magic 7 telecommand) \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]status This routine provides the absolute time for next PPS. This should come from the spacecrasft once a sec approx 500-800 milliSec before PPS. \end{Desc}


Referenced by THS\_\-slaveProcessMsg().\hypertarget{THS_8c_4b801a1f8fc468fd5f1f8fb2c3fb67ee}{
\index{THS.c@{THS.c}!THS\_\-wait@{THS\_\-wait}}
\index{THS\_\-wait@{THS\_\-wait}!THS.c@{THS.c}}
\subsubsection[{THS\_\-wait}]{\setlength{\rightskip}{0pt plus 5cm}unsigned int THS\_\-wait ()}}
\label{THS_8c_4b801a1f8fc468fd5f1f8fb2c3fb67ee}


Wait for TimeHackService module to be fully operational. 

\begin{Desc}
\item[Returns:]status Since time hack requires a number of subsystems to be operating. These includes LCB driver, 1553 driver, and time tone messages from the scapecraft. Since this module requires two timeTone message from the spacecraft it might take 2 seconds before it is operational. THe routine does a slow poll waiting for the THS to be operational.\end{Desc}
The sequence is: \hyperlink{THS_8h_fee2d448fb4959febe78485c62ba3247}{THS\_\-init()};

start up bunch of things including Sw to call THS\_\-timeTone() every second as result of message from spacecraft Hw to cause PPS interrupt every second

\hyperlink{THS_8h_4b801a1f8fc468fd5f1f8fb2c3fb67ee}{THS\_\-wait()} (this routine waits till receives two THS\_\-timeTone() followed by PPS interrupt) 

References THS\_\-CPU\_\-HACK, THS\_\-cpuWait(), THS\_\-GEM\_\-HACK, and THS\_\-gemWait().