With the continuous expansion of ARM processor applications, ARM has introduced an increasing number of peripherals to meet diverse requirements. Common communication interfaces include RS232, RS485, CAN, and Ethernet. Among these, RS485 is widely used in industrial environments due to its long transmission distance, strong anti-interference capability, and low cost. This system utilizes the S3C2440 ARM9 processor’s built-in UART peripherals and an RSM485 module to create an isolated RS485 bus interface that includes power isolation, electrical isolation, and bus protection. By modifying the embedded Linux system's RS232 driver, the serial port driver can automatically control the GPIO pins to manage the direction of RS485 communication, simplifying the overall control flow. The RS485 communication program enables interaction with other devices on the bus by reading and writing to the serial port.
The hardware design of the communication interface leverages the rich peripheral resources integrated into the S3C2440 processor. In this design, UART0 serves as the console interface for the embedded Linux operating system, UART1 functions as an RS232 interface for communication with other RS232 devices, and UART3 is used for RS485 data communication. Since the voltage levels of the ARM9 processor differ from those of the RS485 standard—where differential signals are used (positive logic: +2 to +6V represents “0,†and negative logic: -6 to -2V represents “1â€)—external level conversion chips are necessary. To ensure reliability in harsh industrial environments, factors such as power isolation, electrical isolation, and bus protection must be considered. Therefore, the RSM485 module from Guangzhou Zhou Ligong was selected for this design.
The RSM485 isolated transceiver module integrates power isolation, electrical isolation, an RS485 interface chip, and bus protection features. It uses potting technology to achieve excellent isolation performance, with an isolation voltage of up to 2500VDC, support for up to 400 nodes, and a maximum communication baud rate of 115200.
Figure 1 shows a half-duplex RS485 bus setup using UART2 of the S3C2440 in the system. Data can only be transmitted in one direction at a time. The CON pin controls the direction of the chip data. It is connected to an IO pin of the S3C2440, and its level determines whether the chip is in transmit or receive mode. When sending data, the pin is set to 0, and when receiving, it is set to 1.
[Image: S3C2440-485 interface]
In the software design, the RS485 communication process begins by opening the modified serial port 2 for RS485 communication and setting its parameters. Initially, the serial port is in receive mode. Then, a read command is sent to the first device on the bus to trigger a response. After initializing the S3C2440-485 interface, the select function waits for the device to return data within the user-defined timeout. If an exception occurs, the interface is reinitialized. If no data is received within the set time, the system waits for the next device and sends the next read command. Once data is received, it is parsed according to the user protocol, completing the master-slave communication.
[Image: RS485 communication software flow]
For the RS485 driver design, the S3C2440’s UART peripheral and the RSM485 module are utilized. Compared to the RS232 driver, the RS485 driver provides more control over the communication direction pin. In the Linux kernel, the serial port driver is modified to include code for controlling the direction pin. In the Linux 2.6.32 kernel, the relevant code is found in `linux-2.6.32.2/drivers/serial/samsung.c`. The modifications mainly involve three parts:
1. **Initialization of the RS485 direction control pin**:
```c
if (port->line == 2) {
s3c2410_gpio_cfgpin(S3C2410_GPH0, S3C2410_GPH0_OUTP);
s3c2410_gpio_setpin(S3C2410_GPH0, 0);
}
```
These functions configure the pin as an output and set it to high initially, placing the serial port in receive mode.
2. **Setting the direction before transmission**:
```c
if (port->line == 2) {
s3c2410_gpio_setpin(S3C2410_GPH0, 1);
udelay(30);
}
```
A delay is added after changing the direction to allow the pin state to stabilize.
3. **Switching back to receive mode after transmission**:
```c
if (port->line == 2) {
while (!(rd_regl(port, S3C2410_UTRSTAT) & 0x04));
s3c2410_gpio_setpin(S3C2410_GPH0, 0);
}
```
This ensures the direction is set to receive after the data has been fully transmitted.
In the Linux environment, the RS485 interface can be operated like a regular serial port. Device files are located in `/dev`, and operations such as open, read, and write can be performed using standard file functions. Key code snippets include:
```c
int fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);
nread = read(fd, s1_buf, 64);
write(fd, send_buff, 6);
```
To handle waiting for responses from slave devices, the `select` function is used. This allows the system to wait for data within a specified timeout period. For example:
```c
switch (select(max_fd, &fds, NULL, NULL, &timeout)) {
case -1: break;
case 0:
// Timeout, poll again
break;
default:
if (FD_ISSET(fd1, &fds)) {
nread = read(fd1, s1_buf, 64);
// Process data
}
}
```
The design was successfully implemented in an environmental parameter acquisition system. The system consists of data acquisition nodes, collection terminals, and a data server. The nodes collect air pressure, temperature, and humidity data, which is then transmitted via the RS485 bus to the terminal. The terminal forwards the data to the server through Ethernet, where it is stored and made available for other applications. The master device polls all RS485 devices every second, sending a read command containing the slave device’s ID. If the data is corrupted, the packet is discarded, and the system waits for the next poll. Although occasional errors may occur, the design avoids complex kernel modifications and remains a practical solution.
Battery connector,battery holder,battery holder spring,power connector,connector
Dongguan Yiyou Electronic Technology Co., Ltd. , https://www.dsubminiature.com