esrf

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

#%TITLE% can2016.mac
#%NAME% 
#   Macros to control a TCA Canberra 2016
#%CATEGORY% Detection, MCA
#%DESCRIPTION% 
#  Simple macros to control the TCA Canberra 2016.
#  Use "can2016menu" to set the parameters, "can2016status" to see them and 
#  "can2016init" to put them to some default values.
#%END%

#%UU% <address>
#%MDESC% Sets up a canberra 2016 tca on ICB address <address>. The MCA device
#name is taken as unit 0 from SPEC  
def can2016setup '{
global CAN2016_DEV CAN2016_ICB_ADR
global CAN2016_GAIN CAN2016_RANGE CAN2016_LLD CAN2016_ULD CAN2016_ZERO
global 	CAN2016_MODE, CAN2016_OFFSET,  CAN2016_ADCGAIN, CAN2016_STATE
long array CAN2016_THRESHOLD[3][2]
CAN2016_DEV = mca_par("device_id")

if ($#) {
  CAN2016_ICB_ADR=$1
} else {
   p "Using MCA device", CAN2016_DEV, "to talk to TCA2016"
   CAN2016_ICB_ADR=getval ("ICB address of TCA module",CAN2016_ICB_ADR)
}

}'


#%UU%
#%MDESC% Sets sca3 low threshold to 0, sca3 high threshold to 4096 
#%MDESC% activate all sca outputs and gate adc with sca3
def can2016init '{
# enable SCA, enable gate3, select TCA
can2016scavalidate(1)
can2016gatesel(1,1)
can2016gatesel(2,1)
can2016gatesel(3,1)
can2016tcaset(1)

# seuil bas = 0 pour sca 1
# seuil haut = max (12 bits) 4095 pour sca 1
can2016scaset(1,0,4095)

# seuil bas = 0 pour sca 2
# seuil haut = max (12 bits) 4095 pour sca 2
can2016scaset(2,0,4095)

# seuil bas = 0 pour sca 3
# seuil haut = max (12 bits) 4095 pour sca 3
can2016scaset(3,0,4095)
}'

#%UU% (gate1, gate2, gate3)
#%MDESC% set or unset all sca gate for mca 
def can2016gateselect(gate1, gate2, gate3) '{
  can2016gatesel(1, gate1)
  can2016gatesel(2, gate2)
  can2016gatesel(3, gate3)
}'

#%UU% (gate, val)
#%MDESC% set or unset sca gate for mca 
def can2016gatesel(sca, val) '{
  local can2016_val

  can2016_val = icb_read(CAN2016_DEV, CAN2016_ICB_ADR, 2)
  if ((val == 0) || (val == 1)) {
    if (val == 1) {
      can2016_val = (can2016_val | (1<<(3+sca)))
    } else {
      can2016_val = (can2016_val & (0xff&(~(1<<(3+sca)))))
    }
  } else {
    return(sprintf("value for sca %d must be set to 0 or 1", sca))
  }

  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 2, can2016_val)
  return("")
}'

#%UU% 
#%MDESC% show low and high threshold for sca
def can2016scashow '{
  local _i

  printf("          low  high\n\n")
  for (_i=0;_i<3;_i++) {
    printf("sca %1d :  %4d  %4d\n", _i+1, CAN2016_THRESHOLD[_i][0], CAN2016_THRESHOLD[_i][1])
  }
}'

#%UU% (sca, low, high)
#%MDESC% set low and high threshold for sca
def can2016scaset(sca, low, high) '{
  local can2016_val

  if ((sca < 1) || (sca > 3)) {
    return("available sca are 1, 2, 3")
  }
  if ((low < 0) || (low > 4095)) {
    return("Low threshold is between 0 and 4095")
  }
  if ((high < 0) || (high > 4095)) {
    return("High threshold is between 0 and 4095")
  }
  if (low > high) {
    return("High threshold must be greater than Low threshold")
  }
  CAN2016_THRESHOLD[sca-1][0] = low
  CAN2016_THRESHOLD[sca-1][1] = high
  can2016_val = low & 0xff
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 3, can2016_val)
  can2016_val = (low>>8)|((sca-1)<<6)
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 4, can2016_val)

  can2016_val = high & 255
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 3, can2016_val)
  can2016_val = (high>>8)|((sca-1)<<6)|(1<<5)
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 4, can2016_val)

  return("")
}'

#%UU% (val)
#%MDESC% validate or not all sca outputs
def can2016scavalidate(val) '{
  local can2016_val

  if ((val != 0) && (val != 1)) {
    return("[1=validate sca outputs] or [0=unvalidate sca outputs]")
  }
  can2016_val = icb_read(CAN2016_DEV, CAN2016_ICB_ADR, 2)
  if (val == 0) {
    can2016_val = (can2016_val&0xf7)
  } else {
    can2016_val = (can2016_val|0x08)
  }
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 2, can2016_val)
  return("")
}'

#%UU% (val)
#%MDESC% validate or not tca mode
def can2016tcaset(val) '{
  local can2016_val

  if ((val != 0) && (val != 1)) {
    return("[1=set tca mode] or [0=unset tca mode]")
  }
  can2016_val = icb_read(CAN2016_DEV, CAN2016_ICB_ADR, 2)
  if (val == 0) {
    can2016_val = (can2016_val&0x7f)
  } else {
    can2016_val = (can2016_val|0x80)
  }
  icb_write(CAN2016_DEV, CAN2016_ICB_ADR, 2, can2016_val)
  return("")
}'

#%UU% (device, addr, reg)
#%MDESC% Read register <reg> from ICB device address <addr> on MCA 
# device <device>
def icb_read (device, addr, reg) '{
local ICB ICBOUT
	ICB[0] = 1 ; 
	ICB[1] = reg + 0x10 * addr
	esrf_io(device,"DevMcaReadICB",ICB,ICBOUT)
	return ICBOUT[0]
}'

#%UU% (device, addr, reg)
#%MDESC% Put value <value> into register <reg> on ICB device on 
# address <addr> on MCA device <device>
def icb_write (device, addr, reg, value) '{
local ICB
	ICB[0]=1
	ICB[1]=reg + 0x10 * addr
	ICB[2]=value
	return esrf_io(device, "DevMcaSendICB",ICB)
}'

#%MACROS%
#%IMACROS%
#%AUTHOR% GB