STM32F1 SPI

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

Previous topic - Next topic

tha

Transmission sequence

The transmission sequence เริ่มเมื่อ a half-word ถูกเขียนลงใน the Tx buffer.

สันนิษฐานว่า, the first data ถูกเขียนลงใน the Tx buffer ตรงกันกับ the channel Left data. เมื่อ data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register, TXE ถูกเซ็ตและ data ที่ตรงกันกับ the channel Right ต้องถูกเขียนลงใน the Tx buffer. The CHSIDE flag แสดงให้เห็นซึ่ง channel ที่ถูกส่ง. มันมีความหมายเมื่อ the TXE flag ถูกเซ็ต เพราะ the CHSIDE flag ถูกอัปเดตเมื่อ TXE ไปเป็น high.

A full frame ต้องถูกพิจารณาเป็น a Left channel data transmission ติดตามโดย a Right channel data transmission. มันเป็นไปไม่ได้ที่จะมี a partial frame ที่ the left channel เท่านั้นที่ถูกส่ง.

The data half-word ถูกโหลดแบบขนานลงใน the 16-bit shift register ในระหว่าง the first bit transmission, และจากนั้นถูกเลื่อนออก, แบบอนุกรม, ไปยัง the MOSI/SD pin, MSB first. The TXE flag ถูกเซ็ตหลังจากแต่ละ transfer จาก the Tx buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit in the SPI_CR2 register ถูกเซ็ต.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the write operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2).

เพื่อให้แน่ใจว่า a continuous audio data transmission, มันมีข้อบังคับให้เขียน the SPI_DR ด้วย the next data ที่จะส่งก่อน the end ของ the current transmission.

เพื่อ switch off the I2S, โดยการเคลียร์ I2SE, มันมีข้อบังคับให้คอยสำหรับ TXE = 1 and BSY = 0.

tha

Reception sequence

โหมดการทำงานก็เหมือนกันกับสำหรับ the transmission mode ยกเว้นสำหรับข้อ 3 (อ้างอิงถึง the procedure ที่อธิบายใน Section 25.4.4), ที่ the configuration ควรถูกเซ็ต the master reception mode ผ่านทาง the I2SCFG[1:0] bits.

อย่างใดก็ตาม the data หรือ channel length, the audio data ถูกรับโดย 16-bit packets. นี้หมายความว่าแต่ละเวลาที่ the Rx buffer เต็ม, the RXNE flag ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน SPI_CR2 register. ขึ้นอยู่กับ the data และ channel length configuration, the audio value ที่รับสำหรับ a right หรือ left channel อาจเป็นผลจากหนึ่งหรือสอง
receptions ลงใน the Rx buffer.

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

CHSIDE ถูกอัปเดตหลังจากแต่ละ reception. มันมีความไวต่อ the WS signal ที่สร้างขึ้นโดย the I2S cell.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the read operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

ถ้า data ถูกรับในขณะที่ดาต้าที่รับก่อนหน้ายังไม่ได้ถูกอ่าน, an overrun ถูกสร้างขึ้นและ the OVR flag ถูกเซ็ต. ถ้า the ERRIE bit ถูกเซ็ตใน the SPI_CR2 register, an interrupt ถูกสร้างเพื่อแสดง the error.

เพื่อปิด the I2S, การกระทำเฉพาะถูกต้องการเพื่อให้แน่ใจว่า the I2S เสร็จสมบูรณ์ the transfer cycle อย่างเหมาะสมโดยปราศจากการเริ่มต้น a new data transfer. The sequence ขึ้นอยู่กับ the configuration ของ the data and channel lengths, และบน the audio protocol mode ที่เลือก. ในกรณีของ:
•    16-bit data length ต่อขยายบน 32-bit channel length (DATLEN = 00 and CHLEN = 1) โดยใช้ the LSB justified
      mode (I2SSTD = 10)
      a)    คอยสำหรับลำดับที่สองก่อนถึงสุดท้าย RXNE = 1 (n – 1)
      b)    จากนั้นคอย 17 I2S clock cycles (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)
•    16-bit data length ต่อขยายบน 32-bit channel length (DATLEN = 00 and CHLEN = 1) in MSB justified, I2S or
      PCM modes (I2SSTD = 00, I2SSTD = 01 or I2SSTD = 11,ตามลำดับ)
      a)    คอยสำหรับ the last RXNE
      b)    จากนั้นคอย 1 I2S clock cycle (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)
•    สำหรับชุดการผสมอื่น ๆทั้งหมดของ DATLEN and CHLEN, อย่างใดก็ตาม the audio mode ที่เลือกผ่านทาง the I2SSTD bits,
      ดำเนินการตามลำดับต่อไปนี้เพื่อปิด the I2S:
      a)    คอยสำหรับลำดับที่สองก่อนถึงสุดท้าย RXNE = 1 (n – 1)
      b)    จากนั้นคอย one I2S clock cycle (โดยใช้ a software loop)
      c)    ปิดการใช้งาน the I2S (I2SE = 0)

Note: The BSY flag ต้องเป็น low ในระหว่าง transfers.

tha

25.4.5 I2S slave mode

ใน slave mode, the I2S สามารถถูกกำหนดค่าใน transmission or reception mode.The operating mode หลักๆจะทำตามกฎเดียวกันดังอธิบายสำหรับ the I2S master configuration. ใน slave mode, ไม่มี clock ที่ถูกสร้างขึ้นโดย the I2S interface. The clock and WS signals ถูกอินพุทจาก the external master ที่ต่อถึง the I2S interface. ไม่มีความจำเป็น, สำหรับผู้ใช้, ที่จะกำหนดค่า the clock.

The configuration steps ทำตามรายการข้างล่าง:
1.    เซ็ต the I2SMOD bit in the SPI_I2SCFGR register ให้ถึง the I2S functionalities และเลือก the I2S standard ผ่านทาง
       the I2SSTD[1:0] bits, the data length ผ่านทาง the DATLEN[1:0] bits และจำนวนของบิตต่อ channel สำหรับ the
       frame โดยการกำหนดค่า the CHLEN bit. เลือก the mode (transmission or reception) อีกด้วยสำหรับ the slave ผ่านทาง
       the I2SCFG[1:0] bits in SPI_I2SCFGR register.
2.    ถ้าจำเป็น, เลือกทุก the potential interruption sources และ the DMA capabilities โดยการเขียน the SPI_CR2 register.
3.    The I2SE bit in SPI_I2SCFGR register ต้องถูกเซ็ต.


tha

Transmission sequence

The transmission sequence เริ่มเมื่อ the external master device ส่ง the clock และเมื่อ the NSS_WS signal ร้องขอ the transfer ของ data. The slave ต้องถูก enabled ก่อน the external master สตาร์ท the communication. The I2S data register ต้องถูกโหลดก่อน the master เริ่มต้น the communication.

สำหรับ the I2S, MSB justified and LSB justified modes, the first data item ที่ถูกเขียนลงใน the data register ตรงกันกับ the data สำหรับ the left channel. เมื่อ the communication สตาร์ท, the data ถูกส่งถ่ายจาก the Tx buffer ไปยัง the shift register. The TXE flag จากนั้นถูกเซ็ตเพื่อร้องขอ the right channel data ให้ถูกเขียนลงใน the I2S data register.

The CHSIDE flag แสดงให้เห็นซึ่ง channel ที่ถูกส่ง. เปรียบเทียบกับ the master transmission mode, ใน slave mode, CHSIDE มีความไวต่อ the WS signal ที่มาจาก the external master. นี้หมายความว่า the slave จำเป็นต้องพร้อมส่ง the first data ก่อน the clock ถูกสร้างขึ้นโดย the master. WS assertion ตรงกันกับ left channel transmitted first.

Note: The I2SE ต้องถูกเขียนอย่างน้อย two PCLK cycles ก่อน the first clock of the master มาบน the CK line.

The data half-word ถูก parallel-loaded ลงใน the 16-bit shift register (จาก the internal bus) ในระหว่าง the first bit transmission, และจากนั้น shifted out แบบอนุกรมไปยัง the MOSI/SD pin MSB first. The TXE flag ถูกเซ็ตหลังจากแต่ละ transfer จาก the Tx buffer ไปยัง the shift register และ an interrupt ถูกสร้างขึ้นถ้า the TXEIE bit ใน the SPI_CR2 register ถูกเซ็ต.

จำไว้ว่า the TXE flag ควรถูกเช็คว่าเป็น 1 ก่อนการพยายามที่จะเขียน the Tx buffer.

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the write operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

เพื่อความปลอดภัย a continuous audio data transmission, มันมีข้อบังคับให้เขียน the SPI_DR register ด้วย the next data ที่จะส่งก่อน the end of the current transmission. An underrun flag ถูเซ็ตและ an interrupt อาจถูกสร้างขึ้นถ้า the data ไม่ถูกเขียนลงใน the SPI_DR register ก่อน the first clock edge of the next data communication. นี้แสดงให้เห็นไปยัง the software ว่า the transferred data ผิดพลาด. ถ้า the ERRIE bit ถูกเซ็ตลงใน the SPI_CR2 register, an interrupt ถูกสร้างขึ้นเมื่อ the UDR flag in the SPI_SR register ไปเป็น high. ในกรณีนี้, มันมีข้อบังคับให้ปิด the I2S และให้รีสตาร์ท a data transfer โดยสตาร์ทจาก the left channel.

เพื่อปิด the I2S, โดยการเคลียร์ the I2SE bit, มันมีข้อบังคับให้คอยสำหรับ TXE = 1 and BSY = 0.

tha

Reception sequence.

โหมดการทำงานก็เหมือนกันกับสำหรับ the transmission mode ยกเว้นสำหรับข้อ 1 (อ้างอิงถึง the procedure ที่อธิบายใน Section 25.4.5), ที่ the configuration ควรเซ็ต the master reception mode ผโดยใช้ the I2SCFG[1:0] bits in the SPI_I2SCFGR register.

อย่างใดก็ตาม the data หรือ channel length, the audio data ถูกรับโดย 16-bit packets. นี้หมายความว่าแต่ละเวลาที่ the Rx buffer เต็ม, the RXNE flag ใน the SPI_SR register ถูกเซ็ตและ an interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูกเซ็ตใน SPI_CR2 register. ขึ้นอยู่กับ the data และ channel length configuration, the audio value ที่รับสำหรับ a right หรือ left channel อาจเป็นผลจากหนึ่งหรือสอง receptions ลงใน the Rx buffer.

CHSIDE ถูกอัปเดตหลังจากแต่ละ data ถูกรับที่อ่านมาจาก SPI_DR. มันมีความไวต่อ the external WS line จัดการโดย the external master component

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

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ the read operations ขึ้นอยู่กับ the I2S standard mode ที่เลือก, อ้างอิงถึง Section 25.4.2.

ถ้า data ถูกรับในขณะที่ดาต้าที่รับก่อนหน้านี้ยังไม่ได้ถูกอ่าน, an overrun ถูกสร้างขึ้นและ the OVR flag ถูกเซ็ต. ถ้า the ERRIE bit ถูกเซ็ตใน the SPI_CR2 register, an interrupt ถูกสร้างเพื่อแสดง the error.

เพื่อปิด the I2S in reception mode, I2SE ต้องถูกเคลียร์ทันทีหลังจากการรับ the last RXNE = 1.

Note: The external master components ควรมีความสามารถของ sending/receiving data in 16-bit or 32-bit packets ผ่าน
          an audio channel.

tha

25.4.6 Status flags

Three status flags ถูกจัดให้มีสำหรับ the application เพื่อการเฝ้ามองอย่างเต็มที่ the state of the I2S bus.

Busy flag (BSY)

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

เมื่อ BSY ถูกเซ็ต, มันแสดงให้เห็นว่า the I2S กำลังยุ่งอยู่กับการสื่อสาร. มีข้อยกเว้นอันหนึ่งใน master receive mode (I2SCFG = 11) ที่ the BSY flag เป็น low ในระหว่าง reception.

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

The BSY flag ถูกเซ็ตเมื่อ a transfer สตาร์ท, ยกเว้นเมื่อ the I2S is in master receiver mode..

The BSY flag ถูกเคลียร์:
•    เมื่อ a transfer เสร็จ (ยกเว้นใน master mode, ซึ่ง the communication ถูกนึกว่ามีความต่อเนื่อง)
•    เมื่อ the SPI ถูกปิดการใช้งาน

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

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

tha

Tx buffer empty flag (TXE)

เมื่อเซ็ต, flag นี้แสดงให้เห็นว่า the Tx buffer ว่างอยู่และ the next data ที่จะถูกส่งสามารถถูกโหลดลงใน the buffer. The TXE flag ถูกรีเซ็ตเมื่อ the Tx buffer บรรจุดาต้าพร้อมแล้วที่จะถูกส่ง. มันถูกรีเซ็ตเมื่อ the I2S ถูก disabled อีกด้วย (I2SE bit ถูก reset).

Rx buffer not empty (RXNE)

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

tha

Channel Side flag (CHSIDE)

ใน transmission mode, flag นี้ถูก refreshed เมื่อ TXE ไปเป็น high. มันแสดงให้เห็น the channel side ที่ the data จะส่งถ่ายบน SD ต้องเป็นของ. ในกรณีของ an underrun error event ใน slave transmission mode, flag ไม่น่าเชื่อถือและ I2S จำเป็นต้องถูก switched off และ switched on ก่อนเริ่มต้นใหม่ the communication.

ใน reception mode, flag นี้ถูก refreshed เมื่อ data ถูกรับลงใน SPI_DR. มันแสดงให้เห็นจากที่ channel side data ถูกรับแล้ว. จำไว้ว่าในกรณ๊ของ (เช่น OVR) flag นี้จะไม่มีความหมายและ the I2S ควรถูก reset โดยการปิดการใช้งานและจากนั้นเปิดการใช้งานมัน (พร้อมด้วยการกำหนดค่าถ้าจำเป็นต้องเปลี่ยน).

flag นี้ไม่มีความหมายใน the PCM standard (สำหรับทั้ง Short and Long frame modes).

เมื่อ the OVR or UDR flag in the SPI_SR ถูกเซ็ตและ the ERRIE bit in SPI_CR2 ถูกเซ็ตด้วย, an interrupt ถูกสร้างขึ้น. interrupt สามารถถูกเคลียร์โดยการอ่าน the SPI_SR status register (เมื่อ the interrupt source ถูกเคลียร์แล้ว).