/***************************************************************************** * * Atmel Corporation * * File : USI_TWI_Master.c * Compiler : AVRGCC Toolchain version 3.4.2 * Revision : $Revision: 992 $ * Date : $Date: 2013-11-07 $ * Updated by : $Author: Atmel $ * * Support mail : avr@atmel.com * * Supported devices : All device with USI module can be used. * The example is written for the ATmega169, ATtiny26 and ATtiny2313 * * AppNote : AVR310 - Using the USI module as a TWI Master * * Description : This is an implementation of an TWI master using * the USI module as basis. The implementation assumes the AVR to * be the only TWI master in the system and can therefore not be * used in a multi-master system. * Usage : Initialize the USI module by calling the USI_TWI_Master_Initialise() * function. Hence messages/data are transceived on the bus using * the USI_TWI_Transceive() function. The transceive function * returns a status byte, which can be used to evaluate the * success of the transmission. * ****************************************************************************/ #include #include "USI_TWI_Master.h" #include unsigned char USI_TWI_Master_Transfer( unsigned char ); unsigned char USI_TWI_Master_Stop( void ); union USI_TWI_state { unsigned char errorState; // Can reuse the TWI_state for error states due to that it will not be need if there exists an error. struct { unsigned char addressMode : 1; unsigned char masterWriteDataMode : 1; unsigned char unused : 6; }; } USI_TWI_state; /*--------------------------------------------------------------- USI TWI single master initialization function ---------------------------------------------------------------*/ void USI_TWI_Master_Initialise( void ) { PORT_USI |= (1< (unsigned char*)RAMEND) // Test if address is outside SRAM space { USI_TWI_state.errorState = USI_TWI_DATA_OUT_OF_BOUND; return (FALSE); } if(msgSize <= 1) // Test if the transmission buffer is empty { USI_TWI_state.errorState = USI_TWI_NO_DATA; return (FALSE); } #endif #ifdef NOISE_TESTING // Test if any unexpected conditions have arrived prior to this execution. if( USISR & (1<