STM32F1 SPI

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

Previous topic - Next topic

tha

25.3.4 Configuring the SPI for half-duplex communication

The SPI มีความสามารถของการทำงานใน half-duplex mode ใน 2 configurations.
•    1 clock และ 1 bidirectional data wire
•    1 clock และ 1 data wire (รับเพียงอย่างเดียวหรือส่งเพียงอย่างเดียว)

1 clock and 1 bidirectional data wire (BIDIMODE = 1)

โหมดนี้ถูกเปิดใช้งานโดยการเซ็ต the BIDIMODE bit ใน the SPI_CR1 register. ในโหมดนี้ SCK ถูกใช้สำหรับ the clock และ MOSI ใน master หรือ MISO ใน slave mode ถูกใช้สำหรับ data communication. The transfer direction (Input/Output) ถูกเลือกโดย the BIDIOE bit ใน the SPI_CR1 register. เมื่อบิตนี้เป็น 1, the data line เป็น output มิฉะนั้นมันจะเป็น input.

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

ในโหมดนี้, the application สามรถใช้ the SPI อย่างใดอย่างหนึ่งใน transmit-only mode หรือใน receive-only mode.
•    Transmit-only mode ก็เหมือนกันกับ full-duplex mode (BIDIMODE=0, RXONLY=0): the data ถูกส่งไปบน the
      transmit pin (MOSI ใน master mode หรือ MISO ใน slave mode) และ the receive pin (MISO ใน master mode หรือ
      MOSI ใน slave mode) สามารถถูกใช้เป็น a general-purpose IO. ในกรณีนี้, the application เพียงต้องการเพิกเฉยต่อ the Rx
      buffer (ถ้า the data register ถูกอ่าน, มันไม่ได้บรรจุ the received value).
•    ใน receive-only mode, the application สามารถปิดการใช้งาน the SPI output function โดยการเซ็ต the RXONLY bit ใน
      the SPI_CR1 register. ในกรณีนี้, มันจะปล่อยเป็นอิสระ the transmit IO pin (MOSI ใน master mode หรือ MISO ใน slave
      mode), ดังนั้นมันสามารถถูกใช้เพื่อวัตถุประสงค์อื่น.

เพื่อสตาร์ท the communication ใน receive-only mode, กำหนดค่าและเปิดการใช้งาน the SPI:
•    ใน master mode, the communication สตาร์ททันทีและหยุดเมื่อ the SPE bit ถูกเคลียร์และ the current reception หยุด.
      ไม่มีความจำเป็นต้องอ่าน the BSY flag ในโหมดนี้. มันถูกเซ็ตเสมอเมื่อ an SPI communication กำลังดำเนินการอยู่.
•    ใน slave mode, the SPI ยังคงรับต่อไปตราบเท่าที่ the NSS ถูก pulled down (หรือ the SSI bit ถูกเคลียร์ใน NSS software
      mode) และ the SCK กำลังรันอยู่.

tha

25.3.5 Data transmission and reception procedures

Rx and Tx buffers

ใน reception, data ถูกรับและจากนั้นถูกเก็บลงใน an internal Rx buffer ขณะที่ใน transmission, data ในอันดับแรกถูกเก็บลงใน an internal Tx buffer ก่อนที่จะถูกส่ง.

A read access ของ the SPI_DR register จะคืนกลับ the Rx buffered value ในขณะที่ a write access ไปยัง the SPI_DR เก็บรักษา the written data ลงใน the Tx buffer.

Start sequence in master mode

•    ใน full-duplex (BIDIMODE=0 and RXONLY=0)
      – The sequence เริ่มเมื่อ data ถูกเขียนลงใน the SPI_DR register (Tx buffer).
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission และจากนั้นเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.
      – ในเวลาเดียวกัน, the received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้น
         parallel loaded ลงใน the SPI_DR register (Rx buffer).
•    ใน unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
      – The sequence เริ่มทันทีที่ SPE=1
      – มีเพียง the receiver ถูกทำให้ทำงานและ the received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift
         register และจากนั้นถูกโหลดแบบขนานลงใน the SPI_DR register (Rx buffer).
•    ใน bidirectional mode, เมื่อทำการส่ง (BIDIMODE=1 and BIDIOE=1)
      – The sequence เริ่มเมื่อ data ถูกเขียนลงใน the SPI_DR register (Tx buffer).
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission และจากนั้นเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.
      – ไม่มี data ถูกรับ.
•    ใน bidirectional mode, เมื่อทำการรับ (BIDIMODE=1 and BIDIOE=0)
      – The sequence เริ่มทันทีที่ SPE=1 and BIDIOE=0.
      – The received data บน the MOSI pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้นถูกโหลดแบบขนาน
         ลงใน the SPI_DR register (Rx buffer).
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MOSI pin.

tha

Start sequence in slave mode

•    ใน full-duplex mode (BIDIMODE=0 and RXONLY=0)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MOSI pin ของมัน.
         The 7 remaining bits ถูกโหลดลงใน the shift register.
      – ในเวลาเดียวกัน, the data ถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission, และจากนั้นถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin. The software ต้องมีการเขียน the data ที่จะถูกส่ง
         ก่อน the SPI master device เริ่นต้น the transfer.
•    ใน unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MOSI pin ของมัน.
         The 7 remaining bits ถูกโหลดลงใน the shift register.
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin.
•    ใน bidirectional mode, เมื่อทำการส่ง (BIDIMODE=1 and BIDIOE=1)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ใน the Tx buffer ถูกส่งไปบน the MISO
         pin.
      – The data จากนั้นถูก parallel loaded จาก the Tx buffer ลงใน the 8-bit shift register ในระหว่าง the first bit
         transmission  และจากนั้นถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin. The software ต้องมีการเขียน the data ที่จะถูกส่ง
         ก่อน the SPI master device เริ่นต้น the transfer.
      – ไม่มี data ถูกรับ.
•    ใน bidirectional mode, เมื่อทำการรับ (BIDIMODE=1 and BIDIOE=0)
      – The sequence เริ่มเมื่อ the slave device รับ the clock signal และ the first bit ของ the data บน MISO pin ของมัน.
      – The received data บน the MISO pin ถูกเลื่อนเข้าแบบอนุกรมไปยัง the 8-bit shift register และจากนั้น parallel loaded
         ลงใน the SPI_DR register (Rx buffer).
      – The transmitter ไม่ถูกทำให้ทำงานและไม่มี data ถูกเลื่อนออกแบบอนุกรมไปยัง the MISO pin.


tha

Handling data transmission and reception

The TXE flag (Tx buffer ว่าง) ถูกเซ็ตเมื่อ the data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register. มันแสดงให้เห็นว่า the internal Tx buffer พร้อมที่จะถูกโหลดด้วย the next data. An interrupt สามารถถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต. การเคลียร์ the TXE bit ถูกกรทำโดยการเขียนไปยัง the SPI_DR register.

Note: The software ต้องแน่ใจว่า the TXE flag ถูกเซ็ตเป็น 1 ก่อนพยายามที่จะเขียนไปยัง the Tx buffer. มิฉะนั้น, มันจะเขียนทับ
         the data ก่อนหน้าที่เขียนไปยัง the Tx buffer.

The RXNE flag (Rx buffer ไม่ว่าง) ถูกเซ็ตบน the last sampling clock edge, เมื่อ the data ถูกส่งถ่ายจาก the shift register ไปยัง the Rx buffer. มันแสดงให้เห็นว่า data พร้อมที่จะถูกอ่านจาก the SPI_DR register. An interrupt สามารถถูกสร้างขึ้นถ้า the RXNEIE bit ใน the SPI_CR2 register ถูกเซ็ต. การเคลียร์ the RXNE bit ถูกกระทำโดยการอ่าน the SPI_DR register

สำหรับบาง configurations, the BSY flag สามารถถูกใช้ในระหว่าง the last data transfer เพื่อคอยจนกระทั่ง the completion ของ the transfer.

Full-duplex transmit และ receive procedure ใน master หรือ slave mode (BIDIMODE=0 และ RXONLY=0)

The software ต้องทำตามขั้นตอนนี้เพื่อ transmit และ receive data (ดู Figure 241 และ Figure 242):
1.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1.
2.    เขียน the first data item ที่จะถูกส่งลงใน the SPI_DR register (นี้เคลียร์ the TXE flag).
3.    คอยจนกระทั่ง TXE=1 และเขียน the second data item ที่จะถูกส่ง. จากนั้นคอยจนกระทั่ง RXNE=1 และอ่าน the SPI_DR จะได้
       the first received data item (นี้เคลียร์ the RXNE bit). ทำซ้ำการทำงานนี้สำหรับแต่ละ data item ที่จะถูก
       transmitted/received จนกระทั่ง the n–1 received data.
4.    คอยจนกระทั่ง RXNE=1 และอ่าน the last received data.
5.    คอยจนกระทั่ง TXE=1 และจากนั้นคอยจนกระทั่ง BSY=0 ก่อนปิดการใช้งาน the SPI.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the RXNE หรือ TXE flag




tha

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

ในโหมดนี้, the procedure สามารถถูกลดลงดังอธิบายข้างล่างและ the BSY bit สามารถถูกใช้เพื่อคอยจนกระทั่ง the completion ของ the transmission (ดู Figure 243 and Figure 244).
1.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1.
2.    เขียน the first data item ที่จะส่งลงใน the SPI_DR register (นี้เคลียร์ the TXE bit).
3.    คอยจนกระทั่ง TXE=1 และเขียน the next data item ที่จะถูกส่ง. ทำซ้ำขั้นตอนนี้สำหรับแต่ละ data item ที่จะถูกส่ง.
4.    หลังจากการเขียน the last data item ลงใน the SPI_DR register, คอยจนกระทั่ง TXE=1, จากนั้นคอยจนกระทั่ง BSY=0, นี้
       แสดงให้เห็นว่า the transmission ของ the last data เสร็จสมบูรณ์.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the TXE flag

Note: ในระหว่างการสื่อสารที่ไม่ต่อเนื่อง, จะมี a 2 APB clock period delay ระหว่าง the write operation ไปยัง SPI_DR และ the
         BSY bit เซ็ต. ด้วยเหตุนี้, ใน transmit-only mode, มันมีข้อบังคับให้คอยเป็นอันดับแรกจนกระทั่ง TXE ถูกเซ็ตและจากนั้นจน
         กระทั่ง BSY ถูกเคลียร์หลังจากการเขียน the last data.

         หลังจากการส่ง two data items ใน transmit-only mode, the OVR flag ถูกเซ็ต in the SPI_SR register เนื่องจาก the
         received data ไม่เคยถูกอ่าน.


tha

Bidirectional transmit procedure (BIDIMODE=1 and BIDIOE=1)

ในโหมดนี้, ขั้นตอนก็เหมือนกันกับขั้นตอนใน Transmit-only mode ยกเว้นที่ the BIDIMODE and BIDIOE bits ทั้งคู่ต้องถูกเซ็ตใน the SPI_CR2 register ก่อนการเปิดการใช้งาน the SPI.

Unidirectional receive-only procedure (BIDIMODE=0 and RXONLY=1)

ในโหมดนี้, ขั้นตอนสามารถถูกลดดังอธิบายข้างล่าง (ดู Figure 245):
1.    เซ็ต the RXONLY bit ใน the SPI_CR1 register.
2.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit เป็น 1:
          a) ใน master mode, นี้จะทำให้ทำงานโดยทันที the generation ของ the SCK clock, และ data ถูกรับแบบอนุกรมจนกระทั่งl
              the SPI ถูกปิดการใช้งาน (SPE=0).
          b) ใน slave mode, data ถูกรับเมื่อ the SPI master device ขับ NSS low และสร้าง the SCK clock.
3.    คอยจนกระทั่ง RXNE=1 และอ่าน the SPI_DR register เพื่อให้ได้ the received data (นี้เคลียร์ the RXNE bit). ทำซ้ำการ
       ทำงานนี้สำหรับแต่ละ data item ที่จะถูกรับ.

ขั้นตอนนี้สามารถถูกนำไปใช้กับ dedicated interrupt subroutines ได้อีกด้วย เริ่มที่แต่ละ rising edges ของ the RXNE flag

Note: ถ้ามีความจำเป็นที่จะปิดการใช้งาน the SPI หลังจาก the last transfer, ทำตามคำแนะนำที่อธิบายใน Section 25.3.8.


tha

Bidirectional receive procedure (BIDIMODE=1 and BIDIOE=0)

ในโหมดนี้, ขั้นตอนก็เหมือนกันกับขั้นตอน the Receive-only mode ยกเว้นที่ the BIDIMODE ต้องถูกเซ็ตและ the BIDIOE bit เคลียร์ใน the SPI_CR2 register ก่อนการเปิดการใช้งาน the SPI.

Continuous and discontinuous transfers

เมื่อส่ง data ใน master mode, ถ้า the software มีความเร็วเพียงพอที่จะตรวจพบแต่ละ rising edge ของ TXE (หรือ TXE interrupt) และเขียนทันทีไปยัง the SPI_DR register ก่อน the ongoing data transfer เสร็จสมบูรณ์, the communication ถูกพูดได้ว่า continuous. ในกรณีนี้, ไม่มีความไม่ต่อเนื่องในการสร้างของ the SPI clock ระหว่างแต่ละ data item และ the BSY bit ไม่เคยถูกเคลียร์ระหว่างแต่ละ data transfer.

ในทางตรงกันข้าม, ถ้า the software ไม่มีความเร็วเพียงพอ, นี้สามารถนำมาซึ่งบางความไม่ต่อเนื่องใน the communication. ในกรณีนี้, the BSY bit ถูกเคลียร์ระหว่างแต่ละ data transmission
(ดู Figure 246).

ใน Master receive-only mode (RXONLY=1), the communication จะต่อเนื่องเสมอและ the BSY flag ถูกอ่านได้เป็น 1 เสมอ.

ใน slave mode, ความต่อเนื่องของ the communication ถูกกำหนดโดย the SPI master device. ในกรณีใดๆ, ถึงแม้ว่า the communication จะต่อเนื่อง, the BSY flag จะ low ระหว่างแต่ละ transfer เป็นระยะเวลาขั้นต่ำของ one SPI clock cycle (ดู Figure 244).


tha

25.3.6 CRC calculation

A CRC calculator ถูกนำมาใช้สำหรับ communication reliability. CRC calculators ที่แยกกันถูกนำมาใช้สำหรับ transmitted data และ received data. The CRC ถูกคำนวนโดยใช้ a programmable polynomial อนุกรมบนแต่ละบิต. มันถูกคำนวนบน the sampling clock edge ที่กำหนดโดย the CPHA และ CPOL bits ใน the SPI_CR1 register.

Note: SPI มีสองชนิคของ of CRC calculation standard ซึ่งขึ้นโดยตรงกับ the data frame format ที่เลือกสำหรับ the
         transmission และ/หรือ reception: 8-bit data (CR8) และ 16-bit data (CRC16).

CRC calculation ถูกเปิดการใช้งานโดยการเซ็ต the CRCEN bit ใน the SPI_CR1 register. การกระทำนี้รีเซ็ต the CRC registers (SPI_RXCRCR และ SPI_TXCRCR). ใน full duplex หรือ transmitter only mode, เมื่อ the transfers ถูกจัดการโดย the software (CPU mode), มันมีความจำเป็นที่ต้องเขียน the bit CRCNEXT ทันทีหลังจาก the last data ที่จะถูกส่งถ่ายถูกเขียนไปยัง the SPI_DR. ที่ the end ของ last data transfer นี้, the SPI_TXCRCR value ถูกส่ง.

ใน receive only mode และเมื่อ the transfers ถูกจัดการโดย software (CPU mode),  มันมีความจำเป็นที่ต้องเขียน the CRCNEXT bit หลังจาก the second last data ถูกรับแล้ว. The CRC ถูกรับพอดีหลังจาก the last data reception และ the CRC check จากนั้นถูกดำเนินการ.

ที่ the end ของ data และ CRC transfers, the CRCERR flag ใน the SPI_SR register ถูกเซ็ตถ้า corruption เกิดขึ้นในระหว่าง the transfer

ถ้า data มีอยู่ใน the TX buffer, the CRC value ถูกส่งได้เพียงหลังจาก the transmission ของ the data byte. ในระหว่าง CRC transmission, the CRC calculator ถูก switched off และ the register value ยังคงไม่เปลี่ยนแปลง.

SPI communication โดยใช้ the CRC สามารถเป็นได้ผ่านทางขั้นตอนต่อไปนี้:
1.    โปรแกรม the CPOL, CPHA, LSBFirst, BR, SSM, SSI and MSTR values.
2.    โปรแกรม the polynomial ใน the SPI_CRCPR register.
3.    เปิดการใช้งาน the CRC calculation โดยการเซ็ต the CRCEN bit ใน the SPI_CR1 register. นี้จะเคลียร์ the SPI_RXCRCR
       and SPI_TXCRCR registers อีกด้วย.
4.    เปิดการใช้งาน the SPI โดยการเซ็ต the SPE bit ใน the SPI_CR1 register
5.    สตาร์ท the communication และค้ำจุน the communication จนกระทั่งหมดเว้นแต่ one byte หรือ halfword ถูก transmitted
      หรือ received แล้ว.
         – ใน full duplex หรือ transmitter-only mode, เมื่อ the transfers ถูกจัดการโดย software, เมื่อเขียน the last byte หรือ
            half word ไปยัง the Tx buffer, เซ็ต the CRCNEXT bit ใน the SPI_CR1 register เพื่อแสดงให้เห็นว่า the CRC จะถูก
            ส่งหลังจาก the transmission ของ the last byte.
         – ใน receiver only mode, เซ็ต the bit CRCNEXT แค่หลังจาก the reception ของที่สองก่อนถึง last data เพื่อเตรียม the
            SPI เข้าสู่ใน CRC Phase ที่ the end ของ the reception ของ the last data. CRC calculation ถูกแช่แข็งในระหว่าง the
            CRC transfer.
6.    หลังจาก the transfer ของ the last byte หรือ half word, the SPI เข้าสู่ the CRC transfer และ check phase. ใน full
       duplex mode หรือ receiver-only mode, the received CRC ถูกเปรียบเทียบกับ the SPI_RXCRCR value. ถ้า the value
       ไม่เท่ากัน, the CRCERR flag ใน SPI_SR ถูกเซ็ตและ an interrupt สามารถถูกสร้างขึ้นเมื่อ the ERRIE bit ใน the SPI_CR2
       register ถูกเซ็ต.

Note: เมื่อ the SPI อยู่ใน slave mode, ควรระมัดระวังการเปิดการใช้งาน CRC calculation เฉพาะเมื่อ the clock มีความเสถียร, นั่นคือ,
         เมื่ the clock อยู่ใน the steady state. ถ้าไม่, a wrong CRC calculation อาจถูกทำได้. ในความเป็นจริง, the CRC มีความไว
         ต่อ the SCK slave input clock ทันทีที่ CRCEN ถูกเซ็ต, และนี่, the value ของ the SPE bit เป็นอะไรก็ตาม.

        ด้วย high bitrate frequencies, โปรดระมัดระวังเมื่อทำการส่ง the CRC. ดังที่ the number ของ used CPU cycles ต้องทำให้
        ต่ำที่สุดเท่าที่เป็นได้ใน the CRC transfer phase, มันมีข้อห้ามที่จะเรียก software functions ใน the CRC transmission
        sequence เพื่อหลีกเลี่ยง errors ใน the last data และ CRC reception. ในความเป็นจริง, CRCNEXT bit ต้องถูกเขียนก่อน
        the end ของ the transmission/reception ของ the last data.

       สำหรับ high bit rate frequencies, แนะนำให้ใช้ the DMA mode เพื่อหลีกเลี่ยง the degradation ของ the SPI speed
       performance เนื่องจาก CPU accesses กระทบต่อ the SPI bandwidth.

       เมื่อ the devices ถูกกำหนดเป็น slaves และ the NSS hardware mode ถูกใช้, the NSS pin จำเป็นต้องให้เป็น low ระหว่าง
       the data phase และ the CRC phase.

เมื่อ the SPI ถูกกำหนดค่าใน slave mode พร้อมด้วย the CRC feature ถูกเปิดการใช้งาน, CRC calculation เกิดขึ้นแม้ว่า a high level ถูกใช้บน the NSS pin. นี้อาจเกิดขึ้นตัวอย่างเช่นในกรณีของ a multislave environment ที่ the communication master ระบุ slaves สลับกัน.


ระหว่าง a slave deselection (high level บน NSS) และ a new slave selection (low level บน NSS), the CRC value ควรถูกเคลียร์บนทั้ง master และ slave sides เพื่อประสานกันใหม่ the master และ slave สำหรับ CRC calculation ของมันตามลำดับ.

เพื่อเคลียร์ the CRC, ทำตามขั้นตอนข้างล่างนี้:
1. Disable SPI (SPE = 0)
2. Clear the CRCEN bit
3. Set the CRCEN bit
4. Enable the SPI (SPE = 1)