123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- """
- Helper methods
- """
- import logging as log
- import time
- from communication import pci
- from errors import *
- from status import StatusStorage
- from board_config import BoardConfiguration
- from .... import config as kcg_config
- def get_dec_from_bits(bits, msb=-1, lsb=-1):
- rtrnValue = 0
- if (msb < 0 or lsb < 0):
- rtrnValue = int(bits)
- elif (msb < lsb) or (msb > 31) or (lsb > 31):
- log.info("Bit range for msb and lsb of get_dec_from_bits was wrong. Not truncating")
- rtrnValue = int(bits, 2)
- else:
- chunk = ('{0:032b}'.format(int(bits, 2)))[31-msb:32-lsb]
- rtrnValue = int(chunk, 2)
- return rtrnValue
- def get_status(board_id):
- """
- Get the satatus of the board (this is used for the status leds)
- :param board_id: the id of the board
- :return: dictionary with the bits for each led (lower case led names are the keys of this dict)
- """
- registers = pci.read(board_id, 3, '0x9050', decimal=True)
- bits = []
- bits += ['{0:032b}'.format(registers[0])]
- bits += ['{0:032b}'.format(registers[1])]
- bits += ['{0:032b}'.format(registers[2])]
- status = {}
- s1 = get_dec_from_bits(bits[0], 2, 0)
- if s1 == 0:
- # Pipeline in reset mode
- # self.pipeline_led.set_tri()
- status['pipeline'] = 2
- elif s1 == 1:
- # Pipeline is idle
- # self.pipeline_led.set_on()
- status['pipeline'] = 3
- elif s1 == 6:
- # Pipeline in error state
- # self.pipeline_led.set_off()
- status['pipeline'] = 1
- else:
- # Should not happen!
- # self.pipeline_led.set_out()
- status['pipeline'] = 0
- s2 = get_dec_from_bits(bits[0], 29, 26)
- if s2 == 0:
- # Master Control in reset mode
- # self.master_control_led.set_tri()
- status['master_control'] = 2
- elif s2 == 1:
- # Master Control is idle
- # self.master_control_led.set_on()
- status['master_control'] = 3
- elif s2 == 8:
- # Master Control in error state
- # self.master_control_led.set_off()
- status['master_control'] = 1
- else:
- # Should not happen!
- # self.master_control_led.set_out()
- status['master_control'] = 0
- s3 = get_dec_from_bits(bits[2], 15, 12)
- if s3 == 15:
- # Data Check Idle
- # self.data_check_led.set_on()
- status['data_check'] = 3
- else:
- # Data Check Error
- # self.data_check_led.set_off()
- status['data_check'] = 1
- s4 = int(bits[0][7])
- if s4 == 0:
- # PLL_LD not active
- # self.pll_ld_led.set_tri()
- status['PLL_LD'] = 2
- elif s4 == 1:
- # PLL_LD is active
- # self.pll_ld_led.set_on()
- status['PLL_LD'] = 3
- else:
- status['PLL_LD'] = 0
- return status
- def is_conneced(board_id):
- try:
- pci.read(board_id, 1, '0x9040')
- return True
- except BoardError:
- return False
- except InterfaceNotFoundError:
- return None
- def is_active(board_id):
- control = pci.read(board_id, 1, '0x9040')[0]
- control_bits = '{0:032b}'.format(int(control, 16))
- return control_bits[22:26] == "1111"
- def wait_for_revolutions(board_id):
- n = pci.read(board_id, 1, '0x9020', decimal=True)[0] # Get the amount of orbits to observe
- # n = 1 # Use this for debugging purposes if no board is connected
- spin_time_ns = kcg_config.tRev * n
- time.sleep(spin_time_ns * 1.1) # 10% Safety margin
- _status = []
- _configs = []
- def create_new_board_config(identifier):
- """
- This creates a new instance of BoardConfiguration and also a new instance of StatusStorage
- :param identifier: the identifier for this board (not the id)
- :return:
- """
- global _configs
- global _status
- _configs.append(BoardConfiguration(identifier))
- _status.append(StatusStorage())
- def get_board_config(id):
- return _configs[id]
- def get_board_status(id):
- return _status[id]
|