i2c.c File Reference

Implemenation of I2C communications protocol. More...

#include <avr/interrupt.h>
#include <util/twi.h>
#include "i2c.h"
#include "ring_buffer.h"

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.
 ISR (TWI_vect)
 Interrupt to handle I2C interrupts from the I2C hardware.


Detailed Description

Implemenation of I2C communications protocol.

Copyright (c) 2007 Colony Project

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

In the case where you have master sends and then a master request to the same address, you will not give up control of the line because the send and request addresses are seen as different addresses. In between it will send a restart but will not give up the line.

Author:
CMU Robotics Club, Kevin Woo, Sursh Nidhiry
Bug:
Not tested.

Function Documentation

ISR ( TWI_vect   ) 

Interrupt to handle I2C interrupts from the I2C hardware.

Uses the status codes from the I2C register to handle the events needed to advance in I2C stages. For instance, you will get a bit for receiving a start ack, then a address ack, then a data ack, etc. The events are handled in each switch case. The status codes are defined by avr-gcc in /util/twi.h but are the same codes as the Atmel documentation.

Bytes are sent by popping off the ring buffer. It also will keep track of what modes the send is in.

Errors are handled here as well.


Generated on Mon Oct 6 15:44:33 2008 for libdragonfly by  doxygen 1.5.4