canfestival/canfestival.py
changeset 1734 750eeb7230a1
parent 1732 94ffe74e6895
child 1736 7e61baa047f0
equal deleted inserted replaced
1733:dea107dce0c4 1734:750eeb7230a1
    70                 "name": "0x%4.4x-0x%2.2x: %s" % (index, subindex, entry_name),
    70                 "name": "0x%4.4x-0x%2.2x: %s" % (index, subindex, entry_name),
    71                 "type": LOCATION_VAR_MEMORY,
    71                 "type": LOCATION_VAR_MEMORY,
    72                 "size": size,
    72                 "size": size,
    73                 "IEC_type": IECTypeConversion.get(typeinfos["name"]),
    73                 "IEC_type": IECTypeConversion.get(typeinfos["name"]),
    74                 "var_name": "%s_%4.4x_%2.2x" % ("_".join(name.split()), index, subindex),
    74                 "var_name": "%s_%4.4x_%2.2x" % ("_".join(name.split()), index, subindex),
    75                 "location": "%s%s"%(SizeConversion[size], ".".join(map(str, current_location +
    75                 "location": "%s%s" % (SizeConversion[size], ".".join(map(str, current_location +
    76                                                                                 (index, subindex)))),
    76                                                                                 (index, subindex)))),
    77                 "description": "",
    77                 "description": "",
    78                 "children": []})
    78                 "children": []})
    79     return  {"name": name,
    79     return  {"name": name,
    80              "type": LOCATION_CONFNODE,
    80              "type": LOCATION_CONFNODE,
   217         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
   217         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
   218         """
   218         """
   219         current_location = self.GetCurrentLocation()
   219         current_location = self.GetCurrentLocation()
   220         # define a unique name for the generated C file
   220         # define a unique name for the generated C file
   221         prefix = "_".join(map(str, current_location))
   221         prefix = "_".join(map(str, current_location))
   222         Gen_OD_path = os.path.join(buildpath, "OD_%s.c"%prefix )
   222         Gen_OD_path = os.path.join(buildpath, "OD_%s.c" % prefix )
   223         # Create a new copy of the model
   223         # Create a new copy of the model
   224         slave = self.GetCurrentNodeCopy()
   224         slave = self.GetCurrentNodeCopy()
   225         slave.SetNodeName("OD_%s"%prefix)
   225         slave.SetNodeName("OD_%s" % prefix)
   226         # allow access to local OD from Slave PLC
   226         # allow access to local OD from Slave PLC
   227         pointers = config_utils.LocalODPointers(locations, current_location, slave)
   227         pointers = config_utils.LocalODPointers(locations, current_location, slave)
   228         res = gen_cfile.GenerateFile(Gen_OD_path, slave, pointers)
   228         res = gen_cfile.GenerateFile(Gen_OD_path, slave, pointers)
   229         if res :
   229         if res :
   230             raise Exception, res
   230             raise Exception, res
   231         res = eds_utils.GenerateEDSFile(os.path.join(buildpath, "Slave_%s.eds"%prefix), slave)
   231         res = eds_utils.GenerateEDSFile(os.path.join(buildpath, "Slave_%s.eds" % prefix), slave)
   232         if res :
   232         if res :
   233             raise Exception, res
   233             raise Exception, res
   234         return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False
   234         return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False
   235 
   235 
   236     def LoadPrevious(self):
   236     def LoadPrevious(self):
   431         """
   431         """
   432         self._CloseView(self._GeneratedMasterView)
   432         self._CloseView(self._GeneratedMasterView)
   433         current_location = self.GetCurrentLocation()
   433         current_location = self.GetCurrentLocation()
   434         # define a unique name for the generated C file
   434         # define a unique name for the generated C file
   435         prefix = "_".join(map(str, current_location))
   435         prefix = "_".join(map(str, current_location))
   436         Gen_OD_path = os.path.join(buildpath, "OD_%s.c"%prefix )
   436         Gen_OD_path = os.path.join(buildpath, "OD_%s.c" % prefix )
   437         # Create a new copy of the model with DCF loaded with PDO mappings for desired location
   437         # Create a new copy of the model with DCF loaded with PDO mappings for desired location
   438         try:
   438         try:
   439             master, pointers = config_utils.GenerateConciseDCF(locations, current_location, self, self.CanFestivalNode.getSync_TPDOs(),"OD_%s"%prefix)
   439             master, pointers = config_utils.GenerateConciseDCF(locations, current_location, self, self.CanFestivalNode.getSync_TPDOs(),"OD_%s" % prefix)
   440         except config_utils.PDOmappingException, e:
   440         except config_utils.PDOmappingException, e:
   441             raise Exception, e.message
   441             raise Exception, e.message
   442         # Do generate C file.
   442         # Do generate C file.
   443         res = gen_cfile.GenerateFile(Gen_OD_path, master, pointers)
   443         res = gen_cfile.GenerateFile(Gen_OD_path, master, pointers)
   444         if res :
   444         if res :
   525             child_data = getattr(child, "CanFestivalSlaveNode", None)
   525             child_data = getattr(child, "CanFestivalSlaveNode", None)
   526             if child_data is None:
   526             if child_data is None:
   527                 # Not a slave -> master
   527                 # Not a slave -> master
   528                 child_data = getattr(child, "CanFestivalNode")
   528                 child_data = getattr(child, "CanFestivalNode")
   529                 # Apply sync setting
   529                 # Apply sync setting
   530                 format_dict["nodes_init"] += 'NODE_MASTER_INIT(%s, %s)\n    '%(
   530                 format_dict["nodes_init"] += 'NODE_MASTER_INIT(%s, %s)\n    ' % (
   531                        nodename,
   531                        nodename,
   532                        child_data.getNodeId())
   532                        child_data.getNodeId())
   533                 if child_data.getSync_TPDOs():
   533                 if child_data.getSync_TPDOs():
   534                     format_dict["nodes_send_sync"] += 'NODE_SEND_SYNC(%s)\n    '%(nodename)
   534                     format_dict["nodes_send_sync"] += 'NODE_SEND_SYNC(%s)\n    ' % (nodename)
   535                     format_dict["nodes_proceed_sync"] += 'NODE_PROCEED_SYNC(%s)\n    '%(nodename)
   535                     format_dict["nodes_proceed_sync"] += 'NODE_PROCEED_SYNC(%s)\n    ' % (nodename)
   536 
   536 
   537                 # initialize and declare node boot status variables for post_SlaveBootup lookup
   537                 # initialize and declare node boot status variables for post_SlaveBootup lookup
   538                 SlaveIDs = child.GetSlaveIDs()
   538                 SlaveIDs = child.GetSlaveIDs()
   539                 if len(SlaveIDs) == 0:
   539                 if len(SlaveIDs) == 0:
   540                     # define post_SlaveBootup lookup functions
   540                     # define post_SlaveBootup lookup functions
   541                     format_dict["slavebootups"] += (
   541                     format_dict["slavebootups"] += (
   542                         "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){}\n"%(nodename))
   542                         "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){}\n" % (nodename))
   543                 else:
   543                 else:
   544                     format_dict["slavebootups"] += (
   544                     format_dict["slavebootups"] += (
   545                         "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){\n"%(nodename)+
   545                         "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){\n" % (nodename)+
   546                         "    check_and_start_node(d, nodeId);\n"+
   546                         "    check_and_start_node(d, nodeId);\n"+
   547                         "}\n")
   547                         "}\n")
   548                 # register previously declared func as post_SlaveBootup callback for that node
   548                 # register previously declared func as post_SlaveBootup callback for that node
   549                 format_dict["slavebootup_register"] += (
   549                 format_dict["slavebootup_register"] += (
   550                     "%s_Data.post_SlaveBootup = %s_post_SlaveBootup;\n"%(nodename,nodename))
   550                     "%s_Data.post_SlaveBootup = %s_post_SlaveBootup;\n" % (nodename,nodename))
   551                 format_dict["pre_op"] += (
   551                 format_dict["pre_op"] += (
   552                     "static void %s_preOperational(CO_Data* d){\n    "%(nodename)+
   552                     "static void %s_preOperational(CO_Data* d){\n    " % (nodename)+
   553                     "".join(["    masterSendNMTstateChange(d, %d, NMT_Reset_Comunication);\n"%NdId for NdId in SlaveIDs])+
   553                     "".join(["    masterSendNMTstateChange(d, %d, NMT_Reset_Comunication);\n" % NdId for NdId in SlaveIDs])+
   554                     "}\n")
   554                     "}\n")
   555                 format_dict["pre_op_register"] += (
   555                 format_dict["pre_op_register"] += (
   556                     "%s_Data.preOperational = %s_preOperational;\n"%(nodename,nodename))
   556                     "%s_Data.preOperational = %s_preOperational;\n" % (nodename,nodename))
   557             else:
   557             else:
   558                 # Slave node
   558                 # Slave node
   559                 align = child_data.getSync_Align()
   559                 align = child_data.getSync_Align()
   560                 align_ratio=child_data.getSync_Align_Ratio()
   560                 align_ratio=child_data.getSync_Align_Ratio()
   561                 if align > 0:
   561                 if align > 0:
   562                     format_dict["post_sync"] += (
   562                     format_dict["post_sync"] += (
   563                         "static int %s_CalCount = 0;\n"%(nodename)+
   563                         "static int %s_CalCount = 0;\n" % (nodename)+
   564                         "static void %s_post_sync(CO_Data* d){\n"%(nodename)+
   564                         "static void %s_post_sync(CO_Data* d){\n" % (nodename)+
   565                         "    if(%s_CalCount < %d){\n"%(nodename, align)+
   565                         "    if(%s_CalCount < %d){\n" % (nodename, align)+
   566                         "        %s_CalCount++;\n"%(nodename)+
   566                         "        %s_CalCount++;\n" % (nodename)+
   567                         "        align_tick(-1);\n"+
   567                         "        align_tick(-1);\n"+
   568                         "    }else{\n"+
   568                         "    }else{\n"+
   569                         "        align_tick(%d);\n"%(align_ratio)+
   569                         "        align_tick(%d);\n" % (align_ratio)+
   570                         "    }\n"+
   570                         "    }\n"+
   571                         "}\n")
   571                         "}\n")
   572                     format_dict["post_sync_register"] += (
   572                     format_dict["post_sync_register"] += (
   573                         "%s_Data.post_sync = %s_post_sync;\n"%(nodename,nodename))
   573                         "%s_Data.post_sync = %s_post_sync;\n" % (nodename,nodename))
   574                 format_dict["nodes_init"] += 'NODE_SLAVE_INIT(%s, %s)\n    '%(
   574                 format_dict["nodes_init"] += 'NODE_SLAVE_INIT(%s, %s)\n    ' % (
   575                        nodename,
   575                        nodename,
   576                        child_data.getNodeId())
   576                        child_data.getNodeId())
   577 
   577 
   578             # Include generated OD headers
   578             # Include generated OD headers
   579             format_dict["nodes_includes"] += '#include "%s.h"\n'%(nodename)
   579             format_dict["nodes_includes"] += '#include "%s.h"\n' % (nodename)
   580             # Declare CAN channels according user filled config
   580             # Declare CAN channels according user filled config
   581             format_dict["board_decls"] += 'BOARD_DECL(%s, "%s", "%s")\n'%(
   581             format_dict["board_decls"] += 'BOARD_DECL(%s, "%s", "%s")\n' % (
   582                    nodename,
   582                    nodename,
   583                    child.GetCanDevice(),
   583                    child.GetCanDevice(),
   584                    child_data.getCAN_Baudrate())
   584                    child_data.getCAN_Baudrate())
   585             format_dict["nodes_open"] += 'NODE_OPEN(%s)\n    '%(nodename)
   585             format_dict["nodes_open"] += 'NODE_OPEN(%s)\n    ' % (nodename)
   586             format_dict["nodes_close"] += 'NODE_CLOSE(%s)\n    '%(nodename)
   586             format_dict["nodes_close"] += 'NODE_CLOSE(%s)\n    ' % (nodename)
   587             format_dict["nodes_stop"] += 'NODE_STOP(%s)\n    '%(nodename)
   587             format_dict["nodes_stop"] += 'NODE_STOP(%s)\n    ' % (nodename)
   588 
   588 
   589         filename = paths.AbsNeighbourFile(__file__,"cf_runtime.c")
   589         filename = paths.AbsNeighbourFile(__file__,"cf_runtime.c")
   590         cf_main = open(filename).read() % format_dict
   590         cf_main = open(filename).read() % format_dict
   591         cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c"%format_dict)
   591         cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c" % format_dict)
   592         f = open(cf_main_path,'w')
   592         f = open(cf_main_path,'w')
   593         f.write(cf_main)
   593         f.write(cf_main)
   594         f.close()
   594         f.close()
   595 
   595 
   596         res = [(cf_main_path, local_canfestival_config.getCFLAGS(CanFestivalPath))],local_canfestival_config.getLDFLAGS(CanFestivalPath), True
   596         res = [(cf_main_path, local_canfestival_config.getCFLAGS(CanFestivalPath))],local_canfestival_config.getLDFLAGS(CanFestivalPath), True