72 _PySafeSetPLCGlob_%(name)s.restype = None |
72 _PySafeSetPLCGlob_%(name)s.restype = None |
73 _PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)] |
73 _PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)] |
74 """ % { "name": variable.getname(), |
74 """ % { "name": variable.getname(), |
75 "configname": configname.upper(), |
75 "configname": configname.upper(), |
76 "uppername": variable.getname().upper(), |
76 "uppername": variable.getname().upper(), |
77 "IECtype": variable.gettype(), |
77 "IECtype": variable.gettype()} |
78 "initial" : str(variable.getinitial())} |
|
79 for variable in self.CodeFile.variables.variable]) |
78 for variable in self.CodeFile.variables.variable]) |
80 |
79 |
81 # Runtime calls (start, stop, init, and cleanup) |
80 # Runtime calls (start, stop, init, and cleanup) |
82 rtcalls = "" |
81 rtcalls = "" |
83 for section in self.SECTIONS_NAMES: |
82 for section in self.SECTIONS_NAMES: |
84 if section != "globals": |
83 if section != "globals": |
85 rtcalls += "def _runtime_%s_%s():\n" % (location_str, section) |
84 rtcalls += "def _runtime_%s_%s():\n" % (location_str, section) |
86 sectiontext = self.GetSection(section).strip() |
85 sectiontext = self.GetSection(section).strip() |
87 if sectiontext: |
86 if sectiontext: |
88 rtcalls += ' ' + \ |
87 rtcalls += ' ' + \ |
89 sectiontext.strip().replace('\n', '\n ')+"\n" |
88 sectiontext.replace('\n', '\n ')+"\n\n" |
90 else: |
89 else: |
91 rtcalls += " pass\n\n" |
90 rtcalls += " pass\n\n" |
92 |
91 |
93 globalsection = self.GetSection("globals") |
92 globalsection = self.GetSection("globals") |
94 |
93 |
119 runtimefile.close() |
118 runtimefile.close() |
120 |
119 |
121 # C code for safe global variables access |
120 # C code for safe global variables access |
122 |
121 |
123 vardecfmt = """\ |
122 vardecfmt = """\ |
124 extern __%(IECtype)s_t %(configname)s__%(uppername)s; |
123 extern __IEC_%(IECtype)s_t %(configname)s__%(uppername)s; |
125 %(IECtype)s __%(name)s_rbuffer = %(initial)s; |
124 IEC_%(IECtype)s __%(name)s_rbuffer = __INIT_%(IECtype)s; |
126 %(IECtype)s __%(name)s_wbuffer; |
125 IEC_%(IECtype)s __%(name)s_wbuffer; |
127 long __%(name)s_rlock = 0; |
126 long __%(name)s_rlock = 0; |
128 long __%(name)s_wlock = 0; |
127 long __%(name)s_wlock = 0; |
129 int __%(name)s_wbuffer_written = 0; |
128 int __%(name)s_wbuffer_written = 0; |
130 void __SafeGetPLCGlob_%(name)s(%(IECtype)s *pvalue){ |
129 void __SafeGetPLCGlob_%(name)s(IEC_%(IECtype)s *pvalue){ |
131 %(IECtype)s res; |
130 IEC_%(IECtype)s res; |
132 while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1)); |
131 while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1)); |
133 *pvalue = __%(name)s_rbuffer; |
132 *pvalue = __%(name)s_rbuffer; |
134 AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0); |
133 AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0); |
135 } |
134 } |
136 __SafeSetPLCGlob_%(name)s(%(IECtype)s *value){ |
135 __SafeSetPLCGlob_%(name)s(IEC_%(IECtype)s *value){ |
137 while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1)); |
136 while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1)); |
138 __%(name)s_wbuffer = *value; |
137 __%(name)s_wbuffer = *value; |
139 __%(name)s_wbuffer_written = 1; |
138 __%(name)s_wbuffer_written = 1; |
140 AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0); |
139 AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0); |
141 } |
140 } |
162 (vardecfmt, varretfmt, varpubfmt)) |
161 (vardecfmt, varretfmt, varpubfmt)) |
163 for varinfo in map(lambda variable : { |
162 for varinfo in map(lambda variable : { |
164 "name": variable.getname(), |
163 "name": variable.getname(), |
165 "configname": configname.upper(), |
164 "configname": configname.upper(), |
166 "uppername": variable.getname().upper(), |
165 "uppername": variable.getname().upper(), |
167 "IECtype": "IEC_%s"%variable.gettype(), |
166 "IECtype": variable.gettype()}, |
168 "initial" : str(variable.getinitial())}, |
|
169 self.CodeFile.variables.variable)])) |
167 self.CodeFile.variables.variable)])) |
170 if len(var_str) > 0: |
168 if len(var_str) > 0: |
171 vardec, varret, varpub = var_str |
169 vardec, varret, varpub = var_str |
172 else: |
170 else: |
173 vardec = varret = varpub = "" |
171 vardec = varret = varpub = "" |