Defines | |
#define | I2C_BIT_RATE_DIVIDER 0x0C |
Set bit rate 12 = 100kbit/s (max speed setting is 10 for an 8 MHz clock). It is a divider, so the lower the number the faster the speed. | |
Functions | |
RING_BUFFER_NEW (i2c_buffer, 128, char, i2c_write_buff, i2c_addr_buff) | |
int | i2c_init (char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) |
Initializes the i2c module. | |
int | i2c_send (char dest, char *data, size_t bytes) |
Sends a byte array over I2C as a master. | |
int | i2c_request (char dest) |
Send a master request to the destination. |
Initiates I2C functions on an ATMega128 which has a fully hardware Two Wire Interface (TWI) module. Any Atmel chip with this hardware should be able to use the software.
This code will operate in a multi-master enviornment and can be either a slave or a master at any time (as long as they are not one or the other at the moment. You can queue up multiple transmission modes in the buffer up to the buffer size. The buffer is implemented as a ring buffer.
It is implemented using callback functions. Whenever you want to send a packet you can call the built in send function (as a master) and it will send an array of bytes. Master recieve and slave send/receive are all handled by the call back functions. It is up to the end user to create functions that will handle the receiving of packets. Their functions will be called with every byte recieved so you must either buffer the inputs or handle each one separately.
On errors we will simply flush the entire buffer.
For information on how I2C operates, read the wikipedia article http://en.wikipedia.org/wiki/I2c for a good explanation of how it works.
int i2c_init | ( | char | addr, | |
fun_mrecv_t | master_recv, | |||
fun_srecv_t | slave_recv, | |||
fun_send_t | slave_send | |||
) |
Initializes the i2c module.
Initializes the I2C module to start listening on the i2c lines. If the callback functions are not set to null they will be called when that transmission mode is called. The address is your address that you will listen to when you are not the master.
addr | Your address on the I2C bus. | |
master_recv | The address of the function to call when you receive a byte when you are a master. | |
slave_recv | The address of the function to call when you are a slave you receive data from the master | |
slave_send | The address of the function to call when you are a slave and the master requests data from you. |
int i2c_request | ( | char | dest | ) |
Send a master request to the destination.
Sends a request of data from the target address and calls the callback function to handle data as it comes in. This function will not work if the slave has not informationt to send or has nothing implemented to send it.
dest | The destination that we want to receive information from. |
int i2c_send | ( | char | dest, | |
char * | data, | |||
size_t | bytes | |||
) |
Sends a byte array over I2C as a master.
Will perform a send over I2C to the destination from data for the ammount of bytes that bytes is.
dest | Destination address of the data on the I2C bus. | |
data | The pointer to the byte array of data | |
bytes | The amount of bytes long that the byte array is. This is how many bytes from the array that the function will send. |