
TME-104-CLR-86DX-R0V2.doc
Rev. 0.2
39(
44
)
RS232 / RS485 switching
The operation mode of the serial interfaces COM1 and COM2 can be switched between RS232 and
RS422/RS485. Therefore you need GPIO06 and GPIO07 (GPIO port 0 bits 6 and 7).
The following example shows how to switch:
#include
<
sys
/
io
.
h
>
#include
<
stdio
.
h
>
#include
<
stdlib
.
h
>
#define
GPIO0_DATA 0x78
#define
GPIO0_DIR 0x98
//GPIO06 defines RS mode of COM1:
#define
COM1_RS232 0x00
//GPIO06=0 --> RS232
#define
COM1_RS485 0x40
//GPIO06=1 --> RS485
//GPIO07 defines RS mode of COM2:
#define
COM2_RS232 0x00
//GPIO07=0 --> RS232
#define
COM2_RS485 0x80
//GPIO07=1 --> RS485
int
main
()
{
unsigned char
dir
=
0
,
data
=
0
;
if
(
iopl
(
3
) !=
0
)
{
printf
(
"IOPL error
\
n"
);
return
1
;
//seems, you are not root!
}
dir
=
inb
(
GPIO0_DIR
);
//get direction bits of GPIO bank 0
outb
(
dir
|
0xC0
,
GPIO0_DIR
);
//set GPIO06 and GPIO07 to output
data
=
inb
(
GPIO0_DATA
);
data
&=
0x3F
;
//reset GPIO06 and GPIO07 to 0
data
|=
COM1_RS485
|
COM2_RS232
;
//set modes for COM1(RS485) and COM2(RS232)
outb
(
data
,
GPIO0_DATA
);
return
0
;
}
Note:
Please note that this source code example is done for a system running with
Linux. For other operation system it may be necessary to adapt the source code
regarding include files or headers and the syntax of I/O out commands because
Linux is using outb(value, address) instead of outb(address, value).