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)