-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial_posix.c
184 lines (155 loc) · 4 KB
/
serial_posix.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
// Serial inteface implementation for POSIX-compliant systems
#include "serial.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
static u32 ser_timeout = SER_INF_TIMEOUT;
// Open the serial port
ser_handler ser_open( const char* sername )
{
int fd;
if( ( fd = open( sername, O_RDWR | O_NOCTTY | O_NDELAY ) ) == -1 )
perror( "ser_open: unable to open port" );
else
fcntl( fd, F_SETFL, 0 );
return ( ser_handler )fd;
}
// Close the serial port
void ser_close( ser_handler id )
{
close( ( int )id );
}
// Helper function: get baud ID from actual baud rate
#define BAUDCASE(x) case x: return B##x
static u32 ser_baud_to_id( u32 baud )
{
switch( baud )
{
BAUDCASE( 1200 );
BAUDCASE( 1800 );
BAUDCASE( 2400 );
BAUDCASE( 4800 );
BAUDCASE( 9600 );
BAUDCASE( 19200 );
BAUDCASE( 38400 );
BAUDCASE( 57600 );
BAUDCASE( 115200 );
BAUDCASE( 230400 );
}
return 0;
}
// Helper function: get number of bits ID from actual number of bits
#define NBCASE(x) case x: return CS##x
static int ser_number_of_bits_to_id( int nb )
{
switch( nb )
{
NBCASE( 5 );
NBCASE( 6 );
NBCASE( 7 );
NBCASE( 8 );
}
return 0;
}
int ser_setup( ser_handler id, u32 baud, int databits, int parity, int stopbits )
{
struct termios termdata;
int hnd = ( int )id;
usleep( 200000 );
tcgetattr( hnd, &termdata );
// Baud rate
cfsetispeed( &termdata, ser_baud_to_id( baud ) );
cfsetospeed( &termdata, ser_baud_to_id( baud ) );
// Parity / stop bits
termdata.c_cflag &= ~CSTOPB;
if( parity == SER_PARITY_NONE ) // no parity
{
termdata.c_cflag &= ~PARENB;
}
else if( parity == SER_PARITY_EVEN ) // even parity
{
termdata.c_cflag |= PARENB;
termdata.c_cflag &= ~PARODD;
}
else if( parity == SER_PARITY_ODD ) // odd parity
{
termdata.c_cflag |= PARENB;
termdata.c_cflag |= PARODD;
}
// Data bits
termdata.c_cflag |= ( CLOCAL | CREAD );
termdata.c_cflag &= ~CSIZE;
termdata.c_cflag |= ser_number_of_bits_to_id( databits );
// Disable HW and SW flow control
termdata.c_cflag &= ~CRTSCTS;
termdata.c_iflag &= ~( IXON | IXOFF | IXANY );
// Raw input
termdata.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
// Raw output
termdata.c_oflag &= ~OPOST;
// Check and strip parity bit
if( parity == SER_PARITY_NONE )
termdata.c_iflag &= ~( INPCK | ISTRIP );
else
termdata.c_iflag |= ( INPCK | ISTRIP );
// Setup timeouts
termdata.c_cc[ VMIN ] = 1;
termdata.c_cc[ VTIME ] = 0;
// Set the attibutes now
tcsetattr( hnd, TCSANOW, &termdata );
// Flush everything
tcflush( hnd, TCIOFLUSH );
// And set blocking mode by default
fcntl( id, F_SETFL, 0 );
}
// Read up to the specified number of bytes, return bytes actually read
u32 ser_read( ser_handler id, u8* dest, u32 maxsize )
{
if( ser_timeout == SER_INF_TIMEOUT )
return ( u32 )read( ( int )id, dest, maxsize );
else
{
fd_set readfs;
struct timeval tv;
int retval;
FD_ZERO( &readfs );
FD_SET( ( int )id, &readfs );
tv.tv_sec = ser_timeout / 1000000;
tv.tv_usec = ( ser_timeout % 1000000 ) * 1000;
retval = select( ( int )id + 1, &readfs, NULL, NULL, &tv );
if( retval == -1 || retval == 0 )
return 0;
else
return ( u32 )read( ( int )id, dest, maxsize );
}
}
// Read a single byte and return it (or -1 for error)
int ser_read_byte( ser_handler id )
{
u8 data;
int res = ser_read( id, &data, 1 );
return res == 1 ? data : -1;
}
// Write up to the specified number of bytes, return bytes actually written
u32 ser_write( ser_handler id, const u8 *src, u32 size )
{
u32 res;
res = ( u32 )write( ( int )id, src, size );
return res;
}
// Write a byte to the serial port
u32 ser_write_byte( ser_handler id, u8 data )
{
return ( u32 )write( id, &data, 1 );
}
// Set communication timeout
void ser_set_timeout_ms( ser_handler id, u32 timeout )
{
ser_timeout = timeout;
}