#include <aversive.h>
#include <i2c_config.h>
Go to the source code of this file.
Defines | |
#define | I2C_DEBUG 1 |
#define | I2C_ADD_GENCALL 0x00 |
#define | I2C_ADD_MASTER 0x80 |
#define | I2C_CTRL_GENERIC 0x00 |
#define | I2C_CTRL_SYNC 0x01 |
#define | I2C_CTRL_DONT_RELEASE_BUS 0x02 |
#define | I2C_STATUS_READY 0x00 |
#define | I2C_STATUS_MASTER_XMIT 0x01 |
#define | I2C_STATUS_MASTER_RECV 0x02 |
#define | I2C_STATUS_SLAVE_XMIT_WAIT 0x04 |
#define | I2C_STATUS_SLAVE_XMIT 0x08 |
#define | I2C_STATUS_SLAVE_RECV 0x10 |
#define | I2C_STATUS_OP_FINISHED 0x20 |
#define | I2C_STATUS_NEED_XMIT_EVT 0x40 |
#define | I2C_STATUS_NEED_RECV_EVT 0x80 |
Enumerations | |
enum | i2c_mode_t { I2C_MODE_UNINIT, I2C_MODE_SLAVE } |
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) |
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) |
void | i2c_reset (void) |
void | i2c_debug (void) |
#define I2C_ADD_MASTER 0x80 |
#define I2C_CTRL_DONT_RELEASE_BUS 0x02 |
when the operation is finished as a master, don't release the bus (don't send any stop condition). You can send several commands without beeing interrupted by another master. To release it, send another command without this flag or just call i2c_release_bus(). This has no effect on a slave.
Definition at line 89 of file i2c.h.
Referenced by SIGNAL().
#define I2C_CTRL_SYNC 0x01 |
Error code is returned instead of beeing sent as a callback. Note that the send_event callback is _not_ called if it is registered. This functions waits and only returns when the transmission is finished or if it failed. Note that there is no timeout, so it can loop forever... WARNING : irq MUST be allowed
Definition at line 83 of file i2c.h.
Referenced by i2c_recv(), i2c_send(), and SIGNAL().
#define I2C_DEBUG 1 |
please read carefully !
this implementation of the i2c interface is very specific this is a multi bus implementation. The multi bus operation is done by implementing software i2c modules (slave only)
HARDWARE I2C : --------------
This module implements i2c using the hardware twi interface of AVR devices. It can operates in slave and/or master mode, depending on the initialisation parameter. This module is interrupt driven only.
In master mode, buffer can be transmitted/received on demand (with start and stop conditions). In slave mode, operations are done asynchronously. Like some other modules in aversive, callback functions can be registered and are called on transmission or reception events.
This module should support multimaster mode, send() or recv() depends on the address parameter.
#define I2C_STATUS_MASTER_RECV 0x02 |
Definition at line 95 of file i2c.h.
Referenced by i2c_get_recv_buffer(), i2c_recv(), i2c_send(), i2c_set_recv_size(), and SIGNAL().
#define I2C_STATUS_MASTER_XMIT 0x01 |
#define I2C_STATUS_OP_FINISHED 0x20 |
#define I2C_STATUS_READY 0x00 |
#define I2C_STATUS_SLAVE_RECV 0x10 |
Definition at line 98 of file i2c.h.
Referenced by i2c_get_recv_buffer(), i2c_recv(), i2c_send(), and SIGNAL().
#define I2C_STATUS_SLAVE_XMIT 0x08 |
#define I2C_STATUS_SLAVE_XMIT_WAIT 0x04 |
enum i2c_mode_t |
void i2c_debug | ( | void | ) |
display debug infos
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. The 'ctrl' parameter is composed by the flags I2C_CTRL_SYNC and I2C_CTRL_DONT_RELEASE_BUS
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_size value, which is the number of bytes to be received in the frame: this can be done by calling i2c_set_recv_size().
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:
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 states
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 | ) |