1
|
#include <linux/serial.h>
|
2
|
#include <stdio.h>
|
3
|
#include <stdlib.h>
|
4
|
#include <fcntl.h>
|
5
|
#include <termios.h>
|
6
|
|
7
|
/* Driver-specific ioctls: */
|
8
|
#define TIOCGRS485 0x542E
|
9
|
#define TIOCSRS485 0x542F
|
10
|
|
11
|
/* new defines in linux/serial.h of CL provided kernel */
|
12
|
#ifndef SER_RS485_USE_GPIO
|
13
|
#define SER_RS485_USE_GPIO (1<<5)
|
14
|
#define gpio_pin padding[0]
|
15
|
#endif
|
16
|
|
17
|
/* Convert GPIO signal to GPIO pin number */
|
18
|
#define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))
|
19
|
|
20
|
|
21
|
#define GATERO_UART1_RS485EN GPIO_TO_PIN(0,12)
|
22
|
#define GATERO_UART2_RS485EN GPIO_TO_PIN(0,13)
|
23
|
#define GATERO_UART3_RS485EN GPIO_TO_PIN(1,31)
|
24
|
#define GATERO_UART4_RS485EN GPIO_TO_PIN(1,30)
|
25
|
|
26
|
|
27
|
/*
|
28
|
* Test program for 485 control
|
29
|
*/
|
30
|
int main(int argc, char* argv[])
|
31
|
{
|
32
|
struct serial_rs485 rs485conf;
|
33
|
char buffer[20], buffer2[21],uartname[10];
|
34
|
int i, rv, opt, uartnr,gpio_txen;
|
35
|
struct termios my_termios, new_termios;
|
36
|
if (argc > 1)
|
37
|
{
|
38
|
uartnr = atoi(argv[1]);
|
39
|
sprintf(uartname, "/dev/ttyO%d", uartnr);
|
40
|
switch(uartnr)
|
41
|
{
|
42
|
case 1: gpio_txen = GATERO_UART1_RS485EN; break;
|
43
|
case 2: gpio_txen = GATERO_UART2_RS485EN; break;
|
44
|
case 3: gpio_txen = GATERO_UART3_RS485EN; break;
|
45
|
case 4: gpio_txen = GATERO_UART4_RS485EN; break;
|
46
|
default: printf("The command had out of range uartNr: Only 1 to 4 available.\n"); exit(1);
|
47
|
}
|
48
|
}
|
49
|
else
|
50
|
{
|
51
|
printf("The command had no other arguments. Usage: %s uartNr\n", argv[0]); exit(1);
|
52
|
}
|
53
|
|
54
|
int fd0 = open(uartname, O_RDWR | O_NDELAY | O_NOCTTY);
|
55
|
|
56
|
rs485conf.flags = (1<<0); //SER_RS485_ENABLED;
|
57
|
rs485conf.flags |= SER_RS485_USE_GPIO;
|
58
|
rs485conf.delay_rts_before_send = 0;
|
59
|
rs485conf.gpio_pin = gpio_txen;
|
60
|
|
61
|
rv = ioctl (fd0, TIOCSRS485, &rs485conf);
|
62
|
if (rv) {
|
63
|
printf("rv = %d\n", rv);
|
64
|
perror("unable to set IOCTL:");
|
65
|
}
|
66
|
|
67
|
tcgetattr(fd0, &my_termios);
|
68
|
my_termios.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
|
69
|
my_termios.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);
|
70
|
my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG | ECHOK | ECHOCTL | ECHOPRT);
|
71
|
my_termios.c_cflag &= ~(CSIZE | PARENB);
|
72
|
my_termios.c_cflag &= ~CRTSCTS;
|
73
|
my_termios.c_cflag |= CS8 | CLOCAL;
|
74
|
my_termios.c_cflag &= ~CBAUD;
|
75
|
my_termios.c_cflag |= B115200; //B115200; // B19200; //
|
76
|
my_termios.c_cc[VMIN] = 1;
|
77
|
my_termios.c_cc[VTIME] = 0;
|
78
|
rv = tcsetattr(fd0, TCSANOW, &my_termios);
|
79
|
tcgetattr(fd0, &new_termios);
|
80
|
|
81
|
|
82
|
for (i = 0; i < 20; i++) {
|
83
|
buffer[i] = 'A'+i;
|
84
|
}
|
85
|
|
86
|
for (i = 0; i < 1; i++) {
|
87
|
printf("Writing [%d]\n", i);
|
88
|
write(fd0, buffer, 15);
|
89
|
sleep(1);
|
90
|
}
|
91
|
close(fd0);
|
92
|
}
|