Bing fire fourteen USART-- 429 notes of serial communication

Copyright: Professional idle & LoaferPro https://blog.csdn.net/XieWinter/article/details/90176151

table of Contents

1. Introduction to serial communication protocols

1.1 Physical Layer

1.2 protocol layer

2  ST USART

2.1 Functional Pin

2.2 Important state

2.3 Programming Notes

Code Example 2.4


1. Introduction to serial communication protocols

Serial communication (Serial Communication) is an inter-device commonly used serial communication as simple and convenient, most electronic devices support the communication, electronic engineers in the debugging equipment often use the communication output debugging information.

For the protocol, we are also in a hierarchical way to understand the most basic is to put it into physical layer and protocol layer. The physical layer communications system having predetermined mechanical characteristics, electronic functional parts, to ensure the transmission of the original data in the physical media. The main provisions of the protocol layer communication logic, unified send and receive both data packing, unpacking standards.

1.1 Physical Layer

Serial communication physical layer standards and there are many variants, we mainly explain the standard RS-232, RS-232 standard specifies the level of the main uses standard signal, and the signal communication interface.

Between "the DB9 Interface" two communication devices establish a connection through the serial signal line, a serial signal line using "RS-232 standard" for transmitting data signals. Since the level of the standard RS-232 signal can not directly be directly recognized by the controller, these signals are converted through a "chip-level conversion" into "the TTL Calibration" level signal recognized by the controller, the communication can be achieved.

  • Level standard

The different communication standards use levels, serial communication can be divided into standard TTL and RS-232 standard

Under standard level, over the state of the electronic circuit electrically common TTL often used, the use of 5V indicates a binary logic 1, logic 0 indicates 0V; and long-distance transmission in order to increase the anti-jamming capability serial communication, which uses -15V represent a logical 1 , + 15V represents a logical 0. The controller is generally used as a standard TTL level, so often used MA3232 chip TTL signals and RS-232 level will be mutually converted.

  • RS-232 signal lines

In the first application, RS-232 serial communication standard commonly used between the computers, modems and route (MODEN, commonly known as "cat"), in such a communication system, the device is divided into the DTE Data Terminal Equipment (Computer , routing) and data communication equipment DCE (modem). We explain their role in the signal line connection and the respective signal lines in this communication model.

In old desktop computers generally have a standard RS-232 COM port (also called DB9 connector)

The electrical connections to the signal line is referred to as the lead pin male to female signal lead lines is called female. In general the lead male computer interface, and drawn in a modem apparatus generally female, using the above figure it can be a serial cable connected to the computer. When the communication signal transmitted in serial cable RS-232 standard is used to explain the preceding modulation.

Description signal line
No. name symbol Data direction Explanation
1 Carrier detect DCD DTE> DCE Data Carrier Detect, data carrier detect, for the DTE to inform each other, this machine is the carrier signal received from the peer
2 Receive data RXD THE <-DCE Receive Data, a data signal received, i.e., the input.
3 send data TXD DTE> DCE Transmit Data, a data transmission signal, i.e., the output. TXD and RXD between the two devices to be connected to the cross
4 Data terminal (DTE) Ready DTR DTE> DCE Data Terminal Ready, Data Terminal Ready for DTE to inform the other side of this machine is ready
5 Signal ground GND -- Ground, ground communications between two devices may not be the same, this will affect the level of the signal sending and receiving sides, it is between two serial devices must use ground connection, that common ground.
6 Data Equipment (DCE) Ready DSR THE <-DCE Data Set Ready, ready to send data for DCE to inform each other if the unit is on standby
7 Request to Send RTS DTE> DCE Request To Send, request, DTE requests the present device sends data to DCE DCE end
8 Allowed to send CTS THE <-DCE Clear To Send, allowed to transmit, the DCE sends a request to respond with RTS, whether data can be sent to inform each other
9 Ring Indicator RI THE <-DCE Ring Indicator, ringing indication that the line is connected to the DCE

串口线中的RTS、CTS、DSR、DTR及DCD信号,使用逻辑 1表示信号有效,逻辑0表示信号无效。例如,当计算机端控制DTR信号线表示为逻辑1时,它是为了告知远端的调制调解器,本机已准备好接收数据,0则表示还没准备就绪。

在目前的其它工业控制使用的串口通讯中,一般只使用RXD、TXD以及GND三条信号线,直接传输数据信号。

1.2 协议层

串口通讯的数据包由发送设备通过自身的TXD接口传输到接收设备的RXD接口。在串口通讯的协议层中,规定了数据包的内容,它由启始位、主体数据、校验位以及停止位组成,通讯双方的数据包格式要约定一致才能正常收发数据。

  • 波特率

两个通讯设备之间需要约定好波特率,即每个码元的长度,以便对信号进行解码,图 中用虚线分开的每一格就是代表一个码元。常见的波特率为4800、9600、115200等。

  • 通讯的起始和停止信号

串口通讯的一个数据包从起始信号开始,直到停止信号结束。数据包的起始信号由一个逻辑0的数据位表示,而数据包的停止信号可由0.5、1、1.5或2个逻辑1的数据位表示,只要双方约定一致即可

  • 有效数据

在数据包的起始位之后紧接着的就是要传输的主体数据内容,也称为有效数据,有效数据的长度常被约定为5、6、7或8位长。

  • 数据校验

在有效数据之后,有一个可选的数据校验位。由于数据通信相对更容易受到外部干扰导致传输数据出现偏差,可以在传输过程加上校验位来解决这个问题。校验方法有奇校验(odd)、偶校验(even)、0校验(space)、1校验(mark)以及无校验(noparity)

  1. 奇校验要求有效数据和校验位中“1”的个数为奇数
  2. 偶校验与奇校验要求刚好相反,要求帧数据和校验位中“1”的个数为偶数
  3. 0校验是不管有效数据中的内容是什么,校验位总为“0”,1校验是校验位总为“1”。
  4. 在无校验的情况下,数据包中不包含校验位

2  ST USART

2.1 功能引脚

  • TX:发送数据输出引脚。
  • RX:接收数据输入引脚。
  • SW_RX:数据接收引脚,只用于单线和智能卡模式,属于内部引脚,没有具体外部引脚。
  • nRTS:请求以发送(Request To Send),n表示低电平有效。如果使能RTS流控制,当USART接收器准备好接收新数据时就会将nRTS变成低电平;当接收寄存器已满时,nRTS将被设置为高电平。该引脚只适用于硬件流控制。
  • nCTS:清除以发送(Clear To Send),n表示低电平有效。如果使能CTS流控制,发送器在发送下一帧数据之前会检测nCTS引脚,如果为低电平,表示可以发送数据,如果为高电平则在发送完当前数据帧之后停止发送。该引脚只适用于硬件流控制。
  • SCLK:发送器时钟输出引脚。这个引脚仅适用于同步模式。

STM32F42xxx系统控制器有四个USART和四个UART,其中USART1和USART6的时钟来源于APB2总线时钟,其最大频率为90MHz,其他六个的时钟来源于APB1总线时钟,其最大频率为45MHz

UART只是异步传输功能,所以没有SCLK、nCTS和nRTS功能引脚。

2.2 重要状态

编程重要状态
1 名称 描述
2 TE 发送使能,能力
3 TXE 发送寄存器为空,
4 TC 发送完成
5 TXIE 发送完成中断使能
6 RE 接收使能,能力
7 RXNE 读数据寄存器非空
8 RXNEIE 发送完成中断使能

 

  •            空闲字符 可理解为整个帧周期内电平均为“1”(停止位的电平也是“1”),该字符后是下一个数据帧的起始位。
  • 停止字符 可理解为在一个帧周期内接收到的电平均为“0”。发送器在中断帧的末尾插入 1 或 2 个停止位(逻辑“1”位)以确认起始位。

2.3 编程要点

常规中断方式:

  • 使能RX和TX引脚GPIO时钟和USART时钟
  • 初始化GPIO,并将GPIO复用到USART上
  • 配置USART参数;
  • 配置中断控制器并使能USART收发中断;
  • Enable USART;
  • In the USART receive interrupt service functions for sending and receiving data

DMA interrupt:

  • RX and TX Enable and clock USART clock pin GPIO
  • Initialize GPIO, GPIO and multiplexed onto USART
  • USART configuration parameters;
  • Interrupt controller configuration and can receive and transmit interrupts USART DMA
  • Configuration parameters and associated DMA data transfer complete interrupt and configured BUF
  • Enable USART
  • IDLE idle interrupt is identified by a single transfer is completed, the count data size, and with the completion of the DMA buffer full interrupt data reception Analyzing
  • DMA transmission by resetting the counter and enabling DMA
  • Note that considering the size of the receiving buffer exceeds closed DMA transfer, into the waiting IDLE, then processing the data, beyond the direct lost

Code Example 2.4

Redirect prinft, scanf

In the C language standard library, fputc internal function is a function of the printf function , function is the character ch f written to the file pointer points to the file of the current write pointer position, simple understanding is write characters to a specific file. We use the USART function re-edit function fputc content, to achieve a similar "write" function. Between some cases, not necessarily a hardware UART, a similar method can also redefine the RAM to facilitate debugging, subsequent devoted.

fputc fgetc function and function is very similar to continually read the characters. When using scanf function note character input format.
Another point to note, using fput and fgetc function reaches redirect input and output C language standard library functions must MDK project options on the "Use MicroLIB" tick, MicoroLIB default C library is an alternative library, it the standard C library has been highly optimized to make the code less, take up less resources. Of course, vecuronium does not need some special conditions, such as RL_FLASH, to achieve redirected through other means.

Using printf, scanf function need to include stdio.h header file in the file.

///重定向c库函数printf到串口,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
		/* 发送一个字节数据到串口 */
		USART_SendData(DEBUG_USART, (uint8_t) ch);
		
		/* 等待发送完毕 */
		while (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_TXE) == RESET);		
	
		return (ch);
}

///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
		/* 等待串口输入数据 */
		while (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(DEBUG_USART);
}

 

Guess you like

Origin blog.csdn.net/XieWinter/article/details/90176151