\section{aversive\_\-10-03-12/modules/comm/i2c/i2c.c File Reference}
\label{i2c_8c}\index{aversive\_\-10-03-12/modules/comm/i2c/i2c.c@{aversive\_\-10-03-12/modules/comm/i2c/i2c.c}}
{\tt \#include $<$stdlib.h$>$}\par
{\tt \#include $<$string.h$>$}\par
{\tt \#include $<$util/twi.h$>$}\par
{\tt \#include $<$autoconf.h$>$}\par
{\tt \#include $<$aversive/errno.h$>$}\par
{\tt \#include $<$i2c.h$>$}\par
\subsection*{Functions}
\begin{CompactItemize}
\item 
void {\bf i2c\_\-init} ({\bf i2c\_\-mode\_\-t} mode, uint8\_\-t add)
\item 
void {\bf i2c\_\-register\_\-recv\_\-event} (void($\ast${\bf event})(uint8\_\-t $\ast$, int8\_\-t))
\item 
void {\bf i2c\_\-register\_\-recv\_\-byte\_\-event} (void($\ast${\bf event})(uint8\_\-t, uint8\_\-t, uint8\_\-t))
\item 
void {\bf i2c\_\-register\_\-send\_\-event} (void($\ast${\bf event})(int8\_\-t))
\item 
int8\_\-t {\bf i2c\_\-send} (uint8\_\-t dest\_\-add, uint8\_\-t $\ast$buf, uint8\_\-t size, uint8\_\-t ctrl)
\item 
int8\_\-t {\bf i2c\_\-resend} (void)
\item 
int8\_\-t {\bf i2c\_\-rerecv} (void)
\item 
void {\bf i2c\_\-release\_\-bus} (void)
\item 
void {\bf i2c\_\-reset} (void)
\item 
int8\_\-t {\bf i2c\_\-recv} (uint8\_\-t dest\_\-add, uint8\_\-t size, uint8\_\-t ctrl)
\item 
int8\_\-t {\bf i2c\_\-flush} (void)
\item 
uint8\_\-t {\bf i2c\_\-set\_\-recv\_\-size} (uint8\_\-t size)
\item 
{\bf i2c\_\-mode\_\-t} {\bf i2c\_\-mode} (void)
\item 
uint8\_\-t {\bf i2c\_\-status} (void)
\item 
uint8\_\-t {\bf i2c\_\-get\_\-recv\_\-buffer} (uint8\_\-t $\ast$buf, uint8\_\-t size)
\item 
{\bf SIGNAL} (SIG\_\-2WIRE\_\-SERIAL)
\end{CompactItemize}


\subsection{Function Documentation}
\index{i2c.c@{i2c.c}!i2c\_\-flush@{i2c\_\-flush}}
\index{i2c\_\-flush@{i2c\_\-flush}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-flush]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t i2c\_\-flush (void)}\label{i2c_8c_b70b41e07d082581ea209e319fb32d40}


Try to flush the current operation, before it is started. The i2c module is then tagged as ready. If it returns 0, the flush was a success, and \doxyref{i2c\_\-send()}{p.}{i2c_8c_560d3f33dacd2199dd9bac3a5a7afe57} can be called. Else, it means that a transmission was running. 

Definition at line 420 of file i2c.c.

References EBUSY, ESUCCESS, I2C\_\-STATUS\_\-SLAVE\_\-XMIT\_\-WAIT, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-get\_\-recv\_\-buffer@{i2c\_\-get\_\-recv\_\-buffer}}
\index{i2c\_\-get\_\-recv\_\-buffer@{i2c\_\-get\_\-recv\_\-buffer}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-get\_\-recv\_\-buffer]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t i2c\_\-get\_\-recv\_\-buffer (uint8\_\-t $\ast$ {\em buf}, \/  uint8\_\-t {\em size})}\label{i2c_8c_e7824af2548e0a0e4d15a5420a515813}


Copy the received buffer in the buffer given as parameter. Return number of copied bytes or $<$ 0 on \doxyref{error}{p.}{structerror}. 

Definition at line 495 of file i2c.c.

References EBUSY, I2C\_\-STATUS\_\-MASTER\_\-RECV, I2C\_\-STATUS\_\-SLAVE\_\-RECV, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-init@{i2c\_\-init}}
\index{i2c\_\-init@{i2c\_\-init}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-init]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-init ({\bf i2c\_\-mode\_\-t} {\em mode}, \/  uint8\_\-t {\em add})}\label{i2c_8c_06d2ef9760a322ed36bc2485ae2141e4}


mode is I2C\_\-MODE\_\-UNINIT, I2C\_\-MODE\_\-MASTER, I2C\_\-MODE\_\-MULTIMASTER or I2C\_\-MODE\_\-SLAVE. Parameter add is the address in slave mode, it is composed from: b7 : true if the uC can be addressed with GENCALL b0-6: slave address 

Definition at line 89 of file i2c.c.

References I2C\_\-MODE\_\-UNINIT, I2C\_\-STATUS\_\-READY, IRQ\_\-LOCK, IRQ\_\-UNLOCK, and sbi.\index{i2c.c@{i2c.c}!i2c\_\-mode@{i2c\_\-mode}}
\index{i2c\_\-mode@{i2c\_\-mode}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-mode]{\setlength{\rightskip}{0pt plus 5cm}{\bf i2c\_\-mode\_\-t} i2c\_\-mode (void)}\label{i2c_8c_1eacf5805ac520bfc7dad3d8ad12c57c}


return the current mode of the i2c module. 

Definition at line 478 of file i2c.c.\index{i2c.c@{i2c.c}!i2c\_\-recv@{i2c\_\-recv}}
\index{i2c\_\-recv@{i2c\_\-recv}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-recv]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t i2c\_\-recv (uint8\_\-t {\em dest\_\-add}, \/  uint8\_\-t {\em size}, \/  uint8\_\-t {\em ctrl})}\label{i2c_8c_1c03f80593e8304b612425659d324841}


In slave mode, it returns \doxyref{error}{p.}{structerror} and is useless (all data is received trough the callback). In master mode, if dest\_\-add is between 0 and 127, it will start to read the addressed slave. The size of the buffer to read must be specified. Return 0 on success. 

Definition at line 352 of file i2c.c.

References EBUSY, EINVAL, ENXIO, ESUCCESS, I2C\_\-ADD\_\-MASTER, I2C\_\-CTRL\_\-SYNC, I2C\_\-MODE\_\-SLAVE, I2C\_\-MODE\_\-UNINIT, I2C\_\-STATUS\_\-MASTER\_\-RECV, I2C\_\-STATUS\_\-MASTER\_\-XMIT, I2C\_\-STATUS\_\-OP\_\-FINISHED, I2C\_\-STATUS\_\-READY, I2C\_\-STATUS\_\-SLAVE\_\-RECV, I2C\_\-STATUS\_\-SLAVE\_\-XMIT, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.

Referenced by i2c\_\-rerecv().\index{i2c.c@{i2c.c}!i2c\_\-register\_\-recv\_\-byte\_\-event@{i2c\_\-register\_\-recv\_\-byte\_\-event}}
\index{i2c\_\-register\_\-recv\_\-byte\_\-event@{i2c\_\-register\_\-recv\_\-byte\_\-event}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-register\_\-recv\_\-byte\_\-event]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-register\_\-recv\_\-byte\_\-event (void($\ast$)(uint8\_\-t, uint8\_\-t, uint8\_\-t) {\em event})}\label{i2c_8c_880a5ff73b3bbb7da40fd1f0aca3c658}


Register a function that is called when a byte is received. Arguments of the callback are: (hwstatus, numbyte, byte). The user app can modify the g\_\-recv\_\-size value, which is the number of bytes to be received in the frame: this can be done by calling \doxyref{i2c\_\-set\_\-recv\_\-size()}{p.}{i2c_8c_ce99548d1f29c7e06a3ed769673e0e9b}. 

Definition at line 169 of file i2c.c.

References IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-register\_\-recv\_\-event@{i2c\_\-register\_\-recv\_\-event}}
\index{i2c\_\-register\_\-recv\_\-event@{i2c\_\-register\_\-recv\_\-event}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-register\_\-recv\_\-event]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-register\_\-recv\_\-event (void($\ast$)(uint8\_\-t $\ast$, int8\_\-t) {\em event})}\label{i2c_8c_545b66d49e0bf82c8930367ff9abf1a2}


Register a function that is called when a buffer is received. The user application is always notified when data frame is received. Arguments of the callback are:\begin{itemize}
\item (recv\_\-buf, n$>$0) if transmission succedded. The first parameter contains the address of the reception buffer and the second contains the number of received bytes.\item (NULL, err$<$0) if the transmission failed (slave not answering or arbiteration lost). The first parameter is NULL and the second contains the \doxyref{error}{p.}{structerror} code. \end{itemize}


Definition at line 153 of file i2c.c.

References IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-register\_\-send\_\-event@{i2c\_\-register\_\-send\_\-event}}
\index{i2c\_\-register\_\-send\_\-event@{i2c\_\-register\_\-send\_\-event}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-register\_\-send\_\-event]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-register\_\-send\_\-event (void($\ast$)(int8\_\-t) {\em event})}\label{i2c_8c_10a9741651e7020a6899c66bc08ec4be}


register a function that is called when a buffer is sent (or an \doxyref{error}{p.}{structerror} occured while sending) on the i2c bus. The event function is always called by software if the \doxyref{i2c\_\-send()}{p.}{i2c_8c_560d3f33dacd2199dd9bac3a5a7afe57} function returned 0. The parameter of the event function is the \doxyref{error}{p.}{structerror} code:\begin{itemize}
\item $<$0 if 0 byte has been transmitted (arbiteration lost)\item Else, the number of transmitted bytes is given, including the one that was not acked. \end{itemize}


Definition at line 187 of file i2c.c.

References IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-release\_\-bus@{i2c\_\-release\_\-bus}}
\index{i2c\_\-release\_\-bus@{i2c\_\-release\_\-bus}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-release\_\-bus]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-release\_\-bus (void)}\label{i2c_8c_5222b4067cf31018763371bbd2021115}


release the bus 

Definition at line 320 of file i2c.c.\index{i2c.c@{i2c.c}!i2c\_\-rerecv@{i2c\_\-rerecv}}
\index{i2c\_\-rerecv@{i2c\_\-rerecv}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-rerecv]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t i2c\_\-rerecv (void)}\label{i2c_8c_753fca9ef38792bb6296496b4a3d56cf}


same but for recv 

Definition at line 311 of file i2c.c.

References i2c\_\-recv().\index{i2c.c@{i2c.c}!i2c\_\-resend@{i2c\_\-resend}}
\index{i2c\_\-resend@{i2c\_\-resend}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-resend]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t i2c\_\-resend (void)}\label{i2c_8c_7db23666169c7abe9c0335a50667532f}


Resend the same buffer. This call is equivalent to \doxyref{i2c\_\-send()}{p.}{i2c_8c_560d3f33dacd2199dd9bac3a5a7afe57} with the same parameters as the last call. It safe to call it from the send\_\-event, but else the send buffer may have been overwritten. 

Definition at line 302 of file i2c.c.

References i2c\_\-send().\index{i2c.c@{i2c.c}!i2c\_\-reset@{i2c\_\-reset}}
\index{i2c\_\-reset@{i2c\_\-reset}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-reset]{\setlength{\rightskip}{0pt plus 5cm}void i2c\_\-reset (void)}\label{i2c_8c_f85c47697c983de6b37a6d5d44a3001d}


recover from \doxyref{error}{p.}{structerror} state 

Definition at line 329 of file i2c.c.

References I2C\_\-STATUS\_\-READY, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-send@{i2c\_\-send}}
\index{i2c\_\-send@{i2c\_\-send}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-send]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t i2c\_\-send (uint8\_\-t {\em dest\_\-add}, \/  uint8\_\-t $\ast$ {\em buf}, \/  uint8\_\-t {\em size}, \/  uint8\_\-t {\em ctrl})}\label{i2c_8c_560d3f33dacd2199dd9bac3a5a7afe57}


Send a buffer. Return 0 if xmit starts correctly. On \doxyref{error}{p.}{structerror}, return $<$ 0.\begin{itemize}
\item If mode is slave, dest\_\-add should be I2C\_\-ADD\_\-MASTER, and transmission starts when the master transmits a clk.\item If mode is master and if dest\_\-add != I2C\_\-ADD\_\-MASTER, it will transmit a START condition if bus is available (the uc will act as a master)\item If mode is master and if dest\_\-add == I2C\_\-ADD\_\-MASTER, the uC will act as a slave, and data will be sent when the uC will be addressed. The transmission will be processed with these params until a \doxyref{i2c\_\-flush()}{p.}{i2c_8c_b70b41e07d082581ea209e319fb32d40} is called. The 'ctrl' parameter is composed by the flags I2C\_\-CTRL\_\-SYNC and I2C\_\-CTRL\_\-DONT\_\-RELEASE\_\-BUS \end{itemize}


Definition at line 212 of file i2c.c.

References EBUSY, EINVAL, ENXIO, ESUCCESS, I2C\_\-ADD\_\-MASTER, I2C\_\-CTRL\_\-SYNC, I2C\_\-MODE\_\-SLAVE, I2C\_\-MODE\_\-UNINIT, I2C\_\-STATUS\_\-MASTER\_\-RECV, I2C\_\-STATUS\_\-MASTER\_\-XMIT, I2C\_\-STATUS\_\-OP\_\-FINISHED, I2C\_\-STATUS\_\-SLAVE\_\-RECV, I2C\_\-STATUS\_\-SLAVE\_\-XMIT, I2C\_\-STATUS\_\-SLAVE\_\-XMIT\_\-WAIT, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.

Referenced by i2c\_\-resend().\index{i2c.c@{i2c.c}!i2c\_\-set\_\-recv\_\-size@{i2c\_\-set\_\-recv\_\-size}}
\index{i2c\_\-set\_\-recv\_\-size@{i2c\_\-set\_\-recv\_\-size}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-set\_\-recv\_\-size]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t i2c\_\-set\_\-recv\_\-size (uint8\_\-t {\em size})}\label{i2c_8c_ce99548d1f29c7e06a3ed769673e0e9b}


In MASTER RECEIVER mode, it is possible that the user application does not know the size of the buffer. You can adjust this size during transmission (generally the size can be specified at the beginning of received data, so the user app can be notified thanks to recv\_\-byte\_\-event(). Note that \doxyref{i2c\_\-set\_\-recv\_\-size()}{p.}{i2c_8c_ce99548d1f29c7e06a3ed769673e0e9b} function has to be used with careful, making sure you understand i2c protocol and this code. Return 0 on success. Note than in SLAVE RECEIVER mode, you don't have to use this function, because the master can end the transmission by sending a stop condition on the bus. 

Definition at line 447 of file i2c.c.

References EBUSY, EINVAL, ESUCCESS, I2C\_\-STATUS\_\-MASTER\_\-RECV, IRQ\_\-LOCK, and IRQ\_\-UNLOCK.\index{i2c.c@{i2c.c}!i2c\_\-status@{i2c\_\-status}}
\index{i2c\_\-status@{i2c\_\-status}!i2c.c@{i2c.c}}
\subsubsection[i2c\_\-status]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t i2c\_\-status (void)}\label{i2c_8c_d7adbd3b7a1d80828814ab11d574b988}


return the status of the i2c module. 

Definition at line 486 of file i2c.c.\index{i2c.c@{i2c.c}!SIGNAL@{SIGNAL}}
\index{SIGNAL@{SIGNAL}!i2c.c@{i2c.c}}
\subsubsection[SIGNAL]{\setlength{\rightskip}{0pt plus 5cm}SIGNAL (SIG\_\-2WIRE\_\-SERIAL)}\label{i2c_8c_76216358046f104b52928cb3841a144e}


interrupt Interrupt routing for I2C. Refer to datasheets for more informations. 

Definition at line 538 of file i2c.c.

References EAGAIN, ENOENT, I2C\_\-CTRL\_\-DONT\_\-RELEASE\_\-BUS, I2C\_\-CTRL\_\-SYNC, I2C\_\-STATUS\_\-MASTER\_\-RECV, I2C\_\-STATUS\_\-MASTER\_\-XMIT, I2C\_\-STATUS\_\-NEED\_\-RECV\_\-EVT, I2C\_\-STATUS\_\-NEED\_\-XMIT\_\-EVT, I2C\_\-STATUS\_\-OP\_\-FINISHED, I2C\_\-STATUS\_\-SLAVE\_\-RECV, I2C\_\-STATUS\_\-SLAVE\_\-XMIT, and I2C\_\-STATUS\_\-SLAVE\_\-XMIT\_\-WAIT.