--实际应用发现9位数据大时候有丢包错包问题??
利用USART可以进行多机处理器通信,其原理就是使从机处于静默模式,由主机在需要的时候发送指令唤醒从机,并传输数据。STM32静默模式特点:1、所有接收状态位都不会被设置;2、所有的接收中断都被禁止;3、USART_CR1寄存器中的RWU位被置1,RUW可以硬件自动控制或者在某些条件下由软件写。
连接方法很简单,主机的TX输出与从机的RX端口直接相连,从机TX端口要经过与门与主机RX端口连接。
多机通信方式有2种:空闲帧唤醒和地址唤醒。
空闲帧唤醒可以同时唤醒所有从机,在从机处于静默模式时发送空闲帧(即所有位均为1的数据),唤醒多个从机,实现多个从机同步。
地址唤醒可以唤醒单个从机,从机静默时发送地址帧,从机自动对比地址,地址配对正确则该从机唤醒,否则继续进入静默。这样只有被寻址者才被激活,来接收数据,减少由未被寻址的接收机器参与带来的多余的USART服务开销。这种模式下,MSB为1的字节被认为是地址,否则被认为是数据(MSB一般为数据传送的最高位,8位传送则MSB为第八位;9位传送则MSB为第九位)。在一个地址字节中,目标接收者的地址放在低4位。这4位会被接收器拿来和设置在USART_CR2寄存器中ADD位中的自身地址比较。当接收到一个和设置地址相匹配的地址字符时,RWU被清除,后面的字节将正常接收。因为RWU位已经被清除,RXEN位会因为接收到地址符被置1。当从机再次接收到地址符,如若地址不匹配则从机再次进入静默模式。
程序实际编写时可以注意以下3部分的设置:
一、设置静默模式:
函数原形
void USART_WakeUpConfig(USART_TypeDef* USARTx, u16 USART_WakeUp)
其中USART_WakeUp:USART的唤醒方式:
1、USART_WakeUp_IdleLine 空闲总线唤醒
2、USART_WakeUp_AddressMark 地址标记唤醒
二、地址唤醒中地址设置:
函数原形
void USART_SetAddress(USART_TypeDef* USARTx, u8 USART_Address)
其中USART_Address:是设置USART 节点的地址。
例如:USART_SetAddress(USART2, 0x5);
三、是否进入静默:
函数原形
void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState Newstate)
其中NewState: USART 静默模式的新状态这个参数可以取:ENABLE或者DISABLE。
Address mark detection (WAKE=1)
In this mode, bytes are recognized as addresses if their MSB is a ‘1 else they are
considered as data. In an address byte, the address of the targeted receiver is put on the 4
LSB. This 4-bit word is compared by the receiver with its own address which is programmed
in the ADD bits in the USART_CR2 register.
The USART enters mute mode when an address character is received which does not
match its programmed address. In this case, the RWU bit is set by hardware. The RXNE flag
is not set for this address byte and no interrupt nor DMA request is issued as the USART
would have entered mute mode.
It exits from mute mode when an address character is received which matches the
programmed address. Then the RWU bit is cleared and subsequent bytes are received
normally. The RXNE bit is set for the address character since the RWU bit has been cleared.
The RWU bit can be written to as 0 or 1 when the receiver buffer contains no data (RXNE=0
in the USART_SR register). Otherwise the write attempt is ignored.
Bit 1 RWU: Receiver wakeup
This bit determines if the USART is in mute mode or not. It is set and cleared by software
and can be cleared by hardware when a wakeup sequence is recognized.
0: Receiver in active mode
1: Receiver in mute mode
Note: 1: Before selecting Mute mode (by setting the RWU bit) the USART must first receive a
data byte, otherwise it cannot function in Mute mode with wakeup by Idle line detection.
2: In Address Mark Detection wakeup configuration (WAKE bit=1) the RWU bit cannot
be modified by software while the RXNE bit is set.
Bit 11 WAKE: Wakeup method
This bit determines the USART wakeup method, it is set or cleared by software.
0: Idle Line
1: Address Mark
“logically ANDed ” 指的是逻辑上的关系。某些情况下,不需要另外加与门电路。
比如:板上有3个STM32,1主2从,中间未加电平变换芯片(直连),从机的发送脚都配置成open drain输出并带上拉,这种情况下2个从STM32芯片的TX脚可以 直接连到主STM32芯片的RX脚。
如果用了电平变换芯片比如MAX232,一般需要另外加与门电路。