STM32F1 USART

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

Previous topic - Next topic

tha

27 Universal synchronous asynchronous receiver transmitter (USART)

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 ทั้งหมดเว้นแต่จะระบุไว้เป็นอย่างอื่น

27.1 USART introduction

The universal synchronous asynchronous receiver transmitter (USART) เสนอวิธีการที่หยืดหยุ่นของ full-duplex data exchange กับ external equipment ที่ต้องการ an industry standard NRZ asynchronous serial data format. The USART เสนอ a very wide range ของ baud rates โดยใช้ a fractional baud rate generator.

มันรองรับ synchronous one-way communication และ half-duplex single wire communication. มันยังรองรับ the LIN (local interconnection network), Smartcard Protocol และ IrDA (infrared data association) SIR ENDEC specifications, และ modem operations (CTS/RTS). มันยอมให้ multiprocessor communication

High speed data communication สามารถเป็นได้โดยการใช้ the DMA สำหรับ multibuffer configuration.

tha

27.2 USART main features

•    Full duplex, asynchronous communications
•    NRZ standard format (Mark/Space)
•    Fractional baud rate generator systems
      –    A common programmable transmit and receive baud rates สูงถึง 4.5 MBits/s
•    Programmable data word length (8 or 9 bits)
•    กำหนดค่าได้ stop bits - รองรับสำหรับ 1 or 2 stop bits
•    LIN Master Synchronous Break send capability and LIN slave break detection capability
      –    13-bit break generation and 10/11 bit break detection เมื่อ USART เป็น hardware ที่กำหนดค่าสำหรับ LIN
•    Transmitter clock output for synchronous transmission
•    IrDA SIR Encoder Decoder
      –    รองรับสำหรับ 3/16 bit duration for normal mode
•    Smartcard Emulation Capability
      –    The Smartcard interface รองรับ the asynchronous protocol Smartcards ตามที่กำหนดใน ISO 7816-3 standards
      –    0.5, 1.5 Stop Bits for Smartcard operation
•    Single wire half duplex communication
•    กำหนดค่าได้ multibuffer communication โดยใช้ DMA (direct memory access)
      –    Buffering ของ received/transmitted bytes ใน reserved SRAM โดยใช้ centralized DMA
•    Separate(แยกกัน) enable bits สำหรับ Transmitter and Receiver
•    Transfer detection flags:
      –    Receive buffer full
      –    Transmit buffer empty
      –    End of Transmission flags
•    Parity control:
      –    Transmits parity bit
      –    Checks parity of received data byte
•    Four error detection flags:
      –    Overrun error
      –    Noise error
      –    Frame error
      –    Parity error
•    Ten interrupt sources with flags:
      –    CTS changes
      –    LIN break detection
      –    Transmit data register empty
      –    Transmission complete
      –    Receive data register full
      –    Idle line received
      –    Overrun error
      –    Framing error
      –    Noise error
      –    Parity error
•    Multiprocessor communication - เข้าสู่ mute mode ถ้า address match ไม่เกิดขึ้น
•    Wake up from mute mode (by idle line detection or address mark detection)
•    Two receiver wakeup modes: Address bit (MSB, 9th bit), Idle line

tha

27.3 USART functional description

The interface ถูกต่อภายนอกไปยัง device อื่นโดย three pins (ดู Figure 279). Any USART bidirectional communication ต้องการอย่างต่ำ two pins: Receive Data In (RX) และ Transmit Data Out (TX):

RX: Receive Data Input เป็น the serial data input. Oversampling techniques ถูกใช้สำหรับการกู้คืน data โดยการแยกแยะระหว่าง valid incoming data และ noise.

TX: Transmit Data Output. เมื่อ the transmitter ถูกปิดการใช้งาน, the output pin คืนกลับไปยัง IO port configuration ของมัน. เมื่อ the transmitter ถูกเปิดการใช้งานและไม่มีอะไรถูกส่ง, the TX pin จะอยู่ที่ high level. ใน single-wire และ smartcard modes, IO นี้ถูกใช้ส่งและรับ the data (ที่ USART level, data จากนั้นถูกรับบน SW_RX).

ผ่านทางพินเหล่านี้, serial data ถูก transmitted และ received ใน normal USART mode เป็น frames ประกอบด้วย:
•    An Idle Line ก่อนที่จะ transmission หรือ reception
•    A start bit
•    A data word (8 or 9 bits) least significant bit first(เป็นอันดับแรก)
•    0.5,1, 1.5, 2 Stop bits แสดงให้เห็นว่า the frame เสร็จสมบูรณ์
•    interface นี้ใช้ a fractional baud rate generator - ด้วย a 12-bit เป็นตัวเลขที่อยู่ทางขวาของจุดและ 4-bit เป็นเศษส่วน
•    A status register (USART_SR)
•    Data register (USART_DR)
•    A baud rate register (USART_BRR) - 12-bit เป็นตัวเลขที่อยู่ทางขวาของจุดและ 4-bit เป็นเศษส่วน
•    A Guardtime Register (USART_GTPR) ในกรณีของ Smartcard mode.

อ้างอิงถึง Section 27.6: USART registers สำหรับคำจำกัดความของแต่ละบิต

พินต่อไปนี้ถูกต้องการเพื่อเชื่อมต่อใน synchronous mode:
•    CK: Transmitter clock output. พินนี้ส่งออก the transmitter data clock สำหรับ synchronous transmission ตรง
      กันกับ SPI master mode (ไม่มี clock pulses บน start bit และ stop bit, และ a software มีทางเลือกที่จะส่ง a clock pulse
      บน the last data bit). ใน parallel data สามารถถูกรับแบบ synchronously บน RX. นี้สามารถถูกใช้เพื่อควบคุม peripherals ที่
      มี shift registers (ตัวอย่างเช่น LCD drivers). The clock phase และ polarity โปรแกรมได้ทาง software. ใน Smartcard
      mode, CK สามารถจัดให้มี the clock ไปยัง the smartcard.

พินต่อไปนี้ถูกต้องการใน Hardware flow control mode:
•    CTS: Clear To Send บล็อก the data transmission ที่ the end of the current transfer เมื่อ high
•    RTS: Request to send แสดงให้เห็นว่า the USART พร้อมที่จะรับ a data (เมื่อ low).



tha

27.3.1 USART character description

Word length อาจถูกเลือกให้เป็นอย่างใดอย่างหนึ่ง 8 หรือ 9 bits โดยการโปรแกรม the M bit ใน the USART_CR1 register (ดู Figure 280).

The TX pin อยู่ใน low state ในระหว่าง the start bit. มันอยู่ใน high state ในระหว่าง the stop bit.

An Idle character ถูกตีความหมายว่าเป็นเฟรมทั้งหมดของ" 1" ตามด้วย the start bit ของ the next frame ซึ่งบรรจุ data (จำนวนของ "1" 's จะรวมถึงจำนวนของ stop bits).

A Break character ถูกตีความหมายบนการรับ "0"s สำหรับ a frame period. ที่ the end of the break frame the transmitter แทรกอย่างใดอย่างหนึ่ง 1 or 2 stop bits (logic "1" bit) เพื่อ acknowledge the start bit.

Transmission and reception ถูกขับโดย a common baud rate generator, the clock สำหรับแต่ละตัวนั้นถูกสร้างขึ้นเมื่อ the enable bit ถูกเซ็ตสำหรับ the transmitter and receiver ตามลำดับ

รายละเอียดของแต่ละ block ถูกให้ไว้ด้านล่างนี้.


tha

27.3.2 Transmitter

The transmitter สามารถส่ง data words ของอย่างใดอย่างหนึ่ง 8 หรือ 9 bits ขึ้นอยู่กับ the M bit status. เมื่อ the transmit enable bit (TE) ถูกเซ็ต, the data ใน the transmit shift register ถูกส่งออกไปบน the TX pin และ the corresponding clock pulses ถูกส่งออกไปบน the CK pin.

Character transmission

ในระหว่าง a USART transmission, data เลื่อนออก least significant bit เป็นอันดับแรกไปบน the TX pin. ในโหมดนี้, the USART_DR register ประกอบด้วย a buffer (TDR) ระหว่าง the internal bus และ the transmit shift register (ดู Figure 279).

ทุกๆ character ถูกนำหน้าด้วย a start bit, ซึ่งเป็น a logic level low เป็นเวลา one bit period. The character ถูกปิดท้ายด้วย จำนวนที่กำหนดค่าได้ของ stop bits.

The following stop bits ถูกรองรับโดย USART: 0.5, 1, 1.5 and 2 stop bits.

Note: The TE bit ไม่ควรถูกรีเซ็ตในระหว่าง transmission of data. การรีเซ็ต the TE bit ในระหว่าง the transmission จะทำความ
           เสียหายให้ the data บน the TX pin ดังที่ the baud rate counters จะได้รับการแช่แข็ง. The current data ที่กำลังถูกส่งจะ
           สูญหาย.

            An idle frame จะถูกส่งหลังจาก the TE bit ถูก enabled.

tha

Configurable stop bits

จำนวนของ stop bits ที่จะถูกส่งพร้อมกับทุกๆ character สามารถถูกโปรแกรมใน Control register 2, bits 13,12.
1.    1 stop bit: นี้เป็นค่าเริ่มต้น.
2.    2 stop bits: นี้ถูกรองรับโดย USART ปกติ, single-wire และ modem modes.
3.    0.5 stop bit: ถูกใช้เมื่อ receiving data ใน Smartcard mode.
4.    1.5 stop bits: ถูกใช้เมื่อ transmitting and receiving data in Smartcard mode

An idle frame transmission จะรวม the stop bits เข้าด้วย.

A break transmission จะเป็น 10 low bits ตามด้วยจำนวนที่กำหนดค่าได้ของ stop bits (เมื่อ m = 0) และ 11 low bits ตามด้วยจำนวนที่กำหนดค่าได้ของ stop bits (เมื่อ m = 1). มันเป็นไปไม่ได้ที่จะส่ง long breaks (break ของความยาวที่มากกว่า 10/11 low bits).



ขั้นตอน:
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.
5.    เลือก the desired baud rate โดยใช้ the USART_BRR register.
6.    เซ็ต the TE bit ใน USART_CR1 เพื่อส่ง an idle frame เป็น first transmission.
7.    เขียน the data ที่จะส่งใน the USART_DR register (นี้จะเคลียร์ the TXE bit). ทำซ้ำอย่างนี้สำหรับแต่ละ data ที่จะถูกส่งในกรณี
       ของ single buffer.
8.    หลังจากการเขียน the last data ลงใน the USART_DR register, คอยจนกระทั่ง TC=1. นี้แสดงให้เห็นว่า the transmission
       ของ the last frame เสร็จสมบูรณ์. นี้ถูกต้องการอย่างเช่นเมื่อ the USART ถูกปิดการใช้งานหรือเข้าสู่ the Halt mode เพื่อหลีก
       เลี่ยงการเสียหาย the last transmission.

tha

Single byte communication

The TXE bit ถูกเคลียร์เสมอโดยการเขียนไปยัง the data register.

The TXE bit ถูกเซ็ตโดย hardware และมันแสดงให้เห็น:
•    The data ถูกเคลื่อนจาก TDR ไปยัง the shift register แล้วและ the data transmission ถูกสตาร์ทแล้ว.
•    The TDR register ว่างเปล่า.
•    The next data สามารถถูกเขียนใน the USART_DR register โดยปราศจากการเขียนทับ data ก่อนหน้า.

flag นี้สร้าง an interrupt ถ้า the TXEIE bit ถูกเซ็ต

เมื่อ a transmission กำลังเกิดขึ้น, a write instruction ไปยัง the USART_DR register จะเก็บ the data ใน the TDR register และซึ่งจะถูกคัดลอกลงใน the shift register ที่ the end of the currenttransmission

เมื่อไม่มี transmission เกิดขึ้น, a write instruction ไปยัง the USART_DR register จะใส่ the data โดยตรงลงใน the shift register, the data transmission จะสตาร์ท, และ the TXE bit ถูกเซ็ตโดยทันที.

ถ้าเฟรมเดียวถูกส่ง (หลังจาก the stop bit) และ the TXE bit ถูกเซ็ต, the TC bit ไปเป็น high. An interrupt ถูกสร้างขึ้นถ้า the TCIE bit ถูกเซ็ตใน the USART_CR1 register.

หลังจากการเขียน the last data ลงใน the USART_DR register, มีข้อบังคับว่าให้คอยสำหรับ TC=1 ก่อนการปิดการใช้งาน the USART หรือการเป็นสาเหตุให้ the microcontroller เข้าสู่ the low-power mode (ดู Figure 282).

The TC bit ถูกเคลียร์โดย the following software sequence:
1.    การอ่านจาก the USART_SR register
2.    การเขียนไปยัง the USART_DR register

Note: The TC bit สามารถถูกเคลียร์โดยการเขียน '0' ไปยังมันได้อีกด้วย. ลำดับการเคลียร์นี้แนะนำเฉพาะสำหรับ Multibuffer
           communication เท่านั้น.


tha

Break characters

การเซ็ต the SBK bit จะส่ง a break character. The break frame length ขึ้นอยู่กับ the M bit (see Figure 280).

ถ้า the SBK bit ถูกเซ็ตเป็น '1' a break character ถูกส่งไปบน the TX line หลังจากการเสร็จสมบูรณ์ the current character transmission. บิตนี้ถูกรีเซ็ตโดย hardware เมื่อ the break character ถูกทำให้เสร็จสมบูรณ์ (ในระหว่าง the stop bit of the break character). The USART แทรก a logic 1 bit ที่ the end of the last break frame เพื่อรับประกันการรับรู้ของ the start bit of the next frame

Note: ถ้า the software รีเซ็ต the SBK bit ก่อนการเริ่มต้นของ break transmission, the break character จะไม่ถูกส่ง. สำหรับ
           สอง breaks ที่ติดตามกัน, the SBK bit ควรถูกเซ็ตหลังจาก the stop bit ของ break ก่อนหน้า.

Idle characters

การเซ็ต the TE bit จะขับ the USART เพื่อส่ง an idle frame ก่อน the first data frame.