Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
authoretisserant
Mon, 24 Sep 2007 17:07:42 +0200
changeset 52 eaffcd0a2f03
parent 51 c31c55601556
child 53 805abb954de2
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
plugins/canfestival/canfestival.py
plugins/canfestival/cf_runtime.c
--- a/plugins/canfestival/canfestival.py	Mon Sep 24 17:06:36 2007 +0200
+++ b/plugins/canfestival/canfestival.py	Mon Sep 24 17:07:42 2007 +0200
@@ -22,6 +22,8 @@
       <xsd:element name="CanFestivalNode">
         <xsd:complexType>
           <xsd:attribute name="CAN_Device" type="xsd:string" use="required" />
+          <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="required" />
+          <xsd:attribute name="NodeId" type="xsd:string" use="required" />
           <xsd:attribute name="Sync_TPDOs" type="xsd:boolean" use="required" default="true"/>
         </xsd:complexType>
       </xsd:element>
@@ -81,11 +83,12 @@
         Gen_OD_path = os.path.join(buildpath, "OD_%s.c"%prefix )
         # Create a new copy of the model with DCF loaded with PDO mappings for desired location
         master = config_utils.GenerateConciseDCF(locations, current_location, self, self.CanFestivalNode.getSync_TPDOs())
+        master.SetNodeName("OD_%s"%prefix)
         res = gen_cfile.GenerateFile(Gen_OD_path, master)
         if res :
             raise Exception, res
         
-        return [(Gen_OD_path,canfestival_config.getCFLAGS(CanFestivalPath))],""
+        return [(Gen_OD_path,canfestival_config.getCFLAGS(CanFestivalPath))],"",False
     
 class RootClass:
     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
@@ -101,6 +104,35 @@
     PlugChildsTypes = [("CanOpenNode",_NodeListPlug)]
     
     def PlugGenerate_C(self, buildpath, locations, logger):
-        return [],canfestival_config.getLDFLAGS(CanFestivalPath)
+        
+        format_dict = {"locstr" : "_".join(map(str,self.GetCurrentLocation())),
+                       "candriver" : self.CanFestivalInstance.getCAN_Driver(),
+                       "nodes_includes" : "",
+                       "board_decls" : "",
+                       "nodes_init" : "",
+                       "nodes_open" : "",
+                       "nodes_close" : ""}
+        for child in self.IECSortedChilds():
+            childlocstr = "_".join(map(str,child.GetCurrentLocation()))
+            nodename = "OD_%s" % childlocstr
+
+            format_dict["nodes_includes"] += '#include "%s.h"\n'%(nodename)
+            format_dict["board_decls"] += 'BOARD_DECL(%s, "%s", "%s")\n'%(
+                   nodename,
+                   child.CanFestivalNode.getCAN_Device(),
+                   child.CanFestivalNode.getCAN_Baudrate())
+            format_dict["nodes_init"] += 'NODE_INIT(%s, %s)\n'%(
+                   nodename,
+                   child.CanFestivalNode.getNodeId())
+            format_dict["nodes_open"] += 'NODE_OPEN(%s)\n'%(nodename)
+            format_dict["nodes_close"] += 'NODE_CLOSE(%s)\n'%(nodename)
+        filename = os.path.join(os.path.split(__file__)[0],"cf_runtime.c")
+        cf_main = open(filename).read() % format_dict
+        cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c"%format_dict)
+        f = open(cf_main_path,'w')
+        f.write(cf_main)
+        f.close()
+        
+        return [(cf_main_path, canfestival_config.getCFLAGS(CanFestivalPath))],canfestival_config.getLDFLAGS(CanFestivalPath), True
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/canfestival/cf_runtime.c	Mon Sep 24 17:07:42 2007 +0200
@@ -0,0 +1,75 @@
+
+#include "canfestival.h"
+
+%(nodes_includes)s
+
+#define BOARD_DECL(nodename, busname, baudrate)\
+    s_BOARD nodename##Board = {busname, baudrate};
+
+%(board_decls)s
+
+static int init_level=0;
+
+#define NODE_INIT(nodename, nodeid) \
+    /* Defining the node Id */\
+    setNodeId(&nodename##_Data, nodeid);\
+    /* init */\
+    setState(&nodename##_Data, Initialisation);
+
+void InitNodes(CO_Data* d, UNS32 id)
+{
+    %(nodes_init)s
+}
+
+#define NODE_CLOSE(nodename) \
+    if(init_level--)\
+    {\
+        EnterMutex();\
+        setState(&nodename##_Data, Stopped);\
+        LeaveMutex();\
+        canClose(&nodename##_Data);\
+    }
+
+void __cleanup_%(locstr)s()
+{
+    %(nodes_close)s
+    
+    // Stop timer thread
+    StopTimerLoop();
+
+}
+
+#define NODE_OPEN(nodename)\
+    if(!canOpen(&nodename##Board,&nodename##_Data)){\
+        printf("Cannot open " #nodename " Board (%%s,%%s)\n",nodename##Board.busname, nodename##Board.baudrate);\
+        __cleanup_%(locstr)s();\
+        return -1;\
+    }\
+    init_level++;
+
+/***************************  INIT  *****************************************/
+int __init_%(locstr)s(int argc,char **argv)
+{
+
+    %(nodes_open)s
+
+#ifndef NOT_USE_DYNAMIC_LOADING
+    LoadCanDriver("libcanfestival_can_%(candriver)s.so");
+#endif      
+    // Start timer thread
+    StartTimerLoop(&InitNodes);
+    return 0;
+}
+
+void __retrive_%(locstr)s()
+{
+    /*TODO: Send Sync */
+    EnterMutex();
+}
+
+void __publish_%(locstr)s()
+{
+    /*TODO: Call SendPDOEvent */
+    LeaveMutex();
+}
+