3 #define F_CPU 4000000UL
\r
5 #define F_CPU 8000000UL
\r
8 #include "TWI_Master.h"
\r
9 #include <util/delay.h>
\r
11 #if defined(__AVR_AT90Mega169__) | defined(__AVR_ATmega169PA__) | \
\r
12 defined(__AVR_AT90Mega165__) | defined(__AVR_ATmega165__) | \
\r
13 defined(__AVR_ATmega325__) | defined(__AVR_ATmega3250__) | \
\r
14 defined(__AVR_ATmega645__) | defined(__AVR_ATmega6450__) | \
\r
15 defined(__AVR_ATmega329__) | defined(__AVR_ATmega3290__) | \
\r
16 defined(__AVR_ATmega649__) | defined(__AVR_ATmega6490__) |\
\r
17 defined(__AVR_ATtiny25__) | defined(__AVR_ATtiny45__) | defined(__AVR_ATtiny85__) | \
\r
18 defined(__AVR_AT90Tiny26__) | defined(__AVR_ATtiny26__) |\
\r
19 defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__) |\
\r
20 defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny84A__)
\r
22 unsigned char USI_TWI_Master_Transfer( unsigned char );
\r
23 unsigned char USI_TWI_Master_Stop( void );
\r
27 unsigned char errorState; // Can reuse the TWI_state for error states due to that it will not be need if there exists an error.
\r
30 unsigned char addressMode : 1;
\r
31 unsigned char masterWriteDataMode : 1;
\r
32 unsigned char unused : 6;
\r
36 /*---------------------------------------------------------------
\r
37 USI TWI single master initialization function
\r
38 ---------------------------------------------------------------*/
\r
39 void TWI_Master_Initialise( void )
\r
41 PORT_USI |= (1<<PIN_USI_SDA); // Enable pullup on SDA, to set high as released state.
\r
42 PORT_USI |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state.
\r
44 DDR_USI |= (1<<PIN_USI_SCL); // Enable SCL as output.
\r
45 DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
\r
47 USIDR = 0xFF; // Preload dataregister with "released level" data.
\r
48 USICR = (0<<USISIE)|(0<<USIOIE)| // Disable Interrupts.
\r
49 (1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
\r
50 (1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software stobe as counter clock source
\r
52 USISR = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Clear flags,
\r
53 (0x0<<USICNT0); // and reset counter.
\r
56 /*---------------------------------------------------------------
\r
57 Use this function to get hold of the error message from the last transmission
\r
58 ---------------------------------------------------------------*/
\r
59 unsigned char USI_TWI_Get_State_Info( void )
\r
61 return ( USI_TWI_state.errorState ); // Return error state.
\r
64 /*---------------------------------------------------------------
\r
65 USI Transmit and receive function. LSB of first byte in data
\r
66 indicates if a read or write cycles is performed. If set a read
\r
67 operation is performed.
\r
69 Function generates (Repeated) Start Condition, sends address and
\r
70 R/W, Reads/Writes Data, and verifies/sends ACK.
\r
72 Success or error code is returned. Error codes are defined in
\r
74 ---------------------------------------------------------------*/
\r
75 unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned char msgSize)
\r
77 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
78 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
\r
79 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
80 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
\r
82 USI_TWI_state.errorState = 0;
\r
83 USI_TWI_state.addressMode = TRUE;
\r
85 #ifdef PARAM_VERIFICATION
\r
86 if(msg > (unsigned char*)RAMEND) // Test if address is outside SRAM space
\r
88 USI_TWI_state.errorState = USI_TWI_DATA_OUT_OF_BOUND;
\r
91 if(msgSize <= 1) // Test if the transmission buffer is empty
\r
93 USI_TWI_state.errorState = USI_TWI_NO_DATA;
\r
98 #ifdef NOISE_TESTING // Test if any unexpected conditions have arrived prior to this execution.
\r
99 if( USISR & (1<<USISIF) )
\r
101 USI_TWI_state.errorState = USI_TWI_UE_START_CON;
\r
104 if( USISR & (1<<USIPF) )
\r
106 USI_TWI_state.errorState = USI_TWI_UE_STOP_CON;
\r
109 if( USISR & (1<<USIDC) )
\r
111 USI_TWI_state.errorState = USI_TWI_UE_DATA_COL;
\r
116 if ( !(*msg & (1<<TWI_READ_BIT)) ) // The LSB in the address byte determines if is a masterRead or masterWrite operation.
\r
118 USI_TWI_state.masterWriteDataMode = TRUE;
\r
121 /* Release SCL to ensure that (repeated) Start can be performed */
\r
122 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
\r
123 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
\r
124 #ifdef TWI_FAST_MODE
\r
125 _delay_us( T4_TWI/4 ); // Delay for T4TWI if TWI_FAST_MODE
\r
127 _delay_us( T2_TWI/4 ); // Delay for T2TWI if TWI_STANDARD_MODE
\r
130 /* Generate Start Condition */
\r
131 PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
\r
132 _delay_us( T4_TWI/4 );
\r
133 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
\r
134 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
\r
136 #ifdef SIGNAL_VERIFY
\r
137 if( !(USISR & (1<<USISIF)) )
\r
139 USI_TWI_state.errorState = USI_TWI_MISSING_START_CON;
\r
144 /*Write address and Read/Write data */
\r
147 /* If masterWrite cycle (or inital address tranmission)*/
\r
148 if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode)
\r
151 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
\r
152 USIDR = *(msg++); // Setup data.
\r
153 USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus.
\r
155 /* Clock and verify (N)ACK from slave */
\r
156 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
\r
157 if( USI_TWI_Master_Transfer( tempUSISR_1bit ) & (1<<TWI_NACK_BIT) )
\r
159 if ( USI_TWI_state.addressMode )
\r
160 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
\r
162 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
\r
165 USI_TWI_state.addressMode = FALSE; // Only perform address transmission once.
\r
167 /* Else masterRead cycle*/
\r
170 /* Read a data byte */
\r
171 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
\r
172 *(msg++) = USI_TWI_Master_Transfer( tempUSISR_8bit );
\r
174 /* Prepare to generate ACK (or NACK in case of End Of Transmission) */
\r
175 if( msgSize == 1) // If transmission of last byte was performed.
\r
177 USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
\r
181 USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
\r
183 USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK.
\r
185 }while( --msgSize) ; // Until all data sent/received.
\r
187 USI_TWI_Master_Stop(); // Send a STOP condition on the TWI bus.
\r
189 /* Transmission successfully completed*/
\r
193 /*---------------------------------------------------------------
\r
194 Core function for shifting data in and out from the USI.
\r
195 Data to be sent has to be placed into the USIDR prior to calling
\r
196 this function. Data read, will be return'ed from the function.
\r
197 ---------------------------------------------------------------*/
\r
198 unsigned char USI_TWI_Master_Transfer( unsigned char temp )
\r
200 USISR = temp; // Set USISR according to temp.
\r
201 // Prepare clocking.
\r
202 temp = (0<<USISIE)|(0<<USIOIE)| // Interrupts disabled
\r
203 (1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
\r
204 (1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software clock strobe as source.
\r
205 (1<<USITC); // Toggle Clock Port.
\r
208 _delay_us( T2_TWI/4 );
\r
209 USICR = temp; // Generate positve SCL edge.
\r
210 while( !(PIN_USI & (1<<PIN_USI_SCL)) );// Wait for SCL to go high.
\r
211 _delay_us( T4_TWI/4 );
\r
212 USICR = temp; // Generate negative SCL edge.
\r
213 }while( !(USISR & (1<<USIOIF)) ); // Check for transfer complete.
\r
215 _delay_us( T2_TWI/4 );
\r
216 temp = USIDR; // Read out data.
\r
217 USIDR = 0xFF; // Release SDA.
\r
218 DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
\r
220 return temp; // Return the data from the USIDR
\r
223 /*---------------------------------------------------------------
\r
224 Function for generating a TWI Stop Condition. Used to release
\r
226 ---------------------------------------------------------------*/
\r
227 unsigned char USI_TWI_Master_Stop( void )
\r
229 PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low.
\r
230 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
\r
231 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
\r
232 _delay_us( T4_TWI/4 );
\r
233 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
\r
234 _delay_us( T2_TWI/4 );
\r
236 #ifdef SIGNAL_VERIFY
\r
237 if( !(USISR & (1<<USIPF)) )
\r
239 USI_TWI_state.errorState = USI_TWI_MISSING_STOP_CON;
\r
249 unsigned char I2c_WriteByte(unsigned char msg) {
\r
250 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
251 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
\r
252 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
253 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
\r
256 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
\r
257 USIDR = msg; // Setup data.
\r
258 USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus.
\r
259 /* Clock and verify (N)ACK from slave */
\r
260 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
\r
261 if( USI_TWI_Master_Transfer( tempUSISR_1bit ) & (1<<TWI_NACK_BIT) ){
\r
262 if ( USI_TWI_state.addressMode )
\r
263 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
\r
265 USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
\r
270 unsigned char I2c_ReadByte(unsigned char ack_mode) {
\r
271 unsigned char tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
272 (0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
\r
273 unsigned char tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
\r
274 (0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
\r
276 /* Read a data byte */
\r
277 DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
\r
278 unsigned char msg = USI_TWI_Master_Transfer( tempUSISR_8bit );
\r
280 /* Prepare to generate ACK (or NACK in case of End Of Transmission) */
\r
281 if( ack_mode == NO_ACK) { // If transmission of last byte was performed.
\r
282 USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
\r
284 USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
\r
286 USI_TWI_Master_Transfer( tempUSISR_1bit ); // Generate ACK/NACK.
\r
290 void I2c_StartCondition(void) {
\r
291 /* Release SCL to ensure that (repeated) Start can be performed */
\r
292 PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
\r
293 while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
\r
294 #ifdef TWI_FAST_MODE
\r
295 _delay_us( T4_TWI/4 ); // Delay for T4TWI if TWI_FAST_MODE
\r
297 _delay_us( T2_TWI/4 ); // Delay for T2TWI if TWI_STANDARD_MODE
\r
300 /* Generate Start Condition */
\r
301 PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
\r
302 _delay_us( T4_TWI/4 );
\r
303 PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
\r
304 PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
\r
308 void I2c_StopCondition(void) {
\r
309 USI_TWI_Master_Stop();
\r
317 #if defined(__AVR_ATmega328PB__)| defined(__AVR_ATmega328P__)
\r
320 #include <util/twi.h>
\r
322 void TWI_Wait_Busy() {
\r
324 while (!( TWCR & (1<<TWINT) ));
\r
328 void TWI_Master_Initialise( void ) {
\r
329 TWBR = TWI_TWBR; // Set bit rate register (Baud rate). Defined in header file.Driver presumes prescaler to be 00.
\r TWSR=0;
\r TWDR = 0xFF; // Default content = SDA released.
\r TWCR = (1<<TWEN)| // Enable TWI-interface and release TWI pins.
\r (0<<TWIE)|(0<<TWINT)| // Disable Interrupt.
\r (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // No Signal requests.
\r (0<<TWWC);
\r
333 unsigned char I2c_WriteByte(unsigned char msg) {
\r
334 uint8_t twst;
\r TWDR = msg;
\r TWCR = (1<<TWINT) | (1<<TWEN);
\r TWI_Wait_Busy();
\r // check value of TWI Status Register. Mask prescaler bits
\r twst = TW_STATUS & 0xF8;
\r if( twst != TW_MT_DATA_ACK) return 1;
\r return 0;
\r
336 unsigned char I2c_ReadByte(unsigned char ack_mode) {
\r
337 TWCR = (1<<TWINT) | (1<<TWEN) |ack_mode;
\r TWI_Wait_Busy();
\r return TWDR;
\r
340 void I2c_StartCondition(void) {
\r
341 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
\r
343 uint8_t twst = TW_STATUS & 0xF8;
\r if ( (twst != TW_START) && (twst != TW_REP_START)) return ;// return 1;
\r}
\r
345 // send device address
\r\r
347 TWCR = (1<<TWINT) | (1<<TWEN);
\r\r
349 // wail until transmission completed and ACK/NACK has been received
\r\r
350 while(!(TWCR & (1<<TWINT)));
\r\r
352 // check value of TWI Status Register. Mask prescaler bits.
\r\r
353 twst = TW_STATUS & 0xF8;
\r\r
354 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
\r\r
360 void I2c_StopCondition(void) {
\r
362 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
\r\r
363 while(TWCR & (1<<TWSTO));
\r