esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#%TITLE% candsa1000.mac
#%NAME% 
#  Set the parameters of the Canberra dsa1000 module
#
#%CATEGORY% Detection, mca
#
#%END%

#%UU% [mca_no]
#%MDESC% Setup the module, attached to the %B%mca_no%B% MCA as set in the
#SPEC config.
def candsa1000setup '{
global DSA1000_DEV[] DSA1000_OK[]
global DSA1000_NO DSA1000_BASE DSA1000_EXPERT
global CAN_HVPS CAN_GAIN CAN_FILTER CAN_STAT
global CAN_HVPSF CAN_GAINF CAN_FILTERF
local i inp mca_no dsa_no

  if ($#==0) {
    DSA1000_NO = getval ("How many DSA1000 MCA do you want to use?",DSA1000_NO)
    for (i=0; i<DSA1000_NO; i++) {
      mca_no[i] = getval("MCA number as in the config: ",mca_no[i])
    }
  } else {
    DSA1000_NO = (split("$*",inp))
    for (i=0; i<DSA1000_NO; i++) {
      mca_no[i] = inp[i]
    }
  }
  for (i=0; i<DSA1000_NO; i++) {
    DSA1000_OK[i] = _dsa1000setup(mca_no[i])
    if (DSA1000_OK[i]) {
      tty_cntl("md")
      eprintf("DSA 1000 #%d not configured. Check MCA device #%d.\n", i, \
		mca_no[i])
      tty_cntl("me")
    }
    CAN_STAT[i]["ok"] = 0
    CAN_HVPS[i]["readok"] = 0 
    CAN_HVPS[i]["checkok"] = 0
    CAN_GAIN[i]["readok"] = 0
    CAN_GAIN[i]["auxok"] = 0
    CAN_GAIN[i]["checkok"] = 0
    CAN_FILTER[i]["readok"] = 0
    CAN_FILTER[i]["checkok"] = 0
  
  }
  DSA1000_BASE = 0x20000000
  DSA1000_EXPERT = 0
}'

#%UI% (no)
#%MDESC% Get the device name, set to TCP and increase the timeout for the
#%B%no%B% mca number.
def _dsa1000setup(no) '{
  DSA1000_DEV[no] = ""

  DSA1000_DEV[no] = mca_spar(no,"device_id")
  if ((DSA1000_DEV[no] != -1) && (DSA1000_DEV[no] != "?")) {
    esrf_io(DSA1000_DEV[no],"tcp")
    esrf_io(DSA1000_DEV[no],"timeout",1000)
  } else
    return(-1)
  return(0)
}'

#%UI%
#%MDESC% List all the configured modules
def dsa1000list '{
local i
  for (i=0; i<DSA1000_NO; i++) { _dsa1000list(i) }
}'

#%UI% (no)
#%MDESC% Print the module %B%no%B%.
def _dsa1000list(no) '{

  if (!DSA1000_OK[no]) {
    tty_cntl("md"); printf("%2d: ", no); tty_cntl("me")
    printf("%s\n", DSA1000_DEV[no])
  }
}'

#%UU% [no]
#%MDESC% Start the menu for all the configured modules or modul #%B%no%B%.
def candsa1000menu '{
local dsa_no interact ok str

  if (!DSA1000_NO) {
    tty_cntl("md")
    printf("No DSA 1000 configured  !! Please, run candsa1000setup.")
    tty_cntl("me")
    exit
  }

  dsa_no = -1
  interact = ($# == 0)
  while (1) {
    ok = 1
    if (interact) {
      clscreen()
      tty_cntl("md")
      printf("\n\nDSA 1000 Devices Configured:\n\n")
      tty_cntl("me")
      dsa1000list
      while (++dsa_no < DSA1000_NO) {
	dsa_no = (dsa_no < DSA1000_NO) ? dsa_no : -1
      }
      str = "\n\tDSA 1000 number or -1 to Exit"
      dsa_no = getval(str, -1)
      if (dsa_no < 0)
	break  
    } else
      dsa_no = int($1)
    if ((dsa_no >= DSA1000_NO) || (DSA1000_OK[dsa_no])) {
      _msg(dsa_no, "Device not in spec config !!")
      ok = 0
    }
    if (ok)
      _dsa1000submenu(dsa_no)

    if (!interact)
      break
  }
}'

#%UI% (no)
#%MDESC% Display the parameters submenu for module %B%no%B%.
def _dsa1000submenu(no) '{

  CAN_STAT[no]["ok"] = _dsa1000stat(no)
  CAN_HVPS[no]["readok"] = _canHVPS_read(no)
  CAN_HVPS[no]["checkok"] = _canHVPS_check(no)
  CAN_GAIN[no]["readok"] = _canGain_read(no)
  CAN_GAIN[no]["auxok"] = _canGain_readaux(no)
  CAN_GAIN[no]["checkok"] = _canGain_check(no)
  CAN_FILTER[no]["readok"] = _canFilter_read(no)
  CAN_FILTER[no]["checkok"] = _canFilter_check(no)

  CAN_HVPSF[no] = 0
  CAN_GAINF[no] = 0
  CAN_FILTERF[no] = 0

  option = 1
  while (option) {
    clscreen()
    tty_cntl("md"); printf("\t\t< DSA1000 SETUP >\n"); tty_cntl("me")
    _dsa1000list(no)
    tty_cntl("us")
    printf("                                                        \n\n")
    tty_cntl("ue")

    #Setup
    if (CAN_STAT[no]["ok"] != 0) {
      tty_cntl("md")
      printf ("Attention! DSA1000 #%d:", no)
      tty_cntl("me")
      printf ("%s\n",CAN_STAT[no]["ok"])
    } else {
      printf("      Preamp output polarity. . . . . . . :")
      tty_cntl("md")
      printf("<%s>\n", CAN_STAT[no]["pamppol"]?"Negative":"Positive")
      tty_cntl("me")
      printf("      Preamp type . . . . . . . . . . . . :")
      tty_cntl("md"); printf("<%s>\n", CAN_STAT[no]["pamptype"]); tty_cntl("me")
      printf("      Pole/Zero . . . . . . . . . . . . . :")
      tty_cntl("md"); printf("<%s>\n", CAN_STAT[no]["pz"]); tty_cntl("me")
      printf("      Acquisitoion Mode . . . . . . . . . :")
      tty_cntl("md"); printf("<%s>\n", CAN_STAT[no]["acqmode"]); tty_cntl("me")
    }

    #HVPS
    if (CAN_HVPS[no]["readok"] != 0) {
      tty_cntl("md")
      printf ("Attention! DSA1000 #%d:", no)
      tty_cntl("me")
      printf ("%s\n", CAN_HVPS[no]["readok"])
    } else {
      printf("\n  1 - High Voltage:")
      if (CAN_HVPS[no]["checkok"] != 0) {
        tty_cntl("md")
        printf ("Attention! DSA1000 #%d:", no)
        tty_cntl("me")
        printf ("%s\n", CAN_HVPS[no]["checkok"])      
      }
      if (DSA1000_EXPERT)
        printf("\n      11 -")
      else
        printf("\n          ")
      printf(" Range . . . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s V>", CAN_HVPS[no]["range"])
      tty_cntl("me")
      if (DSA1000_EXPERT)
        printf("\n      12 -")
      else
        printf("\n          ")
      printf (" Voltage Limit . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s V>", CAN_HVPS[no]["vlim"])
      tty_cntl("me")
      printf("\n      13 - Voltage . . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s V>", CAN_HVPS[no]["v"])
      tty_cntl("me")
      if (DSA1000_EXPERT)
        printf("\n      14 -")
      else
        printf("\n          ")
      printf(" Inhibit Signal . . . . . . . . :")
      tty_cntl("md")
      printf("<%s>", CAN_HVPS[no]["inh"]?"Negative":"Positive")
      tty_cntl("me")
      printf("\n      15 - State . . . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s>", CAN_HVPS[no]["state"]?"On":"Off")
      tty_cntl("me")
    }

    #DSP Gain
    if (CAN_GAIN[no]["readok"] != 0) {
      tty_cntl("md")
      printf ("Attention! DSA1000 #%d:", no)
      tty_cntl("me")
      printf ("%s\n", CAN_GAIN[no]["readok"])
    } else if (CAN_GAIN[no]["auxok"] != 0) {
      tty_cntl("md")
      printf ("Attention! DSA1000 #%d:", no)
      tty_cntl("me")
      printf ("%s\n", CAN_GAIN[no]["auxok"])
    } else {
      printf("\n  2 - DSP Gain:")
      if (CAN_GAIN[no]["checkok"] != 0) {
        tty_cntl("md")
        printf ("Attention! DSA1000 #%d:", no)
        tty_cntl("me")
        printf ("%s\n", CAN_GAIN[no]["checkok"])
      }
      printf("\n      21 - Coarse Gain . . . . . . . . . .:")
      tty_cntl("md")
      printf("<x %s>", CAN_GAIN[no]["coarse"])
      tty_cntl("me")
      printf("\n      22 - Fine Gain . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<x %s>", CAN_GAIN[no]["fine"])
      tty_cntl("me")
      printf("\n      23 - S-fine Gain . . . . . . . . . .:")
      tty_cntl("md")
      printf("<x %s>", CAN_GAIN[no]["sfine"])
      tty_cntl("me")
      if (DSA1000_EXPERT)
        printf("\n      24 -")
      else
        printf("\n          ")
      printf(" Gain Attenuator . . . . . . . .:")
      tty_cntl("md")
      printf("<%s>", CAN_GAIN[no]["att"]?"On":"Off")
      tty_cntl("me")
      printf("\n      25 - LLD Mode . . .  . . . . . . . .:")
      tty_cntl("md")
      printf("<%s>", CAN_GAIN[no]["lldmode"]?"Auto":"Manual")
      tty_cntl("me")
      printf("\n      26 - LLD . . . . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s %>", CAN_GAIN[no]["lld"])
      tty_cntl("me")
      printf("\n      27 - FDISC Mode . . . . . . . . . . :")
      tty_cntl("md")
      printf("<%s>", CAN_GAIN[no]["fdiscmode"])
      tty_cntl("me")
      printf("\n      28 - FDISC . . . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s>", CAN_GAIN[no]["fdisc"])
      tty_cntl("me")
    }

    #DSP Filter
    if (CAN_FILTER[no]["readok"] != 0) {
      tty_cntl("md")
      printf ("Attention! DSA1000 #%d:", no)
      tty_cntl("me")
      printf ("%s\n",CAN_FILTER[no]["readok"])
    } else {
      printf("\n  3 - DSP Filter:")
      if (CAN_FILTER[no]["checkok"] != 0) {
        tty_cntl("md")
        printf ("Attention! DSA1000 #%d:", no)
        tty_cntl("me")
        printf ("%s\n",CAN_FILTER[no]["checkok"])
      }
      printf("\n      31 - Rise Time . . . . . . . . . . .:")
      tty_cntl("md")
      printf("<%s us>", CAN_FILTER[no]["risetime"])
      tty_cntl("me")
      printf("\n      32 - Flat Top . . . . . . . . . . . :")
      tty_cntl("md")
      printf("<%s us>", CAN_FILTER[no]["flattop"])
      tty_cntl("me")
      printf("\n      33 - LTC/PUR Mode . . . . . . . . . :")
      tty_cntl("md")
      printf("<%s>", CAN_FILTER[no]["ltcmode"]?"Enable":"Disable")
      tty_cntl("me")
      printf("\n           BLR Mode . . . . . . . . . . . :")
      tty_cntl("md")
      printf("<%s>", CAN_FILTER[no]["blrmode"])
      tty_cntl("me")
    }
    printf("\n 99 - Save Parameters")

    option = getval("\n\n\n\t     Option  ",0)
    if ((option == 0) || (option == 99)) {
      tty_cntl("md")
      printf("Please wait, setting may take few seccods.\n")
      tty_cntl("me")
      _canHVPS_set(no)
      _canGain_set(no)
      _canFilter_set(no)
    } else
      _dsa1000option(no, option)
  }
}'

#%UI% (no, option)
#%MDESC% Set parameter(s) for module %B%no%B%, menu option %B%option%B%.
def _dsa1000option(no,option) '{
local str llim hlim oldval

  #HVPS
  if (((option == 1) && DSA1000_EXPERT) || (option == 11)) {
    oldval = CAN_HVPS[no]["range"]
    CAN_HVPS[no]["range"] = getval("Range (-5000, -1300, 1300, 5000):",\
	CAN_HVPS[no]["range"])
    if (_rcheck(no) == 0)
      CAN_HVPSF[no] = 1
    else
      CAN_HVPS[no]["range"] = oldval
  }
  if (((option == 1) && DSA1000_EXPERT) || (option == 12)) {
    oldval = CAN_HVPS[no]["vlim"]
    str = sprintf ("Limit (%d to %4d):", llim, CAN_HVPS[no]["range"])
    CAN_HVPS[no]["vlim"] = getval(sprintf ("%s", str),CAN_HVPS[no]["vlim"])
    if (_vlimcheck(no) == 0)
      CAN_HVPSF[no] = 1
    else
      CAN_HVPS[no]["vlim"] = oldval
  }
  if ((option == 1) || (option == 13)) {
    oldval = CAN_HVPS[no]["v"]
    str = sprintf ("Voltage (%d to %4d):", llim, CAN_HVPS[no]["vlim"])
    CAN_HVPS[no]["v"] = getval(sprintf("%s", str),CAN_HVPS[no]["v"])
    if (_vcheck(no) == 0)
      CAN_HVPSF[no] = 1
    else
     CAN_HVPS[no]["v"] = oldval
  }
  if ((option == 14) && DSA1000_EXPERT) {
    CAN_HVPS[no]["inh"] = CAN_HVPS[no]["inh"]?0:1
    CAN_HVPSF[no] = 1
  }
  if (option == 15) {
    CAN_HVPS[no]["state"] = CAN_HVPS[no]["state"]?0:1
    _canHVPS_on(no,CAN_HVPS[no]["state"])
  }

  #DSP Gain
  if ((option == 2) || (option == 21)){
    str = sprintf ("Coarse gain (2.5")
    for (i=1; i<10; i++)
      str = sprintf("%s %d",str, 2.5*pow(2,i))
    CAN_GAIN[no]["coarse"] = \
	getval(sprintf ("%s):", str), CAN_GAIN[no]["coarse"])
    CAN_GAINF[no] = 1
  }
  if ((option == 2) || (option == 22)) {
    CAN_GAIN[no]["fine"] = getval("Fine Gain (0.900 to 1.900):", \
	CAN_GAIN[no]["fine"])
    CAN_GAINF[no] = 1
  }
  if ((option == 2) || (option == 23)) {
    CAN_GAIN[no]["sfine"] = getval("S-fine Gain (0.997500 to 1.002500):", \
	CAN_GAIN[no]["sfine"])
    CAN_GAINF[no] = 1
  }
  if ((option == 24) && DSA1000_EXPERT) {
    CAN_GAIN[no]["att"] = CAN_GAIN[no]["att"]?0:1
    _canGain_setaux(no) 
  }
  if (option == 25) {
    CAN_GAIN[no]["lldmode"] = CAN_GAIN[no]["lldmode"]?0:1
    CAN_GAINF[no] = 1
  }
  if ((option == 2) || (option == 26)) {
    CAN_GAIN[no]["lld"] = getval("LLD ():", CAN_GAIN[no]["lld"])
    CAN_GAINF[no] = 1
  }
  if (option == 27) {
    CAN_GAIN[no]["fdiscmode"] = (CAN_GAIN[no]["fdiscmode"] == "Auto")?"Manual":"Auto"
    CAN_GAINF[no] = 1
  }
  if ((option == 2) || (option == 28)) {
    CAN_GAIN[no]["fdisc"] = getval("FDISC ():", CAN_GAIN[no]["fdisc"])
    CAN_GAINF[no] = 1
  }

  #DSP Filter
  if ((option == 3) || (option == 31)){
    oldval = sprintf("%0.1f", CAN_FILTER[no]["risetime"])
    str = sprintf ("Rise Time ( 0.4")
    for (i=1; i<7; i++)
      str = sprintf("%s %4.1f",str, 0.4+i*0.1)
    str = sprintf("%s\n          ",str)
    for (i=0; i<8; i++)
      str = sprintf("%s %4.1f",str, 1.2+i*0.4)
    str = sprintf("%s\n          ",str)
    for (i=0; i<10; i++)
      str = sprintf("%s %4.1f",str, 4.8+i*0.8)
    str = sprintf("%s\n          ",str)
    for (i=0; i<10; i++)
      str = sprintf("%s %4.1f",str, 13.6+i*1.6)
    str = sprintf("%s\n          ",str)
    for (i=0; i<5; i++)
      str = sprintf("%s %4.1f",str, 30+i*2)
    CAN_FILTER[no]["risetime"] = \
	getval(sprintf ("%s):  ", str), CAN_FILTER[no]["risetime"])
    if (_rtcheck(no) == 0)
      CAN_FILTERF[no] = 1
    else
      CAN_FILTER[no]["risetime"] = oldval
  }
  if ((option == 3) || (option == 32)) {
    oldval = sprintf("%0.1f", CAN_FILTER[no]["flattop"])
    str = sprintf ("Flat Top (0.0")
    for (i=1; i<11; i++)
      str = sprintf("%s %0.1f",str, i*0.1)
    str = sprintf("%s\n         ",str)
    for (i=0; i<10; i++)
      str = sprintf("%s %0.1f",str, 1.2+i*0.2)
    CAN_FILTER[no]["flattop"] = getval(sprintf ("%s):  ", str), \
	CAN_FILTER[no]["flattop"])
    if (_ftcheck(no) == 0)
      CAN_FILTERF[no] = 1
    else
      CAN_FILTER[no]["flattop"] = oldval
  }
  if (option == 33) {
    CAN_FILTER[no]["ltcmode"] = CAN_FILTER[no]["ltcmode"]?0:1
    CAN_FILTERF[no] = 1
  }
}'

#%UI% (no)
#%MDESC% Check the HVPS parameters for module %B%no%B%.
def _canHVPS_check(no) '{

  if (_rcheck(no) == -1)
    return("HVPS range out of limits")
  if (_vlimcheck(no) == -1)
    return("HVPS voltage limit out of range")
  if (_vcheck(no) == -1)
    return("HVPS voltage out of range")
  return(0)
}'

#%UI% (no)
#%MDESC% Check the range HVPS parameter for module %B%no%B%.
def _rcheck(no) '{
local val

  val = CAN_HVPS[no]["range"] 
  if ((val != -5000) && (val != -1300) && (val != 1300) && (val != 5000)) {
    eprintf("Range %f out of limits\n", CAN_HVPS[no]["range"])
    CAN_HVPSF[no] = 0
    return(-1)
  }
  return(0)
}'

#%UI% (no)
#%MDESC% Check the voltage limit HVPS parameter for module %B%no%B%.
def _vlimcheck(no) '{
local val vlim
global hlim llim

  val = CAN_HVPS[no]["vlim"]
  vlim = CAN_HVPS[no]["range"]
  if (vlim < 0)
    llim = vlim
  else
    hlim = vlim

  if (vlim == -5000)
    hlim = -1300
  else if (vlim == -1300)
    hlim = 0
  else if (vlim == 1300)
    llim = 0
  else if (vlim == 5000)
    llim = 1300

  if ((val < llim) || (val > hlim)) {
    eprintf("Voltage limit %g out of range %d to %d\n", \
	CAN_HVPS[no]["vlim"], llim, hlim)
    CAN_HVPSF[no] = 0
    return(-1)
  }
  if (llim >=0)
    hlim = val
  else
    llim = val
  return(0)
}'

#%UI% (no)
#%MDESC% Check the voltage HVPS parameter for module %B%no%B%.
def _vcheck(no) '{
local val

  val = CAN_HVPS[no]["v"]
  if ((val < llim) || (val > hlim)) {
    eprintf("Voltage %g out of limits %d to %d\n", \
	CAN_HVPS[no]["v"], llim, hlim)
    CAN_HVPSF[no] = 0
    return(-1)
  }
  return(0)
}'

#%UI% (no)
#%MDESC% Set the HVPS parameters for module %B%no%B%.
def _canHVPS_set(no) '{
local double array argin[12]

  if (CAN_HVPSF[no] == 0)
    return(0)
  if (_canHVPS_check(no) == -1)
    return(-1)

  CAN_HVPSF[no] = 0

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x105
  argin[2] = CAN_HVPS[no]["range"]
  argin[3] = 1
  argin[4] = DSA1000_BASE + 0xA8
  argin[5] = CAN_HVPS[no]["vlim"]
  argin[6] = 1
  argin[7] = DSA1000_BASE + 0x3A
  argin[8] = CAN_HVPS[no]["v"]
  argin[9] = 0
  argin[10] = DSA1000_BASE + 0xAB
  argin[11] = CAN_HVPS[no]["inh"]

  return(esrf_io(DSA1000_DEV[no],"DevSetParam",argin))
}'

#%UI% (no)
#%MDESC% Read the HVPS parameters for module %B%no%B%.
def _canHVPS_read(no) '{
local double array argin[12]
local double array argout[6]

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x105
  argin[2] = 1
  argin[3] = DSA1000_BASE + 0xA8
  argin[4] = 1
  argin[5] = DSA1000_BASE + 0x3A
  argin[6] = 0
  argin[7] = DSA1000_BASE + 0xAB
  argin[8] = 0
  argin[9] = DSA1000_BASE + 0xB2
  argin[10] = 0
  argin[11] = DSA1000_BASE + 0x132

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1) {
    CAN_HVPS[no]["range"] = argout[0]
    CAN_HVPS[no]["vlim"] = argout[1]
    CAN_HVPS[no]["v"] = argout[2]
    CAN_HVPS[no]["inh"] = argout[3]
    CAN_HVPS[no]["state"] = argout[4]
    CAN_HVPS[no]["fault"] = argout[5]
  } else
    return("cannot read HVPS parameters")
  return(0)
}'

#%UI% (no)
#%MDESC% Read the HVPS state (on/off) for module %B%no%B%.
def _canHVPS_state(no) '{
local double array argin[2]
local double array argout[2]

  argin[0] = 0
  argin[1] = 0X200000B2

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1)
    CAN_HVPS[no]["state"] = argout[0]
  else
    CAN_HVPS[no]["state"] = -1
  return(CAN_HVPS[no]["state"])
}'

#%UI% (no, onoff)
#%MDESC% Set HVPS on (onoff = 1), or off( onoff = 0) for module %B%no%B%.
def _canHVPS_on(no,onoff) '{
  return(esrf_io(DSA1000_DEV[no],"DevSetValue", onoff))
}'

#%UI% (no)
#%MDESC% Check the DSP Gain parameters for module %B%no%B%.
def _canGain_check(no) '{
  if (_cgaincheck(no) == -1)
    return("coarse gain out of limits")
  if (_fgaincheck(no) == -1)
    return("fine gain out of limits")
  if (_sfgaincheck(no) == -1)
    return("S-fine gain out of limits")
  return(0)
}'

#%UI% (no)
#%MDESC% Check the coarse DSP Gain for module %B%no%B%.
def _cgaincheck(no) '{
local i

  for (i=1; i<10; i++) {
    if (CAN_GAIN[no]["coarse"] == 2.5*pow(2,i))
      return(0)
  }
  return(-1)
}'

#%UI% (no)
#%MDESC% Check the fine DSP Gain for module %B%no%B%.
def _fgaincheck(no) '{

  if ((CAN_GAIN[no]["fine"] < 0.8900) || (CAN_GAIN[no]["fine"] > 1.910))
    return(-1)
  return(0)
}'

#%UI% (no)
#%MDESC% Check the S-fine DSP Gain for module %B%no%B%.
def _sfgaincheck(no) '{
  if ((CAN_GAIN[no]["sfine"] < 0.9975) || (CAN_GAIN[no]["sfine"] > 1.0025))
    return(-1)
  return(0)
}'

#%UI% (no)
#%MDESC% Set the DSP Gain parameters for module %B%no%B%.
def _canGain_set(no) '{
local double array argin[18]
local sargin[] ret

  if (CAN_GAINF[no] == 0)
    return(0)

  if(_canGain_check(no) != 0)
    return(-1)

  CAN_GAINF[no] = 0

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x87
  argin[2] = CAN_GAIN[no]["coarse"]
  argin[3] = 1
  argin[4] = DSA1000_BASE + 0x88
  argin[5] = CAN_GAIN[no]["fine"]
  argin[6] = 1
  argin[7] = DSA1000_BASE + 0x89
  argin[8] = CAN_GAIN[no]["sfine"]
  argin[9] = 0
  argin[10] = DSA1000_BASE + 0x1e9
  argin[11] = CAN_GAIN[no]["lldmode"]
  argin[12] = 1
  argin[13] = DSA1000_BASE + 0x28
  argin[14] = CAN_GAIN[no]["lld"]
  argin[15] = 1
  argin[16] = DSA1000_BASE + 0x188
  argin[17] = CAN_GAIN[no]["fdisc"]

  ret = esrf_io(DSA1000_DEV[no],"DevSetParam",argin)
  if (ret == -1)
    return(-1)

  sargin[0] = 9
  sargin[1] = DSA1000_BASE + 0x187
  sargin[2] = CAN_GAIN[no]["fdiscmode"]
  ret = esrf_io(DSA1000_DEV[no],"DevSetMode",sargin)
  if (ret == -1)
    return(-1)
  return(0)
}'

#%UI% (no)
#%MDESC% Set auxilary DSP Gain parameters for module %B%no%B%.
def _canGain_setaux(no) '{
local double array argin[3]

  argin[0] = 0
  argin[1] = DSA1000_BASE + 0x1EA
  argin[2] = CAN_GAIN[no]["att"]
  return(esrf_io(DSA1000_DEV[no],"DevSetParam",argin))
}'

#%UI% (no)
#%MDESC% Read the DSP Gain parameters for module %B%no%B%.
def _canGain_read(no) '{
local double array argin[12]
local double array argout[12]
local sargout

  sargout[0]=""

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x87
  argin[2] = 1
  argin[3] = DSA1000_BASE + 0x88
  argin[4] = 1
  argin[5] = DSA1000_BASE + 0x89
  argin[6] = 0
  argin[7] = DSA1000_BASE + 0x1e9
  argin[8] = 1
  argin[9] = DSA1000_BASE + 0x28
  argin[10] = 1
  argin[11] = DSA1000_BASE + 0x188

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1) {
    CAN_GAIN[no]["coarse"] = argout[0]
    CAN_GAIN[no]["fine"] = argout[1]
    CAN_GAIN[no]["sfine"] = argout[2]
    CAN_GAIN[no]["lldmode"] = argout[3]
    CAN_GAIN[no]["lld"] = argout[4]
    CAN_GAIN[no]["fdisc"] = argout[5]
  } else
    return("cannot read  the gain parameters")

  local long array argin[2]
  argin[0] = 9
  argin[1] = DSA1000_BASE + 0x187

  if (esrf_io(DSA1000_DEV[no],"DevReadMode",argin,sargout) != -1) {
    CAN_GAIN[no]["fdiscmode"] = sargout[0]
  } else
    return("cannot read FDISC mode")
  return(0)
}'

#%UI% (no)
#%MDESC% Read auxilary DSP Gain parameters for module %B%no%B%.
def _canGain_readaux(no) '{
local double array argin[2]
local double array argout[1]

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x1EA

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1) {
    CAN_GAIN[no]["att"] = argout[0]
  } else
    return("cannot read auxilary gain parameters")
  return(0)
}'

#%UI% (no)
#%MDESC% Check the DSP Filter parameters for module %B%no%B%.
def _canFilter_check(no) '{
  if (_rtcheck(no) == -1)
    return("Filter rise time out of limits")
  if (_ftcheck(no) == -1)
    return("Filter flat top out of limits")
  return(0)
}'

#%UI% (no)
#%MDESC% Check the rise time DSP Filter parameter for module %B%no%B%.
def _rtcheck(no) '{
local i

  CAN_FILTER[no]["risetime"] = sprintf("%0.1f",CAN_FILTER[no]["risetime"])

  for (i=0; i<7; i++) {
    if (CAN_FILTER[no]["risetime"] == 0.4+i*0.1)
      return(0)
  }
  for (i=0; i<8; i++) {
    if (CAN_FILTER[no]["risetime"] == 1.2+i*0.4)
      return(0)
  }
  for (i=0; i<10; i++) {
    if (CAN_FILTER[no]["risetime"] == 4.8+i*0.8)
      return(0)
  }
  for (i=0; i<10; i++) {
    if (CAN_FILTER[no]["risetime"] == 13.6+i*1.6)
      return(0)
  }
  for (i=0; i<5; i++) {
    if (CAN_FILTER[no]["risetime"] == 30+i*2)
      return(0)
  }
  return(-1)
}'

#%UI% (no)
#%MDESC% Check the flat top DSP Filter parameter for module %B%no%B%.
def _ftcheck(no) '{
local i

  CAN_FILTER[no]["flattop"] = sprintf("%0.1f",CAN_FILTER[no]["flattop"])

  for (i=0; i<11; i++) {
    if (CAN_FILTER[no]["flattop"] == i*0.1)
      return(0)
  }

  for (i=0; i<10; i++) {
    if (CAN_FILTER[no]["flattop"] == 1.2+i*0.2)
      return(0)
  }
  return(-1)
}'

#%UI% (no)
#%MDESC% Read the DSP Filter parameters for module %B%no%B%.
def _canFilter_read(no) '{
local double array argin[6]
local double array argout[12]
local sargout

  sargout[0]=""

  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x185
  argin[2] = 1
  argin[3] = DSA1000_BASE + 0x186
  argin[4] = 0
  argin[5] = DSA1000_BASE + 0xc5

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1) {
    CAN_FILTER[no]["risetime"] = argout[0]
    CAN_FILTER[no]["flattop"] = argout[1]
    CAN_FILTER[no]["ltcmode"] = argout[2]
  } else
    return("cannot read filter parameters")

  local long array argin[2]

  argin[0] = 9
  argin[1] = DSA1000_BASE + 0x8c

  if (esrf_io(DSA1000_DEV[no],"DevReadMode",argin,sargout) != -1) {
    CAN_FILTER[no]["blrmode"] = sargout[0]
  } else
    return("cannot read BLR mode")
  return(0)
}'

#%UI% (no)
#%MDESC% Set the DSP Filter parameters for module %B%no%B%.
def _canFilter_set(no) '{
local double array argin[9]

  if (CAN_FILTERF[no] == 0)
    return(0)
  argin[0] = 1
  argin[1] = DSA1000_BASE + 0x185
  argin[2] = CAN_FILTER[no]["risetime"]
  argin[3] = 1
  argin[4] = DSA1000_BASE + 0x186
  argin[5] = CAN_FILTER[no]["flattop"]
  argin[6] = 0
  argin[7] = DSA1000_BASE + 0xc5
  argin[8] = CAN_FILTER[no]["ltcmode"]

  CAN_FILTERF[no] = 0
  return(esrf_io(DSA1000_DEV[no],"DevSetParam",argin))
}'

#%UI% (no)
#%MDESC% Read some status parameters for module %B%no%B%.
def _dsa1000stat(no) '{
local double array argin[6]
local double array argout[12]
local sargout

  sargout[0]=""

  argin[0] = 0
  argin[1] = DSA1000_BASE + 0x8b
  argin[2] = 1
  argin[3] = DSA1000_BASE + 0x18b
  argin[4]= 0
  argin[5]= DSA1000_BASE + 0x79

  if (esrf_io(DSA1000_DEV[no],"DevReadParam",argin,argout) != -1) {
    CAN_STAT[no]["pz"] = argout[0]
    CAN_STAT[no]["icr"] = argout[1]
    CAN_STAT[no]["pamppol"] = argout[2]
  } else
    return("cannot read status parameters")

  local long array argin[4]
  argin[0] = 5
  argin[1] = DSA1000_BASE + 0x4
  argin[2] = 9
  argin[3] = DSA1000_BASE + 0xb3

  if (esrf_io(DSA1000_DEV[no],"DevReadMode",argin,sargout) != -1) {
    CAN_STAT[no]["acqmode"] = sargout[0]
    CAN_STAT[no]["pamptype"] = sargout[1]

  } else
    return("cannot read status mode")

  return(0)
}'

#%UI% (no,msg)
#%MDESC% Print the message %B%msg%B% for module %B%no%B%.
def _msg(no, msg) '{
   tty_cntl("md")
   if (no=="all")
     printf("DSA 1000: ")
   else
     printf("DSA 1000 MCA device %d: ", no)
   tty_cntl("me")
   printf("%s\n", msg)
}'

#%MACROS%
#%IMACROS%
#%DEPENDENCIES%
#%B%multimca.mac%B% - mca%BR%
#%TOC%
#%AUTHOR% A.Beteva/BLISS
#$Revision: 1.5 $$Date: 2013/08/19 11:28:00 $
#%END%
#%LOG%
#$Log: candsa1000.mac,v $
#Revision 1.5  2013/08/19 11:28:00  beteva
#return human readable error messages instead of -1.
#
#Revision 1.4  2008/02/27 12:52:43  rey
#doc changes
#
#Revision 1.3  2008/02/27 12:50:15  rey
#documentation changes
#
#Revision 1.2  2007/03/29 11:52:19  beteva
#added "save parameters", "preamp output polarity"
#
#Revision 1.1  2007/03/21 17:25:01  beteva
#Initial revision