Skip to content
Snippets Groups Projects
mpodCmd.py 19.24 KiB
#!/usr/bin/python
""" NAME
        mpodCmd.py

    SYNOPSIS
        python module

    DESCRIPTION
        Module to manage the minipods parameters/registers.
        This modules make use of the "minipods" command/program 
        to execute the actions and doesn't calls directly the
        C library like the mpod.py module does.

    OPTIONS
        > ...

    EXAMPLE
        > ...

    AUTHOR
        PY Duval -- 2016

    lICENSE
    (c) Copyright CERN for the benefit of the LHCb Collaboration.
    Distributed under the terms of the GNU General Public Licence V3.
    ana.py script is free and open source software.

    CHANGELOG
        PY Duval 10/02/2017 add the board device number
"""
import subprocess, re, time

def initI2c(clkFreq, SCLFreq):
    """
       set the i2c bus speed

       ./minipods --init-speed --param-pair 125000000,100000
       set bus CLK to 125000000 SCL freq 100000

       no return
    """
    cmd = 'minipods --init-speed --param-pair {0},{1}'.format(clkFreq, SCLFreq)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return output,err

# Can't work because library relaoded at each program execution and thus
# library global variables are also reinitialized
#def getSpeed():
#    """
#       set the i2c bus speed
#
#       ./minipods --get-speed 
#       get the last values set for i2c speed inClock and SCLFreq in Hz
#
#       returns the two values comma separated
#    """
#    cmd = 'minipods --get-speed'
#    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
#    (output, err) = p.communicate()
#    return output,err

def temperature(dev, mpod):
    """
       reads the temperature of minipod number mpod

       ./minipods -m1 --temperature
       minipod 1 internal temperature 40C

       returns the value in celsius degrees as a string
    """
    cmd = 'minipods -b{0} -m{1} --temperature'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    t = re.search('temperature (.+?)C', output)
    if t:
        return t.group(1)
    else:
        return None

def vcc33(dev, mpod):
    """
       reads the 3.3 power of minipod number mpod

       ./minipods -m1 --vcc-3.3
       3.3 power of minipod 1 is 3.23V

       returns the value in Volts as a string
    """
    cmd = 'minipods -b{0} -m{1} --vcc-3.3'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    t = re.search('is (.+?)V', output)
    if t:
        return t.group(1)
    else:
        return None

def vcc25(dev, mpod):
    """
       reads the 2.5 power of minipod number mpod

       ./minipods -m1 --vcc-2.5
       2.5 power of minipod 1 is 2.47V

       returns the value in Volts as a string
    """
    cmd = 'minipods -b{0} -m{1} --vcc-2.5'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    t = re.search('is (.+?)V', output)
    if t:
        return t.group(1)
    else:
        return None

def errorStatus(dev, mpod):
    """
       reads the error status of minipod number mpod

       ./minipods -s -m1 --error-status
       1,1,0,0,0

       order is mpod,fault,los,intL,data-not-ready

       returns a list
    """
    cmd = 'minipods -s -b{0} -m{1} --error-status'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',')

def losStatus(dev, mpod):
    """
       reads the los status of minipod number mpod

       ./minipods -s -m1 --los-status
       1,1,1,1,1,1,1,1,1,1,1,1

       returns a list with status of channels 0 to 11
    """
    cmd = 'minipods -s -b{0} -m{1} --los-status'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',')

def faultStatus(dev, mpod):
    """
       reads the faults status of minipod number mpod (TX only)

       ./minipods -s -m1 --faults-status
       0,0,0,0,0,0,0,0,0,0,0,0

       returns a list with status of channels 0 to 11
    """
    cmd = 'minipods -s -b{0} -m{1} --faults-status'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',')

def biasCurrent(dev, mpod):
    """
       reads the bias current of minipod number mpod (Tx only)

       ./minipods -s -m1 --bias-current
       5926.0,6120.0,5678.0,6054.0,5798.0,6154.0,5800.0,5980.0,5772.0,6066.0,5942.0,5892.0,

       returns a list measures in uA of channels 0 to 11
    """
    cmd = 'minipods -s -b{0} -m{1} --bias-current'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',')

def lightOutput(dev, mpod):
    """
       reads the light power output of minipod number mpod (Tx only)

       ./minipods -s -m1 --light-output
       1025.2,0.11,904.7,-0.43, ... ,871.5,-0.60,

       returns 2 list of measures for channels 0 to 11
                    - list1 is power in uW 
                    - list2 is same in dBm
    """
    cmd = 'minipods -s -b{0} -m{1} --light-output'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    data = output.rstrip('\n').split(',')
    uW = []
    dBm= []
    for i in range(24):
       dBm.append(data[i]) if i%2 else uW.append(data[i])
    return uW, dBm

def lightInput(dev, mpod):
    """
       reads the light power input of minipod number mpod (Rx only)

       ./minipods -s -m1 --light-input
       1025.2,0.11,904.7,-0.43, ... ,871.5,-0.60,

       returns 2 list of measures for channels 0 to 11
                    - list1 is power in uW 
                    - list2 is same in dBm
    """
    cmd = 'minipods -s -b{0} -m{1} --light-input'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    data = output.rstrip('\n').split(',')
    uW = []
    dBm= []
    if not "ERROR" in str(data):
       for i in range(24):
           dBm.append(data[i]) if i%2 else uW.append(data[i])
    return uW, dBm			

def reset(dev, mpod):
    """
      Resets all registers values to their default factory values
      ./minipods -s -m1 --reset
      Reset done on minipod 1

      returns the info string
    """
    cmd = 'minipods -s -b{0} -m{1} --reset'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return output.rstrip('\n')

def channelDump(dev, mpod):
    """
      Function to dump the enable/disable status of a minipods' channel.
      ./minipods -s -m1 --channel-dump
      e,e,e,d,e,e,e,e,e,e,e,e

      returns a list of status: e for enabled, d for disabled
    """
    cmd = 'minipods -s -b{0} -m{1} --channel-dump'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',') 

def enableChannel(dev, mpod,ch,en):
    """
      Function enable/disable channel ch of minipod mpod
      en=0 disable, else enable

      ./minipods -s -m1 --channel-enable -c 3
      or
      ./minipods -s -m1 --channel-disable -c 3
      no output message

      no return
    """
    if en==0:
       cmd = 'minipods -s -b{0} -m{1} --channel-disable -c{2}'.format(dev, mpod,ch)
    else:
       cmd = 'minipods -s -b{0} -m{1} --channel-enable -c{2}'.format(dev, mpod,ch)
    # PYD
    print(cmd)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return

def polarityDump(dev, mpod):
    """
      Function to dump the inverted/normal status of a minipods' channel.
      ./minipods -s -m1 --polarity-dump	
      0,0,0,1,0,0,0,0,0,0,0,0

      returns a list of status: 1 for inverted, 0 for normal
    """
    cmd = 'minipods -s -b{0} -m{1} --polarity-dump'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',') 

def invertPolarity(dev, mpod,ch,inv):
    """
      Function invert/normal channel ch of minipod mpod
      inv=0 normal, else invert

      ./minipods -s -m1 --polarity-invert -c 3
      or
      ./minipods -s -m1 --polarity-normal -c 3
      no output message

      no return
    """
    if inv==0:
       cmd = 'minipods -s -b{0} -m{1} --polarity-normal -c{2}'.format(dev, mpod,ch)
    else:
       cmd = 'minipods -s -b{0} -m{1} --polarity-invert -c{2}'.format(dev, mpod,ch)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return

def squelchDump(dev, mpod):
    """
      Function to dump the squelch enable/disable status of a minipods' channel.
      ./minipods -s -m1 --channel-dump
      e,e,e,d,e,e,e,e,e,e,e,e

      returns a list of status: e for enabled, d for disabled
    """
    cmd = 'minipods -s -b{0} -m{1} --squelch-dump'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',') 

def enableSquelch(dev, mpod,ch,en):
    """
      Function enable/disable squelch of channel ch of minipod mpod
      en=0 disable, else enable

      ./minipods -s -m1 --channel-enable -c 3
      or
      ./minipods -s -m1 --channel-disable -c 3
      no output message

      no return
    """
    if en==0:
       cmd = 'minipods -s -b{0} -m{1} --squelch-disable -c{2}'.format(dev, mpod,ch)
    else:
       cmd = 'minipods -s -b{0} -m{1} --squelch-enable -c{2}'.format(dev, mpod,ch)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return

def marginDump(dev, mpod):
    """
      Function to dump the margin activation/deactivation status of a minipods' channel.  (Tx only)
      ./minipods -s -m1 --margin-dump
      a,a,a,a,d,a,a,a,a,a,a,a

      returns a list of status: a for activated, d for deactivated
    """
    cmd = 'minipods -s -b{0} -m{1} --margin-dump'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return output.rstrip('\n').split(',') 

def activateMargin(dev, mpod,ch,act):
    """
      Function activate/deactivate margin of channel ch of minipod mpod  (Tx only)
      act=0 deactivate, else activate

      ./minipods -s -m1 --margin-deactivation -c 3
      or
      ./minipods -s -m1 --margin-activation -c 3
      no output message

      no return
    """
    if act==0:
       cmd = 'minipods -s -b{0} -m{1} --margin-deactivation -c{2}'.format(dev, mpod,ch)
    else:
       cmd = 'minipods -s -b{0} -m{1} --margin-activation -c{2}'.format(dev, mpod,ch)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    return
 
def vendorInfo(dev, mpod):
    """
      Function to dump the vendor info

       ./minipods -m1 --vendor-info

       vendor name     :AVAGO          
       vendor OUI      :0x00h 0x17h 0x6ah 
       part number     :AFBR-811FN1Z    
       revision number :  
       serial number   :A1452308D       
       date            :20141226

      return a dictionnary of the vendor info
    """
    cmd = 'minipods -s -b{0} -m{1} --vendor-info'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    # split line by line
    lines = output.split("\n");
    data = {}
    for item in lines:
        if len(item)==0:
            continue
        info = item.split(":")
        data[info[0].rstrip()]=info[1].rstrip()
    return data

def readInEqual(dev, mpod):
    """
       reads the input equalization of minipod number mpod (Tx only)

       ./minipods -s -m1 --in-equal-read
       0,  2,2.10,-1.80,3.90,  1,  2,2.10,-1.80,3.90, ... , 11,  2,2.10,-1.80,3.90,

       returns 4 list of data for channels 0 to 11
                    - list1 input code value 
                    - list2 peak value for this code
                    - list3 midband value for this code
                    - list3 width from peak to midband
    """
    cmd = 'minipods -s -b{0} -m{1} --in-equal-read'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    data = output.split(',')
    code = []
    peak= []
    midband = []
    width = []
    if not "ERROR" in str(data):
       for i in range(12):
           code.append(data[5*i+1].lstrip())
           peak.append(data[5*i+2].lstrip())
           midband.append(data[5*i+3].lstrip())
           width.append(data[5*i+4].lstrip())
    return code,peak,midband,width			

def writeInEqual(dev, mpod,ch,val):
    """
      Function to write a new code val for input equalization of channel ch of minipod mpod  (Tx only)

      ./minipods -s -m1 -c5 -v3 --in-equal-write 

      returns nothing
    """
    cmd = 'minipods -s -b{0} -m{1} -c{2} -v{3} --in-equal-write'.format(dev, mpod,ch,val)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return

def readoutAmpVOD(dev, mpod): 
    """
       reads the output amplitude VOD of minipod number mpod (Rx only)

       ./minipods -s -m0 --out-amplitude-read
        0,  4,400,500,600,  1,  4,400,500,600, ... , 11,  4,400,500,600,

       returns 3 list of data for channels 0 to 11
                    - list1 mini value 
                    - list2 nominal value
                    - list3 maxi value
    """
    cmd = 'minipods -s -b{0} -m{1} --out-amplitude-read'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    data = output.split(',')

    chan   = [] # MJ
    code   = []
    mini   = []
    nominal= []
    maxi   = []
    """    
    if not "ERROR" in str(data):
       print data
       for i in range(12):
           code.append(data[5*i+0].lstrip())
           mini.append(data[5*i+1].lstrip())
           nominal.append(data[5*i+2].lstrip())
           maxi.append(data[5*i+3].lstrip())
    return code,mini,nominal,maxi
    """    
    # modify by MJ
    
    if not "ERROR" in str(data):
       #print data
       for i in range(12):
           chan.append(data[5*i+0].lstrip())
           code.append(data[5*i+1].lstrip())
           mini.append(data[5*i+2].lstrip())
           nominal.append(data[5*i+3].lstrip())
           maxi.append(data[5*i+4].lstrip())
    return chan,code,mini,nominal,maxi
    

def writeAmpVOD(dev, mpod,ch,val):
    """
       writes the output amplitude VOD of minipod number mpod (Rx only)

       ./minipods -s -m0 -c4 -v2 --out-amplitude-write      

      returns nothing
    """
    cmd = 'minipods -s -b{0} -m{1} -c{2} -v{3} --out-amplitude-write'.format(dev, mpod,ch,val)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return

def readoutDeamphasis(dev, mpod): 
    """
       reads the output deamphasis of minipod number mpod (Tx only)

       ./minipods -s -m0 --out-deamphas-read
       0,  3, 42%,  1,  3, 42%,  ... , 11,  3, 42%,

       returns 2 list of data for channels 0 to 11
                    - list1 input code value 
                    - list2 deamphasis in %
    """
    cmd = 'minipods -s -b{0} -m{1} --out-deamphas-read'.format(dev, mpod)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    data = output.split(',')
    code = []
    rate= []

    if not "ERROR" in str(data):
       for i in range(12):
           code.append(data[3*i+1].lstrip())
           rate.append(data[3*i+2].lstrip())
    return code,rate

def writeOutDeamphasis(dev, mpod,ch,val):
    """
      Function to write a new val for output deamphasis of channel ch of minipod mpod

      ./minipods -s -m1 -c5 -v3 --out-deamphas-write 

      returns nothing
    """
    cmd = 'minipods -s -b{0} -m{1} -c{2} -v{3} --out-deamphas-write'.format(dev, mpod,ch,val)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    return
 
def main():
   dev = 0
   print 'Start on device {0}'.format(dev)
#   initI2c(125000000,100000)
#   print getSpeed()

#  print vendorInfo(5)    
   dico = vendorInfo(dev, 0)
   print dico['vendor name']
#  print vendorInfo(1)
   
   print "Temperature mpod 1"
   temp = temperature(dev, 1)
   print temp

   print "Power 3.3 mpod 1"
   power = vcc33(dev, 1)
   print power

   print "Power 2.5 mpod 1"
   power = vcc25(dev, 1)
   print power

   print "Error status mpod 1"
   errors = errorStatus(dev, 1)
   print errors

   print "Loss status mpod 1"
   los = losStatus(dev, 1)
   print los

   print "Faults status mpod 1"
   faults = faultStatus(dev, 1)
   print faults

   print "Bias current mpod 1"
   bias = biasCurrent(dev, 1)
   print bias

   print "Light ouput power mpod 1"
   W,db = lightOutput(dev, 1)
   print W
   print db

   print "Light input power mpod 0"
   W,db = lightInput(dev, 0)
   print W
   print db

# ATTENTION: this disturb the following commands
#   print "Reset mpod 1"
#   print reset(1)
   
   print "Dump enable/disable channels status of mpod 4"
   print channelDump(dev, 2)
   print "Disable channel 2 of mpod 4"
   enableChannel(dev, 4,2,0)
   print channelDump(dev, 4)
   
   print "Enable channel 7 of mpod 4"
   enableChannel(dev, 4,7,1)
   print channelDump(dev, 4)

   print "Dump invert/normal channels status of mpod 4"
   print polarityDump(dev, 4)
   print "invert polarity of channel 7 of mpod 4"
   invertPolarity(dev, 2,7,1)
   print polarityDump(dev, 4)

   print "Set normal polarity of channel 7 of mpod 4"
   invertPolarity(dev, 4,7,0)
   print polarityDump(dev, 4)

   print "Dump squelch enable/disable channels status of mpod 4"
   print squelchDump(dev, 4)
   print "Disable squelch on channel 2 of mpod 4"
   enableSquelch(dev, 4,2,0)
   print squelchDump(dev, 4)

   print "Enable squelch on channel 2 of mpod 4"
   enableSquelch(dev, 4,2,1)
   print squelchDump(dev, 4)

   print "Dump margin activation/deactivation status of channels status of mpod 3"
   print marginDump(dev, 3)
   print "Deactivate margin on channel 9 of mpod 3"
   activateMargin(dev, 3,9,0)
   print marginDump(dev, 3)

   print "Activate margin on channel 9 of mpod 3"
   activateMargin(dev, 3,9,1)
   print marginDump(dev, 3)

   print "Input equalization of mpod3"
   code,peak,midband,width = readInEqual(dev, 3)
   print code
   print peak
   print midband
   print width

   print "write 0 in channel 10"
   writeInEqual(dev, 3,10,0)
   print readInEqual(dev, 3)
   
   print "Output deamphasis of minipod 4"
   code,rate = readoutDeamphasis(dev, 4)
   print code
   print rate
   
   print "Changing deamphasis of mpod 4 chanel 9 value 4"
   writeOutDeamphasis(dev, 4,9,4)
   code,rate = readoutDeamphasis(dev, 4)
   print code
   print rate   

   print "output VOD amplitude of mpod 4"
   #code,mini,nominal,maxi = readoutAmpVOD(4) MJ
   code,chan,mini,nominal,maxi = readoutAmpVOD(dev, 4) # MJ
   print code
   print mini
   print nominal
   print maxi

   print "write VOD amplitude of mpod 4 chanel 1 value 2"
   writeAmpVOD(dev, 4,1,2)
   #code,mini,nominal,maxi = readoutAmpVOD(4) MJ
   code,chan,mini,nominal,maxi = readoutAmpVOD(dev, 4) # MJ
   print code
   print mini
   print nominal
   print maxi
   

if __name__ == "__main__":
    main()