ST7LITE2
73/131
SERIAL PERIPHERAL INTERFACE (Cont’d)
11.4.3.3 Master Mode Operation
In master mode, the serial clock is output on the
SCK pin. The c lock f requency, polarity an d phase
are configured by software (refer to the description
of the SPICSR register).
Note: The idle state of SCK must correspond to
the polarity selected in the SPICSR register (by
pulling up SCK if CPOL=1 or pulling down SCK if
CPOL=0).
To operate the SPI in master mode, perform the
following two steps in order (if t he SPICSR register
is not written first, the SPICR register setting may
be not taken into account):
1. Write to the SPICSR register:
– Select the clock frequency by configuring the
SPR[2:0] bits.
– Select the clock pola rity and c lock phase by
configuring the CP OL a nd CP HA b its. Figur e
46 shows the four possible configurations.
Note: The slave must have the same CPOL
and CPHA settings as the master.
– Either set the SSM bit and s et the SSI bit or
clear the SSM bit and tie the SS
pin high for
the complete byte transmit sequence.
2. Write to the SPICR register:
– Set the MSTR and SPE bits
Note: MSTR and SPE bits remain set onl y if
SS
is high).
The transmit sequence begins when software
writes a byte in the SPIDR register.
11.4.3.4 Master Mode Transmit Sequen ce
When software writes to the SPIDR register, the
data byte is loaded into the 8-bit shift register and
then shifted out serially to the MOSI pin most significant bit first.
When data transfer is complete:
– The SPIF bit is set by hardware
– An interrupt request is generated if the SPIE
bit is set and the interrupt mask in the C CR
register is cleared.
Clearing the SPIF bit is performed by the following
software sequence:
1. An access to the SPICSR register while the
SPIF bit is set
2. A read to the SPIDR register.
Note: While the SPIF bit is set, all writes to the
SPIDR register are inhibited until the SPICSR register is rea d.
11.4.3.5 Slave Mode Operation
In slave mode, the s erial clock is received on t he
SCK pin from the master device.
To operate the SPI in slave mode:
1. W rite to the SPICSR regist er to perform t he following actions:
– Select the clock po larity and clock phase by
configuring the CPOL and CPHA bits (see
Figure 46).
Note: The slave must have the same CPOL
and CPHA settings as the master.
– Manage the SS
pin as described in Section
11.4.3.2 and Figure 44. If C PHA=1 SS
must
be held low continuously. If CPHA=0 SS
must
be held low during byte transmission and
pulled up between each b yte to let the slave
write in the shift register.
2. W rite to the SP ICR register to cl ear the MS TR
bit and set the SPE bit to enable the SPI I/O
functions.
11.4.3.6 Slave Mode Transmit Seq uence
When software writes to the SPIDR register, the
data byte is loaded into the 8-bit shift register and
then shifted out serially to the MISO pin most significant bit first.
The transmit sequence begins when the slave device receives th e clock si g n al and the most significant bit of the data on its MOSI pin.
When data transfer is c omplete:
– The SPIF bit is set by hardware
– An interrupt request is generated if SPIE bit is
set and interrupt mask in the CCR register is
cleared.
Clearing the SPIF bit is performed by the following
software sequence:
1. An access to the SPICSR register while the
SPIF bit is set.
2. A write or a read to the SPIDR register.
Notes: While the SPIF bit is se t, all writes to the
SPIDR register are inhibited until the SPICSR register is read.
The SPIF bit can be cleared during a second
transmission; however, it m ust be cleared before
the second SPIF bit in order to prevent an Overrun
condition (see Section 11.4.5.2).
1