esrf

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

#%TITLE% C353.mac
#%NAME% 
#   C353 bunchmarker control
#%CATEGORY% Accelerator
#%DESCRIPTION%
#   Macros to configure and operate the csel C353 card.
#   This card provides bunch marking for synchronization with
#   experiments.
#%BR%26/01/10: add message with calculated frequency in c353_setsignals
#%BR%29/01/10: c353_select saves present config in array and retreives the selected
#one
#%BR%29/01/10: c353_putconf and c353_getconf work in ~blissadm/local/spec/userconf/C353
#directory
#%END%

global C353_dev
global C353_ch C353_pre1 C353_pre2 C353_pre3 C353_pre4
global C353_shortp C353_longp1 C353_longp2 C353_RFshift C353_Chanshift
global C353_chused C353_shape C353_pol C353_tog
global C353_nbdev C353_ARR  C353_IND C353_RFshift_val
global C353_PATH_SAVE

C353_PATH_SAVE = "~blissadm/local/spec/userconf/C353/"

string array C353_stat[32][20]
C353_stat0[0] = "OUTPUT ON"
C353_stat1[0] = "OUTPUT OFF"
C353_stat0[1] = "RF DLL PHASE OK"
C353_stat1[1] = "RF DLL PHASE ERROR"
C353_stat0[2] = "RF DLL ERROR"
C353_stat1[2] = "RF DLL locked"
C353_stat0[3] = "-V ERROR"
C353_stat1[3] = "-V OK"
C353_stat0[4] = "+V ERROR"
C353_stat1[4] = "+V OK"
C353_stat0[5] = "Temp. ALERT"
C353_stat1[5] = "Temp. OK"
C353_stat0[6] = "Temp. PANIC"
C353_stat1[6] = "Temp. OK"


#%UU% <device name>
#%MDESC% setting the list of bunchmarkers
def c353setup'{

   if ($# == 0) {
      p "usage: c353setup <device_name> <device_name> ..."
      exit
   }
   
   C353_nbdev = split("$*",C353_ARR) 
   if (C353_nbdev == 1) {
      C353_IND = 0
      C353_dev = C353_ARR[C353_IND]
   }  
}'

#%UU%
#%MDESC%select the current bunchmarker
def c353_select'{
   local ind
   
   if ($# == 0) {
      ind = getval(sprintf("which bunchmarker [0..%d]?",C353_nbdev-1),C353_IND)
   }
   else {
      ind = $1
   }
   if ( (ind <0 ) || (ind >= C353_nbdev) ){
       p "index should be in range [0.." C353_nbdev-1 "]"
   }
   else {
      _c353_saveoneconf()
      C353_IND = ind
      C353_dev = C353_ARR[C353_IND]
      _c353_getoneconf()
   }
}'

#%IU%
#%MDESC% checks if CUB_ARR global associative array has
#been already set
def _C353_checkifset'{
  local found ii

  found = 0
  ii = 0
  while ((found == 0) && (ii <C353_nbdev)){
     if (C353_dev == C353_ARR[ii]) {
        found = 1
     }
     ii++
  }
  if (found == 0) {
     p "ERROR: no device is selected"
     exit
  }
}'

#%UU%
#%MDESC% read state
def c353_state'{
   local mstate ii bit val res temp1 temp2
   
   _C353_checkifset   

   mstate = esrf_io(C353_dev,"DevReadState")
   
   printf("%x\n", mstate)

#  the useful bits   
   val = 0x200
   for (ii=0; ii<7; ii++) {
      res = mstate & val
      if (res == 0) {
         p "bit " ii+9 ": "  C353_stat0[ii]
      }
      else {
         p "bit " ii+9 ": "  C353_stat1[ii]
      }
      val = val << 1
   }
   
#  the temperature
   mstate = mstate >> 16
   temp1 = mstate & 0x00ff
   temp2 = mstate >> 8
    
   p temp1 " dc; " temp2 " dc"  
}'


#%UU%
#%MDESC%Setting of output inhibit off
def c353_on'{
   _C353_checkifset   
   esrf_io(C353_dev,"DevOn")
}'

#%UU%
#%MDESC%Setting of output inhibit on
def c353_off'{
   _C353_checkifset   
   esrf_io(C353_dev,"DevOff")
}'

#%UU%
#%MDESC%Resets the DLL. This is done automatically
#when the device server starts, and must be done
#afterwards if cable unplugged. Furthermore, when run, it:
#%BR% - sets all channel to RF phases to 0
#%BR% - sets all channel phase shift by N RF to 0 
def c353_reset'{
   _C353_checkifset   
   p "This command resets all channel phase shift"
   esrf_io(C353_dev,"DevReset")
}'

#%UU% <channel> <prescale1> <prescale2> <prescale3> <prescale4>
#%MDESC%Defines the frequency divider for a channel. Furthermore,
#when run, it sets all channel phase shift by N RF to 0.
def c353_setfreq'{
   local mypre mydiv 

   _C353_checkifset   
   if ($# != 5){
      C353_ch = getval("channel number [0..2]?", C353_ch)
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      if (C353_tog[ii] == 0) {
         p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4"
      }
      else {
         p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4 *2 (toggle)"
      }
      C353_pre1[C353_ch] = getval("... frequency prescal1 [4..34]?",C353_pre1[C353_ch])
      C353_pre2[C353_ch] = getval("... frequency prescal2 [1..32]?",C353_pre2[C353_ch])
      C353_pre3[C353_ch] = getval("... frequency prescal3 [1..32]?",C353_pre3[C353_ch])
      C353_pre4[C353_ch] = getval("... frequency prescal4 [1..32]?",C353_pre4[C353_ch])
   }
   else {
      C353_ch = $1
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      C353_pre1[C353_ch] = $2
      C353_pre2[C353_ch] = $3
      C353_pre3[C353_ch] = $4
      C353_pre4[C353_ch] = $5
   }

#  check that parameters are valid   
   if ( (C353_pre1[C353_ch] <4 ) || (C353_pre1[C353_ch] > 34) ) {
      p "prescaler1 is not in [4..34] range. exit"
      exit
   }
   if ( (C353_pre2[C353_ch] <1 ) || (C353_pre2[C353_ch] > 32) ) {
      p "prescaler1 is not in [1..32] range. exit"
      exit
   }
   if ( (C353_pre3[C353_ch] <1 ) || (C353_pre3[C353_ch] > 32) ) {
      p "prescaler1 is not in [1..32] range. exit"
      exit
   }
   if ( (C353_pre4[C353_ch] <1 ) || (C353_pre4[C353_ch] > 32) ) {
      p "prescaler1 is not in [1..32] range. exit"
      exit
   }
   
   mydiv = C353_pre1[C353_ch]*C353_pre2[C353_ch]*C353_pre3[C353_ch]*C353_pre4[C353_ch]
   
   if (C353_tog[ii] == 0) {
      p "frequency divider is: " C353_pre1[C353_ch] " * " C353_pre2[C353_ch] " * " C353_pre3[C353_ch] " * " C353_pre4[C353_ch] " = " mydiv
   }
   else {
      p "frequency divider is: " C353_pre1[C353_ch] " * " C353_pre2[C353_ch] " * " C353_pre3[C353_ch] " * " C353_pre4[C353_ch] " *2 = " mydiv " *2"
   }
#  now, the server parameters
   mypre[0] = C353_ch
   mypre[1] = C353_pre1[C353_ch] - 3
   if (C353_pre2[C353_ch] == 32){
      mypre[2] = 0
   }
   else {
      mypre[2] = C353_pre2[C353_ch]
   }
   if (C353_pre3[C353_ch] == 32){
      mypre[3] = 0
   }
   else {
      mypre[3] = C353_pre3[C353_ch]
   }
   if (C353_pre4[C353_ch] == 32){
      mypre[4] = 0
   }
   else {
      mypre[4] = C353_pre4[C353_ch]
   }

#   p mypre 
   p "This commands resets all channel phase shift by N RF"  
   esrf_io(C353_dev,"DevSetFreq",mypre)

}'


#%IU% <channel>
#%MDESC%Checks that channel is in valid range
#%BR%Returns 1 (if OK) and 0 (if NOT OK)
def _c353_valid_channel(ch)'{
   if ( (ch >=0) && (ch <=2) ){
      return 1
   }
   else {
      return 0
   }
}'

#%IU% <channel>
#%MDESC%Reads the channel frequency divider
def c353_readfreq'{
   local myfreq ch pre1 pre2 pre3 pre4 mydiv
   
   _C353_checkifset   
   ch = $1
   if ( _c353_valid_channel(ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   myfreq[0] = 0
   esrf_io(C353_dev,"DevReadFreq",ch,myfreq)
   
   p "frequency divider is:  " myfreq[0] " ; " myfreq[1] " ; " myfreq[2] " ; " myfreq[3]
   pre1 = myfreq[0]+3
   pre2 = myfreq[1]
   if (myfreq[1] == 0) {
      pre2 = 32
   }
   pre3 = myfreq[2]
   if (myfreq[2] == 0) {
      pre3 = 32
   }
   pre4 = myfreq[3]
   if (myfreq[3] == 0) {
      pre4 = 32
   }

   mydiv = pre1 * pre2 * pre3 * pre4
   if (C353_tog[ii] == 0) {
      p "user frequency divider is: " pre1 " * " pre2 " * " pre3 " * " pre4 " = " mydiv
   }
   else {
      p "user frequency divider is: " pre1 " * " pre2 " * " pre3 " * " pre4 *2 " = " mydiv " * 2"
   }
}'

#%UU% <channel> <prescaler>
#%MDESC%Sets a short pulse shape for a channel
def c353_setshortp'{
   local myshape
   
   _C353_checkifset   
   if ($# != 2) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      C353_shortp[C353_ch] = getval("Pulse width [2,4..34]?",C353_shortp[C353_ch])
   }
   else {
      C353_ch = $1
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }       C353_shortp[C353_ch] = $2
   }
   if (_c353_check_shortp(C353_shortp[C353_ch]) != 1) {
      p "short pulse not in range [2,4..34]. exit"
      exit
   } 
   
#  now prepare the parameters for the server
   myshape[0] = C353_ch
   if (C353_shortp[C353_ch] == 2) {
      myshape[1] = 0
   }
   else {
      myshape[1] = C353_shortp[C353_ch] - 3
   }  
   myshape [2] = 0
   
   esrf_io(C353_dev,"DevSetWidth",myshape)    
   
}'

#%UU% <channel>
#%MDESC% reads the channel pulse shape
def c353_readshape'{
   local mypul ch pulval
   
   _C353_checkifset   
   ch = $1
   if (_c353_valid_channel(ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   mypul[0] = 0
   
   esrf_io(C353_dev,"DevGetWidth",ch,mypul)
   
   if (mypul[1] == 0) {
#     Short pulse
      if (mypul[0] == 0) {
         pulval = 2
      }
      else {
         pulval = mypul[0] + 3
      }
      p "Short pulse: " pulval " RF clock unit"
   }
   else {
#      p mypul
      p "Long pulse: " mypul[1] * (mypul[0]+3) + 3 " RF clock unit"
   }

   
}'

#%IU% <pulse>
#%MDESC%check that short pulse is in the range
#%BR%Returns 1 (if OK) and 0 (if NOT OK)
def _c353_check_shortp(pu)'{
    if ( (pu < 2) || (pu == 3) || (pu > 34) ) {
       return 0
    }
    else {
       return 1
    }
}'

#%IU% <channel> <prescaler1> <prescaler2>
#%MDESC% Sets a Long pulse shape for a channel
def c353_setlongp'{
   local myshape myreg
   
   _C353_checkifset   
   if ($# != 3) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      p "The Long pulse shape will be: prescaler1*prescaler2 + 3 "
      C353_longp1[C353_ch] = getval("Long Pulse prescaler1 [5..34]?",C353_longp1[C353_ch])
      C353_longp2[C353_ch] = getval("Long Pulse prescaler2 [2..31]?",C353_longp2[C353_ch])
   }
   else {
      C353_ch = $1
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }      
      C353_longp1[C353_ch] = $2
      C353_longp2[C353_ch] = $3
   }
   if ( (C353_longp1[C353_ch] <5) || (C353_longp1[C353_ch] > 34) ) {
      p "Long pulse prescaler1 not in range [5..34]. exit"
      exit
   } 
   if ( (C353_longp2[C353_ch] <2) || (C353_longp2[C353_ch] > 31) ) {
      p "Long pulse prescaler2 not in range [2..31]. exit"
      exit
   } 

   myreg = C353_longp1[C353_ch]*C353_longp2[C353_ch] + 3
   p "Long pulse shape is: " C353_longp1[C353_ch] " * " C353_longp2[C353_ch] " + 3 = " myreg
   
#  now prepare the parameters for the server
   myshape[0] = C353_ch
   myshape[1] = C353_longp1[C353_ch] - 3
   myshape[2] = C353_longp2[C353_ch]

#   p myshape
   
   esrf_io(C353_dev,"DevSetWidth",myshape)    
      
}'

#%UU% <channel> <0/1>
#%MDESC%Sets the toggle mode (0:no toggle, 1:toggle) for channel
def c353_settog'{
   local tog mypar
   
   _C353_checkifset   
   if ($# != 2) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      C353_tog[C353_ch] = getval("toggle mode [0:no, 1:toggle]?",C353_tog[C353_ch])
   }
   else {
      C353_ch = $1
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }      
      C353_tog[C353_ch] = $2
   }
   
   if ( (C353_tog[C353_ch] != 0) && (C353_tog[C353_ch]!= 1) ){
      p "toggle parameter not in range [0,1]. exit"
      exit
   }
   
   mypar[0] = C353_ch
   mypar[1] = C353_tog[C353_ch]
   
#   p mypar
   
   esrf_io(C353_dev,"DevSetToggle",mypar)

}'

#%UU% <channel>
#%MDESC%Reads the toggle mode (0:no toggle, 1:toggle) for channel
def c353_readtog'{
   local tog mytog
   
   _C353_checkifset   
   if ($# != 1) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
   }
   else {
      C353_ch = $1
   }
   if (_c353_valid_channel(C353_ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   mytog = esrf_io(C353_dev,"DevReadToggle",C353_ch)
   
   p "Toggle mode: " mytog

}'



#%UU% <channel>
#%MDESC%Sets the polarity mode for channel 
def c353_setpol'{
   local ch pol
   
   _C353_checkifset   
   if ($# != 2) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }
      C353_pol[C353_ch] = getval("polarity [0:normal, 1:inverted]?",C353_pol[C353_ch])
   }
   else {
      C353_ch = $1
      if (_c353_valid_channel(C353_ch) != 1) {
         p "channel number not in range [0..2]. exit"
         exit
      }      
      C353_pol[C353_ch] = $2
   }
   
   if ( (C353_pol[C353_ch] != 0) && (C353_pol[C353_ch] != 1) ){
      p "polarity parameter not in range [0,1]. exit"
      exit
   }
   
   mypar[0] = C353_ch
   mypar[1] = C353_pol[C353_ch]

#   p mypar   
   esrf_io(C353_dev,"DevSetPol",mypar)

}'


#%UU% <channel>
#%MDESC%Reads the polarity mode (0:normal, 1:inverted) for channel
def c353_readpol'{
   local tog mypol
   
   _C353_checkifset   
   if ($# != 1) {
      C353_ch = getval("channel number [0..2]?", C353_ch)
   }
   else {
      C353_ch = $1
   }
   if (_c353_valid_channel(C353_ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   mypol = esrf_io(C353_dev,"DevReadPol",C353_ch)
   
   p "Polarity mode: " mypol

}'
#%UU% <RFshift>
#%MDESC% Sets RF clock shifting for all the channels (in 1/256 RF clock increment)
def c353_RFshift'{
   local myshift
   
   _C353_checkifset   
   myshift = $1
   
   if ( (myshift <-255) || (myshift > 255) {
      p "Valid range is [-255..255]. exit"
      exit
   }

   p "This command resets all phase shift"
  
   if (myshift != 0) {
      esrf_io(C353_dev,"DevSetRFShift",myshift)
   }
}'

#%UU% <RFshift>
#%MDESC% Sets RF clock incremental shifting for all the channels (in 1/256 RF clock increment)
def c353_RFincshift'{
   local myshift
   
   _C353_checkifset   
   myshift = $1

   if ( (myshift <-255) || (myshift > 255) ) {
      p "Valid range is [-255..255]. exit"
      exit
   }
   
   if (myshift != 0) {
      esrf_io(C353_dev,"DevSetIncRFShift",myshift)
   }
}'

#%UU%
#%MDESC% Reads the RF clock shift of all the channels
def c353_RFrshift'{
   local myshift

   _C353_checkifset   
   myshift = esrf_io(C353_dev,"DevReadRFShift")
   
   p "RF shift is: "  myshift
}'

#%UU% <channel> <chanshift>
#%MDESC% Sets channel phase shift by N RF for one channel (in RF clock increment)
def c353_Chanshift'{
   local myshift ch mypar

   _C353_checkifset   
   if ($# != 2) {
      p "usage: c353_Chanshift <channel> <shift>"
      exit
   } 
     
   ch = $1
   myshift = $2

   if (_c353_valid_channel(ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   mypar[0] = ch
   mypar[1] = myshift

#  no limit !!!   
#   if ( (myshift < 1) || (myshift > 100) ){
#      p "valid range is [1..100]. exit"
#      exit
#   }

   p "This command resets all phase shifts"
   
   esrf_io(C353_dev,"DevSetChanShift",mypar)
}'

#%UU% <channel> <chanshift>
#%MDESC% Sets channel incremental phase shift by N RF for one channel (in RF clock increment)
def c353_Chanincshift'{
   local myshift ch mypar

   _C353_checkifset   
   if ($# != 2) {
      p "usage: c353_Chanincshift <channel> <shift>"
      exit
   } 
     
   ch = $1
   myshift = $2

   if (_c353_valid_channel(ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }
   
   mypar[0] = ch
   mypar[1] = myshift
   
   esrf_io(C353_dev,"DevSetIncChanShift",mypar)
}'

#%UU% <channel>
#%MDESC% Reads the channel phase shift by N RF for one channel
def c353_Chanrshift'{
   local ch
   local myChanshift 

   _C353_checkifset   
   ch = $1
   if (_c353_valid_channel(ch) != 1) {
      p "channel number not in range [0..2]. exit"
      exit
   }

   myChanshift = esrf_io(C353_dev,"DevReadChanShift",ch)
   
   p "Channel phase shift by N RF for channel " ch " is: "  myChanshift
}'

#%UU%
#%MDESC%Defines the parameters, frequencies and phase shifts
def c353_setsignals'{
   local ii
   
   _C353_checkifset   
   for (ii=0; ii<3; ii++) {
      C353_chused[ii] = getval(sprintf("use channel %d [0:no, 1:yes]?",ii),C353_chused[ii])
   }
   
   p " "
   p "Resetting DLL"
   c353_reset
   
   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){

#        Setting the toggle
         C353_tog[ii] = getval(sprintf("...ch: %d signal normal(0) or toggled (1) ?",ii),C353_tog[ii])
	 c353_settog ii C353_tog[ii]

#        Setting the frequency
         p "*** On channel " ii
         if (C353_tog[ii] == 0) {
            p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4"
         }
         else {
            p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4 *2 (toggle)"
         }
         C353_pre1[ii] = getval(sprintf("...ch: %d frequency prescal1 [4..34]?",ii),C353_pre1[ii])
         C353_pre2[ii] = getval(sprintf("...ch: %d frequency prescal2 [1..32]?",ii),C353_pre2[ii])
         C353_pre3[ii] = getval(sprintf("...ch: %d frequency prescal3 [1..32]?",ii),C353_pre3[ii])
         C353_pre4[ii] = getval(sprintf("...ch: %d frequency prescal4 [1..32]?",ii),C353_pre4[ii])
         c353_setfreq ii  C353_pre1[ii]  C353_pre2[ii] C353_pre3[ii] C353_pre4[ii]
         if (C353_tog[ii] == 0) {
            p "... Calculated frequency:  " 352202/C353_pre1[ii]/C353_pre2[ii]/C353_pre3[ii]/C353_pre4[ii] "  khz"
         }
         else {
            p "... Calculated frequency:  " 352202/C353_pre1[ii]/C353_pre2[ii]/C353_pre3[ii]/C353_pre4[ii]/2 "  khz"
         }
         
         if (C353_tog[ii] == 0) {
#           Meaningful only if no toggle         

#           Setting the shape  
            C353_shape[ii] = getval(sprintf("...ch: %d Long shape (1) or short shape (0)?",ii),C353_shape[ii])
	    if (C353_shape[ii] == 0) {
               C353_shortp[ii] = getval(sprintf("...ch: %d Pulse width [2,4..34]?",ii),C353_shortp[ii])
	       c353_setshortp ii C353_shortp[ii]
	    }
	    else {
               p "The Long pulse shape will be: prescaler1*prescaler2 + 3 "
               C353_longp1[C353_ch] = getval(sprintf("...ch: %d Long Pulse prescaler1 [5..34]?",ii),C353_longp1[C353_ch])
               C353_longp2[C353_ch] = getval(sprintf("...ch: %d Long Pulse prescaler2 [2..31]?",ii),C353_longp2[C353_ch])
	       c353_setlongp ii C353_longp1[C353_ch] C353_longp2[C353_ch]
	    }
            
	 
#           Setting the polarity
            C353_pol[ii] = getval(sprintf("...ch: %d polarity normal(0) or inverted (1) ?",ii),C353_pol[ii])
	    c353_setpol ii C353_pol[ii]
         }
	 
	 p " "
      }
   } 

   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){
#        Setting the channel phase shift by N RF
#        no limit
#         C353_Chanshift[ii] = getval(sprintf("...ch: %d channel phase shift by x RF x:[0..100]?",ii),C353_Chanshift[ii]) 
         C353_Chanshift[ii] = getval(sprintf("...ch: %d channel phase shift by N RF ?",ii),C353_Chanshift[ii]) 
         if (C353_Chanshift[ii] != 0) {
	    c353_Chanincshift ii C353_Chanshift[ii] 
	 }
      }
   }
	 
   
#  Setting the RF shifting
   C353_RFshift = getval("All channel to RF phase shift [-255..255]?",C353_RFshift)  
   c353_RFincshift  C353_RFshift    	 

#  release inhibit (if any)
   p " "
   p "release inhibit"
   c353_on
}'

#%IU%<index>
#%MDESC%store the configuration for C353 device <index>in saving arrays
def _c353_saveoneconf()'{
   local ii
   
   for (ii=0; ii<3;ii++) {
      C353_chused[ii][C353_IND] = C353_chused[ii]
      C353_tog[ii][C353_IND] = C353_tog[ii]
      C353_pre1[ii][C353_IND] = C353_pre1[ii]
      C353_pre2[ii][C353_IND] = C353_pre2[ii]
      C353_pre3[ii][C353_IND] = C353_pre3[ii]
      C353_pre4[ii][C353_IND] = C353_pre4[ii]
      C353_shape[ii][C353_IND] = C353_shape[ii]
      C353_shortp[ii][C353_IND] = C353_shortp[ii]
      C353_longp1[ii][C353_IND] = C353_longp1[ii]
      C353_longp2[ii][C353_IND] = C353_longp2[ii]
      C353_pol[ii][C353_IND] = C353_pol[ii]
      C353_Chanshift[ii][C353_IND] = C353_Chanshift[ii]
      C353_RFshift_val[C353_IND] = C353_RFshift
   }
}'

#%IU%<index>
#%MDESC%get the configuration for C353 device <index> from saving arrays
def _c353_getoneconf()'{
   local ii
   
   for (ii=0; ii<3;ii++) {
      C353_chused[ii] = C353_chused[ii][C353_IND]
      C353_tog[ii] = C353_tog[ii][C353_IND]
      C353_pre1[ii] = C353_pre1[ii][C353_IND]
      C353_pre2[ii] = C353_pre2[ii][C353_IND]
      C353_pre3[ii] = C353_pre3[ii][C353_IND]
      C353_pre4[ii] = C353_pre4[ii][C353_IND]
      C353_shape[ii] = C353_shape[ii][C353_IND]
      C353_shortp[ii] = C353_shortp[ii][C353_IND]
      C353_longp1[ii] = C353_longp1[ii][C353_IND]
      C353_longp2[ii] = C353_longp2[ii][C353_IND]
      C353_pol[ii] = C353_pol[ii][C353_IND]
      C353_Chanshift[ii] = C353_Chanshift[ii][C353_IND]
      C353_RFshift = C353_RFshift_val[C353_IND]
   }
}'


#%UU%
#%MDESC%Reads signals
def c353_readsignals'{
   local ii
   
   _C353_checkifset   
   for (ii=0; ii<3; ii++) {
         p "*** Channel " ii 
	 p "* Reading toggle mode: "
	 c353_readtog ii
         p "* Reading frequency:"
         c353_readfreq ii
         if (C353_tog[ii] == 0) {
	    p "* Reading shape: "
            c353_readshape ii
	    p "* Reading polarity: "
	    c353_readpol ii
         }
	 p " "
   }
   
   for (ii=0; ii<3; ii++) {
         p "*** Channel " ii 
	 p "* Reading channel phase shift by N RF:"
         c353_Chanrshift ii
	 p " "
         p " "
   }   
	 
   p "*** Reading RF shift"
   c353_RFrshift
   
   p " "
   
   p "*** Reading inhibit"
   c353_readinhibit

}'

#%UU%
#%MDESC% Reads inhibit signal status bit
def c353_readinhibit'{
   _C353_checkifset   
   p ( (esrf_io(C353_dev,"DevReadState") & (1<<9)) != 0)
}'

#%UU%<filename>
#%MDESC%stores the present C353 configuration on <filename>
#(under ~blissadm/local/spec/userconf/C353)
def c353_putconf'{
   local ii mystr myofile mycompletefile ret
   
   if ($# == 0) {
      p "usage: c353_putconf <filename>"
      exit
   }
   myofile = "$1"
   p myofile
   mycompletefile = C353_PATH_SAVE myofile
   if (unix(sprintf("test -s %s",mycompletefile))){
      p "Writing configuration to file " mycompletefile
   }
   else{
      p "file already exists. exit"
      exit
   }    
   ret = open(mycompletefile)
   if (ret == -1) {
      p "cannot open file" mycompletefile
      p "exit"
      exit
   }
   on(mycompletefile);offt
   
   for (ii=0; ii<3; ii++){
      p "C353_chused[" ii "]=" C353_chused[ii]
      if (C353_chused[ii] == 1) {
         p "C353_tog[" ii "]=" C353_tog[ii]
         p "C353_pre1[" ii "]=" C353_pre1[ii]
         p "C353_pre2[" ii "]=" C353_pre2[ii]
         p "C353_pre3[" ii "]=" C353_pre3[ii]
         p "C353_pre4[" ii "]=" C353_pre4[ii]
         if (C353_tog[ii] == 0) {
            p "C353_shape[" ii "]=" C353_shape[ii]
            if (C353_shape[ii] == 0) {
               p "C353_shortp[" ii "]=" C353_shortp[ii]
            }
            else {
               p "C353_longp1[" ii "]=" C353_longp1[ii]
               p "C353_longp2[" ii "]=" C353_longp2[ii]
            }
         }
         p "C353_pol[" ii "]=" C353_pol[ii]
         p "C353_Chanshift[" ii "]=" C353_Chanshift[ii]
      }
   }
   p "C353_RFshift =" C353_RFshift
   
   ont;close(mycompletefile)
}'

#%UU%<filename>
#%MDESC%get the C353 configuration stored in <filename> and
#applies it to the hardware
def c353_getconf'{
#   local myifile 
   local mycompletefilename flag mystr ii

   _C353_checkifset   
   if ($# == 0) {
      p "usage: c353_getconf <filename>"
      exit
   }
  
   _c353_readfile $*

}'   

#%IU%
#%MDESC%applying the last loaded configuration to the C353
def _c353_applyconf'{
   local ii
   p "Setting the C353 with the present parameters"
   
   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){

#        Setting the toggle
	 c353_settog ii C353_tog[ii]

#        Setting the frequency
         p "*** On channel " ii
         c353_setfreq ii  C353_pre1[ii]  C353_pre2[ii] C353_pre3[ii] C353_pre4[ii]

         if (C353_tog[ii] == 0) {
#           Meaningful only if no toggle         
#           Setting the shape  
	    if (C353_shape[ii] == 0) {
	       c353_setshortp ii C353_shortp[ii]
	    }
	    else {
	       c353_setlongp ii C353_longp1[C353_ch] C353_longp2[C353_ch]
	    }
#           Setting the polarity
	    c353_setpol ii C353_pol[ii]
         }
      }
   } 

   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){
#        Setting the channel phase shift by N RF
#        no limit
         if (C353_Chanshift[ii] != 0) {
	    c353_Chanincshift ii C353_Chanshift[ii] 
	 }
      }
   }
	 
   
#  Setting the RF shifting
   c353_RFincshift  C353_RFshift    	 

#  release inhibit (if any)
   c353_on   
    
}'

#%IU% <filename>
#%MDESC%reads the file in ~blissadm/local/spec/userconf/C353 as
#a spec macro file
def _c353_readfile'{
   local myfile ii cont ret myf
   
   myf = "$1"
   myfile = C353_PATH_SAVE myf
   p "reading file " myfile
   cont = 1
   ret = getline(myfile,"open")
   if (ret != -1)
   {
      while(cont == 1) {
         ret = getline(myfile)
         if (ret == -1) {
            cont = 0
            getline(myfile,"close")  
         }
         else {
            eval(ret)  
         }
      }
      _c353_applyconf
   }
}'

#%UU% <gate mode>
#%MDESC% resync according to gate mode
def c353_resync(arg)'{
   local val myp myw

   if ( (arg !=0) && (arg!=1) ) {
      p "usage: c353_resync(arg) with arg=(0:gate mode, 1:resync)"
      exit
   }  
   myp[0] = 1
   myp[1] = 0x1c

   val = esrf_io(C353_dev,"DevReadReg32",myp)
   printf("%x\n",val)

   val = val & 0xfffffffe
   val = val | (arg&0x1)
   printf("%x\n",val)
   myp[2] = val
   
   esrf_io(C353_dev,"DevWriteReg32",myp)
   
}'

#%IU%
#%MDESC%testing macro for storage in arrays only (does not apply to hardware)
def c353_test_setsignals'{
   local ii
   
   _C353_checkifset   
   for (ii=0; ii<3; ii++) {
      C353_chused[ii] = getval(sprintf("use channel %d [0:no, 1:yes]?",ii),C353_chused[ii])
   }
   
   
   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){

#        Setting the toggle
         C353_tog[ii] = getval(sprintf("...ch: %d signal normal(0) or toggled (1) ?",ii),C353_tog[ii])

#        Setting the frequency
         p "*** On channel " ii
         if (C353_tog[ii] == 0) {
            p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4"
         }
         else {
            p "...The frequency divider will be: prescal1 * prescal2 * prescal3 * prescal4 *2 (toggle)"
         }
         C353_pre1[ii] = getval(sprintf("...ch: %d frequency prescal1 [4..34]?",ii),C353_pre1[ii])
         C353_pre2[ii] = getval(sprintf("...ch: %d frequency prescal2 [1..32]?",ii),C353_pre2[ii])
         C353_pre3[ii] = getval(sprintf("...ch: %d frequency prescal3 [1..32]?",ii),C353_pre3[ii])
         C353_pre4[ii] = getval(sprintf("...ch: %d frequency prescal4 [1..32]?",ii),C353_pre4[ii])
         if (C353_tog[ii] == 0) {
            p "... Calculated frequency:  " 352202/C353_pre1[ii]/C353_pre2[ii]/C353_pre3[ii]/C353_pre4[ii] "  khz"
         }
         else {
            p "... Calculated frequency:  " 352202/C353_pre1[ii]/C353_pre2[ii]/C353_pre3[ii]/C353_pre4[ii]/2 "  khz"
         }
         
         if (C353_tog[ii] == 0) {
#           Meaningful only if no toggle         

#           Setting the shape  
            C353_shape[ii] = getval(sprintf("...ch: %d Long shape (1) or short shape (0)?",ii),C353_shape[ii])
	    if (C353_shape[ii] == 0) {
               C353_shortp[ii] = getval(sprintf("...ch: %d Pulse width [2,4..34]?",ii),C353_shortp[ii])
	    }
	    else {
               p "The Long pulse shape will be: prescaler1*prescaler2 + 3 "
               C353_longp1[C353_ch] = getval(sprintf("...ch: %d Long Pulse prescaler1 [5..34]?",ii),C353_longp1[C353_ch])
               C353_longp2[C353_ch] = getval(sprintf("...ch: %d Long Pulse prescaler2 [2..31]?",ii),C353_longp2[C353_ch])
	    }
            
	 
#           Setting the polarity
            C353_pol[ii] = getval(sprintf("...ch: %d polarity normal(0) or inverted (1) ?",ii),C353_pol[ii])
         }
	 
	 p " "
      }
   } 

   for (ii=0; ii<3; ii++) {
      if (C353_chused[ii] == 1){
#        Setting the channel phase shift by N RF
#        no limit
#         C353_Chanshift[ii] = getval(sprintf("...ch: %d channel phase shift by x RF x:[0..100]?",ii),C353_Chanshift[ii]) 
         C353_Chanshift[ii] = getval(sprintf("...ch: %d channel phase shift by N RF ?",ii),C353_Chanshift[ii]) 
      }
   }
	 
   
#  Setting the RF shifting
   C353_RFshift = getval("All channel to RF phase shift [-255..255]?",C353_RFshift)  

#  release inhibit (if any)
   p " "
   p "release inhibit"

}'

#%MACROS%
#%IMACROS%
#%AUTHOR%
# C353.mac mcd jan 07
#%TOC%