/*
|
/*
|
|
*This file is part of fpga_gpib_controller.
|
|
*
|
|
* Fpga_gpib_controller is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Fpga_gpib_controller is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Fpga_gpib_controller. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
/*
|
* RegAccess.c
|
* RegAccess.c
|
*
|
*
|
* Created on: 2012-01-28
|
* Created on: 2012-01-28
|
* Author: Administrator
|
* Author: Andrzej Paluch
|
*/
|
*/
|
|
|
#include <windows.h>
|
#include <windows.h>
|
|
|
#include "GpibRegAccess.h"
|
#include "GpibRegAccess.h"
|
|
|
|
|
bool GpibRegAccess_init(struct GpibRegAccess *ra)
|
bool GpibRegAccess_init(struct GpibRegAccess *ra)
|
{
|
{
|
const char *portName = "COM3";
|
const char *portName = "COM3";
|
|
|
ra->isBurstMode = false;
|
ra->isBurstMode = false;
|
|
|
ra->portHandle = (HandleType)CreateFile(
|
ra->portHandle = (HandleType)CreateFile(
|
portName,
|
portName,
|
GENERIC_READ | GENERIC_WRITE,
|
GENERIC_READ | GENERIC_WRITE,
|
0,
|
0,
|
NULL,
|
NULL,
|
OPEN_EXISTING,
|
OPEN_EXISTING,
|
FILE_ATTRIBUTE_NORMAL,
|
FILE_ATTRIBUTE_NORMAL,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
COMMTIMEOUTS timeouts;
|
COMMTIMEOUTS timeouts;
|
|
|
timeouts.ReadIntervalTimeout = 5000;
|
timeouts.ReadIntervalTimeout = 5000;
|
timeouts.ReadTotalTimeoutMultiplier = 2;
|
timeouts.ReadTotalTimeoutMultiplier = 2;
|
timeouts.ReadTotalTimeoutConstant = 5000;
|
timeouts.ReadTotalTimeoutConstant = 5000;
|
timeouts.WriteTotalTimeoutMultiplier = 1;
|
timeouts.WriteTotalTimeoutMultiplier = 1;
|
timeouts.WriteTotalTimeoutConstant= 5000;
|
timeouts.WriteTotalTimeoutConstant= 5000;
|
|
|
SetCommTimeouts(
|
SetCommTimeouts(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
&timeouts
|
&timeouts
|
);
|
);
|
|
|
DCB commSetting;
|
DCB commSetting;
|
|
|
memset(&commSetting, 0, sizeof(DCB));
|
memset(&commSetting, 0, sizeof(DCB));
|
|
|
commSetting.DCBlength = sizeof(DCB);
|
commSetting.DCBlength = sizeof(DCB);
|
commSetting.BaudRate = 921600;
|
commSetting.BaudRate = 921600;
|
commSetting.fBinary = TRUE;
|
commSetting.fBinary = TRUE;
|
commSetting.fParity = FALSE;
|
commSetting.fParity = FALSE;
|
commSetting.fOutxCtsFlow = FALSE;
|
commSetting.fOutxCtsFlow = FALSE;
|
commSetting.fOutxDsrFlow = FALSE;
|
commSetting.fOutxDsrFlow = FALSE;
|
commSetting.fDtrControl = DTR_CONTROL_DISABLE;
|
commSetting.fDtrControl = DTR_CONTROL_DISABLE;
|
commSetting.fDsrSensitivity = FALSE;
|
commSetting.fDsrSensitivity = FALSE;
|
commSetting.fTXContinueOnXoff = FALSE;
|
commSetting.fTXContinueOnXoff = FALSE;
|
commSetting.fOutX = FALSE;
|
commSetting.fOutX = FALSE;
|
commSetting.fInX = FALSE;
|
commSetting.fInX = FALSE;
|
commSetting.fErrorChar = FALSE;
|
commSetting.fErrorChar = FALSE;
|
commSetting.fNull = FALSE;
|
commSetting.fNull = FALSE;
|
commSetting.fRtsControl = RTS_CONTROL_DISABLE;
|
commSetting.fRtsControl = RTS_CONTROL_DISABLE;
|
commSetting.fAbortOnError = TRUE;
|
commSetting.fAbortOnError = TRUE;
|
commSetting.ByteSize = 8;
|
commSetting.ByteSize = 8;
|
commSetting.Parity = NOPARITY;
|
commSetting.Parity = NOPARITY;
|
commSetting.StopBits = ONESTOPBIT;
|
commSetting.StopBits = ONESTOPBIT;
|
|
|
SetCommState(
|
SetCommState(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
&commSetting
|
&commSetting
|
);
|
);
|
}
|
}
|
|
|
return INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle;
|
return INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle;
|
}
|
}
|
|
|
void GpibRegAccess_release(struct GpibRegAccess *ra)
|
void GpibRegAccess_release(struct GpibRegAccess *ra)
|
{
|
{
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
CloseHandle((HANDLE)ra->portHandle);
|
CloseHandle((HANDLE)ra->portHandle);
|
}
|
}
|
}
|
}
|
|
|
bool GpibRegAccess_readReg(struct GpibRegAccess *ra, SizeType addr, RegType *value)
|
bool GpibRegAccess_readReg(struct GpibRegAccess *ra, SizeType addr, RegType *value)
|
{
|
{
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
unsigned char realAddr = (unsigned char)addr | 0x80;
|
unsigned char realAddr = (unsigned char)addr | 0x80;
|
DWORD bytesWrittenRead;
|
DWORD bytesWrittenRead;
|
bool result;
|
bool result;
|
|
|
result = WriteFile(
|
result = WriteFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
&realAddr,
|
&realAddr,
|
1,
|
1,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != 1)
|
if(!result || bytesWrittenRead != 1)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
|
|
result = ReadFile(
|
result = ReadFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
value,
|
value,
|
2,
|
2,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != 2)
|
if(!result || bytesWrittenRead != 2)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
else
|
else
|
{
|
{
|
return true;
|
return true;
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|
bool GpibRegAccess_repeatedlyRead(struct GpibRegAccess *ra, SizeType addr,
|
bool GpibRegAccess_repeatedlyRead(struct GpibRegAccess *ra, SizeType addr,
|
char *buf, SizeType bufLen)
|
char *buf, SizeType bufLen)
|
{
|
{
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
if(!ra->isBurstMode)
|
if(!ra->isBurstMode)
|
{
|
{
|
ra->isBurstMode = true;
|
ra->isBurstMode = true;
|
}
|
}
|
|
|
unsigned char realAddr = (unsigned char)addr | 0xc0;
|
unsigned char realAddr = (unsigned char)addr | 0xc0;
|
DWORD bytesWrittenRead;
|
DWORD bytesWrittenRead;
|
char addrBuf[3];
|
char addrBuf[3];
|
bool result;
|
bool result;
|
|
|
addrBuf[0] = realAddr;
|
addrBuf[0] = realAddr;
|
addrBuf[1] = bufLen & 0xFF;
|
addrBuf[1] = bufLen & 0xFF;
|
addrBuf[2] = (bufLen >> 8) & 0xFF;
|
addrBuf[2] = (bufLen >> 8) & 0xFF;
|
|
|
result = WriteFile(
|
result = WriteFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
addrBuf,
|
addrBuf,
|
3,
|
3,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != 3)
|
if(!result || bytesWrittenRead != 3)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
|
|
bytesWrittenRead = 0;
|
bytesWrittenRead = 0;
|
|
|
result = ReadFile(
|
result = ReadFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
buf,
|
buf,
|
bufLen,
|
bufLen,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != bufLen)
|
if(!result || bytesWrittenRead != bufLen)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
|
|
return true;
|
return true;
|
}
|
}
|
else
|
else
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|
bool GpibRegAccess_writeReg(struct GpibRegAccess *ra, SizeType addr, RegType value)
|
bool GpibRegAccess_writeReg(struct GpibRegAccess *ra, SizeType addr, RegType value)
|
{
|
{
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
unsigned char realAddr = (unsigned char)addr & 0x7F;
|
unsigned char realAddr = (unsigned char)addr & 0x7F;
|
DWORD writeBuf;
|
DWORD writeBuf;
|
DWORD bytesWrittenRead;
|
DWORD bytesWrittenRead;
|
bool result;
|
bool result;
|
|
|
writeBuf = realAddr;
|
writeBuf = realAddr;
|
writeBuf |= value << 8;
|
writeBuf |= value << 8;
|
|
|
result = WriteFile(
|
result = WriteFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
&writeBuf,
|
&writeBuf,
|
3,
|
3,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != 3)
|
if(!result || bytesWrittenRead != 3)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
else
|
else
|
{
|
{
|
return true;
|
return true;
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|
bool GpibRegAccess_repeatedlyWrite(struct GpibRegAccess *ra, SizeType addr,
|
bool GpibRegAccess_repeatedlyWrite(struct GpibRegAccess *ra, SizeType addr,
|
char *buf, SizeType bufLen)
|
char *buf, SizeType bufLen)
|
{
|
{
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
|
{
|
{
|
unsigned char realAddr = (unsigned char)addr | 0x40;
|
unsigned char realAddr = (unsigned char)addr | 0x40;
|
DWORD bytesWrittenRead;
|
DWORD bytesWrittenRead;
|
char addrBuf[3];
|
char addrBuf[3];
|
bool result;
|
bool result;
|
|
|
addrBuf[0] = realAddr;
|
addrBuf[0] = realAddr;
|
addrBuf[1] = bufLen & 0xFF;
|
addrBuf[1] = bufLen & 0xFF;
|
addrBuf[2] = (bufLen >> 8) & 0xFF;
|
addrBuf[2] = (bufLen >> 8) & 0xFF;
|
|
|
result = WriteFile(
|
result = WriteFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
addrBuf,
|
addrBuf,
|
3,
|
3,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != 3)
|
if(!result || bytesWrittenRead != 3)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
|
|
result = WriteFile(
|
result = WriteFile(
|
(HANDLE)ra->portHandle,
|
(HANDLE)ra->portHandle,
|
buf,
|
buf,
|
bufLen,
|
bufLen,
|
&bytesWrittenRead,
|
&bytesWrittenRead,
|
NULL
|
NULL
|
);
|
);
|
|
|
if(!result || bytesWrittenRead != bufLen)
|
if(!result || bytesWrittenRead != bufLen)
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
|
|
return true;
|
return true;
|
}
|
}
|
else
|
else
|
{
|
{
|
return false;
|
return false;
|
}
|
}
|
}
|
}
|
|
|