mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 """
elessair 0:f269e3021894 2 mbed SDK
elessair 0:f269e3021894 3 Copyright (c) 2011-2013 ARM Limited
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 6 you may not use this file except in compliance with the License.
elessair 0:f269e3021894 7 You may obtain a copy of the License at
elessair 0:f269e3021894 8
elessair 0:f269e3021894 9 http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10
elessair 0:f269e3021894 11 Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 limitations under the License.
elessair 0:f269e3021894 16 """
elessair 0:f269e3021894 17 import re
elessair 0:f269e3021894 18 from os.path import join, dirname, splitext, basename
elessair 0:f269e3021894 19
elessair 0:f269e3021894 20 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
elessair 0:f269e3021894 21 from tools.hooks import hook_tool
elessair 0:f269e3021894 22 from tools.utils import mkdir
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 class ARM(mbedToolchain):
elessair 0:f269e3021894 25 LINKER_EXT = '.sct'
elessair 0:f269e3021894 26 LIBRARY_EXT = '.ar'
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 STD_LIB_NAME = "%s.ar"
elessair 0:f269e3021894 29 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error): (?P<message>.+)')
elessair 0:f269e3021894 30 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
elessair 0:f269e3021894 31 DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 @staticmethod
elessair 0:f269e3021894 34 def check_executable():
elessair 0:f269e3021894 35 """Returns True if the executable (armcc) location specified by the
elessair 0:f269e3021894 36 user exists OR the executable can be found on the PATH.
elessair 0:f269e3021894 37 Returns False otherwise."""
elessair 0:f269e3021894 38 return mbedToolchain.generic_check_executable("ARM", 'armcc', 2, 'bin')
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 def __init__(self, target, notify=None, macros=None,
elessair 0:f269e3021894 41 silent=False, extra_verbose=False, build_profile=None):
elessair 0:f269e3021894 42 mbedToolchain.__init__(self, target, notify, macros, silent,
elessair 0:f269e3021894 43 extra_verbose=extra_verbose,
elessair 0:f269e3021894 44 build_profile=build_profile)
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 if target.core == "Cortex-M0+":
elessair 0:f269e3021894 47 cpu = "Cortex-M0"
elessair 0:f269e3021894 48 elif target.core == "Cortex-M4F":
elessair 0:f269e3021894 49 cpu = "Cortex-M4.fp"
elessair 0:f269e3021894 50 elif target.core == "Cortex-M7FD":
elessair 0:f269e3021894 51 cpu = "Cortex-M7.fp.dp"
elessair 0:f269e3021894 52 elif target.core == "Cortex-M7F":
elessair 0:f269e3021894 53 cpu = "Cortex-M7.fp.sp"
elessair 0:f269e3021894 54 else:
elessair 0:f269e3021894 55 cpu = target.core
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 ARM_BIN = join(TOOLCHAIN_PATHS['ARM'], "bin")
elessair 0:f269e3021894 58 ARM_INC = join(TOOLCHAIN_PATHS['ARM'], "include")
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 main_cc = join(ARM_BIN, "armcc")
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 self.flags['common'] += ["--cpu=%s" % cpu]
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 self.asm = [main_cc] + self.flags['common'] + self.flags['asm'] + ["-I \""+ARM_INC+"\""]
elessair 0:f269e3021894 65 self.cc = [main_cc] + self.flags['common'] + self.flags['c'] + ["-I \""+ARM_INC+"\""]
elessair 0:f269e3021894 66 self.cppc = [main_cc] + self.flags['common'] + self.flags['c'] + self.flags['cxx'] + ["-I \""+ARM_INC+"\""]
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 self.ld = [join(ARM_BIN, "armlink")]
elessair 0:f269e3021894 69 self.sys_libs = []
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 self.ar = join(ARM_BIN, "armar")
elessair 0:f269e3021894 72 self.elf2bin = join(ARM_BIN, "fromelf")
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 def parse_dependencies(self, dep_path):
elessair 0:f269e3021894 75 dependencies = []
elessair 0:f269e3021894 76 for line in open(dep_path).readlines():
elessair 0:f269e3021894 77 match = ARM.DEP_PATTERN.match(line)
elessair 0:f269e3021894 78 if match is not None:
elessair 0:f269e3021894 79 #we need to append chroot, because when the .d files are generated the compiler is chrooted
elessair 0:f269e3021894 80 dependencies.append((self.CHROOT if self.CHROOT else '') + match.group('file'))
elessair 0:f269e3021894 81 return dependencies
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 def parse_output(self, output):
elessair 0:f269e3021894 84 msg = None
elessair 0:f269e3021894 85 for line in output.splitlines():
elessair 0:f269e3021894 86 match = ARM.DIAGNOSTIC_PATTERN.match(line)
elessair 0:f269e3021894 87 if match is not None:
elessair 0:f269e3021894 88 if msg is not None:
elessair 0:f269e3021894 89 self.cc_info(msg)
elessair 0:f269e3021894 90 msg = None
elessair 0:f269e3021894 91 msg = {
elessair 0:f269e3021894 92 'severity': match.group('severity').lower(),
elessair 0:f269e3021894 93 'file': match.group('file'),
elessair 0:f269e3021894 94 'line': match.group('line'),
elessair 0:f269e3021894 95 'col': match.group('column') if match.group('column') else 0,
elessair 0:f269e3021894 96 'message': match.group('message'),
elessair 0:f269e3021894 97 'text': '',
elessair 0:f269e3021894 98 'target_name': self.target.name,
elessair 0:f269e3021894 99 'toolchain_name': self.name
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101 elif msg is not None:
elessair 0:f269e3021894 102 # Determine the warning/error column by calculating the ^ position
elessair 0:f269e3021894 103 match = ARM.INDEX_PATTERN.match(line)
elessair 0:f269e3021894 104 if match is not None:
elessair 0:f269e3021894 105 msg['col'] = len(match.group('col'))
elessair 0:f269e3021894 106 self.cc_info(msg)
elessair 0:f269e3021894 107 msg = None
elessair 0:f269e3021894 108 else:
elessair 0:f269e3021894 109 msg['text'] += line+"\n"
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 if msg is not None:
elessair 0:f269e3021894 112 self.cc_info(msg)
elessair 0:f269e3021894 113
elessair 0:f269e3021894 114 def get_dep_option(self, object):
elessair 0:f269e3021894 115 base, _ = splitext(object)
elessair 0:f269e3021894 116 dep_path = base + '.d'
elessair 0:f269e3021894 117 return ["--depend", dep_path]
elessair 0:f269e3021894 118
elessair 0:f269e3021894 119 def get_config_option(self, config_header):
elessair 0:f269e3021894 120 return ['--preinclude=' + config_header]
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122 def get_compile_options(self, defines, includes, for_asm=False):
elessair 0:f269e3021894 123 opts = ['-D%s' % d for d in defines]
elessair 0:f269e3021894 124 if self.RESPONSE_FILES:
elessair 0:f269e3021894 125 opts += ['--via', self.get_inc_file(includes)]
elessair 0:f269e3021894 126 else:
elessair 0:f269e3021894 127 opts += ["-I%s" % i for i in includes]
elessair 0:f269e3021894 128
elessair 0:f269e3021894 129 if not for_asm:
elessair 0:f269e3021894 130 config_header = self.get_config_header()
elessair 0:f269e3021894 131 if config_header is not None:
elessair 0:f269e3021894 132 opts = opts + self.get_config_option(config_header)
elessair 0:f269e3021894 133 return opts
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 @hook_tool
elessair 0:f269e3021894 136 def assemble(self, source, object, includes):
elessair 0:f269e3021894 137 # Preprocess first, then assemble
elessair 0:f269e3021894 138 dir = join(dirname(object), '.temp')
elessair 0:f269e3021894 139 mkdir(dir)
elessair 0:f269e3021894 140 tempfile = join(dir, basename(object) + '.E.s')
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 # Build preprocess assemble command
elessair 0:f269e3021894 143 cmd_pre = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-E", "-o", tempfile, source]
elessair 0:f269e3021894 144
elessair 0:f269e3021894 145 # Build main assemble command
elessair 0:f269e3021894 146 cmd = self.asm + ["-o", object, tempfile]
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148 # Call cmdline hook
elessair 0:f269e3021894 149 cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
elessair 0:f269e3021894 150 cmd = self.hook.get_cmdline_assembler(cmd)
elessair 0:f269e3021894 151
elessair 0:f269e3021894 152 # Return command array, don't execute
elessair 0:f269e3021894 153 return [cmd_pre, cmd]
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 @hook_tool
elessair 0:f269e3021894 156 def compile(self, cc, source, object, includes):
elessair 0:f269e3021894 157 # Build compile command
elessair 0:f269e3021894 158 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 cmd.extend(self.get_dep_option(object))
elessair 0:f269e3021894 161
elessair 0:f269e3021894 162 cmd.extend(["-o", object, source])
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 # Call cmdline hook
elessair 0:f269e3021894 165 cmd = self.hook.get_cmdline_compiler(cmd)
elessair 0:f269e3021894 166
elessair 0:f269e3021894 167 return [cmd]
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 def compile_c(self, source, object, includes):
elessair 0:f269e3021894 170 return self.compile(self.cc, source, object, includes)
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 def compile_cpp(self, source, object, includes):
elessair 0:f269e3021894 173 return self.compile(self.cppc, source, object, includes)
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 @hook_tool
elessair 0:f269e3021894 176 def link(self, output, objects, libraries, lib_dirs, mem_map):
elessair 0:f269e3021894 177 map_file = splitext(output)[0] + ".map"
elessair 0:f269e3021894 178 if len(lib_dirs):
elessair 0:f269e3021894 179 args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--map", "--list=%s" % map_file]
elessair 0:f269e3021894 180 else:
elessair 0:f269e3021894 181 args = ["-o", output, "--info=totals", "--map", "--list=%s" % map_file]
elessair 0:f269e3021894 182
elessair 0:f269e3021894 183 if mem_map:
elessair 0:f269e3021894 184 args.extend(["--scatter", mem_map])
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 # Build linker command
elessair 0:f269e3021894 187 cmd = self.ld + args + objects + libraries + self.sys_libs
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 # Call cmdline hook
elessair 0:f269e3021894 190 cmd = self.hook.get_cmdline_linker(cmd)
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 if self.RESPONSE_FILES:
elessair 0:f269e3021894 193 # Split link command to linker executable + response file
elessair 0:f269e3021894 194 cmd_linker = cmd[0]
elessair 0:f269e3021894 195 link_files = self.get_link_file(cmd[1:])
elessair 0:f269e3021894 196 cmd = [cmd_linker, '--via', link_files]
elessair 0:f269e3021894 197
elessair 0:f269e3021894 198 # Exec command
elessair 0:f269e3021894 199 self.cc_verbose("Link: %s" % ' '.join(cmd))
elessair 0:f269e3021894 200 self.default_cmd(cmd)
elessair 0:f269e3021894 201
elessair 0:f269e3021894 202 @hook_tool
elessair 0:f269e3021894 203 def archive(self, objects, lib_path):
elessair 0:f269e3021894 204 if self.RESPONSE_FILES:
elessair 0:f269e3021894 205 param = ['--via', self.get_arch_file(objects)]
elessair 0:f269e3021894 206 else:
elessair 0:f269e3021894 207 param = objects
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 # Exec command
elessair 0:f269e3021894 210 self.default_cmd([self.ar, '-r', lib_path] + param)
elessair 0:f269e3021894 211
elessair 0:f269e3021894 212 @hook_tool
elessair 0:f269e3021894 213 def binary(self, resources, elf, bin):
elessair 0:f269e3021894 214 # Build binary command
elessair 0:f269e3021894 215 cmd = [self.elf2bin, '--bin', '-o', bin, elf]
elessair 0:f269e3021894 216
elessair 0:f269e3021894 217 # Call cmdline hook
elessair 0:f269e3021894 218 cmd = self.hook.get_cmdline_binary(cmd)
elessair 0:f269e3021894 219
elessair 0:f269e3021894 220 # Exec command
elessair 0:f269e3021894 221 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
elessair 0:f269e3021894 222 self.default_cmd(cmd)
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224
elessair 0:f269e3021894 225 class ARM_STD(ARM):
elessair 0:f269e3021894 226 def __init__(self, target, notify=None, macros=None,
elessair 0:f269e3021894 227 silent=False, extra_verbose=False, build_profile=None):
elessair 0:f269e3021894 228 ARM.__init__(self, target, notify, macros, silent,
elessair 0:f269e3021894 229 extra_verbose=extra_verbose, build_profile=build_profile)
elessair 0:f269e3021894 230
elessair 0:f269e3021894 231 # Run-time values
elessair 0:f269e3021894 232 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])
elessair 0:f269e3021894 233
elessair 0:f269e3021894 234
elessair 0:f269e3021894 235 class ARM_MICRO(ARM):
elessair 0:f269e3021894 236 PATCHED_LIBRARY = False
elessair 0:f269e3021894 237
elessair 0:f269e3021894 238 def __init__(self, target, notify=None, macros=None,
elessair 0:f269e3021894 239 silent=False, extra_verbose=False, build_profile=None):
elessair 0:f269e3021894 240 ARM.__init__(self, target, notify, macros, silent,
elessair 0:f269e3021894 241 extra_verbose=extra_verbose, build_profile=build_profile)
elessair 0:f269e3021894 242
elessair 0:f269e3021894 243 # Run-time values
elessair 0:f269e3021894 244 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])