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

Subversion Repositories gpib_controller

[/] [gpib_controller/] [trunk/] [prototype_1/] [PC_software/] [gpib_src/] [RegAccess_windows.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 Andrewski
/*
2 12 Andrewski
*This file is part of fpga_gpib_controller.
3
*
4
* Fpga_gpib_controller is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* Fpga_gpib_controller is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with Fpga_gpib_controller.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
/*
18 3 Andrewski
 * RegAccess.c
19
 *
20
 *  Created on: 2012-01-28
21 12 Andrewski
 *      Author: Andrzej Paluch
22 3 Andrewski
 */
23
 
24
#include <windows.h>
25
 
26
#include "GpibRegAccess.h"
27
 
28
 
29
bool GpibRegAccess_init(struct GpibRegAccess *ra)
30
{
31
        const char *portName = "COM3";
32
 
33
        ra->isBurstMode = false;
34
 
35
        ra->portHandle = (HandleType)CreateFile(
36
                portName,
37
                GENERIC_READ | GENERIC_WRITE,
38
                0,
39
                NULL,
40
                OPEN_EXISTING,
41
                FILE_ATTRIBUTE_NORMAL,
42
                NULL
43
        );
44
 
45
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
46
        {
47
                COMMTIMEOUTS timeouts;
48
 
49
                timeouts.ReadIntervalTimeout = 5000;
50
                timeouts.ReadTotalTimeoutMultiplier = 2;
51
                timeouts.ReadTotalTimeoutConstant = 5000;
52
                timeouts.WriteTotalTimeoutMultiplier = 1;
53
                timeouts.WriteTotalTimeoutConstant= 5000;
54
 
55
                SetCommTimeouts(
56
                        (HANDLE)ra->portHandle,
57
                        &timeouts
58
                );
59
 
60
                DCB commSetting;
61
 
62
                memset(&commSetting, 0, sizeof(DCB));
63
 
64
                commSetting.DCBlength = sizeof(DCB);
65
                commSetting.BaudRate = 921600;
66
                commSetting.fBinary = TRUE;
67
                commSetting.fParity = FALSE;
68
                commSetting.fOutxCtsFlow = FALSE;
69
                commSetting.fOutxDsrFlow = FALSE;
70
                commSetting.fDtrControl = DTR_CONTROL_DISABLE;
71
                commSetting.fDsrSensitivity = FALSE;
72
                commSetting.fTXContinueOnXoff = FALSE;
73
                commSetting.fOutX = FALSE;
74
                commSetting.fInX = FALSE;
75
                commSetting.fErrorChar = FALSE;
76
                commSetting.fNull = FALSE;
77
                commSetting.fRtsControl = RTS_CONTROL_DISABLE;
78
                commSetting.fAbortOnError = TRUE;
79
                commSetting.ByteSize = 8;
80
                commSetting.Parity = NOPARITY;
81
                commSetting.StopBits = ONESTOPBIT;
82
 
83
                SetCommState(
84
                        (HANDLE)ra->portHandle,
85
                        &commSetting
86
                );
87
        }
88
 
89
        return INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle;
90
}
91
 
92
void GpibRegAccess_release(struct GpibRegAccess *ra)
93
{
94
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
95
        {
96
                CloseHandle((HANDLE)ra->portHandle);
97
        }
98
}
99
 
100
bool GpibRegAccess_readReg(struct GpibRegAccess *ra, SizeType addr, RegType *value)
101
{
102
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
103
        {
104
                unsigned char realAddr = (unsigned char)addr | 0x80;
105
                DWORD bytesWrittenRead;
106
                bool result;
107
 
108
                result = WriteFile(
109
                                (HANDLE)ra->portHandle,
110
                        &realAddr,
111
                        1,
112
                        &bytesWrittenRead,
113
                        NULL
114
                );
115
 
116
                if(!result || bytesWrittenRead != 1)
117
                {
118
                        return false;
119
                }
120
 
121
                result = ReadFile(
122
                                (HANDLE)ra->portHandle,
123
                        value,
124
                        2,
125
                        &bytesWrittenRead,
126
                        NULL
127
                );
128
 
129
                if(!result || bytesWrittenRead != 2)
130
                {
131
                        return false;
132
                }
133
                else
134
                {
135
                        return true;
136
                }
137
        }
138
        else
139
        {
140
                return false;
141
        }
142
}
143
 
144
bool GpibRegAccess_repeatedlyRead(struct GpibRegAccess *ra, SizeType addr,
145
                char *buf, SizeType bufLen)
146
{
147
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
148
        {
149
                if(!ra->isBurstMode)
150
                {
151
                        ra->isBurstMode = true;
152
                }
153
 
154
                unsigned char realAddr = (unsigned char)addr | 0xc0;
155
                DWORD bytesWrittenRead;
156
                char addrBuf[3];
157
                bool result;
158
 
159
                addrBuf[0] = realAddr;
160
                addrBuf[1] = bufLen & 0xFF;
161
                addrBuf[2] = (bufLen >> 8) & 0xFF;
162
 
163
                result = WriteFile(
164
                        (HANDLE)ra->portHandle,
165
                        addrBuf,
166
                        3,
167
                        &bytesWrittenRead,
168
                        NULL
169
                );
170
 
171
                if(!result || bytesWrittenRead != 3)
172
                {
173
                        return false;
174
                }
175
 
176
                bytesWrittenRead = 0;
177
 
178
                result = ReadFile(
179
                        (HANDLE)ra->portHandle,
180
                        buf,
181
                        bufLen,
182
                        &bytesWrittenRead,
183
                        NULL
184
                );
185
 
186
                if(!result || bytesWrittenRead != bufLen)
187
                {
188
                        return false;
189
                }
190
 
191
                return true;
192
        }
193
        else
194
        {
195
                return false;
196
        }
197
}
198
 
199
bool GpibRegAccess_writeReg(struct GpibRegAccess *ra, SizeType addr, RegType value)
200
{
201
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
202
        {
203
                unsigned char realAddr = (unsigned char)addr & 0x7F;
204
                DWORD writeBuf;
205
                DWORD bytesWrittenRead;
206
                bool result;
207
 
208
                writeBuf = realAddr;
209
                writeBuf |= value << 8;
210
 
211
                result = WriteFile(
212
                                (HANDLE)ra->portHandle,
213
                        &writeBuf,
214
                        3,
215
                        &bytesWrittenRead,
216
                        NULL
217
                );
218
 
219
                if(!result || bytesWrittenRead != 3)
220
                {
221
                        return false;
222
                }
223
                else
224
                {
225
                        return true;
226
                }
227
        }
228
        else
229
        {
230
                return false;
231
        }
232
}
233
 
234
bool GpibRegAccess_repeatedlyWrite(struct GpibRegAccess *ra, SizeType addr,
235
                char *buf, SizeType bufLen)
236
{
237
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
238
        {
239
                unsigned char realAddr = (unsigned char)addr | 0x40;
240
                DWORD bytesWrittenRead;
241
                char addrBuf[3];
242
                bool result;
243
 
244
                addrBuf[0] = realAddr;
245
                addrBuf[1] = bufLen & 0xFF;
246
                addrBuf[2] = (bufLen >> 8) & 0xFF;
247
 
248
                result = WriteFile(
249
                        (HANDLE)ra->portHandle,
250
                        addrBuf,
251
                        3,
252
                        &bytesWrittenRead,
253
                        NULL
254
                );
255
 
256
                if(!result || bytesWrittenRead != 3)
257
                {
258
                        return false;
259
                }
260
 
261
                result = WriteFile(
262
                        (HANDLE)ra->portHandle,
263
                        buf,
264
                        bufLen,
265
                        &bytesWrittenRead,
266
                        NULL
267
                );
268
 
269
                if(!result || bytesWrittenRead != bufLen)
270
                {
271
                        return false;
272
                }
273
 
274
                return true;
275
        }
276
        else
277
        {
278
                return false;
279
        }
280
}

powered by: WebSVN 2.1.0

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