Xmega Application Note


twi_example.c File Reference

XMEGA TWI slave example source file. More...

#include "avr_compiler.h"
#include "twi_slave_driver.h"

Include dependency graph for twi_example.c:

Go to the source code of this file.

Defines

#define NUM_BYTES   8
#define SLAVE_ADDRESS   0x51

Functions

 ISR (PORTC_INT0_vect)
int main (void)
void TWI_process (TWI_Slave_t *twi)
 Process TWI command.
void TWIC_SlaveProcessData (void)

Variables

uint8_t twic_ctrla_reg
uint8_t twic_ctrlb_reg
uint8_t twic_data_reg
uint8_t twic_status_reg
TWI_Slave_t twiSlave


Detailed Description

XMEGA TWI slave example source file.

Application note:
AVR1320: True 400kHz operation for TWI slave
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
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.

4. This software may only be redistributed and used in connection with an Atmel AVR product.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 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 NUM_BYTES   8

Defining number of bytes in buffer.

Definition at line 67 of file twi_example.c.

#define SLAVE_ADDRESS   0x51

Defining an example slave address.

ATxmega128A1 TWI address

Definition at line 64 of file twi_example.c.

Referenced by main().


Function Documentation

ISR ( PORTC_INT0_vect   ) 

/brief TWI Slave Process Data Interrupt

ISR to process TWI

Definition at line 129 of file twi_example.c.

References TWI_process().

00130 {
00131    PORTC.INTFLAGS |= PORT_INT0IF_bm;
00132    PORTC.INTCTRL = PORT_INT0LVL_OFF_gc;
00133 
00134    TWI_process(&twiSlave);
00135 }

Here is the call graph for this function:

int main ( void   ) 

/brief main function

Example code of TWI application in slave mode with no clock stretching at 400kHz. The data received from the master are stored in receive buffer and then copied in send buffer.

Definition at line 95 of file twi_example.c.

References SLAVE_ADDRESS, TWI_SlaveInitializeDriver(), TWI_SlaveInitializeModule(), and TWIC_SlaveProcessData().

00096 {
00097    /* Initialize PORTF for debug on STK600 */
00098    PORTF.DIRSET = 0xFF;
00099         
00100    // Configure LEDs for STK600
00101    PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time
00102    PORTF.PIN0CTRL = PORT_INVEN_bm; // Invert input to turn the leds on when port output value is 1
00103    PORTF.OUT = 0x00;
00104    
00105    /* Set up interrupt mask on SCL pin */
00106    PORTC.INT0MASK = PIN1_bm;
00107         
00108    /* Initialize TWI slave. */
00109    TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
00110    TWI_SlaveInitializeModule(&twiSlave,
00111                              SLAVE_ADDRESS,
00112                              TWI_SLAVE_INTLVL_HI_gc);   
00113    TWIC.SLAVE.CTRLA |= TWI_SLAVE_SMEN_bm;
00114         
00115    /* interrupt level. */
00116    PMIC.CTRL |= PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm;
00117    sei();
00118         
00119    do{
00120       //User application        
00121    }while (1);  
00122 }

Here is the call graph for this function:

void TWI_process ( TWI_Slave_t twi  ) 

Process TWI command.

This function is called out of ISR to process TWI command.

Parameters:
twi The TWI_Slave_t struct instance.

< Clear ACKACT

Definition at line 116 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(), twic_status_reg, TWIS_RESULT_BUS_ERROR, TWIS_RESULT_FAIL, TWIS_RESULT_TRANSMIT_COLLISION, and TWIS_STATUS_READY.

Referenced by ISR().

00117 {
00118    //static uint8_t sent_NACK = 0;      //!< Holds whether NACK has been transmitted
00119    uint8_t currentStatus;
00120    currentStatus = twic_status_reg;
00121 
00122    twi->interface->SLAVE.CTRLB &= ~TWI_SLAVE_ACKACT_bm; 
00123 
00124    /* If bus error. */
00125    if (currentStatus & TWI_SLAVE_BUSERR_bm) {
00126       twi->bytesReceived = 0;
00127       twi->bytesSent = 0;
00128       twi->result = TWIS_RESULT_BUS_ERROR;
00129       twi->status = TWIS_STATUS_READY;
00130    }
00131    else if (currentStatus & TWI_SLAVE_COLL_bm) {
00132       /* If transmit collision. */
00133       twi->bytesReceived = 0;
00134       twi->bytesSent = 0;
00135       twi->result = TWIS_RESULT_TRANSMIT_COLLISION;
00136       twi->status = TWIS_STATUS_READY;
00137    }
00138    else if ((currentStatus & TWI_SLAVE_APIF_bm) && (currentStatus & TWI_SLAVE_AP_bm)) {
00139       /* If address match. */
00140       TWI_SlaveAddressMatchHandler(twi);        
00141    }
00142    else if (currentStatus & TWI_SLAVE_APIF_bm) {
00143       /* If stop (only enabled through slave read transaction). */        
00144       TWI_SlaveStopHandler(twi);
00145    }
00146    else if (currentStatus & TWI_SLAVE_DIF_bm) {
00147       /* If data interrupt. */     
00148       TWI_SlaveDataHandler(twi);
00149    }
00150    else {
00151       /* If unexpected state. */
00152       TWI_SlaveTransactionFinished(twi, TWIS_RESULT_FAIL);
00153    }
00154 }

Here is the call graph for this function:

void TWIC_SlaveProcessData ( void   ) 

/brief Process data from master

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 77 of file twi_example.c.

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

Referenced by main().

00078 {
00079    PORTF.OUT=twiSlave.receivedData[twiSlave.bytesReceived];
00080    uint8_t recvData = twiSlave.receivedData[twiSlave.bytesReceived];
00081    twiSlave.sendData[twiSlave.bytesReceived] = recvData;
00082 
00083    //init first byte to write
00084    twic_data_reg =  twiSlave.sendData[0];
00085 }


Variable Documentation

uint8_t twic_ctrla_reg

Definition at line 58 of file twi_example.c.

uint8_t twic_ctrlb_reg

Definition at line 59 of file twi_example.c.

uint8_t twic_data_reg

uint8_t twic_status_reg

Definition at line 57 of file twi_example.c.

Referenced by TWI_process(), TWI_SlaveDataHandler(), and TWI_SlaveWriteHandler().

TWI slave module.

Definition at line 70 of file twi_example.c.

@DOC_TITLE@
Generated on Tue Jan 19 18:42:00 2010 for AVR1320: True 400kHz operation for TWI slave by doxygen 1.5.8