Ch4 Stm32 Interface Usart Can
Ch4 Stm32 Interface Usart Can
Ch4 Stm32 Interface Usart Can
2ING-ISI
1
L’interface série : Définition et Principe
• Permet au MCU de communiquer avec des systèmes à base de
microprocesseur.
• Les données envoyées ou reçues se présentes sous la forme d’une
succession temporelle (sur une seule ligne) de valeurs binaires
images d’un mots.
• Transmission simultanée de 8 bits sur 8 • Données envoyées ou reçues bit par bit (+ lente)
voies différentes (rapide) • données en série pour émission / réception
• fils proches perturbations importantes à (UART registres à décalage)
haut débit • avantage : grande distance, universelle
• pb de place (beaucoup fils) (beaucoup applications)
2 On s’intéresse à ce type de transmission
L’interface série : Synchrone
• Les unités en liaison possèdent chacune une horloge interne cadencée à la même
fréquence.
• Lorsqu’une unité veut émettre un mot binaire, elle génère un front descendant
sur sa ligne émettrice . A la fin de l’émission de ce mot, la ligne repasse au niveau
haut.
5
Les différents interfaces séries
Plusieurs normes de communication série:
UART (Universal Asynchronous Recever Transmitter)
CAN (Controller Area Network)
SPI (Serial Peripheral Interface)
I2C (Inter Integrated Circuit)
USB (Universal serial Bus)
LIN (Local Interconnect Network)
Ethernet
Wireless interfaces
Zigbee
bluetooth
Flash I/F
512kB- 1MB Camera Interface
168 MHz
10
UART Protocol (1/2)
Standard
0.5 and 1.5 stop bit: To be used when receiving and transmetting data in Smartcard mode.
UART Protocol (2/2)
12
USART ON STM32F4
CORTEX-M4 AHB2 (max 168MHz)
D-bus Encryption**
CPU + FPU +
MPU I-bus
Flash I/F
512kB- 1MB Camera Interface
168 MHz
PLL
USART 2, 3 ,4,5 Clock Control AHB1
Bridge APB1 (max 42MHz)
RTC / AWU
(max 168MHz)
5x 16-bit Timer
51/82/114/140 I/Os
Bridge 4KB backup RAM
2x 32-bit Timer
2x6x 16-bit PWM
Synchronized AC Timer 2x DAC + 2 Timers
2x Watchdog
APB2 (max 84MHz)
14
APB1 or APB2
write Read
Transmit Data Register Receive Data Register
8 bits 12 bits
TXE bit=1
TX pin RX pin
Transmit shift register Receive shift register
Génération d’horloge
SClk
15
Comment marquer la fin de la
transmission
Un bit de flag TC (Transfer Complete) est mis à 1 par le hardware dans le Status
Register (SR):
Si une trame est envoyé et le TXE=1
pas de nouvelle écriture dans le TDR donc le TC=1
16
UART Features (1/3)
Full duplex, asynchronous communications
Fully-programmable serial interface characteristics:
Data can be 8, or 9 bits
Parity :
Even (pair), odd (impair), or no-parity bit generation and detection
MSB bit : The 8th bit when data=8 bits ) or 9 th bit when the data=9)
stop bit generation : 0,5; 1; 1,5 or 2 (0, 5 and 1,5 for smart card
mode)
UART Features (3/3)
Programmable baud rate generator
Integer part mantisse (12 bits)
Fractional part (4bits)
Up to 2 Mbps
18
USART Registers
Data registers (32 bits) :
TDR : contain the data to be transmitted TDR[8…0]
RDR : contain the received Data
Configuration registers (32 bits):
3 USART_CR (Configuration register): to define:
M bit( word length): 8 or 9 bits
Number of stop bit,;
UE bit (UART Enable) to enable UART in CR1
TE bit(=1) to send an idle frame as first transmission, USART mode (Lin,
Smartcard mode…),
DMA use or not (DMAR, DMAT) in CR3 …
USART_BRR register : 12 bits mantissa + 4 bits fractional to define
USARTDIV desired baud rate
USART_SR register : Status Register
(TXE, RXNE, TC, …
TXEIE( TX Empty Interrupt Enable), RXNEIE( RXNE Interrupt Enable) pour
pouvoir générer des interruptions
19
Qlq problèmes? Et solution
Lors de la transmission, le récepteur peut ne pas
pouvoir traiter les données reçues (une donnée non
traitée peut être écraser par la suivante)
un buffer pour pouvoir stocker un certain nombre de
données avant leur traitement FIFO mode enabled (
not implemented in STM32F4 and available in STM32F7
contrôle du flow de transmission par le matériel (CTS
et RTS)
20
APB1 or APB2
TXE bit=1
TX pin RX pin
Transmit shift register Receive shift register
Génération d’horloge
SClk
21
UART Features (2/3)
16 x 8 bits Tx FIFO (
16 x 12 bits Rx FIFO (12 bits: 8 Data bits + 4 Error bits : Frame Error,
parity error,Noise Detection, overrun error)
Configurable Tx and Rx FIFO level
7/8 14 – Bytes
UART interrupts:
Receive Interrupt 3/4 12 – Bytes
Transmit Interrupt 1/2 8 - Bytes
Receive Timeout Interrupt
1/4 4 - Bytes
Framing Interrupt
Parity Error Interrupt 1/8 2 - Bytes
FIFO Levels
1/2 8 - Bytes
1/4 4 - Bytes
1/8 2 - Bytes
Disabled 1 - Byte
FIFO Levels
7/8 14 - Bytes
3/4 12 - Bytes
1/2 8 - Bytes
FIFO Level 1/2 (8 - Bytes Depth) Receive Interrupt
1/4 4 - Bytes
1/8 2 - Bytes
Disabled 1 - Byte
Receive Interrupt
RXNE=1 et si RXNEEI =1 alors Interrupt
25
Hardware Flow Control :
UART4 send bytes to UART2 Free one location (Read action)
UART4 UART2
Rx Rx
Tx Tx
RTS RTS
CTS CTS
UART4 CTS
UART4 Tx FIFO UART2 Rx FIFO
UART2 RTS
UART4 DMA Requests:Transmit Request
Example of an UART4 Transmit DMA Single and Burst request
DMA mode can be enabled for transmit by setting the DMAT bit in USART_CR3 register.
USART_DMACmd(USART4, USART_DMAReq_Tx, Enable)
DMA
Stream 3
locations
is requested when the transmit FIFO
becomes less than the
programmed watermark. The Burst
length depends from the FIFO level.
Tx Burst Length = 8
UART4 DMA Requests: Receive Request
Example of an UART4 Receive DMA Single and Burst request
DMA mode can be enabled for reception by setting the DMAR bit in USART_CR3 register.
USART_DMACmd(USARTx, USART_DMAReq_Rx, FunctionalState NewState)
DMA
Stream 2
Rx Burst Length = 4
UART1 configuration example
/* Enable USART1 Clock */ 29
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
USART_InitStructure.USART_BaudRate = 19200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART1 */
USART_Init( USART1, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd( USART1, ENABLE ); , // UART is Now Ready USART 1 is ready now …
USART_SendData( USART1, Data[i]); // écriture dans TX buffer/register
tableauR[i]= ReceiveData(USART1); // Read depuis RX buffer/register
Quiz
What is the maximum UART BaudRate?
____________
What are the two signals needed for Hardware flow control ?
____________
Receiver ERRORS
Overrun error
An "overrun error" occurs when the receiver cannot process the
character that just came in before the next one arrives. Various
devices have different amounts of buffer space to hold received
characters. The CPU must service the UART in order to remove
characters from the input buffer. If the CPU does not service the
UART quickly enough and the buffer becomes full, an Overrun Error
will occur, and incoming characters will be lost.
Framing error
A "framing error" occurs when the designated "start" and "stop" bits
are not found. As the "start" bit is used to identify the beginning of an
incoming character, it acts as a reference for the remaining bits. If the
data line is not in the expected state (hi/lo) when the "stop" bit is
expected, a Framing Error will occur.
Parity error[
A Parity Error occurs when the parity of the number of 1 bits
disagrees with that specified by the parity bit. Use of a parity bit is
optional, so this error will only occur if parity-checking has been
enabled. 31