Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 """
marcozecchini 0:9fca2b23d0ba 2 mbed SDK
marcozecchini 0:9fca2b23d0ba 3 Copyright (c) 2011-2013 ARM Limited
marcozecchini 0:9fca2b23d0ba 4
marcozecchini 0:9fca2b23d0ba 5 Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 6 you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 7 You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 8
marcozecchini 0:9fca2b23d0ba 9 http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 10
marcozecchini 0:9fca2b23d0ba 11 Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 12 distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 14 See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 15 limitations under the License.
marcozecchini 0:9fca2b23d0ba 16 """
marcozecchini 0:9fca2b23d0ba 17 import re
marcozecchini 0:9fca2b23d0ba 18 from os import remove
marcozecchini 0:9fca2b23d0ba 19 from os.path import join, splitext, exists
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
marcozecchini 0:9fca2b23d0ba 22 from tools.hooks import hook_tool
marcozecchini 0:9fca2b23d0ba 23
marcozecchini 0:9fca2b23d0ba 24 class IAR(mbedToolchain):
marcozecchini 0:9fca2b23d0ba 25 LIBRARY_EXT = '.a'
marcozecchini 0:9fca2b23d0ba 26 LINKER_EXT = '.icf'
marcozecchini 0:9fca2b23d0ba 27 STD_LIB_NAME = "%s.a"
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error|Fatal error)(?P<message>.+)')
marcozecchini 0:9fca2b23d0ba 30 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
marcozecchini 0:9fca2b23d0ba 31
marcozecchini 0:9fca2b23d0ba 32 @staticmethod
marcozecchini 0:9fca2b23d0ba 33 def check_executable():
marcozecchini 0:9fca2b23d0ba 34 """Returns True if the executable (arm-none-eabi-gcc) location
marcozecchini 0:9fca2b23d0ba 35 specified by the user exists OR the executable can be found on the PATH.
marcozecchini 0:9fca2b23d0ba 36 Returns False otherwise."""
marcozecchini 0:9fca2b23d0ba 37 return mbedToolchain.generic_check_executable("IAR", 'iccarm', 2, "bin")
marcozecchini 0:9fca2b23d0ba 38
marcozecchini 0:9fca2b23d0ba 39 def __init__(self, target, notify=None, macros=None,
marcozecchini 0:9fca2b23d0ba 40 silent=False, extra_verbose=False, build_profile=None,
marcozecchini 0:9fca2b23d0ba 41 build_dir=None):
marcozecchini 0:9fca2b23d0ba 42 mbedToolchain.__init__(self, target, notify, macros, silent,
marcozecchini 0:9fca2b23d0ba 43 build_dir=build_dir,
marcozecchini 0:9fca2b23d0ba 44 extra_verbose=extra_verbose,
marcozecchini 0:9fca2b23d0ba 45 build_profile=build_profile)
marcozecchini 0:9fca2b23d0ba 46 if target.core == "Cortex-M7F" or target.core == "Cortex-M7FD":
marcozecchini 0:9fca2b23d0ba 47 cpuchoice = "Cortex-M7"
marcozecchini 0:9fca2b23d0ba 48 elif target.core.startswith("Cortex-M23"):
marcozecchini 0:9fca2b23d0ba 49 cpuchoice = "8-M.baseline"
marcozecchini 0:9fca2b23d0ba 50 elif target.core.startswith("Cortex-M33"):
marcozecchini 0:9fca2b23d0ba 51 cpuchoice = "8-M.mainline"
marcozecchini 0:9fca2b23d0ba 52 else:
marcozecchini 0:9fca2b23d0ba 53 cpuchoice = target.core
marcozecchini 0:9fca2b23d0ba 54
marcozecchini 0:9fca2b23d0ba 55 # flags_cmd are used only by our scripts, the project files have them already defined,
marcozecchini 0:9fca2b23d0ba 56 # using this flags results in the errors (duplication)
marcozecchini 0:9fca2b23d0ba 57 # asm accepts --cpu Core or --fpu FPU, not like c/c++ --cpu=Core
marcozecchini 0:9fca2b23d0ba 58 asm_flags_cmd = ["--cpu", cpuchoice]
marcozecchini 0:9fca2b23d0ba 59 # custom c flags
marcozecchini 0:9fca2b23d0ba 60 c_flags_cmd = ["--cpu", cpuchoice]
marcozecchini 0:9fca2b23d0ba 61
marcozecchini 0:9fca2b23d0ba 62 c_flags_cmd.extend([
marcozecchini 0:9fca2b23d0ba 63 "--thumb", "--dlib_config", "DLib_Config_Full.h"
marcozecchini 0:9fca2b23d0ba 64 ])
marcozecchini 0:9fca2b23d0ba 65 # custom c++ cmd flags
marcozecchini 0:9fca2b23d0ba 66 cxx_flags_cmd = [
marcozecchini 0:9fca2b23d0ba 67 "--c++", "--no_rtti", "--no_exceptions"
marcozecchini 0:9fca2b23d0ba 68 ]
marcozecchini 0:9fca2b23d0ba 69 if target.core == "Cortex-M7FD":
marcozecchini 0:9fca2b23d0ba 70 asm_flags_cmd += ["--fpu", "VFPv5"]
marcozecchini 0:9fca2b23d0ba 71 c_flags_cmd.append("--fpu=VFPv5")
marcozecchini 0:9fca2b23d0ba 72 elif target.core == "Cortex-M7F":
marcozecchini 0:9fca2b23d0ba 73 asm_flags_cmd += ["--fpu", "VFPv5_sp"]
marcozecchini 0:9fca2b23d0ba 74 c_flags_cmd.append("--fpu=VFPv5_sp")
marcozecchini 0:9fca2b23d0ba 75 elif target.core == "Cortex-M23" or target.core == "Cortex-M33":
marcozecchini 0:9fca2b23d0ba 76 self.flags["asm"] += ["--cmse"]
marcozecchini 0:9fca2b23d0ba 77
marcozecchini 0:9fca2b23d0ba 78 IAR_BIN = join(TOOLCHAIN_PATHS['IAR'], "bin")
marcozecchini 0:9fca2b23d0ba 79 main_cc = join(IAR_BIN, "iccarm")
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 self.asm = [join(IAR_BIN, "iasmarm")] + asm_flags_cmd + self.flags["asm"]
marcozecchini 0:9fca2b23d0ba 82 self.cc = [main_cc]
marcozecchini 0:9fca2b23d0ba 83 self.cppc = [main_cc]
marcozecchini 0:9fca2b23d0ba 84 self.cc += self.flags["common"] + c_flags_cmd + self.flags["c"]
marcozecchini 0:9fca2b23d0ba 85 self.cppc += self.flags["common"] + c_flags_cmd + cxx_flags_cmd + self.flags["cxx"]
marcozecchini 0:9fca2b23d0ba 86
marcozecchini 0:9fca2b23d0ba 87 self.ld = [join(IAR_BIN, "ilinkarm")] + self.flags['ld']
marcozecchini 0:9fca2b23d0ba 88 self.ar = join(IAR_BIN, "iarchive")
marcozecchini 0:9fca2b23d0ba 89 self.elf2bin = join(IAR_BIN, "ielftool")
marcozecchini 0:9fca2b23d0ba 90
marcozecchini 0:9fca2b23d0ba 91 def parse_dependencies(self, dep_path):
marcozecchini 0:9fca2b23d0ba 92 return [(self.CHROOT if self.CHROOT else '')+path.strip() for path in open(dep_path).readlines()
marcozecchini 0:9fca2b23d0ba 93 if (path and not path.isspace())]
marcozecchini 0:9fca2b23d0ba 94
marcozecchini 0:9fca2b23d0ba 95 def parse_output(self, output):
marcozecchini 0:9fca2b23d0ba 96 msg = None
marcozecchini 0:9fca2b23d0ba 97 for line in output.splitlines():
marcozecchini 0:9fca2b23d0ba 98 match = IAR.DIAGNOSTIC_PATTERN.match(line)
marcozecchini 0:9fca2b23d0ba 99 if match is not None:
marcozecchini 0:9fca2b23d0ba 100 if msg is not None:
marcozecchini 0:9fca2b23d0ba 101 self.cc_info(msg)
marcozecchini 0:9fca2b23d0ba 102 msg = None
marcozecchini 0:9fca2b23d0ba 103 msg = {
marcozecchini 0:9fca2b23d0ba 104 'severity': match.group('severity').lower(),
marcozecchini 0:9fca2b23d0ba 105 'file': match.group('file'),
marcozecchini 0:9fca2b23d0ba 106 'line': match.group('line'),
marcozecchini 0:9fca2b23d0ba 107 'col': 0,
marcozecchini 0:9fca2b23d0ba 108 'message': match.group('message'),
marcozecchini 0:9fca2b23d0ba 109 'text': '',
marcozecchini 0:9fca2b23d0ba 110 'target_name': self.target.name,
marcozecchini 0:9fca2b23d0ba 111 'toolchain_name': self.name
marcozecchini 0:9fca2b23d0ba 112 }
marcozecchini 0:9fca2b23d0ba 113 elif msg is not None:
marcozecchini 0:9fca2b23d0ba 114 # Determine the warning/error column by calculating the ^ position
marcozecchini 0:9fca2b23d0ba 115 match = IAR.INDEX_PATTERN.match(line)
marcozecchini 0:9fca2b23d0ba 116 if match is not None:
marcozecchini 0:9fca2b23d0ba 117 msg['col'] = len(match.group('col'))
marcozecchini 0:9fca2b23d0ba 118 self.cc_info(msg)
marcozecchini 0:9fca2b23d0ba 119 msg = None
marcozecchini 0:9fca2b23d0ba 120 else:
marcozecchini 0:9fca2b23d0ba 121 msg['text'] += line+"\n"
marcozecchini 0:9fca2b23d0ba 122
marcozecchini 0:9fca2b23d0ba 123 if msg is not None:
marcozecchini 0:9fca2b23d0ba 124 self.cc_info(msg)
marcozecchini 0:9fca2b23d0ba 125
marcozecchini 0:9fca2b23d0ba 126 def get_dep_option(self, object):
marcozecchini 0:9fca2b23d0ba 127 base, _ = splitext(object)
marcozecchini 0:9fca2b23d0ba 128 dep_path = base + '.d'
marcozecchini 0:9fca2b23d0ba 129 return ["--dependencies", dep_path]
marcozecchini 0:9fca2b23d0ba 130
marcozecchini 0:9fca2b23d0ba 131 def cc_extra(self, object):
marcozecchini 0:9fca2b23d0ba 132 base, _ = splitext(object)
marcozecchini 0:9fca2b23d0ba 133 return ["-l", base + '.s.txt']
marcozecchini 0:9fca2b23d0ba 134
marcozecchini 0:9fca2b23d0ba 135 def get_config_option(self, config_header):
marcozecchini 0:9fca2b23d0ba 136 return ['--preinclude=' + config_header]
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 def get_compile_options(self, defines, includes, for_asm=False):
marcozecchini 0:9fca2b23d0ba 139 opts = ['-D%s' % d for d in defines]
marcozecchini 0:9fca2b23d0ba 140 if for_asm :
marcozecchini 0:9fca2b23d0ba 141 return opts
marcozecchini 0:9fca2b23d0ba 142 if self.RESPONSE_FILES:
marcozecchini 0:9fca2b23d0ba 143 opts += ['-f', self.get_inc_file(includes)]
marcozecchini 0:9fca2b23d0ba 144 else:
marcozecchini 0:9fca2b23d0ba 145 opts += ["-I%s" % i for i in includes]
marcozecchini 0:9fca2b23d0ba 146
marcozecchini 0:9fca2b23d0ba 147 config_header = self.get_config_header()
marcozecchini 0:9fca2b23d0ba 148 if config_header is not None:
marcozecchini 0:9fca2b23d0ba 149 opts = opts + self.get_config_option(config_header)
marcozecchini 0:9fca2b23d0ba 150 return opts
marcozecchini 0:9fca2b23d0ba 151
marcozecchini 0:9fca2b23d0ba 152 @hook_tool
marcozecchini 0:9fca2b23d0ba 153 def assemble(self, source, object, includes):
marcozecchini 0:9fca2b23d0ba 154 # Build assemble command
marcozecchini 0:9fca2b23d0ba 155 cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]
marcozecchini 0:9fca2b23d0ba 156
marcozecchini 0:9fca2b23d0ba 157 # Call cmdline hook
marcozecchini 0:9fca2b23d0ba 158 cmd = self.hook.get_cmdline_assembler(cmd)
marcozecchini 0:9fca2b23d0ba 159
marcozecchini 0:9fca2b23d0ba 160 # Return command array, don't execute
marcozecchini 0:9fca2b23d0ba 161 return [cmd]
marcozecchini 0:9fca2b23d0ba 162
marcozecchini 0:9fca2b23d0ba 163 @hook_tool
marcozecchini 0:9fca2b23d0ba 164 def compile(self, cc, source, object, includes):
marcozecchini 0:9fca2b23d0ba 165 # Build compile command
marcozecchini 0:9fca2b23d0ba 166 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
marcozecchini 0:9fca2b23d0ba 167
marcozecchini 0:9fca2b23d0ba 168 cmd.extend(self.get_dep_option(object))
marcozecchini 0:9fca2b23d0ba 169
marcozecchini 0:9fca2b23d0ba 170 cmd.extend(self.cc_extra(object))
marcozecchini 0:9fca2b23d0ba 171
marcozecchini 0:9fca2b23d0ba 172 cmd.extend(["-o", object, source])
marcozecchini 0:9fca2b23d0ba 173
marcozecchini 0:9fca2b23d0ba 174 # Call cmdline hook
marcozecchini 0:9fca2b23d0ba 175 cmd = self.hook.get_cmdline_compiler(cmd)
marcozecchini 0:9fca2b23d0ba 176
marcozecchini 0:9fca2b23d0ba 177 return [cmd]
marcozecchini 0:9fca2b23d0ba 178
marcozecchini 0:9fca2b23d0ba 179 def compile_c(self, source, object, includes):
marcozecchini 0:9fca2b23d0ba 180 return self.compile(self.cc, source, object, includes)
marcozecchini 0:9fca2b23d0ba 181
marcozecchini 0:9fca2b23d0ba 182 def compile_cpp(self, source, object, includes):
marcozecchini 0:9fca2b23d0ba 183 return self.compile(self.cppc, source, object, includes)
marcozecchini 0:9fca2b23d0ba 184
marcozecchini 0:9fca2b23d0ba 185 @hook_tool
marcozecchini 0:9fca2b23d0ba 186 def link(self, output, objects, libraries, lib_dirs, mem_map):
marcozecchini 0:9fca2b23d0ba 187 # Build linker command
marcozecchini 0:9fca2b23d0ba 188 map_file = splitext(output)[0] + ".map"
marcozecchini 0:9fca2b23d0ba 189 cmd = self.ld + [ "-o", output, "--map=%s" % map_file] + objects + libraries
marcozecchini 0:9fca2b23d0ba 190
marcozecchini 0:9fca2b23d0ba 191 if mem_map:
marcozecchini 0:9fca2b23d0ba 192 cmd.extend(["--config", mem_map])
marcozecchini 0:9fca2b23d0ba 193
marcozecchini 0:9fca2b23d0ba 194 # Call cmdline hook
marcozecchini 0:9fca2b23d0ba 195 cmd = self.hook.get_cmdline_linker(cmd)
marcozecchini 0:9fca2b23d0ba 196
marcozecchini 0:9fca2b23d0ba 197 if self.RESPONSE_FILES:
marcozecchini 0:9fca2b23d0ba 198 # Split link command to linker executable + response file
marcozecchini 0:9fca2b23d0ba 199 cmd_linker = cmd[0]
marcozecchini 0:9fca2b23d0ba 200 link_files = self.get_link_file(cmd[1:])
marcozecchini 0:9fca2b23d0ba 201 cmd = [cmd_linker, '-f', link_files]
marcozecchini 0:9fca2b23d0ba 202
marcozecchini 0:9fca2b23d0ba 203 # Exec command
marcozecchini 0:9fca2b23d0ba 204 self.cc_verbose("Link: %s" % ' '.join(cmd))
marcozecchini 0:9fca2b23d0ba 205 self.default_cmd(cmd)
marcozecchini 0:9fca2b23d0ba 206
marcozecchini 0:9fca2b23d0ba 207 @hook_tool
marcozecchini 0:9fca2b23d0ba 208 def archive(self, objects, lib_path):
marcozecchini 0:9fca2b23d0ba 209 if self.RESPONSE_FILES:
marcozecchini 0:9fca2b23d0ba 210 param = ['-f', self.get_arch_file(objects)]
marcozecchini 0:9fca2b23d0ba 211 else:
marcozecchini 0:9fca2b23d0ba 212 param = objects
marcozecchini 0:9fca2b23d0ba 213
marcozecchini 0:9fca2b23d0ba 214 if exists(lib_path):
marcozecchini 0:9fca2b23d0ba 215 remove(lib_path)
marcozecchini 0:9fca2b23d0ba 216
marcozecchini 0:9fca2b23d0ba 217 self.default_cmd([self.ar, lib_path] + param)
marcozecchini 0:9fca2b23d0ba 218
marcozecchini 0:9fca2b23d0ba 219 @hook_tool
marcozecchini 0:9fca2b23d0ba 220 def binary(self, resources, elf, bin):
marcozecchini 0:9fca2b23d0ba 221 _, fmt = splitext(bin)
marcozecchini 0:9fca2b23d0ba 222 bin_arg = {".bin": "--bin", ".hex": "--ihex"}[fmt]
marcozecchini 0:9fca2b23d0ba 223 # Build binary command
marcozecchini 0:9fca2b23d0ba 224 cmd = [self.elf2bin, bin_arg, elf, bin]
marcozecchini 0:9fca2b23d0ba 225
marcozecchini 0:9fca2b23d0ba 226 # Call cmdline hook
marcozecchini 0:9fca2b23d0ba 227 cmd = self.hook.get_cmdline_binary(cmd)
marcozecchini 0:9fca2b23d0ba 228
marcozecchini 0:9fca2b23d0ba 229 # Exec command
marcozecchini 0:9fca2b23d0ba 230 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
marcozecchini 0:9fca2b23d0ba 231 self.default_cmd(cmd)
marcozecchini 0:9fca2b23d0ba 232
marcozecchini 0:9fca2b23d0ba 233 @staticmethod
marcozecchini 0:9fca2b23d0ba 234 def name_mangle(name):
marcozecchini 0:9fca2b23d0ba 235 return "_Z%i%sv" % (len(name), name)
marcozecchini 0:9fca2b23d0ba 236
marcozecchini 0:9fca2b23d0ba 237 @staticmethod
marcozecchini 0:9fca2b23d0ba 238 def make_ld_define(name, value):
marcozecchini 0:9fca2b23d0ba 239 return "--config_def %s=0x%x" % (name, value)
marcozecchini 0:9fca2b23d0ba 240
marcozecchini 0:9fca2b23d0ba 241 @staticmethod
marcozecchini 0:9fca2b23d0ba 242 def redirect_symbol(source, sync, build_dir):
marcozecchini 0:9fca2b23d0ba 243 return "--redirect %s=%s" % (source, sync)