I2C Programming Examples

For a master receiver to terminate a data transfer, it must inform the slave transmitter by not acknowledging the last data byte. This is done by setting I2CR[TXAK] before reading the next-to-last byte. Before the last byte is read, a STOP signal must be generated, as in the following example.

MASR

MOVE.B RXCNT,D0

;Decrease RXCNT

 

SUBQ.L #1,D0

 

 

MOVE.B D0,RXCNT

 

 

BEQ.S ENMASR

;Last byte to be read

 

MOVE.B RXCNT,D1

;Check second-to-last byte to be read

 

EXTB.L D1

 

 

SUBI.L #1,D1;

 

 

BNE.S NXMAR

;Not last one or second last

 

LAMAR BSET.B #3,I2CR

;Disable ACK

 

BRA NXMAR

 

ENMASR

BCLR.B #5,I2CR

;Last one, generate STOP signal

NXMAR

MOVE.B I2DR,RXBUF

;Read data and store RTE

24.6.5 Generation of Repeated START

After the data transfer, if the master still wants the bus, it can signal another START followed by another slave address without signalling a STOP, as in the following example.

RESTART MOVE.B

I2CR,-(A7)

;Repeat START (RESTART)

BSET.B

#2, (A7)

 

MOVE.B

(A7)+, I2CR

 

MOVE.B

CALLING,-(A7)

;Transmit the calling address, D0=R/W-

MOVE.B

CALLING,-(A7)

 

MOVE.B

(A7)+, I2DR

 

24.6.6 Slave Mode

In the slave interrupt service routine, software should poll the I2SR[IAAS] bit to determine if the controller has received its slave address. If IAAS is set, software should set the transmit/receive mode select bit (I2CR[MTX]) according to the I2SR[SRW]. Writing to the I2CR clears the IAAS automatically. The only time IAAS is read as set is from the interrupt at the end of the address cycle where an address match occurred; interrupts resulting from subsequent data transfers will have IAAS cleared. A data transfer can now be initiated by writing information to I2DR for slave transmits, or read from I2DR in slave-receive mode. A dummy read of I2DR in slave/receive mode releases SCL, allowing the master to send data.

In the slave transmitter routine, I2SR[RXAK] must be tested before sending the next byte of data. Setting RXAK means an end-of-data signal from the master receiver, after which software must switch it from transmitter to receiver mode. Reading I2DR then releases SCL so that the master can generate a STOP signal.

MOTOROLA

Chapter 24. I2C Interface

24-13

Page 523
Image 523
Motorola MCF5281, MCF5282 user manual Generation of Repeated Start, Slave Mode