utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """
  2. Helper methods
  3. """
  4. import logging as log
  5. import time
  6. from communication import pci
  7. from errors import *
  8. from status import StatusStorage
  9. from board_config import BoardConfiguration
  10. from .... import config as kcg_config
  11. def get_dec_from_bits(bits, msb=-1, lsb=-1):
  12. rtrnValue = 0
  13. if (msb < 0 or lsb < 0):
  14. rtrnValue = int(bits)
  15. elif (msb < lsb) or (msb > 31) or (lsb > 31):
  16. log.info("Bit range for msb and lsb of get_dec_from_bits was wrong. Not truncating")
  17. rtrnValue = int(bits, 2)
  18. else:
  19. chunk = ('{0:032b}'.format(int(bits, 2)))[31-msb:32-lsb]
  20. rtrnValue = int(chunk, 2)
  21. return rtrnValue
  22. def get_status(board_id):
  23. """
  24. Get the satatus of the board (this is used for the status leds)
  25. :param board_id: the id of the board
  26. :return: dictionary with the bits for each led (lower case led names are the keys of this dict)
  27. """
  28. registers = pci.read(board_id, 3, '0x9050', decimal=True)
  29. bits = []
  30. bits += ['{0:032b}'.format(registers[0])]
  31. bits += ['{0:032b}'.format(registers[1])]
  32. bits += ['{0:032b}'.format(registers[2])]
  33. status = {}
  34. s1 = get_dec_from_bits(bits[0], 2, 0)
  35. if s1 == 0:
  36. # Pipeline in reset mode
  37. # self.pipeline_led.set_tri()
  38. status['pipeline'] = 2
  39. elif s1 == 1:
  40. # Pipeline is idle
  41. # self.pipeline_led.set_on()
  42. status['pipeline'] = 3
  43. elif s1 == 6:
  44. # Pipeline in error state
  45. # self.pipeline_led.set_off()
  46. status['pipeline'] = 1
  47. else:
  48. # Should not happen!
  49. # self.pipeline_led.set_out()
  50. status['pipeline'] = 0
  51. s2 = get_dec_from_bits(bits[0], 29, 26)
  52. if s2 == 0:
  53. # Master Control in reset mode
  54. # self.master_control_led.set_tri()
  55. status['master_control'] = 2
  56. elif s2 == 1:
  57. # Master Control is idle
  58. # self.master_control_led.set_on()
  59. status['master_control'] = 3
  60. elif s2 == 8:
  61. # Master Control in error state
  62. # self.master_control_led.set_off()
  63. status['master_control'] = 1
  64. else:
  65. # Should not happen!
  66. # self.master_control_led.set_out()
  67. status['master_control'] = 0
  68. s3 = get_dec_from_bits(bits[2], 15, 12)
  69. if s3 == 15:
  70. # Data Check Idle
  71. # self.data_check_led.set_on()
  72. status['data_check'] = 3
  73. else:
  74. # Data Check Error
  75. # self.data_check_led.set_off()
  76. status['data_check'] = 1
  77. s4 = int(bits[0][7])
  78. if s4 == 0:
  79. # PLL_LD not active
  80. # self.pll_ld_led.set_tri()
  81. status['PLL_LD'] = 2
  82. elif s4 == 1:
  83. # PLL_LD is active
  84. # self.pll_ld_led.set_on()
  85. status['PLL_LD'] = 3
  86. else:
  87. status['PLL_LD'] = 0
  88. return status
  89. def is_conneced(board_id):
  90. try:
  91. pci.read(board_id, 1, '0x9040')
  92. return True
  93. except BoardError:
  94. return False
  95. except InterfaceNotFoundError:
  96. return None
  97. def is_active(board_id):
  98. control = pci.read(board_id, 1, '0x9040')[0]
  99. control_bits = '{0:032b}'.format(int(control, 16))
  100. return control_bits[22:26] == "1111"
  101. def wait_for_revolutions(board_id):
  102. n = pci.read(board_id, 1, '0x9020', decimal=True)[0] # Get the amount of orbits to observe
  103. # n = 1 # Use this for debugging purposes if no board is connected
  104. spin_time_ns = kcg_config.tRev * n
  105. time.sleep(spin_time_ns * 1.1) # 10% Safety margin
  106. _status = []
  107. _configs = []
  108. def create_new_board_config(identifier):
  109. """
  110. This creates a new instance of BoardConfiguration and also a new instance of StatusStorage
  111. :param identifier: the identifier for this board (not the id)
  112. :return:
  113. """
  114. global _configs
  115. global _status
  116. _configs.append(BoardConfiguration(identifier))
  117. _status.append(StatusStorage())
  118. def get_board_config(id):
  119. return _configs[id]
  120. def get_board_status(id):
  121. return _status[id]