1 /*****************************************************************************
5 * File : USI_TWI_Master.c
6 * Compiler : AVRGCC Toolchain version 3.4.2
7 * Revision : $Revision: 992 $
8 * Date : $Date: 2013-11-07 $
9 * Updated by : $Author: Atmel $
11 * Support mail : avr@atmel.com
13 * Supported devices : All device with USI module can be used.
14 * The example is written for the ATmega169, ATtiny26 and ATtiny2313
16 * AppNote : AVR310 - Using the USI module as a TWI Master
18 * Description : This is an implementation of an TWI master using
19 * the USI module as basis. The implementation assumes the AVR to
20 * be the only TWI master in the system and can therefore not be
21 * used in a multi-master system.
22 * Usage : Initialize the USI module by calling the USI_TWI_Master_Initialise()
23 * function. Hence messages/data are transceived on the bus using
24 * the USI_TWI_Transceive() function. The transceive function
25 * returns a status byte, which can be used to evaluate the
26 * success of the transmission.
28 ****************************************************************************/
29 #define F_CPU 8000000UL
31 #include "USI_TWI_Master.h"
32 #include <util/delay.h>
34 unsigned char USI_TWI_Master_Transfer( unsigned char );
35 unsigned char USI_TWI_Master_Stop( void );
39 unsigned char errorState; // Can reuse the TWI_state for error states due to that it will not be need if there exists an error.
42 unsigned char addressMode : 1;
43 unsigned char masterWriteDataMode : 1;
44 unsigned char unused : 6;
48 /*---------------------------------------------------------------
49 USI TWI single master initialization function
50 ---------------------------------------------------------------*/
51 void USI_TWI_Master_Initialise( void )
53 PORT_USI |= (1<<PIN_USI_SDA); // Enable pullup on SDA, to set high as released state.
54 PORT_USI |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state.
56 DDR_USI |= (1<<PIN_USI_SCL); // Enable SCL as output.
57 DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
59 USIDR = 0xFF; // Preload dataregister with "released level" data.
60 USICR = (0<<USISIE)|(0<<USIOIE)| // Disable Interrupts.
61 (1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
62 (1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software stobe as counter clock source
64 USISR = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Clear flags,
65 (0x0<<USICNT0); // and reset counter.
68 /*---------------------------------------------------------------
69 Use this function to get hold of the error message from the last transmission
70 ---------------------------------------------------------------*/
71 unsigned char USI_TWI_Get_State_Info( void )
73 return ( USI_TWI_state.errorState ); // Return error state.
76 /*---------------------------------------------------------------
77 USI Transmit and receive function. LSB of first byte in data
78 indicates if a read or write cycles is performed. If set a read
79 operation is performed.
81 Function generates (Repeated) Start Condition, sends address and
82 R/W, Reads/Writes Data, and verifies/sends ACK.
84 Success or error code is returned. Error codes are defined in
86 ---------------------------------------------------------------*/
87 unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned char msgSize)
89 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
90 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
91 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
92 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
94 USI_TWI_state.errorState = 0;
95 USI_TWI_state.addressMode = TRUE;
97 #ifdef PARAM_VERIFICATION
98 if(msg > (unsigned char*)RAMEND) // Test if address is outside SRAM space
100 USI_TWI_state.errorState = USI_TWI_DATA_OUT_OF_BOUND;
103 if(msgSize <= 1) // Test if the transmission buffer is empty
105 USI_TWI_state.errorState = USI_TWI_NO_DATA;
110 #ifdef NOISE_TESTING // Test if any unexpected conditions have arrived prior to this execution.
111 if( USISR & (1<<USISIF) )
113 USI_TWI_state.errorState = USI_TWI_UE_START_CON;
116 if( USISR & (1<<USIPF) )
118 USI_TWI_state.errorState = USI_TWI_UE_STOP_CON;
121 if( USISR & (1<<USIDC) )
123 USI_TWI_state.errorState = USI_TWI_UE_DATA_COL;
128 if ( !(*msg & (1<<TWI_READ_BIT)) ) // The LSB in the address byte determines if is a masterRead or masterWrite operation.
130 USI_TWI_state.masterWriteDataMode = TRUE;
133 /* Release SCL to ensure that (repeated) Start can be performed */
134 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
135 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
137 _delay_us( T4_TWI/4 ); // Delay for T4TWI if TWI_FAST_MODE
139 _delay_us( T2_TWI/4 ); // Delay for T2TWI if TWI_STANDARD_MODE
142 /* Generate Start Condition */
143 PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
144 _delay_us( T4_TWI/4 );
145 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
146 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
149 if( !(USISR & (1<<USISIF)) )
151 USI_TWI_state.errorState = USI_TWI_MISSING_START_CON;
156 /*Write address and Read/Write data */
159 /* If masterWrite cycle (or inital address tranmission)*/
160 if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode)
163 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
164 USIDR = *(msg++); // Setup data.
165 USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus.
167 /* Clock and verify (N)ACK from slave */
168 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
169 if( USI_TWI_Master_Transfer( tempUSISR_1bit ) & (1<<TWI_NACK_BIT) )
171 if ( USI_TWI_state.addressMode )
172 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
174 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
177 USI_TWI_state.addressMode = FALSE; // Only perform address transmission once.
179 /* Else masterRead cycle*/
182 /* Read a data byte */
183 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
184 *(msg++) = USI_TWI_Master_Transfer( tempUSISR_8bit );
186 /* Prepare to generate ACK (or NACK in case of End Of Transmission) */
187 if( msgSize == 1) // If transmission of last byte was performed.
189 USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
193 USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
195 USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK.
197 }while( --msgSize) ; // Until all data sent/received.
199 USI_TWI_Master_Stop(); // Send a STOP condition on the TWI bus.
201 /* Transmission successfully completed*/
205 /*---------------------------------------------------------------
206 Core function for shifting data in and out from the USI.
207 Data to be sent has to be placed into the USIDR prior to calling
208 this function. Data read, will be return'ed from the function.
209 ---------------------------------------------------------------*/
210 unsigned char USI_TWI_Master_Transfer( unsigned char temp )
212 USISR = temp; // Set USISR according to temp.
214 temp = (0<<USISIE)|(0<<USIOIE)| // Interrupts disabled
215 (1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
216 (1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software clock strobe as source.
217 (1<<USITC); // Toggle Clock Port.
220 _delay_us( T2_TWI/4 );
221 USICR = temp; // Generate positve SCL edge.
222 while( !(PIN_USI & (1<<PIN_USI_SCL)) );// Wait for SCL to go high.
223 _delay_us( T4_TWI/4 );
224 USICR = temp; // Generate negative SCL edge.
225 }while( !(USISR & (1<<USIOIF)) ); // Check for transfer complete.
227 _delay_us( T2_TWI/4 );
228 temp = USIDR; // Read out data.
229 USIDR = 0xFF; // Release SDA.
230 DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
232 return temp; // Return the data from the USIDR
235 /*---------------------------------------------------------------
236 Function for generating a TWI Stop Condition. Used to release
238 ---------------------------------------------------------------*/
239 unsigned char USI_TWI_Master_Stop( void )
241 PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low.
242 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
243 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
244 _delay_us( T4_TWI/4 );
245 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
246 _delay_us( T2_TWI/4 );
249 if( !(USISR & (1<<USIPF)) )
251 USI_TWI_state.errorState = USI_TWI_MISSING_STOP_CON;
261 unsigned char I2c_WriteByte(unsigned char msg) {
262 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
263 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
264 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
265 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
268 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
269 USIDR = msg; // Setup data.
270 USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus.
271 /* Clock and verify (N)ACK from slave */
272 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
273 if( USI_TWI_Master_Transfer( tempUSISR_1bit ) & (1<<TWI_NACK_BIT) ){
274 if ( USI_TWI_state.addressMode )
275 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
277 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
282 unsigned char I2c_ReadByte(unsigned char ack_mode) {
283 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
284 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
285 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
286 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
288 /* Read a data byte */
289 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
290 unsigned char msg = USI_TWI_Master_Transfer( tempUSISR_8bit );
292 /* Prepare to generate ACK (or NACK in case of End Of Transmission) */
293 if( ack_mode == NO_ACK) { // If transmission of last byte was performed.
294 USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
296 USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
298 USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK.
302 void I2c_StartCondition() {
303 /* Release SCL to ensure that (repeated) Start can be performed */
304 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
305 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
307 _delay_us( T4_TWI/4 ); // Delay for T4TWI if TWI_FAST_MODE
309 _delay_us( T2_TWI/4 ); // Delay for T2TWI if TWI_STANDARD_MODE
312 /* Generate Start Condition */
313 PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
314 _delay_us( T4_TWI/4 );
315 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
316 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
320 void I2c_StopCondition() {
321 USI_TWI_Master_Stop();