??? 08/21/10 10:17 Read: times |
#178172 - breaking infinite waits in slave acknowledge wait Responding to: ???'s previous message |
Your code "while(!data_ack);" does only work if you get the expected byte from the master. If the master sends a byte that isn't an acknowledge, you just stay in your loop without caring what the board received. If the master doesn't send anything at all, your slave stays in the loop indefinitely.
In runtime, a master, after addressing the slave, waits for 'end of data' byte. Let us consider an instance when a slave starts sending data bytes and went to waiting mode, as master did not send acknowledge. Meanwhile, the master, which is waiting for the 'end of data' byte from the slave, comes out of wait after some timeout value (synch_flag in the following code): A piece of master code already posted is as shown: void getDataFromSlaves(void) { unsigned char slave_card_id; for(slave_card_id=0;slave_card_id<13;slave_card_id++) { if(end_of_comm == 0X55) { // If Comm Port Is Free... end_of_comm = 0XAA;//MAKE IT BUSY if(enableSlaveToTx(slave_card_id)) { // If The Addressed Slave Is Enabled For Transmitting Mode... synch_flag = 75; // Load The Synchronisation Flag Count, which counts down in TISR every 20 ms while( (end_of_comm == 0XAA) && synch_flag){;} // Wait Until The Slave Transmits The Total Data Or Synch Count Comes To Zero thro Timer 0 ISR if(end_of_comm == 0XAA) { // If The Slave Card Lost The Communication /*Error Log storing code goes here*/ } end_of_comm = 0x55;// Make Comm Port Free... } else { // If The Addressed Slave Does Not Respond At All... /*We give 5 turns for the slave to respond. If it fails, we store the Error Log and send reset signal to restart the system. else, */ end_of_comm = 0x55;// Make Comm Port Free... } } } } So, the master identifies this as an error and sends universal reset command to all slaves. This command at slave end clears all flags and variables including data_ack. So, there is no problem of a slave waiting in infinite loop. |