forked from jsnyder/stm32ld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_win32.c
181 lines (157 loc) · 4.39 KB
/
serial_win32.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
// Serial inteface implementation for POSIX-compliant systems
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "type.h"
#include "serial.h"
#define WIN_ERROR ( HANDLE )-1
#define WIN_MAX_PORT_NAME 1024
// Helper: set timeout
static int ser_win32_set_timeouts( HANDLE hComm, DWORD ri, DWORD rtm, DWORD rtc, DWORD wtm, DWORD wtc )
{
COMMTIMEOUTS timeouts;
if( GetCommTimeouts( hComm, &timeouts ) == FALSE )
{
CloseHandle( hComm );
return SER_ERR;
}
timeouts.ReadIntervalTimeout = ri;
timeouts.ReadTotalTimeoutConstant = rtm;
timeouts.ReadTotalTimeoutMultiplier = rtc;
timeouts.WriteTotalTimeoutConstant = wtm;
timeouts.WriteTotalTimeoutMultiplier = wtc;
if( SetCommTimeouts( hComm, &timeouts ) == FALSE )
{
CloseHandle( hComm );
return SER_ERR;
}
return SER_OK;
}
// Open the serial port
ser_handler ser_open( const char* sername )
{
char portname[ WIN_MAX_PORT_NAME + 1 ];
HANDLE hComm;
portname[ 0 ] = portname[ WIN_MAX_PORT_NAME ] = '\0';
_snprintf( portname, WIN_MAX_PORT_NAME, "\\\\.\\%s", sername );
hComm = CreateFile( portname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if( hComm == INVALID_HANDLE_VALUE )
return WIN_ERROR;
if( !SetupComm( hComm, 2048, 2048 ) )
return WIN_ERROR;
return hComm;
}
// Close the serial port
void ser_close( ser_handler id )
{
CloseHandle( id );
}
int ser_setup( ser_handler id, u32 baud, int databits, int parity, int stopbits )
{
HANDLE hComm = ( HANDLE )id;
DCB dcb;
if( GetCommState( hComm, &dcb ) == FALSE )
{
CloseHandle( hComm );
return SER_ERR;
}
dcb.BaudRate = baud;
dcb.ByteSize = databits;
dcb.Parity = parity == SER_PARITY_NONE ? NOPARITY : ( parity == SER_PARITY_EVEN ? EVENPARITY : ODDPARITY );
dcb.StopBits = stopbits == SER_STOPBITS_1 ? ONESTOPBIT : ( stopbits == SER_STOPBITS_1_5 ? ONE5STOPBITS : TWOSTOPBITS );
dcb.fBinary = TRUE;
dcb.fDsrSensitivity = FALSE;
dcb.fParity = parity != SER_PARITY_NONE ? TRUE : FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fNull = FALSE;
/**/ dcb.fAbortOnError = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow = FALSE;
if( SetCommState( hComm, &dcb ) == 0 )
{
CloseHandle( hComm );
return SER_ERR;
}
if( ser_win32_set_timeouts( hComm, 0, 0, 0, 0, 0 ) == SER_ERR )
{
CloseHandle( hComm );
return SER_ERR;
}
FlushFileBuffers( hComm );
return SER_OK;
}
// Read up to the specified number of bytes, return bytes actually read
u32 ser_read( ser_handler id, u8* dest, u32 maxsize )
{
HANDLE hComm = ( HANDLE )id;
DWORD readbytes;
if( ReadFile( hComm, dest, maxsize, &readbytes, NULL ) == FALSE )
return 0;
return readbytes;
}
// 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 );
//printf( "READ %02X, res is %d\n", data, res );
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 )
{
HANDLE hComm = ( HANDLE )id;
DWORD written;
if( WriteFile( hComm, src, size, &written, NULL ) == FALSE )
return 0;
return written;
}
// Write a byte to the serial port
u32 ser_write_byte( ser_handler id, u8 data )
{
return ser_write( id, &data, 1 );
}
// Set communication timeout
void ser_set_timeout_ms( ser_handler id, u32 timeout )
{
if( timeout == SER_NO_TIMEOUT )
ser_win32_set_timeouts( id, MAXDWORD, 0, 0, 0, 0 );
else if( timeout == SER_INF_TIMEOUT )
ser_win32_set_timeouts( id, 0, 0, 0, 0, 0 );
else
ser_win32_set_timeouts( id, 0, 0, timeout, 0, 0 );
}
// entry sequence helper
int ser_ctl(ser_handler id, int bit) {
EscapeCommFunction(id, bit);
usleep(10000);
return 0;
}
// entry sequence
int ser_entry(ser_handler id, entry_type_t ent) {
if (ent == MAINBOARD_V1) {
// mainboard v1
ser_ctl(id, CLRRTS);
ser_ctl(id, CLRDTR);
ser_ctl(id, SETDTR);
usleep(500000);
} else if (ent == MBLC) {
ser_ctl(id, CLRRTS);
ser_ctl(id, CLRDTR);
ser_ctl(id, SETDTR);
usleep(300000);
ser_ctl(id, CLRDTR);
} else if (ent == MAINBOARD_V2) {
usleep(100000);
ser_ctl(id, SETRTS);
usleep(200000);
ser_ctl(id, CLRRTS);
usleep(100000);
}
return 0;
}