Wiblocks --- TWI Library

Two Wire Interface Library

The TWI library is derived from the Atmel C-code described in AVR315: Using the TWI module as I2C master. To use this C code in a C++ program include the code as an external C block. For example ---


extern "C" 
{
#include <inttypes.h>
#include "WConstants.h"
#include "TWI.h"
}

TWI/TWI.c File Reference

#include <TWI.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include "WConstants.h"
#include <inttypes.h>

Go to the source code of this file.

Functions

unsigned char twi_busy_p (void)
 Call this function to test if the TWI_ISR is busy transmitting.
unsigned char twi_get_state (void)
 Returns the state of TWI interface.
void twi_transmit (unsigned char *msg, unsigned char msg_size)
 Call this function to send a prepared message.
void twi_resend (void)
unsigned char twi_get_data (unsigned char *msg, unsigned char msg_size)
 ISR (TWI_vect)

Variables

union twi_status_reg twi_status_reg = {0}

Detailed Description

From AVR315 TWI Master Implementation, by ltwa, March 24, 2004

Definition in file TWI.c.

Function Documentation

ISR ( TWI_vect   ) 

This function is the Interrupt Service Routine (ISR), and called when the TWI interrupt is triggered; that is whenever a TWI event has occurred. This function should not be called directly from the main application.

Definition at line 133 of file TWI.c.

unsigned char twi_busy_p ( void   ) 

Call this function to test if the TWI_ISR is busy transmitting.

Returns:
a true value if the TWI interrupt is enabled and the transceiver is busy

Definition at line 28 of file TWI.c.

unsigned char twi_get_data ( unsigned char *  msg,
unsigned char  msg_size 
)

Call this function to read out the requested data from the TWI transceiver buffer. I.e. first call TWI_Start_Transceiver to send a request for data to the slave. Then Run this function to collect the data when they have arrived. Include a pointer to where to place the data and the number of bytes requested (including the address field) in the function call. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, before reading out the data and returning. If there was an error in the previous transmission the function will return the TWI error code.

Definition at line 112 of file TWI.c.

unsigned char twi_get_state ( void   ) 

Returns the state of TWI interface.

Call this function to fetch the state information of the previous operation. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation. If there was an error, then the function will return the TWI State code.

Returns:
the state of the TWI interface

Definition at line 42 of file TWI.c.

void twi_resend ( void   ) 

Call this function to resend the last message. The driver will reuse the data previously put in the transceiver buffers. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, then initialize the next operation and return.

Definition at line 88 of file TWI.c.

void twi_transmit ( unsigned char *  msg,
unsigned char  msg_size 
)

Call this function to send a prepared message.

The first byte must contain the slave address and the read/write bit. Consecutive bytes contain the data to be sent, or empty locations for data to be read from the slave. Also include how many bytes that should be sent/read including the address byte. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, then initialize the next operation and return.

Parameters:
[in] *msg address in memory where the message begins
[in] msg_size number of chars in the message

Definition at line 60 of file TWI.c.