TWI/TWI.c
00001
00002
00003
00004
00005
00006
00007 #include <TWI.h>
00008 #include <inttypes.h>
00009 #include <avr/interrupt.h>
00010 #include <avr/io.h>
00011 #include "WConstants.h"
00012
00013 static unsigned char twi_buf[ TWI_BUFFER_SIZE ];
00014 static unsigned char twi_msg_size;
00015 static unsigned char twi_state = TWI_NO_STATE;
00016
00017 union twi_status_reg twi_status_reg = {0};
00018
00023 unsigned char twi_busy_p( void ) {
00024 return ( TWCR & (1<<TWIE) );
00025 }
00026
00036
00037 unsigned char twi_get_state( void ) {
00038 while ( twi_busy_p() );
00039 return ( twi_state );
00040 }
00041
00054
00055 void twi_transmit( unsigned char *msg, unsigned char msg_size )
00056 {
00057 unsigned char temp;
00058
00059 while ( twi_busy_p() );
00060
00061 twi_msg_size = msg_size;
00062 twi_buf[0] = msg[0];
00063 if (!( msg[0] & (TRUE<<TWI_READ_BIT) )) {
00064 for ( temp = 1; temp < msg_size; temp++ )
00065 twi_buf[ temp ] = msg[ temp ];
00066 }
00067 twi_status_reg.all = 0;
00068 twi_state = TWI_NO_STATE ;
00069 TWCR = (1<<TWEN)|
00070 (1<<TWIE)|(1<<TWINT)|
00071 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00072 (0<<TWWC);
00073 }
00074
00082
00083 void twi_resend( void )
00084 {
00085 while ( twi_busy_p() );
00086 twi_status_reg.all = 0;
00087 twi_state = TWI_NO_STATE ;
00088 TWCR = (1<<TWEN)|
00089 (1<<TWIE)|(1<<TWINT)|
00090 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00091 (0<<TWWC);
00092 }
00093
00106
00107 unsigned char twi_get_data( unsigned char *msg, unsigned char msg_size )
00108 {
00109 unsigned char i;
00110
00111 while ( twi_busy_p() );
00112
00113 if( twi_status_reg.last_trans_ok ) {
00114 for ( i=0; i<msg_size; i++ ) {
00115 msg[ i ] = twi_buf[ i ];
00116 }
00117 }
00118 return( twi_status_reg.last_trans_ok );
00119 }
00120
00127
00128 ISR(TWI_vect) {
00129 static unsigned char twi_buf_ptr;
00130 switch (TWSR)
00131 {
00132 case TWI_START:
00133 case TWI_REP_START:
00134 twi_buf_ptr = 0;
00135 case TWI_MTX_ADR_ACK:
00136 case TWI_MTX_DATA_ACK:
00137 if (twi_buf_ptr < twi_msg_size) {
00138 TWDR = twi_buf[twi_buf_ptr++];
00139 TWCR = (1<<TWEN)|
00140 (1<<TWIE)|(1<<TWINT)|
00141 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00142 (0<<TWWC);
00143 } else {
00144 twi_status_reg.last_trans_ok = TRUE;
00145 TWCR = (1<<TWEN)|
00146 (0<<TWIE)|(1<<TWINT)|
00147 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
00148 (0<<TWWC);
00149 }
00150 break;
00151 case TWI_MRX_DATA_ACK:
00152 twi_buf[twi_buf_ptr++] = TWDR;
00153 case TWI_MRX_ADR_ACK:
00154 if (twi_buf_ptr < (twi_msg_size-1) ) {
00155 TWCR = (1<<TWEN)|
00156 (1<<TWIE)|(1<<TWINT)|
00157 (1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00158 (0<<TWWC);
00159 } else {
00160 TWCR = (1<<TWEN)|
00161 (1<<TWIE)|(1<<TWINT)|
00162 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00163 (0<<TWWC);
00164 }
00165 break;
00166 case TWI_MRX_DATA_NACK:
00167 twi_buf[twi_buf_ptr] = TWDR;
00168 twi_status_reg.last_trans_ok = TRUE;
00169 TWCR = (1<<TWEN)|
00170 (0<<TWIE)|(1<<TWINT)|
00171 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
00172 (0<<TWWC);
00173 break;
00174 case TWI_ARB_LOST:
00175 TWCR = (1<<TWEN)|
00176 (1<<TWIE)|(1<<TWINT)|
00177 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00178 (0<<TWWC);
00179 break;
00180 case TWI_MTX_ADR_NACK:
00181 case TWI_MRX_ADR_NACK:
00182 case TWI_MTX_DATA_NACK:
00183
00184 case TWI_BUS_ERROR:
00185 default:
00186 twi_state = TWSR;
00187
00188 TWCR = (1<<TWEN)|
00189 (0<<TWIE)|(0<<TWINT)|
00190 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00191 (0<<TWWC);
00192 }
00193 }
00194
00195
Generated on Sun Dec 13 16:47:18 2009 by
1.6.1