def _getInitValue(self, ws, def_name, value, dataType): """ returns a list or sequence """ code = C.sequence() if isinstance(value, autosar.constant.IntegerValue): if dataType.minVal >= 0: suffix = 'u' else: suffix = '' code.append( C.define(def_name, '((%s)%s%s)' % (dataType.name, value.value, suffix))) elif isinstance(value, autosar.constant.StringValue): code.append(C.define(def_name, '"%s"' % (value.value))) elif isinstance(value, autosar.constant.BooleanValue): if value.value: text = '((boolean) TRUE)' else: text = '((boolean) FALSE)' code.append(C.define(def_name, text)) elif isinstance(value, autosar.constant.RecordValue): for element in value.elements: prefix = '%s_%s' % (def_name, element.name) dataType = ws.find(element.typeRef) if dataType is not None: code.extend( self._getInitValue(ws, prefix, element, dataType)) elif isinstance(value, autosar.constant.ArrayValue): pass else: raise NotImplementedError(type(value)) return code
def _genComponentHeader(self, fp, component): ws = component.swc.rootWS() assert(ws is not None) hfile=C.hfile(None, guard='RTE_%s_H'%(component.swc.name.upper())) hfile.code.append(C.include('Rte.h')) hfile.code.append(C.include('Rte_Type.h')) lines = self._genInitValues(ws, component.swc.requirePorts+component.swc.providePorts) if len(lines)>0: hfile.code.extend([C.line(x) for x in _genCommentHeader('Init Values')]) hfile.code.extend(lines) #Write API hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('API Prototypes')]) for proto in component.clientAPI.get_all(): hfile.code.append(C.statement(proto.func)) if len(component.clientAPI.final['read'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Read_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['read']]) if len(component.clientAPI.final['write'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Write_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['write']]) if len(component.clientAPI.final['receive'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Receive_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['receive']]) if len(component.clientAPI.final['send'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Send_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['send']]) if len(component.clientAPI.final['mode'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']]) if len(component.clientAPI.final['mode'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<mode>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']]) if len(component.clientAPI.final['calprm'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Calprm_<name>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['calprm']]) if len(component.clientAPI.final['call'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Call_<p>_<o> ')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['call']]) if len(component.rte_runnables)>0: for name in sorted(component.rte_runnables): runnable = component.rte_runnables[name] tmp = self._writeRunnableProto(runnable) hfile.code.extend(tmp) fp.write('\n'.join(hfile.lines())) fp.write('\n')
def _define_event_masks(self): bit_mask = 1 num_timer_events = 0 for num,event_mask in enumerate(sorted(self.event_map.keys())): if num>32: raise RuntimeError('Task %s cannot support more than 32 events'%(os_task.name)) event_list = self.event_map[event_mask] for event in event_list: if isinstance(event, autosar.rte.TimerEvent): self.timer_events.append(event) runnables_string = ", ".join([event.runnable.symbol for event in event_list]) self.event_masks.append(C.define(event_mask, str('((uint32) 0x%08X)'%bit_mask)+' '+str(C.linecomment(runnables_string)), align=80)) bit_mask=bit_mask<<1
def generateEventCfg(self, destdir, destfile='os_event_cfg.h'): self.event_id = 1 self.num_timer_events = 0 file = C.hfile(os.path.join(destdir, destfile)) code = file.code with io.open(file.path, 'w', newline='\n') as fp: for os_task in self.cfg.tasks: for event_name in sorted(os_task.event_map.keys()): rte_event = os_task.event_map[event_name] if isinstance(rte_event, autosar.rte.TimerEvent): self.num_timer_events += 1 code.append( C.define( event_name, str(self.event_id) + ' ' + str(C.linecomment(rte_event.rte_runnable.symbol)), align=60)) self.event_id += 1 code.append(C.blank()) code.append( C.define('OS_NUM_TIMER_EVENTS', str(self.num_timer_events))) for line in file.lines(): fp.write(line + '\n')
def _generate_event_cfg_header(self, dest_dir, file_name='os_event_cfg.h'): header = C.hfile(os.path.join(dest_dir, file_name)) code = header.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include('PlatForm_Types.h')) code.append('') code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES')) for os_task in self.cfg.tasks: for event_mask in os_task.event_masks: code.append(event_mask) code.append(C.define('OS_NUM_ALARMS_%s'%os_task.name, str(len(os_task.timer_events)))) code.append(C.blank()) with io.open(header.path, 'w', newline='\n') as fp: for line in header.lines(): fp.write(line+'\n')
def _generate_event_cfg_header(self, dest_dir, file_name='os_event_cfg.h'): header = C.hfile(os.path.join(dest_dir, file_name)) code = header.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include('PlatForm_Types.h')) code.append('') code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES')) for os_task in self.cfg.tasks: for event_mask in os_task.event_masks: code.append(event_mask) code.append( C.define('OS_NUM_ALARMS_%s' % os_task.name, str(len(os_task.timer_events)))) code.append(C.blank()) with io.open(header.path, 'w', newline='\n') as fp: for line in header.lines(): fp.write(line + '\n')
def _define_event_masks(self): bit_mask = 1 num_timer_events = 0 for num, event_mask in enumerate(sorted(self.event_map.keys())): if num > 32: raise RuntimeError( 'Task %s cannot support more than 32 events' % (os_task.name)) event_list = self.event_map[event_mask] for event in event_list: if isinstance(event, autosar.rte.TimerEvent): self.timer_events.append(event) runnables_string = ", ".join( [event.runnable.symbol for event in event_list]) self.event_masks.append( C.define(event_mask, str('((uint32) 0x%08X)' % bit_mask) + ' ' + str(C.linecomment(runnables_string)), align=80)) bit_mask = bit_mask << 1
def _generate_task_cfg_header(self, dest_dir, file_name='os_task_cfg.h'): header = C.hfile(os.path.join(dest_dir, file_name)) code = header.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include('os_types.h')) code.append(C.include('os_task.h')) code.append('') code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES')) code.append(C.define('OS_NUM_TASKS', str(len(self.cfg.tasks)) + 'u')) code.append('') code.extend(_genCommentHeader('PUBLIC VARIABLES')) code.append(C.statement('extern os_cfg_t g_os_cfg')) code.append('') code.extend(_genCommentHeader('PUBLIC FUNCTION PROTOTYPES')) for task in self.cfg.tasks: code.append(C.statement('OS_TASK_HANDLER(%s, arg)' % task.name)) for function_name in self.cfg.mode_switch_calls: code.append(C.statement(C.function(function_name, 'void'))) with io.open(header.path, 'w', newline='\n') as fp: for line in header.lines(): fp.write(line + '\n') return file_name
def _generate_task_cfg_header(self, dest_dir, file_name = 'os_task_cfg.h'): header = C.hfile(os.path.join(dest_dir, file_name)) code = header.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include('os_types.h')) code.append(C.include('os_task.h')) code.append('') code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES')) code.append(C.define('OS_NUM_TASKS',str(len(self.cfg.tasks))+'u')) code.append('') code.extend(_genCommentHeader('PUBLIC VARIABLES')) code.append(C.statement('extern os_cfg_t g_os_cfg')) code.append('') code.extend(_genCommentHeader('PUBLIC FUNCTION PROTOTYPES')) for task in self.cfg.tasks: code.append(C.statement('OS_TASK_HANDLER(%s, arg)'%task.name)) for function_name in self.cfg.mode_switch_calls: code.append(C.statement(C.function(function_name, 'void'))) with io.open(header.path, 'w', newline='\n') as fp: for line in header.lines(): fp.write(line+'\n') return file_name
def generate(self, dest_dir='.', file_name='Rte_Type.h'): """ Generates Rte_Type.h Note: The last argument has been deprecated and is no longer in use """ if self.partition.isFinalized == False: self.partition.finalize() file_path = os.path.join(dest_dir, file_name) with io.open(file_path, 'w', newline='\n') as fp: hfile = C.hfile(file_name) hfile.code.extend( [C.line(x) for x in _genCommentHeader('Includes')]) hfile.code.append(C.include("Std_Types.h")) hfile.code.append(C.blank()) (basicTypes, complexTypes, modeTypes) = self.partition.types.getTypes() hfile.code.extend([ C.line(x) for x in _genCommentHeader('Data Type Definitions') ]) hfile.code.append(C.blank()) ws = self.partition.ws unusedDefaultTypes = self._findUnusedDefaultTypes(ws, basicTypes) first = True for ref in sorted(basicTypes) + sorted(complexTypes): dataType = ws.find(ref) if dataType is not None: typedef = None if first: first = False else: hfile.code.append(C.blank()) hfile.code.append('#define Rte_TypeDef_%s' % dataType.name) if isinstance(dataType, autosar.datatype.BooleanDataType): typedef = C.typedef('boolean', dataType.name) hfile.code.append(C.statement(typedef)) elif isinstance(dataType, autosar.datatype.IntegerDataType): valrange = dataType.maxVal - dataType.minVal bitcount = valrange.bit_length() typename = dataType.name basetype = self._typename(bitcount, dataType.minVal) typedef = C.typedef(basetype, typename) hfile.code.append(C.statement(typedef)) isUnsigned = True if basetype in ('uint8', 'uint16', 'uint32') else False if isUnsigned: minval = str(dataType.minVal) + 'u' maxval = str(dataType.maxVal) + 'u' else: minval = str(dataType.minVal) maxval = str(dataType.maxVal) hfile.code.append('#define %s_LowerLimit ((%s)%s)' % (typename, typename, minval)) hfile.code.append('#define %s_UpperLimit ((%s)%s)' % (typename, typename, maxval)) if dataType.compuMethodRef is not None: compuMethod = ws.find(dataType.compuMethodRef) if compuMethod is not None: lines1 = [] lines2 = [] if isinstance( compuMethod, autosar.datatype.CompuMethodConst): for elem in compuMethod.elements: if isUnsigned: value = str(elem.upperLimit) + 'u' else: value = str(elem.upperLimit) lines1.append( '#define RTE_CONST_%s (%s)' % (elem.textValue, value)) lines2.append( '#define %s ((%s)%s)' % (elem.textValue, typename, value)) if len(lines2) > 0: tmp = lines1 + [C.blank()] + lines2 else: tmp = lines1 for line in tmp: hfile.code.append(line) else: raise ValueError(dataType.compuMethodRef) elif isinstance(dataType, autosar.datatype.RecordDataType): body = C.block(innerIndent=innerIndentDefault) for elem in dataType.elements: childType = ws.find(elem.typeRef, role='DataType') body.append( C.statement( C.variable(elem.name, childType.name))) struct = C.struct(None, body, typedef=dataType.name) hfile.code.append(C.statement(struct)) elif isinstance(dataType, autosar.datatype.StringDataType): hfile.code.append('typedef uint8 %s[%d];' % (dataType.name, dataType.length + 1)) elif isinstance(dataType, autosar.datatype.ArrayDataType): childType = ws.find(dataType.typeRef, role='DataType') if childType is None: raise ValueError('invalid type reference: ' + dataType.typeRef) hfile.code.append( 'typedef %s %s[%d];' % (childType.name, dataType.name, dataType.length)) elif isinstance(dataType, autosar.datatype.RealDataType): if dataType.encoding == 'DOUBLE': platform_typename = 'float64' else: platform_typename = 'float32' hfile.code.append('typedef %s %s;' % (platform_typename, dataType.name)) else: raise NotImplementedError(type(dataType)) #sys.stderr.write('not implemented: %s\n'%str(type(dataType))) else: raise ValueError(ref) if len(modeTypes) > 0: lines = _genCommentHeader('Mode Types') tmp = [] hfile.code.extend(lines) first = True for ref in modeTypes: if first: first = False else: tmp.append(C.blank()) modeType = ws.find(ref) hfile.code.append( C.statement( C.typedef('uint8', 'Rte_ModeType_' + modeType.name))) for i, elem in enumerate(modeType.modeDeclarations): # define RTE_MODE_EcuM_Mode_POST_RUN ((Rte_ModeType_EcuM_Mode)0) tmp.append( C.define( 'RTE_MODE_%s_%s' % (modeType.name, elem.name), '((Rte_ModeType_EcuM_Mode)%d)' % i)) hfile.code.append(C.blank()) hfile.code.extend(tmp) if len(unusedDefaultTypes) > 0: hfile.code.append(C.blank(2)) hfile.code.append( C.line('#ifndef RTE_SUPPRESS_UNUSED_DATATYPES')) for name in sorted(unusedDefaultTypes): hfile.code.append(C.blank()) hfile.code.extend(self.defaultTypes[name]) hfile.code.append(C.blank()) hfile.code.append(C.line('#endif')) fp.write('\n'.join(hfile.lines())) fp.write('\n')
def _initDefaultType(self): self.defaultTypes['Boolean'] = C.sequence().extend( [C.statement(C.typedef('boolean', 'Boolean'))]) self.defaultTypes['UInt8'] = C.sequence().extend([ C.statement(C.typedef('uint8', 'UInt8')), C.define('UInt8_LowerLimit', '((UInt8)0u)'), C.define('UInt8_UpperLimit', '((UInt8)255u)') ]) self.defaultTypes['UInt16'] = C.sequence().extend([ C.statement(C.typedef('uint16', 'UInt16')), C.define('UInt16_LowerLimit', '((UInt16)0u)'), C.define('UInt16_UpperLimit', '((UInt16)65535u)') ]) self.defaultTypes['UInt32'] = C.sequence().extend([ C.statement(C.typedef('uint32', 'UInt32')), C.define('UInt32_LowerLimit', '((UInt32)0u)'), C.define('UInt32_UpperLimit', '((UInt32)4294967295u)') ]) self.defaultTypes['SInt8'] = C.sequence().extend([ C.statement(C.typedef('sint8', 'SInt8')), C.define('SInt8_LowerLimit', '((SInt8)-128)'), C.define('SInt8_UpperLimit', '((SInt8)127)') ]) self.defaultTypes['SInt16'] = C.sequence().extend([ C.statement(C.typedef('sint16', 'SInt16')), C.define('SInt16_LowerLimit', '((SInt16)-32768)'), C.define('SInt16_UpperLimit', '((SInt16)32767)') ]) self.defaultTypes['SInt32'] = C.sequence().extend([ C.statement(C.typedef('sint32', 'SInt32')), C.define('SInt32_LowerLimit', '((SInt32)-2147483648)'), C.define('SInt32_UpperLimit', '((SInt32)2147483647)') ])