27.7.1 Master Transmitter Mode
In the Master Transmitter (MT) mode, a number of data bytes are transmitted to a Slave Receiver, see figure below. In order to enter a Master mode, a START condition must be transmitted. The format of the following address packet determines whether MT or Master Receiver (MR) mode is to be entered: If SLA+W is transmitted, MT mode is entered, if SLA+R is transmitted, MR mode is entered. All the status codes mentioned in this section assume that the prescaler bits are zero or are masked to zero.
A START condition is sent by writing a value to the TWI Control Register n (TWCRn) of the type TWCRn=1x10x10x:
- The TWI Enable bit (TWCRn.TWEN) must be written to '1' to enable the 2-wire Serial Interface
- The TWI Start Condition bit (TWCRn.TWSTA) must be written to '1' to transmit a START condition
- The TWI Interrupt Flag (TWCRn.TWINT) must be written to '1' to clear the flag.
When SLA+W have been transmitted and an acknowledgment bit has been received, TWINT is set again and a number of status codes in TWSR are possible. Possible status codes in Master mode are 0x18, 0x20, or 0x38. The appropriate action to be taken for each of these status codes is detailed in the Status Code table below.
When SLA+W has been successfully transmitted, a data packet should be transmitted. This is done by writing the data byte to TWDR. TWDR must only be written when TWINT is high. If not, the access will be discarded, and the Write Collision bit (TWWC) will be set in the TWCRn Register. After updating TWDRn, the TWINT bit should be cleared (by writing '1' to it) to continue the transfer. This is accomplished by writing again a value to TWCRn of the type TWCRn=1x00x10x.
This scheme is repeated until the last byte has been sent and the transfer is ended, either by generating a STOP condition or a by a repeated START condition. A repeated START condition is accomplished by writing a regular START value TWCRn=1x10x10x. A STOP condition is generated by writing a value of the type TWCRn=1x01x10x.
After a repeated START condition (status code 0x10), the 2-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 of the bus.
Status Code (TWSR) Prescaler Bits are 0 | Status of the 2-wire Serial Bus and 2-wire Serial Interface Hardware | Application Software Response | Next Action Taken by TWI Hardware | ||||
---|---|---|---|---|---|---|---|
To/from TWDR | To TWCRn | ||||||
STA | STO | TWINT | TWEA | ||||
0x08 | A START condition has been transmitted | Load SLA+W | 0 | 0 | 1 | X | SLA+W will be transmitted; ACK or NOT ACK will be received |
0x10 | A repeated START condition has been transmitted | Load SLA+W or | 0 | 0 | 1 | X | SLA+W will be
transmitted; ACK or NOT ACK will be received |
Load SLA+R | 0 | 0 | 1 | X | SLA+R will be transmitted; Logic will switch to Master Receiver mode | ||
0x18 | SLA+W has been transmitted; ACK has been received | Load data byte or | 0 | 0 | 1 | X | Data byte will be transmitted and ACK or NOT ACK will be received |
No TWDR action or | 1 | 0 | 1 | X | Repeated START will be transmitted | ||
No TWDR action or | 0 | 1 | 1 | X | STOP condition will be transmitted and TWSTO Flag will be reset | ||
No TWDR action | 1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset | ||
0x20 | SLA+W has been transmitted; NOT ACK has been received | Load data byte or | 0 | 0 | 1 | X | Data byte will be transmitted and ACK or NOT ACK will be received |
No TWDR action or | 1 | 0 | 1 | X | Repeated START will be transmitted | ||
No TWDR action or | 0 | 1 | 1 | X | STOP condition will be transmitted and TWSTO Flag will be reset | ||
No TWDR action | 1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset | ||
0x28 | Data byte has been transmitted; ACK has been received | Load data byte or | 0 | 0 | 1 | X | Data byte will be transmitted and ACK or NOT ACK will be received |
No TWDR action or | 1 | 0 | 1 | X | Repeated START will be transmitted | ||
No TWDR action or | 0 | 1 | 1 | X | STOP condition will be transmitted and TWSTO Flag will be reset | ||
No TWDR action | 1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset | ||
0x30 | Data byte has been transmitted; NOT ACK has been received | Load data byte or | 0 | 0 | 1 | X | Data byte will be transmitted and ACK or NOT ACK will be received |
No TWDR action or | 1 | 0 | 1 | X | Repeated START will be transmitted | ||
No TWDR action or | 0 | 1 | 1 | X | STOP condition will be transmitted and TWSTO Flag will be reset | ||
No TWDR action | 1 | 1 | 1 | X | STOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset | ||
0x38 | Arbitration lost in SLA+W or data bytes | No TWDR action or | 0 | 0 | 1 | X | 2-wire Serial Bus will be released and not addressed Slave mode entered |
No TWDR action | 1 | 0 | 1 | X | A START condition will be transmitted when the bus becomes free |