forked from jheising/node.pcduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.cpp
executable file
·357 lines (322 loc) · 7.35 KB
/
Serial.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Arduino.h"
#include "wiring_private.h"
#include <termios.h>
#include "Serial.h"
HwSerial Serial;
void serialEvent() __attribute__((weak));
#define serialEvent_implemented
void serialEvent()
{
Serial.process_recv();
}
void serialEventRun(void)
{
#ifdef serialEvent_implemented
serialEvent();
#endif
}
static const char* serial_name = "/dev/ttyS1";
static inline char get_databit(byte config)
{
switch (config)
{
case SERIAL_5N1:
case SERIAL_5N2:
case SERIAL_5E1:
case SERIAL_5E2:
case SERIAL_5O1:
case SERIAL_5O2:
return CS5;
case SERIAL_6N1:
case SERIAL_6N2:
case SERIAL_6E1:
case SERIAL_6E2:
case SERIAL_6O1:
case SERIAL_6O2:
return CS6;
case SERIAL_7N1:
case SERIAL_7N2:
case SERIAL_7E1:
case SERIAL_7E2:
case SERIAL_7O1:
case SERIAL_7O2:
return CS7;
case SERIAL_8N1:
case SERIAL_8N2:
case SERIAL_8E1:
case SERIAL_8E2:
case SERIAL_8O1:
case SERIAL_8O2:
default:
return CS8;
}
}
static inline char get_stopbit(byte config)
{
switch (config)
{
case SERIAL_5N2:
case SERIAL_6N2:
case SERIAL_7N2:
case SERIAL_8N2:
case SERIAL_5E2:
case SERIAL_6E2:
case SERIAL_7E2:
case SERIAL_8E2:
case SERIAL_5O2:
case SERIAL_6O2:
case SERIAL_7O2:
case SERIAL_8O2:
return 2;
case SERIAL_5N1:
case SERIAL_6N1:
case SERIAL_7N1:
case SERIAL_8N1:
case SERIAL_5E1:
case SERIAL_6E1:
case SERIAL_7E1:
case SERIAL_8E1:
case SERIAL_5O1:
case SERIAL_6O1:
case SERIAL_7O1:
case SERIAL_8O1:
default:
return 1;
}
}
static inline char get_parity(byte config)
{
switch (config)
{
case SERIAL_5N1:
case SERIAL_5N2:
case SERIAL_6N1:
case SERIAL_6N2:
case SERIAL_7N1:
case SERIAL_7N2:
case SERIAL_8N1:
case SERIAL_8N2:
default:
return 'N';
case SERIAL_5O1:
case SERIAL_5O2:
case SERIAL_6O1:
case SERIAL_6O2:
case SERIAL_7O1:
case SERIAL_7O2:
case SERIAL_8O1:
case SERIAL_8O2:
return 'O';
case SERIAL_5E1:
case SERIAL_5E2:
case SERIAL_6E1:
case SERIAL_6E2:
case SERIAL_7E1:
case SERIAL_7E2:
case SERIAL_8E1:
case SERIAL_8E2:
return 'E';
}
}
static inline int get_valid_baud(unsigned long speed)
{
switch (speed)
{
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 14400:
return 0;
case 19200:
return B19200;
case 28800:
return 0;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
default:
return 0;
}
}
//static void signal_handler_IO ()
//{
// serial.process_recv();
// serial.process_send();
//}
//under construct
HwSerial::HwSerial()
{
_rx_buffer.head = _rx_buffer.tail = 0;
_tx_buffer.head = _tx_buffer.tail = 0;
_fd = -1;
}
HwSerial::~HwSerial()
{
end();
}
void HwSerial::begin(unsigned long baud, byte config)
{
int ret;
struct termios Opt;
//struct sigaction saio;
hw_pinMode(GPIO0, IO_UART_FUNC); //uart_rx
hw_pinMode(GPIO1, IO_UART_FUNC); //uart_tx
_fd = open(serial_name, O_RDWR| O_NOCTTY | O_NONBLOCK );
if (_fd < 0 )
{
pabort("can't open tty");
}
tcgetattr(_fd, &Opt);
tcflush(_fd, TCIOFLUSH);
int rate = get_valid_baud(baud);
if(rate > 0)
{
cfsetispeed(&Opt, rate);
cfsetospeed(&Opt, rate);
}
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= get_databit(config);
switch (get_parity(config))
{
case 'N':
default:
Opt.c_cflag &= ~PARENB;
Opt.c_iflag &= ~INPCK;
break;
case 'O':
Opt.c_cflag |= (PARODD | PARENB);
Opt.c_iflag |= INPCK;
break;
case 'E':
Opt.c_cflag |= PARENB;
Opt.c_cflag &= ~PARODD;
Opt.c_iflag |= INPCK;
break;
}
switch (get_stopbit(config))
{
case 1:
default:
Opt.c_cflag &= ~CSTOPB;
break;
case 2:
Opt.c_cflag |= CSTOPB;
break;
}
//Opt.c_cflag |= (CLOCAL | CREAD);
Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//Opt.c_lflag &= ~(ECHO);
//Opt.c_cc[VTIME] = 0;
//Opt.c_cc[VMIN] = 0;
//saio.sa_handler = signal_handler_IO;
//sigemptyset (&(saio.sa_mask));
//saio.sa_flags = 0;
//saio.sa_restorer = NULL;
//sigaction (SIGIO, &saio, NULL);
//if (-1 == fcntl (_fd, F_SETFL, O_ASYNC))
// pabort("SETFL SIGIO");
//if (-1 == fcntl (_fd, F_SETOWN, getpid ()))
// pabort("F_SETOWN SIGIO");
ret = tcsetattr(_fd, TCSANOW, &Opt);
if (ret != 0)
pabort("can't set tty baut");
tcflush(_fd,TCIOFLUSH);
}
void HwSerial::end()
{
if(_fd)
close(_fd);
_fd = -1;
}
int HwSerial::available(void)
{
if(_rx_buffer.head <= _rx_buffer.tail)
return (_rx_buffer.tail - _rx_buffer.head);
else
return (SERIAL_BUFFER_SIZE - _rx_buffer.head + _rx_buffer.tail);
}
int HwSerial::peek(void)
{
if (_rx_buffer.head == _rx_buffer.tail) {
return -1;
} else {
return _rx_buffer.buffer[_rx_buffer.head];
}
}
int HwSerial::read(void)
{
if (_rx_buffer.head == _rx_buffer.tail) {
return -1;
} else {
unsigned char c = _rx_buffer.buffer[_rx_buffer.head];
_rx_buffer.head = (unsigned int)(_rx_buffer.head + 1) % SERIAL_BUFFER_SIZE;
return c;
}
}
void HwSerial::flush()
{
_rx_buffer.head = _rx_buffer.tail = 0;
_tx_buffer.head = _tx_buffer.tail = 0;
//::flush(_fd);
tcflush(_fd, TCIOFLUSH);
}
int HwSerial::write(byte c)
{
return (::write(_fd, &c ,1));
}
int HwSerial::process_recv()
{
int len = SERIAL_BUFFER_SIZE - available();
int retval = 0;
if ( _fd < 0 )
return -1;
if(len > 0)
{
//fd_set fs_read;
//struct timeval tv_timeout={_timeout/1000,(_timeout%1000)*1000};
//FD_ZERO (&fs_read);
//FD_SET (_fd, &fs_read);
//retval = select (_fd+1, &fs_read, NULL, NULL, &tv_timeout);
//if (retval>0)
{
//if(FD_ISSET(_fd,&fs_read))
{
char buf[SERIAL_BUFFER_SIZE];
retval = ::read(_fd, buf, len);
if(retval > 0)
{
if(retval >= SERIAL_BUFFER_SIZE -_rx_buffer.tail)
{
memcpy(_rx_buffer.buffer + _rx_buffer.tail, buf, SERIAL_BUFFER_SIZE - _rx_buffer.tail);
memcpy(_rx_buffer.buffer, buf + SERIAL_BUFFER_SIZE - _rx_buffer.tail, retval - (SERIAL_BUFFER_SIZE - _rx_buffer.tail));
_rx_buffer.tail = retval - ( SERIAL_BUFFER_SIZE - _rx_buffer.tail);
}
else
{
memcpy(_rx_buffer.buffer + _rx_buffer.tail, buf, retval);
_rx_buffer.tail += retval;
}
}
}
}
}
return retval;
}
HwSerial::operator bool() {
return true;
}