STM32F1 I2C

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

Previous topic - Next topic

tha

26 Inter-integrated circuit (I2C) interface
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
ส่วนนี้ใช้กับตระกูล STM32F10xxx ทั้งหมดเว้นแต่จะระบุไว้เป็นอย่างอื่น

26.1 I2C introduction
I2C (inter-integrated circuit) bus Interface บริการเป็น an interface ระหว่าง the microcontroller และ the serial I2C bus. จัดให้มี multimaster capability, และควบคุม I2C bus-specific sequencing ทั้งหมด, protocol, arbitration และ timing. มันรับรอง the standard mode (Sm, สูงถึง 100 kHz) และ Fm mode (Fm, สูงถึง 400 kHz).

อาจถูกใช้เพื่อวัตถุประสงค์หลายประการรวมถึงการสร้างและการตรวจสอบ CRC, SMBus (system management bus) and PMBus (power management bus).

ขึ้นอยู่กับ specific device implementation DMA capability สามารถมีให้ใช้สำหรับลดการเกินพิกัดของ CPU .

26.2 I2C main features
•    Parallel-bus/I2C protocol converter
•    Multimaster capability: the same interface สามารถทำงานเป็น Master or Slave
•    I2C Master features:
      –    Clock generation
      –    Start and Stop generation
•    I2C Slave features:
      –    Programmable I2C Address detection
      –    Dual Addressing Capability เพื่อรู้จัก 2 slave addresses
      –    Stop bit detection
•    Generation and detection of 7-bit/10-bit addressing and General Call
•    Supports different communication speeds:
      –    Standard Speed (up to 100 kHz)
      –    Fast Speed (up to 400 kHz)
•    Analog noise filter
•    Status flags:
      –    Transmitter/Receiver mode flag
      –    End-of-Byte transmission flag
      –    I2C busy flag
•    Error flags:
      –    Arbitration lost condition for master mode
      –    Acknowledgment failure after address/ data transmission
      –    Detection of misplaced start or stop condition
      –    Overrun/Underrun if clock stretching is disabled
•    2 Interrupt vectors:
      –    1 Interrupt for successful address/ data communication
      –    1 Interrupt for error condition
•    Optional clock stretching
•    1-byte buffer with DMA capability
•    Configurable PEC (packet error checking) generation or verification:
      –    PEC value สามารถถูกส่งเป็น last byte in Tx mode
      –    PEC error checking for last received byte
•    SMBus 2.0 Compatibility:
      –    25 ms clock low timeout delay
      –    10 ms master cumulative clock low extend time
      –    25 ms slave cumulative clock low extend time
      –    Hardware PEC generation/verification with ACK control
      –    Address Resolution
•    PMBus Compatibility
Note: บางคุณลักษณะข้างบนนี้อาจไม่มีให้ใช้ในทุก mcu. ผู้ใช้ควรดูที่ the product data sheet, พื่อระบุคุณสมบัติเฉพาะที่รองรับโดย the
           I2C interface implementation(การนำไปใช้งาน)

tha

26.3 I2C functional description

นอกเหนือจาก receiving and transmitting data, interface แปลงมันจาก serial ไปสู่ parallel format และในทางกลับกัน. The interrupts ถูก enabled หรือ disabled โดย software. The interface ถูกต่อถึง the I2C bus โดย a data pin (SDA) และโดย a clock pin (SCL). มันสามารถถูกต่อด้วย a standard (สูงถึง 100 kHz) หรือ fast (สูงถึง 400 kHz) I2C bus.

26.3.1 Mode selection

The interface สามารถทำงานในหนึ่งของ the four following modes:
• Slave transmitter
• Slave receiver
• Master transmitter
• Master receiver

โดยค่าเริ่มต้น, มันทำงานใน slave mode. The interface สวิทช์โดยอัตโนมัติจาก slave ไปยัง master, หลังจากมันสร้าง a START condition และจาก master ไปยัง slave, ถ้า an arbitration loss หรือ a Stop generation เกิดขึ้น, จะยอมให้ multimaster capability.

tha

Communication flow

ใน Master mode, the I2C interface เริ่มต้น a data transfer และสร้าง the clock signal. A serial data transfer เริ่มด้วย a start condition และสิ้นสุดด้วย a stop condition เสมอ. ทั้ง start and stop conditions ถูกสร้างขึ้นใน master mode โดย software.

ใน Slave mode, the interface มีสามารถของการจดจำ addresses ของตัวเอง (7 or 10-bit), และ the General Call address. The General Call address detection อาจถูก enabled หรือ disabled โดย software

Data and addresses ถูกส่งถ่ายเป็น 8-bit bytes, MSB first. The first byte(s) ที่ตาม the start condition บรรจุ the address (one in 7-bit mode, two in 10-bit mode). The address ถูกส่งใน Master mode เสมอ.

A 9th clock pulse ที่ตาม the 8 clock cycles ของ a byte transfer, ในระหว่างนี้ the receiver ต้องส่ง an acknowledge bit ไปยัง the transmitter. อ้างอิงถึง Figure 269.



Acknowledge อาจถูก enabled หรือ disabled โดย software. The I2C interface addresses (dual addressing 7-bit/ 10-bit and/or general call address) สามารถถูกเลือกโดย software.

The block diagram of the I2C interface ถูกแสดงใน Figure 270.


1. SMBA เป็น an optional signal ใน SMBus mode. สัญญานนี้ไม่ได้ใช้ถ้า SMBus ถูก disabled.


tha

26.3.2 I2C slave mode

โดยค่าเริ่มต้น the I2C interface ทำงานใน Slave mode. เพื่อสวิทช์จาก default Slave mode ไปยัง Master mode a Start condition generation ถูกต้องการ.

The peripheral input clock ต้องถูกโปรแกรมใน the I2C_CR2 register เพื่อสร้าง correct timings. The peripheral input clock frequency ต้องเป็นอย่างน้อย:
•    2 MHz in Sm mode
•    4 MHz in Fm mode

ทันทีที่ a start condition ถูกตรวจพบ, the address ได้รับจาก the SDA line และส่งไปยัง the shift register. จากนั้นมันจะถูกเปรียบเทียบกับ the address ของ the interface (OAR1) และกับ OAR2 (if ENDUAL=1) หรือ the General Call address (if ENGC = 1).

Note: ใน 10-bit addressing mode, the comparison รวมเอา the header sequence (11110xx0), ที่ xx แสดงถึง the two
           most significant bits of the address.

Header หรือ address ไม่ตรงกัน: the interface เพิกเฉยมันและคอยสำหรับ another Start condition.

Header ตรงกัน (10-bit mode only): the interface สร้าง an acknowledge pulse ถ้า the ACK bit ถูกเซ็ตและคอยสำหรับ the 8-bit slave address.

Address ตรงกัน: the interface สร้างในลำดับ:
•    An acknowledge pulse ถ้า the ACK bit ถูก set
•    The ADDR bit ถูก set โดย hardware แะ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN bit ถูก set.
•    ถ้า ENDUAL=1, the software ต้องอ่าน the DUALF bit เพื่อเช็คว่า slave address ถูก acknowledged แล้ว.

ใน 10-bit mode, หลังจากการรับ the address sequence, the slave จะอยู่ใน Receiver mode เสมอ. มันจะเข้าสู่ Transmitter mode บนการรับ a repeated Start condition ที่ตามโดย the header sequence ด้วย matching address bits และ the least significant bit เซ็ต (11110xx1).

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

tha

Slave transmitter

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

The slave จะขึง SCL low จนกระทั่ง ADDR ถูกเคลียร์และ DR ถูกเต็มด้วย the data ที่จะถูกส่ง (ดู Figure 271 Transfer sequencing EV1 EV3).

เมื่อ the acknowledge pulse ถูกรับ:
•    The TxE bit ถูก set โดย hardware พร้อมด้วย an interrupt ถ้า the ITEVFEN และ the ITBUFEN bits ถูก set.

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


tha

Slave receiver

หลังจาก the address reception และหลังจากเคลียร์ ADDR, the slave รับ bytes จาก the SDA line ลงใน the DR register โดยทาง the internal shift register. หลังจากแต่ละ byte, the interface สร้างในลำดับ:
•    An acknowledge pulse ถ้า the ACK bit ถูก set
•    The RxNE bit ถูก set โดย hardware และ an interrupt ถูกสร้างขึ้นถ้า the ITEVFEN และ ITBUFEN bit ถูก set.

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


1.    The EV1 event ขึง SCL low จนกระทั่ง the end of the corresponding software sequence.
2.    The EV2 software sequence ต้องทำให้เสร็จสมบูรณ์ก่อน the end of the current byte transfer
3.    หลังจากเช็ค the SR1 register content, the user ควรดำเนินการ the complete clearing sequence สำหรับแต่ละ flag ที่พบ
       ว่า set.
ดังนั้น, สำหรับ ADDR and STOPF flags, ลำดับต่อไปนี้ถูกต้องการภายใน the I2C interrupt routine:
READ SR1
if (ADDR == 1) {READ SR1; READ SR2}
if (STOPF == 1) {READ SR1; WRITE CR1}
จุดประสงค์คือทำให้แน่ใจว่าทั้ง ADDR and STOPF flags ถูกเคลียร์ถ้าทั้งสองถูกพบว่า set.

tha

Closing slave communication

หลังจาก the last data byte ถูกส่งถ่าย a Stop Condition ถูกสร้างขึ้นโดย the master. The interface ตรวจพบเงื่อนไขนี้และ sets:
•    The STOPF bit และสร้าง an interrupt ถ้า the ITEVFEN bit ถูก set.

The STOPF bit ถูกเคลียร์โดยการอ่านของ the SR1 register ตามด้วยการเขียนไปยัง the CR1 register (ดู EV4 in Figure 272)

tha

26.3.3 I2C master mode

ใน Master mode, the I2C interface เริ่มต้น a data transfer และสร้าง the clock signal. A serial data transfer เริ่มด้วย a Start condition และจบด้วย a Stop condition เสมอ.

Master mode ถูกเลือกทันทีที่ the Start condition ถูกสร้างขึ้นบน the bus ด้วย a START bit.

ต่อไปนี้คือลำดับที่ต้องการใน master mode.
•    โปรแกรม the peripheral input clock ใน I2C_CR2 Register เพื่อสร้าง correct timings
•    กำหนดค่า the clock control registers
•    กำหนดค่า the rise time register
•    โปรแกรม the I2C_CR1 register เพื่อเปิดการใช้งาน the peripheral
•    เซ็ต the START bit ใน the I2C_CR1 register เพื่อสร้าง a Start condition

The peripheral input clock frequency ต้องเป็นอย่างน้อย:
•    2 MHz in Sm mode
•    4 MHz in Fm mode