OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [comport/] [com_win.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/*
2
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15 3 sergeykhbr
 */
16
 
17
#include "api_types.h"
18
#include "api_core.h"
19
#include "attribute.h"
20
#include "comport.h"
21
#include <winspool.h>
22
#include <cstdlib>
23
 
24
namespace debugger {
25
 
26 4 sergeykhbr
void ComPortService::getListOfPorts(AttributeType *list) {
27 3 sergeykhbr
    list->make_list(0);
28
 
29
    AttributeType portInfo;
30
    DWORD  Ports_MemSize = 0;
31
    DWORD  Ports_Count   = 0;
32
    BYTE*  lpPorts       = NULL;
33
 
34
    //Getting Ports_MemSize value...
35
    EnumPorts(NULL,
36
              1,
37
              lpPorts,
38
              0,
39
              &Ports_MemSize,
40
              &Ports_Count);
41
 
42
 
43
    //Getting lpPorts...
44
    lpPorts = new BYTE[Ports_MemSize];
45
    EnumPorts(NULL,
46
              1,
47
              lpPorts,
48
              Ports_MemSize,
49
              &Ports_MemSize,
50
              &Ports_Count);
51
 
52
 
53
    //Forming List Of Ports...
54
    DWORD dw;
55
    char temp[4] = {0};
56
    int port = -1;
57
    PORT_INFO_1 *pPortInfo;
58
    pPortInfo = (PORT_INFO_1 *)lpPorts;
59
 
60
 
61
    char comName[16];
62
    char chCom[20];
63
    size_t szComLen;
64
    for (dw = 0; dw < Ports_Count; dw++) {
65
 
66
        if (strstr(pPortInfo->pName, "com") == 0) {
67
            continue;
68
        }
69
        temp[0] = pPortInfo->pName[3];
70
                if (pPortInfo->pName[4] != ':' && pPortInfo->pName[4] != '\0') {
71
                        temp[1] = pPortInfo->pName[4];
72
        }
73
        if (pPortInfo->pName[5] != ':' && pPortInfo->pName[5] != '\0') {
74
                    temp[2] = pPortInfo->pName[5];
75
        }
76
 
77
                port = strtoul(temp, NULL, 0);;
78
        szComLen = RISCV_sprintf(chCom, sizeof(chCom),
79
                                 "\\\\.\\COM%d", port) + 1;
80
 
81
 
82
                HANDLE h = CreateFile(chCom, GENERIC_READ | GENERIC_WRITE,
83
                              0, NULL, OPEN_EXISTING,
84
                              FILE_ATTRIBUTE_NORMAL,NULL);
85
 
86
                if (h == INVALID_HANDLE_VALUE) {
87
            pPortInfo++;
88
                    continue;
89
                }
90
 
91
        portInfo.make_dict();
92
        RISCV_sprintf(comName, sizeof(comName), "'COM%d'", port);
93
        portInfo["id"].make_string(comName);
94
                list->add_to_list(&portInfo);
95
 
96
        CloseHandle(h);
97
        pPortInfo++;
98
    }
99
 
100
    delete [] lpPorts;
101
}
102
 
103 4 sergeykhbr
int ComPortService::openPort(const char *port, AttributeType settings) {
104 3 sergeykhbr
    char chCom[20];
105
    char chConfig[64];
106
    HANDLE hFile;
107
    COMMPROP CommProp;
108
    DCB dcb;
109
    COMMTIMEOUTS CommTimeOuts;
110
    DWORD dwStoredFlags;
111
    DWORD  Errors;
112
    COMSTAT  Stat;
113
 
114
    RISCV_sprintf(chCom, sizeof(chCom), "\\\\.\\%s", port);
115
    RISCV_sprintf(chConfig, sizeof(chConfig),
116 4 sergeykhbr
                  "baud=%d parity=N data=8 stop=1", settings[0u].to_int());
117 3 sergeykhbr
 
118
    hFile = CreateFile(chCom,
119
                        GENERIC_READ|GENERIC_WRITE,
120
                        0,//FILE_SHARE_READ|FILE_SHARE_WRITE,
121
                        NULL,
122
                            //OPEN_ALWAYS,
123
                        OPEN_EXISTING,
124
                        FILE_ATTRIBUTE_NORMAL,
125
                        NULL);
126
 
127 4 sergeykhbr
    prtHandler_ = hFile;
128 3 sergeykhbr
    if (hFile == INVALID_HANDLE_VALUE) {
129
        if (GetLastError() == ERROR_ACCESS_DENIED) {
130
            RISCV_error("%s is locked by another device", chCom);
131
        } else {
132
            RISCV_error("Can't open port %s", chCom);
133
        }
134
            return -1;
135
    }
136
 
137
    // Read capabilities:
138
    GetCommProperties(hFile, &CommProp);
139
    FillMemory(&dcb, sizeof(dcb), 0);
140
 
141
    dcb.DCBlength = sizeof(dcb);
142
    if (!BuildCommDCB(chConfig, &dcb)) {
143
        RISCV_error("Can't BuildCommDCB(%s,)", chConfig);
144
        CloseHandle(hFile);
145
        return -1;
146
    }
147
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
148
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
149
 
150
    Sleep(100);
151
    if (!SetCommState(hFile, &dcb)) {
152
        RISCV_error("Can't set port %s state", chCom);
153
        CloseHandle(hFile);
154
        return -1;
155
    }
156
 
157
#if 0
158
    /** ...A value of MAXDWORD , combined with zero values for both
159
     * the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members,
160
     * specifies that the read operation is to return immediately with the
161
     * characters that have already been received, even if no characters
162
     * have been received...
163
     **/
164
    CommTimeOuts.ReadIntervalTimeout             = MAXDWORD;
165
    CommTimeOuts.ReadTotalTimeoutMultiplier  = 0;
166
    CommTimeOuts.ReadTotalTimeoutConstant    = 0;
167
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
168
    CommTimeOuts.WriteTotalTimeoutConstant   = 0;
169
#else
170
    CommTimeOuts.ReadIntervalTimeout             = MAXDWORD;
171
    CommTimeOuts.ReadTotalTimeoutMultiplier  = MAXDWORD;//0;
172
    CommTimeOuts.ReadTotalTimeoutConstant    = 100;
173
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
174
    CommTimeOuts.WriteTotalTimeoutConstant   = 0;//1000;
175
#endif
176
 
177
    if(!SetCommTimeouts(hFile, &CommTimeOuts)) {
178
        RISCV_error("Can't set port %s timeouts", chCom);
179
        CloseHandle(hFile);
180
        return -1;
181
    }
182
 
183
    dwStoredFlags = EV_BREAK | EV_CTS  | EV_DSR | EV_ERR | EV_RING |
184
                EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY;
185
    if(!SetCommMask(hFile, dwStoredFlags)) {
186
        RISCV_error("Can't set mask %s", chCom);
187
        CloseHandle(hFile);
188
        return -1;
189
    }
190
 
191
    RISCV_info("Serial port %s opened", chCom);
192
 
193
    RISCV_sleep_ms(100);
194
    ClearCommError(hFile, &Errors, &Stat);
195
    PurgeComm(hFile, PURGE_RXCLEAR | PURGE_TXCLEAR);
196
    PurgeComm(hFile, PURGE_RXABORT | PURGE_TXABORT);
197
 
198
    return 0;
199
}
200
 
201 4 sergeykhbr
void ComPortService::closePort() {
202
    if (prtHandler_) {
203
        CloseHandle(*static_cast<HANDLE *>(prtHandler_));
204
    }
205
    prtHandler_ = 0;
206 3 sergeykhbr
}
207
 
208
int ComPortService::readSerialPort(void *hdl, char *buf, int bufsz) {
209
    HANDLE hFile = *static_cast<HANDLE *>(hdl);
210
    DWORD dwBytesRead;
211
    BOOL success = ReadFile(hFile, buf, bufsz, &dwBytesRead, NULL);
212
    if (!success) {
213
        return -1;
214
    }
215 4 sergeykhbr
    buf[dwBytesRead] = 0;
216 3 sergeykhbr
    return static_cast<int>(dwBytesRead);
217
}
218
 
219
int ComPortService::writeSerialPort(void *hdl, char *buf, int bufsz) {
220
    DWORD lpdwBytesWrittens;
221
    WriteFile(*static_cast<HANDLE *>(hdl),
222
                buf, bufsz, &lpdwBytesWrittens, NULL);
223
    return (int)lpdwBytesWrittens;
224
}
225
 
226
void ComPortService::cleanSerialPort(void *hdl) {
227
    PurgeComm(*static_cast<HANDLE *>(hdl), PURGE_TXCLEAR|PURGE_RXCLEAR);
228
}
229
 
230
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.