
% ===============================================================================
% Purpose: Firmware of the AIDA mini-TLU
% Author: Alvaro Dosil
% Created on: 2013-03-13
% ===============================================================================
\newcolumntype{C}[1]{>{\small\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\section{Firmware}
\label{sec:Firmware}

The AIDA mini-TLU firmware is composed basically by the next main elements: 
the Ethernet communication driver (IPbus), the time to digital converter (TDC), 
the handshake controller, the controler for discriminator threshold DAC and
the controller for programmable shutter/spill-on/power-cycling signal.

\subsection{IPbus}
IPbus is a simple way to control and communicate with your Ethernet-attached xTCA hardware via the IPbus protocol.
IPbus is the communication protocol used for the data interchange between the mini-TLU
and the DAQ pc. Its source code is available to download in \cite{ref:IPbus_webpage}, 
it is written in VHDL and it has a small footprint. Data is sent directly to 
the FPGA and you do not need a microblaze or other CPU. It is not needed any proprietary 
hardware, firmware or software to operate it as well. The transport protocol is UDP and 
the data is addressable in 32b packets. \\

\subsection{Time to digital converter (TDC)}
The TDC is the component in charge of time stamping every interesting signal with an accuracy of 0.725 ns.
To accomplish this precision the TDC uses a 640 MHz input clock as reference and splits the input, 
delaying one of the split signals one half cycle. 
Each branch goes then to the deserializer module IOSERDES (Input/Output SERializar/DESerializer). 
This module generates a 4 bits parallel word with the input data deserialized, then this data is
merged and we obtain an 8 bits parallel word every 1/(160 MHz) ns.
The data obtained is analysed and the system generates a rising or falling signal, depending on the input.
The rising and falling edges are sent then to the event formatter, which generates the final information
word to be sent out. Finally, the data arrives to the event buffer, where it is saved in a FIFO while it 
waits to be sent to the DAQ pc. The schematic of this process can be seen in Figure \ref{fig:TDC}.

\begin{figure}
  \centering
    \includegraphics[scale=0.4]{figs/TDC_Block_diagram.png}
  \caption{TDC global schematic view}
  \label{fig:TDC}
\end{figure}

The IODELAY module needs to be recalibrated every once in a while. For the moment the calibration is started
externally from IPbus. The finite state machine of the calibration process is indicated in Figure \ref{fig:IODELAY_recal}.

\begin{figure}
  \centering
    \includegraphics[scale=0.25]{figs/FSM_IODELAY_cal.png}
  \caption{Finite state machine for the IODELAY calibration}
  \label{fig:IODELAY_recal}
\end{figure}

\subsection{Data Format}
The AIDA mini-TLU records the time stamp of input and internally generated signals with an accuracy better
than 1 ns. The time stamp format is different depending on the signal recorded and the number of input signals
configured in the measuring moment. There are three possible signal types: trigger, internally generated and edge signals.
The internally generated and edge signals have the properties of rising and falling edges. The coding of these names is indicated
in Table \ref{tab:Evt_Type}. Data is sent to the DAQ pc in packages from one to three words of 64 bits.
\begin{table}[t]
\centering
\begin{tabular}{|r|l|}

\hline
0000 & internal trigger \\ \hline
0001 & external trigger \\ \hline
0010 & shutter falling \\ \hline
0011 & shutter rising \\ \hline
0100 & edge falling \\ \hline
0101 & edge rising \\ \hline
0110 & spill off \\ \hline
0111 & spill on \\ \hline

\end{tabular}
\caption{Event type codification}
\label{tab:Evt_Type}
\end{table}


\subsubsection{Trigger data}
The AIDA TLU will have 12 dedicated trigger inputs. 
The mapping for this type of signal is indicated in Figure \ref{fig:trig_map}. 
The information of this signal is sent in two or three 64 bits word depending on whether any of the 
4 to 11 dedicated trigger inputs are enabled. The AIDA mini-TLU only has four trigger inputs, 
so it will always send only two 64 bits words. \\
The 48 lowest significant bits of the first word indicate the 25 ns time stamp. 
This time stamp is synchronized with the 40 MHz main system clock. 
Bits from 48 to 59 indicate which of the trigger inputs have been fired and the 4 most significant bits indicate the event type. 
In this particular case the possibilities are only internal or external trigger.
The 32 lowest significant bits of the second word indicate the event number.
The 32 highest significant bits are divided in four words of 8 bits. 
Each word indicates the 1 ns accuracy time stamp of the signal received in each dedicated input.
Only in the case that at least one of the 4 to 11 dedicated trigger inputs is enabled, the third word is created and sent out.
This third word contains eight words of eight bits, each indicating the 1 ns time stamp of the trigger in each input.

\begin{figure}
  \centering
    \includegraphics[scale=0.35]{figs/trigger_map.png}
  \caption{Mapping of the trigger signal information. The third word, marked with a star, is only sent when
  at least one of the 4 to 11 dedicated trigger inputs is enabled.}
  \label{fig:trig_map}
\end{figure}

\subsubsection{Internally generated data}
The internally generated signal information includes the event type, an event number counter and a 25 ns time stamp, as can
be seen in Figure \ref{fig:shut_map}. In this case only one 64 bits word. 
These signals are synchronous with the 40 MHz system clock, therefore only a 25 ns time stamp is needed. \\
The 48 lowest significant bits indicate the 25 ns time stamp. The next 12 bits are the event number and the four most significant
bits indicate the event type.
\begin{figure}
  \centering
    \includegraphics[scale=0.35]{figs/shutter_spill_map.png}
  \caption{Mapping of the internally generated signals information.}
  \label{fig:shut_map}
\end{figure}

\subsubsection{Edge data}
There are some external signals that can be interesting to record. We call them edge data because we only want to know their 
rising or falling edge time stamps. The data format can be seen in Figure \ref{fig:edge_map}. 
As in the shutter and spill case, only one 64 bits word is sent. The 48 lowest significant bits indicate the 25 ns time stamp and the 
4 highest significant bits indicate the event type, as in every other signals. Bits from 49 to 55 indicate the 1 ns time stamp and
the next 4 bits code which input detected the event.
\begin{figure}
  \centering
    \includegraphics[scale=0.35]{figs/edge_map.png}
  \caption{Mapping of the edge signal information.}
  \label{fig:edge_map}
\end{figure}

\subsection{Handshake between TLU and DUT}
The AIDA TLU provides two different handshake modes. One EUDET handshake, 
compatible with the old JRA1 TLU, and a new synchronous mode.

\subsubsection{EUDET handshake (Trigger/busy with trigger number)}
\begin{enumerate}
 \item TLU receives trigger from beam scintillators
 \item TLU asserts TRIGGER
 \item On receipt of TRIGGER going high, the detector asserts BUSY
 \item On receipt of BUSY going high, TLU de-asserts TRIGGER and switches the TRIGGER line to the output of a 
shift register holding the trigger number/data.
 \item The DUT clocks data out of the shift register by toggling TRIGGER CLOCK.
Data changes on the rising edge of TRIGGER CLOCK3 . The least significant bit
of the trigger data is shifted out first. Only the bottom 15-bits of the 32-bit
trigger counter are clocked out. If more than 15 clock pulses are issued on the
TRIGGER CLOCK line the TRIGGER output is set to zero. The DUT should
issue 16 clock pulses which will clock out the bottom 15-bits of the trigger number
and return the TRIGGER line to logical low. This will avoid glitches on the
TRIGGER line when the DUT returns the BUSY line to logical low.
 \item After clocking out the trigger number (and the detector being ready to take more
data, the DUT de-asserts BUSY)
 \item System is ready for triggers again.
\end{enumerate}

\begin{figure}[H]
  \centering
    \includegraphics[scale=0.4]{figs/Trigger_Data_Handshake.png}
  \caption{Timing of signals in ``EUDET Handshake''}
  \label{fig:Trigger_Handshake}
\end{figure}

\subsubsection{Synchronous mode}
This mode is new for the AIDA TLU and it is incompatible with old handshake modes.
\begin{enumerate}
  \item TLU receives trigger from beam scintillators
  \item TLU asserts TRIGGER synchronous with its internal clock
  \item After one clock cycle the system is ready for triggers again
\end{enumerate}
\begin{itemize}
 \item If at any time the TLU receives a busy signal from DUT, it will veto all incoming triggers while the signal is active
\end{itemize}
\begin{figure}[H]
  \centering
    \includegraphics[scale=0.9]{figs/tlu_timing_diagram_02.pdf}
  \caption{Timing of signals in ``Synchronous mode''}
  \label{fig:Trigger_Handshake}
\end{figure}