STM32F1 I2C

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

Previous topic - Next topic

tha

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

ด้วยวิธีนี้, DataN_2 ไม่ถูกอ่าน, ดังนั้นหลังจาก DataN_1, the communication ถูกขึง (ทั้ง RxNE and BTF ถูก set). จากนั้น, เคลียร์ the ACK bit ก่อนการอ่าน DataN-2 ใน DR เพื่อให้แน่ใจว่ามันถูกเคลียร์ก่อน the DataN Acknowledge pulse. หลังจากนั้น, เพียงแค่หลังจากการอ่าน DataN_2, เซ็ต the STOP/ START bit และอ่าน DataN_1. หลังจาก RxNE ถูก set, อ่าน DataN. นี้ถูกแสดงข้างล่างนี้



เมื่อ 3 bytes ยังเหลืออยู่ให้ถูกอ่าน:
•    RxNE = 1 => ไม่มีอะไร (DataN-2 ไม่ถูกอ่าน).
•    DataN-1 ถูกรับ
•    BTF = 1 เพราะทั้ง shift and data registers เต็ม: DataN-2 ใน DR และ DataN-1 ใน the shift register => SCL ถูกดึง
      low: ไม่มี data อื่นที่จะถูกรับบน the bus.
•    เคลียร์ ACK bit
•    อ่าน DataN-2 in DR => นี้จะเริ่ม the DataN reception ใน the shift register
•    DataN ถูกรับ (พร้อมด้วย a NACK)
•    โปรแกรม START/STOP
•    อ่าน DataN-1
•    RxNE = 1
•    อ่าน DataN

tha

ขั้นตอนที่อธิบายข้างบนใช้ได้สำหรับ N>2. กรณีที่ a single byte หรือ two bytes ถูกรับควรถูกรับมือต่างออกไป, ดังอธิบายข้างล่าง:

•    กรณีของ a single byte ถูกรับ:
      –    ใน the ADDR event, เคลียร์ the ACK bit.
      –    เคลียร์ ADDR
      –    โปรแกรม the STOP/START bit.
      –    อ่าน the data หลังจาก the RxNE flag ถูก set.

•    กรณีของ two bytes ถูกรับ:
      –    เซ็ต POS และ ACK
      –    คอยสำหรับ the ADDR flag ถูกเซ็ต
      –    เคลียร์ ADDR
      –    เคลียร์ ACK
      –    คอยสำหรับ BTF ถูกเซ็ต
      –    โปรแกรม STOP
      –    อ่าน DR สองครั้ง




tha

26.3.4 Error conditions

ต่อไปนี้เป็น the error conditions ซึ่งอาจเป็นเหตุให้ communication ล้มเหลว.

Bus error (BERR)

error นี้เกิดขึ้นเมื่อ the I2C interface ตรวจพบ an external Stop or Start condition ในระหว่าง an address or a data transfer. ในกรณีนี้:
•    the BERR bit ถูก set และ an interrupt ถูกสร้างขึ้นถ้า the ITERREN bit ถูก set
•    ใน Slave mode: data ถูกละทิ้งและ the lines ถูกปลดปล่อยโดย hardware:
      –    ในกรณีของ a misplaced Start, the slave พิจารณาว่ามันคือ a restart และคอยสำหรับ an address, หรือ a Stop
            condition
      –    ในกรณีของ a misplaced Stop, the slave จะกระทำเหมือน a Stop condition และ the lines ถูกปลดปล่อยโดย
            hardware
•    ใน Master mode: the lines ไม่ถูกปลดปล่อยและสถานะของ the current transmission ไม่ถูกกระทบ. มันขึ้นอยู่กับ the
      software ที่จะทำแท้ง the current transmission หรือไม่

Acknowledge failure (AF)

error นี้เกิดขึ้นเมื่อ the interface ตรวจพบ a nonacknowledge bit. ในกรณีนี้:
•    the AF bit ถูก set และ an interrupt ถูกสร้างขึ้นถ้า the ITERREN bit ถูก set
•    a transmitter ซึ่งรับ a NACK ต้องรีเซ็ต the communication:
      –    ถ้า Slave: lines ถูกปลดปล่อยโดย hardware
      –    ถ้า Master: a Stop or repeated Start condition ต้องถูกสร้างขึ้นโดย software

Arbitration lost (ARLO)

error นี้เกิดขึ้นเมื่อ the I2C interface ตรวจพบ an arbitration lost condition. ในกรณีนี้:
•    the ARLO bit ถูก set โดย hardware (และ an interrupt ถูกสร้างขึ้นถ้า the ITERREN bit ถูก set)
•    the I2C Interface กลับไปสู่ slave mode โดยอัตโนมัติ (the MSL bit ถูกเคลียร์). เมื่อ the I2C สูญเสีย the arbitration, มันไม่
      สามารถ acknowledge  slave address ของมันใน the same transfer, แต่มันสามารถ acknowledge มันหลังจาก a repeated
      Start จาก the winning master
•    lines ถูกปลดปล่อยโดย hardware

Overrun/underrun error (OVR)

An overrun error สามารถเกิดขึ้นใน slave mode เมื่อ clock stretching ถูกปิดการใช้งานและ the I2C interface กำลังรับ data. The interface รับ a byte แล้ว (RxNE=1) และ the data ใน DR ยังไม่ถูกอ่าน, ก่อน the next byte ถูกรับโดย the interface. ในกรณีนี้,
•    The last received byte จะสูญหาย.
•    ในกรณีของ Overrun error, software ควรเคลียร์ the RxNE bit และ the transmitter ควรส่งซ้ำ the last received byte.

Underrun error สามารถเกิดขึ้นใน slave mode เมื่อ clock stretching ถูกปิดการใช้งานและ the I2C interface กำลังส่ง data. The interface ไม่อัปเดต the DR ด้วย the next byte (TxE=1), ก่อน the clock มาสำหรับ the next byte. ในกรณีนี้,
•    The same byte ใน the DR register จะถูกส่งอีกครั้ง
•    The user ควรทำให้แน่ใจว่า data ที่รับบน the receiver side ในระหว่าง an underrun error ถูกละทิ้งและ the next bytes ถูก
      เขียนภายใน the clock low time ที่ระบุใน the I2C bus standard.

สำหรับ the first byte ที่ถูกส่ง, the DR ต้องถูกเขียนหลังจาก ADDR ถูกเคลียร์และก่อน the first SCL rising edge. ถ้าเป็นไปไม่ได้, the receiver ต้องละทิ้ง the first data.


tha

26.3.5 SDA/SCL line control

•    ถ้า clock stretching ถูก enabled:
      –    Transmitter mode: ถ้า TxE=1 and BTF=1: the interface ยึด the clock line low ก่อน transmission เพื่อคอย
            สำหรับ the microcontroller อ่าน SR1 และจากนั้นเขียน the byte ใน the Data Register (ทั้ง buffer and shift
            register ว่างเปล่า).
      –    Receiver mode: ถ้า RxNE=1 and BTF=1: the interface ยึด the clock line low หลังจาก reception เพื่อคอยสำหรับ
            the microcontroller อ่าน SR1 และจากนั้นอ่าน the byte ใน the Data Register (ทั้ง buffer and shift register เต็ม).
•    ถ้า clock stretching ถูก disabled in Slave mode:
      –    Overrun Error ในกรณีของ RxNE=1 และไม่มีการอ่านของ DR ถูกทำก่อน the next byte ถูกรับ. The last received byte
            จะสูญหาย.
      –    Underrun Error ในกรณี TxE=1 และไม่มีการเขียนลงใน DR ถูกทำก่อน the next byte ต้องถูกส่ง. The same byte จะถูก
            ส่งอีกครั้ง.
      –    Write Collision ไม่ถูกจัดการ.

tha

26.3.6 SMBus

Introduction

The System Management Bus (SMBus) คือ a two-wire interface ซึ่งมีหลากหลาย devices สามารถสื่อสารซึ่งกันและกันและกับส่วนที่เหลือของระบบ. มันอยู่บนฐานของ I2C principles of operation. SMBus จัดให้มี a control bus สำหรับ system and power management งานที่เกี่ยวข้อง. A system อาจใช้ SMBus ส่งผ่าน messages ถึงและจาก devices แทนการสลับ control lines แต่ละราย.

The System Management Bus Specification อ้างอิงถึงสามชนิดของ devices. A slave คือ a device ที่กำลังรับหรือกำลังตอบสนองต่อ a command. A master คือ a device ที่ปล่อยออก commands, สร้าง the clocks, และยุติ the transfer. A host คือ a specialized master ที่จัดให้มี the main อินเตอร์เฟสไปยัง the system's CPU. A host ต้องเป็น a master-slave และต้องรองรับ
the SMBus host notify protocol. มีเพียง one host เท่านั้นที่ยอมให้ใน a system.

ความคล้ายคลึงกันระหว่าง SMBus and I2C

•    2-wire bus protocol (1 Clk, 1 Data) + SMBus Alert line ให้เลือกได้
•    Master-slave communication, Master จัดให้มี clock
•    Multi master capability
•    SMBus data format คล้ายกันกับ I2C 7-bit addressing format (Figure 269).

ความแตกต่างระหว่าง SMBus and I2C

ตารางต่อไปนี้อธิบายความแตกต่างระหว่าง SMBus and I2C.


tha

SMBus application usage

ด้วย System Management Bus, a device สามารถจัดให้มี manufacturer information, บอก the system ว่า model/part number ของมันคือ, บันทึกสถานะของมันสำหรับ a suspend event, รายงานชนิดต่างๆของ errors, ยอมรับ control parameters, และคืนกลับ status ของมัน. SMBus จัดให้มี a control bus สำหรับ system and power management งานที่เกี่ยวข้อง.

Device identification

device ใดๆที่มีอยู่บน the System Management Bus ที่เป็น a slave จะมี address เฉพาะที่เรียกว่า the Slave Address. สำหรับรายการของ slave addresses ที่สงวนไว้, อ้างอิงถึง the SMBus specification version. 2.0 (http://smbus.org/).

Bus protocols

The SMBus specification รองรับได้มากถึง nine bus protocols. สำหรับรายละเอียดที่มากขึ้นของ protocols เหล่านี้และ SMBus address types, อ้างอิงถึง SMBus specification version. 2.0. protocols เหล่านี้ควรถูกทำให้เป็นผลโดย the user software

Address resolution protocol (ARP)

SMBus slave address conflicts สามารถถูกแก้ไขโดยการกำหนดแบบไดนามิค a new unique address ไปยังแต่ละ slave device. The Address Resolution Protocol (ARP) มีคุณลักษณะต่อไปนี้:
•    Address assignment ใช้ the standard SMBus physical layer arbitration mechanism
•    Assigned addresses ยังคงคงที่ในขณะที่ device power ถูกประยุกต์ใช้; address retention ผ่านทาง device power loss ถูก
      ยอมให้ได้อีกด้วย
•    ไม่มี additional SMBus packet overhead เกิดขึ้นหลังจาก address assignment. (กล่าวคือการเข้าถึงที่ตามมาไปยัง assigned
      slave addresses มี the same overhead เหมือนการเข้าถึงไปยัง fixed address devices.)
•    Any SMBus master สามารถแจกแจง the bus

Unique device identifier (UDID)

เพื่อจัดให้มี a mechanism เพื่อแยกแต่ละ device สำหรับความมุ่งหมายของ address assignment, แต่ละ device ต้องใช้ a unique device identifier (UDID).

สำหรับรายละเอียดบน 128-bit UDID และข้อมูลที่มากขึ้นบน ARP, อ้างอิงถึง SMBus specification version 2.0.

tha

SMBus alert mode

SMBus Alert คือ an optional signal พร้อมด้วย an interrupt line สำหรับ devices ที่ต้องการแลกเปลี่ยนความสามารถของมันไปยัง master สำหรับ a pin. SMBA เป็น a wired-AND signal เช่นเดียวกันกับ the SCL and SDA signals เป็น. SMBA ถูกใช้ร่วมกันกับ the SMBus General Call Address. Messages ที่เรียกด้วยด้วย the SMBus มีความยาว two bytes.

A slave-only device สามารถส่งสัญญาน the host ผ่านทาง SMBA ที่มันต้องการจะคุยโดยการเซ็ต ALERT bit ใน I2C_CR1 register. The host ประมวลผล the interrupt และเข้าถึงพร้อมกันทุก SMBA devices ผ่านทาง the Alert Response Address (รู้ในนาม ARA ที่มีค่า 0001100X). Only the device(s) ซึ่งดึง SMBA low จะ acknowledge the Alert Response Address. status นี้ถูกระบุโดยใช้ SMBALERT Status flag ใน I2C_SR1 register. The host ดำเนินการ a modified Receive Byte operation. The 7 bit device address ถูกจัดให้มีโดย the slave transmit device ถูกวางใน the 7 most significant bits of the byte. The eighth bit สามารถเป็นศูนย์หรือหนึ่ง.

ถ้ามากกว่าหนึ่ง device ดึง SMBA low, the highest priority (lowest address) device จะชนะ communication rights โดยทาง standard arbitration ในระหว่าง the slave address transfer. หลังจาก acknowledging the slave address the device ต้องปลด SMBA pull-down ของมัน. ถ้า the host ยังคงดู SMBA low เมื่อ the message transfer เสร็จสมบูรณ์, มันรู้ที่จะอ่าน the ARA อีกครั้ง

A host ซึ่งไม่ได้ใช้ the SMBA signal อาจเข้าถึง the ARA เป็นระยะ.

สำหรับรายละเอียดที่มากขึ้นบน SMBus Alert mode, อ้างอิงถึง SMBus specification version 2.0.

tha

Timeout error

มีความแตกต่างกันใน the timing specifications ระหว่าง I2C and SMBus. SMBus กำหนด a clock low timeout, TIMEOUT of 35 ms. ยัง SMBus ระบุ TLOW: SEXT เป็นดัง the cumulative clock low ยืดเวลาสำหรับ a slave device. SMBus ระบุ TLOW: MEXT เป็นดัง the cumulative clock low ยืดเวลาสำหรับ a master device. สำหรับรายละเอียดที่มากขึ้นบน timeouts เหล่านี้, อ้าง
อิงถึง SMBus specification version 2.0.

The status flag Timeout หรือ Tlow Error ใน I2C_SR1 แสดงสถานะของคุณลักษณะนี้

How to use the interface in SMBus mode

เพื่อสวิทช์จาก I2C mode ไปเป็น SMBus mode, ลำดับต่อไปนี้ควรถูกกระทำ.
•    Set the SMBus bit in the I2C_CR1 register
•    กำหนดค่า the SMBTYPE and ENARP bits in the I2C_CR1 register ตามต้องการสำหรับ the application

ถ้าคุณต้องการกำหนดค่า the device เป็น a master, ทำตาม the Start condition generation ขั้นตอนใน Section 26.3.3. มิฉะนั้น, ทำตามลำดับใน Section 26.3.2.

The application ต้องควบคุม the various SMBus protocols โดย software.
•    SMB Device Default Address รับทราบ if ENARP=1 and SMBTYPE=0
•    SMB Host Header acknowledged if ENARP=1 and SMBTYPE=1
•    SMB Alert Response Address acknowledged if SMBALERT=1