x393sata_control.py 3.62 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
from time import sleep
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
9

10 11
LOGFILE = "/var/log/x393sata_control.log"

12 13 14
# constants
RESET_LIMIT = 10

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
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():
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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
79
        log_msg("%s: 0x%08x [%08x]"%(group_range, data, byte_addr))
80 81
        first_line = False
    if fld_value or not skip0:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
82
        log_msg("%8x : %s (%s)"%(fld_value, fld['name'], fld['description'] ))
83
        # the device is there but fails to establish a correct link
84
        if fld['name']=="DIAG.B" or fld['name']=="DIAG.S" or fld['name']=="ERR.E":
85
          result = False
86
        
87 88
  return result

89

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

107

108
mem = x393_mem.X393Mem(0,0,1)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
109 110 111 112 113 114 115
sata = x393sata.x393sata() # 1,0,"10389B")

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

116

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