GD32VF103 User Manual
359
programmed by software and finished sending a START condition on I2C bus, it changes into
master mode. The I2C changes back to slave mode after it
’
s programmed by software and
finished sending a STOP condition on I2C bus.
Programming model in slave transmitting mode
Figure 17-9. Programming model for slave transmitting(10-bit address
the following software procedure should be followed if users wish to make transaction
in slave transmitter mode:
1.
First of all, software should enable I2C peripheral clock as well as configure clock related
registers in I2C_CTL1 to make sure correct I2C timing. After enabled and configured,
I2C operates in its default slave state and waits for START condition followed by address
on I2C bus.
2.
After receiving a START condition followed by a matched address, either in 7-bit format
or in 10-bit format, the I2C hardware sets the ADDSEND bit in I2C_STAT0 register, which
should be monitored by software either by polling or interrupt. After that software should
read I2C_STAT0 and then I2C_STAT1 to clear ADDSEND bit. If 10-bit addressing format
is selected, the I2C master should then send a repeated START(Sr) condition followed
by a header to the I2C bus. The slave sets ADDSEND bit again after it detects the
repeated START(Sr) condition and the following header. Software needs to clear the
ADDSEND bit again by reading I2C_STAT0 and then I2C_STAT1.
3.
Now I2C enters data transmission stage and hardware sets TBE bit because both the
shift register and data register I2C_DATA are empty. Once TBE is set, Software should
write the first byte of data to I2C_DATA register, TBE is not cleared in this case because
the write byte in I2C_DATA is moved to the internal shift register immediately. I2C begins
to transmit data to I2C bus as soon as the shift register is not empty.
4.
During the first byte
’
s transmission, software can write the second byte to I2C_DATA,
and this time TBE is cleared because neither I2C_DATA nor shift register is empty.
5.
Any time TBE is set, software can write a byte to I2C_DATA as long as there are still data
to be transmitted.
6.
During the second last byte
’
s transmission, software write the last data to I2C_DATA to
clear the TBE flag and doesn
’
t care TBE anymore. So TBE will be set after the byte
’
s
transmission and not cleared until a STOP condition.
7.
I2C master doesn
’
t acknowledge to the last byte according to the I2C protocol, so after
sending the last byte, I2C slave will wait for the STOP condition on I2C bus and sets
AERR (Acknowledge Error) bit to notify software that transmission completes. Software
clears AERR bit by writing 0 to it.