aversive_10-03-12/modules/comm/i2c/i2c.c File Reference

#include <stdlib.h>
#include <string.h>
#include <util/twi.h>
#include <autoconf.h>
#include <aversive/errno.h>
#include <i2c.h>

Go to the source code of this file.

Functions

void i2c_init (i2c_mode_t mode, uint8_t add)
void i2c_register_recv_event (void(*event)(uint8_t *, int8_t))
void i2c_register_recv_byte_event (void(*event)(uint8_t, uint8_t, uint8_t))
void i2c_register_send_event (void(*event)(int8_t))
int8_t i2c_send (uint8_t dest_add, uint8_t *buf, uint8_t size, uint8_t ctrl)
int8_t i2c_resend (void)
int8_t i2c_rerecv (void)
void i2c_release_bus (void)
void i2c_reset (void)
int8_t i2c_recv (uint8_t dest_add, uint8_t size, uint8_t ctrl)
int8_t i2c_flush (void)
uint8_t i2c_set_recv_size (uint8_t size)
i2c_mode_t i2c_mode (void)
uint8_t i2c_status (void)
uint8_t i2c_get_recv_buffer (uint8_t *buf, uint8_t size)
 SIGNAL (SIG_2WIRE_SERIAL)


Function Documentation

int8_t i2c_flush ( void   ) 

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 i2c_send() 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.

uint8_t i2c_get_recv_buffer ( uint8_t *  buf,
uint8_t  size 
)

Copy the received buffer in the buffer given as parameter. Return number of copied bytes or < 0 on error.

Definition at line 495 of file i2c.c.

References EBUSY, I2C_STATUS_MASTER_RECV, I2C_STATUS_SLAVE_RECV, IRQ_LOCK, and IRQ_UNLOCK.

void i2c_init ( i2c_mode_t  mode,
uint8_t  add 
)

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.

i2c_mode_t i2c_mode ( void   ) 

return the current mode of the i2c module.

Definition at line 478 of file i2c.c.

int8_t i2c_recv ( uint8_t  dest_add,
uint8_t  size,
uint8_t  ctrl 
)

In slave mode, it returns error 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().

void i2c_register_recv_byte_event ( void(*)(uint8_t, uint8_t, uint8_t)  event  ) 

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 i2c_set_recv_size().

Definition at line 169 of file i2c.c.

References IRQ_LOCK, and IRQ_UNLOCK.

void i2c_register_recv_event ( void(*)(uint8_t *, int8_t)  event  ) 

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:

  • (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.
  • (NULL, err<0) if the transmission failed (slave not answering or arbiteration lost). The first parameter is NULL and the second contains the error code.

Definition at line 153 of file i2c.c.

References IRQ_LOCK, and IRQ_UNLOCK.

void i2c_register_send_event ( void(*)(int8_t)  event  ) 

register a function that is called when a buffer is sent (or an error occured while sending) on the i2c bus. The event function is always called by software if the i2c_send() function returned 0. The parameter of the event function is the error code:

  • <0 if 0 byte has been transmitted (arbiteration lost)
  • Else, the number of transmitted bytes is given, including the one that was not acked.

Definition at line 187 of file i2c.c.

References IRQ_LOCK, and IRQ_UNLOCK.

void i2c_release_bus ( void   ) 

release the bus

Definition at line 320 of file i2c.c.

int8_t i2c_rerecv ( void   ) 

same but for recv

Definition at line 311 of file i2c.c.

References i2c_recv().

int8_t i2c_resend ( void   ) 

Resend the same buffer. This call is equivalent to i2c_send() 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().

void i2c_reset ( void   ) 

recover from error state

Definition at line 329 of file i2c.c.

References I2C_STATUS_READY, IRQ_LOCK, and IRQ_UNLOCK.

int8_t i2c_send ( uint8_t  dest_add,
uint8_t *  buf,
uint8_t  size,
uint8_t  ctrl 
)

Send a buffer. Return 0 if xmit starts correctly. On error, return < 0.

  • If mode is slave, dest_add should be I2C_ADD_MASTER, and transmission starts when the master transmits a clk.
  • 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)
  • 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 i2c_flush() is called. The 'ctrl' parameter is composed by the flags I2C_CTRL_SYNC and I2C_CTRL_DONT_RELEASE_BUS

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().

uint8_t i2c_set_recv_size ( uint8_t  size  ) 

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 i2c_set_recv_size() 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.

uint8_t i2c_status ( void   ) 

return the status of the i2c module.

Definition at line 486 of file i2c.c.

SIGNAL ( SIG_2WIRE_SERIAL   ) 


Generated on Fri Mar 12 06:32:05 2010 for AVR by  doxygen 1.5.6