26.7.2 Master Receiver Mode
In the Master Receiver (MR) mode, a number of data bytes are received from a slave transmitter (see next figure). In order to enter a Master mode, a START condition must be transmitted. The format of the following address packet determines whether Master Transmitter (MT) or MR mode is to be entered. If SLA+W is transmitted the MT mode is entered, if SLA+R is transmitted the MR mode is entered. All the status codes mentioned in this section assume that the prescaler bits are zero or are masked to zero.
- TWCRn.TWEN must be written to '1' to enable the two-wire serial interface
- TWCRn.TWSTA must be written to '1' to transmit a START condition
- TWCRn.TWINT must be cleared by writing a '1' to it
The TWI will then test the two-wire serial bus and generate a START condition as soon as the bus becomes free. After a START condition has been transmitted, the TWINT flag is set by hardware and the status code in TWSRn will be 0x08 (see the Status Code table below). In order to enter MR mode, SLA+R must be transmitted. This is done by writing SLA+R to TWDR. Thereafter, the TWINT flag should be cleared (by writing '1' to it) to continue the transfer. This is accomplished by writing a value to TWCRn of the type TWCRn=1x00x10x.
When SLA+R has been transmitted and an acknowledgment bit has been received, TWINT is set again and a number of status codes in TWSRn are possible. Possible status codes in Master mode are 0x38, 0x40, or 0x48. The appropriate action to be taken for each of these status codes is detailed in the table below. Received data can be read from the TWDR register when the TWINT flag is set high by hardware. This scheme is repeated until the last byte has been received. After the last byte has been received, the MR should inform the ST by sending a NACK after the last received data byte. The transfer is ended by generating a STOP condition or a repeated START condition. A repeated START condition is sent by writing to the TWI Control Register (TWCRn) a value of the type TWCRn=1x10x10x again. A STOP condition is generated by writing TWCRn=1x01x10x:
After a repeated START condition (status code 0x10) the two-wire Serial Interface can access the same Slave again, or a new slave without transmitting a STOP condition. Repeated START enables the master to switch between slaves, Master Transmitter mode and Master Receiver mode without losing control over the bus.
Status Code (TWSRn) Prescaler Bits are 0 | Status of the Two-Wire Serial Bus and Two-Wire Serial Interface Hardware | Application Software Response | Next Action Taken by TWI Hardware | ||||
---|---|---|---|---|---|---|---|
To/From TWD | To TWCRn | ||||||
STA | STO | TWINT | TWEA | ||||
0x08 | A START condition has been transmitted | Load SLA+R | 0 | 0 | 1 | X | SLA+R will be
transmitted ACK or NOT ACK will be received |
0x10 | A repeated START condition has been transmitted | Load SLA+R | 0 | 0 | 1 | X | SLA+R will be
transmitted ACK or NOT ACK will be received |
Load SLA+W | 0 | 0 | 1 | X |
SLA+W will be transmitted Logic will switch to Master Transmitter mode | ||
0x38 | Arbitration lost in SLA+R or NOT ACK bit | No TWDR action | 0 | 0 | 1 | X | two-wire serial bus will be released and not addressed Slave mode will be entered |
1 | 0 | 1 | X |
A START condition will be transmitted when the bus becomes free | |||
0x40 | SLA+R has been transmitted; ACK has been received | No TWDR action | 0 | 0 | 1 | 0 | Data byte will be received and NOT ACK will be returned |
0 | 0 | 1 | 1 | Data byte will be received and ACK will be returned | |||
0x48 | SLA+R has been transmitted; NOT ACK has been received | 1 | 0 | 1 | X | Repeated START will be transmitted | |
0 | 1 | 1 | X |
STOP condition will be transmitted and TWSTO flag will be reset | |||
1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO flag will be reset | |||
0x50 | Data byte has been received; ACK has been returned | Read data byte | 0 | 0 | 1 | 0 | Data byte will be received and NOT ACK will be returned |
0 | 0 | 1 | 1 | Data byte will be received and ACK will be returned | |||
0x58 | Data byte has been received; NOT ACK has been returned | Read data byte | 1 | 0 | 1 | X | Repeated START will be transmitted |
0 | 1 | 1 | X |
STOP condition will be transmitted and TWSTO flag will be reset | |||
1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO flag will be reset |