1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""A simple tool to decode a CFSR register from the command line 15 16Example usage: 17 18 $ python -m pw_cpu_exception_cortex_m.cfsr_decoder 0x00010100 19 20 20210412 15:09:01 INF Exception caused by a usage fault, bus fault. 21 22 Active Crash Fault Status Register (CFSR) fields: 23 IBUSERR Bus fault on instruction fetch. 24 UNDEFINSTR Encountered invalid instruction. 25 26 All registers: 27 cfsr 0x00010100 28""" 29 30import argparse 31import logging 32import sys 33import pw_cli.log 34 35from pw_cpu_exception_cortex_m_protos import cpu_state_pb2 36from pw_cpu_exception_cortex_m import exception_analyzer 37 38_LOG = logging.getLogger('decode_cfsr') 39 40 41def _parse_args() -> argparse.Namespace: 42 """Parses arguments for this script, splitting out the command to run.""" 43 parser = argparse.ArgumentParser(description=__doc__) 44 parser.add_argument( 45 'cfsr', type=lambda val: int(val, 0), help='The Cortex-M CFSR to decode' 46 ) 47 return parser.parse_args() 48 49 50def dump_cfsr(cfsr: int) -> int: 51 cpu_state_proto = cpu_state_pb2.ArmV7mCpuState() 52 cpu_state_proto.cfsr = cfsr 53 cpu_state_info = exception_analyzer.CortexMExceptionAnalyzer( 54 cpu_state_proto 55 ) 56 _LOG.info(cpu_state_info) 57 return 0 58 59 60if __name__ == '__main__': 61 pw_cli.log.install(level=logging.INFO) 62 sys.exit(dump_cfsr(**vars(_parse_args()))) 63