\section{I\textsuperscript{2}C Unit}

The \gls{I2C} unit provides access to one of the microcontroller's \gls{I2C} peripherals. It can be configured to use either of the three speeds (Standard, Fast and Fast+) and supports both 10-bit and 7-bit addressing. 10-bit addresses can be used in commands by setting their highest bit (0x8000), as a flag to the unit.

\subsection{I2C Configuration}

\begin{inicode}
[I2C:d@4]
# Peripheral number (I2Cx)
device=1
# Pin mappings (SCL,SDA)
#  I2C1: (0) B6,B7    (1) B8,B9
#  I2C2: (0) B10,B11  (1) B13,B14
remap=0

# Speed: 1-Standard, 2-Fast, 3-Fast+
speed=1
# Analog noise filter enable (Y,N)
analog-filter=Y
# Digital noise filter bandwidth (0-15)
digital-filter=0
\end{inicode}

\subsection{I\textsuperscript{2}C Commands}

\begin{cmdlist}

	0 & \cname{WRITE}
	Raw write transaction
	& \begin{cmdreq}
		\cfield{u16} slave address
		\cfield{u8[]} bytes to write
	\end{cmdreq} \\

	1 & \cname{READ}
	Raw read transaction
	& \begin{cmdreq}
		\cfield{u16} slave address
		\cfield{u16} number of read bytes
    \end{cmdreq}
\cjoin
    \begin{cmdresp}
		\cfield{u8[]} received bytes
    \end{cmdresp} \\

	2 & \cname{WRITE\_REG}
	Write to a slave register. Sends the register number and the data in the same \gls{I2C} transaction. Multiple registers can be written to slaves supporting auto-increment.
	& \begin{cmdreq}
		\cfield{u16} slave address
		\cfield{u8} register number
		\cfield{u8[]} bytes to write
    \end{cmdreq} \\

	3 & \cname{READ\_REG}
	Read from a slave register. Writes the register number and issues a read transaction of the given length. Multiple registers can be read from slaves supporting auto-increment.
	& \begin{cmdreq}
		\cfield{u16} slave address
		\cfield{u8} register number
		\cfield{u16} number of read bytes
    \end{cmdreq}
	\cjoin
    \begin{cmdresp}
		\cfield{u8[]} received bytes
    \end{cmdresp} \\

\end{cmdlist}

