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


{\tt \#include \char`\"{}PBS/PBS.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/WCT.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/TASK.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/TMR.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/LSU.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/MBA.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/FPA.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/BSWP.ih\char`\"{}}\par
{\tt \#include \char`\"{}LCBD/LCBD.h\char`\"{}}\par
{\tt \#include \char`\"{}MSG/MSG\_\-pubdefs.h\char`\"{}}\par
{\tt \#include \char`\"{}THS/THS\_\-msgs.h\char`\"{}}\par
{\tt \#include \char`\"{}THS\_\-prvdefs.h\char`\"{}}\par
{\tt \#include $<$stdio.h$>$}\par


Include dependency graph for THS.c:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=177pt]{THS_8c__incl}
\end{center}
\end{figure}
\subsection*{Functions}
\begin{CompactItemize}
\item 
unsigned int \hyperlink{THS_8c_a0}{THS\_\-init} (int mask)
\begin{CompactList}\small\item\em Inits the Time\-Hack\-Service module. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_a1}{THS\_\-wait} ()
\begin{CompactList}\small\item\em Wait for Time\-Hack\-Service module to be fully operational. \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_a2}{THS\_\-timetone} (unsigned int abs\-Time\-Sec, unsigned short flags)
\begin{CompactList}\small\item\em Provide abs\-Time for next entry in table (on next PPS). \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_a3}{THS\_\-get\-Last\-Pps\-Abs\-Time\-Sec} ()
\begin{CompactList}\small\item\em Get the absolute time value at last PPS. \item\end{CompactList}\item 
void \hyperlink{THS_8c_a4}{THS\_\-set\-Slave\-Send} (\hyperlink{THS_8h_a1}{THS\_\-Send\-Slave\-Rtn} rtn, void $\ast$arg, int lsm\-ID)
\begin{CompactList}\small\item\em stash away routine (from LCM) to send msg to EPU \item\end{CompactList}\item 
void \hyperlink{THS_8c_a5}{THS\_\-send\-Slave\-Gem\-Set\-Table\-Msg} (unsigned int abs\-Time, unsigned int flags, unsigned int gem\-Strobe, LSU\_\-factors Kgem)
\begin{CompactList}\small\item\em send gm\-Hack from SIU to EPU (this runs on SIU) \item\end{CompactList}\item 
unsigned int \hyperlink{THS_8c_a6}{THS\_\-slave\-Process\-Msg} (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} \end{Desc}
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\_\-gem\-XXX routines to control time hack for 20 MHz clock on gem/gasu\item Call THS\_\-cpu\-Xxx routines to control time hack for Cpu CLock\end{itemize}


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 micro\-Sec 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 m\-Sec before pulse) giving next abs\-Time\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\_\-gem\-Abs\-Time\-Event\end{itemize}


\subsection{Function Documentation}
\hypertarget{THS_8c_a3}{
\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\_\-get\-Last\-Pps\-Abs\-Time\-Sec ()}}
\label{THS_8c_a3}


Get the absolute time value at last PPS. 

\begin{Desc}
\item[Returns:]absolute time value \end{Desc}
\hypertarget{THS_8c_a0}{
\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_a0}


Inits the Time\-Hack\-Service module. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em mask}]time hack options, 1=CPU time hack, 2=Gem Time Hack, 4=Cpu\-Is\-Siu \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]status \end{Desc}
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. 

Here is the call graph for this function:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=111pt]{THS_8c_a0_cgraph}
\end{center}
\end{figure}
\hypertarget{THS_8c_a5}{
\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\_\-send\-Slave\-Gem\-Set\-Table\-Msg (unsigned int {\em abs\-Time}, unsigned int {\em flags}, unsigned int {\em gem\-Strobe}, LSU\_\-factors {\em Kgem})}}
\label{THS_8c_a5}


send gm\-Hack from SIU to EPU (this runs on SIU) 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em abs\-Time}]- time in sec since 1/1/2001 from tt message \item[{\em flags}]- flags from tt message \item[{\em gem\-Strobe}]- contents of gem\-Strobe 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_a4}{
\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\_\-set\-Slave\-Send (\hyperlink{THS_8h_a1}{THS\_\-Send\-Slave\-Rtn} {\em rtn}, void $\ast$ {\em arg}, int {\em lsm\-ID})}}
\label{THS_8c_a4}


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 lsm\-ID}]- 2nd arg passed into rtn \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]none \end{Desc}
Given routine should be called as: (rtn)(arg, lsm\-Id, int nid, void $\ast$payload, int payload\-Len, void$\ast$ payload\-Free\-Rtn, void $\ast$payload\-Free\-Rtn\-Arg); \hypertarget{THS_8c_a6}{
\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\_\-slave\-Process\-Msg (int {\em func}, void $\ast$ {\em payload}, unsigned int {\em len})}}
\label{THS_8c_a6}


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}


Here is the call graph for this function:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=155pt]{THS_8c_a6_cgraph}
\end{center}
\end{figure}
\hypertarget{THS_8c_a2}{
\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 abs\-Time\-Sec}, unsigned short {\em flags})}}
\label{THS_8c_a2}


Provide abs\-Time for next entry in table (on next PPS). 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em abs\-Time\-Sec}]- absolute time for next PPS pulse in secs since 1/1/2001 \item[{\em flags}]\end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]status \end{Desc}
This routine provides the absolute time for next PPS. This should come from the spacecrasft once a sec approx 500-800 milli\-Sec before PPS. \hypertarget{THS_8c_a1}{
\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_a1}


Wait for Time\-Hack\-Service module to be fully operational. 

\begin{Desc}
\item[Returns:]status \end{Desc}
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 time\-Tone 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.

The sequence is: \hyperlink{THS_8h_a6}{THS\_\-init()};

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

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

Here is the call graph for this function:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=117pt]{THS_8c_a1_cgraph}
\end{center}
\end{figure}
