123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- """
- Discover and manage connected boards
- """
- import glob
- import logging as log
- from .... import config
- from ... kcgwidget import error
- from communication import pci
- DUMMY_PCI_i_OUTPUT = 'Vendor: 10ee, Device: 6028, Bus: 3, Slot: 0, Function: 0\n\
- Interrupt - Pin: 1, Line: 255\n\
- BAR 0 - MEM32, Start: 0xf5500000, Length: 0x 100000, Flags: 0x00040200\n\
- \n\
- DMA Engines: \n\
- DMA 0 C2S - Type: Packet, Address Width: 32 bits\n\
- \n\
- Banks: \n\
- 0x80 dma: DMA Registers\n\
- '
- class BoardsConnected(object):
- """
- Container for connected/available boards
- This will work as a generator and yield the available board_ids
- NOTE: this is subject to change. In the future this may yield board_names or tuples or something else
- """
- def __init__(self):
- self._board_ids = {}
- self._board_ids_reverse = {}
- self._device_files = {}
- self.count = 0
- def _device_id(self, dev_file):
- """
- Get the device id
- :param dev_file: the dev_file to use
- :return: string for the dev id
- """
- info_string = pci.info(dev_file=dev_file)
- # info_string = DUMMY_PCI_i_OUTPUT
- try:
- return info_string.split('Device: ')[1].split(',')[0]
- except IndexError:
- error(0x002, "Pci returned no or wrong information string. Device probably not found.", severe=True)
- def discover(self):
- """
- Discover connected boards
- This is either a dummy implementation that defaults to /dev/fpga0 for every board
- it will create 'virtual' boards
- OR:
- If config.use_dev_fpga_for_detection == True use number of /dev/fpga devices and use them
- """
- def get(dict_, what): # this will return the dict value for what (Key) or what if what not in dict_
- return dict_.get(what, what)
- if config.board_detection_method == 'dev':
- searchstring = '/dev/fpga'
- # searchstring = '/dev/lp'
- device_list = glob.glob(searchstring+'*')
- log.info("Found {num} devices.".format(num=len(device_list)))
- self._board_ids = {int(i.replace(searchstring, '')): get(config.device_names, self._device_id(i))
- for i in device_list}
- self._device_files = {int(i.replace(searchstring, '')): i for i in device_list}
- elif config.board_detection_method == 'dummy':
- num = 5
- self._board_ids = {i: get(config.device_names, 'test'+str(i)) for i in range(num)}
- self._device_files = {i: '/dev/fpga0' for i in self.board_ids}
- elif config.board_detection_method == 'list':
- self._board_ids = {i: get(config.device_names, self._device_id(d_f)) for i, d_f in enumerate(config.device_list)}
- self._device_files = {i: d_f for i, d_f in enumerate(config.device_list)}
- else:
- raise config.MisconfigurationError("board_detection_method was misconfigured")
- # if no boards detected create a dummy board (everything will be disabled in the gui) so the gui does
- # not crash
- if len(self._board_ids) == 0:
- self._board_ids[0] = None
- self._board_ids_reverse = {v: k for k, v in self._board_ids.items()} # build reverse look up
- @property
- def has_boards(self):
- """
- Check if at least one board is connected
- """
- if len(self.board_ids) == 1 and self._board_ids[0] is None:
- return False
- elif len(self.board_ids) > 0:
- return True
- else:
- return False
- @property
- def board_ids(self):
- """
- Get Board Ids as list
- """
- return self._board_ids.keys()
- @property
- def board_names(self):
- """
- Get Board Names as list
- :return:
- """
- return self._board_ids.values()
- def get_board_name_from_id(self, board_id):
- """
- Get the name of a board with the given board_id
- :param board_id: the board to get the name for
- :return: the name of the board with id board_id
- """
- return self._board_ids[board_id]
- def get_board_id_from_name(self, board_name):
- """
- Get the id of a board with the name board_name
- :param board_name: the name of the board to get the id for
- :return: the id of the board with name board_name
- """
- return self._board_ids_reverse[board_name]
- def get_device_file(self, board_id):
- """
- Get the device file (/dev/...) to use when communicating with the board
- :param board_id: the id of the board
- :return: the device file as string
- """
- return self._device_files[board_id]
- @property
- def multi_board(self):
- """
- If multiple boards are connected this is true else false
- """
- if len(self.board_ids) > 1:
- return True
- return False
- def __iter__(self):
- self.__cur_idx = 0
- return self
- def next(self):
- self.__cur_idx += 1
- if self.__cur_idx > len(self.board_ids):
- raise StopIteration
- return self.board_ids[self.__cur_idx-1]
- def __getitem__(self, item):
- return self.board_ids[item]
- available_boards = BoardsConnected()
- available_boards.discover()
|