x393sata_control.py 3.59 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7
#!/usr/bin/env python

from __future__ import print_function
from __future__ import division
import x393sata
import x393_mem
import sys
8
import time
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
LOGFILE = "/var/log/x393sata_control.log"

def colorize(string, color, bold):
    color=color.upper()
    attr = []
    if color == 'RED':
        attr.append('31')
    elif color == 'GREEN':    
        attr.append('32')
    elif color == 'YELLOW':    
        attr.append('33')
    elif color == 'BLUE':    
        attr.append('34')
    elif color == 'MAGENTA':    
        attr.append('35')
    elif color == 'CYAN':    
        attr.append('36')
    elif color == 'GRAY':    
        attr.append('37')
    else:
        pass
        # red
    if bold:
        attr.append('1')
    return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
  
def log_msg(msg, mode=0):
    bold = False
    color = ""
    if mode == 2: #bold red - error
        color = "RED"
        bold = True
    elif mode == 3: # just bold
        bold = True
    elif mode == 4: # just bold
        bold = True
        color = "YELLOW" #warning
            
    with open ('/proc/uptime') as f: 
        t=float(f.read().split()[0])
    with open(LOGFILE,'a') as msg_file:
        print("[%8.2f]  %s"%(t,msg),file=msg_file)
    if bold or color:
        msg = colorize(msg,color,bold)    
    print (colorize("[%8.2f] %s: "%(t, sys.argv[0].split('/')[-1].split('.')[0]),'CYAN',0)+msg)

def connection_errors():
  result = True
  skip0 = True
  MAXI1_ADDR = 0x80000000
  group_range = "HBA_PORT__PxSERR"
  cached_addr = None
  cached_data = None
  first_line = True
  
  range_defines = sata.register_defines[group_range]
  
  for fld in range_defines:  
    byte_addr = 4 * fld['dword_address'] + MAXI1_ADDR
    if byte_addr != cached_addr:
        cached_addr = byte_addr
        cached_data = mem.read_mem(cached_addr)
    data = cached_data
    fld_value = (data >> fld['start_bit']) & ((1 << fld['num_bits']) - 1)
    if first_line:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
75
        log_msg("%s: 0x%08x [%08x]"%(group_range, data, byte_addr))
76 77
        first_line = False
    if fld_value or not skip0:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
78
        log_msg("%8x : %s (%s)"%(fld_value, fld['name'], fld['description'] ))
79
        # the device is there but fails to establish a correct link
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
80
        if fld['name']=="DIAG.B" or fld['name']=="DIAG.S":
81 82 83 84 85
          result = False
  return result

def reset_device():
  result = False
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
86 87 88 89
  
  #sata.reset_device()
  sata.reset_ie()
  
90 91 92 93 94 95 96 97 98 99 100 101 102 103
  for i in range(reset_limit):
    if not connection_errors():
      log_msg("connection error ("+str(i)+"), resetting device",4)
      sata.reset_device()
      sata.reset_ie()
    else:
      if i!=0: 
        log_msg("resetting device: success")
      result = True
      break
    time.sleep(1)
  return result

mem = x393_mem.X393Mem(0,0,1)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
104 105 106 107 108 109 110
sata = x393sata.x393sata() # 1,0,"10389B")

if len(sys.argv) > 1:
  cmd = sys.argv[1]
else:
  cmd = "donothing"

111 112
reset_limit = 10

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
113
if   cmd == "set_zynq_ssd":
114
  sata.vsc3304.disconnect_all()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115
  sata.set_zynq_ssd()
116
  reset_device()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
117
elif cmd == "set_zynq_esata":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
118
  sata.vsc3304.disconnect_all()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
119
  sata.set_zynq_esata()
120
  reset_device()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
121
elif cmd == "set_zynq_ssata":
122
  sata.vsc3304.disconnect_all()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
123
  sata.set_zynq_ssata()
124
  reset_device()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
125
elif cmd == "set_esata_ssd":
126
  sata.vsc3304.disconnect_all()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
127
  sata.set_esata_ssd()
128
  reset_device()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
129 130
else:
  print("Usage:")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
131 132
  print("    * camera <-> internal SSD  :            x393sata_control.py set_zynq_ssd")
  print("    * camera <-> external disk :            x393sata_control.py set_zynq_esata")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
133
  print("    * camera <-> external disk (crossover): x393sata_control.py set_zynq_ssata")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
134
  print("    * PC <-> internal SSD)     :            x393sata_control.py set_esata_ssd")