XMEGA_TWI.zip

ATxmega TWI - nie umiem obsłużyć tej magistrali po przesiadce z Mega

Witam Oto przykład Atmela. Niestety, nie mam czasu by go sprawdzić. Oraz sprawdzony (otrzymałem razem z płytką XMegi)

  • XMEGA_TWI.zip
    • twi_master_driver.h
    • twi_master_driver.c
    • twi_example.c
    • documentation.h
    • config.doxygen
    • avr1308.eww
    • twi_slave_driver.h
    • avr_compiler.h
    • Array
    • twi_example.ewp
    • readme.html
    • twi_slave_driver.c


Download file - link to post

XMEGA_TWI.zip > twi_master_driver.h

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief XMEGA TWI master driver header file.
*
* This file contains the function prototypes and enumerator definitions
* for various configuration parameters for the XMEGA TWI master driver.
*
* The driver is not intended for size and/or speed critical code, since
* most functions are just a few lines of code, and the function call
* overhead would decrease code performance. The driver is intended for
* rapid prototyping and documentation purposes for getting started with
* the XMEGA TWI master module.
*
* For size and/or speed critical code, it is recommended to copy the
* function contents directly into your application instead of making
* a function call.
*
* \par Application note:
* AVR1308: Using the XMEGA TWI
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 1569 $
* $Date: 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef TWI_MASTER_DRIVER_H
#define TWI_MASTER_DRIVER_H

#include " avr_compiler.h "

/*! Baud register setting calculation. Formula described in datasheet. */
#define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5)


/*! Transaction status defines. */
#define TWIM_STATUS_READY 0
#define TWIM_STATUS_BUSY 1


/*! Transaction result enumeration. */
typedef enum TWIM_RESULT_enum {
TWIM_RESULT_UNKNOWN = (0x00 & lt; & lt; 0),
TWIM_RESULT_OK = (0x01 & lt; & lt; 0),
TWIM_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt; 0),
TWIM_RESULT_ARBITRATION_LOST = (0x03 & lt; & lt; 0),
TWIM_RESULT_BUS_ERROR = (0x04 & lt; & lt; 0),
TWIM_RESULT_NACK_RECEIVED = (0x05 & lt; & lt; 0),
TWIM_RESULT_FAIL = (0x06 & lt; & lt; 0),
} TWIM_RESULT_t;

/*! Buffer size defines */
#define TWIM_WRITE_BUFFER_SIZE 8
#define TWIM_READ_BUFFER_SIZE 8


/*! \brief TWI master driver struct
*
* TWI master struct. Holds pointer to TWI module,
* buffers and necessary varibles.
*/
typedef struct TWI_Master {
TWI_t *interface; /*! & lt; Pointer to what interface to use */
register8_t address; /*! & lt; Slave address */
register8_t writeData[TWIM_WRITE_BUFFER_SIZE]; /*! & lt; Data to write */
register8_t readData[TWIM_READ_BUFFER_SIZE]; /*! & lt; Read data */
register8_t bytesToWrite; /*! & lt; Number of bytes to write */
register8_t bytesToRead; /*! & lt; Number of bytes to read */
register8_t bytesWritten; /*! & lt; Number of bytes written */
register8_t bytesRead; /*! & lt; Number of bytes read */
register8_t status; /*! & lt; Status of transaction */
register8_t result; /*! & lt; Result of transaction */
}TWI_Master_t;



void TWI_MasterInit(TWI_Master_t *twi,
TWI_t *module,
TWI_MASTER_INTLVL_t intLevel,
uint8_t baudRateRegisterSetting);
TWI_MASTER_BUSSTATE_t TWI_MasterState(TWI_Master_t *twi);
bool TWI_MasterReady(TWI_Master_t *twi);
bool TWI_MasterWrite(TWI_Master_t *twi,
uint8_t address,
uint8_t * writeData,
uint8_t bytesToWrite);
bool TWI_MasterRead(TWI_Master_t *twi,
uint8_t address,
uint8_t bytesToRead);
bool TWI_MasterWriteRead(TWI_Master_t *twi,
uint8_t address,
uint8_t *writeData,
uint8_t bytesToWrite,
uint8_t bytesToRead);
void TWI_MasterInterruptHandler(TWI_Master_t *twi);
void TWI_MasterArbitrationLostBusErrorHandler(TWI_Master_t *twi);
void TWI_MasterWriteHandler(TWI_Master_t *twi);
void TWI_MasterReadHandler(TWI_Master_t *twi);
void TWI_MasterTransactionFinished(TWI_Master_t *twi, uint8_t result);


/*! TWI master interrupt service routine.
*
* Interrupt service routine for the TWI master. Copy the needed vectors
* into your code.
*
ISR(TWIC_TWIM_vect)
{
TWI_MasterInterruptHandler( & twiMaster);
}

*
*/

#endif /* TWI_MASTER_DRIVER_H */


XMEGA_TWI.zip > twi_master_driver.c

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief
* XMEGA TWI master driver source file.
*
* This file contains the function implementations the XMEGA master TWI
* driver.
*
* The driver is not intended for size and/or speed critical code, since
* most functions are just a few lines of code, and the function call
* overhead would decrease code performance. The driver is intended for
* rapid prototyping and documentation purposes for getting started with
* the XMEGA TWI master module.
*
* For size and/or speed critical code, it is recommended to copy the
* function contents directly into your application instead of making
* a function call.
*
* Several functions use the following construct:
* " some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ... "
* Although the use of the ternary operator ( if ? then : else ) is
* discouraged, in some occasions the operator makes it possible to write
* pretty clean and neat code. In this driver, the construct is used to
* set or not set a configuration bit based on a boolean input parameter,
* such as the " some_parameter " in the example above.
*
* \par Application note:
* AVR1308: Using the XMEGA TWI
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 1569 $
* $Date: 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#include " twi_master_driver.h "


/*! \brief Initialize the TWI module.
*
* TWI module initialization function.
* Enables master read and write interrupts.
* Remember to enable interrupts globally from the main application.
*
* \param twi The TWI_Master_t struct instance.
* \param module The TWI module to use.
* \param intLevel Master interrupt level.
* \param baudRateRegisterSetting The baud rate register value.
*/
void TWI_MasterInit(TWI_Master_t *twi,
TWI_t *module,
TWI_MASTER_INTLVL_t intLevel,
uint8_t baudRateRegisterSetting)
{
twi- & gt; interface = module;
twi- & gt; interface- & gt; MASTER.CTRLA = intLevel |
TWI_MASTER_RIEN_bm |
TWI_MASTER_WIEN_bm |
TWI_MASTER_ENABLE_bm;
twi- & gt; interface- & gt; MASTER.BAUD = baudRateRegisterSetting;
twi- & gt; interface- & gt; MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
}


/*! \brief Returns the TWI bus state.
*
* Returns the TWI bus state (type defined in device headerfile),
* unknown, idle, owner or busy.
*
* \param twi The TWI_Master_t struct instance.
*
* \retval TWI_MASTER_BUSSTATE_UNKNOWN_gc Bus state is unknown.
* \retval TWI_MASTER_BUSSTATE_IDLE_gc Bus state is idle.
* \retval TWI_MASTER_BUSSTATE_OWNER_gc Bus state is owned by the master.
* \retval TWI_MASTER_BUSSTATE_BUSY_gc Bus state is busy.
*/
TWI_MASTER_BUSSTATE_t TWI_MasterState(TWI_Master_t *twi)
{
TWI_MASTER_BUSSTATE_t twi_status;
twi_status = (TWI_MASTER_BUSSTATE_t) (twi- & gt; interface- & gt; MASTER.STATUS &
TWI_MASTER_BUSSTATE_gm);
return twi_status;
}


/*! \brief Returns true if transaction is ready.
*
* This function returns a boolean whether the TWI Master is ready
* for a new transaction.
*
* \param twi The TWI_Master_t struct instance.
*
* \retval true If transaction could be started.
* \retval false If transaction could not be started.
*/
bool TWI_MasterReady(TWI_Master_t *twi)
{
bool twi_status = (twi- & gt; status & TWIM_STATUS_READY);
return twi_status;
}


/*! \brief TWI write transaction.
*
* This function is TWI Master wrapper for a write-only transaction.
*
* \param twi The TWI_Master_t struct instance.
* \param address Slave address.
* \param writeData Pointer to data to write.
* \param bytesToWrite Number of data bytes to write.
*
* \retval true If transaction could be started.
* \retval false If transaction could not be started.
*/
bool TWI_MasterWrite(TWI_Master_t *twi,
uint8_t address,
uint8_t *writeData,
uint8_t bytesToWrite)
{
bool twi_status = TWI_MasterWriteRead(twi, address, writeData, bytesToWrite, 0);
return twi_status;
}


/*! \brief TWI read transaction.
*
* This function is a TWI Maste wrapper for read-only transaction.
*
* \param twi The TWI_Master_t struct instance.
* \param address The slave address.
* \param bytesToRead The number of bytes to read.
*
* \retval true If transaction could be started.
* \retval false If transaction could not be started.
*/
bool TWI_MasterRead(TWI_Master_t *twi,
uint8_t address,
uint8_t bytesToRead)
{
bool twi_status = TWI_MasterWriteRead(twi, address, 0, 0, bytesToRead);
return twi_status;
}


/*! \brief TWI write and/or read transaction.
*
* This function is a TWI Master write and/or read transaction. The function
* can be used to both write and/or read bytes to/from the TWI Slave in one
* transaction.
*
* \param twi The TWI_Master_t struct instance.
* \param address The slave address.
* \param writeData Pointer to data to write.
* \param bytesToWrite Number of bytes to write.
* \param bytesToRead Number of bytes to read.
*
* \retval true If transaction could be started.
* \retval false If transaction could not be started.
*/
bool TWI_MasterWriteRead(TWI_Master_t *twi,
uint8_t address,
uint8_t *writeData,
uint8_t bytesToWrite,
uint8_t bytesToRead)
{
/*Parameter sanity check. */
if (bytesToWrite & gt; TWIM_WRITE_BUFFER_SIZE) {
return false;
}
if (bytesToRead & gt; TWIM_READ_BUFFER_SIZE) {
return false;
}

/*Initiate transaction if bus is ready. */
if (twi- & gt; status == TWIM_STATUS_READY) {

twi- & gt; status = TWIM_STATUS_BUSY;
twi- & gt; result = TWIM_RESULT_UNKNOWN;

twi- & gt; address = address & lt; & lt; 1;

/* Fill write data buffer. */
for (uint8_t bufferIndex=0; bufferIndex & lt; bytesToWrite; bufferIndex++) {
twi- & gt; writeData[bufferIndex] = writeData[bufferIndex];
}

twi- & gt; bytesToWrite = bytesToWrite;
twi- & gt; bytesToRead = bytesToRead;
twi- & gt; bytesWritten = 0;
twi- & gt; bytesRead = 0;

/* If write command, send the START condition + Address +
* 'R/_W = 0'
*/
if (twi- & gt; bytesToWrite & gt; 0) {
uint8_t writeAddress = twi- & gt; address & ~0x01;
twi- & gt; interface- & gt; MASTER.ADDR = writeAddress;
}

/* If read command, send the START condition + Address +
* 'R/_W = 1'
*/
else if (twi- & gt; bytesToRead & gt; 0) {
uint8_t readAddress = twi- & gt; address | 0x01;
twi- & gt; interface- & gt; MASTER.ADDR = readAddress;
}
return true;
} else {
return false;
}
}


/*! \brief Common TWI master interrupt service routine.
*
* Check current status and calls the appropriate handler.
*
* \param twi The TWI_Master_t struct instance.
*/
void TWI_MasterInterruptHandler(TWI_Master_t *twi)
{
uint8_t currentStatus = twi- & gt; interface- & gt; MASTER.STATUS;

/* If arbitration lost or bus error. */
if ((currentStatus & TWI_MASTER_ARBLOST_bm) ||
(currentStatus & TWI_MASTER_BUSERR_bm)) {

TWI_MasterArbitrationLostBusErrorHandler(twi);
}

/* If master write interrupt. */
else if (currentStatus & TWI_MASTER_WIF_bm) {
TWI_MasterWriteHandler(twi);
}

/* If master read interrupt. */
else if (currentStatus & TWI_MASTER_RIF_bm) {
TWI_MasterReadHandler(twi);
}

/* If unexpected state. */
else {
TWI_MasterTransactionFinished(twi, TWIM_RESULT_FAIL);
}
}


/*! \brief TWI master arbitration lost and bus error interrupt handler.
*
* Handles TWI responses to lost arbitration and bus error.
*
* \param twi The TWI_Master_t struct instance.
*/
void TWI_MasterArbitrationLostBusErrorHandler(TWI_Master_t *twi)
{
uint8_t currentStatus = twi- & gt; interface- & gt; MASTER.STATUS;

/* If bus error. */
if (currentStatus & TWI_MASTER_BUSERR_bm) {
twi- & gt; result = TWIM_RESULT_BUS_ERROR;
}
/* If arbitration lost. */
else {
twi- & gt; result = TWIM_RESULT_ARBITRATION_LOST;
}

/* Clear interrupt flag. */
twi- & gt; interface- & gt; MASTER.STATUS = currentStatus | TWI_MASTER_ARBLOST_bm;

twi- & gt; status = TWIM_STATUS_READY;
}


/*! \brief TWI master write interrupt handler.
*
* Handles TWI transactions (master write) and responses to (N)ACK.
*
* \param twi The TWI_Master_t struct instance.
*/
void TWI_MasterWriteHandler(TWI_Master_t *twi)
{
/* Local variables used in if tests to avoid compiler warning. */
uint8_t bytesToWrite = twi- & gt; bytesToWrite;
uint8_t bytesToRead = twi- & gt; bytesToRead;

/* If NOT acknowledged (NACK) by slave cancel the transaction. */
if (twi- & gt; interface- & gt; MASTER.STATUS & TWI_MASTER_RXACK_bm) {
twi- & gt; interface- & gt; MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
twi- & gt; result = TWIM_RESULT_NACK_RECEIVED;
twi- & gt; status = TWIM_STATUS_READY;
}

/* If more bytes to write, send data. */
else if (twi- & gt; bytesWritten & lt; bytesToWrite) {
uint8_t data = twi- & gt; writeData[twi- & gt; bytesWritten];
twi- & gt; interface- & gt; MASTER.DATA = data;
++twi- & gt; bytesWritten;
}

/* If bytes to read, send repeated START condition + Address +
* 'R/_W = 1'
*/
else if (twi- & gt; bytesRead & lt; bytesToRead) {
uint8_t readAddress = twi- & gt; address | 0x01;
twi- & gt; interface- & gt; MASTER.ADDR = readAddress;
}

/* If transaction finished, send STOP condition and set RESULT OK. */
else {
twi- & gt; interface- & gt; MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
TWI_MasterTransactionFinished(twi, TWIM_RESULT_OK);
}
}


/*! \brief TWI master read interrupt handler.
*
* This is the master read interrupt handler that takes care of
* reading bytes from the TWI slave.
*
* \param twi The TWI_Master_t struct instance.
*/
void TWI_MasterReadHandler(TWI_Master_t *twi)
{
/* Fetch data if bytes to be read. */
if (twi- & gt; bytesRead & lt; TWIM_READ_BUFFER_SIZE) {
uint8_t data = twi- & gt; interface- & gt; MASTER.DATA;
twi- & gt; readData[twi- & gt; bytesRead] = data;
twi- & gt; bytesRead++;
}

/* If buffer overflow, issue STOP and BUFFER_OVERFLOW condition. */
else {
twi- & gt; interface- & gt; MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
TWI_MasterTransactionFinished(twi, TWIM_RESULT_BUFFER_OVERFLOW);
}

/* Local variable used in if test to avoid compiler warning. */
uint8_t bytesToRead = twi- & gt; bytesToRead;

/* If more bytes to read, issue ACK and start a byte read. */
if (twi- & gt; bytesRead & lt; bytesToRead) {
twi- & gt; interface- & gt; MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
}

/* If transaction finished, issue NACK and STOP condition. */
else {
twi- & gt; interface- & gt; MASTER.CTRLC = TWI_MASTER_ACKACT_bm |
TWI_MASTER_CMD_STOP_gc;
TWI_MasterTransactionFinished(twi, TWIM_RESULT_OK);
}
}


/*! \brief TWI transaction finished handler.
*
* Prepares module for new transaction.
*
* \param twi The TWI_Master_t struct instance.
* \param result The result of the operation.
*/
void TWI_MasterTransactionFinished(TWI_Master_t *twi, uint8_t result)
{
twi- & gt; result = result;
twi- & gt; status = TWIM_STATUS_READY;
}


XMEGA_TWI.zip > twi_example.c

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief XMEGA TWI driver example source.
*
* This file contains an example application that demonstrates the TWI
* master and slave driver. It shows how to set up one TWI module as both
* master and slave, and communicate with itself.
*
* The recommended test setup for this application is to connect 10K
* pull-up resistors on PC0 (SDA) and PC1 (SCL). Connect a 10-pin cable
* between the PORTD and SWITCHES, and PORTE and LEDS.
*
* \par Application note:
* AVR1308: Using the XMEGA TWI
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 2660 $
* $Date: 2009-08-11 12:28:58 +0200 (ti, 11 aug 2009) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include " avr_compiler.h "
#include " twi_master_driver.h "
#include " twi_slave_driver.h "

/*! Defining an example slave address. */
#define SLAVE_ADDRESS 0x55

/*! Defining number of bytes in buffer. */
#define NUM_BYTES 8

/*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
#define CPU_SPEED 2000000
#define BAUDRATE 100000
#define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)


/* Global variables */
TWI_Master_t twiMaster; /*! & lt; TWI master module. */
TWI_Slave_t twiSlave; /*! & lt; TWI slave module. */


/*! Buffer with test data to send.*/
uint8_t sendBuffer[NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD};


/*! Simple function that invert the received value in the sendbuffer. This
* function is used in the driver and passed on as a pointer to the driver.
*/
void TWIC_SlaveProcessData(void)
{
uint8_t bufIndex = twiSlave.bytesReceived;
twiSlave.sendData[bufIndex] = (~twiSlave.receivedData[bufIndex]);
}


/*! /brief Example code
*
* Example code that reads the key pressed and show a value from the buffer,
* sends the value to the slave and read back the processed value which will
* be inverted and displayed after key release.
*/
int main(void)
{
/* Initialize PORTE for output and PORTD for inverted input. */
PORTE.DIRSET = 0xFF;
PORTD.DIRCLR = 0xFF;
PORTCFG.MPCMASK = 0xFF;
PORTD.PIN0CTRL |= PORT_INVEN_bm;
// PORTCFG.MPCMASK = 0xFF;
// PORTD.PIN0CTRL = (PORTD.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc;

// Enable internal pull-up on PC0, PC1.. Uncomment if you don't have external pullups
// PORTCFG.MPCMASK = 0x03; // Configure several PINxCTRL registers at the same time
// PORTC.PIN0CTRL = (PORTC.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches



/* Initialize TWI master. */
TWI_MasterInit( & twiMaster,
& TWIC,
TWI_MASTER_INTLVL_LO_gc,
TWI_BAUDSETTING);

/* Initialize TWI slave. */
TWI_SlaveInitializeDriver( & twiSlave, & TWIC, TWIC_SlaveProcessData);
TWI_SlaveInitializeModule( & twiSlave,
SLAVE_ADDRESS,
TWI_SLAVE_INTLVL_LO_gc);

/* Enable LO interrupt level. */
PMIC.CTRL |= PMIC_LOLVLEN_bm;
sei();

uint8_t BufPos = 0;
while (1) {
while(!PORTD.IN); /* Wait for user to press button */

switch(PORTD.IN){
case (PIN0_bm): BufPos = 0; break;
case (PIN1_bm): BufPos = 1; break;
case (PIN2_bm): BufPos = 2; break;
case (PIN3_bm): BufPos = 3; break;
case (PIN4_bm): BufPos = 4; break;
case (PIN5_bm): BufPos = 5; break;
case (PIN6_bm): BufPos = 6; break;
case (PIN7_bm): BufPos = 7; break;
default: break;
}

/* Show the byte to send while holding down the key. */
while(PORTD.IN != 0x00){
PORTE.OUT = sendBuffer[BufPos];
}

TWI_MasterWriteRead( & twiMaster,
SLAVE_ADDRESS,
& sendBuffer[BufPos],
1,
1);


while (twiMaster.status != TWIM_STATUS_READY) {
/* Wait until transaction is complete. */
}

/* Show the sent byte received and processed on LEDs. */
PORTE.OUT = (twiMaster.readData[0]);

while(PORTD.IN); /* Wait for user to release button */
}
}

/*! TWIC Master Interrupt vector. */
ISR(TWIC_TWIM_vect)
{
TWI_MasterInterruptHandler( & twiMaster);
}

/*! TWIC Slave Interrupt vector. */
ISR(TWIC_TWIS_vect)
{
TWI_SlaveInterruptHandler( & twiSlave);
}


XMEGA_TWI.zip > documentation.h

/* Doxygen documentation mainpage ********************************************/
/*! \mainpage
* \section intro Introduction
* This documents the firmware for the application note. \n
*
* \section compinfo Compilation Info
* This firmware was compiled with IAR Embedded Workbench 5.30.0 and WinAVR 20090313.\n
*
* To make the project in IAR EWAVR:\n
* Add the .c files (and .S90 files where applicable) for the given example to your
* project. Use device ATxmega128A1 and enable bit definitions in I/O include files,
* optimization low for debug target and high for release, output format: ubrof8 for
* Debug and intel_extended for Release, select Normal DLIB as library. \n
*
* To make the project in WinAVR:\n
* Add the .c files (and .S files where applicable) for the given example to your project.
* Use device ATxmega128A1, optimization low for debug target and high for release. \n
*
* \section deviceinfo Device Info
* All XMEGA devices with the targeted module can be used. The example is
* written for ATxmega128A1.
*
* \section contactinfo Contact Info
* For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n
* For application notes visit
* http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n
* Support mail: avr@atmel.com \n
*
* $Revision: 2661 $
* $Date: 2009-08-11 12:33:26 +0200 (ti, 11 aug 2009) $
*
* Copyright (c) 2009, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/


XMEGA_TWI.zip > twi_slave_driver.h

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief XMEGA TWI slave driver header file.
*
* This file contains the function prototypes and enumerator definitions
* for various configuration parameters for the XMEGA TWI slave driver.
*
* The driver is not intended for size and/or speed critical code, since
* most functions are just a few lines of code, and the function call
* overhead would decrease code performance. The driver is intended for
* rapid prototyping and documentation purposes for getting started with
* the XMEGA TWI slave module.
*
* For size and/or speed critical code, it is recommended to copy the
* function contents directly into your application instead of making
* a function call.
*
* \par Application note:
* AVR1307: Using the XMEGA TWI
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 1569 $
* $Date: 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef TWI_DRIVER_H
#define TWI_DRIVER_H

#include " avr_compiler.h "


/* Transaction status defines.*/
#define TWIS_STATUS_READY 0
#define TWIS_STATUS_BUSY 1

/* Transaction result enumeration */
typedef enum TWIS_RESULT_enum {
TWIS_RESULT_UNKNOWN = (0x00 & lt; & lt; 0),
TWIS_RESULT_OK = (0x01 & lt; & lt; 0),
TWIS_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt; 0),
TWIS_RESULT_TRANSMIT_COLLISION = (0x03 & lt; & lt; 0),
TWIS_RESULT_BUS_ERROR = (0x04 & lt; & lt; 0),
TWIS_RESULT_FAIL = (0x05 & lt; & lt; 0),
TWIS_RESULT_ABORTED = (0x06 & lt; & lt; 0),
} TWIS_RESULT_t;

/* Buffer size defines. */
#define TWIS_RECEIVE_BUFFER_SIZE 8
#define TWIS_SEND_BUFFER_SIZE 8



/*! \brief TWI slave driver struct.
*
* TWI slave struct. Holds pointer to TWI module and data processing routine,
* buffers and necessary varibles.
*/
typedef struct TWI_Slave {
TWI_t *interface; /*! & lt; Pointer to what interface to use*/
void (*Process_Data) (void); /*! & lt; Pointer to process data function*/
register8_t receivedData[TWIS_RECEIVE_BUFFER_SIZE]; /*! & lt; Read data*/
register8_t sendData[TWIS_SEND_BUFFER_SIZE]; /*! & lt; Data to write*/
register8_t bytesReceived; /*! & lt; Number of bytes received*/
register8_t bytesSent; /*! & lt; Number of bytes sent*/
register8_t status; /*! & lt; Status of transaction*/
register8_t result; /*! & lt; Result of transaction*/
bool abort; /*! & lt; Strobe to abort*/
} TWI_Slave_t;



void TWI_SlaveInitializeDriver(TWI_Slave_t *twi,
TWI_t *module,
void (*processDataFunction) (void));

void TWI_SlaveInitializeModule(TWI_Slave_t *twi,
uint8_t address,
TWI_SLAVE_INTLVL_t intLevel);

void TWI_SlaveInterruptHandler(TWI_Slave_t *twi);
void TWI_SlaveAddressMatchHandler(TWI_Slave_t *twi);
void TWI_SlaveStopHandler(TWI_Slave_t *twi);
void TWI_SlaveDataHandler(TWI_Slave_t *twi);
void TWI_SlaveReadHandler(TWI_Slave_t *twi);
void TWI_SlaveWriteHandler(TWI_Slave_t *twi);
void TWI_SlaveTransactionFinished(TWI_Slave_t *twi, uint8_t result);


/*! TWI slave interrupt service routine.
*
* Interrupt service routine for the TWI slave. Copy the interrupt vector
* into your code if needed.
*
ISR(TWIC_TWIS_vect)
{
TWI_SlaveInterruptHandler( & twiSlaveC);
}
*
*/


#endif /* TWI_DRIVER_H */


XMEGA_TWI.zip > avr_compiler.h

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief This file implements some macros that makes the IAR C-compiler and
* avr-gcc work with the same code base for the AVR architecture.
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 613 $
* $Date: 2006-04-07 14:40:07 +0200 (fr, 07 apr 2006) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/

#ifndef COMPILER_AVR_H
#define COMPILER_AVR_H

#ifndef F_CPU
/*! \brief Define default CPU frequency, if this is not already defined. */
#define F_CPU 2000000UL
#endif

#include & lt; stdint.h & gt;
#include & lt; stdbool.h & gt;
#include & lt; stdlib.h & gt;

/*! \brief This macro will protect the following code from interrupts. */
#define AVR_ENTER_CRITICAL_REGION( ) uint8_t volatile saved_sreg = SREG; \
cli();

/*! \brief This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION
* so the interrupts are enabled again.
*/
#define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;

#if defined( __ICCAVR__ )

#include & lt; inavr.h & gt;
#include & lt; ioavr.h & gt;
#include & lt; intrinsics.h & gt;
#include & lt; pgmspace.h & gt;

#ifndef __HAS_ELPM__
#define _MEMATTR __flash
#else /* __HAS_ELPM__ */
#define _MEMATTR __farflash
#endif /* __HAS_ELPM__ */

/*! \brief Perform a delay of \c us microseconds.
*
* The macro F_CPU is supposed to be defined to a constant defining the CPU
* clock frequency (in Hertz).
*
* The maximal possible delay is 262.14 ms / F_CPU in MHz.
*
* \note For the IAR compiler, currently F_CPU must be a
* multiple of 1000000UL (1 MHz).
*/
#define delay_us( us ) ( __delay_cycles( ( F_CPU / 1000000UL ) * ( us ) ) )

/*! \brief Preprocessor magic.
*
* Some preprocessor magic to allow for a header file abstraction of
* interrupt service routine declarations for the IAR compiler. This
* requires the use of the C99 _Pragma() directive (rather than the
* old #pragma one that could not be used as a macro replacement), as
* well as two different levels of preprocessor concetanations in
* order to do both, assign the correct interrupt vector name, as well
* as construct a unique function name for the ISR.
*
* \note Do *NOT* try to reorder the macros below, as this will only
* work in the given order.
*/
#define PRAGMA(x) _Pragma( #x )
#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void)
#define sei( ) (__enable_interrupt( ))
#define cli( ) (__disable_interrupt( ))

/*! \brief Define the no operation macro. */
#define nop( ) (__no_operation())

/*! \brief Define the watchdog reset macro. */
#define watchdog_reset( ) (__watchdog_reset( ))


#define INLINE PRAGMA( inline=forced ) static

#define FLASH_DECLARE(x) _MEMATTR x
#define FLASH_STRING(x) ((_MEMATTR const char *)(x))
#define FLASH_STRING_T char const _MEMATTR *
#define FLASH_BYTE_ARRAY_T uint8_t const _MEMATTR *
#define PGM_READ_BYTE(x) *(x)
#define PGM_READ_WORD(x) *(x)

#define SHORTENUM /**/

#elif defined( __GNUC__ )

#include & lt; avr/io.h & gt;
#include & lt; avr/interrupt.h & gt;
#include & lt; avr/pgmspace.h & gt;
#include & lt; util/delay.h & gt;

/*! \brief Define the delay_us macro for GCC. */
#define delay_us( us ) (_delay_us( us ))

#define INLINE static inline

/*! \brief Define the no operation macro. */
#define nop() do { __asm__ __volatile__ ( " nop " ); } while (0)

#define MAIN_TASK_PROLOGUE int


#define MAIN_TASK_EPILOGUE() return -1;

#define SHORTENUM __attribute__ ((packed))

#else
#error Compiler not supported.
#endif

#endif


XMEGA_TWI.zip > globals_type.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines



& nbsp;


TWI_Master_t
: twi_master_driver.h
TWI_Slave_t
: twi_slave_driver.h
TWIM_RESULT_t
: twi_master_driver.h
TWIS_RESULT_t
: twi_slave_driver.h





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__slave__driver_8c_source.html

@DOC_TITLE@







Xmega Application Note







twi_slave_driver.c Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00069 #include " twi_slave_driver.h "
00070
00071
00080 void TWI_SlaveInitializeDriver ( TWI_Slave_t *twi,
00081 TWI_t *module,
00082 void (*processDataFunction) ( void ))
00083 {
00084 twi- & gt; interface = module;
00085 twi- & gt; Process_Data = processDataFunction;
00086 twi- & gt; bytesReceived = 0;
00087 twi- & gt; bytesSent = 0;
00088 twi- & gt; status = TWIS_STATUS_READY ;
00089 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00090 twi- & gt; abort = false ;
00091 }
00092
00093
00103 void TWI_SlaveInitializeModule ( TWI_Slave_t *twi,
00104 uint8_t address,
00105 TWI_SLAVE_INTLVL_t intLevel)
00106 {
00107 twi- & gt; interface - & gt;SLAVE.CTRLA = intLevel |
00108 TWI_SLAVE_DIEN_bm |
00109 TWI_SLAVE_APIEN_bm |
00110 TWI_SLAVE_ENABLE_bm;
00111 twi- & gt; interface - & gt;SLAVE.ADDR = (address & lt; & lt;1);
00112 }
00113
00114
00122 void TWI_SlaveInterruptHandler ( TWI_Slave_t *twi)
00123 {
00124 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00125
00126 /* If bus error. */
00127 if (currentStatus & amp; TWI_SLAVE_BUSERR_bm) {
00128 twi- & gt; bytesReceived = 0;
00129 twi- & gt; bytesSent = 0;
00130 twi- & gt; result = TWIS_RESULT_BUS_ERROR ;
00131 twi- & gt; status = TWIS_STATUS_READY ;
00132 }
00133
00134 /* If transmit collision. */
00135 else if (currentStatus & amp; TWI_SLAVE_COLL_bm) {
00136 twi- & gt; bytesReceived = 0;
00137 twi- & gt; bytesSent = 0;
00138 twi- & gt; result = TWIS_RESULT_TRANSMIT_COLLISION ;
00139 twi- & gt; status = TWIS_STATUS_READY ;
00140 }
00141
00142 /* If address match. */
00143 else if ((currentStatus & amp; TWI_SLAVE_APIF_bm) & amp; & amp;
00144 (currentStatus & amp; TWI_SLAVE_AP_bm)) {
00145
00146 TWI_SlaveAddressMatchHandler (twi);
00147 }
00148
00149 /* If stop (only enabled through slave read transaction). */
00150 else if (currentStatus & amp; TWI_SLAVE_APIF_bm) {
00151 TWI_SlaveStopHandler (twi);
00152 }
00153
00154 /* If data interrupt. */
00155 else if (currentStatus & amp; TWI_SLAVE_DIF_bm) {
00156 TWI_SlaveDataHandler (twi);
00157 }
00158
00159 /* If unexpected state. */
00160 else {
00161 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_FAIL );
00162 }
00163 }
00164
00171 void TWI_SlaveAddressMatchHandler ( TWI_Slave_t *twi)
00172 {
00173 /* If application signalling need to abort (error occured). */
00174 if (twi- & gt; abort ) {
00175 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00176 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00177 twi- & gt; abort = false ;
00178 } else {
00179 twi- & gt; status = TWIS_STATUS_BUSY ;
00180 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00181
00182 /* Disable stop interrupt. */
00183 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00184 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00185
00186 twi- & gt; bytesReceived = 0;
00187 twi- & gt; bytesSent = 0;
00188
00189 /* Send ACK, wait for data interrupt. */
00190 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00191 }
00192 }
00193
00194
00199 void TWI_SlaveStopHandler ( TWI_Slave_t *twi)
00200 {
00201 /* Disable stop interrupt. */
00202 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00203 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00204
00205 /* Clear APIF, according to flowchart don't ACK or NACK */
00206 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00207 twi- & gt; interface - & gt;SLAVE.STATUS = currentStatus | TWI_SLAVE_APIF_bm;
00208
00209 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00210
00211 }
00212
00213
00220 void TWI_SlaveDataHandler ( TWI_Slave_t *twi)
00221 {
00222 if (twi- & gt; interface - & gt;SLAVE.STATUS & amp; TWI_SLAVE_DIR_bm) {
00223 TWI_SlaveWriteHandler (twi);
00224 } else {
00225 TWI_SlaveReadHandler (twi);
00226 }
00227 }
00228
00229
00236 void TWI_SlaveReadHandler ( TWI_Slave_t *twi)
00237 {
00238 /* Enable stop interrupt. */
00239 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00240 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA | TWI_SLAVE_PIEN_bm;
00241
00242 /* If free space in buffer. */
00243 if (twi- & gt; bytesReceived & lt; TWIS_RECEIVE_BUFFER_SIZE ) {
00244 /* Fetch data */
00245 uint8_t data = twi- & gt; interface - & gt;SLAVE.DATA;
00246 twi- & gt; receivedData [twi- & gt; bytesReceived ] = data;
00247
00248 /* Process data. */
00249 twi- & gt; Process_Data ();
00250
00251 twi- & gt; bytesReceived ++;
00252
00253 /* If application signalling need to abort (error occured),
00254 * complete transaction and wait for next START. Otherwise
00255 * send ACK and wait for data interrupt.
00256 */
00257 if (twi- & gt; abort ) {
00258 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00259 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00260 twi- & gt; abort = false ;
00261 } else {
00262 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00263 }
00264 }
00265 /* If buffer overflow, send NACK and wait for next START. Set
00266 * result buffer overflow.
00267 */
00268 else {
00269 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_ACKACT_bm |
00270 TWI_SLAVE_CMD_COMPTRANS_gc;
00271 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00272 }
00273 }
00274
00275
00282 void TWI_SlaveWriteHandler ( TWI_Slave_t *twi)
00283 {
00284 /* If NACK, slave write transaction finished. */
00285 if ((twi- & gt; bytesSent & gt; 0) & amp; & amp; (twi- & gt; interface - & gt;SLAVE.STATUS & amp;
00286 TWI_SLAVE_RXACK_bm)) {
00287
00288 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00289 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00290 }
00291 /* If ACK, master expects more data. */
00292 else {
00293 if (twi- & gt; bytesSent & lt; TWIS_SEND_BUFFER_SIZE ) {
00294 uint8_t data = twi- & gt; sendData [twi- & gt; bytesSent ];
00295 twi- & gt; interface - & gt;SLAVE.DATA = data;
00296 twi- & gt; bytesSent ++;
00297
00298 /* Send data, wait for data interrupt. */
00299 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00300 }
00301 /* If buffer overflow. */
00302 else {
00303 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00304 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00305 }
00306 }
00307 }
00308
00309
00317 void TWI_SlaveTransactionFinished ( TWI_Slave_t *twi, uint8_t result)
00318 {
00319 twi- & gt; result = result;
00320 twi- & gt; status = TWIS_STATUS_READY ;
00321 }




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__slave__driver_8h_source.html

@DOC_TITLE@







Xmega Application Note







twi_slave_driver.h Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00059 #ifndef TWI_DRIVER_H
00060 #define TWI_DRIVER_H
00061
00062 #include " avr_compiler.h "
00063
00064
00065 /* Transaction status defines.*/
00066 #define TWIS_STATUS_READY 0
00067 #define TWIS_STATUS_BUSY 1
00068
00069 /* Transaction result enumeration */
00070 typedef enum TWIS_RESULT_enum {
00071 TWIS_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
00072 TWIS_RESULT_OK = (0x01 & lt; & lt;0),
00073 TWIS_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
00074 TWIS_RESULT_TRANSMIT_COLLISION = (0x03 & lt; & lt;0),
00075 TWIS_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
00076 TWIS_RESULT_FAIL = (0x05 & lt; & lt;0),
00077 TWIS_RESULT_ABORTED = (0x06 & lt; & lt;0),
00078 } TWIS_RESULT_t ;
00079
00080 /* Buffer size defines. */
00081 #define TWIS_RECEIVE_BUFFER_SIZE 8
00082 #define TWIS_SEND_BUFFER_SIZE 8
00083
00084
00085
00091 typedef struct TWI_Slave {
00092 TWI_t * interface ;
00093 void (* Process_Data ) (void);
00094 register8_t receivedData [ TWIS_RECEIVE_BUFFER_SIZE ];
00095 register8_t sendData [ TWIS_SEND_BUFFER_SIZE ];
00096 register8_t bytesReceived ;
00097 register8_t bytesSent ;
00098 register8_t status ;
00099 register8_t result ;
00100 bool abort ;
00101 } TWI_Slave_t ;
00102
00103
00104
00105 void TWI_SlaveInitializeDriver ( TWI_Slave_t *twi,
00106 TWI_t *module,
00107 void (*processDataFunction) ( void ));
00108
00109 void TWI_SlaveInitializeModule ( TWI_Slave_t *twi,
00110 uint8_t address,
00111 TWI_SLAVE_INTLVL_t intLevel);
00112
00113 void TWI_SlaveInterruptHandler ( TWI_Slave_t *twi);
00114 void TWI_SlaveAddressMatchHandler ( TWI_Slave_t *twi);
00115 void TWI_SlaveStopHandler ( TWI_Slave_t *twi);
00116 void TWI_SlaveDataHandler ( TWI_Slave_t *twi);
00117 void TWI_SlaveReadHandler ( TWI_Slave_t *twi);
00118 void TWI_SlaveWriteHandler ( TWI_Slave_t *twi);
00119 void TWI_SlaveTransactionFinished ( TWI_Slave_t *twi, uint8_t result);
00120
00121
00135 #endif /* TWI_DRIVER_H */




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > documentation_8h_source.html

@DOC_TITLE@







Xmega Application Note







documentation.h Go to the documentation of this file. 00001 /* Doxygen documentation mainpage ********************************************/




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > atmel-footer.html

@DOC_TITLE@












Generated on $datetime for $projectname by $doxygenversion


XMEGA_TWI.zip > twi__master__driver_8h_source.html

@DOC_TITLE@







Xmega Application Note







twi_master_driver.h Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00059 #ifndef TWI_MASTER_DRIVER_H
00060 #define TWI_MASTER_DRIVER_H
00061
00062 #include " avr_compiler.h "
00063
00065 #define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5)
00066
00067
00069 #define TWIM_STATUS_READY 0
00070 #define TWIM_STATUS_BUSY 1
00071
00072
00074 typedef enum TWIM_RESULT_enum {
00075 TWIM_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
00076 TWIM_RESULT_OK = (0x01 & lt; & lt;0),
00077 TWIM_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
00078 TWIM_RESULT_ARBITRATION_LOST = (0x03 & lt; & lt;0),
00079 TWIM_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
00080 TWIM_RESULT_NACK_RECEIVED = (0x05 & lt; & lt;0),
00081 TWIM_RESULT_FAIL = (0x06 & lt; & lt;0),
00082 } TWIM_RESULT_t ;
00083
00085 #define TWIM_WRITE_BUFFER_SIZE 8
00086 #define TWIM_READ_BUFFER_SIZE 8
00087
00088
00094 typedef struct TWI_Master {
00095 TWI_t * interface ;
00096 register8_t address ;
00097 register8_t writeData [ TWIM_WRITE_BUFFER_SIZE ];
00098 register8_t readData [ TWIM_READ_BUFFER_SIZE ];
00099 register8_t bytesToWrite ;
00100 register8_t bytesToRead ;
00101 register8_t bytesWritten ;
00102 register8_t bytesRead ;
00103 register8_t status ;
00104 register8_t result ;
00105 } TWI_Master_t ;
00106
00107
00108
00109 void TWI_MasterInit ( TWI_Master_t *twi,
00110 TWI_t *module,
00111 TWI_MASTER_INTLVL_t intLevel,
00112 uint8_t baudRateRegisterSetting);
00113 TWI_MASTER_BUSSTATE_t TWI_MasterState ( TWI_Master_t *twi);
00114 bool TWI_MasterReady ( TWI_Master_t *twi);
00115 bool TWI_MasterWrite ( TWI_Master_t *twi,
00116 uint8_t address,
00117 uint8_t * writeData,
00118 uint8_t bytesToWrite);
00119 bool TWI_MasterRead ( TWI_Master_t *twi,
00120 uint8_t address,
00121 uint8_t bytesToRead);
00122 bool TWI_MasterWriteRead ( TWI_Master_t *twi,
00123 uint8_t address,
00124 uint8_t *writeData,
00125 uint8_t bytesToWrite,
00126 uint8_t bytesToRead);
00127 void TWI_MasterInterruptHandler ( TWI_Master_t *twi);
00128 void TWI_MasterArbitrationLostBusErrorHandler ( TWI_Master_t *twi);
00129 void TWI_MasterWriteHandler ( TWI_Master_t *twi);
00130 void TWI_MasterReadHandler ( TWI_Master_t *twi);
00131 void TWI_MasterTransactionFinished ( TWI_Master_t *twi, uint8_t result);
00132
00133
00147 #endif /* TWI_MASTER_DRIVER_H */




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > globals_eval.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines



& nbsp;


TWIM_RESULT_ARBITRATION_LOST
: twi_master_driver.h
TWIM_RESULT_BUFFER_OVERFLOW
: twi_master_driver.h
TWIM_RESULT_BUS_ERROR
: twi_master_driver.h
TWIM_RESULT_FAIL
: twi_master_driver.h
TWIM_RESULT_NACK_RECEIVED
: twi_master_driver.h
TWIM_RESULT_OK
: twi_master_driver.h
TWIM_RESULT_UNKNOWN
: twi_master_driver.h
TWIS_RESULT_ABORTED
: twi_slave_driver.h
TWIS_RESULT_BUFFER_OVERFLOW
: twi_slave_driver.h
TWIS_RESULT_BUS_ERROR
: twi_slave_driver.h
TWIS_RESULT_FAIL
: twi_slave_driver.h
TWIS_RESULT_OK
: twi_slave_driver.h
TWIS_RESULT_TRANSMIT_COLLISION
: twi_slave_driver.h
TWIS_RESULT_UNKNOWN
: twi_slave_driver.h





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > structTWI__Master.html

@DOC_TITLE@







Xmega Application Note








TWI_Master Struct Reference TWI master driver struct.
More...

#include & lt; twi_master_driver.h & gt;



Data Fields
register8_t & nbsp; address

register8_t & nbsp; bytesRead

register8_t & nbsp; bytesToRead

register8_t & nbsp; bytesToWrite

register8_t & nbsp; bytesWritten

TWI_t * & nbsp; interface

register8_t & nbsp; readData [TWIM_READ_BUFFER_SIZE]

register8_t & nbsp; result

register8_t & nbsp; status

register8_t & nbsp; writeData [TWIM_WRITE_BUFFER_SIZE]


Detailed Description
TWI master driver struct.

TWI master struct. Holds pointer to TWI module, buffers and necessary varibles.
Definition at line 94 of file twi_master_driver.h .
Field Documentation





register8_t TWI_Master::address






Slave address
Definition at line 96 of file twi_master_driver.h .

Referenced by TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::bytesRead






Number of bytes read
Definition at line 102 of file twi_master_driver.h .

Referenced by TWI_MasterReadHandler() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::bytesToRead






Number of bytes to read
Definition at line 100 of file twi_master_driver.h .

Referenced by TWI_MasterReadHandler() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::bytesToWrite






Number of bytes to write
Definition at line 99 of file twi_master_driver.h .

Referenced by TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::bytesWritten






Number of bytes written
Definition at line 101 of file twi_master_driver.h .

Referenced by TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








TWI_t* TWI_Master::interface






Pointer to what interface to use
Definition at line 95 of file twi_master_driver.h .

Referenced by TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterInit() , TWI_MasterInterruptHandler() , TWI_MasterReadHandler() , TWI_MasterState() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::readData [TWIM_READ_BUFFER_SIZE]






Read data
Definition at line 98 of file twi_master_driver.h .

Referenced by main() , and TWI_MasterReadHandler() .








register8_t TWI_Master::result






Result of transaction
Definition at line 104 of file twi_master_driver.h .

Referenced by TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterTransactionFinished() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::status






Status of transaction
Definition at line 103 of file twi_master_driver.h .

Referenced by main() , TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterReady() , TWI_MasterTransactionFinished() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








register8_t TWI_Master::writeData [TWIM_WRITE_BUFFER_SIZE]






Data to write
Definition at line 97 of file twi_master_driver.h .

Referenced by TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .







@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > globals_enum.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines



& nbsp;


TWIM_RESULT_enum
: twi_master_driver.h
TWIS_RESULT_enum
: twi_slave_driver.h





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__slave__driver_8h.html

@DOC_TITLE@







Xmega Application Note








twi_slave_driver.h File Reference XMEGA TWI slave driver header file.
More...

#include & quot; avr_compiler.h & quot;



Include dependency graph for twi_slave_driver.h:









This graph shows which files directly or indirectly include this file:









Go to the source code of this file.

Data Structures
struct & nbsp; TWI_Slave

& nbsp; TWI slave driver struct. More...
Defines
#define & nbsp; TWIS_RECEIVE_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8

#define & nbsp; TWIS_SEND_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8

#define & nbsp; TWIS_STATUS_BUSY & nbsp; & nbsp; & nbsp;1

#define & nbsp; TWIS_STATUS_READY & nbsp; & nbsp; & nbsp;0

Typedefs
typedef struct TWI_Slave & nbsp; TWI_Slave_t

& nbsp; TWI slave driver struct.
typedef enum TWIS_RESULT_enum & nbsp; TWIS_RESULT_t

Enumerations
enum & nbsp; TWIS_RESULT_enum {
& nbsp; & nbsp; TWIS_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
TWIS_RESULT_OK = (0x01 & lt; & lt;0),
TWIS_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
TWIS_RESULT_TRANSMIT_COLLISION = (0x03 & lt; & lt;0),

& nbsp; & nbsp; TWIS_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
TWIS_RESULT_FAIL = (0x05 & lt; & lt;0),
TWIS_RESULT_ABORTED = (0x06 & lt; & lt;0)

}

Functions
void & nbsp; TWI_SlaveAddressMatchHandler ( TWI_Slave_t *twi)

& nbsp; TWI address match interrupt handler.
void & nbsp; TWI_SlaveDataHandler ( TWI_Slave_t *twi)

& nbsp; TWI data interrupt handler.
void & nbsp; TWI_SlaveInitializeDriver ( TWI_Slave_t *twi, TWI_t *module, void(*processDataFunction)(void))

& nbsp; Initalizes TWI slave driver structure.
void & nbsp; TWI_SlaveInitializeModule ( TWI_Slave_t *twi, uint8_t address, TWI_SLAVE_INTLVL_t intLevel)

& nbsp; Initialize the TWI module.
void & nbsp; TWI_SlaveInterruptHandler ( TWI_Slave_t *twi)

& nbsp; Common TWI slave interrupt service routine.
void & nbsp; TWI_SlaveReadHandler ( TWI_Slave_t *twi)

& nbsp; TWI slave read interrupt handler.
void & nbsp; TWI_SlaveStopHandler ( TWI_Slave_t *twi)

& nbsp; TWI stop condition interrupt handler.
void & nbsp; TWI_SlaveTransactionFinished ( TWI_Slave_t *twi, uint8_t result)

& nbsp; TWI transaction finished function.
void & nbsp; TWI_SlaveWriteHandler ( TWI_Slave_t *twi)

& nbsp; TWI slave write interrupt handler.

Detailed Description
XMEGA TWI slave driver header file.

This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA TWI slave driver.
The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA TWI slave module.
For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.
Application note: AVR1307: Using the XMEGA TWI
Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 1569
Date 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_slave_driver.h .
Define Documentation





#define TWIS_RECEIVE_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8







Definition at line 81 of file twi_slave_driver.h .

Referenced by TWI_SlaveReadHandler() .








#define TWIS_SEND_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8







Definition at line 82 of file twi_slave_driver.h .

Referenced by TWI_SlaveWriteHandler() .








#define TWIS_STATUS_BUSY & nbsp; & nbsp; & nbsp;1







Definition at line 67 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() .








#define TWIS_STATUS_READY & nbsp; & nbsp; & nbsp;0







Definition at line 66 of file twi_slave_driver.h .

Referenced by TWI_SlaveInitializeDriver() , TWI_SlaveInterruptHandler() , and TWI_SlaveTransactionFinished() .



Typedef Documentation





typedef struct TWI_Slave TWI_Slave_t






TWI slave driver struct.

TWI slave struct. Holds pointer to TWI module and data processing routine, buffers and necessary varibles.







typedef enum TWIS_RESULT_enum TWIS_RESULT_t









Enumeration Type Documentation





enum TWIS_RESULT_enum






Enumerator:

TWIS_RESULT_UNKNOWN & nbsp;

TWIS_RESULT_OK & nbsp;

TWIS_RESULT_BUFFER_OVERFLOW & nbsp;

TWIS_RESULT_TRANSMIT_COLLISION & nbsp;

TWIS_RESULT_BUS_ERROR & nbsp;

TWIS_RESULT_FAIL & nbsp;

TWIS_RESULT_ABORTED & nbsp;




Definition at line 70 of file twi_slave_driver.h .
00070 {
00071 TWIS_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
00072 TWIS_RESULT_OK = (0x01 & lt; & lt;0),
00073 TWIS_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
00074 TWIS_RESULT_TRANSMIT_COLLISION = (0x03 & lt; & lt;0),
00075 TWIS_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
00076 TWIS_RESULT_FAIL = (0x05 & lt; & lt;0),
00077 TWIS_RESULT_ABORTED = (0x06 & lt; & lt;0),
00078 } TWIS_RESULT_t ;





Function Documentation





void TWI_SlaveAddressMatchHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI address match interrupt handler.

Prepares TWI module for transaction when an address match occures.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 171 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::result , TWI_Slave::status , TWI_SlaveTransactionFinished() , TWIS_RESULT_ABORTED , TWIS_RESULT_UNKNOWN , and TWIS_STATUS_BUSY .

Referenced by TWI_SlaveInterruptHandler() .
00172 {
00173 /* If application signalling need to abort (error occured). */
00174 if (twi- & gt; abort ) {
00175 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00176 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00177 twi- & gt; abort = false ;
00178 } else {
00179 twi- & gt; status = TWIS_STATUS_BUSY ;
00180 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00181
00182 /* Disable stop interrupt. */
00183 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00184 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00185
00186 twi- & gt; bytesReceived = 0;
00187 twi- & gt; bytesSent = 0;
00188
00189 /* Send ACK, wait for data interrupt. */
00190 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00191 }
00192 }





Here is the call graph for this function:














void TWI_SlaveDataHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI data interrupt handler.

Calls the appropriate slave read or write handler.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 220 of file twi_slave_driver.c .

References TWI_Slave::interface , TWI_SlaveReadHandler() , and TWI_SlaveWriteHandler() .

Referenced by TWI_SlaveInterruptHandler() .
00221 {
00222 if (twi- & gt; interface - & gt;SLAVE.STATUS & amp; TWI_SLAVE_DIR_bm) {
00223 TWI_SlaveWriteHandler (twi);
00224 } else {
00225 TWI_SlaveReadHandler (twi);
00226 }
00227 }





Here is the call graph for this function:
















void TWI_SlaveInitializeDriver
(
TWI_Slave_t * & nbsp;
twi ,




TWI_t * & nbsp;
module ,




void(*)(void) & nbsp;
processDataFunction & nbsp;



)







Initalizes TWI slave driver structure.

Initialize the instance of the TWI Slave and set the appropriate values.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
module & nbsp; Pointer to the TWI module.
processDataFunction & nbsp; Pointer to the function that handles incoming data.



Definition at line 80 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::Process_Data , TWI_Slave::result , TWI_Slave::status , TWIS_RESULT_UNKNOWN , and TWIS_STATUS_READY .

Referenced by main() .
00083 {
00084 twi- & gt; interface = module;
00085 twi- & gt; Process_Data = processDataFunction;
00086 twi- & gt; bytesReceived = 0;
00087 twi- & gt; bytesSent = 0;
00088 twi- & gt; status = TWIS_STATUS_READY ;
00089 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00090 twi- & gt; abort = false ;
00091 }










void TWI_SlaveInitializeModule
(
TWI_Slave_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




TWI_SLAVE_INTLVL_t & nbsp;
intLevel & nbsp;



)







Initialize the TWI module.

Enables interrupts on address recognition and data available. Remember to enable interrupts globally from the main application.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
address & nbsp; Slave address for this module.
intLevel & nbsp; Interrupt level for the TWI slave interrupt handler.



Definition at line 103 of file twi_slave_driver.c .

References TWI_Slave::interface .

Referenced by main() .
00106 {
00107 twi- & gt; interface - & gt;SLAVE.CTRLA = intLevel |
00108 TWI_SLAVE_DIEN_bm |
00109 TWI_SLAVE_APIEN_bm |
00110 TWI_SLAVE_ENABLE_bm;
00111 twi- & gt; interface - & gt;SLAVE.ADDR = (address & lt; & lt;1);
00112 }










void TWI_SlaveInterruptHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







Common TWI slave interrupt service routine.

Handles all TWI transactions and responses to address match, data reception, data transmission, bus error and data collision.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 122 of file twi_slave_driver.c .

References TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::result , TWI_Slave::status , TWI_SlaveAddressMatchHandler() , TWI_SlaveDataHandler() , TWI_SlaveStopHandler() , TWI_SlaveTransactionFinished() , TWIS_RESULT_BUS_ERROR , TWIS_RESULT_FAIL , TWIS_RESULT_TRANSMIT_COLLISION , and TWIS_STATUS_READY .

Referenced by ISR() .
00123 {
00124 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00125
00126 /* If bus error. */
00127 if (currentStatus & amp; TWI_SLAVE_BUSERR_bm) {
00128 twi- & gt; bytesReceived = 0;
00129 twi- & gt; bytesSent = 0;
00130 twi- & gt; result = TWIS_RESULT_BUS_ERROR ;
00131 twi- & gt; status = TWIS_STATUS_READY ;
00132 }
00133
00134 /* If transmit collision. */
00135 else if (currentStatus & amp; TWI_SLAVE_COLL_bm) {
00136 twi- & gt; bytesReceived = 0;
00137 twi- & gt; bytesSent = 0;
00138 twi- & gt; result = TWIS_RESULT_TRANSMIT_COLLISION ;
00139 twi- & gt; status = TWIS_STATUS_READY ;
00140 }
00141
00142 /* If address match. */
00143 else if ((currentStatus & amp; TWI_SLAVE_APIF_bm) & amp; & amp;
00144 (currentStatus & amp; TWI_SLAVE_AP_bm)) {
00145
00146 TWI_SlaveAddressMatchHandler (twi);
00147 }
00148
00149 /* If stop (only enabled through slave read transaction). */
00150 else if (currentStatus & amp; TWI_SLAVE_APIF_bm) {
00151 TWI_SlaveStopHandler (twi);
00152 }
00153
00154 /* If data interrupt. */
00155 else if (currentStatus & amp; TWI_SLAVE_DIF_bm) {
00156 TWI_SlaveDataHandler (twi);
00157 }
00158
00159 /* If unexpected state. */
00160 else {
00161 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_FAIL );
00162 }
00163 }





Here is the call graph for this function:



















void TWI_SlaveReadHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI slave read interrupt handler.

Handles TWI slave read transactions and responses.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 236 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::interface , TWI_Slave::Process_Data , TWI_Slave::receivedData , TWI_SlaveTransactionFinished() , TWIS_RECEIVE_BUFFER_SIZE , TWIS_RESULT_ABORTED , and TWIS_RESULT_BUFFER_OVERFLOW .

Referenced by TWI_SlaveDataHandler() .
00237 {
00238 /* Enable stop interrupt. */
00239 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00240 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA | TWI_SLAVE_PIEN_bm;
00241
00242 /* If free space in buffer. */
00243 if (twi- & gt; bytesReceived & lt; TWIS_RECEIVE_BUFFER_SIZE ) {
00244 /* Fetch data */
00245 uint8_t data = twi- & gt; interface - & gt;SLAVE.DATA;
00246 twi- & gt; receivedData [twi- & gt; bytesReceived ] = data;
00247
00248 /* Process data. */
00249 twi- & gt; Process_Data ();
00250
00251 twi- & gt; bytesReceived ++;
00252
00253 /* If application signalling need to abort (error occured),
00254 * complete transaction and wait for next START. Otherwise
00255 * send ACK and wait for data interrupt.
00256 */
00257 if (twi- & gt; abort ) {
00258 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00259 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00260 twi- & gt; abort = false ;
00261 } else {
00262 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00263 }
00264 }
00265 /* If buffer overflow, send NACK and wait for next START. Set
00266 * result buffer overflow.
00267 */
00268 else {
00269 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_ACKACT_bm |
00270 TWI_SLAVE_CMD_COMPTRANS_gc;
00271 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00272 }
00273 }





Here is the call graph for this function:














void TWI_SlaveStopHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI stop condition interrupt handler.

Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 199 of file twi_slave_driver.c .

References TWI_Slave::interface , TWI_SlaveTransactionFinished() , and TWIS_RESULT_OK .

Referenced by TWI_SlaveInterruptHandler() .
00200 {
00201 /* Disable stop interrupt. */
00202 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00203 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00204
00205 /* Clear APIF, according to flowchart don't ACK or NACK */
00206 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00207 twi- & gt; interface - & gt;SLAVE.STATUS = currentStatus | TWI_SLAVE_APIF_bm;
00208
00209 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00210
00211 }





Here is the call graph for this function:














void TWI_SlaveTransactionFinished
(
TWI_Slave_t * & nbsp;
twi ,




uint8_t & nbsp;
result & nbsp;



)







TWI transaction finished function.

Prepares module for new transaction.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
result & nbsp; The result of the transaction.



Definition at line 317 of file twi_slave_driver.c .

References TWI_Slave::result , TWI_Slave::status , and TWIS_STATUS_READY .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInterruptHandler() , TWI_SlaveReadHandler() , TWI_SlaveStopHandler() , and TWI_SlaveWriteHandler() .
00318 {
00319 twi- & gt; result = result;
00320 twi- & gt; status = TWIS_STATUS_READY ;
00321 }










void TWI_SlaveWriteHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI slave write interrupt handler.

Handles TWI slave write transactions and responses.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 282 of file twi_slave_driver.c .

References TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::sendData , TWI_SlaveTransactionFinished() , TWIS_RESULT_BUFFER_OVERFLOW , TWIS_RESULT_OK , and TWIS_SEND_BUFFER_SIZE .

Referenced by TWI_SlaveDataHandler() .
00283 {
00284 /* If NACK, slave write transaction finished. */
00285 if ((twi- & gt; bytesSent & gt; 0) & amp; & amp; (twi- & gt; interface - & gt;SLAVE.STATUS & amp;
00286 TWI_SLAVE_RXACK_bm)) {
00287
00288 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00289 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00290 }
00291 /* If ACK, master expects more data. */
00292 else {
00293 if (twi- & gt; bytesSent & lt; TWIS_SEND_BUFFER_SIZE ) {
00294 uint8_t data = twi- & gt; sendData [twi- & gt; bytesSent ];
00295 twi- & gt; interface - & gt;SLAVE.DATA = data;
00296 twi- & gt; bytesSent ++;
00297
00298 /* Send data, wait for data interrupt. */
00299 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00300 }
00301 /* If buffer overflow. */
00302 else {
00303 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00304 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00305 }
00306 }
00307 }





Here is the call graph for this function:













@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > globals_func.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines




i
m
t



& nbsp;

- i -
ISR()
: twi_example.c

- m -
main()
: twi_example.c

- t -
TWI_MasterArbitrationLostBusErrorHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterInit()
: twi_master_driver.h
, twi_master_driver.c
TWI_MasterInterruptHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterRead()
: twi_master_driver.h
, twi_master_driver.c
TWI_MasterReadHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterReady()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterState()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterTransactionFinished()
: twi_master_driver.h
, twi_master_driver.c
TWI_MasterWrite()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterWriteHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterWriteRead()
: twi_master_driver.c
, twi_master_driver.h
TWI_SlaveAddressMatchHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveDataHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveInitializeDriver()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveInitializeModule()
: twi_slave_driver.h
, twi_slave_driver.c
TWI_SlaveInterruptHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveReadHandler()
: twi_slave_driver.h
, twi_slave_driver.c
TWI_SlaveStopHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveTransactionFinished()
: twi_slave_driver.h
, twi_slave_driver.c
TWI_SlaveWriteHandler()
: twi_slave_driver.h
, twi_slave_driver.c
TWIC_SlaveProcessData()
: twi_example.c





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > functions.html

@DOC_TITLE@







Xmega Application Note









All
Variables



Here is a list of all struct and union fields with links to the structures/unions they belong to:


abort
: TWI_Slave
address
: TWI_Master
bytesRead
: TWI_Master
bytesReceived
: TWI_Slave
bytesSent
: TWI_Slave
bytesToRead
: TWI_Master
bytesToWrite
: TWI_Master
bytesWritten
: TWI_Master
interface
: TWI_Master
, TWI_Slave
Process_Data
: TWI_Slave
readData
: TWI_Master
receivedData
: TWI_Slave
result
: TWI_Master
, TWI_Slave
sendData
: TWI_Slave
status
: TWI_Slave
, TWI_Master
writeData
: TWI_Master





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > avr__compiler_8h.html

@DOC_TITLE@







Xmega Application Note








avr_compiler.h File Reference This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code base for the AVR architecture.
More...

#include & lt;stdint.h & gt;
#include & lt;stdbool.h & gt;
#include & lt;stdlib.h & gt;



Include dependency graph for avr_compiler.h:






This graph shows which files directly or indirectly include this file:












Go to the source code of this file.

Defines
#define & nbsp; AVR_ENTER_CRITICAL_REGION ()

& nbsp; This macro will protect the following code from interrupts.
#define & nbsp; AVR_LEAVE_CRITICAL_REGION () & nbsp; & nbsp; & nbsp;SREG = saved_sreg;

& nbsp; This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION so the interrupts are enabled again.
#define & nbsp; F_CPU & nbsp; & nbsp; & nbsp;2000000UL

& nbsp; Define default CPU frequency, if this is not already defined.

Detailed Description
This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code base for the AVR architecture.

Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 613
Date 2006-04-07 14:40:07 +0200 (fr, 07 apr 2006)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file avr_compiler.h .
Define Documentation





#define AVR_ENTER_CRITICAL_REGION
(
& nbsp;

& nbsp;) & nbsp;







Value: uint8_t volatile saved_sreg = SREG; \
cli();
This macro will protect the following code from interrupts.


Definition at line 58 of file avr_compiler.h .








#define AVR_LEAVE_CRITICAL_REGION
(
& nbsp;

& nbsp;) & nbsp;
& nbsp; & nbsp; & nbsp;SREG = saved_sreg;






This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION so the interrupts are enabled again.


Definition at line 64 of file avr_compiler.h .








#define F_CPU & nbsp; & nbsp; & nbsp;2000000UL






Define default CPU frequency, if this is not already defined.


Definition at line 50 of file avr_compiler.h .







@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > functions_vars.html

@DOC_TITLE@







Xmega Application Note









All
Variables



& nbsp;


abort
: TWI_Slave
address
: TWI_Master
bytesRead
: TWI_Master
bytesReceived
: TWI_Slave
bytesSent
: TWI_Slave
bytesToRead
: TWI_Master
bytesToWrite
: TWI_Master
bytesWritten
: TWI_Master
interface
: TWI_Master
, TWI_Slave
Process_Data
: TWI_Slave
readData
: TWI_Master
receivedData
: TWI_Slave
result
: TWI_Master
, TWI_Slave
sendData
: TWI_Slave
status
: TWI_Slave
, TWI_Master
writeData
: TWI_Master





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > main.html

@DOC_TITLE@







Xmega Application Note








AVR1308 Using the XMEGA TWI Documentation

Atmel AVR Application Notes
Introduction
This documents the firmware for the application note.

Compilation Info
This firmware was compiled with IAR Embedded Workbench 5.30.0 and WinAVR 20090313.

To make the project in IAR EWAVR:
Add the .c files (and .S90 files where applicable) for the given example to your project. Use device ATxmega128A1 and enable bit definitions in I/O include files, optimization low for debug target and high for release, output format: ubrof8 for Debug and intel_extended for Release, select Normal DLIB as library.

To make the project in WinAVR:
Add the .c files (and .S files where applicable) for the given example to your project. Use device ATxmega128A1, optimization low for debug target and high for release.

Device Info
All XMEGA devices with the targeted module can be used. The example is written for ATxmega128A1.
Contact Info
For more info about Atmel AVR visit http://www.atmel.com/products/AVR/
For application notes visit http://www.atmel.com/dyn/products/app_notes.asp?family_id=607
Support mail: avr@atmel.com

Revision 2661
Date 2009-08-11 12:33:26 +0200 (ti, 11 aug 2009)

Copyright (c) 2009, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > classes.html

@DOC_TITLE@







Xmega Application Note








Data Structure Index T

& nbsp; & nbsp;T & nbsp; & nbsp;
TWI_Master & nbsp; & nbsp; & nbsp; TWI_Slave & nbsp; & nbsp; & nbsp; T




@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__slave__driver_8c.html

@DOC_TITLE@







Xmega Application Note








twi_slave_driver.c File Reference XMEGA TWI slave driver source file.
More...

#include & quot; twi_slave_driver.h & quot;



Include dependency graph for twi_slave_driver.c:









Go to the source code of this file.

Functions
void & nbsp; TWI_SlaveAddressMatchHandler ( TWI_Slave_t *twi)

& nbsp; TWI address match interrupt handler.
void & nbsp; TWI_SlaveDataHandler ( TWI_Slave_t *twi)

& nbsp; TWI data interrupt handler.
void & nbsp; TWI_SlaveInitializeDriver ( TWI_Slave_t *twi, TWI_t *module, void(*processDataFunction)(void))

& nbsp; Initalizes TWI slave driver structure.
void & nbsp; TWI_SlaveInitializeModule ( TWI_Slave_t *twi, uint8_t address, TWI_SLAVE_INTLVL_t intLevel)

& nbsp; Initialize the TWI module.
void & nbsp; TWI_SlaveInterruptHandler ( TWI_Slave_t *twi)

& nbsp; Common TWI slave interrupt service routine.
void & nbsp; TWI_SlaveReadHandler ( TWI_Slave_t *twi)

& nbsp; TWI slave read interrupt handler.
void & nbsp; TWI_SlaveStopHandler ( TWI_Slave_t *twi)

& nbsp; TWI stop condition interrupt handler.
void & nbsp; TWI_SlaveTransactionFinished ( TWI_Slave_t *twi, uint8_t result)

& nbsp; TWI transaction finished function.
void & nbsp; TWI_SlaveWriteHandler ( TWI_Slave_t *twi)

& nbsp; TWI slave write interrupt handler.

Detailed Description
XMEGA TWI slave driver source file.

This file contains the function implementations the XMEGA TWI slave driver.
The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA TWI slave module.
For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.
Several functions use the following construct: " some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ... " Although the use of the ternary operator ( if ? then : else ) is discouraged, in some occasions the operator makes it possible to write pretty clean and neat code. In this driver, the construct is used to set or not set a configuration bit based on a boolean input parameter, such as the " some_parameter " in the example above.
Application note: AVR1308: Using the XMEGA TWI
Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 2660
Date 2009-08-11 12:28:58 +0200 (ti, 11 aug 2009)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_slave_driver.c .
Function Documentation





void TWI_SlaveAddressMatchHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI address match interrupt handler.

Prepares TWI module for transaction when an address match occures.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 171 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::result , TWI_Slave::status , TWI_SlaveTransactionFinished() , TWIS_RESULT_ABORTED , TWIS_RESULT_UNKNOWN , and TWIS_STATUS_BUSY .

Referenced by TWI_SlaveInterruptHandler() .
00172 {
00173 /* If application signalling need to abort (error occured). */
00174 if (twi- & gt; abort ) {
00175 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00176 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00177 twi- & gt; abort = false ;
00178 } else {
00179 twi- & gt; status = TWIS_STATUS_BUSY ;
00180 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00181
00182 /* Disable stop interrupt. */
00183 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00184 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00185
00186 twi- & gt; bytesReceived = 0;
00187 twi- & gt; bytesSent = 0;
00188
00189 /* Send ACK, wait for data interrupt. */
00190 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00191 }
00192 }





Here is the call graph for this function:














void TWI_SlaveDataHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI data interrupt handler.

Calls the appropriate slave read or write handler.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 220 of file twi_slave_driver.c .

References TWI_Slave::interface , TWI_SlaveReadHandler() , and TWI_SlaveWriteHandler() .

Referenced by TWI_SlaveInterruptHandler() .
00221 {
00222 if (twi- & gt; interface - & gt;SLAVE.STATUS & amp; TWI_SLAVE_DIR_bm) {
00223 TWI_SlaveWriteHandler (twi);
00224 } else {
00225 TWI_SlaveReadHandler (twi);
00226 }
00227 }





Here is the call graph for this function:
















void TWI_SlaveInitializeDriver
(
TWI_Slave_t * & nbsp;
twi ,




TWI_t * & nbsp;
module ,




void(*)(void) & nbsp;
processDataFunction & nbsp;



)







Initalizes TWI slave driver structure.

Initialize the instance of the TWI Slave and set the appropriate values.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
module & nbsp; Pointer to the TWI module.
processDataFunction & nbsp; Pointer to the function that handles incoming data.



Definition at line 80 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::Process_Data , TWI_Slave::result , TWI_Slave::status , TWIS_RESULT_UNKNOWN , and TWIS_STATUS_READY .

Referenced by main() .
00083 {
00084 twi- & gt; interface = module;
00085 twi- & gt; Process_Data = processDataFunction;
00086 twi- & gt; bytesReceived = 0;
00087 twi- & gt; bytesSent = 0;
00088 twi- & gt; status = TWIS_STATUS_READY ;
00089 twi- & gt; result = TWIS_RESULT_UNKNOWN ;
00090 twi- & gt; abort = false ;
00091 }










void TWI_SlaveInitializeModule
(
TWI_Slave_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




TWI_SLAVE_INTLVL_t & nbsp;
intLevel & nbsp;



)







Initialize the TWI module.

Enables interrupts on address recognition and data available. Remember to enable interrupts globally from the main application.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
address & nbsp; Slave address for this module.
intLevel & nbsp; Interrupt level for the TWI slave interrupt handler.



Definition at line 103 of file twi_slave_driver.c .

References TWI_Slave::interface .

Referenced by main() .
00106 {
00107 twi- & gt; interface - & gt;SLAVE.CTRLA = intLevel |
00108 TWI_SLAVE_DIEN_bm |
00109 TWI_SLAVE_APIEN_bm |
00110 TWI_SLAVE_ENABLE_bm;
00111 twi- & gt; interface - & gt;SLAVE.ADDR = (address & lt; & lt;1);
00112 }










void TWI_SlaveInterruptHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







Common TWI slave interrupt service routine.

Handles all TWI transactions and responses to address match, data reception, data transmission, bus error and data collision.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 122 of file twi_slave_driver.c .

References TWI_Slave::bytesReceived , TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::result , TWI_Slave::status , TWI_SlaveAddressMatchHandler() , TWI_SlaveDataHandler() , TWI_SlaveStopHandler() , TWI_SlaveTransactionFinished() , TWIS_RESULT_BUS_ERROR , TWIS_RESULT_FAIL , TWIS_RESULT_TRANSMIT_COLLISION , and TWIS_STATUS_READY .

Referenced by ISR() .
00123 {
00124 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00125
00126 /* If bus error. */
00127 if (currentStatus & amp; TWI_SLAVE_BUSERR_bm) {
00128 twi- & gt; bytesReceived = 0;
00129 twi- & gt; bytesSent = 0;
00130 twi- & gt; result = TWIS_RESULT_BUS_ERROR ;
00131 twi- & gt; status = TWIS_STATUS_READY ;
00132 }
00133
00134 /* If transmit collision. */
00135 else if (currentStatus & amp; TWI_SLAVE_COLL_bm) {
00136 twi- & gt; bytesReceived = 0;
00137 twi- & gt; bytesSent = 0;
00138 twi- & gt; result = TWIS_RESULT_TRANSMIT_COLLISION ;
00139 twi- & gt; status = TWIS_STATUS_READY ;
00140 }
00141
00142 /* If address match. */
00143 else if ((currentStatus & amp; TWI_SLAVE_APIF_bm) & amp; & amp;
00144 (currentStatus & amp; TWI_SLAVE_AP_bm)) {
00145
00146 TWI_SlaveAddressMatchHandler (twi);
00147 }
00148
00149 /* If stop (only enabled through slave read transaction). */
00150 else if (currentStatus & amp; TWI_SLAVE_APIF_bm) {
00151 TWI_SlaveStopHandler (twi);
00152 }
00153
00154 /* If data interrupt. */
00155 else if (currentStatus & amp; TWI_SLAVE_DIF_bm) {
00156 TWI_SlaveDataHandler (twi);
00157 }
00158
00159 /* If unexpected state. */
00160 else {
00161 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_FAIL );
00162 }
00163 }





Here is the call graph for this function:



















void TWI_SlaveReadHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI slave read interrupt handler.

Handles TWI slave read transactions and responses.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 236 of file twi_slave_driver.c .

References TWI_Slave::abort , TWI_Slave::bytesReceived , TWI_Slave::interface , TWI_Slave::Process_Data , TWI_Slave::receivedData , TWI_SlaveTransactionFinished() , TWIS_RECEIVE_BUFFER_SIZE , TWIS_RESULT_ABORTED , and TWIS_RESULT_BUFFER_OVERFLOW .

Referenced by TWI_SlaveDataHandler() .
00237 {
00238 /* Enable stop interrupt. */
00239 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00240 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA | TWI_SLAVE_PIEN_bm;
00241
00242 /* If free space in buffer. */
00243 if (twi- & gt; bytesReceived & lt; TWIS_RECEIVE_BUFFER_SIZE ) {
00244 /* Fetch data */
00245 uint8_t data = twi- & gt; interface - & gt;SLAVE.DATA;
00246 twi- & gt; receivedData [twi- & gt; bytesReceived ] = data;
00247
00248 /* Process data. */
00249 twi- & gt; Process_Data ();
00250
00251 twi- & gt; bytesReceived ++;
00252
00253 /* If application signalling need to abort (error occured),
00254 * complete transaction and wait for next START. Otherwise
00255 * send ACK and wait for data interrupt.
00256 */
00257 if (twi- & gt; abort ) {
00258 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00259 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_ABORTED );
00260 twi- & gt; abort = false ;
00261 } else {
00262 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00263 }
00264 }
00265 /* If buffer overflow, send NACK and wait for next START. Set
00266 * result buffer overflow.
00267 */
00268 else {
00269 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_ACKACT_bm |
00270 TWI_SLAVE_CMD_COMPTRANS_gc;
00271 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00272 }
00273 }





Here is the call graph for this function:














void TWI_SlaveStopHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI stop condition interrupt handler.

Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 199 of file twi_slave_driver.c .

References TWI_Slave::interface , TWI_SlaveTransactionFinished() , and TWIS_RESULT_OK .

Referenced by TWI_SlaveInterruptHandler() .
00200 {
00201 /* Disable stop interrupt. */
00202 uint8_t currentCtrlA = twi- & gt; interface - & gt;SLAVE.CTRLA;
00203 twi- & gt; interface - & gt;SLAVE.CTRLA = currentCtrlA & amp; ~TWI_SLAVE_PIEN_bm;
00204
00205 /* Clear APIF, according to flowchart don't ACK or NACK */
00206 uint8_t currentStatus = twi- & gt; interface - & gt;SLAVE.STATUS;
00207 twi- & gt; interface - & gt;SLAVE.STATUS = currentStatus | TWI_SLAVE_APIF_bm;
00208
00209 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00210
00211 }





Here is the call graph for this function:














void TWI_SlaveTransactionFinished
(
TWI_Slave_t * & nbsp;
twi ,




uint8_t & nbsp;
result & nbsp;



)







TWI transaction finished function.

Prepares module for new transaction.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.
result & nbsp; The result of the transaction.



Definition at line 317 of file twi_slave_driver.c .

References TWI_Slave::result , TWI_Slave::status , and TWIS_STATUS_READY .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInterruptHandler() , TWI_SlaveReadHandler() , TWI_SlaveStopHandler() , and TWI_SlaveWriteHandler() .
00318 {
00319 twi- & gt; result = result;
00320 twi- & gt; status = TWIS_STATUS_READY ;
00321 }










void TWI_SlaveWriteHandler
(
TWI_Slave_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI slave write interrupt handler.

Handles TWI slave write transactions and responses.
Parameters:

twi & nbsp; The TWI_Slave_t struct instance.



Definition at line 282 of file twi_slave_driver.c .

References TWI_Slave::bytesSent , TWI_Slave::interface , TWI_Slave::sendData , TWI_SlaveTransactionFinished() , TWIS_RESULT_BUFFER_OVERFLOW , TWIS_RESULT_OK , and TWIS_SEND_BUFFER_SIZE .

Referenced by TWI_SlaveDataHandler() .
00283 {
00284 /* If NACK, slave write transaction finished. */
00285 if ((twi- & gt; bytesSent & gt; 0) & amp; & amp; (twi- & gt; interface - & gt;SLAVE.STATUS & amp;
00286 TWI_SLAVE_RXACK_bm)) {
00287
00288 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00289 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_OK );
00290 }
00291 /* If ACK, master expects more data. */
00292 else {
00293 if (twi- & gt; bytesSent & lt; TWIS_SEND_BUFFER_SIZE ) {
00294 uint8_t data = twi- & gt; sendData [twi- & gt; bytesSent ];
00295 twi- & gt; interface - & gt;SLAVE.DATA = data;
00296 twi- & gt; bytesSent ++;
00297
00298 /* Send data, wait for data interrupt. */
00299 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
00300 }
00301 /* If buffer overflow. */
00302 else {
00303 twi- & gt; interface - & gt;SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
00304 TWI_SlaveTransactionFinished (twi, TWIS_RESULT_BUFFER_OVERFLOW );
00305 }
00306 }
00307 }





Here is the call graph for this function:













@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__master__driver_8h.html

@DOC_TITLE@







Xmega Application Note








twi_master_driver.h File Reference XMEGA TWI master driver header file.
More...

#include & quot; avr_compiler.h & quot;



Include dependency graph for twi_master_driver.h:









This graph shows which files directly or indirectly include this file:









Go to the source code of this file.

Data Structures
struct & nbsp; TWI_Master

& nbsp; TWI master driver struct. More...
Defines
#define & nbsp; TWI_BAUD (F_SYS, F_TWI) & nbsp; & nbsp; & nbsp;((F_SYS / (2 * F_TWI)) - 5)

#define & nbsp; TWIM_READ_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8

#define & nbsp; TWIM_STATUS_BUSY & nbsp; & nbsp; & nbsp;1

#define & nbsp; TWIM_STATUS_READY & nbsp; & nbsp; & nbsp;0

#define & nbsp; TWIM_WRITE_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8

Typedefs
typedef struct TWI_Master & nbsp; TWI_Master_t

& nbsp; TWI master driver struct.
typedef enum TWIM_RESULT_enum & nbsp; TWIM_RESULT_t

Enumerations
enum & nbsp; TWIM_RESULT_enum {
& nbsp; & nbsp; TWIM_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
TWIM_RESULT_OK = (0x01 & lt; & lt;0),
TWIM_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
TWIM_RESULT_ARBITRATION_LOST = (0x03 & lt; & lt;0),

& nbsp; & nbsp; TWIM_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
TWIM_RESULT_NACK_RECEIVED = (0x05 & lt; & lt;0),
TWIM_RESULT_FAIL = (0x06 & lt; & lt;0)

}

Functions
void & nbsp; TWI_MasterArbitrationLostBusErrorHandler ( TWI_Master_t *twi)

& nbsp; TWI master arbitration lost and bus error interrupt handler.
void & nbsp; TWI_MasterInit ( TWI_Master_t *twi, TWI_t *module, TWI_MASTER_INTLVL_t intLevel, uint8_t baudRateRegisterSetting)

& nbsp; Initialize the TWI module.
void & nbsp; TWI_MasterInterruptHandler ( TWI_Master_t *twi)

& nbsp; Common TWI master interrupt service routine.
bool & nbsp; TWI_MasterRead ( TWI_Master_t *twi, uint8_t address, uint8_t bytesToRead)

& nbsp; TWI read transaction.
void & nbsp; TWI_MasterReadHandler ( TWI_Master_t *twi)

& nbsp; TWI master read interrupt handler.
bool & nbsp; TWI_MasterReady ( TWI_Master_t *twi)

& nbsp; Returns true if transaction is ready.
TWI_MASTER_BUSSTATE_t & nbsp; TWI_MasterState ( TWI_Master_t *twi)

& nbsp; Returns the TWI bus state.
void & nbsp; TWI_MasterTransactionFinished ( TWI_Master_t *twi, uint8_t result)

& nbsp; TWI transaction finished handler.
bool & nbsp; TWI_MasterWrite ( TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite)

& nbsp; TWI write transaction.
void & nbsp; TWI_MasterWriteHandler ( TWI_Master_t *twi)

& nbsp; TWI master write interrupt handler.
bool & nbsp; TWI_MasterWriteRead ( TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite, uint8_t bytesToRead)

& nbsp; TWI write and/or read transaction.

Detailed Description
XMEGA TWI master driver header file.

This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA TWI master driver.
The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA TWI master module.
For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.
Application note: AVR1308: Using the XMEGA TWI
Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 1569
Date 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_master_driver.h .
Define Documentation





#define TWI_BAUD
(
F_SYS,


F_TWI & nbsp;

& nbsp;) & nbsp;
& nbsp; & nbsp; & nbsp;((F_SYS / (2 * F_TWI)) - 5)






Baud register setting calculation. Formula described in datasheet.
Definition at line 65 of file twi_master_driver.h .








#define TWIM_READ_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8







Definition at line 86 of file twi_master_driver.h .

Referenced by TWI_MasterReadHandler() , and TWI_MasterWriteRead() .








#define TWIM_STATUS_BUSY & nbsp; & nbsp; & nbsp;1







Definition at line 70 of file twi_master_driver.h .

Referenced by TWI_MasterWriteRead() .








#define TWIM_STATUS_READY & nbsp; & nbsp; & nbsp;0






Transaction status defines.
Definition at line 69 of file twi_master_driver.h .

Referenced by main() , TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterReady() , TWI_MasterTransactionFinished() , TWI_MasterWriteHandler() , and TWI_MasterWriteRead() .








#define TWIM_WRITE_BUFFER_SIZE & nbsp; & nbsp; & nbsp;8






Buffer size defines
Definition at line 85 of file twi_master_driver.h .

Referenced by TWI_MasterWriteRead() .



Typedef Documentation





typedef struct TWI_Master TWI_Master_t






TWI master driver struct.

TWI master struct. Holds pointer to TWI module, buffers and necessary varibles.







typedef enum TWIM_RESULT_enum TWIM_RESULT_t






Transaction result enumeration.


Enumeration Type Documentation





enum TWIM_RESULT_enum






Transaction result enumeration. Enumerator:

TWIM_RESULT_UNKNOWN & nbsp;

TWIM_RESULT_OK & nbsp;

TWIM_RESULT_BUFFER_OVERFLOW & nbsp;

TWIM_RESULT_ARBITRATION_LOST & nbsp;

TWIM_RESULT_BUS_ERROR & nbsp;

TWIM_RESULT_NACK_RECEIVED & nbsp;

TWIM_RESULT_FAIL & nbsp;




Definition at line 74 of file twi_master_driver.h .
00074 {
00075 TWIM_RESULT_UNKNOWN = (0x00 & lt; & lt;0),
00076 TWIM_RESULT_OK = (0x01 & lt; & lt;0),
00077 TWIM_RESULT_BUFFER_OVERFLOW = (0x02 & lt; & lt;0),
00078 TWIM_RESULT_ARBITRATION_LOST = (0x03 & lt; & lt;0),
00079 TWIM_RESULT_BUS_ERROR = (0x04 & lt; & lt;0),
00080 TWIM_RESULT_NACK_RECEIVED = (0x05 & lt; & lt;0),
00081 TWIM_RESULT_FAIL = (0x06 & lt; & lt;0),
00082 } TWIM_RESULT_t ;





Function Documentation





void TWI_MasterArbitrationLostBusErrorHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master arbitration lost and bus error interrupt handler.

Handles TWI responses to lost arbitration and bus error.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 287 of file twi_master_driver.c .

References TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWIM_RESULT_ARBITRATION_LOST , TWIM_RESULT_BUS_ERROR , and TWIM_STATUS_READY .

Referenced by TWI_MasterInterruptHandler() .
00288 {
00289 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00290
00291 /* If bus error. */
00292 if (currentStatus & amp; TWI_MASTER_BUSERR_bm) {
00293 twi- & gt; result = TWIM_RESULT_BUS_ERROR ;
00294 }
00295 /* If arbitration lost. */
00296 else {
00297 twi- & gt; result = TWIM_RESULT_ARBITRATION_LOST ;
00298 }
00299
00300 /* Clear interrupt flag. */
00301 twi- & gt; interface - & gt;MASTER.STATUS = currentStatus | TWI_MASTER_ARBLOST_bm;
00302
00303 twi- & gt; status = TWIM_STATUS_READY ;
00304 }










void TWI_MasterInit
(
TWI_Master_t * & nbsp;
twi ,




TWI_t * & nbsp;
module ,




TWI_MASTER_INTLVL_t & nbsp;
intLevel ,




uint8_t & nbsp;
baudRateRegisterSetting & nbsp;



)







Initialize the TWI module.

TWI module initialization function. Enables master read and write interrupts. Remember to enable interrupts globally from the main application.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
module & nbsp; The TWI module to use.
intLevel & nbsp; Master interrupt level.
baudRateRegisterSetting & nbsp; The baud rate register value.



Definition at line 83 of file twi_master_driver.c .

References TWI_Master::interface .

Referenced by main() .
00087 {
00088 twi- & gt; interface = module;
00089 twi- & gt; interface - & gt;MASTER.CTRLA = intLevel |
00090 TWI_MASTER_RIEN_bm |
00091 TWI_MASTER_WIEN_bm |
00092 TWI_MASTER_ENABLE_bm;
00093 twi- & gt; interface - & gt;MASTER.BAUD = baudRateRegisterSetting;
00094 twi- & gt; interface - & gt;MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
00095 }










void TWI_MasterInterruptHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Common TWI master interrupt service routine.

Check current status and calls the appropriate handler.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 253 of file twi_master_driver.c .

References TWI_Master::interface , TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterReadHandler() , TWI_MasterTransactionFinished() , TWI_MasterWriteHandler() , and TWIM_RESULT_FAIL .

Referenced by ISR() .
00254 {
00255 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00256
00257 /* If arbitration lost or bus error. */
00258 if ((currentStatus & amp; TWI_MASTER_ARBLOST_bm) ||
00259 (currentStatus & amp; TWI_MASTER_BUSERR_bm)) {
00260
00261 TWI_MasterArbitrationLostBusErrorHandler (twi);
00262 }
00263
00264 /* If master write interrupt. */
00265 else if (currentStatus & amp; TWI_MASTER_WIF_bm) {
00266 TWI_MasterWriteHandler (twi);
00267 }
00268
00269 /* If master read interrupt. */
00270 else if (currentStatus & amp; TWI_MASTER_RIF_bm) {
00271 TWI_MasterReadHandler (twi);
00272 }
00273
00274 /* If unexpected state. */
00275 else {
00276 TWI_MasterTransactionFinished (twi, TWIM_RESULT_FAIL );
00277 }
00278 }





Here is the call graph for this function:

















bool TWI_MasterRead
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t & nbsp;
bytesToRead & nbsp;



)







TWI read transaction.

This function is a TWI Maste wrapper for read-only transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; The slave address.
bytesToRead & nbsp; The number of bytes to read.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 169 of file twi_master_driver.c .

References TWI_MasterWriteRead() .
00172 {
00173 bool twi_status = TWI_MasterWriteRead (twi, address, 0, 0, bytesToRead);
00174 return twi_status;
00175 }





Here is the call graph for this function:














void TWI_MasterReadHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master read interrupt handler.

This is the master read interrupt handler that takes care of reading bytes from the TWI slave.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 356 of file twi_master_driver.c .

References TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::interface , TWI_Master::readData , TWI_MasterTransactionFinished() , TWIM_READ_BUFFER_SIZE , TWIM_RESULT_BUFFER_OVERFLOW , and TWIM_RESULT_OK .

Referenced by TWI_MasterInterruptHandler() .
00357 {
00358 /* Fetch data if bytes to be read. */
00359 if (twi- & gt; bytesRead & lt; TWIM_READ_BUFFER_SIZE ) {
00360 uint8_t data = twi- & gt; interface - & gt;MASTER.DATA;
00361 twi- & gt; readData [twi- & gt; bytesRead ] = data;
00362 twi- & gt; bytesRead ++;
00363 }
00364
00365 /* If buffer overflow, issue STOP and BUFFER_OVERFLOW condition. */
00366 else {
00367 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00368 TWI_MasterTransactionFinished (twi, TWIM_RESULT_BUFFER_OVERFLOW );
00369 }
00370
00371 /* Local variable used in if test to avoid compiler warning. */
00372 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00373
00374 /* If more bytes to read, issue ACK and start a byte read. */
00375 if (twi- & gt; bytesRead & lt; bytesToRead) {
00376 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
00377 }
00378
00379 /* If transaction finished, issue NACK and STOP condition. */
00380 else {
00381 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_ACKACT_bm |
00382 TWI_MASTER_CMD_STOP_gc;
00383 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00384 }
00385 }





Here is the call graph for this function:














bool TWI_MasterReady
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Returns true if transaction is ready.

This function returns a boolean whether the TWI Master is ready for a new transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 129 of file twi_master_driver.c .

References TWI_Master::status , and TWIM_STATUS_READY .
00130 {
00131 bool twi_status = (twi- & gt; status & amp; TWIM_STATUS_READY );
00132 return twi_status;
00133 }










TWI_MASTER_BUSSTATE_t TWI_MasterState
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Returns the TWI bus state.

Returns the TWI bus state (type defined in device headerfile), unknown, idle, owner or busy.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.


Return values:

TWI_MASTER_BUSSTATE_UNKNOWN_gc & nbsp; Bus state is unknown.
TWI_MASTER_BUSSTATE_IDLE_gc & nbsp; Bus state is idle.
TWI_MASTER_BUSSTATE_OWNER_gc & nbsp; Bus state is owned by the master.
TWI_MASTER_BUSSTATE_BUSY_gc & nbsp; Bus state is busy.



Definition at line 110 of file twi_master_driver.c .

References TWI_Master::interface .
00111 {
00112 TWI_MASTER_BUSSTATE_t twi_status;
00113 twi_status = (TWI_MASTER_BUSSTATE_t) (twi- & gt; interface - & gt;MASTER.STATUS & amp;
00114 TWI_MASTER_BUSSTATE_gm);
00115 return twi_status;
00116 }










void TWI_MasterTransactionFinished
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
result & nbsp;



)







TWI transaction finished handler.

Prepares module for new transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
result & nbsp; The result of the operation.



Definition at line 395 of file twi_master_driver.c .

References TWI_Master::result , TWI_Master::status , and TWIM_STATUS_READY .

Referenced by TWI_MasterInterruptHandler() , TWI_MasterReadHandler() , and TWI_MasterWriteHandler() .
00396 {
00397 twi- & gt; result = result;
00398 twi- & gt; status = TWIM_STATUS_READY ;
00399 }










bool TWI_MasterWrite
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t * & nbsp;
writeData ,




uint8_t & nbsp;
bytesToWrite & nbsp;



)







TWI write transaction.

This function is TWI Master wrapper for a write-only transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; Slave address.
writeData & nbsp; Pointer to data to write.
bytesToWrite & nbsp; Number of data bytes to write.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 148 of file twi_master_driver.c .

References TWI_MasterWriteRead() .
00152 {
00153 bool twi_status = TWI_MasterWriteRead (twi, address, writeData, bytesToWrite, 0);
00154 return twi_status;
00155 }





Here is the call graph for this function:














void TWI_MasterWriteHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master write interrupt handler.

Handles TWI transactions (master write) and responses to (N)ACK.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 313 of file twi_master_driver.c .

References TWI_Master::address , TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::bytesToWrite , TWI_Master::bytesWritten , TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWI_MasterTransactionFinished() , TWIM_RESULT_NACK_RECEIVED , TWIM_RESULT_OK , TWIM_STATUS_READY , and TWI_Master::writeData .

Referenced by TWI_MasterInterruptHandler() .
00314 {
00315 /* Local variables used in if tests to avoid compiler warning. */
00316 uint8_t bytesToWrite = twi- & gt; bytesToWrite ;
00317 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00318
00319 /* If NOT acknowledged (NACK) by slave cancel the transaction. */
00320 if (twi- & gt; interface - & gt;MASTER.STATUS & amp; TWI_MASTER_RXACK_bm) {
00321 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00322 twi- & gt; result = TWIM_RESULT_NACK_RECEIVED ;
00323 twi- & gt; status = TWIM_STATUS_READY ;
00324 }
00325
00326 /* If more bytes to write, send data. */
00327 else if (twi- & gt; bytesWritten & lt; bytesToWrite) {
00328 uint8_t data = twi- & gt; writeData [twi- & gt; bytesWritten ];
00329 twi- & gt; interface - & gt;MASTER.DATA = data;
00330 ++twi- & gt; bytesWritten ;
00331 }
00332
00333 /* If bytes to read, send repeated START condition + Address +
00334 * 'R/_W = 1'
00335 */
00336 else if (twi- & gt; bytesRead & lt; bytesToRead) {
00337 uint8_t readAddress = twi- & gt; address | 0x01;
00338 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00339 }
00340
00341 /* If transaction finished, send STOP condition and set RESULT OK. */
00342 else {
00343 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00344 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00345 }
00346 }





Here is the call graph for this function:














bool TWI_MasterWriteRead
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t * & nbsp;
writeData ,




uint8_t & nbsp;
bytesToWrite ,




uint8_t & nbsp;
bytesToRead & nbsp;



)







TWI write and/or read transaction.

This function is a TWI Master write and/or read transaction. The function can be used to both write and/or read bytes to/from the TWI Slave in one transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; The slave address.
writeData & nbsp; Pointer to data to write.
bytesToWrite & nbsp; Number of bytes to write.
bytesToRead & nbsp; Number of bytes to read.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 193 of file twi_master_driver.c .

References TWI_Master::address , TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::bytesToWrite , TWI_Master::bytesWritten , TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWIM_READ_BUFFER_SIZE , TWIM_RESULT_UNKNOWN , TWIM_STATUS_BUSY , TWIM_STATUS_READY , TWIM_WRITE_BUFFER_SIZE , and TWI_Master::writeData .

Referenced by main() , TWI_MasterRead() , and TWI_MasterWrite() .
00198 {
00199 /*Parameter sanity check. */
00200 if (bytesToWrite & gt; TWIM_WRITE_BUFFER_SIZE ) {
00201 return false ;
00202 }
00203 if (bytesToRead & gt; TWIM_READ_BUFFER_SIZE ) {
00204 return false ;
00205 }
00206
00207 /*Initiate transaction if bus is ready. */
00208 if (twi- & gt; status == TWIM_STATUS_READY ) {
00209
00210 twi- & gt; status = TWIM_STATUS_BUSY ;
00211 twi- & gt; result = TWIM_RESULT_UNKNOWN ;
00212
00213 twi- & gt; address = address & lt; & lt;1;
00214
00215 /* Fill write data buffer. */
00216 for (uint8_t bufferIndex=0; bufferIndex & lt; bytesToWrite; bufferIndex++) {
00217 twi- & gt; writeData [bufferIndex] = writeData[bufferIndex];
00218 }
00219
00220 twi- & gt; bytesToWrite = bytesToWrite;
00221 twi- & gt; bytesToRead = bytesToRead;
00222 twi- & gt; bytesWritten = 0;
00223 twi- & gt; bytesRead = 0;
00224
00225 /* If write command, send the START condition + Address +
00226 * 'R/_W = 0'
00227 */
00228 if (twi- & gt; bytesToWrite & gt; 0) {
00229 uint8_t writeAddress = twi- & gt; address & amp; ~0x01;
00230 twi- & gt; interface - & gt;MASTER.ADDR = writeAddress;
00231 }
00232
00233 /* If read command, send the START condition + Address +
00234 * 'R/_W = 1'
00235 */
00236 else if (twi- & gt; bytesToRead & gt; 0) {
00237 uint8_t readAddress = twi- & gt; address | 0x01;
00238 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00239 }
00240 return true ;
00241 } else {
00242 return false ;
00243 }
00244 }









@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > files.html

@DOC_TITLE@







Xmega Application Note








File List Here is a list of all files with brief descriptions:
avr_compiler.h [code] This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code base for the AVR architecture
documentation.h [code]
twi_example.c [code] XMEGA TWI driver example source
twi_master_driver.c [code] XMEGA TWI master driver source file
twi_master_driver.h [code] XMEGA TWI master driver header file
twi_slave_driver.c [code] XMEGA TWI slave driver source file
twi_slave_driver.h [code] XMEGA TWI slave driver header file





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > globals.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines




a
b
c
f
i
m
n
s
t



Here is a list of all functions, variables, defines, enums, and typedefs with links to the files they belong to:

- a -
AVR_ENTER_CRITICAL_REGION
: avr_compiler.h
AVR_LEAVE_CRITICAL_REGION
: avr_compiler.h

- b -
BAUDRATE
: twi_example.c

- c -
CPU_SPEED
: twi_example.c

- f -
F_CPU
: avr_compiler.h

- i -
ISR()
: twi_example.c

- m -
main()
: twi_example.c

- n -
NUM_BYTES
: twi_example.c

- s -
sendBuffer
: twi_example.c
SLAVE_ADDRESS
: twi_example.c

- t -
TWI_BAUD
: twi_master_driver.h
TWI_BAUDSETTING
: twi_example.c
TWI_Master_t
: twi_master_driver.h
TWI_MasterArbitrationLostBusErrorHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterInit()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterInterruptHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterRead()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterReadHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterReady()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterState()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterTransactionFinished()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterWrite()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterWriteHandler()
: twi_master_driver.c
, twi_master_driver.h
TWI_MasterWriteRead()
: twi_master_driver.c
, twi_master_driver.h
TWI_Slave_t
: twi_slave_driver.h
TWI_SlaveAddressMatchHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveDataHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveInitializeDriver()
: twi_slave_driver.h
, twi_slave_driver.c
TWI_SlaveInitializeModule()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveInterruptHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveReadHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveStopHandler()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveTransactionFinished()
: twi_slave_driver.c
, twi_slave_driver.h
TWI_SlaveWriteHandler()
: twi_slave_driver.h
, twi_slave_driver.c
TWIC_SlaveProcessData()
: twi_example.c
TWIM_READ_BUFFER_SIZE
: twi_master_driver.h
TWIM_RESULT_ARBITRATION_LOST
: twi_master_driver.h
TWIM_RESULT_BUFFER_OVERFLOW
: twi_master_driver.h
TWIM_RESULT_BUS_ERROR
: twi_master_driver.h
TWIM_RESULT_enum
: twi_master_driver.h
TWIM_RESULT_FAIL
: twi_master_driver.h
TWIM_RESULT_NACK_RECEIVED
: twi_master_driver.h
TWIM_RESULT_OK
: twi_master_driver.h
TWIM_RESULT_t
: twi_master_driver.h
TWIM_RESULT_UNKNOWN
: twi_master_driver.h
TWIM_STATUS_BUSY
: twi_master_driver.h
TWIM_STATUS_READY
: twi_master_driver.h
TWIM_WRITE_BUFFER_SIZE
: twi_master_driver.h
twiMaster
: twi_example.c
TWIS_RECEIVE_BUFFER_SIZE
: twi_slave_driver.h
TWIS_RESULT_ABORTED
: twi_slave_driver.h
TWIS_RESULT_BUFFER_OVERFLOW
: twi_slave_driver.h
TWIS_RESULT_BUS_ERROR
: twi_slave_driver.h
TWIS_RESULT_enum
: twi_slave_driver.h
TWIS_RESULT_FAIL
: twi_slave_driver.h
TWIS_RESULT_OK
: twi_slave_driver.h
TWIS_RESULT_t
: twi_slave_driver.h
TWIS_RESULT_TRANSMIT_COLLISION
: twi_slave_driver.h
TWIS_RESULT_UNKNOWN
: twi_slave_driver.h
TWIS_SEND_BUFFER_SIZE
: twi_slave_driver.h
TWIS_STATUS_BUSY
: twi_slave_driver.h
TWIS_STATUS_READY
: twi_slave_driver.h
twiSlave
: twi_example.c





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > structTWI__Slave.html

@DOC_TITLE@







Xmega Application Note








TWI_Slave Struct Reference TWI slave driver struct.
More...

#include & lt; twi_slave_driver.h & gt;



Data Fields
bool & nbsp; abort

register8_t & nbsp; bytesReceived

register8_t & nbsp; bytesSent

TWI_t * & nbsp; interface

void(* & nbsp; Process_Data )(void)

register8_t & nbsp; receivedData [TWIS_RECEIVE_BUFFER_SIZE]

register8_t & nbsp; result

register8_t & nbsp; sendData [TWIS_SEND_BUFFER_SIZE]

register8_t & nbsp; status


Detailed Description
TWI slave driver struct.

TWI slave struct. Holds pointer to TWI module and data processing routine, buffers and necessary varibles.
Definition at line 91 of file twi_slave_driver.h .
Field Documentation





bool TWI_Slave::abort






Strobe to abort
Definition at line 100 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInitializeDriver() , and TWI_SlaveReadHandler() .








register8_t TWI_Slave::bytesReceived






Number of bytes received
Definition at line 96 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInitializeDriver() , TWI_SlaveInterruptHandler() , TWI_SlaveReadHandler() , and TWIC_SlaveProcessData() .








register8_t TWI_Slave::bytesSent






Number of bytes sent
Definition at line 97 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInitializeDriver() , TWI_SlaveInterruptHandler() , and TWI_SlaveWriteHandler() .








TWI_t* TWI_Slave::interface






Pointer to what interface to use
Definition at line 92 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveDataHandler() , TWI_SlaveInitializeDriver() , TWI_SlaveInitializeModule() , TWI_SlaveInterruptHandler() , TWI_SlaveReadHandler() , TWI_SlaveStopHandler() , and TWI_SlaveWriteHandler() .








void(* TWI_Slave::Process_Data )(void)






Pointer to process data function
Referenced by TWI_SlaveInitializeDriver() , and TWI_SlaveReadHandler() .








register8_t TWI_Slave::receivedData [TWIS_RECEIVE_BUFFER_SIZE]






Read data
Definition at line 94 of file twi_slave_driver.h .

Referenced by TWI_SlaveReadHandler() , and TWIC_SlaveProcessData() .








register8_t TWI_Slave::result






Result of transaction
Definition at line 99 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInitializeDriver() , TWI_SlaveInterruptHandler() , and TWI_SlaveTransactionFinished() .








register8_t TWI_Slave::sendData [TWIS_SEND_BUFFER_SIZE]






Data to write
Definition at line 95 of file twi_slave_driver.h .

Referenced by TWI_SlaveWriteHandler() , and TWIC_SlaveProcessData() .








register8_t TWI_Slave::status






Status of transaction
Definition at line 98 of file twi_slave_driver.h .

Referenced by TWI_SlaveAddressMatchHandler() , TWI_SlaveInitializeDriver() , TWI_SlaveInterruptHandler() , and TWI_SlaveTransactionFinished() .







@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > avr__compiler_8h_source.html

@DOC_TITLE@







Xmega Application Note







avr_compiler.h Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00045 #ifndef COMPILER_AVR_H
00046 #define COMPILER_AVR_H
00047
00048 #ifndef F_CPU
00049
00050 #define F_CPU 2000000UL
00051 #endif
00052
00053 #include & lt;stdint.h & gt;
00054 #include & lt;stdbool.h & gt;
00055 #include & lt;stdlib.h & gt;
00056
00058 #define AVR_ENTER_CRITICAL_REGION( ) uint8_t volatile saved_sreg = SREG; \
00059 cli();
00060
00064 #define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;
00065
00066 #if defined( __ICCAVR__ )
00067
00068 #include & lt;inavr.h & gt;
00069 #include & lt;ioavr.h & gt;
00070 #include & lt;intrinsics.h & gt;
00071 #include & lt;pgmspace.h & gt;
00072
00073 #ifndef __HAS_ELPM__
00074 #define _MEMATTR __flash
00075 #else /* __HAS_ELPM__ */
00076 #define _MEMATTR __farflash
00077 #endif /* __HAS_ELPM__ */
00078
00089 #define delay_us( us ) ( __delay_cycles( ( F_CPU / 1000000UL ) * ( us ) ) )
00090
00104 #define PRAGMA(x) _Pragma( #x )
00105 #define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void)
00106 #define sei( ) (__enable_interrupt( ))
00107 #define cli( ) (__disable_interrupt( ))
00108
00110 #define nop( ) (__no_operation())
00111
00113 #define watchdog_reset( ) (__watchdog_reset( ))
00114
00115
00116 #define INLINE PRAGMA( inline=forced ) static
00117
00118 #define FLASH_DECLARE(x) _MEMATTR x
00119 #define FLASH_STRING(x) ((_MEMATTR const char *)(x))
00120 #define FLASH_STRING_T char const _MEMATTR *
00121 #define FLASH_BYTE_ARRAY_T uint8_t const _MEMATTR *
00122 #define PGM_READ_BYTE(x) *(x)
00123 #define PGM_READ_WORD(x) *(x)
00124
00125 #define SHORTENUM
00126
00127 #elif defined( __GNUC__ )
00128
00129 #include & lt;avr/io.h & gt;
00130 #include & lt;avr/interrupt.h & gt;
00131 #include & lt;avr/pgmspace.h & gt;
00132 #include & lt;util/delay.h & gt;
00133
00135 #define delay_us( us ) (_delay_us( us ))
00136
00137 #define INLINE static inline
00138
00140 #define nop() do { __asm__ __volatile__ ( " nop " ); } while (0)
00141
00142 #define MAIN_TASK_PROLOGUE int
00143
00144
00145 #define MAIN_TASK_EPILOGUE() return -1;
00146
00147 #define SHORTENUM __attribute__ ((packed))
00148
00149 #else
00150 #error Compiler not supported.
00151 #endif
00152
00153 #endif
00154




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__example_8c.html

@DOC_TITLE@







Xmega Application Note








twi_example.c File Reference XMEGA TWI driver example source.
More...

#include & quot; avr_compiler.h & quot;
#include & quot; twi_master_driver.h & quot;
#include & quot; twi_slave_driver.h & quot;



Include dependency graph for twi_example.c:










Go to the source code of this file.

Defines
#define & nbsp; BAUDRATE & nbsp; & nbsp; & nbsp;100000

#define & nbsp; CPU_SPEED & nbsp; & nbsp; & nbsp;2000000

#define & nbsp; NUM_BYTES & nbsp; & nbsp; & nbsp;8

#define & nbsp; SLAVE_ADDRESS & nbsp; & nbsp; & nbsp;0x55

#define & nbsp; TWI_BAUDSETTING & nbsp; & nbsp; & nbsp;TWI_BAUD(CPU_SPEED, BAUDRATE)

Functions
& nbsp; ISR (TWIC_TWIS_vect)

& nbsp; ISR (TWIC_TWIM_vect)

int & nbsp; main (void)

void & nbsp; TWIC_SlaveProcessData (void)

Variables
uint8_t & nbsp; sendBuffer [NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD}

TWI_Master_t & nbsp; twiMaster

TWI_Slave_t & nbsp; twiSlave


Detailed Description
XMEGA TWI driver example source.

This file contains an example application that demonstrates the TWI master and slave driver. It shows how to set up one TWI module as both master and slave, and communicate with itself.
The recommended test setup for this application is to connect 10K pull-up resistors on PC0 (SDA) and PC1 (SCL). Connect a 10-pin cable between the PORTD and SWITCHES, and PORTE and LEDS.
Application note: AVR1308: Using the XMEGA TWI
Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 2660
Date 2009-08-11 12:28:58 +0200 (ti, 11 aug 2009)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_example.c .
Define Documentation





#define BAUDRATE & nbsp; & nbsp; & nbsp;100000







Definition at line 66 of file twi_example.c .








#define CPU_SPEED & nbsp; & nbsp; & nbsp;2000000






CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings
Definition at line 65 of file twi_example.c .








#define NUM_BYTES & nbsp; & nbsp; & nbsp;8






Defining number of bytes in buffer.
Definition at line 62 of file twi_example.c .








#define SLAVE_ADDRESS & nbsp; & nbsp; & nbsp;0x55






Defining an example slave address.
Definition at line 59 of file twi_example.c .

Referenced by main() .








#define TWI_BAUDSETTING & nbsp; & nbsp; & nbsp;TWI_BAUD(CPU_SPEED, BAUDRATE)







Definition at line 67 of file twi_example.c .

Referenced by main() .



Function Documentation





ISR
(
TWIC_TWIS_vect & nbsp;

& nbsp;) & nbsp;







TWIC Slave Interrupt vector.
Definition at line 173 of file twi_example.c .

References TWI_SlaveInterruptHandler() .
00174 {
00175 TWI_SlaveInterruptHandler ( & amp; twiSlave );
00176 }





Here is the call graph for this function:




















ISR
(
TWIC_TWIM_vect & nbsp;

& nbsp;) & nbsp;







TWIC Master Interrupt vector.
Definition at line 167 of file twi_example.c .

References TWI_MasterInterruptHandler() .
00168 {
00169 TWI_MasterInterruptHandler ( & amp; twiMaster );
00170 }





Here is the call graph for this function:


















int main
(
void & nbsp;

& nbsp;) & nbsp;







/brief Example code
Example code that reads the key pressed and show a value from the buffer, sends the value to the slave and read back the processed value which will be inverted and displayed after key release.
Definition at line 95 of file twi_example.c .

References TWI_Master::readData , sendBuffer , SLAVE_ADDRESS , TWI_Master::status , TWI_BAUDSETTING , TWI_MasterInit() , TWI_MasterWriteRead() , TWI_SlaveInitializeDriver() , TWI_SlaveInitializeModule() , TWIC_SlaveProcessData() , and TWIM_STATUS_READY .
00096 {
00097 /* Initialize PORTE for output and PORTD for inverted input. */
00098 PORTE.DIRSET = 0xFF;
00099 PORTD.DIRCLR = 0xFF;
00100 PORTCFG.MPCMASK = 0xFF;
00101 PORTD.PIN0CTRL |= PORT_INVEN_bm;
00102 // PORTCFG.MPCMASK = 0xFF;
00103 // PORTD.PIN0CTRL = (PORTD.PIN0CTRL & amp; ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc;
00104
00105 // Enable internal pull-up on PC0, PC1.. Uncomment if you don't have external pullups
00106 // PORTCFG.MPCMASK = 0x03; // Configure several PINxCTRL registers at the same time
00107 // PORTC.PIN0CTRL = (PORTC.PIN0CTRL & amp; ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches
00108
00109
00110
00111 /* Initialize TWI master. */
00112 TWI_MasterInit ( & amp; twiMaster ,
00113 & amp;TWIC,
00114 TWI_MASTER_INTLVL_LO_gc,
00115 TWI_BAUDSETTING );
00116
00117 /* Initialize TWI slave. */
00118 TWI_SlaveInitializeDriver ( & amp; twiSlave , & amp;TWIC, TWIC_SlaveProcessData );
00119 TWI_SlaveInitializeModule ( & amp; twiSlave ,
00120 SLAVE_ADDRESS ,
00121 TWI_SLAVE_INTLVL_LO_gc);
00122
00123 /* Enable LO interrupt level. */
00124 PMIC.CTRL |= PMIC_LOLVLEN_bm;
00125 sei();
00126
00127 uint8_t BufPos = 0;
00128 while (1) {
00129 while (!PORTD.IN); /* Wait for user to press button */
00130
00131 switch (PORTD.IN){
00132 case (PIN0_bm): BufPos = 0; break ;
00133 case (PIN1_bm): BufPos = 1; break ;
00134 case (PIN2_bm): BufPos = 2; break ;
00135 case (PIN3_bm): BufPos = 3; break ;
00136 case (PIN4_bm): BufPos = 4; break ;
00137 case (PIN5_bm): BufPos = 5; break ;
00138 case (PIN6_bm): BufPos = 6; break ;
00139 case (PIN7_bm): BufPos = 7; break ;
00140 default : break ;
00141 }
00142
00143 /* Show the byte to send while holding down the key. */
00144 while (PORTD.IN != 0x00){
00145 PORTE.OUT = sendBuffer [BufPos];
00146 }
00147
00148 TWI_MasterWriteRead ( & amp; twiMaster ,
00149 SLAVE_ADDRESS ,
00150 & amp; sendBuffer [BufPos],
00151 1,
00152 1);
00153
00154
00155 while ( twiMaster . status != TWIM_STATUS_READY ) {
00156 /* Wait until transaction is complete. */
00157 }
00158
00159 /* Show the sent byte received and processed on LEDs. */
00160 PORTE.OUT = ( twiMaster . readData [0]);
00161
00162 while (PORTD.IN); /* Wait for user to release button */
00163 }
00164 }





Here is the call graph for this function:


















void TWIC_SlaveProcessData
(
void & nbsp;

& nbsp;) & nbsp;







Simple function that invert the received value in the sendbuffer. This function is used in the driver and passed on as a pointer to the driver.
Definition at line 82 of file twi_example.c .

References TWI_Slave::bytesReceived , TWI_Slave::receivedData , and TWI_Slave::sendData .

Referenced by main() .
00083 {
00084 uint8_t bufIndex = twiSlave . bytesReceived ;
00085 twiSlave . sendData [bufIndex] = (~ twiSlave . receivedData [bufIndex]);
00086 }





Variable Documentation





uint8_t sendBuffer [NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD}






Buffer with test data to send.
Definition at line 76 of file twi_example.c .

Referenced by main() .








TWI_Master_t twiMaster






TWI master module.
Definition at line 71 of file twi_example.c .








TWI_Slave_t twiSlave






TWI slave module.
Definition at line 72 of file twi_example.c .







@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > atmel-header.html

@DOC_TITLE@







Xmega Application Note


XMEGA_TWI.zip > documentation_8h.html

@DOC_TITLE@







Xmega Application Note








documentation.h File Reference

Go to the source code of this file.






@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > index.html

AVR1308 Using the XMEGA TWI




Frames are disabled. Click here to go to the main page.


XMEGA_TWI.zip > globals_vars.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines



& nbsp;


sendBuffer
: twi_example.c
twiMaster
: twi_example.c
twiSlave
: twi_example.c





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > graph_legend.html

@DOC_TITLE@







Xmega Application Note








Graph Legend This page explains how to interpret the graphs that are generated by doxygen.
Consider the following example: /*! Invisible class because of truncation */
class Invisible { };

/*! Truncated class, inheritance relation is hidden */
class Truncated : public Invisible { };

/* Class not documented with doxygen comments */
class Undocumented { };

/*! Class that is inherited using public inheritance */
class PublicBase : public Truncated { };

/*! A template class */
template & lt; class T & gt; class Templ { };

/*! Class that is inherited using protected inheritance */
class ProtectedBase { };

/*! Class that is inherited using private inheritance */
class PrivateBase { };

/*! Class that is used by the Inherited class */
class Used { };

/*! Super class that inherits a number of other classes */
class Inherited : public PublicBase,
protected ProtectedBase,
private PrivateBase,
public Undocumented,
public Templ & lt;int & gt;
{
private :
Used *m_usedClass;
};
This will result in the following graph:




The boxes in the above graph have the following meaning:

A filled gray box represents the struct or class for which the graph is generated.

A box with a black border denotes a documented struct or class.

A box with a grey border denotes an undocumented struct or class.

A box with a red border denotes a documented struct or class forwhich not all inheritance/containment relations are shown. A graph is truncated if it does not fit within the specified boundaries.

The arrows have the following meaning:

A dark blue arrow is used to visualize a public inheritance relation between two classes.

A dark green arrow is used for protected inheritance.

A dark red arrow is used for private inheritance.

A purple dashed arrow is used if a class is contained or used by another class. The arrow is labeled with the variable(s) through which the pointed class or struct is accessible.

A yellow dashed arrow denotes a relation between a template instance and the template class it was instantiated from. The arrow is labeled with the template parameters of the instance.





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__master__driver_8c.html

@DOC_TITLE@







Xmega Application Note








twi_master_driver.c File Reference XMEGA TWI master driver source file.
More...

#include & quot; twi_master_driver.h & quot;



Include dependency graph for twi_master_driver.c:









Go to the source code of this file.

Functions
void & nbsp; TWI_MasterArbitrationLostBusErrorHandler ( TWI_Master_t *twi)

& nbsp; TWI master arbitration lost and bus error interrupt handler.
void & nbsp; TWI_MasterInit ( TWI_Master_t *twi, TWI_t *module, TWI_MASTER_INTLVL_t intLevel, uint8_t baudRateRegisterSetting)

& nbsp; Initialize the TWI module.
void & nbsp; TWI_MasterInterruptHandler ( TWI_Master_t *twi)

& nbsp; Common TWI master interrupt service routine.
bool & nbsp; TWI_MasterRead ( TWI_Master_t *twi, uint8_t address, uint8_t bytesToRead)

& nbsp; TWI read transaction.
void & nbsp; TWI_MasterReadHandler ( TWI_Master_t *twi)

& nbsp; TWI master read interrupt handler.
bool & nbsp; TWI_MasterReady ( TWI_Master_t *twi)

& nbsp; Returns true if transaction is ready.
TWI_MASTER_BUSSTATE_t & nbsp; TWI_MasterState ( TWI_Master_t *twi)

& nbsp; Returns the TWI bus state.
void & nbsp; TWI_MasterTransactionFinished ( TWI_Master_t *twi, uint8_t result)

& nbsp; TWI transaction finished handler.
bool & nbsp; TWI_MasterWrite ( TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite)

& nbsp; TWI write transaction.
void & nbsp; TWI_MasterWriteHandler ( TWI_Master_t *twi)

& nbsp; TWI master write interrupt handler.
bool & nbsp; TWI_MasterWriteRead ( TWI_Master_t *twi, uint8_t address, uint8_t *writeData, uint8_t bytesToWrite, uint8_t bytesToRead)

& nbsp; TWI write and/or read transaction.

Detailed Description
XMEGA TWI master driver source file.

This file contains the function implementations the XMEGA master TWI driver.
The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA TWI master module.
For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.
Several functions use the following construct: " some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ... " Although the use of the ternary operator ( if ? then : else ) is discouraged, in some occasions the operator makes it possible to write pretty clean and neat code. In this driver, the construct is used to set or not set a configuration bit based on a boolean input parameter, such as the " some_parameter " in the example above.
Application note: AVR1308: Using the XMEGA TWI
Documentation For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author: Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision 1569
Date 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)


Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_master_driver.c .
Function Documentation





void TWI_MasterArbitrationLostBusErrorHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master arbitration lost and bus error interrupt handler.

Handles TWI responses to lost arbitration and bus error.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 287 of file twi_master_driver.c .

References TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWIM_RESULT_ARBITRATION_LOST , TWIM_RESULT_BUS_ERROR , and TWIM_STATUS_READY .

Referenced by TWI_MasterInterruptHandler() .
00288 {
00289 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00290
00291 /* If bus error. */
00292 if (currentStatus & amp; TWI_MASTER_BUSERR_bm) {
00293 twi- & gt; result = TWIM_RESULT_BUS_ERROR ;
00294 }
00295 /* If arbitration lost. */
00296 else {
00297 twi- & gt; result = TWIM_RESULT_ARBITRATION_LOST ;
00298 }
00299
00300 /* Clear interrupt flag. */
00301 twi- & gt; interface - & gt;MASTER.STATUS = currentStatus | TWI_MASTER_ARBLOST_bm;
00302
00303 twi- & gt; status = TWIM_STATUS_READY ;
00304 }










void TWI_MasterInit
(
TWI_Master_t * & nbsp;
twi ,




TWI_t * & nbsp;
module ,




TWI_MASTER_INTLVL_t & nbsp;
intLevel ,




uint8_t & nbsp;
baudRateRegisterSetting & nbsp;



)







Initialize the TWI module.

TWI module initialization function. Enables master read and write interrupts. Remember to enable interrupts globally from the main application.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
module & nbsp; The TWI module to use.
intLevel & nbsp; Master interrupt level.
baudRateRegisterSetting & nbsp; The baud rate register value.



Definition at line 83 of file twi_master_driver.c .

References TWI_Master::interface .

Referenced by main() .
00087 {
00088 twi- & gt; interface = module;
00089 twi- & gt; interface - & gt;MASTER.CTRLA = intLevel |
00090 TWI_MASTER_RIEN_bm |
00091 TWI_MASTER_WIEN_bm |
00092 TWI_MASTER_ENABLE_bm;
00093 twi- & gt; interface - & gt;MASTER.BAUD = baudRateRegisterSetting;
00094 twi- & gt; interface - & gt;MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
00095 }










void TWI_MasterInterruptHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Common TWI master interrupt service routine.

Check current status and calls the appropriate handler.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 253 of file twi_master_driver.c .

References TWI_Master::interface , TWI_MasterArbitrationLostBusErrorHandler() , TWI_MasterReadHandler() , TWI_MasterTransactionFinished() , TWI_MasterWriteHandler() , and TWIM_RESULT_FAIL .

Referenced by ISR() .
00254 {
00255 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00256
00257 /* If arbitration lost or bus error. */
00258 if ((currentStatus & amp; TWI_MASTER_ARBLOST_bm) ||
00259 (currentStatus & amp; TWI_MASTER_BUSERR_bm)) {
00260
00261 TWI_MasterArbitrationLostBusErrorHandler (twi);
00262 }
00263
00264 /* If master write interrupt. */
00265 else if (currentStatus & amp; TWI_MASTER_WIF_bm) {
00266 TWI_MasterWriteHandler (twi);
00267 }
00268
00269 /* If master read interrupt. */
00270 else if (currentStatus & amp; TWI_MASTER_RIF_bm) {
00271 TWI_MasterReadHandler (twi);
00272 }
00273
00274 /* If unexpected state. */
00275 else {
00276 TWI_MasterTransactionFinished (twi, TWIM_RESULT_FAIL );
00277 }
00278 }





Here is the call graph for this function:

















bool TWI_MasterRead
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t & nbsp;
bytesToRead & nbsp;



)







TWI read transaction.

This function is a TWI Maste wrapper for read-only transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; The slave address.
bytesToRead & nbsp; The number of bytes to read.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 169 of file twi_master_driver.c .

References TWI_MasterWriteRead() .
00172 {
00173 bool twi_status = TWI_MasterWriteRead (twi, address, 0, 0, bytesToRead);
00174 return twi_status;
00175 }





Here is the call graph for this function:














void TWI_MasterReadHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master read interrupt handler.

This is the master read interrupt handler that takes care of reading bytes from the TWI slave.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 356 of file twi_master_driver.c .

References TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::interface , TWI_Master::readData , TWI_MasterTransactionFinished() , TWIM_READ_BUFFER_SIZE , TWIM_RESULT_BUFFER_OVERFLOW , and TWIM_RESULT_OK .

Referenced by TWI_MasterInterruptHandler() .
00357 {
00358 /* Fetch data if bytes to be read. */
00359 if (twi- & gt; bytesRead & lt; TWIM_READ_BUFFER_SIZE ) {
00360 uint8_t data = twi- & gt; interface - & gt;MASTER.DATA;
00361 twi- & gt; readData [twi- & gt; bytesRead ] = data;
00362 twi- & gt; bytesRead ++;
00363 }
00364
00365 /* If buffer overflow, issue STOP and BUFFER_OVERFLOW condition. */
00366 else {
00367 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00368 TWI_MasterTransactionFinished (twi, TWIM_RESULT_BUFFER_OVERFLOW );
00369 }
00370
00371 /* Local variable used in if test to avoid compiler warning. */
00372 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00373
00374 /* If more bytes to read, issue ACK and start a byte read. */
00375 if (twi- & gt; bytesRead & lt; bytesToRead) {
00376 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
00377 }
00378
00379 /* If transaction finished, issue NACK and STOP condition. */
00380 else {
00381 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_ACKACT_bm |
00382 TWI_MASTER_CMD_STOP_gc;
00383 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00384 }
00385 }





Here is the call graph for this function:














bool TWI_MasterReady
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Returns true if transaction is ready.

This function returns a boolean whether the TWI Master is ready for a new transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 129 of file twi_master_driver.c .

References TWI_Master::status , and TWIM_STATUS_READY .
00130 {
00131 bool twi_status = (twi- & gt; status & amp; TWIM_STATUS_READY );
00132 return twi_status;
00133 }










TWI_MASTER_BUSSTATE_t TWI_MasterState
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







Returns the TWI bus state.

Returns the TWI bus state (type defined in device headerfile), unknown, idle, owner or busy.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.


Return values:

TWI_MASTER_BUSSTATE_UNKNOWN_gc & nbsp; Bus state is unknown.
TWI_MASTER_BUSSTATE_IDLE_gc & nbsp; Bus state is idle.
TWI_MASTER_BUSSTATE_OWNER_gc & nbsp; Bus state is owned by the master.
TWI_MASTER_BUSSTATE_BUSY_gc & nbsp; Bus state is busy.



Definition at line 110 of file twi_master_driver.c .

References TWI_Master::interface .
00111 {
00112 TWI_MASTER_BUSSTATE_t twi_status;
00113 twi_status = (TWI_MASTER_BUSSTATE_t) (twi- & gt; interface - & gt;MASTER.STATUS & amp;
00114 TWI_MASTER_BUSSTATE_gm);
00115 return twi_status;
00116 }










void TWI_MasterTransactionFinished
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
result & nbsp;



)







TWI transaction finished handler.

Prepares module for new transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
result & nbsp; The result of the operation.



Definition at line 395 of file twi_master_driver.c .

References TWI_Master::result , TWI_Master::status , and TWIM_STATUS_READY .

Referenced by TWI_MasterInterruptHandler() , TWI_MasterReadHandler() , and TWI_MasterWriteHandler() .
00396 {
00397 twi- & gt; result = result;
00398 twi- & gt; status = TWIM_STATUS_READY ;
00399 }










bool TWI_MasterWrite
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t * & nbsp;
writeData ,




uint8_t & nbsp;
bytesToWrite & nbsp;



)







TWI write transaction.

This function is TWI Master wrapper for a write-only transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; Slave address.
writeData & nbsp; Pointer to data to write.
bytesToWrite & nbsp; Number of data bytes to write.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 148 of file twi_master_driver.c .

References TWI_MasterWriteRead() .
00152 {
00153 bool twi_status = TWI_MasterWriteRead (twi, address, writeData, bytesToWrite, 0);
00154 return twi_status;
00155 }





Here is the call graph for this function:














void TWI_MasterWriteHandler
(
TWI_Master_t * & nbsp;
twi
& nbsp;) & nbsp;







TWI master write interrupt handler.

Handles TWI transactions (master write) and responses to (N)ACK.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.



Definition at line 313 of file twi_master_driver.c .

References TWI_Master::address , TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::bytesToWrite , TWI_Master::bytesWritten , TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWI_MasterTransactionFinished() , TWIM_RESULT_NACK_RECEIVED , TWIM_RESULT_OK , TWIM_STATUS_READY , and TWI_Master::writeData .

Referenced by TWI_MasterInterruptHandler() .
00314 {
00315 /* Local variables used in if tests to avoid compiler warning. */
00316 uint8_t bytesToWrite = twi- & gt; bytesToWrite ;
00317 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00318
00319 /* If NOT acknowledged (NACK) by slave cancel the transaction. */
00320 if (twi- & gt; interface - & gt;MASTER.STATUS & amp; TWI_MASTER_RXACK_bm) {
00321 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00322 twi- & gt; result = TWIM_RESULT_NACK_RECEIVED ;
00323 twi- & gt; status = TWIM_STATUS_READY ;
00324 }
00325
00326 /* If more bytes to write, send data. */
00327 else if (twi- & gt; bytesWritten & lt; bytesToWrite) {
00328 uint8_t data = twi- & gt; writeData [twi- & gt; bytesWritten ];
00329 twi- & gt; interface - & gt;MASTER.DATA = data;
00330 ++twi- & gt; bytesWritten ;
00331 }
00332
00333 /* If bytes to read, send repeated START condition + Address +
00334 * 'R/_W = 1'
00335 */
00336 else if (twi- & gt; bytesRead & lt; bytesToRead) {
00337 uint8_t readAddress = twi- & gt; address | 0x01;
00338 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00339 }
00340
00341 /* If transaction finished, send STOP condition and set RESULT OK. */
00342 else {
00343 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00344 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00345 }
00346 }





Here is the call graph for this function:














bool TWI_MasterWriteRead
(
TWI_Master_t * & nbsp;
twi ,




uint8_t & nbsp;
address ,




uint8_t * & nbsp;
writeData ,




uint8_t & nbsp;
bytesToWrite ,




uint8_t & nbsp;
bytesToRead & nbsp;



)







TWI write and/or read transaction.

This function is a TWI Master write and/or read transaction. The function can be used to both write and/or read bytes to/from the TWI Slave in one transaction.
Parameters:

twi & nbsp; The TWI_Master_t struct instance.
address & nbsp; The slave address.
writeData & nbsp; Pointer to data to write.
bytesToWrite & nbsp; Number of bytes to write.
bytesToRead & nbsp; Number of bytes to read.


Return values:

true & nbsp; If transaction could be started.
false & nbsp; If transaction could not be started.



Definition at line 193 of file twi_master_driver.c .

References TWI_Master::address , TWI_Master::bytesRead , TWI_Master::bytesToRead , TWI_Master::bytesToWrite , TWI_Master::bytesWritten , TWI_Master::interface , TWI_Master::result , TWI_Master::status , TWIM_READ_BUFFER_SIZE , TWIM_RESULT_UNKNOWN , TWIM_STATUS_BUSY , TWIM_STATUS_READY , TWIM_WRITE_BUFFER_SIZE , and TWI_Master::writeData .

Referenced by main() , TWI_MasterRead() , and TWI_MasterWrite() .
00198 {
00199 /*Parameter sanity check. */
00200 if (bytesToWrite & gt; TWIM_WRITE_BUFFER_SIZE ) {
00201 return false ;
00202 }
00203 if (bytesToRead & gt; TWIM_READ_BUFFER_SIZE ) {
00204 return false ;
00205 }
00206
00207 /*Initiate transaction if bus is ready. */
00208 if (twi- & gt; status == TWIM_STATUS_READY ) {
00209
00210 twi- & gt; status = TWIM_STATUS_BUSY ;
00211 twi- & gt; result = TWIM_RESULT_UNKNOWN ;
00212
00213 twi- & gt; address = address & lt; & lt;1;
00214
00215 /* Fill write data buffer. */
00216 for (uint8_t bufferIndex=0; bufferIndex & lt; bytesToWrite; bufferIndex++) {
00217 twi- & gt; writeData [bufferIndex] = writeData[bufferIndex];
00218 }
00219
00220 twi- & gt; bytesToWrite = bytesToWrite;
00221 twi- & gt; bytesToRead = bytesToRead;
00222 twi- & gt; bytesWritten = 0;
00223 twi- & gt; bytesRead = 0;
00224
00225 /* If write command, send the START condition + Address +
00226 * 'R/_W = 0'
00227 */
00228 if (twi- & gt; bytesToWrite & gt; 0) {
00229 uint8_t writeAddress = twi- & gt; address & amp; ~0x01;
00230 twi- & gt; interface - & gt;MASTER.ADDR = writeAddress;
00231 }
00232
00233 /* If read command, send the START condition + Address +
00234 * 'R/_W = 1'
00235 */
00236 else if (twi- & gt; bytesToRead & gt; 0) {
00237 uint8_t readAddress = twi- & gt; address | 0x01;
00238 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00239 }
00240 return true ;
00241 } else {
00242 return false ;
00243 }
00244 }









@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__example_8c_source.html

@DOC_TITLE@







Xmega Application Note







twi_example.c Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00054 #include " avr_compiler.h "
00055 #include " twi_master_driver.h "
00056 #include " twi_slave_driver.h "
00057
00059 #define SLAVE_ADDRESS 0x55
00060
00062 #define NUM_BYTES 8
00063
00065 #define CPU_SPEED 2000000
00066 #define BAUDRATE 100000
00067 #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
00068
00069
00070 /* Global variables */
00071 TWI_Master_t twiMaster ;
00072 TWI_Slave_t twiSlave ;
00076 uint8_t sendBuffer [ NUM_BYTES ] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD};
00077
00078
00082 void TWIC_SlaveProcessData ( void )
00083 {
00084 uint8_t bufIndex = twiSlave. bytesReceived ;
00085 twiSlave. sendData [bufIndex] = (~twiSlave. receivedData [bufIndex]);
00086 }
00087
00088
00095 int main ( void )
00096 {
00097 /* Initialize PORTE for output and PORTD for inverted input. */
00098 PORTE.DIRSET = 0xFF;
00099 PORTD.DIRCLR = 0xFF;
00100 PORTCFG.MPCMASK = 0xFF;
00101 PORTD.PIN0CTRL |= PORT_INVEN_bm;
00102 // PORTCFG.MPCMASK = 0xFF;
00103 // PORTD.PIN0CTRL = (PORTD.PIN0CTRL & amp; ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc;
00104
00105 // Enable internal pull-up on PC0, PC1.. Uncomment if you don't have external pullups
00106 // PORTCFG.MPCMASK = 0x03; // Configure several PINxCTRL registers at the same time
00107 // PORTC.PIN0CTRL = (PORTC.PIN0CTRL & amp; ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches
00108
00109
00110
00111 /* Initialize TWI master. */
00112 TWI_MasterInit ( & amp;twiMaster,
00113 & amp;TWIC,
00114 TWI_MASTER_INTLVL_LO_gc,
00115 TWI_BAUDSETTING );
00116
00117 /* Initialize TWI slave. */
00118 TWI_SlaveInitializeDriver ( & amp;twiSlave, & amp;TWIC, TWIC_SlaveProcessData );
00119 TWI_SlaveInitializeModule ( & amp;twiSlave,
00120 SLAVE_ADDRESS ,
00121 TWI_SLAVE_INTLVL_LO_gc);
00122
00123 /* Enable LO interrupt level. */
00124 PMIC.CTRL |= PMIC_LOLVLEN_bm;
00125 sei();
00126
00127 uint8_t BufPos = 0;
00128 while (1) {
00129 while (!PORTD.IN); /* Wait for user to press button */
00130
00131 switch (PORTD.IN){
00132 case (PIN0_bm): BufPos = 0; break ;
00133 case (PIN1_bm): BufPos = 1; break ;
00134 case (PIN2_bm): BufPos = 2; break ;
00135 case (PIN3_bm): BufPos = 3; break ;
00136 case (PIN4_bm): BufPos = 4; break ;
00137 case (PIN5_bm): BufPos = 5; break ;
00138 case (PIN6_bm): BufPos = 6; break ;
00139 case (PIN7_bm): BufPos = 7; break ;
00140 default : break ;
00141 }
00142
00143 /* Show the byte to send while holding down the key. */
00144 while (PORTD.IN != 0x00){
00145 PORTE.OUT = sendBuffer [BufPos];
00146 }
00147
00148 TWI_MasterWriteRead ( & amp;twiMaster,
00149 SLAVE_ADDRESS ,
00150 & amp; sendBuffer [BufPos],
00151 1,
00152 1);
00153
00154
00155 while (twiMaster. status != TWIM_STATUS_READY ) {
00156 /* Wait until transaction is complete. */
00157 }
00158
00159 /* Show the sent byte received and processed on LEDs. */
00160 PORTE.OUT = (twiMaster. readData [0]);
00161
00162 while (PORTD.IN); /* Wait for user to release button */
00163 }
00164 }
00165
00167 ISR (TWIC_TWIM_vect)
00168 {
00169 TWI_MasterInterruptHandler ( & amp;twiMaster);
00170 }
00171
00173 ISR (TWIC_TWIS_vect)
00174 {
00175 TWI_SlaveInterruptHandler ( & amp;twiSlave);
00176 }




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > annotated.html

@DOC_TITLE@







Xmega Application Note








Data Structures Here are the data structures with brief descriptions:
TWI_Master TWI master driver struct
TWI_Slave TWI slave driver struct





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > globals_defs.html

@DOC_TITLE@







Xmega Application Note









All
Functions
Variables
Typedefs
Enumerations
Enumerator
Defines



& nbsp;


AVR_ENTER_CRITICAL_REGION
: avr_compiler.h
AVR_LEAVE_CRITICAL_REGION
: avr_compiler.h
BAUDRATE
: twi_example.c
CPU_SPEED
: twi_example.c
F_CPU
: avr_compiler.h
NUM_BYTES
: twi_example.c
SLAVE_ADDRESS
: twi_example.c
TWI_BAUD
: twi_master_driver.h
TWI_BAUDSETTING
: twi_example.c
TWIM_READ_BUFFER_SIZE
: twi_master_driver.h
TWIM_STATUS_BUSY
: twi_master_driver.h
TWIM_STATUS_READY
: twi_master_driver.h
TWIM_WRITE_BUFFER_SIZE
: twi_master_driver.h
TWIS_RECEIVE_BUFFER_SIZE
: twi_slave_driver.h
TWIS_SEND_BUFFER_SIZE
: twi_slave_driver.h
TWIS_STATUS_BUSY
: twi_slave_driver.h
TWIS_STATUS_READY
: twi_slave_driver.h





@DOC_TITLE@












Generated on Tue Aug 11 12:42:13 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > twi__master__driver_8c_source.html

@DOC_TITLE@







Xmega Application Note







twi_master_driver.c Go to the documentation of this file. 00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00069 #include " twi_master_driver.h "
00070
00071
00083 void TWI_MasterInit ( TWI_Master_t *twi,
00084 TWI_t *module,
00085 TWI_MASTER_INTLVL_t intLevel,
00086 uint8_t baudRateRegisterSetting)
00087 {
00088 twi- & gt; interface = module;
00089 twi- & gt; interface - & gt;MASTER.CTRLA = intLevel |
00090 TWI_MASTER_RIEN_bm |
00091 TWI_MASTER_WIEN_bm |
00092 TWI_MASTER_ENABLE_bm;
00093 twi- & gt; interface - & gt;MASTER.BAUD = baudRateRegisterSetting;
00094 twi- & gt; interface - & gt;MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
00095 }
00096
00097
00110 TWI_MASTER_BUSSTATE_t TWI_MasterState ( TWI_Master_t *twi)
00111 {
00112 TWI_MASTER_BUSSTATE_t twi_status;
00113 twi_status = (TWI_MASTER_BUSSTATE_t) (twi- & gt; interface - & gt;MASTER.STATUS & amp;
00114 TWI_MASTER_BUSSTATE_gm);
00115 return twi_status;
00116 }
00117
00118
00129 bool TWI_MasterReady ( TWI_Master_t *twi)
00130 {
00131 bool twi_status = (twi- & gt; status & amp; TWIM_STATUS_READY );
00132 return twi_status;
00133 }
00134
00135
00148 bool TWI_MasterWrite ( TWI_Master_t *twi,
00149 uint8_t address,
00150 uint8_t *writeData,
00151 uint8_t bytesToWrite)
00152 {
00153 bool twi_status = TWI_MasterWriteRead (twi, address, writeData, bytesToWrite, 0);
00154 return twi_status;
00155 }
00156
00157
00169 bool TWI_MasterRead ( TWI_Master_t *twi,
00170 uint8_t address,
00171 uint8_t bytesToRead)
00172 {
00173 bool twi_status = TWI_MasterWriteRead (twi, address, 0, 0, bytesToRead);
00174 return twi_status;
00175 }
00176
00177
00193 bool TWI_MasterWriteRead ( TWI_Master_t *twi,
00194 uint8_t address,
00195 uint8_t *writeData,
00196 uint8_t bytesToWrite,
00197 uint8_t bytesToRead)
00198 {
00199 /*Parameter sanity check. */
00200 if (bytesToWrite & gt; TWIM_WRITE_BUFFER_SIZE ) {
00201 return false ;
00202 }
00203 if (bytesToRead & gt; TWIM_READ_BUFFER_SIZE ) {
00204 return false ;
00205 }
00206
00207 /*Initiate transaction if bus is ready. */
00208 if (twi- & gt; status == TWIM_STATUS_READY ) {
00209
00210 twi- & gt; status = TWIM_STATUS_BUSY ;
00211 twi- & gt; result = TWIM_RESULT_UNKNOWN ;
00212
00213 twi- & gt; address = address & lt; & lt;1;
00214
00215 /* Fill write data buffer. */
00216 for (uint8_t bufferIndex=0; bufferIndex & lt; bytesToWrite; bufferIndex++) {
00217 twi- & gt; writeData [bufferIndex] = writeData[bufferIndex];
00218 }
00219
00220 twi- & gt; bytesToWrite = bytesToWrite;
00221 twi- & gt; bytesToRead = bytesToRead;
00222 twi- & gt; bytesWritten = 0;
00223 twi- & gt; bytesRead = 0;
00224
00225 /* If write command, send the START condition + Address +
00226 * 'R/_W = 0'
00227 */
00228 if (twi- & gt; bytesToWrite & gt; 0) {
00229 uint8_t writeAddress = twi- & gt; address & amp; ~0x01;
00230 twi- & gt; interface - & gt;MASTER.ADDR = writeAddress;
00231 }
00232
00233 /* If read command, send the START condition + Address +
00234 * 'R/_W = 1'
00235 */
00236 else if (twi- & gt; bytesToRead & gt; 0) {
00237 uint8_t readAddress = twi- & gt; address | 0x01;
00238 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00239 }
00240 return true ;
00241 } else {
00242 return false ;
00243 }
00244 }
00245
00246
00253 void TWI_MasterInterruptHandler ( TWI_Master_t *twi)
00254 {
00255 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00256
00257 /* If arbitration lost or bus error. */
00258 if ((currentStatus & amp; TWI_MASTER_ARBLOST_bm) ||
00259 (currentStatus & amp; TWI_MASTER_BUSERR_bm)) {
00260
00261 TWI_MasterArbitrationLostBusErrorHandler (twi);
00262 }
00263
00264 /* If master write interrupt. */
00265 else if (currentStatus & amp; TWI_MASTER_WIF_bm) {
00266 TWI_MasterWriteHandler (twi);
00267 }
00268
00269 /* If master read interrupt. */
00270 else if (currentStatus & amp; TWI_MASTER_RIF_bm) {
00271 TWI_MasterReadHandler (twi);
00272 }
00273
00274 /* If unexpected state. */
00275 else {
00276 TWI_MasterTransactionFinished (twi, TWIM_RESULT_FAIL );
00277 }
00278 }
00279
00280
00287 void TWI_MasterArbitrationLostBusErrorHandler ( TWI_Master_t *twi)
00288 {
00289 uint8_t currentStatus = twi- & gt; interface - & gt;MASTER.STATUS;
00290
00291 /* If bus error. */
00292 if (currentStatus & amp; TWI_MASTER_BUSERR_bm) {
00293 twi- & gt; result = TWIM_RESULT_BUS_ERROR ;
00294 }
00295 /* If arbitration lost. */
00296 else {
00297 twi- & gt; result = TWIM_RESULT_ARBITRATION_LOST ;
00298 }
00299
00300 /* Clear interrupt flag. */
00301 twi- & gt; interface - & gt;MASTER.STATUS = currentStatus | TWI_MASTER_ARBLOST_bm;
00302
00303 twi- & gt; status = TWIM_STATUS_READY ;
00304 }
00305
00306
00313 void TWI_MasterWriteHandler ( TWI_Master_t *twi)
00314 {
00315 /* Local variables used in if tests to avoid compiler warning. */
00316 uint8_t bytesToWrite = twi- & gt; bytesToWrite ;
00317 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00318
00319 /* If NOT acknowledged (NACK) by slave cancel the transaction. */
00320 if (twi- & gt; interface - & gt;MASTER.STATUS & amp; TWI_MASTER_RXACK_bm) {
00321 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00322 twi- & gt; result = TWIM_RESULT_NACK_RECEIVED ;
00323 twi- & gt; status = TWIM_STATUS_READY ;
00324 }
00325
00326 /* If more bytes to write, send data. */
00327 else if (twi- & gt; bytesWritten & lt; bytesToWrite) {
00328 uint8_t data = twi- & gt; writeData [twi- & gt; bytesWritten ];
00329 twi- & gt; interface - & gt;MASTER.DATA = data;
00330 ++twi- & gt; bytesWritten ;
00331 }
00332
00333 /* If bytes to read, send repeated START condition + Address +
00334 * 'R/_W = 1'
00335 */
00336 else if (twi- & gt; bytesRead & lt; bytesToRead) {
00337 uint8_t readAddress = twi- & gt; address | 0x01;
00338 twi- & gt; interface - & gt;MASTER.ADDR = readAddress;
00339 }
00340
00341 /* If transaction finished, send STOP condition and set RESULT OK. */
00342 else {
00343 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00344 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00345 }
00346 }
00347
00348
00356 void TWI_MasterReadHandler ( TWI_Master_t *twi)
00357 {
00358 /* Fetch data if bytes to be read. */
00359 if (twi- & gt; bytesRead & lt; TWIM_READ_BUFFER_SIZE ) {
00360 uint8_t data = twi- & gt; interface - & gt;MASTER.DATA;
00361 twi- & gt; readData [twi- & gt; bytesRead ] = data;
00362 twi- & gt; bytesRead ++;
00363 }
00364
00365 /* If buffer overflow, issue STOP and BUFFER_OVERFLOW condition. */
00366 else {
00367 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;
00368 TWI_MasterTransactionFinished (twi, TWIM_RESULT_BUFFER_OVERFLOW );
00369 }
00370
00371 /* Local variable used in if test to avoid compiler warning. */
00372 uint8_t bytesToRead = twi- & gt; bytesToRead ;
00373
00374 /* If more bytes to read, issue ACK and start a byte read. */
00375 if (twi- & gt; bytesRead & lt; bytesToRead) {
00376 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
00377 }
00378
00379 /* If transaction finished, issue NACK and STOP condition. */
00380 else {
00381 twi- & gt; interface - & gt;MASTER.CTRLC = TWI_MASTER_ACKACT_bm |
00382 TWI_MASTER_CMD_STOP_gc;
00383 TWI_MasterTransactionFinished (twi, TWIM_RESULT_OK );
00384 }
00385 }
00386
00387
00395 void TWI_MasterTransactionFinished ( TWI_Master_t *twi, uint8_t result)
00396 {
00397 twi- & gt; result = result;
00398 twi- & gt; status = TWIM_STATUS_READY ;
00399 }




@DOC_TITLE@












Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by 1.5.9


XMEGA_TWI.zip > tree.html

TreeView







AVR1308 Using the XMEGA TWI

Main Page
Data Structures

TWI_Master
TWI_Slave

Data Fields
File List

avr_compiler.h
documentation.h
twi_example.c
twi_master_driver.c
twi_master_driver.h
twi_slave_driver.c
twi_slave_driver.h

Globals


XMEGA_TWI.zip > readme.html

Code Documentation Redirecting


XMEGA_TWI.zip > twi_slave_driver.c

/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief
* XMEGA TWI slave driver source file.
*
* This file contains the function implementations the XMEGA TWI slave
* driver.
*
* The driver is not intended for size and/or speed critical code, since
* most functions are just a few lines of code, and the function call
* overhead would decrease code performance. The driver is intended for
* rapid prototyping and documentation purposes for getting started with
* the XMEGA TWI slave module.
*
* For size and/or speed critical code, it is recommended to copy the
* function contents directly into your application instead of making
* a function call.
*
* Several functions use the following construct:
* " some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ... "
* Although the use of the ternary operator ( if ? then : else ) is
* discouraged, in some occasions the operator makes it possible to write
* pretty clean and neat code. In this driver, the construct is used to
* set or not set a configuration bit based on a boolean input parameter,
* such as the " some_parameter " in the example above.
*
* \par Application note:
* AVR1308: Using the XMEGA TWI
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: avr@atmel.com
*
* $Revision: 2660 $
* $Date: 2009-08-11 12:28:58 +0200 (ti, 11 aug 2009) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL " AS IS " AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#include " twi_slave_driver.h "


/*! \brief Initalizes TWI slave driver structure.
*
* Initialize the instance of the TWI Slave and set the appropriate values.
*
* \param twi The TWI_Slave_t struct instance.
* \param module Pointer to the TWI module.
* \param processDataFunction Pointer to the function that handles incoming data.
*/
void TWI_SlaveInitializeDriver(TWI_Slave_t *twi,
TWI_t *module,
void (*processDataFunction) (void))
{
twi- & gt; interface = module;
twi- & gt; Process_Data = processDataFunction;
twi- & gt; bytesReceived = 0;
twi- & gt; bytesSent = 0;
twi- & gt; status = TWIS_STATUS_READY;
twi- & gt; result = TWIS_RESULT_UNKNOWN;
twi- & gt; abort = false;
}


/*! \brief Initialize the TWI module.
*
* Enables interrupts on address recognition and data available.
* Remember to enable interrupts globally from the main application.
*
* \param twi The TWI_Slave_t struct instance.
* \param address Slave address for this module.
* \param intLevel Interrupt level for the TWI slave interrupt handler.
*/
void TWI_SlaveInitializeModule(TWI_Slave_t *twi,
uint8_t address,
TWI_SLAVE_INTLVL_t intLevel)
{
twi- & gt; interface- & gt; SLAVE.CTRLA = intLevel |
TWI_SLAVE_DIEN_bm |
TWI_SLAVE_APIEN_bm |
TWI_SLAVE_ENABLE_bm;
twi- & gt; interface- & gt; SLAVE.ADDR = (address & lt; & lt; 1);
}


/*! \brief Common TWI slave interrupt service routine.
*
* Handles all TWI transactions and responses to address match, data reception,
* data transmission, bus error and data collision.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveInterruptHandler(TWI_Slave_t *twi)
{
uint8_t currentStatus = twi- & gt; interface- & gt; SLAVE.STATUS;

/* If bus error. */
if (currentStatus & TWI_SLAVE_BUSERR_bm) {
twi- & gt; bytesReceived = 0;
twi- & gt; bytesSent = 0;
twi- & gt; result = TWIS_RESULT_BUS_ERROR;
twi- & gt; status = TWIS_STATUS_READY;
}

/* If transmit collision. */
else if (currentStatus & TWI_SLAVE_COLL_bm) {
twi- & gt; bytesReceived = 0;
twi- & gt; bytesSent = 0;
twi- & gt; result = TWIS_RESULT_TRANSMIT_COLLISION;
twi- & gt; status = TWIS_STATUS_READY;
}

/* If address match. */
else if ((currentStatus & TWI_SLAVE_APIF_bm) & &
(currentStatus & TWI_SLAVE_AP_bm)) {

TWI_SlaveAddressMatchHandler(twi);
}

/* If stop (only enabled through slave read transaction). */
else if (currentStatus & TWI_SLAVE_APIF_bm) {
TWI_SlaveStopHandler(twi);
}

/* If data interrupt. */
else if (currentStatus & TWI_SLAVE_DIF_bm) {
TWI_SlaveDataHandler(twi);
}

/* If unexpected state. */
else {
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_FAIL);
}
}

/*! \brief TWI address match interrupt handler.
*
* Prepares TWI module for transaction when an address match occures.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveAddressMatchHandler(TWI_Slave_t *twi)
{
/* If application signalling need to abort (error occured). */
if (twi- & gt; abort) {
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_ABORTED);
twi- & gt; abort = false;
} else {
twi- & gt; status = TWIS_STATUS_BUSY;
twi- & gt; result = TWIS_RESULT_UNKNOWN;

/* Disable stop interrupt. */
uint8_t currentCtrlA = twi- & gt; interface- & gt; SLAVE.CTRLA;
twi- & gt; interface- & gt; SLAVE.CTRLA = currentCtrlA & ~TWI_SLAVE_PIEN_bm;

twi- & gt; bytesReceived = 0;
twi- & gt; bytesSent = 0;

/* Send ACK, wait for data interrupt. */
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
}
}


/*! \brief TWI stop condition interrupt handler.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveStopHandler(TWI_Slave_t *twi)
{
/* Disable stop interrupt. */
uint8_t currentCtrlA = twi- & gt; interface- & gt; SLAVE.CTRLA;
twi- & gt; interface- & gt; SLAVE.CTRLA = currentCtrlA & ~TWI_SLAVE_PIEN_bm;

/* Clear APIF, according to flowchart don't ACK or NACK */
uint8_t currentStatus = twi- & gt; interface- & gt; SLAVE.STATUS;
twi- & gt; interface- & gt; SLAVE.STATUS = currentStatus | TWI_SLAVE_APIF_bm;

TWI_SlaveTransactionFinished(twi, TWIS_RESULT_OK);

}


/*! \brief TWI data interrupt handler.
*
* Calls the appropriate slave read or write handler.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveDataHandler(TWI_Slave_t *twi)
{
if (twi- & gt; interface- & gt; SLAVE.STATUS & TWI_SLAVE_DIR_bm) {
TWI_SlaveWriteHandler(twi);
} else {
TWI_SlaveReadHandler(twi);
}
}


/*! \brief TWI slave read interrupt handler.
*
* Handles TWI slave read transactions and responses.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveReadHandler(TWI_Slave_t *twi)
{
/* Enable stop interrupt. */
uint8_t currentCtrlA = twi- & gt; interface- & gt; SLAVE.CTRLA;
twi- & gt; interface- & gt; SLAVE.CTRLA = currentCtrlA | TWI_SLAVE_PIEN_bm;

/* If free space in buffer. */
if (twi- & gt; bytesReceived & lt; TWIS_RECEIVE_BUFFER_SIZE) {
/* Fetch data */
uint8_t data = twi- & gt; interface- & gt; SLAVE.DATA;
twi- & gt; receivedData[twi- & gt; bytesReceived] = data;

/* Process data. */
twi- & gt; Process_Data();

twi- & gt; bytesReceived++;

/* If application signalling need to abort (error occured),
* complete transaction and wait for next START. Otherwise
* send ACK and wait for data interrupt.
*/
if (twi- & gt; abort) {
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_ABORTED);
twi- & gt; abort = false;
} else {
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
}
}
/* If buffer overflow, send NACK and wait for next START. Set
* result buffer overflow.
*/
else {
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_ACKACT_bm |
TWI_SLAVE_CMD_COMPTRANS_gc;
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_BUFFER_OVERFLOW);
}
}


/*! \brief TWI slave write interrupt handler.
*
* Handles TWI slave write transactions and responses.
*
* \param twi The TWI_Slave_t struct instance.
*/
void TWI_SlaveWriteHandler(TWI_Slave_t *twi)
{
/* If NACK, slave write transaction finished. */
if ((twi- & gt; bytesSent & gt; 0) & & (twi- & gt; interface- & gt; SLAVE.STATUS &
TWI_SLAVE_RXACK_bm)) {

twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_OK);
}
/* If ACK, master expects more data. */
else {
if (twi- & gt; bytesSent & lt; TWIS_SEND_BUFFER_SIZE) {
uint8_t data = twi- & gt; sendData[twi- & gt; bytesSent];
twi- & gt; interface- & gt; SLAVE.DATA = data;
twi- & gt; bytesSent++;

/* Send data, wait for data interrupt. */
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_RESPONSE_gc;
}
/* If buffer overflow. */
else {
twi- & gt; interface- & gt; SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
TWI_SlaveTransactionFinished(twi, TWIS_RESULT_BUFFER_OVERFLOW);
}
}
}


/*! \brief TWI transaction finished function.
*
* Prepares module for new transaction.
*
* \param twi The TWI_Slave_t struct instance.
* \param result The result of the transaction.
*/
void TWI_SlaveTransactionFinished(TWI_Slave_t *twi, uint8_t result)
{
twi- & gt; result = result;
twi- & gt; status = TWIS_STATUS_READY;
}