/***************************************************************************** * * * File USI_TWI_Master.c compiled with gcc * Date Friday, 10/31/08 Boo! * Updated by jkl * * AppNote : AVR310 - Using the USI module as a TWI Master * * Extensively modified to provide complete I2C driver. * *Notes: * - T4_TWI and T2_TWI delays are modified to work with 1MHz default clock * and now use hard code values. They would need to change * for other clock rates. Refer to the Apps Note. * * 12/17/08 Added USI_TWI_Start_Memory_Read Routine -jkl * Note msg buffer will have slave adrs ( with write bit set) and memory adrs; * length should be these two bytes plus the number of bytes to read. ****************************************************************************/ #include #define F_CPU 8000000UL // was #define F_CPU 1000000UL Sets up the default speed for delay.h #include #include #include "USI_TWI_Master.h" unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char ); unsigned char USI_TWI_Master_Transfer( unsigned char ); unsigned char USI_TWI_Master_Stop( void ); unsigned char USI_TWI_Master_Start( void ); union USI_TWI_state { unsigned char errorState; // Can reuse the TWI_state for error states since it will not be needed if there is an error. struct { unsigned char addressMode : 1; unsigned char masterWriteDataMode : 1; unsigned char memReadMode : 1; unsigned char unused : 5; }; } 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<