Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
162
Добавлен:
01.03.2016
Размер:
3.56 Mб
Скачать

RM0016

Serial peripheral interface (SPI)

 

 

20.3.4Configuring the SPI for simplex communications

The SPI is capable of operating in simplex mode in 2 configurations.

1 clock and 1 bidirectional data wire

1 clock and 1 data wire (Receive-only or Transmit-only)

1 clock and 1 bidirectional data wire

This mode is enabled by setting the BDM bit in the SPI_CR2 register. In this mode SCK is used for the clock, and MOSI in master or MISO in slave mode is used for data communication. The transfer direction (Input/output) is selected by the BDOE bit in the SPI_CR2 register. When this bit is set to 1, the data line is output, otherwise it is input.

1 clock and 1 unidirectional data wire (BDM = 0)

In this mode, the application can use the SPI either in transmit-only mode or in receive-only mode:

Transmit-only mode is similar to full-duplex mode (BDM = 0, RXONLY = 0): the data is transmitted to the transmit pin (MOSI in master mode or MISO in slave mode) and the receive pin (MISO in master mode or MOSI in slave mode) can be used as general purpose I/O. In this case, the application just needs to ignore the Rx buffer (if the data register is read, it does not contain the received value).

In receive-only mode, the application can disable the SPI output function by setting the RXONLY bit in the SPI_CR2 register. In this case, it frees the transmit I/O pin (MOSI in master mode or MISO in slave mode) so it can be used for other purposes.

To start the communication in receive-only mode, configure and enable the SPI:

In master mode, the communication starts immediately and stops when the SPE bit is reset and the current reception stops. There is no need to read the BSY flag in this mode. It is always set when an SPI communication is ongoing.

In slave mode, the SPI continues to receive as long as the NSS is pulled down (or the SSI bit is reset in NSS software mode) and the SCK is running.

20.3.5Data transmission and reception procedures

Rx and Tx buffer

In reception, data are received and then stored into an internal Rx buffer while In transmission, data are first stored into an internal Tx buffer before being transmitted.

A read access of the SPI_DR register returns the Rx buffered value whereas a write access of the SPI_DR stores the written data into the Tx buffer.

Start sequence in master mode

In full-duplex (BDM = 0 and RXONLY = 0)

The sequence begins when data is written into the SPI_DR register (Tx buffer).

The data is then parallel loaded from the Tx buffer into the 8-bit shift register during the first bit transmission and then shifted out serially to the MOSI pin.

At the same time, the received data on MISO pin is shifted in serially to the 8-bit shift register and then parallel loaded into the SPI_DR register (Rx Buffer).

Doc ID 14587 Rev 9

259/454

Serial peripheral interface (SPI)

RM0016

 

 

In unidirectional receive-only mode (BDM = 0 and RXONLY = 1)

The sequence begins as soon as the bit SPE = 1

Only the receiver is activated and the received data on MISO pin is shifted in serially to the 8-bit shift register and then parallel loaded into the SPI_DR register (Rx Buffer).

In bidirectional mode, when transmitting (BDM = 1 and BDOE = 1)

The sequence begins when a data is written into the SPI_DR register (Tx buffer).

The data is then parallel loaded from the Tx buffer into the 8-bit shift register during the first bit transmission and then shifted out serially to the MOSI pin.

No data is received.

In bidirectional mode, when receiving (BDM = 1 and BDOE = 0)

The sequence begins as soon as SPE = 1 and BDOE = 0.

The received data on MOSI pin is shifted in serially to the 8-bit shift register and then parallel loaded into the SPI_DR register (Rx Buffer).

The transmitter is not activated and no data is shifted out serially to the MOSI pin.

Start sequence in slave mode

In full-duplex (BDM=0 and RXONLY=0)

The sequence begins when the slave device receives the clock signal and the first bit of the data on its MOSI pin. The remaining 7 bits are loaded into the shift register.

At the same time, the data is parallel loaded from the Tx buffer into the 8-bit shift register during the first bit transmission and then shifted out serially to the MISO pin. The software must have written the data to be sent before the SPI master device initiates the transfer.

In unidirectional receive-only mode (BDM = 0 and RXONLY = 1)

The sequence begins when the slave device receives the clock signal and the first bit of the data on its MOSI pin. The remaining 7 bits are loaded into the shift register.

The transmitter is not activated and no data is shifted out serially to the MISO pin.

In bidirectional mode, when transmitting (BDM = 1 and BDOE = 1)

The sequence begins when the slave device receives the clock signal and the first bit of the Tx buffer is transmitted to the MISO pin.

The data is then parallel loaded from the Tx buffer into the 8-bit shift register during the first bit transmission and then shifted out serially to the MISO pin. The software must have written the data to be sent before the SPI master device starts the transfer.

no data is received.

In bidirectional mode, when receiving (BDM = 1 and BDOE = 0)

The sequence starts when the slave device receives the clock signal and the first bit of the data to its MISO pin.

The data received on MISO pin is shifted in serially to the 8-bit shift register and then parallel loaded into the SPI_DR register (Rx Buffer).

The transmitter is not activated and no data is shifted out serially to the MISO pin.

260/454

Doc ID 14587 Rev 9

RM0016 Serial peripheral interface (SPI)

 

Handling data transmission and reception

 

The TXE flag (Tx buffer empty) is set when the data is transferred from the Tx buffer to the

 

shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data.

 

An interrupt can be generated if TXIE bit in the SPI_ICR register is set.

Note:

The software must ensure that TXE flag is set to 1 before attempting to write into the Tx

 

buffer. Otherwise, it will overwrite the data which was previously written in the Tx buffer.

 

The RXNE flag (Rx buffer not empty) is set on the last sampling clock edge, when the data

 

is transferred from the shift register to the Rx buffer. It indicates that a data is ready to be

 

read from the SPI_DR register. An interrupt can be generated if RXIE bit in the SPI_ICR

 

register is set. Clearing the RXNE bit is performed by reading the SPI_DR register.

 

In some configurations, the BSY flag can be used during the last data transfer to wait until

 

the completion of the transfer.

Full Duplex Transmit and receive procedure in master or slave mode (BDM=0 and RXONLY = 0)

1. Enable the SPI by setting the SPE bit

2. Write the first data to be transmitted in the SPI_DR register (this clears the TXE flag).

3. Wait until TXE = 1 and write the second data to be transmitted. Then wait until RXNE = 1 and read the SPI_DR to get the first received data (this clears the RXNE bit). Repeat this operation for each data to be transmitted/received until the n-1 received data.

4. Wait until RXNE = 1 and read the last received data.

5. Wait until TXE = 1 and then wait until BSY = 0 before disabling the SPI.

This procedure can also be implemented using dedicated interrupt subroutines launched at each rising edge of RXNE or TXE flags.

Doc ID 14587 Rev 9

261/454

Serial peripheral interface (SPI)

RM0016

 

 

Figure 94. TXE/RXNE/BSY behavior in full duplex mode (RXONLY = 0).

Case of continuous transfers

Example in Master Mode with CPOL=1, CPHA=1

SCK

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DATA1 = 0xF1

 

 

 

 

DATA2 = 0xF2

 

 

 

 

DATA3 = 0xF3

 

 

MISO/MOSI (out)

b0

b1

b2 b3 b4 b5 b6 b7

b0

b1 b2

b3

b4

b5

b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

 

 

 

set by hw

 

 

 

 

 

set by hw

 

 

 

 

 

 

 

 

 

 

 

TXE flag

 

 

cleared by sw

 

 

 

 

cleared by sw

 

 

 

 

set by hw

 

 

 

Tx Buffer

0xF1

 

 

0xF2

 

 

 

 

 

 

 

 

0xF3

 

 

 

 

 

 

 

 

 

(write SPI_DR)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BSY flag

set by hw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

reset by hw

 

 

 

DATA1 = 0xA1

 

 

 

 

DATA 2 = 0xA2

 

 

 

 

DATA 3 = 0xA3

 

 

MISO/MOSI

b0

b1

b2

b3

b4

b5

b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

RXNE flag

 

 

 

 

 

set by hw

cleared by sw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rx Buffer

 

 

 

 

 

 

 

 

 

0xA1

 

 

 

 

 

 

0xA2

 

 

 

 

 

0xA3

(read SPI_DR)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

software

 

software waits until

 

software waits until

 

software waits until

 

software waits un-

 

software waits until

writes 0xF1 in

 

TXE=1 and writes

 

RXNE=1 and reads

 

TXE=1 and writes

 

til RXNE=1 and

 

RXNE=1 and reads

SPI_DR

 

0xF2 in SPI_DR

 

0xA1 from SPI_DR

 

0xF3 in SPI_DR

 

reads 0xA2 from

 

0xA3 from SPI_DR

 

 

 

 

 

 

 

 

SPI_ DR

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 95. TXE/RXNE/BSY behavior in slave / full duplex mode (BDM = 0, RXONLY = 0). Case of continuous transfers

Example in Slave Mode with CPOL=1, CPHA=1

SCK

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DATA 1 = 0xF1

 

 

 

 

DATA 2 = 0xF2

 

 

 

 

DATA 3 = 0xF3

 

 

MISO/MOSI (out)

b0

b1

b2 b3 b4 b5 b6 b7

b0

b1 b2 b3 b4 b5 b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

 

 

 

set by hw

 

 

 

 

set by hw

 

 

 

 

 

 

 

 

 

 

 

 

TXE flag

 

 

cleared by sw

 

 

 

cleared by sw

 

 

 

 

set by hw

 

 

 

 

Tx Buffer

0xF1

 

 

0xF2

 

 

 

 

 

 

 

 

0xF3

 

 

 

 

 

 

 

 

 

(write SPI_DR)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BSY flag

 

by hw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

reset by hw

 

 

 

DATA 1 = 0xA1

 

 

 

 

DATA 2 = 0xA2

 

 

 

 

DATA 3 = 0xA3

 

 

MISO/MOSI

b0

b1

b2

b3

b4

b5

b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

b0

b1

b2

b3

b4

b5

b6

b7

RXNE flag

 

 

 

 

 

set by hw

 

cleared by sw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rx Buffer

 

 

 

 

 

 

 

 

0xA1

 

 

 

 

 

 

0xA2

 

 

 

 

 

0xA3

(read SPI_DR)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

software

 

software waits until

 

software waits until

 

software waits until

 

software waits un-

writes 0xF1 in

 

TXE=1 and writes

 

RXNE=1 and reads

 

TXE=1 and writes

 

til RXNE=1 and

SPI_DR

 

0xF2 in SPI_DR

 

0xA1 from SPI_DR

 

0xF3 in SPI_DR

 

reads 0xA2 from

 

 

 

 

 

 

 

 

SPI_ DR

 

 

 

 

 

 

 

 

 

software waits until RXNE=1 and reads 0xA3 from SPI_DR

262/454

Doc ID 14587 Rev 9

RM0016

Serial peripheral interface (SPI)

 

 

Transmit-only procedure (BDM = 0 RXONLY = 0)

In this mode, the procedure can be reduced as described below and the BSY bit can be used to wait until the effective completion of the transmission (see Figure 94 and Figure 95):

1.Enable the SPI by setting the SPE bit

2.Write the first data to send in the SPI_DR register (this clears the TXE bit).

3.Wait until TXE = 1 and write the next data to be transmitted. Repeat this step for each data to be transmitted.

4.After writing the last data in the SPI_DR register, wait until TXE = 1 and then wait until BSY=0 which indicates that the transmission of the last data is complete.

This procedure can be also implemented using dedicated interrupt subroutines launched at each rising edge of TXE flag.

Note: 1 In master mode, during discontinuous communications, there is a 2 CPU clock period delay between the write operation to SPI_DR and the BSY bit setting. As a consequence, in transmit-only mode, it is mandatory to wait first until TXE is set and then until BSY is reset after having written the last data.

2After transmitting two data in transmit-only mode, the OVR flag is set in the SPI_SR register since the received data are never read.

Figure 96. TXE/BSY in master transmit-only mode

(BDM = 0 and RXONLY = 0). Case of continuous transfers

Example in master mode with CPOL=1, CPHA=1

 

 

 

 

 

 

 

 

SCK

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DATA 1 = 0xF1

 

 

DATA 2 = 0xF2

 

 

 

DATA 3 = 0xF3

 

 

MISO/MOSI (out)

b0 b1

b2 b3 b4 b5

b6

b7

b0 b1 b2 b3 b4 b5 b6

b7

b0

b1

b2 b3 b4 b5

b6

b7

 

 

 

 

set by hw

 

 

set by hw

 

 

 

 

 

 

TXE flag

 

 

 

cleared by sw

 

cleared by sw

 

 

 

set by hw

 

 

Tx Buffer

 

0xF1

 

0xF2

 

 

0xF3

 

 

 

 

 

 

(write SPI_DR)

 

 

 

 

 

 

 

 

 

 

 

 

 

BSY flag

 

set by hw

 

 

 

 

 

 

 

 

reset by hw

software writes

software waits

 

software waits

 

 

 

 

 

 

 

 

 

0xF1 in SPI_DR

until TXE=1 and

until TXE=1 and

 

 

software waits until TXE=1

 

 

 

software waits until BSY=0

 

writes 0xF2 in

 

writes 0xF3 in

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SPI_DR

 

SPI_DR

 

 

 

 

 

 

 

 

 

Doc ID 14587 Rev 9

263/454

Соседние файлы в папке Минимум документации STM8_1