\subsection{Serial Console}

One core part of any computer systems is it's way to get human input. On older
systems, and even today on server machines, this is done via a serial console.
On this serial console, characters are transmitted in serial, which means bit
by bit over the same line. The voltage levels used in these systems vary from
5V to 3.3V or +-10V. The most common standart for these voltage levels is the
former RS-232\footnote{RS... Recommended Standard} or as it should be called
now TIA-\footnote{TIA...Telecommunications Industry 
Association}/EIA-\footnote{EIA.. Electronic Industries Alliance}232. Voltage-
levels as per TIA-/EIA Standard are not practical to handle over short
distances to handle however, so other voltages are used on most interface chips
and need to be converted.

\subsubsection{16550 UART}

The 16550 UART\footnote{Uinversal Asynchronous Receiver and Transmitter} is a
very common interface chip for serial communications. It produces 5V logic
levels as output on TX and needs the same as input on RX. Thoug common for a
UART, these voltage levels need to be converted to TIA-/EIA-232 levels for a
more common interface.

The 16550 UART is, in it's core a 16450 UART, but has been given a FIFO
\footnote{First-In First-Out} buffer. It needs three address lines, and 8
data lines, which can be seen in figure \ref{fig:16550_pinout}

\begin{figure}[H]
	\centering
	\includesvg[height=.3\textheight, angle=0]{pics/pc16550d_pinout.svg}
	\caption{PC-16550D Pinout\cite{pc16550}}
	\label{fig:16550_pinout}
\end{figure}

In figure \ref{fig:16550_pinout} the most important lanes are the SIN and
sout lanes, as they contain the serial data to and from the 16550 UART.

\subsubsection{MAX-232}

To convert the voltage levels of the 16550 UART to levels compliant wit
TIA-/EIA-232 levels, the MAX-232 is used. It has two transmitters and two
receivers side and generates the needed voltage levels via an internal voltage
pump\cite{max232}.

\subsubsection{Schematics}
Based on the descriptions in the datasheets the schematic in figure 
\ref{fig:schem_uart} was developed.

\begin{figure}[H]
	\centering
	\includegraphics[height=.65\textheight, angle=-90]{schem_pdf/16550.pdf}
	\caption{The schematic of the UART Module}
	\label{fig:schem_uart}
\end{figure}

\paragraph{Element Description}

The quartz oszillator Y1 is the clock source for the Baud Rate generation and
was chosen with 1.8432 MHz for availability reasons and because it is the lowest
ozillator from which all common baud rates can still be derived from
\cite{pc16550}.
Resistors R1 and R2 are for stability and functionality of the Oszillator
nescessary as per datasheet. The resulting frequency can bemeasured via
J1, the measurement can be seen in \ref{fig:uartquartz}. C1 is used to 
stabilize the
voltage for the 16550 UART and is common practice. Via JP1 the UART can be
transformed into a USRT where the receiver is synchronized to the transmitter
via a clock line. This mode has, however, not been tested, and the clock needs
to be 16 times the receiver clock rate\cite{pc16550}. The final output of the
16550 UART can be used and measured via J2, as shown in figure \ref{fig:uart232}
. Before the UART on J2 can be use however, the Jumpers JP2 and JP3 need to be
removed as otherwise the MAX-232 will short out with the incoming signal.
capacitors C4, C6, C7, C7 and C8 are for the voltage pump as defined in the
datasheet\cite{max232}. R4 and R5 have been suggested by the supervisor in 
order toavoid damage to the MAX-232. The RJ-45 plug is used to transmit the
TIA-/EIA-232 signal, rather than the more common D-SUB connector, because the
RJ-45 connector fits on a 2.54mm grid. The Pinout onthe RJ-45 plug can be seen
in figure \ref{fig:rs232rj45}. C5 has the same functionality for the
MAX-232 as the C1 has to the 16550-UART.

\begin{figure}[H]
\begin{tikzpicture}
	\begin{axis}[
		ylabel=Quartz Voltage,
		xlabel=Time,
		grid=both,
		minor tick num=5,
		width=\textwidth,
		height=0.5\textheight] 
		
		\addplot table [x=t, y=c1, col sep=comma, mark=none] {meas/20200319uartquartz.csv};
	\end{axis}
	\end{tikzpicture}
	\caption{Measurement of the 1.8432 MHz Output on J1}
	\label{fig:uartquartz}
\end{figure}

\begin{figure}[H]
\begin{tikzpicture}
	\begin{axis}[
		ylabel=Lane Voltage,
		xlabel=Time,
		grid=both,
		minor tick num=5,
		width=\textwidth,
		height=0.5\textheight] 
		
		\addplot table [x=t, y=c1, col sep=comma, mark=none] {meas/20200218ttluart.csv};
		\addplot table [x=t, y=c2, col sep=comma, mark=none] {meas/20200218ttluart.csv};
		\legend{TIA-/EIA-232 level,UART level}
	\end{axis}
	\end{tikzpicture}
	\caption{Measurement of a character transmission before and after MAX-232}
	\label{fig:uart232}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth, angle=0]{pics/rj45-consoleport-iface-500.png}
	\caption{Pinout of the RJ-45 Plug; Src: \url{https://www.wti.com/}}
	\label{fig:rs232rj45}
\end{figure}

\subsubsection{Demonstration Software}

To demonstrate the functionality and prove, that the schematic has no underlying
error, a program which regularly transmits a character was written as well as
a simple echo program, which transmits all received characters. Both programs
transmit 8 bit characters without parity at 38400 Baud. The output for program
one can be seen in figure \ref{fig:uart232} and the output for program two in
figure \ref{fig:232_echo}. 

\begin{figure}[H]
\begin{tikzpicture}
	\begin{axis}[
		ylabel=Lane Voltage,
		xlabel=Time,
		grid=both,
		minor tick num=5,
		width=\textwidth,
		height=0.5\textheight] 
		
		\addplot table [x=t, y=c1, col sep=comma, mark=none] {meas/20200218echo.csv};
		\addplot table [x=t, y=c2, col sep=comma, mark=none] {meas/20200218echo.csv};
		\legend{RX,TX}
	\end{axis}
	\end{tikzpicture}
	\caption{Measurement of a character echo}
	\label{fig:232_echo}
\end{figure}

\paragraph{Transmit code}
The transmit code regularly transmits the letter capital A  via the 16550 UART,
but before it can do this it needs to perform some initialisations. The
functions shown in listing \ref{lst:16550-general} are the read and write
routines for accessing the 16550 UART. These routines also apply to the echo
code. 
\lstinputlisting[language=C,frame=trBL,
	breaklines=true, breakautoindent=true, formfeed=\newpage,
	label={lst:16550-general}, caption={Read and write routines for the 
		16550 UART},
	columns=flexible, style=cstyle, firstline=0, lastline=69]
		{code/16550/transmit/src/main.c}
To write to the 16550 UART, you need to perform some setup tasks. After startup,
it requires a $MR$ for at least 5µs\cite{pc16550}. The baud rate divisor latch 
needs to be set to the specified divisor for the desired
baud rate, and the character width and parity control
needs to be set. The $MR$ signal is beeing generated by the AVR on bootup. To
access the divisor latch, the divisor latch access bit needs to be set and
after setting up the baud rate divisor latch, it nees to be cleared to allow
a regular transmission. This process can be seen in listing \ref{lst:16550-transmit}
\lstinputlisting[language=C,frame=trBL,
	breaklines=true, breakautoindent=true, formfeed=\newpage,
	label={lst:16550-transmit}, caption={16550 INIT routines and single char transmission},
	columns=flexible, style=cstyle, firstline=71, lastline=106]
		{code/16550/transmit/src/main.c}

The output of this code on the address, data and control bus as well as on the
SOUT lane of the 16550 UART can be seen in figure  \ref{fig:16550A}

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth, angle=0]{meas/20200211_first_trans.png}
	\caption{Transmission of character A via the 16550 UART}
	\label{fig:16550A}
\end{figure}

\paragraph{Echo code}

The echo code permanently polls the 16550 UART wether a character has been
received, and if yes, reads it from the receiver holding register andwrites it
back to the tx holding register. The output of this code can be seen in figure
\ref{fig:232_echo}. The initialisation is practically the same as for the
transmission code, as well as the read and write routines in listing 
\ref{lst:16550-general}.

\lstinputlisting[language=C,frame=trBL,
	breaklines=true, breakautoindent=true, formfeed=\newpage,
	label={lst:16550-echo}, caption={16550 character echo},
	columns=flexible, style=cstyle, firstline=76, lastline=109]
		{code/16550/echo/src/main.c}
