#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) |
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 | ) |
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:
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:
Definition at line 187 of file i2c.c.
References IRQ_LOCK, and IRQ_UNLOCK.
int8_t i2c_rerecv | ( | void | ) |
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.
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 | ) |
SIGNAL | ( | SIG_2WIRE_SERIAL | ) |
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.