STM32F1 USART

Started by tha, January 16, 2021, 07:14:07 AM

Previous topic - Next topic

tha

27.3.3 Receiver

The USART สามารถรับ data words ของอย่างใดอย่างหนึ่ง 8 หรือ 9 bits ขึ้นอยู่กับ the M bit ใน the USART_CR1 register.

Start bit detection

ใน the USART, the start bit ถูกตรวจพบเมื่อ a specific sequence of samples ถูกรู้จัก.
ลำดับนี้คือ : 1 1 1 0 X 0 X 0 X 0 0 0 0.



Note:  ถ้า the sequence ไม่เสร็จสมบูรณ์, the start bit detection จะแท้งและ the receiver กลับคืนสู่ the idle state (ไม่มี flag
            ถูก set) ที่มันคอยสำหรับ a falling edge.

            The start bit ถูกยืนยัน (RXNE flag set, interrupt ถูกสร้างขึ้นถ้า RXNEIE=1) ถ้า the 3 sampled bits อยู่ที่ 0 (first
            sampling บน the 3rd, 5th and 7th bits พบว่า the 3 bits อยู่ที่ 0 และ second sampling บน the 8th, 9th and
            10th bits พบว่า the 3 bits อยู่ที่ 0 อีกด้วย)

            The start bit ถูกตรวจสอบว่าใช้ได้ (RXNE flag set, interrupt ถูกสร้างขึ้นถ้า RXNEIE=1) แต่ the NE noise flag ถูก set
            ถ้า, สำหรับทั้งสองการสุ่มตัวอย่าง, อย่างน้อย 2 ใน the 3 sampled bits อยู่ที่ 0 (sampling on the 3rd, 5th and 7th bits
            and sampling on the 8th, 9th and 10th bits). ถ้าเงื่อนไขนี้ไม่ถูกตรงตาม, the start detection จะแท้งและ the
            receiver กลับคืนสู่ the idle state (ไม่มี flag ถูก set)

            ถ้า, สำหรับหนึ่งของ the samplings (sampling on the 3rd, 5th and 7th bits หรือ sampling on the 8th, 9th and
            10th bits), 2 ใน the 3 bits ถูกพบว่าอยู่ที่ 0, the start bit ถูกตรวจสอบว่าใช้ได้แต่ the NE noise flag bit ถูก set..

tha

Character reception

ในระหว่าง a USART reception, data เลื่อนเข้า least significant bit เป็นอันดับแรกผ่านทาง the RX pin. ในโหมดนี้, the USART_DR register ประกอบด้วย a buffer (RDR) ระหวาง the internal bus and the received shift register.

ขั้นตอน:
1.    เปิดการใช้งาน the USART โดยการเขียน the UE bit ใน USART_CR1 register เป็น 1.
2.    โปรแกรม the M bit ใน USART_CR1 เพื่อกำหนด the word length
3.    โปรแกรมจำนวนของ stop bits ใน USART_CR2
4.    เลือก DMA enable (DMAT) ใน USART_CR3 ถ้า Multi buffer Communication ถูกนำมาใช้. กำหนดค่า the DMA register
       ดังอธิบายใน multibuffer communication. STEP 3
5.    เลือก the desired baud rate โดยใช้ the baud rate register USART_BRR.
6.    เซ็ต the RE bit USART_CR1. นี้เปิดการใช้งาน the receiver ซึ่งเริ่มการค้นหาสำหรับ a start bit.

เมื่อ a character ถูกรับ
•    The RXNE bit ถูก set. มันแสดงให้เห็นว่า the content of the shift register ถูกส่งถ่ายไปยัง the RDR. กล่าวอีกนัยหนึ่ง, data
      ถูกรับแล้วและสามารถถูกอ่าน (เช่นเดียวกับ associated error flags ของมัน).
•    An interrupt ถูกสร้างขึ้นถ้า the RXNEIE bit ถูก set.
•    The error flags สามารถถูกเซ็ตถ้า a frame error, noise or an overrun error ถูกตรวจพบในระหว่าง reception
•    ใน multibuffer, RXNE ถูกเซ็ตหลังจากทุกๆ byte ถูกรับและถูกเคลียร์โดย the DMA อ่านไปยัง the Data Register.
•    ใน single buffer mode, การเคลียร์ the RXNE bit ถูกกระทำโดย a software โดยอ่านไปยัง the USART_DR register. The
      RXNE flag สามารถถูกเคลียร์โดยการเขียนศูนย์ไปยังมันได้อีกด้วย. The RXNE bit ต้องถูกเคลียร์ก่อน the end of the reception
      of the next character เพื่อหลีกเลี่ยง an overrun error.

Note: The RE bit ไม่ควรถูกรีเซ็ตในขณะที่กำลังรับ data. ถ้า the RE bit ถูกปิดการใช้งานในระหว่าง reception, the reception of
           the current byte จะถูกยกเลิก.

tha

Break character

เมื่อ a break character ถูกรับ received, the USART รับมือมันดั่ง a framing error.

Idle character

เมื่อ an idle frame ถูกตรวจพบ, จะมีขั้นตอนเดียวกันกับ a data received character บวก an interrupt ถ้า the IDLEIE bit ถูก set.

Overrun error

An overrun error เกิดขึ้นเมื่อ a character ถูกรับเมื่อ RXNE ยังไม่ถูก reset. Data ไม่สามารถถูกส่งถ่ายจาก the shift register ไปยัง the RDR register จนกระทั่ง the RXNE bit ถูกเคลียร์.

The RXNE flag ถูก set หลังจากทุกๆ byte ถูกรับ. An overrun error เกิดขึ้นถ้า RXNE flag ถูก set เมื่อ the next data ถูกรับหรือ the previous DMA request ยังไม่ได้ถูกบริการ. เมื่อ an overrun error เกิดขึ้น :
•    The ORE bit ถูก set.
•    The RDR content จะไม่สูญหาย. The previous data สามารถใช้ประโยชน์ได้เมื่อการอ่านไปยัง USART_DR ถูกดำเนินการ.
•    The shift register จะถูกเขียนทับ. หลังจากจุดนี้, data ใดๆที่ถูกรับในระหว่าง overrun จะสูญหาย.
•    An interrupt ถูกสร้างขึ้นถ้าอย่างใดอย่างหนึ่ง the RXNEIE bit ถูก set หรือทั้ง the EIE and DMAR bits ถูก set.
•    The ORE bit ถูก reset โดยการอ่านไปยัง the USART_SR register ตามด้วย a USART_DR register read operation

Note: The ORE bit, เมื่อ set, แสดงให้เห็นว่าอย่างน้อยt 1 data จะสูญหาย. มาสองความเป็นไปได้ :
•    ถ้า RXNE=1, ดังนั้น the last valid data ถูกเก็บใน the receive register RDR และสามารถถูกอ่าน,
•    ถ้า RXNE=0, ดังนั้นมันหมายความว่า the last valid data ถูกอ่านไปแล้วและดังนั้นไม่มีอะไรที่ต้องถูกอ่านใน the RDR. กรณีนี้
      สามารถเกิดขึ้นเมื่อ the last valid data ถูกอ่านใน the RDR ในเวลาเดียวกันที่ the new (and lost) data ถูกรับ. มันอาจเกิดขึ้น
      เมื่อ the new data ถูกรับในระหว่าง the reading sequence (ระหว่าง the USART_SR register read access และ the
      USART_DR read access).


tha

Noise error

Over-sampling techniques ถูกใช้ (ยกเว้นใน synchronous mode) สำหรับการกู้คืน data โดยการแยกแยะระหว่าง valid incoming data และ noise.





เมื่อ noise ถูกตรวจพบใน a frame:
•    The NE ถูก set ที่ the rising edge ของ the RXNE bit.
•    The invalid data ถูกส่งถ่ายจาก the Shift register ไปยัง the USART_DR register.
•    ไม่มี interrupt ถูกสร้างขึ้นในกรณีของ single byte communication. อย่างไรก็ตามบิตนี้ rises ในเวลาเดียวกันกับ the RXNE bit
      ซึ่งสร้าง an interrupt ของมันเอง. ในกรณีของ multibuffer communication an interrupt จะถูกปล่อยออกมาถ้า the EIE bit
      ถูก set ใน the USART_CR3 register.

The NE bit ถูก reset โดย a USART_SR register read operation ตามด้วย a USART_DR register read operation.

tha

Framing error

A framing error ถูกตรวจพบเมื่อ :

The stop bit ไม่ถูกรับรู้บน reception ในเวลาที่คาดไว้, ตามด้วยอย่างใดอย่างหนึ่ง a desynchronization หรือ excessive noise.

เมื่อ the framing error ถูกตรวจพบ:
•    The FE bit ถูก set โดย hardware
•    The invalid data ถูกส่งถ่ายจาก the Shift register ไปยัง the USART_DR register.
•    ไม่มี interrupt ถูกสร้างขึ้นในกรณีของ single byte communication. อย่างไรก็ตามบิตนี้ rises ในเวลาเดียวกันกับ the RXNE bit
      ซึ่งสร้าง an interrupt ของมันเอง. ในกรณีของ multibuffer communication an interrupt จะถูกปล่อยออกมาถ้า the EIE bit
      ถูก set ใน the USART_CR3 register.

The FE bit ถูก reset โดย a USART_SR register read operation ตามด้วย a USART_DR register read operation.

tha

Configurable stop bits during reception

จำนวนของ stop bits ที่ถูกรับสามารถถูกกำหนดค่าผ่านทาง the control bits ของ Control Register 2 - มันสามารถเป็นอย่างใดอย่างหนึ่ง 1 or 2 in normal mode และ 0.5 or 1.5 in Smartcard mode.
1.    0.5 stop bit (reception in Smartcard mode): ไม่มีการสุ่มถูกทำสำหรับ 0.5 stop bit. เป็นผลให้, ไม่มี framing error และ
       ไม่มี break frame สามารถถูกตรวจพบเมื่อ 0.5 stop bit ถูกเลือก.
2.    1 stop bit: การสุ่มสำหรับ 1 stop Bit ถูกทำบน the 8th, 9th and 10th samples
3.    1.5 stop bits (Smartcard mode): เมื่อส่งใน Smartcard mode, the device ต้องเช็คว่า the data ถูกส่งอย่างถูกต้อง. ดังนั้น
       the receiver block ต้องถูกเปิดการใช้งาน(RE =1 in the USART_CR1 register) และ the stop bit ถูกเช็คเพื่อ test ถ้า the
       smartcard ถูกตรวจพบ a parity error. ในกรณีของ a parity error, the smartcard บังคับให้ the data signal low ใน
       ระหว่างการสุ่ม - NACK signal-, ซึ่งถูกป้ายชื่อว่าเป็น a framing error. ดังนั้น, the FE flag ถูก set พร้อมด้วย the RXNE ที่ the
       end of the 1.5 stop bit. การสุ่มสำหรับ 1.5 stop bits ถูกทำบน the 16th, 17th and 18th samples (1 baud clock
       period หลังจากการเริ่มต้นของ the stop bit). The 1.5 stop bit สามารถถูกแยกออกเป็นส่วนลงใน 2 ส่วน: one 0.5 baud clock
       period ในระหว่างซึ่งไม่มีอะไรเกิดขึ้น, ตามด้วย 1 normal stop bit period ในระหว่างซึ่งการสุ่มเกิดขึ้นครึ่งทางผ่านไป. Refer to
       Section 27.3.11 for more details.
4.    2 stop bits: Sampling สำหรับ 2 stop bits ถูกทำบน the 8th, 9th and 10th samples of the first stop bit. ถ้า a
       framing error ถูกตรวจพบในระหว่าง the first stop bit the framing error flag จะถูก set. The second stop bit ไม่ถูกเช็ค
       สำหรับ framing error. The RXNE flag จะถูก set ที่ the end of the first stop bit.

tha

27.3.4 Fractional baud rate generation

The baud rate สำหรับ the receiver and transmitter (Rx and Tx) ทั้งคู่ถูกเซ็ตที่ค่าเดียวกันดังที่โปรแกรมใน the Mantissa and Fraction values of USARTDIV.



USARTDIV คือ an unsigned fixed point number ที่ถูกโค้ดใน the USART_BRR register.

Note: The baud counters ถูกอัปเดตด้วย the new value of the Baud registers หลังจากการเขียนไปยัง USART_BRR. ด้วยเหตุนี้
           the Baud rate register value ไม่ควรถูกเปลี่ยนในระหว่าง communication.

How to derive USARTDIV from USART_BRR register values

Example 1:
ถ้า DIV_Mantissa = 0d27 and DIV_Fraction = 0d12 (USART_BRR = 0x1BC), ดังนั้น
Mantissa (USARTDIV) = 0d27
Fraction (USARTDIV) = 12/16 = 0d0.75
ดังนั้น USARTDIV = 0d27.75

Example 2:
เพื่อโปรแกรม USARTDIV = 0d25.62
นี้นำไปสู่ :
DIV_Fraction = 16*0d0.62 = 0d9.92
จำนวนจริงที่ใกล้ที่สุดคือ 0d10 = 0xA
DIV_Mantissa = mantissa (0d25.620) = 0d25 = 0x19
ดังนั้น, USART_BRR = 0x19A ด้วยเหตุนี้ USARTDIV = 0d25.625

Example 3:
เพื่อโปรแกรม USARTDIV = 0d50.99
นี้นำไปสู่ :
DIV_Fraction = 16*0d0.99 = 0d15.84
จำนวนจริงที่ใกล้ที่สุดคือ 0d16 = 0x10 => overflow of DIV_frac[3:0] => ตัวทศต้องถูกบวกขึ้นไปยัง the mantissa
DIV_Mantissa = mantissa (0d50.990 + carry) = 0d51 = 0x33
ดังนั้น, USART_BRR = 0x330 ด้วยเหตุนี้ USARTDIV = 0d51.000



Note: ยิ่ง the CPU clock ต่ำลงเท่าไหร่การต่ำลงนี้จะเป็นความแม่นยำสำหรับ a particular Baud rate. ค่าจำกัดบนของ the
           achievable baud rate สามารถถูกคงที่ด้วย data นี้.
           เฉพาะ USART1 ถูก clocked ด้วย PCLK2 (72 MHz max). USARTs อื่นถูก clocked ด้วย PCLK1 (36 MHz max).

tha

27.3.5 USART receiver's tolerance to clock deviation

The USART's asynchronous receiver ทำงานได้อย่างถูกต้องเฉพาะถ้า the total clock system deviation มีขนาดเล็กกว่า the USART receiver tolerance. สาเหตุที่ทำให้เกิดผลรวม the total deviation คือ:
•    DTRA: Deviation เนื่องจาก the transmitter error (ซึ่งรวมถึง the deviation of the transmitter local oscillator อีกด้วย)
•    DQUANT: Error เนื่องจาก the baud rate quantization of the receiver
•    DREC: Deviation of the receiver's local oscillator
•    DTCL: Deviation เนื่องจาก the transmission line (โดยทั่วไปเนื่องจาก the transceivers ที่สามารถทำให้เกิดความไม่สมมาตร
      ระหว่าง the low-to-high transition timing และ the high-to-low transition timing)

      DTRA + DQUANT + DREC + DTCL < USART receiver tolerance

The USART receiver tolerance ในการรับ data อย่างถูกต้องคือเท่ากับ the maximum tolerated deviation และขึ้นอยู่กับตัวเลือกต่อไปนี้:
•    10- or 11-bit character length ถูกกำหนดโดย the M bit ใน the USART_CR1 register
•    ใช้ fractional baud rate หรือไม่



Note: ตัวเลขที่ระบุใน Table 193 and Table 194 อาจแตกต่างกันเล็กน้อยในกรณีพิเศษเมื่อ the received frames บรรจุ some Idle frames ของ 10-bit ครั้งพอดิบพอดีเมื่อ M=0 (11-bit ครั้งเมื่อ M=1).