|
|
@ -400,7 +400,8 @@ void twi_releaseBus(void) {
|
|
|
|
ISR(TWI_vect) {
|
|
|
|
ISR(TWI_vect) {
|
|
|
|
switch (TW_STATUS) {
|
|
|
|
switch (TW_STATUS) {
|
|
|
|
// All Master
|
|
|
|
// All Master
|
|
|
|
case TW_START: // sent start condition - fall through
|
|
|
|
case TW_START: // sent start condition
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_REP_START: // sent repeated start condition
|
|
|
|
case TW_REP_START: // sent repeated start condition
|
|
|
|
// copy device address and r/w bit to output register and ack
|
|
|
|
// copy device address and r/w bit to output register and ack
|
|
|
|
TWDR = twi_slarw;
|
|
|
|
TWDR = twi_slarw;
|
|
|
@ -408,7 +409,8 @@ ISR(TWI_vect) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Master Transmitter
|
|
|
|
// Master Transmitter
|
|
|
|
case TW_MT_SLA_ACK: // slave receiver acked address - fall through
|
|
|
|
case TW_MT_SLA_ACK: // slave receiver acked address
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_MT_DATA_ACK: // slave receiver acked data
|
|
|
|
case TW_MT_DATA_ACK: // slave receiver acked data
|
|
|
|
// if there is data to send, send it, otherwise stop
|
|
|
|
// if there is data to send, send it, otherwise stop
|
|
|
|
if (twi_masterBufferIndex < twi_masterBufferLength) {
|
|
|
|
if (twi_masterBufferIndex < twi_masterBufferLength) {
|
|
|
@ -442,7 +444,7 @@ ISR(TWI_vect) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Master Receiver
|
|
|
|
// Master Receiver
|
|
|
|
case TW_MR_DATA_ACK: // data received, ack sent fall through
|
|
|
|
case TW_MR_DATA_ACK: // data received, ack sent
|
|
|
|
// put byte into buffer
|
|
|
|
// put byte into buffer
|
|
|
|
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
|
|
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
|
|
/* intentionally fall through */
|
|
|
|
/* intentionally fall through */
|
|
|
@ -475,17 +477,21 @@ ISR(TWI_vect) {
|
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_TWI_SLAVE_MODE
|
|
|
|
#if ENABLE_TWI_SLAVE_MODE
|
|
|
|
// Slave Receiver
|
|
|
|
// Slave Receiver
|
|
|
|
case TW_SR_SLA_ACK: // addressed, returned ack - fall through
|
|
|
|
case TW_SR_SLA_ACK: // addressed, returned ack
|
|
|
|
case TW_SR_GCALL_ACK: // addressed generally, returned ack - fall through
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack - fall through
|
|
|
|
case TW_SR_GCALL_ACK: // addressed generally, returned ack
|
|
|
|
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack - fall through
|
|
|
|
/* Falls through. */
|
|
|
|
|
|
|
|
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
|
|
|
|
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
|
|
|
|
// enter slave receiver mode
|
|
|
|
// enter slave receiver mode
|
|
|
|
twi_state = TWI_SRX;
|
|
|
|
twi_state = TWI_SRX;
|
|
|
|
// indicate that rx buffer can be overwritten and ack
|
|
|
|
// indicate that rx buffer can be overwritten and ack
|
|
|
|
twi_rxBufferIndex = 0;
|
|
|
|
twi_rxBufferIndex = 0;
|
|
|
|
twi_reply(1);
|
|
|
|
twi_reply(1);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case TW_SR_DATA_ACK: // data received, returned ack - fall through
|
|
|
|
case TW_SR_DATA_ACK: // data received, returned ack
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
|
|
|
|
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
|
|
|
|
// if there is still room in the rx buffer
|
|
|
|
// if there is still room in the rx buffer
|
|
|
|
if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) {
|
|
|
|
if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) {
|
|
|
@ -509,14 +515,16 @@ ISR(TWI_vect) {
|
|
|
|
// since we submit rx buffer to "wire" library, we can reset it
|
|
|
|
// since we submit rx buffer to "wire" library, we can reset it
|
|
|
|
twi_rxBufferIndex = 0;
|
|
|
|
twi_rxBufferIndex = 0;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case TW_SR_DATA_NACK: // data received, returned nack - fall through
|
|
|
|
case TW_SR_DATA_NACK: // data received, returned nack
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
|
|
|
|
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
|
|
|
|
// nack back at master
|
|
|
|
// nack back at master
|
|
|
|
twi_reply(0);
|
|
|
|
twi_reply(0);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Slave Transmitter
|
|
|
|
// Slave Transmitter
|
|
|
|
case TW_ST_SLA_ACK: // addressed, returned ack - fall through
|
|
|
|
case TW_ST_SLA_ACK: // addressed, returned ack
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
|
|
|
|
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
|
|
|
|
// enter slave transmitter mode
|
|
|
|
// enter slave transmitter mode
|
|
|
|
twi_state = TWI_STX;
|
|
|
|
twi_state = TWI_STX;
|
|
|
@ -543,7 +551,8 @@ ISR(TWI_vect) {
|
|
|
|
twi_reply(0);
|
|
|
|
twi_reply(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case TW_ST_DATA_NACK: // received nack, we are done - fall through
|
|
|
|
case TW_ST_DATA_NACK: // received nack, we are done
|
|
|
|
|
|
|
|
/* Falls through. */
|
|
|
|
case TW_ST_LAST_DATA: // received ack, but we are done already!
|
|
|
|
case TW_ST_LAST_DATA: // received ack, but we are done already!
|
|
|
|
// ack future responses
|
|
|
|
// ack future responses
|
|
|
|
twi_reply(1);
|
|
|
|
twi_reply(1);
|
|
|
|