]>
Commit | Line | Data |
---|---|---|
63295cb2 CS |
1 | RS485 SERIAL COMMUNICATIONS |
2 | ||
3 | 1. INTRODUCTION | |
4 | ||
5 | EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the | |
6 | electrical characteristics of drivers and receivers for use in balanced | |
7 | digital multipoint systems. | |
8 | This standard is widely used for communications in industrial automation | |
9 | because it can be used effectively over long distances and in electrically | |
10 | noisy environments. | |
11 | ||
12 | 2. HARDWARE-RELATED CONSIDERATIONS | |
13 | ||
de6f86ce YY |
14 | Some CPUs/UARTs (e.g., Atmel AT91 or 16C950 UART) contain a built-in |
15 | half-duplex mode capable of automatically controlling line direction by | |
16 | toggling RTS or DTR signals. That can be used to control external | |
17 | half-duplex hardware like an RS485 transceiver or any RS232-connected | |
18 | half-duplex devices like some modems. | |
63295cb2 CS |
19 | |
20 | For these microcontrollers, the Linux driver should be made capable of | |
21 | working in both modes, and proper ioctls (see later) should be made | |
22 | available at user-level to allow switching from one mode to the other, and | |
23 | vice versa. | |
24 | ||
25 | 3. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL | |
26 | ||
27 | The Linux kernel provides the serial_rs485 structure (see [1]) to handle | |
28 | RS485 communications. This data structure is used to set and configure RS485 | |
29 | parameters in the platform data and in ioctls. | |
30 | ||
31 | Any driver for devices capable of working both as RS232 and RS485 should | |
32 | provide at least the following ioctls: | |
33 | ||
34 | - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used | |
35 | to enable/disable RS485 mode from user-space | |
36 | ||
37 | - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used | |
38 | to get RS485 mode from kernel-space (i.e., driver) to user-space. | |
39 | ||
40 | In other words, the serial driver should contain a code similar to the next | |
41 | one: | |
42 | ||
43 | static struct uart_ops atmel_pops = { | |
44 | /* ... */ | |
45 | .ioctl = handle_ioctl, | |
46 | }; | |
47 | ||
48 | static int handle_ioctl(struct uart_port *port, | |
49 | unsigned int cmd, | |
50 | unsigned long arg) | |
51 | { | |
52 | struct serial_rs485 rs485conf; | |
53 | ||
54 | switch (cmd) { | |
55 | case TIOCSRS485: | |
56 | if (copy_from_user(&rs485conf, | |
57 | (struct serial_rs485 *) arg, | |
58 | sizeof(rs485conf))) | |
59 | return -EFAULT; | |
60 | ||
61 | /* ... */ | |
62 | break; | |
63 | ||
64 | case TIOCGRS485: | |
65 | if (copy_to_user((struct serial_rs485 *) arg, | |
66 | ..., | |
67 | sizeof(rs485conf))) | |
68 | return -EFAULT; | |
69 | /* ... */ | |
70 | break; | |
71 | ||
72 | /* ... */ | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | 4. USAGE FROM USER-LEVEL | |
78 | ||
79 | From user-level, RS485 configuration can be get/set using the previous | |
80 | ioctls. For instance, to set RS485 you can use the following code: | |
81 | ||
82 | #include <linux/serial.h> | |
83 | ||
84 | /* Driver-specific ioctls: */ | |
85 | #define TIOCGRS485 0x542E | |
86 | #define TIOCSRS485 0x542F | |
87 | ||
88 | /* Open your specific device (e.g., /dev/mydevice): */ | |
89 | int fd = open ("/dev/mydevice", O_RDWR); | |
90 | if (fd < 0) { | |
91 | /* Error handling. See errno. */ | |
92 | } | |
93 | ||
94 | struct serial_rs485 rs485conf; | |
95 | ||
96 | /* Set RS485 mode: */ | |
97 | rs485conf.flags |= SER_RS485_ENABLED; | |
98 | ||
99 | /* Set rts delay before send, if needed: */ | |
100 | rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND; | |
101 | rs485conf.delay_rts_before_send = ...; | |
102 | ||
103 | /* Set rts delay after send, if needed: */ | |
104 | rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; | |
105 | rs485conf.delay_rts_after_send = ...; | |
106 | ||
83cac9f3 BR |
107 | /* Set this flag if you want to receive data even whilst sending data */ |
108 | rs485conf.flags |= SER_RS485_RX_DURING_TX; | |
109 | ||
63295cb2 CS |
110 | if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) { |
111 | /* Error handling. See errno. */ | |
112 | } | |
113 | ||
114 | /* Use read() and write() syscalls here... */ | |
115 | ||
116 | /* Close the device when finished: */ | |
117 | if (close (fd) < 0) { | |
118 | /* Error handling. See errno. */ | |
119 | } | |
120 | ||
121 | 5. REFERENCES | |
122 | ||
123 | [1] include/linux/serial.h |