STM32F1 SPI

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

Previous topic - Next topic

tha

25.3.7 Status flags
Four status flags ถูกจัดให้มีเพื่อให้ the application เฝ้าดู the state ของ the SPI bus ได้อย่างสมบูรณ์.

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

Rx buffer not empty (RXNE)
เมื่อเซ็ต, flag นี้แสดงให้เห็นว่ามี valid received data ใน the Rx buffer. มันถูกเคลียร์เมื่อ SPI_DR ถูกอ่าน.

BUSY flag
BSY flag นี้ถูกเซ็ตและเคลียร์โดย hardware (การเขียนไปยัง flag นี้จะไม่มีผลอะไร). The BSY flag แสดงให้เห็นว่า the state ของ the communication layer ของ the SPI.

เมื่อ BSY ถูกเซ็ต, มันแสดงให้เห็นว่า the SPI กำลังยุ่งอยู่กับการสื่อสาร. มีข้อยกเว้นอันหนึ่งใน master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) ที่ the BSY flag เป็น low ในระหว่าง reception.

The BSY flag ถูกใช้ให้เป็นประโยชน์เพื่อตรวจจับ the end ของ a transfer ถ้า the software ต้องการปิดการใช้งาน the SPI และเข้าสู่ Halt mode (หรือปิดการใช้งาน the peripheral clock). นี้หลีกเลี่ยงการเสียหาย the last transfer. สำหรับการนี้, ขั้นตอนที่อธิบายด้านล่างนี้ต้องปฏิบัติตามอย่างเคร่งครัด.

The BSY flag ถูกใช้ให้เป็นประโยชน์เพื่อหลีกเลี่ยง write collisions ใน a multimaster system.

The BSY flag ถูกเซ็ตเมื่อ a transfer สตาร์ท, ด้วยมีข้อยกเว้นของ master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0).

มันถูกเคลียร์:
•    เมื่อ a transfer เสร็จ (ยกเว้นใน master mode ถ้า the communication มีความต่อเนื่อง)
•    เมื่อ the SPI ถูกปิดการใช้งาน
•    เมื่อ a master mode fault เกิดขึ้น (MODF=1)

เมื่อ communication ไม่ต่อเนื่อง, the BSY flag เป็น low ระหว่างแต่ละ communication

เมื่อ communication ต่อเนื่อง:
•    ใน master mode, the BSY flag เป็น high ในระหว่าง the transfers ทั้งหมด
•    ใน slave mode, the BSY flag เป็น low สำหรับ one SPI clock cycle ระหว่างแต่ละ transfer

Note: ไม่ควรใช้ the BSY flag เพื่อจัดการแต่ละ data transmission หรือ reception. มันดีกว่าที่จะใช้ the TXE and RXNE flags แทน.

tha

25.3.8 Disabling the SPI

เมื่อ a transfer สิ้นสุดลง, the application สามารถหยุด the communication โดยการปิดการใช้งาน the SPI peripheral. นี้ถูกทำโดยการเคลียร์ the SPE bit.

สำหรับบางการกำหนดค่า, การปิดการใช้งาน the SPI และเข้าสู่ the Halt mode ในขณะที่ a transfer กำลังดำเนินการอยู่สามารถเป็นเหตุให้ the current transfer เสียหายและ/หรือ the BSY flag อาจจะไม่น่าเชื่อถือได้.

เพื่อหลีกเลี่ยงผลกระทบเหล่านี้ขอแนะนำให้ปฏิบัติตามขั้นตอนต่อไปนี้เมื่อปิดการใช้งาน SPI:

In master or slave full-duplex mode (BIDIMODE=0, RXONLY=0)

1.    คอยจนกระทั่ง RXNE=1 เพื่อรับ the last data
2.    คอยจนกระทั่ง TXE=1
3.    จากนั้นคอยจนกระทั่ง BSY=0
4.    ปิดการใช้งาน the SPI (SPE=0) และ, ท้ายที่สุด, เข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

In master or slave unidirectional transmit-only mode (BIDIMODE=0,
RXONLY=0) or bidirectional transmit mode (BIDIMODE=1, BIDIOE=1)


หลังจาก the last data ถูกเขียนลงใน the SPI_DR register:
1.    คอยจนกระทั่ง TXE=1
2.    จากนั้นคอยจนกระทั่ง BSY=0
3.    ปิดการใช้งาน the SPI (SPE=0) และ, ท้ายที่สุด, เข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

In master unidirectional receive-only mode (MSTR=1, BIDIMODE=0,
RXONLY=1) or bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0)


กรณีนี้ต้องได้รับการจัดการโดยเฉพาะเพื่อให้แน่ใจว่า the SPI ไม่เริ่มต้น a new transfer:
1.    คอยสำหรับที่สองก่อนถึง last occurrence ของ RXNE=1 (n–1)
2.    จากนั้นคอยสำหรับ one SPI clock cycle (โดย a software loop) ก่อนปิดการใช้งาน the SPI (SPE=0)
3.    จากนั้นคอยสำหรับ the last RXNE=1 ก่อนเข้าสู่ the Halt mode (หรือปิดการใช้งาน the peripheral clock)

Note: In master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0), the BSY flag เป็น low ในระหว่าง
           transfers.

In slave receive-only mode (MSTR=0, BIDIMODE=0, RXONLY=1) or
bidirectional receive mode (MSTR=0, BIDIMODE=1, BIDOE=0)


1.    คุณสามารถปิดการใช้งาน the SPI (write SPE=1) ที่เวลาใดก็ได้: the current transfer จะเสร็จสมบูรณ์ก่อน the SPI ถูกปิดการ
       ใช้งานจะมีผล
2.    จากนั้น, ถ้าคุณต้องการเข้าสู่ the Halt mode, คุณต้องอันดับแรกคอยจนกระทั่ง BSY = 0 ก่อนเข้าสู่ the Halt mode (หรือปิดการใช้
       งาน the peripheral clock)

tha

25.3.9 SPI communication using DMA (direct memory addressing)

ในการทำงานที่ maximum speed ของมัน, the SPI จำเป็นต้องป้อนด้วย the data สำหรับ transmission และ the data received บน the Rx buffer ควรถูกอ่านเพื่อหลีกเลี่ยง overrun. เพื่ออำนวยความสะดวก the transfers, the SPI มีคุณลักษณะ a DMA capability จัดให้มี a simple request/acknowledge protocol.

A DMA access ถูกร้องขอเมื่อ the enable bit ใน the SPI_CR2 register ถูกเปิดการใช้งาน. การร้องขอที่แยกกันต้องถูกปล่อยออกไปยัง the Tx และ Rx buffers (ดู Figure 247 และ Figure 248):
•    ใน transmission, a DMA request ถูกปล่อยออกแต่ละเวลาที่ TXE ถูกเซ็ตเป็น 1. The DMA จากนั้นเขียนไปยัง the SPI_DR
      register (นี้เคลียร์ the TXE flag).
•    ใน reception, a DMA request ถูกปล่อยออกแต่ละเวลาที่ RXNE ถูกเซ็ตเป็น 1. The DMA จากนั้นอ่าน the SPI_DR register (นี้
      เคลียร์ the RXNE flag).

เมื่อ the SPI ถูกใช้เฉพาะส่ง data, มันเป็นไปได้ที่จะเปิดการใช้งานเพียง the SPI Tx DMA channel. ในกรณีนี้, the OVR flag ถูกเซ็ตเพราะว่า the data received ไม่ถูกอ่าน.

เมื่อ the SPI ถูกใช้เฉพาะรับ data, มันเป็นไปได้ที่จะเปิดการใช้งานเพียง the SPI Rx DMA channel.

ใน transmission mode, เมื่อ the DMA เขียนl the data ทั้งหมดที่ถูกส่งหมดแล้ว (flag TCIF ถูกเซ็ตใน the DMA_ISR register), the BSY flag สามารถถูกเฝ้าดูเพื่อให้แน่ใจว่า the SPI communication เสร็จสมบูรณ์. นี้ถูกต้องการเพื่อหลีกเลี่ยงความเสียหายของ the last transmission ก่อนการปิดการใช้งาน the SPI หรือการเข้าสู่ the Stop mode. The software ต้องอันดับแรกคอยจนกระทั่ง TXE=1 และจากนั้นจนกระทั่ง BSY=0.

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



DMA capability with CRC

เมื่อ SPI communication ถูกเปิดการใช้งานพร้อมด้วย CRC communication และ DMA mode, the transmission และ reception ของ the CRC ที่ the end ของ communication เป็นไปโดยอัตโนมัตินั่นคือปราศจากการใช้ the bit CRCNEXT. หลังจาก the CRC reception, the CRC ต้องถูกอ่านใน the SPI_DR register เพื่อเคลียร์ the RXNE flag.

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


tha

25.3.10 Error flags

Master mode fault (MODF)

Master mode fault เกิดขึ้นเมื่อ the master device มี NSS pin ของมันถูกดึงลง low (ใน NSS hardware mode) หรือ SSI bit low (ใน NSS software mode), นี้เซ็ต the MODF bit โดยอัตโนมัติ.

Master mode fault มีผลต่อ the SPI peripheral ในวิธีต่อไปนี้:
•    The MODF bit ถูกเซ็ตและ an SPI interrupt ถูกสร้างขึ้นถ้า the ERRIE bit ถูกเซ็ต.
•    The SPE bit ถูกเคลียร์. นี้จะบล็อก output ทั้งหมดจาก the device และปิดการใช้งาน the SPI interface.
•    The MSTR bit ถูกเคลียร์, ดังนั้นบังคับให้ the device ลงใน slave mode

ใช้ the following software sequence เพื่อเคลียร์ the MODF bit:
1.    ทำการ a read or write access ไปยัง the SPI_SR register ในขณะที่ the MODF bit ถูกเซ็ต.
2.    จากนั้นเขียนถึง the SPI_CR1 register.

เพื่อหลีกเลี่ยง slave หลายตัวขัดแย้งกันใน a system ที่ประกอบด้วย MCUs หลายตัว, the NSS pin ต้องถูกดึงขึ้น high ในระหว่าง the MODF bit clearing sequence. The SPE and MSTR bits สามารถถูกเอากลับคืนสู่สถานะเดิมของมันหลังจาก clearing sequence นี้.

เพื่อความปลอดภัย, hardware ไม่ยอมให้มีการเซ็ตของ the SPE and MSTR bits ในขณะที่ the MODF bit ถูกเซ็ต.

ใน a slave device the MODF bit ไม่สามารถถูกเซ็ต. อย่างไรก็ตาม, ใน a multimaster configuration, the device สามารถอยู่ใน slave mode พร้อมด้วย MODF bit นี้เซ็ต. ในกรณีนี้, the MODF bit แสดงให้เห็นว่าอาจมี a multimaster conflict สำหรับ system control. An interrupt routine สามารถถูกใช้เพื่อกู้คืนอย่างหมดจดจากสถานะนี้โดยทำการ a reset หรือกลับคืนสู่สถานะเริ่มต้น.

Overrun condition

An overrun condition เกิดขึ้นเมื่อ the master device ส่ง data bytes และ the slave device ไม่ได้เคลียร์ the RXNE bit อันเป็นผลมาจากการส่ง the previous data byte.
เมื่อ an overrun condition เกิดขึ้น:
•    the OVR bit ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the ERRIE bit ถูกเซ็ต.

ในกรณีนี้, the receiver buffer contents จะไม่ถูกอัปเดตด้วย the newly received data จาก the master device. การอ่านจาก the SPI_DR register จะส่งกลับ byte นี้. ไบต์ที่ส่งในภายหลังอื่นๆทั้งหมดจะสูญหาย.

การเคลียร์ the OVR bit ถูกทำโดยการอ่านจาก the SPI_DR register ตามด้วยโดย a read access ไปยัง the SPI_SR register

CRC error

แฟล็กนี้ถูกใช้เพื่อตรวจสอบความถูกต้องของค่าที่ได้รับเมื่อ the CRCEN bit ใน the SPI_CR1 register ถูกเซ็ต. The CRCERR flag ใน the SPI_SR register ถูกเซ็ตถ้าค่าที่ได้รับใน the shift register ไม่เท่ากันกับ the receiver SPI_RXCRCR value.


tha

25.4 I2S functional description

The I2S audio protocol ไม่มีให้ใช้ใน low- and medium-density devices. ส่วนนี้เกี่ยวข้องเฉพาะ high-density, XL-density and connectivity line devices.



The SPI สามารถทำหน้าที่เป็น an audio I2S interface เมื่อ the I2S capability ถูกเปิดการใช้งาน (โดยการเซ็ต the I2SMOD bit ใน the SPI_I2SCFGR register). interface นี้ใช้เกือบ the same pins, flags และ interrupts เป็น the SPI.

The I2S ใช้ร่วม three common pins กับ the SPI:
•    SD: Serial Data (mapped บน the MOSI pin) เพื่อส่งหรือรับ the two time-multiplexed data channels (ใน half-
      duplex mode เท่านั้น).
•    WS: Word Select (mapped บน the NSS pin) คือ the data control signal เอ๊าพุทใน master mode และอินพุทใน slave
      mode.
•    CK: Serial Clock (mapped บน the SCK pin) คือ the serial clock เอ๊าพุทใน master mode และ serial clock อินพุทใน
      slave mode.

An additional pin สามารถถูกใช้เมื่อ a master clock output จำเป็นสำหรับบาง external audio devices:
•    MCK: Master Clock (mapped แยกต่างหาก) ถูกใช้, เมื่อ the I2S ถูกกำหนดค่าใน master mode (และเมื่อ the MCKOE bit
      ใน the SPI_I2SPR register ถูกเซ็ต), เพื่อเอ๊าพุท additional clock นี้สร้างขึ้นที่ a preconfigured frequency rate เท่ากันกับ
      256 × FS, ที่ FS คือ the audio sampling frequency

The I2S ใช้ clock generator ของตัวเองเพื่อผลิต the communication clock เมื่อมันถูกเซ็ตใน master mode. clock generator เป็น the source ของ the master clock output อีกด้วย. Two additional registers มีให้ใช้ใน I2S mode. รีจีสเตอร์หนึ่งถูกเชื่อมโยงถึง the clock generator configuration SPI_I2SPR และอีกรีจีสเตอร์หนึ่งคือ a generic I2S configuration register
SPI_I2SCFGR (audio standard, slave/master mode, data format, packet frame, clock polarity, etc.).

The SPI_CR1 register และทุก CRC registers ไม่ได้ถูกใช้ใน the I2S mode. ในทำนองเดียวกัน, the SSOE bit ใน the SPI_CR2 register และ the MODF และ CRCERR bits ใน the SPI_SR ไม่ได้ถูกใช้.

The I2S ใช้ the same SPI register สำหรับ data transfer (SPI_DR) ใน 16-bit wide mode.

tha

25.4.2 Supported audio protocols

The three-line bus ต้องจัดการเฉพาะ audio data โดยทั่วไป time-multiplexed บน two channels: the right channel และ the left channel. อย่างไรก็ตามมีเพียง one 16-bit register สำหรับ the transmission และ the reception. ดังนั้น, มันขึ้นอยู่กับ the software ที่เขียนลงใน the data register ค่าที่เพียงพอที่ตรงกันกับ the considered channel side, หรือเพื่ออ่าน the data จาก the data register และเพื่อชี้ตัว the corresponding channel โดยการเช็ค the CHSIDE bit ใน the SPI_SR register. Channel Left ถูกส่งเป็นอันดับแรกตามด้วย the channel right (CHSIDE ไม่มีความหมายสำหรับ the PCM protocol).

Four data และ packet frames มีให้ใช้งาน. Data อาจถูกส่งด้วยรูปแบบของ:
•    16-bit data บรรจุใน 16-bit frame
•    16-bit data packed in 32-bit frame
•    24-bit data packed in 32-bit frame
•    32-bit data packed in 32-bit frame

เมื่อใช้ 16-bit data ขยายบน 32-bit packet, the first 16 bits (MSB) เป็น the significant bits, the 16-bit LSB ถูกบังคับเป็น 0 โดยปราศจากความจำเป็นใดๆสำหรับ software action หรือ DMA request (เฉพาะ one read/write operation).

The 24-bit และ 32-bit data frames ต้องการ two CPU read หรือ write operations ไปยัง/จาก the SPI_DR หรือ two DMA operations ถ้า the DMA ถูกชอบสำหรับ the application. สำหรับ 24-bit data frame โดยเฉพาะ, the 8 nonsignificant bits ถูกขยายเป็น 32 bits ด้วย 0-bits (โดย hardware).

สำหรับทุก data formats และ communication standards, the most significant bit ถูกส่งเป็นอันดับแรกเสมอ (MSB first).

The I2S interface รองรับ four audio standards, กำหนดค่าได้โดยใช้ the I2SSTD[1:0] และ PCMSYNC bits ใน the SPI_I2SCFGR register

tha

I2S Philips standard

สำหรับมาตรฐานนี้, the WS signal ถูกใช้เพื่อแสดงให้เห็นซึ่ง channel กำลังถูกส่ง. มันถูกทำให้ทำงาน one CK clock cycle ก่อน the first bit (MSB) มีให้ใช้งาน.



Data ถูก latched บน the falling edge ของ CK (สำหรับ the transmitter) และถูกอ่านบน the rising edge (สำหรับ the receiver). The WS signal ก็ถูก latched ด้วยบน the falling edge ของ CK.



tha

โหมดนี้ต้องการ two write หรือ read operations ไปยัง/จาก the SPI_DR.
• In transmission mode:
ถ้า 0x8EAA33 ต้องถูกส่ง (24-bit):



• ใน reception mode:
ถ้า data 0x8EAA33 ถูกรับ:



(มีเพียง the 8MSB ถูกส่งเพื่อเทียบเคียงเป็น the 24 bits, 8 LSBs ไม่มีความหมายและสามารถเป็นอะไรก็ได้)