-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibgw.c
183 lines (155 loc) · 3.47 KB
/
libgw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "libgw.h"
int fd;
int data_coming = FALSE ;
void flush_uart_data(void)
{
char buffer[MAX_BUFFER_SIZE];
bzero(buffer, MAX_BUFFER_SIZE);
sprintf(buffer, "\r\n\r\n");
write(fd, buffer, strlen(buffer));
}
void updfrm_command(char* cmd)
{
char buffer[MAX_BUFFER_SIZE];
bzero(buffer, MAX_BUFFER_SIZE);
sprintf(buffer, "%s\r\n",cmd);
write(fd, buffer, strlen(buffer));
}
void choose_upload_ebl(void)
{
write(fd, "1", 1);
}
void choose_run_ebl(void)
{
char buffer[MAX_BUFFER_SIZE] ;
char cmd[] = "2\r\n";
bzero(buffer, MAX_BUFFER_SIZE);
memcpy(buffer, cmd, sizeof(cmd));
write(fd, buffer, strlen(buffer));
}
void dbg(char* filename, int linenum, char* fmt, ...)
{
#ifdef RSX_DEBUG
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
return;
#endif
}
void uart_handler(int status)
{
//can not do the work as receive_uart_data()
//because SIGIO always occuring
//and ohter house keeping work will not execute
data_coming = TRUE;
}
void receive_uart_data(void)
{
int len;
char ch;
time_t start;
time_t end;
if(data_coming == FALSE)
{
return;
}
start = time(NULL);
while(data_coming)
{
len = read(fd, &ch , 1);
if(len>0)
{
printf("%c",ch);
}
end = time(NULL);
if((end - start) > 2)
{
data_coming = FALSE;
return;
}
}
data_coming = FALSE;
return;
}
void init_serial_FW(char* device)
{
struct termios oldtio,newtio;
struct sigaction saio;
//fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0)
{
printf("OPEN RD FAILED, EXIT PROGRAM");
exit(1);
}else{
printf("OPEN RD : %s OK!!\r\n", device);
}
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
saio.sa_handler = uart_handler;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
//fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETFL, FASYNC);
newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD | CRTSCTS;
newtio.c_iflag = IGNPAR; //ignore parity error
newtio.c_oflag = 0;
newtio.c_lflag = 0;//ICANON;
newtio.c_cc[VMIN]=0;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIOFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
}
void init_serial_noFW(char* device)
{
struct termios oldtio,newtio;
struct sigaction saio;
//fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0)
{
printf("OPEN RD FAILED, EXIT PROGRAM");
exit(1);
}else{
printf("OPEN RD : %s OK!!\r\n", device);
}
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
saio.sa_handler = uart_handler;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
//fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETFL, FASYNC);
newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR; //ignore parity error
newtio.c_oflag = 0;
newtio.c_lflag = 0;//ICANON;
newtio.c_cc[VMIN]=0;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIOFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
}
void close_uart_port(void)
{
close(fd);
}