STM32F1 I2C

Started by tha, December 28, 2020, 03:06:11 PM

Previous topic - Next topic

tha

26.3.7 DMA requests

DMA requests (เมื่อเปิดการใช้งาน) ถูกสร้างขึ้นเฉพาะสำหรับ data transfer. DMA requests ถูกสร้างขึ้นโดย Data Register กลายมาเป็นว่างเปล่าใน transmission and Data Register กลายมาเป็นเต็มใน reception. The DMA ต้องถูกเริ่มต้นและเปิดการใช้งานก่อน the I2C data transfer. The DMAEN bit ต้องถูกเซ็ตใน the I2C_CR2 register ก่อน the ADDR event. ใน master mode หรือใน slave mode เมื่อ clock stretching ถูกเปิดการใช้งาน, the DMAEN bit สามารถถูกเซ็ตอีกดวยในระหว่าง the ADDR event, ก่อนการเคลียร์ the ADDR flag. The DMA request ต้องถูกบริการก่อน the end ของ the current byte transfer. เมื่อจำนวนของ data transfers ซึ่งถูกโปรแกรมแล้วสำหรับ the corresponding DMA stream ถูกถึง, the DMA controller ส่ง an End of Transfer EOT signal ไปยัง the I2C interface และสร้าง a Transfer Complete interrupt ถ้าถูกเปิดการใช้งาน:

•    Master transmitter: ใน the interrupt routine หลังจาก the EOT interrupt, ปิดการใช้งาน DMA requests จากนั้นคอย
      สำหรับ a BTF event ก่อนการโปรแกรม the Stop condition.
•    Master receiver: เมื่อจำนวนของ bytes ที่ถูกรับเท่ากับหรือมากกว่า 2 , the DMA controller ส่ง a hardware signal,
      EOT_1, ที่ตรงกันกับลำดับสุดท้ายแต่ one data byte (จำนวนของ bytes – 1). ถ้า, ใน the I2C_CR2 register, the LAST bit
      ถูกเซ็ต, I2C จะส่ง a NACK โดยอัตโนมัติหลังจาก the next byte ตามด้วย EOT_1. The user สามารถสร้าง a Stop condition
      ใน the DMA Transfer Complete interrupt routine ถ้าถูกเปิดการใช้งาน.

tha

Transmission using DMA

DMA mode สามารถถูกเปิดการใช้งานสำหรับ transmission โดยการเซ็ต the DMAEN bit ใน the I2C_CR2 register. Data จะถูกโหลดจาก a Memory area ที่กำหนดค่าโดยใช้ the DMA peripheral (อ้างอิงถึง the DMA specification) ไปยัง the I2C_DR register เมื่อ the TxE bit ถูกเซ็ต. เพื่อแม็ป a DMA stream x สำหรับ I2C transmission (ที่ x คือ the stream number), ทำตามลำดับต่อไปนี้:
1.    เซ็ต the I2C_DR register address ใน the DMA_SxPAR register. The data จะถูกย้ายไปยัง address นี้จาก the
       memory หลังจากแต่ละ TxE event.
2.    เซ็ต the memory address ใน the DMA_SxMA0R register (และใน DMA_SxMA1R register ในกรณีของ a bouble
       buffer mode). The data จะถูกโหลดลงใน I2C_DR จาก memory นี้หลังจากแต่ละ TxE event.
3.    กำหนดค่าจำนวนทั้งหมดของ bytes ที่ถูกส่งถ่ายใน the DMA_SxNDTR register. หลังจากแต่ละ TxE event, ค่านี้จะถูกลดลง.
4.    กำหนดค่า the DMA stream priority โดยใช้ the PL[0:1] bits ใน the DMA_SxCR register
5.    เซ็ต the DIR bit ใน the DMA_SxCR register และกำหนดค่า interrupts หลังจาก half transfer หรือ full transfer ขึ้นอยู่กับ
       application requirements.
6.    ทำให้ the stream ทำงานโดยการเซ็ต the EN bit ใน the DMA_SxCR register.

เมื่อจำนวนของ data transfers ซึ่งถูกโปรแกรมไว้แล้วใน the DMA Controller registers ถูกถึง, the DMA controller ส่ง an End of Transfer EOT/ EOT_1 signal ไปยัง the I2C interface และ the DMA สร้าง an interrupt, ถ้าถูกเปิดการใช้งาน, บน the DMA stream interrupt vector.

Note: ห้ามเปิดการใช้งาน the ITBUFEN bit ใน the I2C_CR2 register ถ้า DMA ถูกใช้สำหรับ transmission

tha

Reception using DMA

DMA mode สามารถถูกเปิดการใช้งานสำหรับ reception โดยการเซ็ต the DMAEN bit ใน the I2C_CR2 register. Data จะถูกโหลดจาก the I2C_DR register ไปยัง a Memory area ที่กำหนดค่าโดยใช้ the DMA peripheral (refer to the DMA specification) เมื่อไรก็ตามที่ a data byte ถูกรับ. เพื่อแม็ป a DMA stream x สำหรับ I2C reception (ที่ x คือ the stream number), ทำตามลำดับต่อไปนี้:
1.    เซ็ต the I2C_DR register address ใน DMA_SxPAR register. The data จะถูกย้ายจาก address นี้ไปยัง the memory
       หลังจากแต่ละ RxNE event.
2.    เซ็ต the memory address ใน the DMA_SxMA0R register (และใน DMA_SxMA1R register ในกรณีของ a bouble
       buffer mode). The data จะถูกโหลดจาก the I2C_DR register ไปยัง memory area นี้หลังจากแต่ละ RxNE event.
3.    กำหนดค่าจำนวนทั้งหมดของ bytes ที่ถูกส่งถ่ายใน the DMA_SxNDTR register. หลังจากแต่ละ RxNE event, ค่านี้จะถูกลดลง.
4.    กำหนดค่า the DMA stream priority โดยใช้ the PL[0:1] bits ใน the DMA_SxCR register
5.    รีเซ็ต the DIR bit  และกำหนดค่า interrupts ใน the DMA_SxCR register หลังจาก half transfer หรือ full transfer ขึ้นอยู่
       กับ application requirements.
6.    ทำให้ the stream ทำงานโดยการเซ็ต the EN bit ใน the DMA_SxCR register.

เมื่อจำนวนของ data transfers ซึ่งถูกโปรแกรมไว้แล้วใน the DMA Controller registers ถูกถึง, the DMA controller ส่ง an End of Transfer EOT/ EOT_1 signal ไปยัง the I2C interface และ the DMA สร้าง an interrupt, ถ้าถูกเปิดการใช้งาน, บน the DMA stream interrupt vector.

Note: ห้ามเปิดการใช้งาน the ITBUFEN bit ใน the I2C_CR2 register ถ้า DMA ถูกใช้สำหรับ transmission


tha

26.3.8 Packet error checking

A PEC calculator ถูกนำมาใช้เพื่อปรับปรุงความน่าเชื่อถือของการสื่อสาร. The PEC ถูกคำนวณโดยใช้ the C(x) = x8 + x2 + x + 1 CRC-8 polynomial อนุกรมบนแต่ละบิต.
•    PEC calculation ถูกเปิดการใช้งานโดยการเซ็ต the ENPEC bit ใน the I2C_CR1 register. PEC คือ a CRC-8 ที่คำนวณบนทุก
      message bytes รวมทั้ง addresses and R/W bits.
      –    ใน transmission: เซ็ต the PEC transfer bit ใน the I2C_CR1 register หลังจาก the TxE event ที่ตรงกันกับ the last
            byte. The PEC จะถูกส่งถ่ายหลังจาก the last transmitted byte
      –    ใน reception: เซ็ต the PEC bit ใน the I2C_CR1 register หลังจาก the RxNE event ที่ตรงกันกับ the last byte ดังนั้น
            the receiver ส่ง a NACK ถ้า the next received byte ไม่เท่ากันกับ the internally calculated PEC. ในกรณีของ
            Master-Receiver, a NACK ต้องตาม the PEC ไม่ว่าผลการตรวจสอบเป็นอย่างไร.The PEC ต้องถูกเซ็ตก่อน the ACK pulse
            of the current byte reception.
•    A PECERR error flag/interrupt มีให้ใช้อีกด้วยใน the I2C_SR1 register.
•    ถ้า DMA and PEC calculation ทั้งคู่ถูกเปิดการใช้งาน:
      –    ใน transmission: เมื่อ the I2C interface ได้รับ an EOT signal จาก the DMA controller, มันจะส่ง a PEC โดยอัตโนมัติ
            หลังจาก the last byte.-
      –    ใน reception: เมื่อ the I2C interface ได้รับ an EOT_1 signal จาก the DMA controller, มันจะพิจารณา the next
            byte โดยอัตโนมัติว่าเป็น a PEC และจะตรวจสอบมัน. A DMA request ถูกสร้างขึ้นหลังจาก PEC reception.
•    เพื่อยอมให้ intermediate PEC transfers, a control bit มีให้ใช้ใน the I2C_CR2 register (LAST bit) เพื่อตัดสินว่าถ้ามันเป็น
      the last DMA transfer จริงๆหรือไม่. ถ้ามันเป็น the last DMA request สำหรับ a master receiver, a NACK จะถูกส่งโดย
      อัตโนมัติหลังจาก the last received byte.
•    PEC calculation ถูกทำให้เสียหายโดย an arbitration loss.

tha

26.4 I2C interrupts

ตารางข้างล่างนี้ให้รายการของ I2C interrupt requests.





Note: SB, ADDR, ADD10, STOPF, BTF, RxNE and TxE ถูกลอจิก OR  บน the same interrupt channel.
           BERR, ARLO, AF, OVR, PECERR, TIMEOUT and SMBALERT ถูกลอจิก OR  บน the same interrupt channel.



tha

26.5 I2C debug mode

เมื่อ the microcontroller เข้าสู่ the debug mode (Cortex®-M3 core ถูกหยุด), the SMBUS timeout อย่างใดอย่างหนึ่งทำงานต่อไปตามปกติหรือหยุด, ขึ้นอยู่กับ the DBG_I2Cx_SMBUS_TIMEOUT configuration bits ใน the DBG module. สำหรับรายละเอียดที่มากขึ้นอ้างอิงถึง Section 31.16.2: Debug support for timers, watchdog, bxCAN and I2C.

tha

26.6 I2C registers

อ้างอิงถึงสำหรับรายการตัวย่อที่ใช้ใน register descriptions.
The peripheral registers ต้องถูกเข้าถึงโดย half-words (16 bits) or words (32 bits).



Bit 15 SWRST: Software reset
เมื่อ set, the I2C อยู่ภายใต้ reset state. ก่อนการรีเซ็ตบิตนี้, ทำให้แน่ใจว่า the I2C lines ถูกปลดปล่อยและ the bus เป็นอิสระ.
     0: I2C Peripheral ไม่อยู่ภายใต้ reset
     1: I2C Peripheral อยู่ภายใต้  reset state
Note: บิตนี้สามารถถูกใช้เพื่อเริ่มต้นใหม่ the peripheral หลังจาก an error หรือ a locked state. ตัวอย่างเช่น, ถ้า the BUSY bit ถูก
           เซ็ตและยังคงถูกล็อคเนืองจากความผิดพลาดบน the bus, the SWRST bit สามารถถูกใช้เพื่อออกจากสภาวะนี้.

Bit 14 Reserved, must be kept at reset value

Bit 13 ALERT: SMBus alert
บิตนี้ถูกเซ็ตและเคลียร์โดย software, และถูกเคลียร์โดย hardware เมื่อ PE=0.
     0: ปลดปล่อย SMBA pin high. Alert Response Address Header ตามด้วย NACK.
     1: ขับ SMBA pin low. Alert Response Address Header ตามด้วย ACK.

Bit 12 PEC: Packet error checking
บิตนี้ถูกเซ็ตและเคลียร์โดย software, และถูกเคลียร์โดย hardware เมื่อ PEC ถูกส่งถ่ายหรือโดย a START หรือ Stop condition หรือเมื่อ PE=0.
     0: ไม่มี PEC transfer
     1: PEC transfer (in Tx or Rx mode)
Note: PEC calculation ถูกทำให้เสียหายโดย an arbitration loss.

tha

Bit 11 POS: Acknowledge/PEC Position (for data reception)
บิตนี้ถูกเซ็ตและเคลียร์โดย software, และถูกเคลียร์โดย hardware เมื่อ PE=0.
     0: ACK bit ควบคุม the (N)ACK ของ the current byte ที่กำลังถูกรับใน the shift register. The PEC bit แสดงให้เห็นว่า
          current byte ใน shift register คือ a PEC.
     1: ACK bit ควบคุม the (N)ACK ของ the next byte ซึ่งจะถูกรับใน the shift register. The PEC bit แสดงให้เห็นว่า the next
          byte ใน the shift register คือ a PEC
Note: The POS bit ถูกใช้เมื่อขั้นตอนสำหรับ reception ของ 2 bytes (see Method 2: transfer sequence diagram for
           master receiver when N=2) ถูกทำตาม. มันต้องถูกกำหนดค่าก่อน data reception starts. ในกรณีนี้, เพื่อ NACK the
           2nd byte, the ACK bit ต้องถูกเคลียร์เพียงแค่หลังจาก ADDR ถูกเคลียร์. เพื่อตรวจสอบ the 2nd byte เป็น PEC, the PEC
           bit ต้องถูกเซ็ตในระหว่าง the ADDR stretch event หลังจากการกำหนดค่า the POS bit.