Hello all ,
There is a twi library in arduino libraries found below
https://github.com/arduino/Arduino/blob/master/libraries/Wire/utility/twi.c
I need to convert this library in to a polling based library. I felt it is quiet easy and thought of following steps.
[1] Remove all _BV(TWIE) - To ensure interrupt is not enabled
[2] Convert the TWI isr - TWI_vect in to a normal function twi_isr( )
[3] Wherever they wait for ISR to finish it work - call normal function twi_isr( ). I found the following places
// wait until twi is ready, become master receiver
while(TWI_READY != twi_state){
continue;
}
// wait for read operation to complete
while(TWI_MRX == twi_state){
continue;
}
// wait for write operation to complete
while(wait && (TWI_MTX == twi_state)){
continue;
}
I converted the above lines as follows
// wait until twi is ready, become master receiver
while(TWI_READY != twi_state){
twi_isr();
continue;
}
// wait for read operation to complete
while(TWI_MRX == twi_state){
while( ! ( TWCR & ( 1 << TWINT ) ) ); // Wait for twi to finish it work
twi_isr(); // to check status register and take corresponding action
continue;
}
// wait for write operation to complete
while(wait && (TWI_MTX == twi_state)){
while( ! ( TWCR & ( 1 << TWINT ) ) ); // Wait for twi to finish it work
twi_isr();
continue;
}
twi_isr is just a normal function version of the original ISR from avr. I did not modify it. After this modification i try to run the examples available in https://github.com/arduino/Arduino/tree/master/libraries/Wire/examples. But this does not work. Please let me know your suggestions. Thanks a lot.
Regards,
Bivin.