UART

Pages List
List view
Home
Portfolio
HW
FW
SW
FPGA / Adaptive SoC
Daily
Photo
Etc
 
STM32 Peripheral

UART


notion image

UART와 USART


notion image
UART는 Asynchronous mode만 지원하고, USART는 Asynchronous mode와 Synchronous mode 모두를 지원한다.
 
notion image
Synchronous transmission의 경우에 Clock은 data stream과 분리되어 있고, Start/Stop bit는 사용되지 않는다.
 
notion image
Asynchronous transmission의 경우에 Clock은 data와 함께 보내지지 않는다. 대신에 Start/Stop bit와 같은synchronization bit를 사용한다.
 
notion image
 
 

Understanding UART pins


notion image
  • UART Bi-directional communication은 최소 2개의 pin(Tx, Rx)이 필요하다.
    (Hardware Flow control을 사용하지 않는 경우)
  • 아무것도 송신하지 않을 때, Tx line은 High 상태를 유지한다.(idle state)
  • data를 수신하는 경우에 UART 모듈은 Rx line을 계속 sampling해서 data frame의 start bit를 detect한다.
  • CTS, RTS pin은 Hardware Flow control을 사용할 때, 쓰이는 pin이다.
    • CTS (Clear To Send) pin은 Hardware Flow control을 사용할 때 필요한 Active Low pin이다. Tx line의 data transmission은 오직 CTS pin이 Low인 상태에서만 가능하다.
    • RTS (Request To Send) pin은 Hardware Flow control을 사용할 때 필요한 Active Low pin이다. UART 모듈이 다른 device에게 data를 요청할 때, RTS pin을 사용한다.
 
notion image
 
 

UART Frame Formats


Frame은 communication 과정에서 송신되거나 수신되는 전체 data packet을 의미한다.
Communication protocol에 따라 Frame의 format은 제각각이다.
(TCP/IP, UDP, USART 서로 다른 Frame Format을 가진다.)
 
UART의 Frame Format은 다음과 같다.
notion image
 
notion image
 
 

Baud Rate


notion image
Baud rate는 data의 송신/수신 속도(Bps)를 의미하고, Baud rate의 역수는 bit 1개가 차지하는 시간이다. 설정 가능한 Baud rate는 2400, 4800, 9600, 19200, 38400, 57600, 115200 이다.
 
 

Synchronization bits


notion image
Synchronization bit는 각 data chunck와 함께 움직이는 Start/Stop bit를 의미한다.
Start bit는 Packet의 시작을 표시하고, Stop bit는 Packet의 끝을 표시한다.
Stop bit는 1, 1.5, 2bit크기로 설정할 수 있는데, Buad rate가 Mbps단위인 경우에는 Stop bit를 2bit로 설정하는 것이 바람직하다.
 
 

Parity


notion image
Error detection의 가장 간단한 방법이 Parity를 추가하는 것이다.
Parity는 어떤 수를 binary로 표현했을 때, 1의 개수를 의미한다.
 

① Even Parity

Even Parity는 Parity bit까지 포함한 전체 binary의 1의 개수가 짝수인 Parity이다.
notion image
data byte의 binary에서 1의 개수가 홀수면 Parity bit를 1로 정해서, 전체 binary의 1의 개수를 짝수로 만든다.
 

② Odd Parity

Odd Parity는 Parity bit까지 포함한 전체 binary의 1의 개수가 홀수인 Parity이다.
notion image
data byte의 binary에서 1의 개수가 홀수면 Parity bit를 0으로 정해서, 전체 binary의 1의 개수를 홀수로 만든다.
 
notion image
Data communication 과정에서 수신한 data의 Parity가 약속한 Parity가 아니라면, data가 corrupt되었다고 판단하고, 해당 data를 제거한다.
(단, 이 경우에는 짝수 개의 bit에 대한 data corruption은 검출할 수 없다.)
 
 

UART Functional block


notion image
 
 

UART Transmitter


이전 data frame의 stop bit가 성공적으로 송신된 이후에 새로운 data가 shift register에 load될 수 있다.
notion image
 
notion image
 
notion image
 
 

UART Receiver


notion image
 
notion image
 
notion image
 
 

USART Driver development and Configurable items


notion image
 
notion image
 
notion image
 
 

USART Send data


notion image

① M = 1 (9bit of data frame)

Parity가 Enable 되어 있다면, 8bit가 user data이고, 나머지 1bit가 parity bit이다.
Hardware가 8th bit 자리에 자동으로 parity bit를 붙인다.
Parity가 Disable 되어 있다면, 9bit 전체가 user data이다.

② M = 0 (8bit of data frame)

Parity가 Enable 되어 있다면, 7bit가 user data이고, 나머지 1bit가 parity bit이다.
Hardware가 7th bit 자리에 자동으로 parity bit를 붙인다.
Parity가 Disable 되어 있다면, 8bit 전체가 user data이다.
 
 

USART Oversampling


notion image
 
notion image

① Oversampling by 16

Recevier engine은 1bit time을 16번 sampling하고 8,9,10번 sample들이 analyze된다.

② Oversampling by 8

Recevier engine은 1bit time을 8번 sampling하고 4,5,6번 sample들이 analyze된다.
 
notion image
3개의 Sample 값을 기준으로 수신된 bit가 0인지 1인지를 결정한다.
만약 3개의 sample값이 모두 0 또는 1로 동일하지 않다면, Noise Error가 발생했다고 간주하고, Noise detection flag를 set한다.
그리고 해당 bit를 제거하거나 application logic에 따라 처리한다.
 
notion image
 
notion image
‘OverSampling by 8’을 선택하면, 최고 Fclk/8의 Baudrate값을 얻을 수 있다.
하지만 Clock deviation에 대한 tolerance가 감소해서 noise error에 취약해 질 수 있다.
 
‘OverSampling by 16’을 선택하면, 최고 Fclk/16의 Baudrate값을 얻을 수 있다.
그리고 앞의 경우보다 Clock deviation에 대한 tolerance가 증가해서 noise error에 상대적으로 강하다.
 
정리하면, Noisy한 환경에서 USART를 사용하는 경우, ‘OverSampling by 16’을 선택하는 것이 바람직하다.
Noise가 거의 없는 환경의 경우에는 ‘OverSampling by8’을 선택하는 것이 바람직하다.
 
 

USART Baudrate calculation


notion image
 

Generic formula

notion image
 

예시 1) USART Baudrate 계산

notion image
USARTDIV를 계산한 다음에 USART_BRR 레지스터에 값을 넣어주어야 한다.
 
notion image
 
notion image
 
notion image
Fractional part에는 Oversampling값인 16을 곱해야 한다.
 

예시 2) USART Baudrate 계산 2

notion image
 
notion image
Fractional part에는 Oversampling값인 8을 곱해야 한다.
 
 

예제1) USART Tx Test


[ 결과 ]

notion image
 
notion image
 
notion image
 
 

예제2) USART Rx Test with Interrupt


이 예제에서는 Rx만 Interrupt를 사용하고, Arduino로의 Tx는 blocking call API를 썼다.
Kiran이 Tx를 Interrupt로 하는게 귀찮아서 그런것 같으니까 나중에 Tx의 Interrupt도 테스트해서 update해볼 것!
notion image
 
 
notion image