STM32F1 I2C

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

Previous topic - Next topic

tha

SCL master clock generation

The CCR bits ถูกใช้เพื่อสร้าง the high and low level of the SCL clock, สตาร์ทจากการสร้างของ the rising and falling edge (ตามลำดับ). ดังที่ a slave อาจขึง the SCL line, the peripheral เช็ค the SCL input จาก the bus ที่ the end ของ the time ที่โปรแกรมใน TRISE bits หลังจาก rising edge generation

•    ถ้า the SCL line เป็น low, มันหมายความว่า a slave กำลังขึง the bus, และ the high level counter จะหยุดจนกระทั่ง the SCL
      line ถูกตรวจพบว่าเป็น high. นี้ยอมให้เพื่อรับประกัน the minimum HIGH period ของ the SCL clock parameter.
•    ถ้า the SCL line เป็น high, the high level counter จะนับต่อไป.

โดยแท้จริงแล้ว, the feedback loop จาก the SCL rising edge generation โดย the peripheral ไปยัง the SCL rising edge detection โดย the peripheral ใช้เวลาถึงแม้ว่าไม่มี slave ขึง the clock. loopback duration นี้เชื่อมโยงถึง the SCL rising time (ส่งผลกระทบ SCL VIH input detection), บวก delay เนื่องจาก the noise filter ที่ปรากฎบน the SCL input path, บวก delay เนื่องจาก internal SCL input synchronization กับ APB clock. The maximum time ที่ใช้โดย the feedback loop ถูกโปรแกรมใน the TRISE bits, ดังนั้น the SCL frequency ยังคง stable ไม่ว่า the SCL rising time ก็ตาม.

tha

Start condition

การเซ็ต the START bit เป็นเหตุให้ the interface สร้าง a Start condition และสวิทช์ไปเป็น Master mode (MSL bit set) เมื่อ the BUSY bit ถูก cleared.

Note: In master mode, การเซ็ต the START bit เป็นเหตุให้ the interface สร้าง a ReStart condition ที่ the end ของ the current byte transfer.

เมื่อ the Start condition ถูกส่ง:
• The SB bit ถูกเซ็ตโดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN bit ถูก set.

จากนั้น the master คอยสำหรับการอ่านของ the SR1 register ตามด้วยการเขียนใน the DR register ด้วย the Slave address (ดู Figure 273 and Figure 274 Transfer sequencing EV5).

tha

Slave address transmission

จากนั้น the slave address ถูกส่งไปยัง the SDA line โดยทาง the internal shift register.
•    ใน 10-bit addressing mode, การส่ง the header sequence เป็นเหตุให้เกิดเหตุการณ์ต่อไปนี้:
      –    The ADD10 bit ถูกเซ็ตโดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN bit ถูกเซ็ต.
      จากนั้น the master จะคอยสำหรับการอ่านของ the SR1 register ตามด้วยการเขียนใน the DR register ด้วย the second
      address byte (ดู Figure 273 and Figure 274 Transfer sequencing).
      –    The ADDR bit  ถูกเซ็ตโดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN bit ถูกเซ็ต. จากนั้น the master
      จะคอยสำหรับการอ่านของ the SR1 register ตามด้วยการอ่านของ the SR2 register (ดู Figure 273 and Figure 274
      Transfer sequencing).
•    In 7-bit addressing mode, one address byte ถูกส่ง.
      ทันทีที่ the address byte ถูกส่ง,
      –    The ADDR bit  ถูกเซ็ตโดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN bit ถูกเซ็ต.
      จากนั้น the master จะคอยสำหรับการอ่านของ the SR1 register ตามด้วยการอ่านของ the SR2 register (ดู Figure 273 and
      Figure 274 Transfer sequencing).

The master สามารถตัดสินใจเพื่อเข้าสู่ Transmitter or Receiver mode ขึ้นอยู่กับ the LSB of the slave address ที่ส่ง.
•    In 7-bit addressing mode,
      –    เพื่อเข้าสู่ Transmitter mode, a master ส่ง the slave address พร้อมด้วย LSB reset.
      –    เพื่อเข้าสู่ Receiver mode, a master ส่ง the slave address พร้อมด้วย LSB set.
•    In 10-bit addressing mode,
      –    เพื่อเข้าสู่ Transmitter mode, a master ส่ง the header (11110xx0) และจากนั้น the slave address, (ที่ xx หมายว่า
            the two most significant bits of the address).
      –    เพื่อเข้าสู่ Receiver mode, a master ส่ง the header (11110xx0) และจากนั้น the slave address. จากนั้นมันควรส่ง a
            repeated Start condition ตามด้วย the header (11110xx1), (ที่ xx หมายว่า the two most significant bits of the
            address)

      The TRA bit แสดงให้เห็นว่า the master อยู่ใน Receiver หรือ Transmitter mode


moneyp1000

เป็นประโยชน์มากเลยครับพี่ ติดตามอ่านอยู่นะครับ

tha

Master transmitter

หลังจาก the address transmission และหลังจากการเคลียร์ ADDR, the master ส่ง bytes จาก the DR register ไปยัง the SDA line โดยทาง the internal shift register.

The master คอยจนกระทั่ง the first data byte ถูกเขียนลงใน I2C_DR (ดู Figure 273 Transfer sequencing EV8_1).

เมื่อ the acknowledge pulse ถูกได้รับ, the TxE bit ถูกเซ็ตโดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN และ ITBUFEN bits ถูกเซ็ต.

ถ้า TxE ถูกเซ็ตและ a data byte ไม่ถูกเขียนใน the DR register ก่อน the end of the last data transmission, BTF ถูกเซ็ตและ the interface คอยจนกระทั่ง BTF ถูกเคลียร์โดยการอ่านจาก I2C_SR1 ตามด้วยการเขียนไปยัง I2C_DR, ขึง SCL low.

tha

Closing the communication

หลังจาก the last byte ถูกเขียนไปยัง the DR register, the STOP bit ถูกเซ็ตโดย software เพื่อสร้าง a Stop condition (ดู Figure 273 Transfer sequencing EV8_2). The interface กลับไปยัง slave mode โดยอัตโนมัติ (MSL bit ถูกเคลียร์).

Note: Stop condition ควรถูกโปรแกรมในระหว่าง EV8_2 event, เมื่ออย่างใดอย่างหนึ่ง TxE หรือ BTF ถูก set.



Notes: 1- The EV5, EV6, EV9, EV8_1 and EV8_2 events ขึง SCL low จนกระทั่ง the end of the corresponding
               software sequence.
           2- The EV8 software sequence ต้องเสร็จสมบูรณ์ the end of the current byte transfer. ในกรณี EV8 software
               sequence ไม่สามารถถูกจัดการก่อน the current byte end of transfer, ขอแนะนำให้ใช้ BTF แทน TXE ที่มีข้อเสียของ
               การช้าลง the communication.

tha

Master receiver

หลังจาก the address transmission และหลังจากการเคลียร์ ADDR, the I2C interface เข้าสู่ Master Receiver mode. ในโหมดนี้ the interface รับ bytes จาก the SDA line ลงใน the DR register โดย the internal shift register. หลังจากแต่ละ byte the interface สร้างในลำดับ:
1.    An acknowledge pulse ถ้า the ACK bit ถูก set
2.    The RxNE bit ถูก set และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN and ITBUFEN bits ถูก set (see Figure 274
       Transfer sequencing EV7).

ถ้า RxNE ถูกเซ็ตและ a data byte ใน the DR register ไม่ถูกอ่านก่อน the end of the last data reception, BTF ถูกเซ็ตโดย hardware และ the interface คอยจนกระทั่ง BTF ถูกเคลียร์โดยการอ่านจาก I2C_SR1 register ตามด้วยการอ่านใน I2C_DR register, ขึง SCL low.

tha

Closing the communication

Method 1: วิธีนี้สำหรับกรณีเมื่อ the I2C ถูกใช้พร้อมด้วย interrupts ที่มี the highest priority ใน the application

The master ส่ง a NACK สำหรับ the last byte ที่รับจาก the slave. หลังจากการรับ NACK นี้, the slave ปลดปล่อย the control of the SCL and SDA lines. จากนั้น the master สามารถส่ง a Stop/Restart condition
1.    เพื่อสร้าง the nonacknowledge pulse หลังจาก the last received data byte, the ACK bit ต้องถูกเคลียร์เพียงแค่หลังจาก
       การอ่าน the second last data byte (หลังจากที่สองก่อนสุดท้าย RxNE
       event).
2.    เพื่อสร้าง the Stop/Restart condition, software ต้องเซ็ต the STOP/START bit เพียงแค่หลังจากการอ่าน the second last
       data byte (after the second last RxNE event).
3.    ในกรณี a single byte ต้องถูกรับ, the Acknowledge ปิดการใช้งานและ the Stop condition generation ถูกทำเพียงแค่หลัง
       จาก EV6 (ใน EV6_1, เพียงแค่หลังจาก ADDR ถูก cleared).

หลังจาก the Stop condition generation, the interface กลับไปยัง slave mode โดยอัตโนมัติ (MSL bit cleared).


1.    ถ้า a single byte ถูกรับ, มันคือ NA.
2.    The EV5, EV6 and EV9 events ขึง SCL low จนกระทั่ง the end of the corresponding software sequence.
3.    The EV7 software sequence ต้องเสร็จสมบูรณ์ก่อน the end of the current byte transfer. ในกรณี EV7 software
       sequence ไม่สามารถถูกจัดการก่อน the current byte end of transfer, ขอแนะนำให้ใช้ BTF แทน RXNE ที่มีข้อเสียของการช้า
       ลง the communication.
4.    The EV6_1 or EV7_1 software sequence ต้องเสร็จสมบูรณ์ก่อน the ACK pulse of the current byte transfer.