STM32F1 SPI

Started by tha, December 16, 2020, 01:16:43 PM

Previous topic - Next topic

tha

25 Serial peripheral interface (SPI)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.

25.1 SPI introduction
ใน high-density, XL-density และ connectivity line devices, the SPI interface จัดให้มี two main functions, รองรับอย่างใดอย่างหนึ่ง the SPI protocol หรือ the I2S audio protocol. โดยค่าเริ่มต้น, คือ SPI function ที่ถูกเลือก. มันเป็นไปได้ที่จะสวิทช์ the interface จาก SPI ไปยัง I2S โดย software.
ใน Cat.1 และ Cat.2 devices, the I2S protocol ไม่มีให้ใช้งาน.

The serial peripheral interface (SPI) ยอมให้ half/ full-duplex, synchronous, serial communication กับ external devices. The interface สามารถถูกกำหนดค่าเป็น the master และในกรณีนี้มันจัดให้มี the communication clock (SCK) ไปยัง the external slave device. The interface ยังสามารถทำงานใน multimaster configuration.

มันอาจใช้เพื่อวัตถุประสงค์หลายประการ, รวมถึง simplex synchronous transfers บน two lines ด้วย a possible bidirectional data line หรือ reliable communication โดยใช้ CRC checking.

The I2S เป็น a synchronous serial communication interface อีกด้วย. มันสามารถจัดการกับ four different audio standards ประกอบด้วย the I2S Philips standard, the MSB- and LSB-justified standards, และ the PCM standard. มันสามารถทำงานเป็น a slave หรือ a master device ใน full-duplex mode (โดยใช้ 4 pins) หรือใน half-duplex mode (โดย 6 pins). Master clock สามารถถูกจัดให้มีโดย the interface ไปยัง an external slave component เมื่อ the I2S ถูกกำหนดค่าเป็น the communication master.

คำเตือน : เนื่องจากบาง SPI3/I2S3 pins ถูกใช้ร่วมกันกับ JTAG pins (SPI3_NSS/I2S3_WS ด้วย JTDI และ SPI3_SCK/I2S3_CK
             ด้วย JTDO), มันไม่ถูกควบคุมโดย the IO controller และถูกสงวนไว้สำหรับ JTAG usage (หลังจากแต่ละ Reset). สำหรับจุด
             มุ่งหมายนี้, ก่อนที่จะกำหนดค่า the SPI3/I2S3 pins, ผู้ใช้ต้องปิดการใช้งาน the JTAG และใช้ the SWD interface (เมื่อ
             ทำการดีบัก the application), หรือปิดการใช้งานทั้ง JTAG/SWD interfaces (สำหรับ standalone applications). สำหรับ
             ข้อมูกที่มากขึ้นบน the configuration ของ JTAG/SWD interface pins อ้างอิงถึง Section 9.3.5: JTAG/SWD
             alternate function remapping.

tha

25.2 SPI and I2S main features

25.2.1 SPI features
•    Full-duplex synchronous transfers บน three lines
•    Simplex synchronous transfers บน two lines พร้อมด้วยหรือปราศจาก a bidirectional data line
•    8- or 16-bit transfer frame format selection
•    Master or slave operation
•    Multimaster mode capability
•    8 master mode baud rate prescalers (fPCLK/2 max.)
•    Slave mode frequency (fPCLK/2 max.)
•    Faster communication สำหรับทั้ง master and slave
•    NSS management โดย hardware หรือ software สำหรับทั้ง master and slave: dynamic change of master/slave
      operations
•    Programmable clock polarity and phase
•    Programmable data order ด้วย MSB-first or LSB-first shifting
•    Dedicated transmission and reception flags with interrupt capability
•    SPI bus busy status flag
•    Hardware CRC feature for reliable communication:
      – CRC value สามารถถูกส่งถ่ายเป็น last byte ใน Tx mode
      – Automatic CRC error checking for last received byte
•    Master mode fault, overrun and CRC error flags with interrupt capability
•    1-byte transmission and reception buffer with DMA capability: Tx and Rx requests

tha

25.2.2 I2S features
•    Half-duplex communication (เพียง transmitter หรือ receiver)
•    Master or slave operations
•    8-bit programmable linear prescaler เพื่อถึง accurate audio sample frequencies (from
      8 kHz to 192 kHz)
•    Data format อาจจะ 16-bit, 24-bit or 32-bit
•    Packet frame ถูกคงที่เป็น 16-bit (16-bit data frame) หรือ 32-bit (16-bit, 24-bit, 32-bit data frame) by audio
      channel
•    Programmable clock polarity (steady state)
•    Underrun flag in slave transmission mode and Overrun flag in reception mode (master
      and slave)
•    16-bit register for transmission and reception with one data register for both channel
      sides
•    Supported I2S protocols:
      – I2S Phillps standard
      – MSB-justified standard (left-justified)
      – LSB-justified standard (right-justified)
      – PCM standard (with short and long frame synchronization on 16-bit channel frame
         or 16-bit data frame extended to 32-bit channel frame)
•    Data direction is always MSB first
•    DMA capability for transmission and reception (16-bit wide)
•    Master clock อาจถูกส่งออกเพื่อขับ an external audio component. Ratio ถูกคงที่ที่ 256 × FS (where FS is the audio
      sampling frequency)
•    In connectivity line devices, both I2S (I2S2 and I2S3) have a dedicated PLL (PLL3) to
     generate an even more accurate clock.


tha

25.3 SPI functional description

25.3.1 General description
The block diagram ของ the SPI ถูกแสดงใน Figure 238.



ปกติแล้ว, the SPI ถูกต่อถึง external devices ผ่านทาง four pins:
•    MISO: Master In / Slave Out data. ขานี้สามารถถูกใช้เพื่อส่ง data ใน slave mode และรับ data ใน master mode.
•    MOSI: Master Out / Slave In data. ขานี้สามารถถูกใช้เพื่อส่ง data ใน master mode และรับ data ใน slave mode.
•    SCK: Serial Clock output สำหรับr SPI masters และ input สำหรับ SPI slaves.
•    NSS: Slave select. นี้เป็นขาทางเลือกเพื่อเลือก a slave device. ขานี้ทำงานเป็น a 'chip select' เพื่อให้ the SPI master
      ติดต่อกับ slaves ได้เฉพาะรายและเพื่อหลีกเลี่ยงความขัดแย้งบน the data lines. Slave NSS inputs สามารถถูกขับโดย standard
      IO ports บน the master device. The NSS pin อาจถูกใช้เป็น an output ได้อีกด้วยถ้าเปิดการใช้งาน (SSOE bit) และขับลง
      low ถ้า the SPI อยู่ใน master configuration. ในลักษณะนี้, ทุก NSS pins จาก devices ที่ต่อถึง the Master NSS pin เห็น a
      low level และกลายเป็น slaves เมื่อพวกมันถูกกำหนดค่าใน NSS hardware mode. เมื่อกำหนดค่าใน master mode ด้วย NSS
      ที่กำหนดค่าเป็น an input (MSTR=1 and SSOE=0) และถ้า NSS ถูกดึงลง low, the SPI จะเข้าสู่ the master mode fault
      state: the MSTR bit จะถูกเคลียร์โดยอัตโนมัติและ the device ถูกกำหนดค่าใน slave mode (อ้างอิงถึง Section 25.3.10).

ตัวอย่างพื้นฐานของ interconnections ระหว่าง a single master และ a single slave ถูกแสดงใน Figure 239.



The MOSI pins ถูกต่อเข้าด้วยกันและ the MISO pins ถูกต่อเข้าด้วยกัน. ในวิธีนี้ data ถูกส่งถ่ายแบบอนุกรมระหว่าง master และ slave (most significant bit first).

The communication ถูกเริ่มต้นโดย the master เสมอ. เมื่อ the master device ส่ง data ไปยัง a slave device ทาง the MOSI pin, the slave device ตอบสนองทาง the MISO pin. นี้หมายถึง full-duplex communication ด้วยทั้ง data out และ data in จะประสานกันด้วย the same clock signal (ซึ่งถูกจัดให้มีโดย the master device ทาง the SCK pin).

tha

Slave select (NSS) pin management
Hardware or software slave select management สามารถถูกเซ็ตโดยใช้ the SSM bit ใน the SPI_CR1 register.
•    Software NSS management (SSM = 1)
      The slave select information ถูกขับภายในโดยค่าของ the SSI bit ใน the SPI_CR1 register. The external NSS pin ยัง
      คงเป็นอิสระสำหรับการใช้โดย application อื่น.
•    Hardware NSS management (SSM = 0)
      Two configurations ที่เป็นได้ขึ้นอยู่กับ the NSS output configuration (SSOE bit ใน register SPI_CR2).
      – NSS output enabled (SSM = 0, SSOE = 1)
         configuration นี้ถูกใช้ได้เพียงเมื่อ the device ทำงานใน master mode. The NSS signal ถูกขับลง low เมื่อ the master
         สตาร์ท the communication และจะเป็น low จนกระทั่งl the SPI ถูกปิดการใช้งาน.
      – NSS output disabled (SSM = 0, SSOE = 0)
         configuration นี้ยอมให้ multimaster capability สำหรับ devices ทำงานใน master mode. สำหรับ devices ที่เซ็ตเป็น
         slave, the NSS pin ทำงานเป็น a classical NSS input: the slave ถูกเลือกเมื่อ NSS เป็น low และไม่ถูกเลือกเมื่อ NSS
         high.

tha

Clock phase and clock polarity
Four possible timing relationships อาจถูกเลือกโดย software, โดยใช้ the CPOL และ CPHA bits ใน the SPI_CR1 register. The CPOL (clock polarity) bit ควบคุม the steady state value ของ the clock เมื่อไม่มี data ถูกส่งถ่าย. บิตนี้มีผลต่อทั้ง master และ slave modes. ถ้า CPOL ถูก reset, the SCK pin มี a low-level idle state. ถ้า CPOL ถูก set, the SCK pin มี a high-level idle state.

ถ้า the CPHA (clock phase) bit ถูก set, the second edge บน the SCK pin (falling edge ถ้า the CPOL bit ถูก reset, rising edge ถ้า the CPOL bit ถูก set) คือ the MSBit capture strobe. Data ถูก latched บนการเกิดขึ้นของ the second clock transition. ถ้า the CPHA bit ถูก reset, the first edge บน the SCK pin (falling edge ถ้า CPOL bit ถูก set, rising edge ถ้า CPOL bit ถูก reset) คือ the MSBit capture strobe. Data ถูก latched บนการเกิดขึ้นของ the first clock transition.

The combination ของ the CPOL (clock polarity) และ CPHA (clock phase) bits เลือก the data capture clock edge.

Figure 240, แสดง an SPI transfer ด้วย the four combinations ของ the CPHA และ CPOL bits. The diagram อาจถูกตีความได้ว่าเป็น a master หรือ slave timing diagram เมื่อ the SCK pin, the MISO pin, the MOSI pin ถูกต่อโดยตรงระหว่าง the master และ the slave device

Note: ก่อนการเปลี่ยน the CPOL/CPHA bits the SPI ต้องถูกปิดการใช้งานโดยการรีเซ็ต the SPE bit. Master และ slave
            ต้องถูกโปรแกรมด้วย the same timing mode.

            The idle state ของ SCK ต้องตรงกันกับ the polarity ที่เลือกใน the SPI_CR1 register (โดย pulling up SCK ถ้า
            CPOL=1 หรือ pulling down SCK ถ้า CPOL=0).

            The Data Frame Format (8- or 16-bit) ถูกเลือกผ่านทาง the DFF bit ใน SPI_CR1 register, และกำหนด the data
             length ในระหว่าง transmission/reception.



Data frame format
Data สามารถถูก shifted out อย่างใดอย่างหนึ่ง MSB-first หรือ LSB-first ขึ้นอยู่กับค่าของ the LSBFIRST bit ใน the SPI_CR1 Register.

แต่ละ data frame เป็น 8 หรือ 16 bits long ขึ้นอยู่กับขนาดของ the data ที่โปรแกรมโดยใช้ the DFF bit ใน the SPI_CR1 register. The selected data frame format ใช้ได้สำหรับ transmission และ/หรือ reception.

tha

25.3.2 Configuring the SPI in slave mode
ใน the slave configuration, the serial clock ถูกรับบน the SCK pin จาก the master device. The value ที่เซ็ตใน the BR[2:0] bits ใน the SPI_CR1 register, ไม่มีผลต่อ the data transfer rate.

Note: ขอแนะนำให้เปิดการใช้งาน the SPI slave ก่อนที่ the master จะส่ง the clock. ถ้าไม่, undesired data transmission อาจ
         เกิดขึ้น. The data register ของ the slave จำเป็นที่จะพร้อมก่อน the first edge ของ the communication clock หรือก่อน
         the end ของ the ongoing communication. มันมีความจำเป็นต้องมี the polarity ของ the communication clock ที่เซ็ต
         เป็น the steady state value ก่อน the slave และ the master ถูก enabled.

ทำตามขั้นตอนด้านล่างเพื่อกำหนดค่า SPI ใน slave mode:

Procedure

1.    เซ็ต the DFF bit เพื่อกำหนด 8- หรือ 16-bit data frame format
2.    เลือก the CPOL and CPHA bits เพื่อกำหนดหนึ่งของ the four relationships ระหว่าง the data transfer และ the serial
       clock (ดู Figure 240). เพื่อ correct data transfer, the CPOL and CPHA bits ต้องถูกกำหนดค่าใน the same way ใน the
       slave device and the master device.
3.    The frame format (MSB-first หรือ LSB-first ขึ้นอยู่กับ the value ของ the LSBFIRST bit ใน the SPI_CR1 register)
       ต้องทำให้เหมือนกันกับ the master device.
4.    ใน Hardware mode (อ้างอิงถึง Slave select (NSS) pin management), the NSS pin ต้องถูกต่อถึง a low level signal
       ในระหว่าง the complete byte transmit sequence. ใน NSS software mode, เซ็ต the SSM bit และเคลียร์ the SSI bit
       ใน the SPI_CR1 register.

ในการกำหนดค่านี้ the MOSI pin เป็น a data input และ the MISO pin เป็น a data output.

Transmit sequence

The data byte ถูก parallel-loaded ลงใน the Tx buffer ในระหว่าง a write cycle.

The transmit sequence เริ่มเมื่อ the slave device รับ the clock signal และ the most significant bit ของ the data บน MOSI pin ของมัน. บิตที่เหลือ (the 7 bits ใน 8-bit data frame format, และ the 15 bits ใน 16-bit data frame format) ถูกโหลดลงใน the shift-register. The TXE flag ใน the SPI_SR register ถูกเซ็ตบน the transfer ของ data จาก the Tx Buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

Receive sequence

สำหรับ the receiver, เมื่อ data transfer เสร็จสมบูรณ์:
•    The Data ใน shift register ถูก transferred ไปยัง Rx Buffer และ the RXNE flag (SPI_SR register) ถูกเซ็ต
•    An Interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน the SPI_CR2 register

หลังจาก the last sampling clock edge the RXNE bit ถูกเซ็ต, a copy ของ the data byte ที่รับใน the shift register ถูกย้ายไปยัง the Rx buffer. เมื่อ the SPI_DR register ถูกอ่าน, the SPI peripheral จะส่งคืนค่า buffered นี้.

การเคลียร์ของ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register.

tha

25.3.3 Configuring the SPI in master mode

ใน the master configuration, the serial clock ถูกสร้างขึ้นบน the SCK pin.

Procedure

1.    เลือก the BR[2:0] bits เพื่อกำหนด the serial clock baud rate (ดู SPI_CR1 register).
2.    เลือก the CPOL and CPHA bits เพื่อกำหนดหนึ่งของ the four relationships ระหว่าง the data transfer และ the serial
       clock (ดู Figure 240).
3.    เซ็ต the DFF bit เพื่อกำหนด 8- หรือ 16-bit data frame format
4.    กำหนดค่า the LSBFIRST bit ใน the SPI_CR1 register เพื่อกำหนด the frame format.
5.    ถ้า the NSS pin ถูกต้องการใน input mode, ใน hardware mode, ต่อ the NSS pin ถึง a high-level signal ในระหว่าง the
       complete byte transmit sequence. ใน NSS software mode, เซ็ต the SSM และ SSI bits ใน the SPI_CR1 register.
       ถ้า the NSS pin ถูกต้องการใน output mode, the SSOE bit เพียงอย่างเดียวที่ควรถูกเซ็ต.
6.    The MSTR and SPE bits ต้องถูกเซ็ต (พวกมันยังคงเซ็ตเพียงถ้า the NSS pin ถูกต่อถึง a high-level signal).

ในการกำหนดค่านี้ the MOSI pin เป็น a data output และ the MISO pin เป็น a data input.

Transmit sequence

The transmit sequence เริ่มเมื่อ a byte ถูกเขียนใน the Tx Buffer.

The data byte ถูก parallel-loaded ลงใน the shift register (จาก the internal bus) ในระหว่าง the first bit transmission และจากนั้นเลื่อนออกไปตามลำดับไปที่ the MOSI pin MSB first หรือ LSB first ขึ้นอยู่กับ the LSBFIRST bit ใน the SPI_CR1 register. The TXE flag ถูกเซ็ตบน the transfer ของ data จาก the Tx Buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

Receive sequence

สำหรับ the receiver, เมื่อ data transfer เสร็จสมบูรณ์:
•    The Data ใน shift register ถูก transferred ไปยัง Rx Buffer และ the RXNE flag (SPI_SR register) ถูกเซ็ต
•    An Interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน the SPI_CR2 register

ที่ the last sampling clock edge the RXNE bit ถูกเซ็ต, a copy ของ the data byte ที่รับใน the shift register ถูกย้ายไปยัง the Rx buffer. เมื่อ the SPI_DR register ถูกอ่าน, the SPI peripheral จะส่งคืนค่า buffered นี้.

การเคลียร์ของ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register.

A continuous transmit stream สามารถถูกรักษาไว้ถ้า the next data ที่จะส่งถ่ายถูกใส่ลงใน the Tx buffer ทันใดนั้น the transmission ถูกสตาร์ท. โปรดจำเอาไว้ว่า TXE flag ควรเป็น '1 ก่อนความพยายามใดๆที่จะเขียน the Tx buffer ถูกทำ.

Note: เมื่อ a master กำลังสื่อสารกับ SPI slaves ซึ่งจำเป็นต้องยกเลิกการเลือกระหว่าง transmissions, the NSS pin ต้องถูกกำหนด
         ค่าเป็น GPIO หรือ GPIO อื่นต้องถูกใช้และสลับโดย software.