00001
00002
00003
00004
00005
00006
00007 #include <TWI.h>
00008 #include <avr/interrupt.h>
00009 #include <avr/io.h>
00010
00011 #if defined(ARDUINO) && (ARDUINO >= 100)
00012 #include "Arduino.h"
00013 #else
00014 #include "WConstants.h"
00015 #include <inttypes.h>
00016 #endif
00017
00018 static unsigned char twi_buf[ TWI_BUFFER_SIZE ];
00019 static unsigned char twi_msg_size;
00020 static unsigned char twi_state = TWI_NO_STATE;
00021
00022 union twi_status_reg twi_status_reg = {0};
00023
00028 unsigned char twi_busy_p( void ) {
00029 return ( TWCR & (1<<TWIE) );
00030 }
00031
00041
00042 unsigned char twi_get_state( void ) {
00043 while ( twi_busy_p() );
00044 return ( twi_state );
00045 }
00046
00059
00060 void twi_transmit( unsigned char *msg, unsigned char msg_size )
00061 {
00062 unsigned char temp;
00063
00064 while ( twi_busy_p() );
00065
00066 twi_msg_size = msg_size;
00067 twi_buf[0] = msg[0];
00068 if (!( msg[0] & (TRUE<<TWI_READ_BIT) )) {
00069 for ( temp = 1; temp < msg_size; temp++ )
00070 twi_buf[ temp ] = msg[ temp ];
00071 }
00072 twi_status_reg.all = 0;
00073 twi_state = TWI_NO_STATE ;
00074 TWCR = (1<<TWEN)|
00075 (1<<TWIE)|(1<<TWINT)|
00076 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00077 (0<<TWWC);
00078 }
00079
00087
00088 void twi_resend( void )
00089 {
00090 while ( twi_busy_p() );
00091 twi_status_reg.all = 0;
00092 twi_state = TWI_NO_STATE ;
00093 TWCR = (1<<TWEN)|
00094 (1<<TWIE)|(1<<TWINT)|
00095 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00096 (0<<TWWC);
00097 }
00098
00111
00112 unsigned char twi_get_data( unsigned char *msg, unsigned char msg_size )
00113 {
00114 unsigned char i;
00115
00116 while ( twi_busy_p() );
00117
00118 if( twi_status_reg.last_trans_ok ) {
00119 for ( i=0; i<msg_size; i++ ) {
00120 msg[ i ] = twi_buf[ i ];
00121 }
00122 }
00123 return( twi_status_reg.last_trans_ok );
00124 }
00125
00132
00133 ISR(TWI_vect) {
00134 static unsigned char twi_buf_ptr;
00135 switch (TWSR)
00136 {
00137 case TWI_START:
00138 case TWI_REP_START:
00139 twi_buf_ptr = 0;
00140 case TWI_MTX_ADR_ACK:
00141 case TWI_MTX_DATA_ACK:
00142 if (twi_buf_ptr < twi_msg_size) {
00143 TWDR = twi_buf[twi_buf_ptr++];
00144 TWCR = (1<<TWEN)|
00145 (1<<TWIE)|(1<<TWINT)|
00146 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00147 (0<<TWWC);
00148 } else {
00149 twi_status_reg.last_trans_ok = TRUE;
00150 TWCR = (1<<TWEN)|
00151 (0<<TWIE)|(1<<TWINT)|
00152 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
00153 (0<<TWWC);
00154 }
00155 break;
00156 case TWI_MRX_DATA_ACK:
00157 twi_buf[twi_buf_ptr++] = TWDR;
00158 case TWI_MRX_ADR_ACK:
00159 if (twi_buf_ptr < (twi_msg_size-1) ) {
00160 TWCR = (1<<TWEN)|
00161 (1<<TWIE)|(1<<TWINT)|
00162 (1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00163 (0<<TWWC);
00164 } else {
00165 TWCR = (1<<TWEN)|
00166 (1<<TWIE)|(1<<TWINT)|
00167 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00168 (0<<TWWC);
00169 }
00170 break;
00171 case TWI_MRX_DATA_NACK:
00172 twi_buf[twi_buf_ptr] = TWDR;
00173 twi_status_reg.last_trans_ok = TRUE;
00174 TWCR = (1<<TWEN)|
00175 (0<<TWIE)|(1<<TWINT)|
00176 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
00177 (0<<TWWC);
00178 break;
00179 case TWI_ARB_LOST:
00180 TWCR = (1<<TWEN)|
00181 (1<<TWIE)|(1<<TWINT)|
00182 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
00183 (0<<TWWC);
00184 break;
00185 case TWI_MTX_ADR_NACK:
00186 case TWI_MRX_ADR_NACK:
00187 case TWI_MTX_DATA_NACK:
00188
00189 case TWI_BUS_ERROR:
00190 default:
00191 twi_state = TWSR;
00192
00193 TWCR = (1<<TWEN)|
00194 (0<<TWIE)|(0<<TWINT)|
00195 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
00196 (0<<TWWC);
00197 }
00198 }
00199
00200