The RS485 interface has good anti-noise performance, long transmission distance and multi-station capability, making it the preferred serial interface for industrial control. The RS485 interface is also widely used in embedded systems as a serial interface for device control. RS485 uses two-wire differential wiring to transmit serial data. Since both transmission and reception are performed using these two differential lines, it is a half-duplex mode of operation. Based on the characteristics of RS485, the hardware and software methods are used to realize the switching of RS485 transmission and reception directions, and the function of RS485 direction switching on the software of DM8168 embedded platform is mainly solved.
The RS485 bus is a very mature technology in industrial applications and one of the industry standards for modern communication technology. The RS485 bus is very convenient for multi-station interconnection. It can be realized by a pair of twisted pairs. It adopts balanced transmission and differential reception. That is, the transmitter driver converts the TTL level signal into a differential signal output. At the receiving end, the receiver will The differential signal becomes TTL level and therefore has the ability to resist common mode interference. According to the RS485 standard, the communication distance can reach 1200 m when the data rate is 100 kb/s.
RS485 is widely used in embedded systems. The embedded system can control the terminal device through the RS485 interface. Since RS485 is a half-duplex mode, the direction of transmission and reception switching requires our attention and research. If the direction switching mode is not well selected, it may cause the RS485 drive capability to drop, the software execution efficiency to drop, and even cause system abnormalities.
In this paper, two ways of hardware RS485 direction switching and software RS485 direction switching are given. Both methods have their own advantages, and the hardware mode is relatively simple to control. The software mode has better drive capability, but it has a close relationship with the embedded platform. Different platforms need to be debugged and verified.
1 Hardware mode controls RS485 direction
Figure 1 shows the circuit diagram of the hardware control RS485. The circuit uses the 2N7002LT1G MOS field effect transistor to logically invert the RS485 transmission signal of the UART_TXD_485 MCU output and send it to the RE/DE PIN pin of the RS485 chip. The principle of control is that the DE of the RS485 chip is enabled when the UART_TXD_485 output is low; the RE is enabled when the output is high. By default UART_TXD_485 is high and the RS485 chip is in the receive state. When transmitting data, there is a high and low level signal change on the UART_TXD_485. The low level signal is directly output through the RS485 chip SP3072EENL/TR, and the high level signal is controlled by an external pull-up resistor.
The advantage of this method is that the control is simple, the software does not need to do extra work, and the RS485 control is like RS232. However, the disadvantage of this method is that the driving capability may be insufficient. Since this control method does not fully exert the driving capability of the RS485 driver chip itself, the output signal depends on the external pull-up resistor, so in a complicated environment, such as when many loads need to be controlled. There will be problems with insufficient driving ability. However, in some simple environments or software implementations that are more complex, it is still feasible to use this method.
Figure 1 hardware control RS485 circuit diagram
2 software mode to control RS485 direction
2.1 Drive capability analysis
In the complex RS485 control environment, using the hardware method described above to control the direction of RS485 will have a problem of insufficient driving ability. Modify the above control method, change the 2-wire control on the TTL side to 3-wire control, that is, the transceiver control signal is not controlled by the current /TXD, and a GPIO line is separated from the master to control the transmission and reception.
According to the output current calculation, the 3-wire control mode is similar to the 2-wire controlled bus pull-down as the output mode, and its driving capability is improved by 25~50 times (different models of different manufacturers), if it is supplemented by the flexible configuration of the terminal resistor The driving capability of RS485 will not be a problem at all. Table 1 is a comparison of the driving abilities of the two control modes.
2.2 hardware and software environment
Figure 2 Hardware design in the software control method
The software control method uses the hardware design of Figure 2. The prominent modification in the figure is to use the GPIO of the MCU to control the power supply of the RE and DE.RS485 chips with 5 V power supply to improve the driving capability. The RE and DE controls of the RS485 chip are controlled using the MCU's GPIO output high and low. Simply put, when RS485 is used for data transmission, the transmission direction is controlled by GPIO. The MCU used here is TI's DM8168 processor to implement the RS485 switching function of the software. The software version uses UBoot2010.06 and linux2.6.37. Use software to realize RS485 transmission and reception, try to ensure the execution efficiency; to achieve the above purpose, you need to debug the serial port driver, use the software resources used by the serial port driver and the hardware resources of the serial port controller to realize RS485 control.
Table 1 Comparison of software and hardware control mode drive capabilities
2.3 UBoot code modification
Files that need to be modified:
1 board/TI/TI8168/evm.c
2 drivers/serial/ns16550.c
3 include/configs/TI8168_evm.h
Add a switch macro definition in the ti8168_evm.h file:
#define CONFIG_RS485_DIR_SW 1
Add a switch function in the evm.c file:
Void rs485_dir_sw(int rs485_dir){
If (rs485_dir ==0)
_raw_writel(RS485_DIR_MASK, TI81XX_GPIO1_CLEARDATAOUT);
Else
_raw_writel(RS485_DIR_MASK, TI81XX_GPIO1_SETDATAOUT);
}
Add RS485 direction control to the s16550.c serial port driver file:
Void NS16550_putc(NS16550_t com_port, char c){
#ifdef CONFIG_RS485_DIR_SW
Rs485_dir_sw(1);
#endif
...//The code here is omitted
#ifdef CONFIG_RS485_DIR_SW
While((serial_in(&com_port-》lsr) & UART_LSR_TEMT) == 0)
Rs485_dir_sw(0);
#endif
}
Where UART_LSR_TEMT indicates that the transmit BUF and the shift register are empty. By default, RS485 is the receiving state. Once the data is to be sent, RS485 is switched to the transmitting state. After sending the data, wait for the send BUF and shift register to be empty, then switch back to the receive state, there is no need to use timeout.
2.4 Linux code modification
Files that need to be modified:
1 arch/arm/machomap2/bordti8168evm.c
2 drivers/serial/omapserial.c
3 include/linux/serial_core.h
Serial_core.h file, add the set_rs485_direction function pointer in the uart_port structure to perform RS485 direction switching void (*set_rs485_direction)(int rs485_dir); originally considered to be added in the uart_ops structure, but this structure is a constant type, It is unchanged, so it is added to the uart_port structure. Add related macro definitions and function pointer types to the file for function registration:
#define SET_RS485_RX0
#define SET_RS485_TX1
Typedef void (*set_rs485_direction_t)(int rs485_dir);//for function registration
The omapserial.c file mainly made the following changes:
1 Add the function pointer of the omap_rs485_dir_fun global.
Static set_rs485_direction_t omap_rs485_dir_fun[OMAP_MAX_HSUART_PORTS]={NULL, NULL, NULL, NULL, NULL, NULL}
2 The external driver uses the omap_rs485_dir_fun_reg registration function to assign the omap_rs485_dir_fun.
Void omap_rs485_dir_fun_reg(int port_num, set_rs485_direction_t rs485_dir_fun){
If (port_num)=OMAP_MAX_HSUART_PORTS)
Printk(KERN_ERR "%s, port_num error max is %d, but %d \", __FUNCTION__, OMAP_MAX_HSUART_PORTS-1, port_num);
Omap_rs485_dir_fun[port_num]= rs485_dir_fun;
}
EXPORT_SYMBOL(omap_rs485_dir_fun_reg);
The serial_omap_probe function assigns the up-port.set_rs485_direction used in the control program.
Up-"port.set_rs485_direction= omap_rs485_dir_fun[pdev-"id];
4 By default RS485 is in the receiving state.
5 The serial_omap_enable_ier_thri function switches RS485 to the transmit state.
Static incline void serial_omap_enable_ier_thri(struct uart_omap_port *up){
If (!(up-》ier & UART_IER_THRI)) {
/* rs485 dir change to tx */
If (up-"port.set_rs485_direction != NULL)
Up-"port.set_rs485_direction(SET_RS485_TX);
...//The code here is omitted
}
}
6 serial_omap_stop_tx function switches RS485 to receive state.
Static void serial_omap_stop_tx(struct uart_omap_port *port){
...//The code here is omitted
If (up-》ier & UART_IER_THRI) {
Up-"ier &= ~UART_IER_THRI;
Serial_out(up, UART_IER, up-"ier);
/* rs485 dir change to rx */
If (port-"set_rs485_direction != NULL)
Port-"set_rs485_direction(SET_RS485_RX);
}
}
7 transmit_chars change, the original code is to turn off the transmission interrupt when there are no more characters to send (the ring buffer is empty), then the serial controller sends the BUF and the shift register still has data, these data serial port control After the automatic transmission is completed, it is finished. Since the transmission interrupt has been turned off, no interruption occurs after the transmission is completed. However, the RS485 switching direction needs to wait until the complete transmission is completed. Therefore, the transmit_chars function has been modified. Before calling the serial_omap_stop_tx function, it is judged whether the transmitting BUF and the shift register are empty. If it is empty, the direction can be switched. In short, the shutdown time of the transmission interrupt is delayed.
Static void transmit_chars(struct uart_omap_port *up){
...//The code here is omitted
If (uart_circ_empty(xmit) || uart_tx_stopped(&up-》port)) {
If (up-"port.ops-"tx_empty(&up-"port)==0)
Return;//added for latest transmit
Serial_omap_stop_tx(&up-"port);
Return;
}
...//The code here is omitted
If (uart_circ_empty(xmit)) {
If (up-"port.ops-"tx_empty(&up-"port)==0)
Return;//added for latest transmit
Serial_omap_stop_tx(&up-"port);
}
}
The 8 arch/arm/machomap2/boardti8168evm.c file calls the omap_rs485_dir_fun_reg function in the ti8168_evm_init function to register the RS485 switch function.
2.5 Analysis of experimental results
The above software modification has the following advantages: no increase in hardware overhead; no increase or use of any hardware resources; no increase in software overhead; no impact on software execution efficiency; hardware control is electrical signal control, direction switching and TX binding; software control is After the entire transmission buffer is sent and then the direction is switched, the control implementation is more reasonable.
Basic testing of software switching RS485, the situation is as follows:
1 Console operation. The entire startup print information is normal. UBoot and Kernel have the same control effect and hardware control, and can smoothly input and echo commands. The serial terminal can add configuration delays after inputting characters. The kernel tests OK at 115 200 and 38 400 respectively.
2 Under the kernel, it is responsible for sending large amounts of data. Increase load, open multiple ping packet processes (generate a large number of interrupts), Nand Flash operation, CPU occupancy rate close to 100%, output a large amount of data through RS485, no garbled, check OK.
3 Very high real-time.
Since the software implementation given in this paper is based on the Linux kernel, the real-time control of the direction is well guaranteed. The actual results show that the time between the completion of the DM8168 data transmission and the generation of the direction control signal is about 25 μs, which is almost negligible. Some designs that use the application to switch direction in user space can cause delays of more than 20 ms, resulting in a series of anomalies.
Conclusion
This paper describes in detail the hardware and software implementation of RS485 direction control. The two control modes have their own characteristics. The hardware control mode is simple to implement and does not require software intervention. For software, RS485 serial port transmission and reception is as simple as RS232. The software control method can greatly improve the driving ability of the entire RS485 line. The control method based on the Linux kernel given in this paper can guarantee the real-time performance of RS485 direction switching and meet the practical requirements. These two methods have been well applied and verified in many occasions. In particular, the software implementation can be extended to more applications, such as the complex multi-master and multi-slave RS485 use environment. The software control can realize different data flow of the entire RS485 line according to its own needs, and can circumvent a certain device. The interference to the abnormal signal on the RS485 link brings a lot of convenience to the practical application.
encapsulated power transformer,EI low frequency transformer,Industrial transformer,power supply transformer,alarm system transformer
IHUA INDUSTRIES CO.,LTD. , https://www.ihua-inductor.com