def _init_gpio(): ret_value = True initconfig = Profile.get_initconfig() digital_io = Profile.get_ioes() if 'gpio' in initconfig.keys(): for io_id, param in initconfig['gpio'].iteritems(): try: device_path = digital_io[io_id]['path'] if Exgpio.dir_set(device_path, (param['dir'], )) is not False: logger.boot( 'set the %s gpio number %s direction:%s success' % (io_id, param['dir'][0], param['dir'][1])) if 'value' in param.keys(): if Exgpio.set(device_path, (param['value'], )) is not False: logger.boot( 'set the %s gpio number %s value:%s success' % (io_id, param['value'][0], param['value'][1])) else: logger.boot( 'warning: set the %s gpio number %s value:%s fail' % (io_id, param['value'][0], param['value'][1])) ret_value = False else: logger.boot( 'warning the %s gpio number %s direction:%s fail' % (io_id, param['dir'][0], param['dir'][1])) ret_value = False except Exception as e: logger.boot("error: %s _init_gpio execute error:\n%s" % (io_id, traceback.format_exc())) ret_value = False return ret_value
def test_initconfig(): initconfig = Profile.get_initconfig() assert len(initconfig['extendio']['instrument']) == 8 assert len(initconfig['boards']) == 4 assert initconfig['extendio']['instrument']['cp1']['value'][0] == int( '0x0', 16) assert initconfig['extendio']['instrument']['cp1']['value'][1] == int( '0x0', 16) assert initconfig['extendio']['instrument']['cp1']['dir'][0] == int( '0x1f', 16) assert initconfig['extendio']['instrument']['cp1']['dir'][1] == int( '0x0', 16)
def init(): initok = True logger.boot('start init') if _create_chip_object() is False: initok = False if _create_board_object() is False: initok = False if _create_uart_object() is False: initok = False initconfig = Profile.get_initconfig() #init gpio if _init_gpio() is False: initok = False #initexitendio if initconfig.has_key('extendio'): if _init_extendio() is False: initok = False #init board if initconfig.has_key('boards'): if _init_board() is False: initok = False #init net try: if _init_net() is False: initok = False except Exception as e: logger.boot("error: _init_net execute error:\n%s" % (traceback.format_exc())) initok = False #init uart if initconfig.has_key('uart'): if _init_uart() is False: initok = False if not initok: logger.boot('error: init project fail') else: logger.boot("init project done") return initok
def parse_chip_profile(chip_profile, board_name): chipname = chip_profile['id'] extendios = Profile.get_extendio() extendios[board_name] = extendios.setdefault(board_name, OrderedDict()) extendio = extendios[board_name] extendio[chipname] = dict() extendio[chipname]['bus'] = Profile.get_bus_path(chip_profile['bus']) extendio[chipname]['addr'] = int(chip_profile['addr'], 16) extendio[chipname]['partno'] = chip_profile['partno'] extendio[chipname]['switch_channel'] = chip_profile['switch_channel'] chips = Profile.get_chips() chips[chipname] = extendio[chipname] bit_values = 0 bit_dir = 0 dir_value = {'input': 1, 'output': 0} for pin in chip_profile['property']: pinname = pin['id'] extendio[pinname] = dict() extendio[pinname]['pin'] = pin['pin'] extendio[pinname]['chip'] = chipname if 'default' in pin.keys(): bit_values |= (int(pin['default']) << (int(pin['pin']) - 1)) if 'dir' in pin.keys(): bit_dir |= (dir_value[pin['dir']] << (int(pin['pin']) - 1)) value_list = [] dir_list = [] for x in xrange(len(chip_profile['property']) / 8): value_list.append((bit_values >> (x * 8)) & 0xff) dir_list.append((bit_dir >> (x * 8)) & 0xff) initconfig = Profile.get_initconfig() initconfig['extendio'] = initconfig.setdefault('extendio', OrderedDict()) initconfig['extendio'][board_name] = initconfig['extendio'].setdefault( board_name, OrderedDict()) initconfig['extendio'][board_name][chipname] = dict(dir=dir_list, value=value_list)
def _init_extendio(): ret_value = True extendios = Profile.get_extendio() initconfig = Profile.get_initconfig() for board_name, extendio_profile in extendios.iteritems(): for chip_name, bit in initconfig['extendio'][board_name].iteritems(): try: partno = extendio_profile[chip_name]['partno'] if Extendio.chip_dir_set(extendio_profile, chip_name, bit['dir']) is True: ret = Extendio.chip_set(extendio_profile, chip_name, bit['value']) if ret is False: logger.boot( 'error: set the %s board %s chip %s value:%s fail' % (board_name, partno, chip_name, logger.print_list2hex(bit['value']))) ret_value = False else: logger.boot( 'init the %s board extendio chip %s success, value is %s, direction is %s' % (board_name, chip_name, logger.print_list2hex(bit['value']), logger.print_list2hex(bit['dir']))) else: logger.boot( 'error: set the %s board %s chip %s direction:%s fail' % (board_name, partno, chip_name, logger.print_list2hex(bit['dir']))) ret_value = False except Exception: logger.boot("error: %s _init_extendio execute error:\n%s" % (chip_name, traceback.format_exc())) ret_value = False return ret_value
def _init_board(): ret_value = True initconfig = Profile.get_initconfig() for board_name in initconfig['boards']: try: obj = XObject.get_board_object(board_name) if obj is None: logger.boot('warning: can not find the %s object' % (board_name)) continue if obj.board_initial(): logger.boot('init the %s board success' % (board_name)) else: logger.boot('error: init the %s board fail' % (board_name)) ret_value = False except Exception: logger.boot('error: %s _init_board execute error:\n%s' % (board_name, traceback.format_exc())) ret_value = False return ret_value
def _init_uart(): ret_value = True initconfig = Profile.get_initconfig() for name, param in initconfig['uart'].iteritems(): try: obj = XObject.get_object(name) if obj is None: logger.boot("warning: can not find the %s uart object" % (name)) continue baudrate = int(param['baudrate']) databits = int(param['databits']) stopbits = int(param['stopbits']) parity = param['parity'].upper() if utility.mount_on_fpga(obj.name): timestamp = param['timestamp'].upper() obj.disable() obj.enable() ret = obj.config(baudrate, databits, parity, stopbits, timestamp) else: ret = obj.config(baudrate, databits, parity, stopbits) if ret is False: logger.boot("error: init the %s uart param:%s fail" % (name, param)) ret_value = False else: logger.boot("init the %s uart success, param:%s" % (name, param)) except Exception as e: logger.boot("error: %s _init_uart execute error:\n%s" % (name, traceback.format_exc())) ret_value = False return ret_value
def _get_network_num(): profile = Profile.get_initconfig() if profile.has_key("netconfig"): net.network_config(profile["netconfig"]) if profile["netconfig"].has_key("netio"): bit_dict = OrderedDict() net_num = 0 index = 0 bit_list = [] net_ioes = [] if "board_id" not in profile["netconfig"][ "netio"] and "path" not in profile["netconfig"]["netio"]: if profile["netconfig"]["netio"].has_key("io"): for io in profile["netconfig"]["netio"]["io"]: net_ioes.append(io['bit']) try: bit_dict = Extendio.get(Profile.get_extendio_by_name(), net_ioes) if bit_dict is False: return -1 except Exception: return -1 for key, value in bit_dict.items(): net_num = value << index | net_num index += 1 return net_num if "board_id" in profile["netconfig"]["netio"]: board_id = profile["netconfig"]["netio"]["board_id"] for io in profile["netconfig"]["netio"]["io"]: net_ioes.append(io['bit']) try: bit_dict = Extendio.get( Profile.get_extendio_by_name(board_id), net_ioes) if bit_dict is False: return -1 except Exception: return -1 for key, value in bit_dict.items(): net_num = value << index | net_num index += 1 return net_num if "path" in profile["netconfig"]["netio"]: gpio_input = [] path = profile["netconfig"]["netio"]["path"] for io in profile["netconfig"]["netio"]["io"]: regular_expression = re.match(r'ch(\w+)_(\w+)', io["bit"]) if regular_expression is None: logger.error('_init_net error, please check %s. ' % io) return -1 else: bit_number_1 = int(regular_expression.group(1), 10) bit_number_2 = int(regular_expression.group(2), 10) bit_number = bit_number_1 * 32 + bit_number_2 gpio_input.append((bit_number, 255)) try: bit_list = Exgpio.get(path, gpio_input) for value in bit_list: net_num = value[1] << index | net_num index += 1 except Exception: return -1 return net_num else: return 0 else: return 0 else: return 0