/*******************************************************************************
project: radicon scscs pc driver (i486)
module: kisl1.c
authors: (c) 1993, 94 by Radicon Ltd.
revised 2000 for Institut fuer Informatik, HU-Berlin by
Sebastian Luehnsdorf (luehnsdo@informatik.hu-berlin.de)
Alexander Schad (schad@informatik.hu-berlin.de)
*******************************************************************************/
/*******************************************************************************
module initialization
*******************************************************************************/
/* include standard header files */
#include <stdio.h>
/* include custom header files */
#include "dfkisl.h"
#include "prkmpt1.h"
#include "radicon.h"
/* include FakeDevice support */
extern int FakeDevice;
extern int FakeData;
#ifdef DEBUG
extern int retspot_set;
extern int retspot_beg;
extern int retspot_get;
extern int retspot_init;
extern int retspot_tr;
extern int retspot_rc;
extern int retspot_out;
extern int retspot_in;
#endif // def DEBUG
/* buffer for the program (scs.prg) 2032 Bytes (0x7f0)*/
unsigned char bufprg[LNGPRG];
/* buffer receive/transmit message */
unsigned char bufmsg[LNGMSG];
/*******************************************************************************
procedure: setprm
purpose: upload parameters to controller
synopsis: int CALLTYPE setprm( rd, rs, cph, cpl, ve, ftmr, imp, intg, snd,
ntrp )
input: int rd = data port address, (data register)
int rs = control port address, that identifies controller (state
register)
unsigned short cph = upper threshold
unsigned short cpl = lower threshold
int ve = high voltage
double ftmr = exposure time
unsigned long imp = impulse count
int intg = energy integral
int snd = sound
int ntrp = interrupt
result: 0 = operation successful
-1 = communication error
-2 = controller can't make this function to present time or error
code function
-3 = parameter error
******************************************************************************/
#ifdef DEBUG
int retspot_set;
#endif // def DEBUG
int CALLTYPE setprm( rd, rs, cph, cpl, ve, ftmr, imp, intg, snd, ntrp )
int rs, rd;
unsigned short cph, cpl;
int ve;
double ftmr;
unsigned long imp;
int intg;
int snd;
int ntrp;
{
/* local variables */
register int i, j;
unsigned long wtmr;
#ifdef DEBUG
retspot_set = retspot_beg = retspot_get = retspot_init = retspot_tr =
retspot_rc = retspot_out = retspot_in = 0;
#endif // def DEBUG
/* do nothing, if using FakeDevice */
if (FakeDevice)
return 0;
/* format energy integral */
bufmsg[0] = (unsigned char) intg;
/* format sound */
bufmsg[1] = (unsigned char) snd;
/* format high voltage do bitshift to get next Byte*/
bufmsg[7] = (unsigned char) ve;
ve = (ve >> 8) & 0xff;
bufmsg[8] = (unsigned char) ve;
/* format lower threshold do bitshift to get next Byte*/
bufmsg[9] = (unsigned char) cpl;
cpl = (cpl >> 8) & 0xff;
bufmsg[10] = (unsigned char) cpl;
/* format upper threshold do bitshift to get next Byte*/
bufmsg[11] = (unsigned char) cph;
cph = (cph >> 8) & 0xff;
bufmsg[12] = (unsigned char) cph;
/* determine and format total counts do bitshift to get next Byte*/
wtmr = ftmr * KMGHZ;
bufmsg[13] = (wtmr & 0xff);
bufmsg[14] = ((wtmr >> 8) & 0xff);
bufmsg[15] = (wtmr >> 16) & 0xff;
bufmsg[16] = (wtmr >> 24) & 0xff;
/* format impulse count do bitshift to get next Byte*/
bufmsg[17] = (imp & 0xff);
bufmsg[18] = ((imp >> 8) & 0xff);
bufmsg[19] = (imp >> 16) & 0xff;
bufmsg[20] = (imp >> 24) & 0xff;
/* format interrupt */
bufmsg[21] = (ntrp & 0xff);
/* transmit message for NCYCL cycles or until return value is 0 */
for ( i = 0; i < NCYCL; i++ )
{
j = tr_message( CF0, rd, rs, bufmsg, LNGMSG_TR );
if (j == 0)
break;
if (j == -2)
{
#ifdef DEBUG
retspot_set = 1;
#endif // def DEBUG
/* controller can't make this function to present time or error code
function */
return -2;
}
}
/* if max number of cycles reached and return value is not 0 */
if (j != 0)
{
#ifdef DEBUG
retspot_set = 2;
#endif // def DEBUG
return -1;
}
#ifdef DEBUG
retspot_set = 0;
#endif // def DEBUG
/* exit positively */
return 0;
}
/*******************************************************************************
procedure: begwrk
purpose: issue command to controller
synopsis: int CALLTYPE begwrk( fun, rd, rs )
input: fun = code of command to issue:
1 = count impulses during exposure time
2 = count time when number of impulses is set
3 = intensimeter
5 = stop controller
int rd = data port address, (data register)
int rs = control port address, that identifies controller (state
register)
result: 0 = operation successful
-1 = communication error
-2 = controller can't make this function to present time or error
code function
-3 = parameter error (unknown command)
*******************************************************************************/
#ifdef DEBUG
int retspot_beg;
#endif // def DEBUG
int CALLTYPE begwrk( fun, rd, rs )
int fun, rs, rd;
{
register int i, j;
/* buffer of receive/transmit message */
unsigned char bufmsg[LNGMSG];
#ifdef DEBUG
retspot_set = retspot_beg = retspot_get = retspot_init = retspot_tr =
retspot_rc = retspot_out = retspot_in = 0;
#endif // def DEBUG
/* do nothing, if using FakeDevice */
if (FakeDevice)
return 0;
/* depending on the command to issue... (function code filter) */
switch (fun)
{
/* count impulses during exposure time */
case 1:
/* count time when number of impulses is set */
case 2:
/* intensimeter */
case 3:
/* stop controller */
case 5:
/* transmit message NCYCL cycles or until return value is 0 */
for ( i = 0; i < NCYCL; i++ )
{
j = tr_message( fun, rd, rs, bufmsg, 0 );
if (j == 0)
break;
if (j == -2)
{
#ifdef DEBUG
retspot_beg = 1;
#endif // def DEBUG
return -2;
}
}
/* if unknown return code */
if (j != 0)
{
#ifdef DEBUG
retspot_beg = 2;
#endif // def DEBUG
/* return error code */
return -1;
}
#ifdef DEBUG
retspot_beg = 0;
#endif // def DEBUG
/* exit positively */
return 0;
/* unknown command */
default :
#ifdef DEBUG
retspot_beg = 3;
#endif // def DEBUG
/* return error code */
return -3;
}
}
/*******************************************************************************
procedure: getinf
purpose: get information from controller
synopsis: int CALLTYPE getinf( fun, rd, rs, ftmr, imp )
input: int fun = function code
int rd = data port address, (data register)
int rs = control port address, that identifies controller (state
register)
double ftmr = exposure time
unsigned long imp = impulse count
result: 0 = operation successful
-1 = communication error
-2 = controller can't make this function to present time or error
code function
-3 = parameter error
-4 = data isn't ready
******************************************************************************/
#ifdef DEBUG
int retspot_get;
#endif // def DEBUG
int CALLTYPE getinf( fun, rd, rs, ftmr, imp )
int fun, rd, rs;
double * ftmr;
unsigned long * imp;
{
register int i, j;
double wftmr;
unsigned long wimp, wtmr, ww;
unsigned char * p;
int l;
/* buffer receive message */
unsigned char bufmsg[LNGMSG];
#ifdef DEBUG
retspot_set = retspot_beg = retspot_get = retspot_init = retspot_tr =
retspot_rc = retspot_out = retspot_in = 0;
#endif // def DEBUG
if (FakeDevice)
{
*ftmr = 0.0;
*imp = 0;
return 0;
}
switch (fun)
{
/* receive result time and impulses */
case 9:
/* receive current time and impulses */
case 0x0b:
for ( i = 0; i < NCYCL; i++ )
{
l = 0;
j = rc_message( fun, rd, rs, bufmsg, LNGMSG, &l );
if ( j == 0 )
break;
if ( j == -2 )
{
#ifdef DEBUG
retspot_get = 1;
#endif // def DEBUG
return -2;
}
if ( j == -4 )
{
#ifdef DEBUG
retspot_get = 2;
#endif // def DEBUG
return -4;
}
}
if ( j != 0 )
{
#ifdef DEBUG
retspot_get = 3;
#endif // def DEBUG
/* error communicate */
return -1;
}
/* read number of counts do Bitshifts to cycle through
the four Bytes of the bufmsg that represent the longint*/
p = bufmsg;
wtmr = 0;
ww = 0;
ww = *p;
wtmr = wtmr + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 8;
wtmr = wtmr + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 16;
wtmr = wtmr + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 24;
wtmr = wtmr + ww;
p++;
/* turn number of counts into exposure time */
wftmr = wtmr;
*ftmr = wftmr / (KMGHZ);
/* read impulse count do Bitshifts to cycle through
the four Bytes of the bufmsg that represent the longint*/
wimp = 0;
ww = 0;
ww = *p;
wimp = wimp + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 8;
wimp = wimp + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 16;
wimp = wimp + ww;
p++;
ww = 0;
ww = *p;
ww = ww << 24;
wimp = wimp + ww;
*imp = wimp;
#ifdef DEBUG
retspot_get = 0;
#endif // def DEBUG
/* exit positively */
return 0;
/* parameter error */
default :
#ifdef DEBUG
retspot_get = 4;
#endif // def DEBUG
return -3;
}
}
/*******************************************************************************
procedure: init_b
purpose: load programm to monitor
synopsis: int CALLTYPE init_b ( pBuf, BufSize, rd, rs )
input: unsigned char * pBuf = buffer for data to transmit
unsigned int BufSize = number of bytes to transmit
int rd = data port address, (data register)
int rs = control port address, that identifies controller (state
register)
result: 0 = operation successful
-1 = communication error
-2 = controller reject connect
-3 = buffer length bigger than maximum length
*******************************************************************************/
#ifdef DEBUG
int retspot_init;
#endif // def DEBUG
int CALLTYPE init_b ( pBuf, BufSize, rd, rs )
unsigned char * pBuf;
unsigned int BufSize;
int rd, rs;
{
/* local variables */
register int i, j;
int d;
#ifdef DEBUG
retspot_set = retspot_beg = retspot_get = retspot_init = retspot_tr =
retspot_rc = retspot_out = retspot_in = 0;
#endif // def DEBUG
/* do nothing, if using FakeDevice */
if (FakeDevice)
return 0;
/* fail with error code -3, if file is bigger than LNGPRG bytes */
if (BufSize > LNGPRG)
{
{
#ifdef DEBUG
retspot_init = 1;
#endif // def DEBUG
return -3;
}
}
/* retrieve current time */
i = crnt_time();
/* timeout after reset controller for TMOUT * 50 mseconds */
while (1)
{
j = crnt_time();
if (j - i < 0)
i = j;
if (j - i > TMOUT)
break;
}
#ifndef __WIN32__
/* d = inp(rd); */
_asm
{
sub ax, ax; // initialize ax
mov dx, rd; // copy data out of rd in dx (16 Bit)
/* get data from port stored in dx and write data to the lower Byte (8 Bit)
of the accumulator */
in al, dx;
mov d, ax; // copy data out of ax (16 Bit) to d
}
#endif // ndef __WIN32__
/* stop controller */
begwrk ( CF5, rd, rs );
/* transmit message NCYCL cycles or until return value is 0 */
for ( i = 0; i < NCYCL; i++ )
{
j = tr_message( CFF, rd, rs, pBuf, BufSize );
if ( j == -2 )
{
#ifdef DEBUG
retspot_init = 2;
#endif // def DEBUG
return -2;
}
if ( j == 0 )
break;
}
/* if max number of cycles reached return error */
if ( i == NCYCL )
{
#ifdef DEBUG
retspot_init = 3;
#endif // def DEBUG
return -1;
}
/* ? */
if ( d == MONIT )
{
i = in_byte( rd, rs, &d );
if ( d == PROGR )
{
#ifdef DEBUG
retspot_init = 0;
#endif // def DEBUG
return 0;
}
}
#ifdef DEBUG
retspot_init = 0;
#endif // def DEBUG
return 0;
}
/*******************************************************************************
procedure: reset
purpose: reset controller
synopsis: void CALLTYPE reset( rs )
input: int rs = port address, that identifies controller (state register)
result: nothing
*******************************************************************************/
void CALLTYPE reset( rs )
int rs;
{
/* do nothing, if using FakeDevice */
if (FakeDevice)
return;
#ifndef __WIN32__
/* first write state register (port address, that identifies controller) into
data register. then write lower byte of accumulator register to the data
register */
_asm {
mov dx, rs;
out dx, al;
}
#endif // ndef __WIN32__
}
// __LastLine__
|