STM32F1 USART

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

Previous topic - Next topic

tha

Reception using DMA

DMA mode สามารถถูก enabled สำหรับ reception โดยการเซ็ต the DMAR bit in USART_CR3 register. Data ถูกโหลดจาก the USART_DR register ไปยัง a SRAM area ที่กำหนดค่าโดยใช้ the DMA peripheral (refer to the DMA specification) เมื่อไรก็ตามที่ a data byte ถูกรับ. เพื่อแม็ป a DMA channel สำหรับ USART reception, ใช้ขั้นตอนดังต่อไปนี้:
1.    เขียน the USART_DR register address ใน the DMA control register เพื่อกำหนดค่ามันเป็นต้นทางของ the transfer. The
       data จะถูกย้ายจาก address นี้ไปยัง the memory หลังจากแต่ละ RXNE event.
2.    เขียน the memory address ใน the DMA control register เพื่อกำหนดค่ามันเป็นปลายทางของ the transfer. The data จะ
       ถูกโหลดจาก the USART_DR register ไปยัง memory area นี้หลังจากแต่ละ RXNE event.
3.    กำหนดค่าจำนวนทั้งหมดของ bytes ที่จะถูกส่งถ่ายลงใน the DMA control register.
4.    กำหนดค่า the channel priority ใน the DMA register
5.    กำหนดค่า DMA interrupt generation หลังจาก half/ full transfer ตามที่ต้องการโดย the application.
6.    ทำให้ทำงาน the channel in the DMA register

เมื่อจำนวนของ data transfers ที่โปรแกรมใน the DMA Controller ถูกถึง, the DMA controller จะสร้าง an interrupt บน the DMA channel interrupt vector


tha

Error flagging and interrupt generation in multibuffer communication

ในกรณีของ multibuffer communication ถ้า error ใดๆเกิดขึ้นในระหว่างการติดต่อกัน the error flag จะถูกถือสิทธิ์หลังจาก the current byte. An interrupt จะถูกสร้างขึ้นถ้า the interrupt enable flag ถูก set. สำหรับ framing error, overrun error and noise flag ซึ่งถูกถือสิทธิ์พร้อมกับ RXNE ในกรณีของ single byte reception, จะมี separate error flag interrupt enable bit (EIE bit in the USART_CR3 register), ซึ่งถ้า set จะปล่อยออก an interrupt หลังจาก the current byte พร้อมกับอย่างใดอย่างหนึ่งของ errors เหล่านี้.


tha

27.3.14 Hardware flow control

มันเป็นไปได้ที่จะควบคุม the serial data flow ระหว่าง two devices โดยการใช้ the CTS input และ the RTS output. Figure 299 แสดงวิธีการต่อ two devices ในโหมดนี้:



RTS and CTS flow control สามารถถูก enabled อย่างอิสระโดยการเขียน RTSE and CTSE bits เป็น 1 ตามลำดับ (ใน the USART_CR3 register).

RTS flow control

ถ้า the RTS flow control ถูก enabled (RTSE=1), จากนั้น RTS ถูกถือสิทธิ์ (tied low) ตราบเท่าที่ the USART receiver พร้อมที่จะรับ new data. เมื่อ the receive register เต็ม, RTS ยกเลิกการถือสิทธิ์, แสดงให้เห็นว่า the transmission คาดว่าจะหยุดที่ the end of the current frame. Figure 300 แสดงตัวอย่างของ communication พร้อมกับ RTS flow control ถูกเปิดการใช้งาน.



CTS flow control

ถ้า the CTS flow control ถูก enabled (CTSE=1), จากนั้น the transmitter เช็ค the CTS input ก่อนการส่ง the next frame. ถ้า CTS ถูกถือสิทธิ์ (tied low), จากนั้น the next data ถูกส่ง (สมมุติว่า a data จะถูกส่ง, กล่าวอีกนัยหนึ่ง, ถ้า TXE=0), ถ้าเป็นอย่างอื่น the transmission จะไม่เกิดขึ้น. เมื่อ CTS ถูกยกเลิกการถือสิทธิ์ในระหว่าง a transmission, the current transmission เสร็จสมบูรณ์ก่อนที่ the transmitter จะหยุด.

เมื่อ CTSE=1, the CTSIF status bit ถูกเซ็ตโดยอัตโนมัติโดย hardware ทันทีที่ the CTS input สลับ. มันแสดงให้เห็นว่าเมื่อ the receiver กลายมาเป็นพร้อมหรือไม่พร้อมสำหรับ communication. An interrupt ถูกสร้างขึ้นถ้า the CTSIE bit ใน the USART_CR3 register ถูกเซ็ต. รูปภายข้างล่างแสดงตัวอย่างของ communication พร้อมกับ CTS flow control ถูกเปิดการใช้งาน.



tha

27.4 USART interrupts



The USART interrupt events ถูกต่อถึง the same interrupt vector (see Figure 302).
•    ในระหว่าง transmission: Transmission Complete, Clear to Send or Transmit Data Register empty interrupt.
•    ในขณะที่ receiving: Idle Line detection, Overrun error, Receive Data register not empty, Parity error, LIN break
      detection, Noise Flag (only in multi buffer communication) and Framing Error (only in multi buffer
      communication).

events เหล่านี้สร้าง an interrupt ถ้า the corresponding Enable Control Bit ถูก set.


tha

27.5 USART mode configuration


tha

27.6 USART registers

อ้างอิงถึง Section 2.2 on page 45 สำหรับรายการของตัวย่อที่ใช้ใน register descriptions.

The peripheral registers สามารถถูกเข้าถึงโดย half-words (16-bit) or words (32-bit).



Bits 31:10 Reserved, forced by hardware to 0.

Bit 9 CTS: CTS flag
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the CTS input สลับ, ถ้า the CTSE bit ถูกเซ็ต. มันถูกเคลียร์โดย software (โดยการเขียนมันเป็น 0). An interrupt ถูกสร้างขึ้นถ้า CTSIE=1 in the USART_CR3 register.
     0: ไม่มีการเปลี่ยนแปลงเกิดขึ้นบน the CTS status line
     1: การเปลี่ยนแปลงเกิดขึ้นบน the CTS status line
บิตนี้ไม่มีให้ใช้สำหรับ UART4 & UART5.

Bit 8 LBD: LIN break detection flag
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the LIN break ถูกตรวจพบ. มันถูกเคลียร์โดย software (โดยการเขียนมันเป็น 0). An interrupt ถูกสร้างขึ้นถ้า LBDIE = 1 in the USART_CR2 register.
     0: LIN Break ไม่ถูกตรวจพบ
     1: LIN break ถูกตรวจพบ
Note: An interrupt ถูกสร้างขึ้นเมื่อ LBD=1 ถ้า LBDIE=1

Bit 7 TXE: Transmit data register empty
บิตนี้ถูกเซ็ตโดย hardware เมื่อสิ่งที่บรรจุของ the TDR register ถูกส่งถ่ายลงใน the shift register. An interrupt ถูกสร้างขึ้นถ้า the TXEIE bit =1 in the USART_CR1 register. มันถูกเคลียร์โดยการเขียนไปยัง the USART_DR register.
     0: Data ไม่ถูกส่งถ่ายไปยัง the shift register
     1: Data ถูกส่งถ่ายไปยัง the shift register)
Note: บิตนี้ถูกใช้ในระหว่าง single buffer transmission.

Bit 6 TC: Transmission complete
บิตนี้ถูกเซ็ตโดย hardware ถ้า the transmission ของ a frame ที่บรรจุ data เสร็จสมบูรณ์และถ้า TXE ถูกเซ็ต. An interrupt ถูกสร้างขึ้นถ้า TCIE=1 in the USART_CR1 register. มันถูกเคลียร์โดย a software sequence (การอ่านจาก the USART_SR register ตามด้วยการเขียนไปยัง the USART_DR register). The TC bit สามารถถูกเคลียร์โดยการเขียน a '0' ไปยังมัน. ลำดับการเคลียร์นี้ขอแนะนำให้ใช้เฉพาะสำหรับ multibuffer communication.
     0: Transmission ไม่เสร็จสมบูรณ์
     1: Transmission เสร็จสมบูรณ์

Bit 5 RXNE: Read data register not empty
บิตนี้ถูกเซ็ตโดย hardware เมื่อสิ่งที่บรรจุของ the RDR shift register ถูกส่งถ่ายไปยัง the USART_DR register. An interrupt ถูกสร้างขึ้นถ้า RXNEIE=1 in the USART_CR1 register. มันถูกเคลียร์โดยการอ่านไปยัง the USART_DR register. The RXNE flag สามารถถูกเคลียร์โดยการเขียนศูนย์ไปยังมันได้อีกด้วย. ลำดับการเคลียร์นี้ขอแนะนำให้ใช้เฉพาะสำหรับ multibuffer communication.
     0: Data ไม่ถูกรับ
     1: Received data พร้อมที่จะถูกอ่าน.

Bit 4 IDLE: IDLE line detected
บิตนี้ถูกเซ็ตโดย hardware เมื่อ an Idle Line ถูกตรวจพบ. An interrupt ถูกสร้างขึ้นถ้า the IDLEIE=1 in the USART_CR1 register. มันถูกเคลียร์โดย a software sequence (การอ่านไปยัง the USART_SR register ตามด้วยการอ่านไปยัง the USART_DR register).
     0: ไม่มี Idle Line ถูกตรวจพบ
     1: Idle Line ถูกตรวจพบ
Note: The IDLE bit จะไม่ถูกเซ็ตอีกครั้งจนกระทั่ง the RXNE bit ถูกเซ็ตด้วยตัวมันเอง (อย่างเช่น a new idle line เกิดขึ้น).

tha

Bit 3 ORE: Overrun error
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the word ที่ปัจจุบันกำลังถูกรับใน the shift register พร้อมที่จะถูกส่งถ่ายลงใน the RDR register ในขณะที่ RXNE=1. An interrupt ถูกสร้างขึ้นถ้า RXNEIE=1 in the USART_CR1 register. มันถูกเคลียร์โดย a software sequence (การอ่านไปยัง the USART_SR register ตามด้วยการอ่านไปยัง the USART_DR register).
     0: ไม่มี Overrun error
     1: Overrun error ถูกตรวจพบ
Note: เมื่อบิตนี้ถูกเซ็ต, the RDR register content จะไม่ถูกทำให้สูญหายแต่ the shift register จะถูกเขียนทับ. An interrupt ถูก
           สร้างขึ้นบน ORE flag ในกรณีของ Multi Buffer communication ถ้า the EIE bit ถูก set.

Bit 2 NE: Noise error flag
บิตนี้ถูกเซ็ตโดย hardware เมื่อ noise ถูกตรวจพบบน a received frame. มันถูกเคลียร์โดย a software sequence (การอ่านไปยัง the USART_SR register ตามด้วยการอ่านไปยัง the USART_DR register).
     0: ไม่มี noise ถูกตรวจพบ
     1: Noise ถูกตรวจพบ
Note: บิตนี้ไม่สร้าง interrupt เนื่องจากมันปรากฎในเวลาเดียวกันกับ the RXNE bit ซึ่งตัวมันเองสร้างการอินเตอรัพท์ interrupt ถูกสร้าง
           ขึ้นบน NE flag ในกรณีของ Multi Buffer communication ถ้า the EIE bit ถูก set.

Bit 1 FE: Framing error
บิตนี้ถูกเซ็ตโดย hardware เมื่อ a de-synchronization, excessive noise or a break character ถูกตรวจพบ. มันถูกเคลียร์โดย a software sequence (การอ่านไปยัง the USART_SR register ตามด้วยการอ่านไปยัง the USART_DR register).
     0: ไม่มี Framing error ถูกตรวจพบ
     1: Framing error or break character ถูกตรวจพบ
Note: บิตนี้ไม่สร้าง interrupt เนื่องจากมันปรากฎในเวลาเดียวกันกับ the RXNE bit ซึ่งตัวมันเองสร้าง an interrupt. ถ้า the word ที่
           ปัจจุบันกำลังถูกส่งถ่ายเป็นสาเหตุทั้ง frame error and overrun error, มันจะถูกส่งถ่ายและเฉพาะ the ORE bit จะถูกเซ็ต. An
           interrupt ถูกสร้างขึ้นบน FE flag ในกรณีของ Multi Buffer communication ถ้า the EIE bit ถูก set.

Bit 0 PE: Parity error
บิตนี้ถูกเซ็ตโดย hardware เมื่อ a parity error เกิดขึ้นใน receiver mode. มันถูกเคลียร์โดย a software sequence (การอ่านไปยัง the USART_SR register ตามด้วยการอ่านไปยัง the USART_DR register). The software ต้องคอยสำหรับ the RXNE flag ถูกเซ็ตก่อนการเคลียร์ the PE bit. An interrupt ถูกสร้างขึ้นถ้า PEIE = 1 in the USART_CR1 register.
     0: No parity error
     1: Parity error

tha



Bits 31:9 Reserved, forced by hardware to 0.

Bits 8:0 DR[8:0]: Data value
บรรจุ the Received or Transmitted data character, ขึ้นอยู่กับว่ามันถูกอ่านจากหรือเขียนถึง.
The Data register ทำหน้าที่ a double function (read and write) เนื่องจากมันประกอบด้วย two registers, one for transmission (TDR) and one for reception (RDR)
The TDR register จัดให้มี the parallel interface ระหว่าง the internal bus และ the output shift register (see Figure 1).
The RDR register จัดให้มี the parallel interface ระหว่าง the input shift register และ the internal bus.
เมื่อการส่งพร้อมด้วย the parity ถูกเปิดการใช้งาน (PCE bit ถูกเซ็ตเป็น 1 ใน the USART_CR1 register), ค่าที่ถูกเขียนใน the MSB (bit 7 หรือ bit 8 ขึ้นอยู่กับ the data length) จะไม่มีผลเพราะมันถูกแทนที่โดย the parity.
เมื่อการรับพร้อมด้วย the parity ถูกเปิดการใช้งาน, ค่าที่ถูกอ่านใน the MSB bit คือ the received parity bit.