Laurent@2111: import os Laurent@2111: Laurent@2111: from EthercatSlave import ExtractHexDecValue, DATATYPECONVERSION, ExtractName Laurent@2111: Laurent@2111: SLAVE_PDOS_CONFIGURATION_DECLARATION = """ Laurent@2111: /* Slave %(slave)d, "%(device_type)s" Laurent@2111: * Vendor ID: 0x%(vendor).8x Laurent@2111: * Product code: 0x%(product_code).8x Laurent@2111: * Revision number: 0x%(revision_number).8x Laurent@2111: */ Laurent@2111: Laurent@2111: ec_pdo_entry_info_t slave_%(slave)d_pdo_entries[] = { Laurent@2111: %(pdos_entries_infos)s Laurent@2111: }; Laurent@2111: Laurent@2111: ec_pdo_info_t slave_%(slave)d_pdos[] = { Laurent@2111: %(pdos_infos)s Laurent@2111: }; Laurent@2111: Laurent@2111: ec_sync_info_t slave_%(slave)d_syncs[] = { Laurent@2111: %(pdos_sync_infos)s Laurent@2111: {0xff} Laurent@2111: }; Laurent@2111: """ Laurent@2111: Laurent@2111: SLAVE_CONFIGURATION_TEMPLATE = """ Laurent@2111: if (!(slave%(slave)d = ecrt_master_slave_config(master, %(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x))) { Edouard@2116: SLOGF(LOG_CRITICAL, "EtherCAT failed to get slave %(device_type)s configuration at alias %(alias)d and position %(position)d."); Edouard@2121: goto ecat_failed; Laurent@2111: } Laurent@2111: Laurent@2111: if (ecrt_slave_config_pdos(slave%(slave)d, EC_END, slave_%(slave)d_syncs)) { Edouard@2116: SLOGF(LOG_CRITICAL, "EtherCAT failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d."); Edouard@2117: goto ecat_failed; Laurent@2111: } Laurent@2111: """ Laurent@2111: Laurent@2111: SLAVE_INITIALIZATION_TEMPLATE = """ Laurent@2111: { Laurent@2111: uint8_t value[%(data_size)d]; Laurent@2111: EC_WRITE_%(data_type)s((uint8_t *)value, %(data)s); Laurent@2111: if (ecrt_master_sdo_download(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &abort_code)) { Edouard@2116: SLOGF(LOG_CRITICAL, "EtherCAT Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%d", abort_code); Edouard@2117: goto ecat_failed; Laurent@2111: } Laurent@2111: } Laurent@2111: """ Laurent@2111: Laurent@2111: SLAVE_OUTPUT_PDO_DEFAULT_VALUE = """ Laurent@2111: { Laurent@2111: uint8_t value[%(data_size)d]; Laurent@2111: if (ecrt_master_sdo_upload(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &result_size, &abort_code)) { Edouard@2116: SLOGF(LOG_CRITICAL, "EtherCAT failed to get default value for output PDO in slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%ud", abort_code); Edouard@2117: goto ecat_failed; Laurent@2111: } Laurent@2111: %(real_var)s = EC_READ_%(data_type)s((uint8_t *)value); Laurent@2111: } Laurent@2111: """ Laurent@2111: Laurent@2111: def ConfigureVariable(entry_infos, str_completion): Laurent@2111: entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None) Laurent@2111: if entry_infos["data_type"] is None: Laurent@2111: raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"] Laurent@2111: Laurent@2111: if not entry_infos.get("no_decl", False): Laurent@2111: if entry_infos.has_key("real_var"): Laurent@2111: str_completion["located_variables_declaration"].append( Laurent@2111: "IEC_%(var_type)s %(real_var)s;" % entry_infos) Laurent@2111: else: Laurent@2111: entry_infos["real_var"] = "beremiz" + entry_infos["var_name"] Laurent@2111: str_completion["located_variables_declaration"].extend( Laurent@2111: ["IEC_%(var_type)s %(real_var)s;" % entry_infos, Laurent@2111: "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos]) Laurent@2111: for declaration in entry_infos.get("extra_declarations", []): Laurent@2111: entry_infos["extra_decl"] = declaration Laurent@2111: str_completion["located_variables_declaration"].append( Laurent@2111: "IEC_%(var_type)s *%(extra_decl)s = &%(real_var)s;" % entry_infos) Laurent@2111: elif not entry_infos.has_key("real_var"): Laurent@2111: entry_infos["real_var"] = "beremiz" + entry_infos["var_name"] Laurent@2111: Laurent@2111: str_completion["used_pdo_entry_offset_variables_declaration"].append( Laurent@2111: "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos) Laurent@2111: Laurent@2111: if entry_infos["data_type"] == "BIT": Laurent@2111: str_completion["used_pdo_entry_offset_variables_declaration"].append( Laurent@2111: "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x_bit;" % entry_infos) Laurent@2111: Laurent@2111: str_completion["used_pdo_entry_configuration"].append( Laurent@2111: (" {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, " + Laurent@2111: "0x%(index).4x, %(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x, " + Laurent@2111: "&slave%(slave)d_%(index).4x_%(subindex).2x_bit},") % entry_infos) Laurent@2111: Laurent@2111: if entry_infos["dir"] == "I": Laurent@2111: str_completion["retrieve_variables"].append( Laurent@2111: (" %(real_var)s = EC_READ_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + Laurent@2111: "slave%(slave)d_%(index).4x_%(subindex).2x_bit);") % entry_infos) Laurent@2111: elif entry_infos["dir"] == "Q": Laurent@2111: str_completion["publish_variables"].append( Laurent@2111: (" EC_WRITE_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + Laurent@2111: "slave%(slave)d_%(index).4x_%(subindex).2x_bit, %(real_var)s);") % entry_infos) Laurent@2111: Laurent@2111: else: Laurent@2111: str_completion["used_pdo_entry_configuration"].append( Laurent@2111: (" {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, 0x%(index).4x, " + Laurent@2111: "%(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x},") % entry_infos) Laurent@2111: Laurent@2111: if entry_infos["dir"] == "I": Laurent@2111: str_completion["retrieve_variables"].append( Laurent@2111: (" %(real_var)s = EC_READ_%(data_type)s(domain1_pd + " + Laurent@2111: "slave%(slave)d_%(index).4x_%(subindex).2x);") % entry_infos) Laurent@2111: elif entry_infos["dir"] == "Q": Laurent@2111: str_completion["publish_variables"].append( Laurent@2111: (" EC_WRITE_%(data_type)s(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + Laurent@2111: "%(real_var)s);") % entry_infos) Laurent@2111: Laurent@2111: def ExclusionSortFunction(x, y): Laurent@2111: if x["matching"] == y["matching"]: Laurent@2111: if x["assigned"] and not y["assigned"]: Laurent@2111: return -1 Laurent@2111: elif not x["assigned"] and y["assigned"]: Laurent@2111: return 1 Laurent@2111: return cmp(x["count"], y["count"]) Laurent@2111: return -cmp(x["matching"], y["matching"]) Laurent@2111: Laurent@2111: class _EthercatCFileGenerator: Laurent@2111: Laurent@2111: def __init__(self, controler): Laurent@2111: self.Controler = controler Laurent@2111: Laurent@2111: self.Slaves = [] Laurent@2111: self.UsedVariables = {} Laurent@2111: Laurent@2111: def __del__(self): Laurent@2111: self.Controler = None Laurent@2111: Laurent@2111: def DeclareSlave(self, slave_index, slave): Laurent@2111: self.Slaves.append((slave_index, slave.getInfo().getAutoIncAddr(), slave)) Laurent@2111: Laurent@2111: def DeclareVariable(self, slave_index, index, subindex, iec_type, dir, name, no_decl=False): Laurent@2111: slave_variables = self.UsedVariables.setdefault(slave_index, {}) Laurent@2111: Laurent@2111: entry_infos = slave_variables.get((index, subindex), None) Laurent@2111: if entry_infos is None: Laurent@2111: slave_variables[(index, subindex)] = { Laurent@2111: "infos": (iec_type, dir, name, no_decl, []), Laurent@2111: "mapped": False} Laurent@2111: return name Laurent@2111: elif entry_infos["infos"][:2] == (iec_type, dir): Laurent@2111: if name != entry_infos["infos"][2]: Laurent@2111: if dir == "I": Laurent@2111: entry_infos["infos"][4].append(name) Laurent@2111: return entry_infos["infos"][2] Laurent@2111: else: Laurent@2111: raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name) Laurent@2111: else: Laurent@2111: raise ValueError, _("Definition conflict for location \"%s\"") % name Laurent@2111: Laurent@2111: def GenerateCFile(self, filepath, location_str, master_number): Laurent@2111: Laurent@2111: # Extract etherlab master code template Laurent@2111: plc_etherlab_filepath = os.path.join(os.path.split(__file__)[0], "plc_etherlab.c") Laurent@2111: plc_etherlab_file = open(plc_etherlab_filepath, 'r') Laurent@2111: plc_etherlab_code = plc_etherlab_file.read() Laurent@2111: plc_etherlab_file.close() Laurent@2111: Laurent@2111: # Initialize strings for formatting master code template Laurent@2111: str_completion = { Laurent@2111: "location": location_str, Laurent@2111: "master_number": master_number, Laurent@2111: "located_variables_declaration": [], Laurent@2111: "used_pdo_entry_offset_variables_declaration": [], Laurent@2111: "used_pdo_entry_configuration": [], Laurent@2111: "pdos_configuration_declaration": "", Laurent@2111: "slaves_declaration": "", Laurent@2111: "slaves_configuration": "", Laurent@2111: "slaves_output_pdos_default_values_extraction": "", Laurent@2111: "slaves_initialization": "", Laurent@2111: "retrieve_variables": [], Laurent@2111: "publish_variables": [], Laurent@2111: } Laurent@2111: Laurent@2111: # Initialize variable storing variable mapping state Laurent@2111: for slave_entries in self.UsedVariables.itervalues(): Laurent@2111: for entry_infos in slave_entries.itervalues(): Laurent@2111: entry_infos["mapped"] = False Laurent@2111: Laurent@2111: # Sort slaves by position (IEC_Channel) Laurent@2111: self.Slaves.sort() Laurent@2111: # Initialize dictionary storing alias auto-increment position values Laurent@2111: alias = {} Laurent@2111: Laurent@2111: # Generating code for each slave Laurent@2111: for (slave_idx, slave_alias, slave) in self.Slaves: Laurent@2111: type_infos = slave.getType() Laurent@2111: Laurent@2111: # Defining slave alias and auto-increment position Laurent@2111: if alias.get(slave_alias) is not None: Laurent@2111: alias[slave_alias] += 1 Laurent@2111: else: Laurent@2111: alias[slave_alias] = 0 Laurent@2111: slave_pos = (slave_alias, alias[slave_alias]) Laurent@2111: Laurent@2111: # Extract slave device informations Laurent@2111: device, alignment = self.Controler.GetModuleInfos(type_infos) Laurent@2111: if device is not None: Laurent@2111: Laurent@2111: # Extract slaves variables to be mapped Laurent@2111: slave_variables = self.UsedVariables.get(slave_idx, {}) Laurent@2111: Laurent@2111: # Extract slave device object dictionary entries Laurent@2111: device_entries = device.GetEntriesList() Laurent@2111: Laurent@2111: # Adding code for declaring slave in master code template strings Laurent@2111: for element in ["vendor", "product_code", "revision_number"]: Laurent@2111: type_infos[element] = ExtractHexDecValue(type_infos[element]) Laurent@2111: type_infos.update(dict(zip(["slave", "alias", "position"], (slave_idx,) + slave_pos))) Laurent@2111: Laurent@2111: # Extract slave device CoE informations Laurent@2111: device_coe = device.getCoE() Laurent@2111: if device_coe is not None: Laurent@2111: Laurent@2111: # If device support CanOpen over Ethernet, adding code for calling Laurent@2111: # init commands when initializing slave in master code template strings Laurent@2111: initCmds = [] Laurent@2111: for initCmd in device_coe.getInitCmd(): Laurent@2111: initCmds.append({ Laurent@2111: "Index": ExtractHexDecValue(initCmd.getIndex()), Laurent@2111: "Subindex": ExtractHexDecValue(initCmd.getSubIndex()), Laurent@2111: "Value": initCmd.getData().getcontent()}) Laurent@2111: initCmds.extend(slave.getStartupCommands()) Laurent@2111: for initCmd in initCmds: Laurent@2111: index = initCmd["Index"] Laurent@2111: subindex = initCmd["Subindex"] Laurent@2111: entry = device_entries.get((index, subindex), None) Laurent@2111: if entry is not None: Laurent@2111: data_size = entry["BitSize"] / 8 Laurent@2111: data_str = ("0x%%.%dx" % (data_size * 2)) % initCmd["Value"] Laurent@2111: init_cmd_infos = { Laurent@2111: "index": index, Laurent@2111: "subindex": subindex, Laurent@2111: "data": data_str, Laurent@2111: "data_type": DATATYPECONVERSION.get(entry["Type"]), Laurent@2111: "data_size": data_size Laurent@2111: } Laurent@2111: init_cmd_infos.update(type_infos) Laurent@2111: str_completion["slaves_initialization"] += SLAVE_INITIALIZATION_TEMPLATE % init_cmd_infos Laurent@2111: Laurent@2111: # Extract slave device PDO configuration capabilities Laurent@2111: PdoAssign = device_coe.getPdoAssign() Laurent@2111: PdoConfig = device_coe.getPdoConfig() Laurent@2111: else: Laurent@2111: PdoAssign = PdoConfig = False Laurent@2111: Laurent@2111: # Test if slave has a configuration or need one Laurent@2111: if len(device.getTxPdo() + device.getRxPdo()) > 0 or len(slave_variables) > 0 and PdoConfig and PdoAssign: Laurent@2111: Laurent@2111: str_completion["slaves_declaration"] += "static ec_slave_config_t *slave%(slave)d = NULL;\n" % type_infos Laurent@2111: str_completion["slaves_configuration"] += SLAVE_CONFIGURATION_TEMPLATE % type_infos Laurent@2111: Laurent@2111: # Initializing Laurent@2111: pdos_infos = { Laurent@2111: "pdos_entries_infos": [], Laurent@2111: "pdos_infos": [], Laurent@2111: "pdos_sync_infos": [], Laurent@2111: } Laurent@2111: pdos_infos.update(type_infos) Laurent@2111: Laurent@2111: sync_managers = [] Laurent@2111: for sync_manager_idx, sync_manager in enumerate(device.getSm()): Laurent@2111: sync_manager_infos = { Laurent@2111: "index": sync_manager_idx, Laurent@2111: "name": sync_manager.getcontent(), Laurent@2111: "slave": slave_idx, Laurent@2111: "pdos": [], Laurent@2111: "pdos_number": 0, Laurent@2111: } Laurent@2111: Laurent@2111: sync_manager_control_byte = ExtractHexDecValue(sync_manager.getControlByte()) Laurent@2111: sync_manager_direction = sync_manager_control_byte & 0x0c Laurent@2111: sync_manager_watchdog = sync_manager_control_byte & 0x40 Laurent@2111: if sync_manager_direction: Laurent@2111: sync_manager_infos["sync_manager_type"] = "EC_DIR_OUTPUT" Laurent@2111: else: Laurent@2111: sync_manager_infos["sync_manager_type"] = "EC_DIR_INPUT" Laurent@2111: if sync_manager_watchdog: Laurent@2111: sync_manager_infos["watchdog"] = "EC_WD_ENABLE" Laurent@2111: else: Laurent@2111: sync_manager_infos["watchdog"] = "EC_WD_DISABLE" Laurent@2111: Laurent@2111: sync_managers.append(sync_manager_infos) Laurent@2111: Laurent@2111: pdos_index = [] Laurent@2111: exclusive_pdos = {} Laurent@2111: selected_pdos = [] Laurent@2111: for pdo, pdo_type in ([(pdo, "Inputs") for pdo in device.getTxPdo()] + Laurent@2111: [(pdo, "Outputs") for pdo in device.getRxPdo()]): Laurent@2111: Laurent@2111: pdo_index = ExtractHexDecValue(pdo.getIndex().getcontent()) Laurent@2111: pdos_index.append(pdo_index) Laurent@2111: Laurent@2111: excluded_list = pdo.getExclude() Laurent@2111: if len(excluded_list) > 0: Laurent@2111: exclusion_list = [pdo_index] Laurent@2111: for excluded in excluded_list: Laurent@2111: exclusion_list.append(ExtractHexDecValue(excluded.getcontent())) Laurent@2111: exclusion_list.sort() Laurent@2111: Laurent@2111: exclusion_scope = exclusive_pdos.setdefault(tuple(exclusion_list), []) Laurent@2111: Laurent@2111: entries = pdo.getEntry() Laurent@2111: pdo_mapping_match = { Laurent@2111: "index": pdo_index, Laurent@2111: "matching": 0, Laurent@2111: "count": len(entries), Laurent@2111: "assigned": pdo.getSm() is not None Laurent@2111: } Laurent@2111: exclusion_scope.append(pdo_mapping_match) Laurent@2111: Laurent@2111: for entry in entries: Laurent@2111: index = ExtractHexDecValue(entry.getIndex().getcontent()) Laurent@2111: subindex = ExtractHexDecValue(entry.getSubIndex()) Laurent@2111: if slave_variables.get((index, subindex), None) is not None: Laurent@2111: pdo_mapping_match["matching"] += 1 Laurent@2111: Laurent@2111: elif pdo.getMandatory(): Laurent@2111: selected_pdos.append(pdo_index) Laurent@2111: Laurent@2111: excluded_pdos = [] Laurent@2111: for exclusion_scope in exclusive_pdos.itervalues(): Laurent@2111: exclusion_scope.sort(ExclusionSortFunction) Laurent@2111: start_excluding_index = 0 Laurent@2111: if exclusion_scope[0]["matching"] > 0: Laurent@2111: selected_pdos.append(exclusion_scope[0]["index"]) Laurent@2111: start_excluding_index = 1 Laurent@2111: excluded_pdos.extend([pdo["index"] for pdo in exclusion_scope[start_excluding_index:] if PdoAssign or not pdo["assigned"]]) Laurent@2111: Laurent@2111: for pdo, pdo_type in ([(pdo, "Inputs") for pdo in device.getTxPdo()] + Laurent@2111: [(pdo, "Outputs") for pdo in device.getRxPdo()]): Laurent@2111: entries = pdo.getEntry() Laurent@2111: Laurent@2111: pdo_index = ExtractHexDecValue(pdo.getIndex().getcontent()) Laurent@2111: if pdo_index in excluded_pdos: Laurent@2111: continue Laurent@2111: Laurent@2111: pdo_needed = pdo_index in selected_pdos Laurent@2111: Laurent@2111: entries_infos = [] Laurent@2111: Laurent@2111: for entry in entries: Laurent@2111: index = ExtractHexDecValue(entry.getIndex().getcontent()) Laurent@2111: subindex = ExtractHexDecValue(entry.getSubIndex()) Laurent@2111: entry_infos = { Laurent@2111: "index": index, Laurent@2111: "subindex": subindex, Laurent@2111: "name": ExtractName(entry.getName()), Laurent@2111: "bitlen": entry.getBitLen(), Laurent@2111: } Laurent@2111: entry_infos.update(type_infos) Laurent@2111: entries_infos.append(" {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos) Laurent@2111: Laurent@2111: entry_declaration = slave_variables.get((index, subindex), None) Laurent@2111: if entry_declaration is not None and not entry_declaration["mapped"]: Laurent@2111: pdo_needed = True Laurent@2111: Laurent@2111: entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], Laurent@2111: entry_declaration["infos"]))) Laurent@2111: entry_declaration["mapped"] = True Laurent@2111: Laurent@2111: entry_type = entry.getDataType().getcontent() Laurent@2111: if entry_infos["var_type"] != entry_type: Laurent@2111: message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"] Laurent@2111: if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != Laurent@2111: self.Controler.GetSizeOfType(entry_type)): Laurent@2111: raise ValueError, message Laurent@2111: else: Laurent@2111: self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n") Laurent@2111: Laurent@2111: if (entry_infos["dir"] == "I" and pdo_type != "Inputs" or Laurent@2111: entry_infos["dir"] == "Q" and pdo_type != "Outputs"): Laurent@2111: raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"] Laurent@2111: Laurent@2111: ConfigureVariable(entry_infos, str_completion) Laurent@2111: Laurent@2111: elif pdo_type == "Outputs" and entry.getDataType() is not None and device_coe is not None: Laurent@2111: data_type = entry.getDataType().getcontent() Laurent@2111: entry_infos["dir"] = "Q" Laurent@2111: entry_infos["data_size"] = max(1, entry_infos["bitlen"] / 8) Laurent@2111: entry_infos["data_type"] = DATATYPECONVERSION.get(data_type) Laurent@2111: entry_infos["var_type"] = data_type Laurent@2111: entry_infos["real_var"] = "slave%(slave)d_%(index).4x_%(subindex).2x_default" % entry_infos Laurent@2111: Laurent@2111: ConfigureVariable(entry_infos, str_completion) Laurent@2111: Laurent@2111: str_completion["slaves_output_pdos_default_values_extraction"] += \ Laurent@2111: SLAVE_OUTPUT_PDO_DEFAULT_VALUE % entry_infos Laurent@2111: Laurent@2111: if pdo_needed: Laurent@2111: for excluded in pdo.getExclude(): Laurent@2111: excluded_index = ExtractHexDecValue(excluded.getcontent()) Laurent@2111: if excluded_index not in excluded_pdos: Laurent@2111: excluded_pdos.append(excluded_index) Laurent@2111: Laurent@2111: sm = pdo.getSm() Laurent@2111: if sm is None: Laurent@2111: for sm_idx, sync_manager in enumerate(sync_managers): Laurent@2111: if sync_manager["name"] == pdo_type: Laurent@2111: sm = sm_idx Laurent@2111: if sm is None: Laurent@2111: raise ValueError, _("No sync manager available for %s pdo!") % pdo_type Laurent@2111: Laurent@2111: sync_managers[sm]["pdos_number"] += 1 Laurent@2111: sync_managers[sm]["pdos"].append( Laurent@2111: {"slave": slave_idx, Laurent@2111: "index": pdo_index, Laurent@2111: "name": ExtractName(pdo.getName()), Laurent@2111: "type": pdo_type, Laurent@2111: "entries": entries_infos, Laurent@2111: "entries_number": len(entries_infos), Laurent@2111: "fixed": pdo.getFixed() == True}) Laurent@2111: Laurent@2111: if PdoConfig and PdoAssign: Laurent@2111: dynamic_pdos = {} Laurent@2111: dynamic_pdos_number = 0 Laurent@2111: for category, min_index, max_index in [("Inputs", 0x1600, 0x1800), Laurent@2111: ("Outputs", 0x1a00, 0x1C00)]: Laurent@2111: for sync_manager in sync_managers: Laurent@2111: if sync_manager["name"] == category: Laurent@2111: category_infos = dynamic_pdos.setdefault(category, {}) Laurent@2111: category_infos["sync_manager"] = sync_manager Laurent@2111: category_infos["pdos"] = [pdo for pdo in category_infos["sync_manager"]["pdos"] Laurent@2111: if not pdo["fixed"] and pdo["type"] == category] Laurent@2111: category_infos["current_index"] = min_index Laurent@2111: category_infos["max_index"] = max_index Laurent@2111: break Laurent@2111: Laurent@2111: for (index, subindex), entry_declaration in slave_variables.iteritems(): Laurent@2111: Laurent@2111: if not entry_declaration["mapped"]: Laurent@2111: entry = device_entries.get((index, subindex), None) Laurent@2111: if entry is None: Laurent@2111: raise ValueError, _("Unknown entry index 0x%4.4x, subindex 0x%2.2x for device %s") % \ Laurent@2111: (index, subindex, type_infos["device_type"]) Laurent@2111: Laurent@2111: entry_infos = { Laurent@2111: "index": index, Laurent@2111: "subindex": subindex, Laurent@2111: "name": entry["Name"], Laurent@2111: "bitlen": entry["BitSize"], Laurent@2111: } Laurent@2111: entry_infos.update(type_infos) Laurent@2111: Laurent@2111: entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], Laurent@2111: entry_declaration["infos"]))) Laurent@2111: entry_declaration["mapped"] = True Laurent@2111: Laurent@2111: if entry_infos["var_type"] != entry["Type"]: Laurent@2111: message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"] Laurent@2111: if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != Laurent@2111: self.Controler.GetSizeOfType(entry["Type"])): Laurent@2111: raise ValueError, message Laurent@2111: else: Laurent@2111: self.Controler.GetCTRoot().logger.write_warning(message + "\n") Laurent@2111: Laurent@2111: if entry_infos["dir"] == "I" and entry["PDOMapping"] in ["T", "RT"]: Laurent@2111: pdo_type = "Inputs" Laurent@2111: elif entry_infos["dir"] == "Q" and entry["PDOMapping"] in ["R", "RT"]: Laurent@2111: pdo_type = "Outputs" Laurent@2111: else: Laurent@2111: raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"] Laurent@2111: Laurent@2111: if not dynamic_pdos.has_key(pdo_type): Laurent@2111: raise ValueError, _("No Sync manager defined for %s!") % pdo_type Laurent@2111: Laurent@2111: ConfigureVariable(entry_infos, str_completion) Laurent@2111: Laurent@2111: if len(dynamic_pdos[pdo_type]["pdos"]) > 0: Laurent@2111: pdo = dynamic_pdos[pdo_type]["pdos"][0] Laurent@2111: else: Laurent@2111: while dynamic_pdos[pdo_type]["current_index"] in pdos_index: Laurent@2111: dynamic_pdos[pdo_type]["current_index"] += 1 Laurent@2111: if dynamic_pdos[pdo_type]["current_index"] >= dynamic_pdos[pdo_type]["max_index"]: Laurent@2111: raise ValueError, _("No more free PDO index available for %s!") % pdo_type Laurent@2111: pdos_index.append(dynamic_pdos[pdo_type]["current_index"]) Laurent@2111: Laurent@2111: dynamic_pdos_number += 1 Laurent@2111: pdo = {"slave": slave_idx, Laurent@2111: "index": dynamic_pdos[pdo_type]["current_index"], Laurent@2111: "name": "Dynamic PDO %d" % dynamic_pdos_number, Laurent@2111: "type": pdo_type, Laurent@2111: "entries": [], Laurent@2111: "entries_number": 0, Laurent@2111: "fixed": False} Laurent@2111: dynamic_pdos[pdo_type]["sync_manager"]["pdos_number"] += 1 Laurent@2111: dynamic_pdos[pdo_type]["sync_manager"]["pdos"].append(pdo) Laurent@2111: dynamic_pdos[pdo_type]["pdos"].append(pdo) Laurent@2111: Laurent@2111: pdo["entries"].append(" {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos) Laurent@2111: if entry_infos["bitlen"] < alignment: Laurent@2111: print (alignment, entry_infos["bitlen"]) Laurent@2111: pdo["entries"].append(" {0x0000, 0x00, %d}, /* None */" % (alignment - entry_infos["bitlen"])) Laurent@2111: pdo["entries_number"] += 1 Laurent@2111: Laurent@2111: if pdo["entries_number"] == 255: Laurent@2111: dynamic_pdos[pdo_type]["pdos"].pop(0) Laurent@2111: Laurent@2111: pdo_offset = 0 Laurent@2111: entry_offset = 0 Laurent@2111: for sync_manager_infos in sync_managers: Laurent@2111: Laurent@2111: for pdo_infos in sync_manager_infos["pdos"]: Laurent@2111: pdo_infos["offset"] = entry_offset Laurent@2111: pdo_entries = pdo_infos["entries"] Laurent@2111: pdos_infos["pdos_infos"].append( Laurent@2111: (" {0x%(index).4x, %(entries_number)d, " + Laurent@2111: "slave_%(slave)d_pdo_entries + %(offset)d}, /* %(name)s */") % pdo_infos) Laurent@2111: entry_offset += len(pdo_entries) Laurent@2111: pdos_infos["pdos_entries_infos"].extend(pdo_entries) Laurent@2111: Laurent@2111: sync_manager_infos["offset"] = pdo_offset Laurent@2111: pdo_offset_shift = sync_manager_infos["pdos_number"] Laurent@2111: pdos_infos["pdos_sync_infos"].append( Laurent@2111: (" {%(index)d, %(sync_manager_type)s, %(pdos_number)d, " + Laurent@2111: ("slave_%(slave)d_pdos + %(offset)d" if pdo_offset_shift else "NULL") + Laurent@2111: ", %(watchdog)s},") % sync_manager_infos) Laurent@2111: pdo_offset += pdo_offset_shift Laurent@2111: Laurent@2111: for element in ["pdos_entries_infos", "pdos_infos", "pdos_sync_infos"]: Laurent@2111: pdos_infos[element] = "\n".join(pdos_infos[element]) Laurent@2111: Laurent@2111: str_completion["pdos_configuration_declaration"] += SLAVE_PDOS_CONFIGURATION_DECLARATION % pdos_infos Laurent@2111: Laurent@2111: for (index, subindex), entry_declaration in slave_variables.iteritems(): Laurent@2111: if not entry_declaration["mapped"]: Laurent@2111: message = _("Entry index 0x%4.4x, subindex 0x%2.2x not mapped for device %s") % \ Laurent@2111: (index, subindex, type_infos["device_type"]) Laurent@2111: self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n") Laurent@2111: Laurent@2111: for element in ["used_pdo_entry_offset_variables_declaration", Laurent@2111: "used_pdo_entry_configuration", Laurent@2111: "located_variables_declaration", Laurent@2111: "retrieve_variables", Laurent@2111: "publish_variables"]: Laurent@2111: str_completion[element] = "\n".join(str_completion[element]) Laurent@2111: Laurent@2111: etherlabfile = open(filepath, 'w') Laurent@2111: etherlabfile.write(plc_etherlab_code % str_completion) Laurent@2111: etherlabfile.close()