-
Notifications
You must be signed in to change notification settings - Fork 3
/
serial.c
72 lines (42 loc) · 1013 Bytes
/
serial.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
#include <sys/types.h>
#include <libsio.h>
#include "serial.h"
void ser_Init(int baud) {
_sio_control(1, 2, MR_SB_01 | MR_CHLEN_8 | 0x02); /* 8bit, no-parity, 1 stop-bit */
_sio_control(1, 3, baud);
_sio_control(1, 1, CR_RXEN | CR_TXEN); /* RTS:off DTR:off */
// Flush UART clean
while(1) {
int tc = 0;
while(!(_sio_control(0, 0, 0) & SR_RXRDY)) {
if (tc > SER_TIMEOUT)
return;
tc++;
}
_sio_control(0, 4, 0);
}
}
int ser_SendBytes(void* buff, int bytes) {
int i;
for(i=0; i<bytes; i++) {
while((_sio_control(0, 0, 0) & (SR_TXU|SR_TXRDY)) != (SR_TXU|SR_TXRDY));
_sio_control(1, 4, ((u_char*)buff)[i]);
}
return(bytes);
}
int ser_ReadBytes(void* buff, int bytes) {
int tc;
int bytesRead = 0;
char* c = (char*)buff;
while(bytesRead < bytes) {
tc = 0;
while(!(_sio_control(0, 0, 0) & SR_RXRDY)) {
if (tc > SER_TIMEOUT)
return(bytesRead);
tc++;
}
*c = _sio_control(0, 4, 0); c++;
bytesRead++;
}
return(bytesRead);
}