├── .clang-format ├── .clang-tidy ├── .gitattributes ├── .github ├── FUNDING.yml ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── build-and-upload.yml │ ├── build-pr.yml │ └── lint.yml ├── .gitignore ├── .gitmodules ├── .pre-commit-config.yaml ├── .vscode └── extensions.json ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── COPYING ├── COPYING-BSD ├── COPYING-MIT ├── Makefile ├── README.md ├── SECURITY.md ├── UsingRTT.md ├── UsingSWO.md ├── contrib └── zsh │ └── _blackmagic ├── driver ├── 99-blackmagic-plugdev.rules ├── 99-blackmagic-uucp.rules ├── 99-stlink-plugdev.rules ├── README.md ├── blackmagic.inf └── blackmagic_upgrade.inf ├── scripts ├── README ├── bootprog.py ├── dfu-convert.py ├── dfu.py ├── diff_size.py ├── gdb.py ├── get_openocd_nrf51_ids.py ├── hexprog.py ├── run-clang-tidy.py ├── stm32_mem.py ├── stubs │ ├── Makefile │ ├── stm32_opterase.S │ └── stm32_optprog.S └── swolisten.c ├── shell.nix ├── src ├── Makefile ├── command.c ├── crc32.c ├── exception.c ├── gdb_hostio.c ├── gdb_hostio.h ├── gdb_main.c ├── gdb_packet.c ├── hex_utils.c ├── include │ ├── command.h │ ├── crc32.h │ ├── exception.h │ ├── gdb_if.h │ ├── gdb_main.h │ ├── gdb_packet.h │ ├── general.h │ ├── hex_utils.h │ ├── jtagtap.h │ ├── morse.h │ ├── platform_support.h │ ├── rtt.h │ ├── rtt_if.h │ ├── serialno.h │ ├── swd.h │ ├── target.h │ └── timing.h ├── main.c ├── morse.c ├── platforms │ ├── 96b_carbon │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ └── usbdfu.c │ ├── README.md │ ├── blackpillv2 │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ └── usbdfu.c │ ├── common │ │ ├── Makefile.inc │ │ ├── aux_serial.c │ │ ├── aux_serial.h │ │ ├── jtagtap.c │ │ ├── swdptap.c │ │ ├── traceswo.h │ │ ├── usb.c │ │ ├── usb.h │ │ ├── usb_descriptors.h │ │ ├── usb_dfu_stub.c │ │ ├── usb_dfu_stub.h │ │ ├── usb_serial.c │ │ ├── usb_serial.h │ │ └── usb_types.h │ ├── f072 │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ └── platform.h │ ├── f3 │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ └── platform.h │ ├── f4discovery │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ └── usbdfu.c │ ├── hosted │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── bmp_hosted.h │ │ ├── bmp_libusb.c │ │ ├── bmp_remote.c │ │ ├── bmp_remote.h │ │ ├── bmp_serial.c │ │ ├── cli.c │ │ ├── cli.h │ │ ├── cmsis_dap.c │ │ ├── cmsis_dap.h │ │ ├── dap.c │ │ ├── dap.h │ │ ├── dap_command.c │ │ ├── dap_command.h │ │ ├── ftdi_bmp.c │ │ ├── ftdi_bmp.h │ │ ├── gdb_if.c │ │ ├── jlink.c │ │ ├── jlink.h │ │ ├── jlink_adiv5_swdp.c │ │ ├── jlink_jtagtap.c │ │ ├── libftdi_jtagtap.c │ │ ├── libftdi_swdptap.c │ │ ├── platform.c │ │ ├── platform.h │ │ ├── probe_info.c │ │ ├── probe_info.h │ │ ├── remote_jtagtap.c │ │ ├── remote_swdptap.c │ │ ├── rtt_if.c │ │ ├── serial_unix.c │ │ ├── serial_win.c │ │ ├── stlinkv2.c │ │ ├── stlinkv2.h │ │ ├── utils.c │ │ └── utils.h │ ├── hydrabus │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ └── usbdfu.c │ ├── launchpad-icdi │ │ ├── Makefile.inc │ │ ├── platform.c │ │ ├── platform.h │ │ └── rtt_if.c │ ├── native │ │ ├── Makefile.inc │ │ ├── platform.c │ │ ├── platform.h │ │ └── usbdfu.c │ ├── stlink │ │ ├── Bootloader_Upgrade │ │ ├── Connector │ │ ├── Flashsize_F103 │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── Readme │ │ ├── platform.c │ │ ├── platform.h │ │ ├── stlink_common.c │ │ └── usbdfu.c │ ├── stlinkv3 │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ ├── stlinkv3.ld │ │ ├── usb_f723.c │ │ └── usbdfu.c │ ├── stm32 │ │ ├── 96b_carbon.ld │ │ ├── blackmagic.ld │ │ ├── blackpillv2.ld │ │ ├── dfu_f1.c │ │ ├── dfu_f4.c │ │ ├── dfucore.c │ │ ├── f4discovery.ld │ │ ├── gdb_if.c │ │ ├── gpio.h │ │ ├── rtt_if.c │ │ ├── serialno.c │ │ ├── stlink.ld │ │ ├── stm32_can.ld │ │ ├── stm32f07xzb.ld │ │ ├── stm32f303xc.ld │ │ ├── timing_stm32.c │ │ ├── timing_stm32.h │ │ ├── traceswo.c │ │ ├── traceswoasync.c │ │ ├── traceswoasync_f723.c │ │ ├── traceswodecode.c │ │ └── usbdfu.h │ ├── swlink │ │ ├── Connectors │ │ ├── Makefile.inc │ │ ├── README.md │ │ ├── platform.c │ │ ├── platform.h │ │ ├── platform_common.c │ │ ├── platform_common.h │ │ └── usbdfu.c │ └── tm4c │ │ ├── gdb_if.c │ │ ├── tm4c.ld │ │ └── traceswo.c ├── remote.c ├── remote.h ├── rtt.c ├── target │ ├── HACKING.md │ ├── adiv5.c │ ├── adiv5.h │ ├── adiv5_jtagdp.c │ ├── adiv5_swdp.c │ ├── ch32f1.c │ ├── cortexa.c │ ├── cortexm.c │ ├── cortexm.h │ ├── efm32.c │ ├── flashstub │ │ ├── Makefile │ │ ├── README.md │ │ ├── efm32.c │ │ ├── efm32.stub │ │ ├── lmi.c │ │ ├── lmi.stub │ │ ├── nrf51.stub │ │ └── stub.h │ ├── gdb_reg.c │ ├── gdb_reg.h │ ├── jep106.h │ ├── jtag_devs.c │ ├── jtag_devs.h │ ├── jtag_scan.c │ ├── jtag_scan.h │ ├── jtagtap_generic.c │ ├── kinetis.c │ ├── lmi.c │ ├── lpc11xx.c │ ├── lpc15xx.c │ ├── lpc17xx.c │ ├── lpc43xx.c │ ├── lpc546xx.c │ ├── lpc55xx.c │ ├── lpc_common.c │ ├── lpc_common.h │ ├── msp432.c │ ├── nrf51.c │ ├── nxpke04.c │ ├── renesas.c │ ├── rp.c │ ├── sam3x.c │ ├── sam4l.c │ ├── samd.c │ ├── samx5x.c │ ├── semihosting.h │ ├── sfdp.c │ ├── sfdp.h │ ├── sfdp_internal.h │ ├── stm32_common.h │ ├── stm32f1.c │ ├── stm32f4.c │ ├── stm32g0.c │ ├── stm32h7.c │ ├── stm32l0.c │ ├── stm32l4.c │ ├── swdptap_generic.c │ ├── target.c │ ├── target_flash.c │ ├── target_internal.h │ ├── target_probe.c │ └── target_probe.h └── timing.c └── upgrade ├── Makefile ├── bindata.S ├── bindata.h ├── dfu.c ├── dfu.h ├── main.c ├── stm32mem.c └── stm32mem.h /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: -4 3 | AlignAfterOpenBracket: DontAlign 4 | AlignConsecutiveAssignments: false 5 | AlignConsecutiveDeclarations: false 6 | AlignConsecutiveMacros: true 7 | AlignEscapedNewlines: Left 8 | AlignOperands: DontAlign 9 | AllowAllArgumentsOnNextLine: true 10 | AllowShortFunctionsOnASingleLine: None 11 | AllowShortIfStatementsOnASingleLine: false 12 | AllowShortEnumsOnASingleLine: false 13 | # This is currently broken: 14 | # https://github.com/llvm/llvm-project/issues/53442 15 | #AlignArrayOfStructures: Left 16 | BraceWrapping: 17 | AfterClass: false 18 | AfterControlStatement: false 19 | AfterEnum: false 20 | AfterFunction: true 21 | AfterNamespace: true 22 | AfterObjCDeclaration: false 23 | AfterStruct: false 24 | AfterUnion: false 25 | BeforeCatch: false 26 | BeforeElse: false 27 | IndentBraces: false 28 | BreakBeforeBraces: Linux 29 | BreakBeforeTernaryOperators: false 30 | BasedOnStyle: LLVM 31 | BinPackArguments: true 32 | BinPackParameters: true 33 | ColumnLimit: 120 34 | Cpp11BracedListStyle: true 35 | IndentCaseLabels: false 36 | IndentWidth: 4 37 | ContinuationIndentWidth: 4 38 | MaxEmptyLinesToKeep: 1 39 | KeepEmptyLinesAtTheStartOfBlocks: false 40 | FixNamespaceComments: true 41 | ForEachMacros: ['TRY_CATCH'] 42 | #QualifierAlignment: Custom 43 | #QualifierOrder: ['inline', 'static', 'const', 'volatile', 'type'] 44 | SpaceAroundPointerQualifiers: After 45 | SeparateDefinitionBlocks: Always 46 | 47 | # Taken from git's rules 48 | #PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0 49 | PenaltyBreakBeforeFirstCallParameter: 50 50 | PenaltyBreakComment: 10 51 | PenaltyBreakFirstLessLess: 0 52 | PenaltyBreakString: 10 53 | PenaltyExcessCharacter: 100 54 | PenaltyReturnTypeOnItsOwnLine: 60 55 | 56 | PointerAlignment: Right 57 | ReflowComments: false 58 | SpacesBeforeTrailingComments: 1 59 | SortIncludes: false 60 | TabWidth: 4 61 | UseTab: AlignWithSpaces 62 | -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | --- 2 | Checks: 'bugprone-*,cert-*,clang-analyzer-*,misc-*,modernize-*,portability-*,performance-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-modernize-macro-to-enum,-bugprone-easily-swappable-parameters' 3 | FormatStyle: 'none' 4 | HeaderFilterRegex: '(src|upgrade)/.+' 5 | AnalyzeTemporaryDtors: false 6 | CheckOptions: 7 | - key: cert-dcl16-c.NewSuffixes 8 | value: 'L;LL;UL;ULL' 9 | - key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField 10 | value: '0' 11 | - key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors 12 | value: '0' 13 | - key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic 14 | value: '1' 15 | - key: modernize-loop-convert.MaxCopySize 16 | value: '16' 17 | - key: modernize-loop-convert.MinConfidence 18 | value: reasonable 19 | - key: modernize-loop-convert.NamingStyle 20 | value: camelBack 21 | - key: modernize-pass-by-value.IncludeStyle 22 | value: llvm 23 | - key: modernize-replace-auto-ptr.IncludeStyle 24 | value: llvm 25 | - key: modernize-use-nullptr.NullMacros 26 | value: 'NULL' 27 | - key: readability-braces-around-statements.ShortStatementLines 28 | value: '2' 29 | ... 30 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Text for humans 2 | LICENSE text eol=lf 3 | HACKING text eol=lf 4 | COPYING text eol=lf 5 | UsingSWO text eol=lf 6 | README.* text eol=lf 7 | 8 | # Source code 9 | Makefile text eol=lf 10 | *.mk text eol=lf 11 | *.mak text eol=lf 12 | *.inc text eol=lf 13 | *.py text eol=lf 14 | *.sh text eol=lf 15 | *.c text eol=lf 16 | *.S text eol=lf 17 | *.s text eol=lf 18 | *.h text eol=lf 19 | *.ld text eol=lf 20 | *.yml text eol=lf 21 | *.rules text eol=lf 22 | 23 | # Git control files 24 | .gitattributes eol=lf 25 | .gitignore eol=lf 26 | .gitmodules eol=lf 27 | 28 | # Windows source code uses CRLF 29 | *.vcxproj text eol=crlf 30 | *.props text eol=crlf 31 | *.bat text eol=crlf 32 | *.ps1 text eol=crlf 33 | *.inf text eol=crlf 34 | 35 | # Other binary files 36 | *.png binary 37 | *.jpg binary 38 | *.bin binary 39 | *.elf binary 40 | *.bin binary 41 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [esden, dragonmux] 2 | patreon: 1bitsquared 3 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Detailed description 4 | 5 | 13 | 14 | ## Your checklist for this pull request 15 | 16 | * [ ] I've read the [Code of Conduct](https://github.com/blackmagic-debug/blackmagic/blob/main/CODE_OF_CONDUCT.md) 17 | * [ ] I've read the [guidelines for contributing](https://github.com/blackmagic-debug/blackmagic/blob/main/CONTRIBUTING.md) to this repository 18 | * [ ] It builds for hardware native (`make PROBE_HOST=native`) 19 | * [ ] It builds as BMDA (`make PROBE_HOST=hosted`) 20 | * [ ] I've tested it to the best of my ability 21 | * [ ] My commit messages provide a useful short description of what the commits do 22 | 23 | ## Closing issues 24 | 25 | 26 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: lint 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | 7 | pull_request: 8 | branches: [ main ] 9 | 10 | workflow_dispatch: 11 | 12 | jobs: 13 | pre-commit: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v3.0.2 18 | 19 | - name: Run pre-commit 20 | uses: before-commit/run-action@v2.0.3 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | src/include/version.h 2 | blackmagic 3 | *.bin 4 | *.hex 5 | blackmagic_dfu 6 | dfu_upgrade 7 | mapfile 8 | *.o 9 | *.d 10 | .*.swp 11 | *~ 12 | *.pyc 13 | tags 14 | .gdbinit 15 | *.s#* 16 | *.b#* 17 | blackmagic_upgrade 18 | *.exe 19 | *.elf 20 | .vscode 21 | .gdb_history 22 | src/artifacts/ 23 | 24 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "libopencm3"] 2 | path = libopencm3 3 | url = https://github.com/libopencm3/libopencm3.git 4 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | exclude: '^upgrade/|^scripts/' # don't run hooks on scripts/ and upgrade/ 2 | 3 | repos: 4 | - repo: https://github.com/pre-commit/mirrors-clang-format 5 | rev: v15.0.4 6 | hooks: 7 | - id: clang-format 8 | args: [-Werror] # change formatting warnings to errors, hook includes -i (Inplace edit) by default 9 | types_or: [c++, c] # override default file types to only C and CPP files 10 | 11 | - repo: https://github.com/perigoso/pre-commit-hooks 12 | rev: v0.2 13 | hooks: 14 | - id: check-hex-case 15 | args: [--lower-digits] # edit-in-place, hex number digits to lower case (defaults to upper case), i.e. 0x55aa 16 | 17 | - id: check-accidental-assignment 18 | args: [--strict, --skip-keywords, DO_PRAGMA, --] # strict, check in all parentheses found, skip check in DO_PRAGMA macro 19 | # FIXME: Known assignment in find_debuggers, needs large refactoring, ignore file temporarily so we get a working check for the rest of the code base 20 | exclude: '^src/platforms/hosted/bmp_serial.c' 21 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "ms-vscode.cpptools", 4 | "notskm.clang-tidy" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /COPYING-BSD: -------------------------------------------------------------------------------- 1 | Copyright (C) 2022 1BitSquared 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /COPYING-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (C) 2022 1BitSquared 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(V), 1) 2 | MFLAGS += --no-print-dir 3 | Q := @ 4 | endif 5 | 6 | PC_HOSTED = 7 | NO_LIBOPENCM3 = 8 | ifeq ($(PROBE_HOST), hosted) 9 | PC_HOSTED = true 10 | NO_LIBOPENCM3 = true 11 | endif 12 | 13 | all: 14 | ifndef NO_LIBOPENCM3 15 | $(Q)if [ ! -f libopencm3/Makefile ]; then \ 16 | echo "Initialising git submodules..." ;\ 17 | git submodule init ;\ 18 | git submodule update ;\ 19 | fi 20 | $(Q)$(MAKE) $(MFLAGS) -C libopencm3 lib/stm32/f1 lib/stm32/f4 lib/lm4f 21 | endif 22 | $(Q)$(MAKE) $(MFLAGS) -C src 23 | 24 | all_platforms: 25 | $(Q)$(MAKE) $(MFLAGS) -C src $@ 26 | 27 | clean: 28 | ifndef NO_LIBOPENCM3 29 | $(Q)$(MAKE) $(MFLAGS) -C libopencm3 $@ 30 | endif 31 | $(Q)$(MAKE) $(MFLAGS) -C src $@ 32 | 33 | clang-tidy: SYSTEM_INCLUDE_PATHS=$(shell pkg-config --silence-errors --cflags libusb-1.0 libftdi1) 34 | clang-tidy: 35 | $(Q)scripts/run-clang-tidy.py -s "$(PWD)" $(SYSTEM_INCLUDE_PATHS) 36 | 37 | clang-format: 38 | $(Q)$(MAKE) $(MFLAGS) -C src $@ 39 | 40 | .PHONY: clean all_platforms clang-tidy clang-format 41 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | | Version | Supported | 6 | |---------|--------------------| 7 | | 1.9.x | :white_check_mark: | 8 | | 1.8.x | :white_check_mark: | 9 | |<= 1.7.x | :x: | 10 | 11 | Any older version not listed in the above table is also not supported 12 | 13 | ## Reporting a Vulnerability 14 | 15 | If you think you've found a vulnerability and wish to responsibly disclose it, please send an email 16 | to disclosure@black-magic.org with a clear description of the issue and steps for how to reproduce 17 | it in the body. 18 | 19 | If we can reproduce the issue we will create a fix branch on the main repository with nondescript 20 | commit messages as to the underlying issue and take the fix through our normal pull request process. 21 | For severe issues we may instead use the private mirror of the repository for fixing the bug so we 22 | can publish a fix and release binaries only once ready. 23 | 24 | Severe security issues may result in out of band point releases being made to address the problem, 25 | even to otherwise unsupported versions. This will be done at the discretion of the project. 26 | 27 | During the process, you will be kept apprised of our progress by email with public disclosure on the 28 | issue tracker or as a PR only once the fix is developed and ready. 29 | -------------------------------------------------------------------------------- /driver/99-blackmagic-plugdev.rules: -------------------------------------------------------------------------------- 1 | # Black Magic Probe 2 | # there are two connections, one for GDB and one for UART debugging 3 | # copy this to /etc/udev/rules.d/99-blackmagic.rules 4 | # and run sudo udevadm control -R 5 | ACTION!="add|change", GOTO="blackmagic_rules_end" 6 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic GDB Server", SYMLINK+="ttyBmpGdb" 7 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic UART Port", SYMLINK+="ttyBmpTarg" 8 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic GDB Server", SYMLINK+="ttyBmpGdb%E{ID_SERIAL_SHORT}" 9 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic UART Port", SYMLINK+="ttyBmpTarg%E{ID_SERIAL_SHORT}" 10 | SUBSYSTEM=="usb", ATTR{idVendor}=="1d50", ATTR{idProduct}=="6017", MODE="0666", GROUP="plugdev", TAG+="uaccess" 11 | SUBSYSTEM=="usb", ATTR{idVendor}=="1d50", ATTR{idProduct}=="6018", MODE="0666", GROUP="plugdev", TAG+="uaccess" 12 | LABEL="blackmagic_rules_end" 13 | -------------------------------------------------------------------------------- /driver/99-blackmagic-uucp.rules: -------------------------------------------------------------------------------- 1 | # Black Magic Probe 2 | # there are two connections, one for GDB and one for UART debugging 3 | # copy this to /etc/udev/rules.d/99-blackmagic.rules 4 | # and run sudo udevadm control -R 5 | ACTION!="add|change", GOTO="blackmagic_rules_end" 6 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic GDB Server", SYMLINK+="ttyBmpGdb" 7 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic UART Port", SYMLINK+="ttyBmpTarg" 8 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic GDB Server", SYMLINK+="ttyBmpGdb%E{ID_SERIAL_SHORT}" 9 | SUBSYSTEM=="tty", ACTION=="add", ATTRS{interface}=="Black Magic UART Port", SYMLINK+="ttyBmpTarg%E{ID_SERIAL_SHORT}" 10 | SUBSYSTEM=="usb", ATTR{idVendor}=="1d50", ATTR{idProduct}=="6017", MODE="0666", GROUP="uucp", TAG+="uaccess" 11 | SUBSYSTEM=="usb", ATTR{idVendor}=="1d50", ATTR{idProduct}=="6018", MODE="0666", GROUP="uucp", TAG+="uaccess" 12 | LABEL="blackmagic_rules_end" 13 | -------------------------------------------------------------------------------- /driver/99-stlink-plugdev.rules: -------------------------------------------------------------------------------- 1 | # ST-Link Debug Probes 2 | # 3 | # Copy this file to /etc/udev/rules.d/99-stlink-plugdev.rules 4 | # and run sudo udevadm control -R 5 | # 6 | # ST-Link v1 7 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3744", TAG+="uaccess", MODE="0664", GROUP="plugdev" 8 | # 9 | # ST-Link v2 10 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", TAG+="uaccess", MODE="0664", GROUP="plugdev" 11 | # 12 | # ST-Link v2.1 13 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374a", MODE="0664", TAG+="uaccess", GROUP="plugdev" 14 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE="0664", TAG+="uaccess", GROUP="plugdev" 15 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3752", MODE="0664", TAG+="uaccess", GROUP="plugdev" 16 | # 17 | # STLink V3SET in Dual CDC mode 18 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3752", TAG+="uaccess", MODE="0664", GROUP="plugdev" 19 | # 20 | # STLink V3SET in Dual CDC mode 21 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3753", TAG+="uaccess", MODE="0664", GROUP="plugdev" 22 | # 23 | # STLink V3SET 24 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374d", TAG+="uaccess", MODE="0664", GROUP="plugdev" 25 | # 26 | # STLink V3SET 27 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374e", TAG+="uaccess", MODE="0664", GROUP="plugdev" 28 | # 29 | # STLink V3SET in normal mode 30 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374f", TAG+="uaccess", MODE="0664", GROUP='plugdev" 31 | -------------------------------------------------------------------------------- /driver/README.md: -------------------------------------------------------------------------------- 1 | # Driver and access files for BMP 2 | 3 | This directory contains a few different important files described in the sections below. These are: 4 | 5 | * [99-blackmagic-plugdev.rules](#99-blackmagic-plugdevrules) 6 | * [99-blackmagic-uucp.rules](#99-blackmagic-uucprules) 7 | * [blackmagic.inf](#blackmagicinf) 8 | * [blackmagic_upgrade.inf](#blackmagic_upgradeinf) 9 | 10 | ## udev rules installation 11 | 12 | Depending on the specific distribution you can choose either the `plugdev` or `uucp` versions 13 | of udev rules. For more information about the distributions and what the differences are refer 14 | to the following sections. 15 | 16 | Independent of which udev rule file is used, the selected file has to be copied into 17 | `/etc/udev/rules.d` directory. The udev rules can then be reloaded with `sudo udevadm control 18 | --reload-rules`. After that the Black Magic Probe can be connected to the system and the new 19 | rules should apply to the connected device. 20 | 21 | ## 99-blackmagic-plugdev.rules 22 | 23 | This file contains [udev](https://www.freedesktop.org/wiki/Software/systemd) rules for Linux 24 | systems that use 'plugdev' as the access group for plug-in devices. This allows GDB, dfu-util, 25 | and other utilities access to your Black Magic Probe hardware without needing `sudo` / root on 26 | the computer you're trying to access the hardware from. 27 | 28 | Distros that use 'plugdev' for this purpose include: 29 | 30 | * Debian 31 | * Ubuntu 32 | * Linux Mint 33 | 34 | ## 99-blackmagic-uucp.rules 35 | 36 | This file contains [udev](https://www.freedesktop.org/wiki/Software/systemd) rules for Linux 37 | systems that use 'uucp' as the access group for plug-in devices. This allows GDB, dfu-util, and 38 | other utilities access to your Black Magic Probe hardware without needing `sudo` / root on the 39 | computer you're trying to access the hardware from. 40 | 41 | Distros that use 'uucp' for this purpose include: 42 | 43 | * Arch 44 | * Manjaro 45 | 46 | ## Notes about udev rules 47 | 48 | Our udev rules include use of the 'uaccess' tag which means either rules file should just work 49 | on any modern distro that uses systemd. The specific user access group only matters when not 50 | using systemd, or on older distros that predate the user access tag. 51 | 52 | ## blackmagic.inf 53 | 54 | This is a windows driver "installation" (actually binding) information file which when 55 | right-clicked and after clicking Install will name the serial ports Black Magic Probe provides 56 | properly and provide them their proper vendor names. 57 | 58 | ## blackmagic_upgrade.inf 59 | 60 | This is a windows driver "installation" (binding) information file which when right-clicked and 61 | after clicking Install will bind suitable drivers and names to the DFU (firmware upgrade) and 62 | Trace/Capture interfaces that Black Magic Probe provides + give them their proper vendor names. 63 | -------------------------------------------------------------------------------- /driver/blackmagic.inf: -------------------------------------------------------------------------------- 1 | ; Windows USB CDC ACM driver setup file. 2 | 3 | ; Copyright (C) 2004 Al Borchers (alborchers@steinerpoint.com) 4 | ; Taken from Linux documentation, modified for Black Magic debug probe 5 | ; by Gareth McMullin 6 | 7 | ; This provides the driver information for the GDB and UART interfaces to 8 | ; be presented as virtual serial ports in Windows. 9 | 10 | ; Common to Windows 32- and 64-bit systems 11 | 12 | [Version] 13 | Signature="$Windows NT$" 14 | Class=Ports 15 | ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318} 16 | Provider=%BLACKMAGIC% 17 | DriverVer=28/12/2011,0.0.1.1 18 | 19 | [Manufacturer] 20 | %VendorName%=DeviceList, NTamd64 21 | 22 | [Strings] 23 | VendorName = "Black Magic Debug" 24 | BLACKMAGICGDB = "Black Magic GDB Server" 25 | BLACKMAGICUART = "Black Magic UART Port" 26 | BLACKMAGIC_DISPLAY_NAME = "Black Magic Probe Driver" 27 | 28 | [DeviceList] 29 | %BLACKMAGICGDB%=DriverInstall, USB\VID_1d50&PID_6018&Rev_0100&MI_00 30 | %BLACKMAGICUART%=DriverInstall, USB\VID_1d50&PID_6018&Rev_0100&MI_02 31 | 32 | [DeviceList.NTamd64] 33 | %BLACKMAGICGDB%=DriverInstall, USB\VID_1d50&PID_6018&Rev_0100&MI_00 34 | %BLACKMAGICUART%=DriverInstall, USB\VID_1d50&PID_6018&Rev_0100&MI_02 35 | 36 | [DestinationDirs] 37 | DefaultDestDir=10,System32\Drivers 38 | 39 | ; Windows 32-bit sections 40 | ;~~~~~~~~~~~~~~~~~~~~~~~~~ 41 | 42 | [DriverInstall.nt] 43 | CopyFiles=DriverCopyFiles.nt 44 | AddReg=DriverInstall.nt.AddReg 45 | 46 | [DriverCopyFiles.nt] 47 | usbser.sys,,,0x20 48 | 49 | [DriverInstall.nt.AddReg] 50 | HKR,,DevLoader,,*ntkern 51 | HKR,,NTMPDriver,,usbser.sys 52 | HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider" 53 | 54 | [DriverInstall.nt.Services] 55 | AddService = usbser,0x0002,DriverService.nt 56 | 57 | [DriverService.nt] 58 | DisplayName = %BLACKMAGIC_DISPLAY_NAME% 59 | ServiceType = 1 ; SERVICE_KERNEL_DRIVER 60 | StartType = 3 ; SERVICE_DEMAND_START 61 | ErrorControl = 1 ; SERVICE_ERROR_NORMAL 62 | ServiceBinary = %10%\System32\Drivers\usbser.sys 63 | LoadOrderGroup = Base 64 | 65 | ; Windows 64-bit sections 66 | ;~~~~~~~~~~~~~~~~~~~~~~~~~ 67 | [DriverInstall.NTamd64] 68 | CopyFiles=DriverCopyFiles.NTamd64 69 | AddReg=DriverInstall.NTamd64.AddReg 70 | 71 | [DriverCopyFiles.NTamd64] 72 | usbser.sys,,,0x20 73 | 74 | [DriverInstall.NTamd64.AddReg] 75 | HKR,,DevLoader,,*ntkern 76 | HKR,,NTMPDriver,,usbser.sys 77 | HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider" 78 | 79 | [DriverInstall.NTamd64.Services] 80 | AddService = usbser,0x0002,DriverService.NTamd64 81 | 82 | [DriverService.NTamd64] 83 | DisplayName = %BLACKMAGIC_DISPLAY_NAME% 84 | ServiceType = 1 ; SERVICE_KERNEL_DRIVER 85 | StartType = 3 ; SERVICE_DEMAND_START 86 | ErrorControl = 1 ; SERVICE_ERROR_NORMAL 87 | ServiceBinary = %10%\System32\Drivers\usbser.sys 88 | LoadOrderGroup = Base 89 | 90 | -------------------------------------------------------------------------------- /scripts/README: -------------------------------------------------------------------------------- 1 | This directory contains some useful scripts for working 2 | on the Black Magic Debug project. 3 | 4 | bootprog.py - Production programmer using the STM32 SystemMemory bootloader. 5 | hexprog.py - Write an Intel hex file to a target using the GDB protocol. 6 | stm32_mem.py - Access STM32 Flash memory using USB DFU class interface. 7 | 8 | stubs/ - Source code for the microcode strings included in hexprog.py. 9 | 10 | -------------------------------------------------------------------------------- /scripts/get_openocd_nrf51_ids.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """Pulls nRF51 IDs from openocd's nrf51.c in a form suitable for 4 | pasting into blackmagic's nrf51.c 5 | 6 | """ 7 | 8 | import subprocess 9 | import re 10 | import io 11 | 12 | cmd = 'git archive --remote=git://git.code.sf.net/p/openocd/code HEAD src/flash/nor/nrf5.c | tar -xO' 13 | 14 | class Spec(): 15 | def __repr__(self): 16 | return "0x%04X: /* %s %s %s */"%(self.hwid,self.comment, self.variant,self.build_code) 17 | 18 | proc = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) 19 | 20 | specdict = {} 21 | specs = [] 22 | spec = Spec() 23 | for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): 24 | m = re.search(r'/\*(.*)\*/',line) 25 | if m: 26 | lastcomment=m.group(1) 27 | 28 | m = re.search(r'NRF51_DEVICE_DEF\((0x[0-9A-F]*),\s*"(.*)",\s*"(.*)",\s*"(.*)",\s*([0-9]*)\s*\),', line) 29 | if m: 30 | spec.hwid = int(m.group(1), base=0) 31 | spec.variant = m.group(3) 32 | spec.build_code = m.group(4) 33 | spec.flash_size_kb = int(m.group(5), base=0) 34 | ram, flash = {'AA':(16,256), 35 | 'AB':(16,128), 36 | 'AC':(32,256)}[spec.variant[-2:]] 37 | assert flash == spec.flash_size_kb 38 | spec.ram_size_kb = ram 39 | nicecomment = lastcomment.strip().replace('IC ','').replace('Devices ','').replace('.','') 40 | spec.comment = nicecomment 41 | 42 | specdict.setdefault((ram,flash),[]).append(spec) 43 | specs.append(spec) 44 | spec=Spec() 45 | 46 | for (ram,flash),specs in specdict.items(): 47 | specs.sort(key=lambda x:x.hwid) 48 | for spec in specs: 49 | print("\tcase",spec) 50 | print('\t\tt->driver = "Nordic nRF51";') 51 | print('\t\ttarget_add_ram(t, 0x20000000, 0x%X);'%(1024*ram)) 52 | print('\t\tnrf51_add_flash(t, 0x00000000, 0x%X, NRF51_PAGE_SIZE);'%(1024*flash)) 53 | print('\t\tnrf51_add_flash(t, NRF51_UICR, 0x100, 0x100);') 54 | print('\t\ttarget_add_commands(t, nrf51_cmd_list, "nRF51");') 55 | print('\t\treturn true;') 56 | 57 | -------------------------------------------------------------------------------- /scripts/run-clang-tidy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # SPDX-License-Identifier: BSD-3-Clause 3 | from argparse import ArgumentParser 4 | from pathlib import Path 5 | from subprocess import run 6 | from concurrent.futures import ThreadPoolExecutor 7 | from sys import exit 8 | 9 | parser = ArgumentParser( 10 | description = 'Light-weight wrapper around clang-tidy to enable `make clang-tidy` or ' + 11 | '`ninja clang-tidy` to function properly', 12 | allow_abbrev = False 13 | ) 14 | parser.add_argument('-s', required = True, type = str, metavar = 'sourcePath', 15 | dest = 'sourcePath', help = 'Path to the source directory to run clang-tidy in') 16 | parser.add_argument('-p', type = str, metavar = 'buildPath', 17 | dest = 'buildPath', help = 'Path to the build directory containing a compile_commands.json') 18 | parser.add_argument('-I', type = str, action = 'append', metavar = 'includePaths', 19 | dest = 'includePaths', default = [], help = 'Additional include paths to use') 20 | args = parser.parse_args() 21 | 22 | def globFiles(): 23 | srcDir = Path(args.sourcePath) 24 | paths = set(('src', 'upgrade')) 25 | suffixes = set(('c', 'h')) 26 | for path in paths: 27 | for suffix in suffixes: 28 | yield srcDir.glob('{}/**/*.{}'.format(path, suffix)) 29 | 30 | def gatherFiles(): 31 | for fileGlob in globFiles(): 32 | for file in fileGlob: 33 | yield file 34 | 35 | extraArgs = [ 36 | '-Isrc/target', '-Isrc', '-Isrc/include', '-Isrc/platforms/common', 37 | '-Isrc/platforms/native', '-Ilibopencm3/include', '-Isrc/platforms/stm32' 38 | ] + args.includePaths 39 | 40 | for i, arg in enumerate(extraArgs): 41 | extraArgs[i] = f'--extra-arg={arg}' 42 | 43 | if args.buildPath is not None: 44 | print(f'Adding build path "{args.buildPath}" to extraArgs') 45 | extraArgs += ['-p', args.buildPath] 46 | 47 | futures = [] 48 | returncode = 0 49 | with ThreadPoolExecutor() as pool: 50 | for file in gatherFiles(): 51 | futures.append(pool.submit(run, ['clang-tidy'] + extraArgs + [str(file)])) 52 | returncode = max((future.result().returncode for future in futures)) 53 | exit(returncode) 54 | -------------------------------------------------------------------------------- /scripts/stubs/Makefile: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE = stm32- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | all: stm32_opterase.bin stm32_optprog.bin 6 | 7 | %.bin: %.S 8 | $(CC) -nostdlib -Wl,-Ttext,0x20000000 $< 9 | $(OBJCOPY) -O binary a.out $@ 10 | 11 | clean: 12 | -rm *.bin 13 | -------------------------------------------------------------------------------- /scripts/stubs/stm32_opterase.S: -------------------------------------------------------------------------------- 1 | @; Assembler sequence to erase option bytes on STM32 2 | @; Takes no parameters, ends with BKPT instruction 3 | .global _start 4 | 5 | .equ FLASHBASE, 0x40022000 6 | 7 | .equ KEY1, 0x45670123 8 | .equ KEY2, 0xCDEF89AB 9 | 10 | .equ FLASH_KEY, 0x04 11 | .equ FLASH_OPTKEY, 0x08 12 | .equ FLASH_CR, 0x10 13 | .equ FLASH_SR, 0x0C 14 | 15 | .equ OPTER, 0x20 16 | .equ STRT, 0x40 17 | 18 | .equ BSY, 0x01 19 | 20 | .syntax unified 21 | 22 | _start: 23 | @; Load FLASH controller base address 24 | ldr r0, =FLASHBASE 25 | 26 | @; Do unlocking sequence 27 | ldr r1, =KEY1 28 | str r1, [r0, #FLASH_KEY] 29 | ldr r1, =KEY2 30 | str r1, [r0, #FLASH_KEY] 31 | 32 | @; Same for option bytes 33 | ldr r1, =KEY1 34 | str r1, [r0, #FLASH_OPTKEY] 35 | ldr r1, =KEY2 36 | str r1, [r0, #FLASH_OPTKEY] 37 | 38 | @; Set OPTER bit in FLASH_CR 39 | ldr r1, [r0, #FLASH_CR] 40 | orr r1, r1, #OPTER 41 | str r1, [r0, #FLASH_CR] 42 | @; Set STRT bit in FLASH_CR 43 | orr r1, r1, #STRT 44 | str r1, [r0, #FLASH_CR] 45 | 46 | _wait: @; Wait for BSY bit to clear 47 | ldr r4, [r0, #FLASH_SR] 48 | mov r6, #BSY 49 | tst r4, r6 50 | bne _wait 51 | 52 | bkpt 53 | 54 | -------------------------------------------------------------------------------- /scripts/stubs/stm32_optprog.S: -------------------------------------------------------------------------------- 1 | @; Assembler sequence to program option bytes on STM32 2 | @; Takes option address in r0 and value in r1. 3 | @; Ends with BKPT instruction 4 | .global _start 5 | 6 | .equ FLASHBASE, 0x40022000 7 | 8 | .equ KEY1, 0x45670123 9 | .equ KEY2, 0xCDEF89AB 10 | 11 | .equ FLASH_KEY, 0x04 12 | .equ FLASH_OPTKEY, 0x08 13 | .equ FLASH_CR, 0x10 14 | .equ FLASH_SR, 0x0C 15 | 16 | .equ OPTPG, 0x10 17 | 18 | .equ BSY, 0x01 19 | 20 | .syntax unified 21 | 22 | _start: 23 | @; Load FLASH controller base address 24 | ldr r2, =FLASHBASE 25 | 26 | @; Do unlocking sequence 27 | ldr r3, =KEY1 28 | str r3, [r2, #FLASH_KEY] 29 | ldr r3, =KEY2 30 | str r3, [r2, #FLASH_KEY] 31 | 32 | @; Same for option bytes 33 | ldr r3, =KEY1 34 | str r3, [r2, #FLASH_OPTKEY] 35 | ldr r3, =KEY2 36 | str r3, [r2, #FLASH_OPTKEY] 37 | 38 | @; Set OPTPG bit in FLASH_CR 39 | ldr r3, [r2, #FLASH_CR] 40 | orr r3, r3, #OPTPG 41 | str r3, [r2, #FLASH_CR] 42 | @; Write data at address 43 | strh r1, [r0] 44 | 45 | _wait: @; Wait for BSY bit to clear 46 | ldr r4, [r2, #FLASH_SR] 47 | mov r6, #BSY 48 | tst r4, r6 49 | bne _wait 50 | 51 | bkpt 52 | 53 | -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | { 2 | pkgs ? import (fetchGit { 3 | name = "nixos-21.11-2022-05-17"; 4 | url = "https://github.com/nixos/nixpkgs/"; 5 | ref = "refs/heads/nixos-21.11"; 6 | # `git ls-remote https://github.com/nixos/nixpkgs nixos-21.11` 7 | rev = "8b3398bc7587ebb79f93dfeea1b8c574d3c6dba1"; 8 | }) {} 9 | }: 10 | 11 | with pkgs; 12 | mkShell { 13 | buildInputs = [ 14 | gnumake 15 | gcc-arm-embedded 16 | dfu-util 17 | 18 | blackmagic 19 | pkg-config 20 | libftdi1 21 | libusb-compat-0_1 22 | hidapi 23 | 24 | (python3.withPackages (python-packages: with python-packages; [ 25 | pyusb 26 | pyserial 27 | ])) 28 | ]; 29 | } 30 | -------------------------------------------------------------------------------- /src/exception.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include "general.h" 22 | #include "exception.h" 23 | 24 | exception_s *innermost_exception = NULL; 25 | 26 | void raise_exception(const uint32_t type, const char *const msg) 27 | { 28 | for (exception_s *exception = innermost_exception; exception; exception = exception->outer) { 29 | if (exception->mask & type) { 30 | exception->type = type; 31 | exception->msg = msg; 32 | innermost_exception = exception->outer; 33 | longjmp(exception->jmpbuf, type); 34 | } 35 | } 36 | DEBUG_WARN("Unhandled exception: %s\n", msg); 37 | abort(); 38 | } 39 | -------------------------------------------------------------------------------- /src/gdb_hostio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2016 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef GDB_HOSTIO_H 22 | #define GDB_HOSTIO_H 23 | 24 | #include "target.h" 25 | 26 | int hostio_reply(target_controller_s *tc, char *packet, int len); 27 | 28 | /* Interface to host system calls */ 29 | int hostio_open(target_controller_s *, target_addr_t path, size_t path_len, target_open_flags_e flags, mode_t mode); 30 | int hostio_close(target_controller_s *, int fd); 31 | int hostio_read(target_controller_s *, int fd, target_addr_t buf, unsigned int count); 32 | int hostio_write(target_controller_s *, int fd, target_addr_t buf, unsigned int count); 33 | long hostio_lseek(target_controller_s *, int fd, long offset, target_seek_flag_e flag); 34 | int hostio_rename(target_controller_s *, target_addr_t oldpath, size_t old_len, target_addr_t newpath, size_t new_len); 35 | int hostio_unlink(target_controller_s *, target_addr_t path, size_t path_len); 36 | int hostio_stat(target_controller_s *, target_addr_t path, size_t path_len, target_addr_t buf); 37 | int hostio_fstat(target_controller_s *, int fd, target_addr_t buf); 38 | int hostio_gettimeofday(target_controller_s *, target_addr_t tv, target_addr_t tz); 39 | int hostio_isatty(target_controller_s *, int fd); 40 | int hostio_system(target_controller_s *, target_addr_t cmd, size_t cmd_len); 41 | 42 | #endif /* GDB_HOSTIO_H */ 43 | -------------------------------------------------------------------------------- /src/hex_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * Copyright (C) 2023 1BitSquared 7 | * Modified by Rachel Mant 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | /* Convenience functions to convert to/from ascii strings of hex digits. */ 24 | 25 | #include "general.h" 26 | #include "hex_utils.h" 27 | 28 | char hex_digit(const uint8_t value) 29 | { 30 | char digit = (char)value; 31 | if (value > 9U) 32 | digit += 'A' - '0' - 10U; 33 | digit += '0'; 34 | return digit; 35 | } 36 | 37 | char *hexify(char *const hex, const void *const buf, const size_t size) 38 | { 39 | char *dst = hex; 40 | const uint8_t *const src = buf; 41 | 42 | for (size_t idx = 0; idx < size; ++idx) { 43 | *dst++ = hex_digit(src[idx] >> 4U); 44 | *dst++ = hex_digit(src[idx] & 0xfU); 45 | } 46 | *dst = 0; 47 | 48 | return hex; 49 | } 50 | 51 | uint8_t unhex_digit(const char hex) 52 | { 53 | uint8_t tmp = hex - '0'; 54 | if (tmp > 9U) 55 | tmp -= 'A' - '0' - 10U; 56 | if (tmp > 16U) 57 | tmp -= 'a' - 'A'; 58 | return tmp; 59 | } 60 | 61 | char *unhexify(void *const buf, const char *hex, const size_t size) 62 | { 63 | uint8_t *const dst = buf; 64 | for (size_t idx = 0; idx < size; ++idx, hex += 2U) 65 | dst[idx] = (unhex_digit(hex[0]) << 4U) | unhex_digit(hex[1]); 66 | return buf; 67 | } 68 | -------------------------------------------------------------------------------- /src/include/command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_COMMAND_H 22 | #define INCLUDE_COMMAND_H 23 | 24 | #include 25 | 26 | #include "target.h" 27 | 28 | int command_process(target_s *t, char *cmd); 29 | 30 | /* 31 | * Attempts to parse a string as either being "enable" or "disable". 32 | * If the parse is successful, returns true and sets the out param to 33 | * indicate what was parsed. If not successful, emits a warning to the 34 | * gdb port, returns false and leaves out untouched. 35 | */ 36 | bool parse_enable_or_disable(const char *s, bool *out); 37 | 38 | #if PC_HOSTED == 1 39 | extern bool shutdown_bmda; 40 | #endif 41 | 42 | #endif /* INCLUDE_COMMAND_H */ 43 | -------------------------------------------------------------------------------- /src/include/crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_CRC32_H 22 | #define INCLUDE_CRC32_H 23 | 24 | bool generic_crc32(target_s *t, uint32_t *crc, uint32_t base, int len); 25 | 26 | #endif /* INCLUDE_CRC32_H */ 27 | -------------------------------------------------------------------------------- /src/include/exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /* Exception handling to escape deep nesting. 22 | * Used for the case of communication failure and timeouts. 23 | */ 24 | 25 | /* Example usage: 26 | * 27 | * volatile exception_s e; 28 | * TRY_CATCH (e, EXCEPTION_TIMEOUT) { 29 | * ... 30 | * raise_exception(EXCEPTION_TIMEOUT, "Timeout occurred"); 31 | * ... 32 | * } 33 | * if (e.type == EXCEPTION_TIMEOUT) { 34 | * printf("timeout: %s\n", e.msg); 35 | * } 36 | */ 37 | 38 | /* Limitations: 39 | * Can't use break, return, goto, etc from inside the TRY_CATCH block. 40 | */ 41 | 42 | #ifndef INCLUDE_EXCEPTION_H 43 | #define INCLUDE_EXCEPTION_H 44 | 45 | #include 46 | #include 47 | 48 | #define EXCEPTION_ERROR 0x01U 49 | #define EXCEPTION_TIMEOUT 0x02U 50 | #define EXCEPTION_ALL (-1) 51 | 52 | typedef struct exception exception_s; 53 | 54 | struct exception { 55 | uint32_t type; 56 | const char *msg; 57 | /* private */ 58 | uint32_t mask; 59 | jmp_buf jmpbuf; 60 | exception_s *outer; 61 | }; 62 | 63 | extern exception_s *innermost_exception; 64 | 65 | #define TRY_CATCH(e, type_mask) \ 66 | (e).type = 0; \ 67 | (e).mask = (type_mask); \ 68 | (e).outer = innermost_exception; \ 69 | innermost_exception = (void *)&(e); \ 70 | if (setjmp(innermost_exception->jmpbuf) == 0) \ 71 | for (; innermost_exception == &(e); innermost_exception = (e).outer) 72 | 73 | void raise_exception(uint32_t type, const char *msg); 74 | 75 | #endif /* INCLUDE_EXCEPTION_H */ 76 | -------------------------------------------------------------------------------- /src/include/gdb_if.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_GDB_IF_H 22 | #define INCLUDE_GDB_IF_H 23 | 24 | #if PC_HOSTED == 0 25 | #include 26 | void gdb_usb_out_cb(usbd_device *dev, uint8_t ep); 27 | #endif 28 | 29 | int gdb_if_init(void); 30 | char gdb_if_getchar(void); 31 | char gdb_if_getchar_to(uint32_t timeout); 32 | 33 | /* sending gdb_if_putchar(0, true) seems to work as keep alive */ 34 | void gdb_if_putchar(char c, int flush); 35 | 36 | #endif /* INCLUDE_GDB_IF_H */ 37 | -------------------------------------------------------------------------------- /src/include/gdb_main.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_GDB_MAIN_H 22 | #define INCLUDE_GDB_MAIN_H 23 | 24 | #include "target.h" 25 | 26 | extern bool gdb_target_running; 27 | extern target_s *cur_target; 28 | 29 | void gdb_poll_target(void); 30 | void gdb_main(char *pbuf, size_t pbuf_size, size_t size); 31 | char *gdb_packet_buffer(); 32 | 33 | #endif /* INCLUDE_GDB_MAIN_H */ 34 | -------------------------------------------------------------------------------- /src/include/gdb_packet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_GDB_PACKET_H 22 | #define INCLUDE_GDB_PACKET_H 23 | 24 | #include 25 | #include 26 | 27 | size_t gdb_getpacket(char *packet, size_t size); 28 | void gdb_putpacket(const char *packet, size_t size); 29 | void gdb_putpacket2(const char *packet1, size_t size1, const char *packet2, size_t size2); 30 | #define gdb_putpacketz(packet) gdb_putpacket((packet), strlen(packet)) 31 | void gdb_putpacket_f(const char *packet, ...); 32 | void gdb_put_notification(const char *packet, size_t size); 33 | #define gdb_put_notificationz(packet) gdb_put_notification((packet), strlen(packet)) 34 | 35 | void gdb_out(const char *buf); 36 | void gdb_voutf(const char *fmt, va_list); 37 | void gdb_outf(const char *fmt, ...); 38 | 39 | #endif /* INCLUDE_GDB_PACKET_H */ 40 | -------------------------------------------------------------------------------- /src/include/hex_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * Copyright (C) 2023 1BitSquared 7 | * Modified by Rachel Mant 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #ifndef INCLUDE_HEX_UTILS_H 24 | #define INCLUDE_HEX_UTILS_H 25 | 26 | #include 27 | #include 28 | 29 | char *hexify(char *hex, const void *buf, size_t size); 30 | char *unhexify(void *buf, const char *hex, size_t size); 31 | 32 | char hex_digit(uint8_t value); 33 | uint8_t unhex_digit(char hex); 34 | 35 | #endif /* INCLUDE_HEX_UTILS_H */ 36 | -------------------------------------------------------------------------------- /src/include/jtagtap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_JTAGTAP_H 22 | #define INCLUDE_JTAGTAP_H 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | typedef struct jtag_proc { 29 | /* Note: Signal names are as for the device under test. */ 30 | 31 | void (*jtagtap_reset)(void); 32 | 33 | /* 34 | * tap_next executes one state transition in the JTAG TAP state machine: 35 | * - Ensure TCK is low 36 | * - Assert the values of TMS and TDI 37 | * - Assert TCK (TMS and TDO are latched on rising edge 38 | * - Capture the value on TDO 39 | * - Release TCK. 40 | */ 41 | bool (*jtagtap_next)(const bool tms, const bool tdi); 42 | void (*jtagtap_tms_seq)(uint32_t tms_states, size_t clock_cycles); 43 | 44 | /* 45 | * Shift out a sequence on MS and DI, capture data to DO. 46 | * - This is not endian safe: First byte will always be first shifted out. 47 | * - DO may be NULL to ignore captured data. 48 | * - DO may be point to the same address as DI. 49 | */ 50 | void (*jtagtap_tdi_tdo_seq)(uint8_t *data_out, const bool final_tms, const uint8_t *data_in, size_t clock_cycles); 51 | void (*jtagtap_tdi_seq)(const bool final_tms, const uint8_t *data_in, size_t clock_cycles); 52 | void (*jtagtap_cycle)(const bool tms, const bool tdi, const size_t clock_cycles); 53 | 54 | /* 55 | * Some debug controllers such as the RISC-V debug controller use idle 56 | * cycles during operations as part of their function, while others 57 | * allow the desirable skipping of the entire state under some circumstances. 58 | */ 59 | uint8_t tap_idle_cycles; 60 | } jtag_proc_s; 61 | 62 | extern jtag_proc_s jtag_proc; 63 | 64 | /* generic soft reset: 1, 1, 1, 1, 1, 0 */ 65 | #define jtagtap_soft_reset() jtag_proc.jtagtap_tms_seq(0x1fU, 6) 66 | 67 | /* Goto Shift-IR: 1, 1, 0, 0 */ 68 | #define jtagtap_shift_ir() jtag_proc.jtagtap_tms_seq(0x03U, 4) 69 | 70 | /* Goto Shift-DR: 1, 0, 0 */ 71 | #define jtagtap_shift_dr() jtag_proc.jtagtap_tms_seq(0x01U, 3) 72 | 73 | /* Goto Run-test/Idle: 1, 1, 0 */ 74 | #define jtagtap_return_idle(cycles) jtag_proc.jtagtap_tms_seq(0x01, (cycles) + 1U) 75 | 76 | #if PC_HOSTED == 1 77 | bool platform_jtagtap_init(void); 78 | #else 79 | void jtagtap_init(void); 80 | #endif 81 | 82 | #endif /* INCLUDE_JTAGTAP_H */ 83 | -------------------------------------------------------------------------------- /src/include/morse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_MORSE_H 22 | #define INCLUDE_MORSE_H 23 | 24 | #include 25 | 26 | extern const char *morse_msg; 27 | 28 | void morse(const char *msg, bool repeat); 29 | bool morse_update(void); 30 | 31 | #endif /* INCLUDE_MORSE_H */ 32 | -------------------------------------------------------------------------------- /src/include/platform_support.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef INCLUDE_PLATFORM_SUPPORT_H 21 | #define INCLUDE_PLATFORM_SUPPORT_H 22 | 23 | #ifndef INCLUDE_GENERAL_H 24 | #error "Include 'general.h' instead" 25 | #endif 26 | 27 | #include "target.h" 28 | 29 | #if PC_HOSTED == 1 30 | void platform_init(int argc, char **argv); 31 | void platform_pace_poll(void); 32 | #else 33 | void platform_init(void); 34 | 35 | inline void platform_pace_poll(void) 36 | { 37 | } 38 | #endif 39 | 40 | typedef struct platform_timeout platform_timeout_s; 41 | void platform_timeout_set(platform_timeout_s *t, uint32_t ms); 42 | bool platform_timeout_is_expired(const platform_timeout_s *t); 43 | void platform_delay(uint32_t ms); 44 | 45 | #define POWER_CONFLICT_THRESHOLD 5U /* in 0.1V, so 5 stands for 0.5V */ 46 | 47 | extern bool connect_assert_nrst; 48 | uint32_t platform_target_voltage_sense(void); 49 | const char *platform_target_voltage(void); 50 | int platform_hwversion(void); 51 | void platform_nrst_set_val(bool assert); 52 | bool platform_nrst_get_val(void); 53 | bool platform_target_get_power(void); 54 | void platform_target_set_power(bool power); 55 | void platform_request_boot(void); 56 | void platform_max_frequency_set(uint32_t frequency); 57 | uint32_t platform_max_frequency_get(void); 58 | 59 | void platform_target_clk_output_enable(bool enable); 60 | 61 | #endif /* INCLUDE_PLATFORM_SUPPORT_H */ 62 | -------------------------------------------------------------------------------- /src/include/rtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2021 Koen De Vleeschauwer 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | #ifndef INCLUDE_RTT_H 28 | #define INCLUDE_RTT_H 29 | #include 30 | 31 | // MAX_RTT_CHAN can be set as low as 6. 32 | #define MAX_RTT_CHAN 16U 33 | 34 | extern char rtt_ident[16]; // string 35 | extern bool rtt_enabled; // rtt on/off 36 | extern bool rtt_found; // control block found 37 | extern uint32_t rtt_cbaddr; // control block address 38 | extern uint32_t rtt_num_up_chan; // number of 'up' channels 39 | extern uint32_t rtt_num_down_chan; // number of 'down' channels 40 | extern uint32_t rtt_min_poll_ms; // min time between polls (ms) 41 | extern uint32_t rtt_max_poll_ms; // max time between polls (ms) 42 | extern uint32_t rtt_max_poll_errs; // max number of errors before disconnect 43 | extern bool rtt_flag_ram; // limit ram scanned by rtt to range rtt_ram_start .. rtt_ram_end 44 | extern uint32_t rtt_ram_start; // if rtt_flag_ram set, lower limit of ram scanned by rtt 45 | extern uint32_t rtt_ram_end; // if rtt_flag_ram set, upper limit of ram scanned by rtt 46 | extern bool rtt_auto_channel; // manual or auto channel selection 47 | extern bool rtt_flag_skip; // skip if host-to-target fifo full 48 | extern bool rtt_flag_block; // block if host-to-target fifo full 49 | extern bool rtt_channel_enabled[MAX_RTT_CHAN]; // true if user wants to see channel 50 | 51 | typedef struct rtt_channel { 52 | uint32_t name_addr; 53 | uint32_t buf_addr; 54 | uint32_t buf_size; 55 | uint32_t head; 56 | uint32_t tail; 57 | uint32_t flag; 58 | } rtt_channel_s; 59 | 60 | extern rtt_channel_s rtt_channel[MAX_RTT_CHAN]; 61 | 62 | void poll_rtt(target_s *cur_target); 63 | 64 | #endif /* INCLUDE_RTT_H */ 65 | -------------------------------------------------------------------------------- /src/include/rtt_if.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2021 Koen De Vleeschauwer 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | #ifndef INCLUDE_RTT_IF_H 28 | #define INCLUDE_RTT_IF_H 29 | 30 | /* rtt i/o to terminal */ 31 | 32 | /* Define NO_LIBOPENCM3 either in "platform.h" or on the command line 33 | * in order to avoid including "libopencm3/usb/usbd.h". 34 | */ 35 | #include "platform.h" 36 | 37 | #if PC_HOSTED == 0 && !defined(NO_LIBOPENCM3) 38 | #include 39 | 40 | /* usb rx callback */ 41 | void rtt_serial_receive_callback(usbd_device *dev, uint8_t ep); 42 | #endif 43 | 44 | /* default buffer sizes, 8 bytes added to up buffer for alignment and padding */ 45 | /* override RTT_UP_BUF_SIZE and RTT_DOWN_BUF_SIZE in platform.h if needed */ 46 | 47 | #if !defined(RTT_UP_BUF_SIZE) || !defined(RTT_DOWN_BUF_SIZE) 48 | #if (PC_HOSTED == 1) 49 | #define RTT_UP_BUF_SIZE (4096U + 8U) 50 | #define RTT_DOWN_BUF_SIZE 512U 51 | #elif defined(STM32F7) 52 | #define RTT_UP_BUF_SIZE (4096U + 8U) 53 | #define RTT_DOWN_BUF_SIZE 2048U 54 | #elif defined(STM32F4) 55 | #define RTT_UP_BUF_SIZE (2048U + 8U) 56 | #define RTT_DOWN_BUF_SIZE 256U 57 | #else /* stm32f103 */ 58 | #define RTT_UP_BUF_SIZE (1024U + 8U) 59 | #define RTT_DOWN_BUF_SIZE 256U 60 | #endif 61 | #endif 62 | 63 | /* hosted initialisation */ 64 | int rtt_if_init(void); 65 | /* hosted teardown */ 66 | int rtt_if_exit(void); 67 | 68 | /* target to host: write len bytes from the buffer starting at buf. return number bytes written */ 69 | uint32_t rtt_write(const char *buf, uint32_t len); 70 | /* host to target: read one character, non-blocking. return character, -1 if no character */ 71 | int32_t rtt_getchar(); 72 | /* host to target: true if no characters available for reading */ 73 | bool rtt_nodata(); 74 | 75 | #endif /* INCLUDE_RTT_IF_H */ 76 | -------------------------------------------------------------------------------- /src/include/serialno.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_SERIALNO_H 22 | #define INCLUDE_SERIALNO_H 23 | 24 | #ifndef DFU_SERIAL_LENGTH 25 | #define DFU_SERIAL_LENGTH 26 | #endif 27 | 28 | extern char serial_no[DFU_SERIAL_LENGTH]; 29 | 30 | void read_serial_number(void); 31 | 32 | #endif /* INCLUDE_SERIALNO_H */ 33 | -------------------------------------------------------------------------------- /src/include/swd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_SWD_H 22 | #define INCLUDE_SWD_H 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | /* Functions interface talking SWD */ 29 | typedef struct swd_proc { 30 | /* Perform a clock_cycles read */ 31 | uint32_t (*seq_in)(size_t clock_cycles); 32 | /* Perform a clock_cycles read + parity */ 33 | bool (*seq_in_parity)(uint32_t *ret, size_t clock_cycles); 34 | /* Perform a clock_cycles write with the provided data */ 35 | void (*seq_out)(uint32_t tms_states, size_t clock_cycles); 36 | /* Perform a clock_cycles write + parity with the provided data */ 37 | void (*seq_out_parity)(uint32_t tms_states, size_t clock_cycles); 38 | } swd_proc_s; 39 | 40 | extern swd_proc_s swd_proc; 41 | 42 | void swdptap_init(void); 43 | 44 | #endif /*INCLUDE_SWD_H*/ 45 | -------------------------------------------------------------------------------- /src/include/timing.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2016 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef INCLUDE_TIMING_H 22 | #define INCLUDE_TIMING_H 23 | 24 | #include 25 | 26 | struct platform_timeout { 27 | uint32_t time; 28 | }; 29 | 30 | extern int32_t swj_delay_cnt; 31 | uint32_t platform_time_ms(void); 32 | 33 | #endif /* INCLUDE_TIMING_H */ 34 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /* Provides main entry point. Initialise subsystems and enter GDB protocol loop. */ 22 | 23 | #include "general.h" 24 | #include "gdb_if.h" 25 | #include "gdb_main.h" 26 | #include "target.h" 27 | #include "exception.h" 28 | #include "gdb_packet.h" 29 | #include "morse.h" 30 | #include "command.h" 31 | #ifdef ENABLE_RTT 32 | #include "rtt.h" 33 | #endif 34 | 35 | #define BUF_SIZE 1024U 36 | /* This has to be aligned so the remote protocol can re-use it without causing Problems */ 37 | static char pbuf[BUF_SIZE + 1U] __attribute__((aligned(8))); 38 | 39 | char *gdb_packet_buffer() 40 | { 41 | return pbuf; 42 | } 43 | 44 | static void bmp_poll_loop(void) 45 | { 46 | SET_IDLE_STATE(false); 47 | while (gdb_target_running && cur_target) { 48 | gdb_poll_target(); 49 | 50 | // Check again, as `gdb_poll_target()` may 51 | // alter these variables. 52 | if (!gdb_target_running || !cur_target) 53 | break; 54 | char c = gdb_if_getchar_to(0); 55 | if (c == '\x03' || c == '\x04') 56 | target_halt_request(cur_target); 57 | platform_pace_poll(); 58 | #ifdef ENABLE_RTT 59 | if (rtt_enabled) 60 | poll_rtt(cur_target); 61 | #endif 62 | } 63 | 64 | SET_IDLE_STATE(true); 65 | size_t size = gdb_getpacket(pbuf, BUF_SIZE); 66 | // If port closed and target detached, stay idle 67 | if (pbuf[0] != '\x04' || cur_target) 68 | SET_IDLE_STATE(false); 69 | gdb_main(pbuf, sizeof(pbuf), size); 70 | } 71 | 72 | int main(int argc, char **argv) 73 | { 74 | #if PC_HOSTED == 1 75 | platform_init(argc, argv); 76 | #else 77 | (void)argc; 78 | (void)argv; 79 | platform_init(); 80 | #endif 81 | 82 | while (true) { 83 | volatile exception_s e; 84 | TRY_CATCH (e, EXCEPTION_ALL) { 85 | bmp_poll_loop(); 86 | } 87 | if (e.type) { 88 | gdb_putpacketz("EFF"); 89 | target_list_free(); 90 | gdb_outf("Uncaught exception: %s\n", e.msg); 91 | morse("TARGET LOST.", true); 92 | } 93 | #if PC_HOSTED == 1 94 | if (shutdown_bmda) 95 | break; 96 | #endif 97 | } 98 | 99 | target_list_free(); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /src/platforms/96b_carbon/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ 6 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 7 | -DSTM32F4 -D_96B_CARBON -I../libopencm3/include \ 8 | -Iplatforms/stm32 -DDFU_SERIAL_LENGTH=9 9 | 10 | LDFLAGS = -lopencm3_stm32f4 -Wl,--defsym,_stack=0x20006000 \ 11 | -Wl,-T,platforms/stm32/96b_carbon.ld -nostartfiles -lc -lnosys \ 12 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ 13 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 14 | -L../libopencm3/lib 15 | 16 | VPATH += platforms/stm32 17 | 18 | SRC += \ 19 | traceswodecode.c \ 20 | traceswo.c \ 21 | serialno.c \ 22 | timing.c \ 23 | timing_stm32.c 24 | 25 | all: blackmagic.bin 26 | 27 | host_clean: 28 | -$(Q)$(RM) blackmagic.bin 29 | -------------------------------------------------------------------------------- /src/platforms/96b_carbon/README.md: -------------------------------------------------------------------------------- 1 | # 96b Carbon 2 | 3 | ## Connections 4 | 5 | The pinout for the programmer is concentrated within a single part of 6 | the Low Speed connector. The target control pins at the even pins 2 7 | through 16 with UART on LS-03 and LS-05. 8 | 9 | The pinout for the programmer allows a Carbon to program another Carbon 10 | (either the STM32 or the nRF51) with adjacent pins from LS-06 to LS-12. 11 | The order matches that of the SWD pins for easy hook up. 12 | 13 | ### JTAG/SWD 14 | 15 | * LS-02 (PB12): TDO/TRACESWO 16 | * LS-04 (PB15): TDI 17 | * LS-06 (PB14): TMS/SWDIO 18 | * LS-08 (PB13): TCK/SWCLK 19 | * LS-10 : GND 20 | * LS-12 : Vcc 21 | * LS-14 (PC3) : TRST (optional Test Reset) 22 | * LS-16 (PC5) : nRST (nRST / System Reset) 23 | 24 | ### LEDs 25 | 26 | * USR1 (green): Debug activity indicator 27 | * USR2 (green): UART activity indicator 28 | * BT (blue) : Error indicator 29 | 30 | ### UART 31 | 32 | * LS-03 (PA2): UART TX 33 | * LS-05 (PA3): UART RX 34 | 35 | ## How to Build 36 | 37 | cd blackmagic 38 | make clean 39 | make PROBE_HOST=96b_carbon 40 | 41 | ## Flashing using dfu-util 42 | 43 | Connect to the USB OTG socket on the Carbon and force the device into 44 | system bootloader mode by holding down the BOOT0 switch whilst pressing 45 | and releasing the RST switch. To program the device try: 46 | 47 | sudo dfu-util -d [0483:df11] -a 0 -D src/blackmagic.bin -s 0x08000000 48 | 49 | ## Self-programming 50 | 51 | A Carbon is capable of self-programming its own nRF51 by connecting two 52 | jumper wires from LS-06 to BLE_SWD-4 (DIO) and LS-08 to BLE_SWD-3 (CLK). 53 | 54 | +------------------------------------------------------------------+ 55 | | +-2--4--6--8-10-12-14-16-18-20-22-24-26-28-30-+ | 56 | | | . . .-+.-+. . . . . . . . . . . | | 57 | | | . . . |. |. . . . . . . . . . . | | 58 | | +-1--3--5-|7-|9-11-13-15-17-19-21-23-25-27-29-+ | 59 | | | | | 60 | | +--+-----------------------------+ | 61 | | | | | 62 | | +--------------------------+ | | 63 | | | | | 64 | | . . . . . | 65 | | . . . . . . . . . | 66 | +------------------------------------------------------------------+ 67 | -------------------------------------------------------------------------------- /src/platforms/96b_carbon/usbdfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include "general.h" 21 | #include "usbdfu.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | void dfu_detach(void) 29 | { 30 | /* USB device must detach, we just reset... */ 31 | scb_reset_system(); 32 | } 33 | 34 | int main(void) 35 | { 36 | /* Check the force bootloader pin*/ 37 | rcc_periph_enable_clock(RCC_GPIOA); 38 | if (!gpio_get(GPIOA, GPIO0)) 39 | dfu_jump_app_if_valid(); 40 | 41 | dfu_protect_enable(); 42 | 43 | /* Set up clock*/ 44 | rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); 45 | systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); 46 | systick_set_reload(2100000); 47 | 48 | systick_interrupt_enable(); 49 | systick_counter_enable(); 50 | 51 | /* Handle LEDs */ 52 | rcc_periph_enable_clock(RCC_GPIOD); 53 | gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); 54 | gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); 55 | 56 | /* Set up USB*/ 57 | rcc_periph_enable_clock(RCC_OTGFS); 58 | 59 | /* Set up USB Pins and alternate function*/ 60 | gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO10 | GPIO11 | GPIO12); 61 | gpio_set_af(GPIOA, GPIO_AF10, GPIO10 | GPIO11 | GPIO12); 62 | dfu_init(&stm32f107_usb_driver); 63 | 64 | dfu_main(); 65 | } 66 | 67 | void sys_tick_handler(void) 68 | { 69 | gpio_toggle(GPIOD, GPIO12); /* Green LED on/off */ 70 | } 71 | -------------------------------------------------------------------------------- /src/platforms/README.md: -------------------------------------------------------------------------------- 1 | # Platforms and platform support files 2 | 3 | This directory contains the implementation of platforms and support file 4 | used by (multiple) platforms. 5 | 6 | ## Implementation directories 7 | 8 | * native: Firmware for [Black Magic Probe](https://1bitsquared.com/products/black-magic-probe) 9 | * stlink: Firmware for ST-Link v2 and ST-Link v2.1 10 | * swlink: Firmware for ST-Link v1 and Bluepill 11 | * blackpillv2: Firmware for the WeAct Blackpill v2 12 | * hydrabus: Firmware for [hydrabus](https://hydrabus.com/) 13 | * f4discovery: Firmware for STM32F407DISCO 14 | * f3: Firmware for the STM32F3 15 | * f072: Firmware for the STM32F072 16 | * 96b_carbon: Firmware for [96Boards' Carbon](https://www.96boards.org/product/carbon/) 17 | * launchpad-icdi: Firmware for the TI Launchpad ICDI processor 18 | * hosted: Black Magic Debug App - able to talk to: 19 | * Black Magic Probe 20 | * ST-Link v2, v2.1, and v3 21 | * FTDI MPSSE probes 22 | * CMSIS-DAP probes and 23 | * JLink probes 24 | 25 | ## Support directories 26 | 27 | * common: common platform support for all platforms except hosted (BMDA) 28 | * stm32: STM32 specific libopencm3 common platform support 29 | * tm4c: Tiva-C specific libopencm3 common platform support 30 | -------------------------------------------------------------------------------- /src/platforms/blackpillv2/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | BMP_BOOTLOADER ?= 3 | CC = $(CROSS_COMPILE)gcc 4 | OBJCOPY = $(CROSS_COMPILE)objcopy 5 | 6 | CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ 7 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 8 | -DSTM32F4 -I../libopencm3/include \ 9 | -Iplatforms/stm32 10 | 11 | LINKER_SCRIPT=platforms/stm32/blackpillv2.ld 12 | 13 | LDFLAGS_BOOT = -lopencm3_stm32f4 \ 14 | -Wl,-T,$(LINKER_SCRIPT) -nostartfiles -lc -lnosys \ 15 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ 16 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 17 | -L../libopencm3/lib 18 | 19 | ifeq ($(BMP_BOOTLOADER), 1) 20 | $(info Load address 0x08004000 for BMPBootloader) 21 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000 22 | CFLAGS += -DDFU_SERIAL_LENGTH=9 23 | else 24 | LDFLAGS += $(LDFLAGS_BOOT) 25 | CFLAGS += -DDFU_SERIAL_LENGTH=13 26 | endif 27 | 28 | VPATH += platforms/stm32 29 | 30 | SRC += \ 31 | traceswodecode.c \ 32 | traceswo.c \ 33 | serialno.c \ 34 | timing.c \ 35 | timing_stm32.c \ 36 | 37 | ifneq ($(BMP_BOOTLOADER), 1) 38 | all: blackmagic.bin 39 | else 40 | all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 41 | blackmagic_dfu: usbdfu.o dfucore.o dfu_f4.o serialno.o 42 | $(CC) $^ -o $@ $(LDFLAGS_BOOT) 43 | 44 | blackmagic_dfu.bin: blackmagic_dfu 45 | $(OBJCOPY) -O binary $^ $@ 46 | 47 | blackmagic_dfu.hex: blackmagic_dfu 48 | $(OBJCOPY) -O ihex $^ $@ 49 | endif 50 | host_clean: 51 | -$(Q)$(RM) blackmagic.bin 52 | -------------------------------------------------------------------------------- /src/platforms/blackpillv2/README.md: -------------------------------------------------------------------------------- 1 | # Firmware BMP for STM32F401/STM32F411 MiniF4 aka BlackPill v2 boards 2 | 3 | Allows the use of [BlackPill v2](https://github.com/WeActStudio/WeActStudio.MiniSTM32F4x1) as a Black Magic Probe 4 | 5 | ## Connections 6 | 7 | * JTAG/SWD 8 | * PA1: TDI 9 | * PA13: TMS/SWDIO 10 | * PA14: TCK/SWCLK 11 | * PB3: TDO/TRACESWO 12 | * PB5: TRST 13 | * PB4: nRST 14 | 15 | * USB USART 16 | * PB6: USART1 TX (usbuart_xxx) 17 | * PB7: USART1 RX (usbuart_xxx) 18 | 19 | * +3V3. 20 | * PB8 - turn on IRLML5103 transistor 21 | 22 | ## How to Build 23 | 24 | ```sh 25 | cd blackmagic 26 | make clean 27 | make PROBE_HOST=blackpillv2 28 | ``` 29 | 30 | ## How to Flash with dfu 31 | 32 | After building the firmware as above: 33 | 34 | * 1) `apt install dfu-util` 35 | * 2) Force the F4 into system bootloader mode by keeping BOOT0 button pressed while pressing and releasing nRST 36 | button. The board should re-enumerate as the bootloader. 37 | * 3) `dfu-util -a 0 --dfuse-address 0x08000000 -D blackmagic.bin` 38 | 39 | To exit from dfu mode just press and release nRST. The newly Flashed BMP firmware should boot and enumerate. 40 | 41 | ## 10 pin male from pins 42 | 43 | | PB3/TDO | PB7/RX | PB6/TX | X | PA1/TDI | 44 | | -------- | ----------- | ---------- | ---------- | ------- | 45 | | PB4/nRST | +3V3/PB8 SW | PA13/SWDIO | PA14/SWCLK | GND | 46 | 47 | ## SWD/JTAG frequency setting 48 | 49 | https://github.com/blackmagic-debug/blackmagic/pull/783#issue-529197718 50 | 51 | `mon freq 900k` helps at most 52 | -------------------------------------------------------------------------------- /src/platforms/blackpillv2/usbdfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "usbdfu.h" 27 | #include "general.h" 28 | #include "platform.h" 29 | 30 | uintptr_t app_address = 0x08004000U; 31 | extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) 32 | 33 | void dfu_detach(void) 34 | { 35 | scb_reset_system(); 36 | } 37 | 38 | #pragma GCC diagnostic push 39 | #pragma GCC diagnostic ignored "-Warray-bounds" 40 | 41 | int main(void) 42 | { 43 | volatile uint32_t *magic = (uint32_t *)&_ebss; 44 | rcc_periph_clock_enable(RCC_GPIOA); 45 | 46 | #pragma GCC diagnostic push 47 | #pragma GCC diagnostic ignored "-Warray-bounds" 48 | if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { 49 | magic[0] = 0; 50 | magic[1] = 0; 51 | } else 52 | dfu_jump_app_if_valid(); 53 | #pragma GCC diagnostic pop 54 | 55 | rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); 56 | 57 | /* Assert blue LED as indicator we are in the bootloader */ 58 | rcc_periph_clock_enable(RCC_GPIOD); 59 | gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER); 60 | gpio_set(LED_PORT, LED_BOOTLOADER); 61 | 62 | /* Enable peripherals */ 63 | rcc_periph_clock_enable(RCC_OTGFS); 64 | 65 | /* Set up USB Pins and alternate function*/ 66 | gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); 67 | gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); 68 | 69 | dfu_protect(false); 70 | dfu_init(&USB_DRIVER); 71 | dfu_main(); 72 | } 73 | 74 | #pragma GCC diagnostic pop 75 | 76 | void dfu_event(void) 77 | { 78 | } 79 | -------------------------------------------------------------------------------- /src/platforms/common/Makefile.inc: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the Black Magic Debug project. 3 | # 4 | # Copyright (C) 2022 1BitSquared 5 | # Written by Rachel Mant 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | # 20 | 21 | VPATH += platforms/common 22 | CFLAGS += -Iplatforms/common 23 | 24 | SRC += \ 25 | jtagtap.c \ 26 | swdptap.c \ 27 | usb.c \ 28 | usb_serial.c \ 29 | usb_dfu_stub.c \ 30 | aux_serial.c 31 | -------------------------------------------------------------------------------- /src/platforms/common/aux_serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #ifndef PLATFORMS_COMMON_AUX_SERIAL_H 21 | #define PLATFORMS_COMMON_AUX_SERIAL_H 22 | 23 | #include 24 | #include 25 | #include 26 | #include "usb_types.h" 27 | 28 | #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) 29 | /* XXX: Does the st_usbfs_v2_usb_driver work on F3 with 128 byte buffers? */ 30 | #if defined(STM32F1) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) 31 | #define USART_DMA_BUF_SHIFT 7U 32 | #elif defined(STM32F0) 33 | /* The st_usbfs_v2_usb_driver only works with up to 64-byte buffers on the F0 parts */ 34 | #define USART_DMA_BUF_SHIFT 6U 35 | #endif 36 | 37 | #define USART_DMA_BUF_SIZE (1U << USART_DMA_BUF_SHIFT) 38 | #define AUX_UART_BUFFER_SIZE (USART_DMA_BUF_SIZE) 39 | #elif defined(LM4F) 40 | #define AUX_UART_BUFFER_SIZE 128U 41 | #endif 42 | 43 | void aux_serial_init(void); 44 | void aux_serial_set_encoding(usb_cdc_line_coding_s *coding); 45 | 46 | #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) 47 | typedef enum aux_serial_led { 48 | AUX_SERIAL_LED_TX = (1U << 0U), 49 | AUX_SERIAL_LED_RX = (1U << 1U) 50 | } aux_serial_led_e; 51 | 52 | void aux_serial_set_led(aux_serial_led_e led); 53 | void aux_serial_clear_led(aux_serial_led_e led); 54 | 55 | void aux_serial_switch_transmit_buffers(void); 56 | #endif 57 | 58 | /* Get the current transmit buffer to stage data into */ 59 | char *aux_serial_current_transmit_buffer(void); 60 | /* Get how full the current transmit buffer is */ 61 | size_t aux_serial_transmit_buffer_fullness(void); 62 | /* Send a number of bytes staged into the current transmit buffer */ 63 | void aux_serial_send(size_t len); 64 | 65 | #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) 66 | void aux_serial_update_receive_buffer_fullness(void); 67 | bool aux_serial_receive_buffer_empty(void); 68 | void aux_serial_drain_receive_buffer(void); 69 | #ifdef ENABLE_DEBUG 70 | void aux_serial_stage_debug_buffer(void); 71 | #endif 72 | void aux_serial_stage_receive_buffer(void); 73 | #endif 74 | 75 | #endif /* PLATFORMS_COMMON_AUX_SERIAL_H */ 76 | -------------------------------------------------------------------------------- /src/platforms/common/traceswo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2012 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_COMMON_TRACESWO_H 22 | #define PLATFORMS_COMMON_TRACESWO_H 23 | 24 | #include 25 | 26 | #if defined TRACESWO_PROTOCOL && TRACESWO_PROTOCOL == 2 27 | /* Default line rate, used as default for a request without baudrate */ 28 | #define SWO_DEFAULT_BAUD 2250000U 29 | void traceswo_init(uint32_t baudrate, uint32_t swo_chan_bitmask); 30 | #else 31 | void traceswo_init(uint32_t swo_chan_bitmask); 32 | #endif 33 | 34 | void trace_buf_drain(usbd_device *dev, uint8_t ep); 35 | 36 | /* Set bitmask of SWO channels to be decoded */ 37 | void traceswo_setmask(uint32_t mask); 38 | 39 | /* Print decoded SWO packet on USB serial */ 40 | uint16_t traceswo_decode(usbd_device *usbd_dev, uint8_t addr, const void *buf, uint16_t len); 41 | 42 | #endif /* PLATFORMS_COMMON_TRACESWO_H */ 43 | -------------------------------------------------------------------------------- /src/platforms/common/usb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include "general.h" 24 | #include "usb.h" 25 | #include "usb_descriptors.h" 26 | #include "usb_serial.h" 27 | #include "usb_dfu_stub.h" 28 | #include "serialno.h" 29 | 30 | usbd_device *usbdev = NULL; 31 | uint16_t usb_config; 32 | 33 | /* We need a special large control buffer for this device: */ 34 | static uint8_t usbd_control_buffer[256]; 35 | 36 | void blackmagic_usb_init(void) 37 | { 38 | read_serial_number(); 39 | 40 | usbdev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, sizeof(usb_strings) / sizeof(char *), 41 | usbd_control_buffer, sizeof(usbd_control_buffer)); 42 | 43 | usbd_register_set_config_callback(usbdev, usb_serial_set_config); 44 | usbd_register_set_config_callback(usbdev, dfu_set_config); 45 | 46 | nvic_set_priority(USB_IRQ, IRQ_PRI_USB); 47 | nvic_enable_irq(USB_IRQ); 48 | } 49 | 50 | uint16_t usb_get_config(void) 51 | { 52 | return usb_config; 53 | } 54 | 55 | void USB_ISR(void) 56 | { 57 | usbd_poll(usbdev); 58 | } 59 | -------------------------------------------------------------------------------- /src/platforms/common/usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_COMMON_USB_H 22 | #define PLATFORMS_COMMON_USB_H 23 | 24 | #include 25 | #include 26 | 27 | extern usbd_device *usbdev; 28 | extern uint16_t usb_config; 29 | 30 | #if defined(USB_HS) 31 | #define CDCACM_PACKET_SIZE 512U 32 | #define TRACE_ENDPOINT_SIZE 512U 33 | #else 34 | #define CDCACM_PACKET_SIZE 64U 35 | #define TRACE_ENDPOINT_SIZE 64U 36 | #endif 37 | 38 | #if !defined(USB_MAX_INTERVAL) 39 | #define USB_MAX_INTERVAL 255U 40 | #endif 41 | 42 | #define CDCACM_GDB_ENDPOINT 1U 43 | #define CDCACM_UART_ENDPOINT 3U 44 | #define TRACE_ENDPOINT 5U 45 | 46 | #define GDB_IF_NO 0U 47 | #define UART_IF_NO 2U 48 | #define DFU_IF_NO 4U 49 | #ifdef PLATFORM_HAS_TRACESWO 50 | #define TRACE_IF_NO 5U 51 | #define TOTAL_INTERFACES 6U 52 | #else 53 | #define TOTAL_INTERFACES 5U 54 | #endif 55 | 56 | void blackmagic_usb_init(void); 57 | 58 | /* Returns current usb configuration, or 0 if not configured. */ 59 | uint16_t usb_get_config(void); 60 | 61 | #endif /* PLATFORMS_COMMON_USB_H */ 62 | -------------------------------------------------------------------------------- /src/platforms/common/usb_dfu_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "general.h" 26 | #include "usb_dfu_stub.h" 27 | #include "usb_types.h" 28 | 29 | static void dfu_detach_complete(usbd_device *const dev, usb_setup_data_s *const req) 30 | { 31 | (void)dev; 32 | (void)req; 33 | platform_request_boot(); 34 | 35 | /* Reset core to enter bootloader */ 36 | #if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) 37 | scb_reset_core(); 38 | #endif 39 | } 40 | 41 | static usbd_request_return_codes_e dfu_control_request(usbd_device *const dev, usb_setup_data_s *req, uint8_t **buf, 42 | uint16_t *len, void (**complete)(usbd_device *dev, usb_setup_data_s *req)) 43 | { 44 | (void)dev; 45 | /* Is the request for the DFU interface? */ 46 | if (req->wIndex != DFU_IF_NO) 47 | return USBD_REQ_NEXT_CALLBACK; 48 | 49 | switch (req->bRequest) { 50 | case DFU_GETSTATUS: 51 | (*buf)[0] = DFU_STATUS_OK; 52 | (*buf)[1] = 0; 53 | (*buf)[2] = 0; 54 | (*buf)[3] = 0; 55 | (*buf)[4] = STATE_APP_IDLE; 56 | (*buf)[5] = 0; /* iString not used here */ 57 | *len = 6; 58 | 59 | return USBD_REQ_HANDLED; 60 | case DFU_DETACH: 61 | *complete = dfu_detach_complete; 62 | return USBD_REQ_HANDLED; 63 | } 64 | /* If the request isn't one of the two above, we don't care as this is a DFU stub. */ 65 | return USBD_REQ_NOTSUPP; 66 | } 67 | 68 | void dfu_set_config(usbd_device *const dev, const uint16_t value) 69 | { 70 | (void)value; 71 | usbd_register_control_callback(dev, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, 72 | USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, dfu_control_request); 73 | } 74 | -------------------------------------------------------------------------------- /src/platforms/common/usb_dfu_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_COMMON_USB_DFU_STUB_H 22 | #define PLATFORMS_COMMON_USB_DFU_STUB_H 23 | 24 | #include "usb.h" 25 | 26 | void dfu_set_config(usbd_device *dev, uint16_t value); 27 | 28 | #endif /* PLATFORMS_COMMON_USB_DFU_STUB_H */ 29 | -------------------------------------------------------------------------------- /src/platforms/common/usb_serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_COMMON_USB_SERIAL_H 22 | #define PLATFORMS_COMMON_USB_SERIAL_H 23 | 24 | #include 25 | #include 26 | #include "usb.h" 27 | 28 | void usb_serial_set_config(usbd_device *dev, uint16_t value); 29 | 30 | bool gdb_serial_get_dtr(void); 31 | 32 | void debug_serial_run(void); 33 | uint32_t debug_serial_fifo_send(const char *fifo, uint32_t fifo_begin, uint32_t fifo_end); 34 | 35 | #endif /* PLATFORMS_COMMON_USB_SERIAL_H */ 36 | -------------------------------------------------------------------------------- /src/platforms/common/usb_types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_COMMON_USB_TYPES_H 22 | #define PLATFORMS_COMMON_USB_TYPES_H 23 | 24 | typedef struct usb_device_descriptor usb_device_descriptor_s; 25 | typedef struct usb_config_descriptor usb_config_descriptor_s; 26 | typedef struct usb_interface_descriptor usb_interface_descriptor_s; 27 | typedef struct usb_endpoint_descriptor usb_endpoint_descriptor_s; 28 | typedef struct usb_iface_assoc_descriptor usb_iface_assoc_descriptor_s; 29 | typedef struct usb_interface usb_interface_s; 30 | 31 | typedef enum usbd_request_return_codes usbd_request_return_codes_e; 32 | typedef struct usb_setup_data usb_setup_data_s; 33 | 34 | typedef struct usb_cdc_header_descriptor usb_cdc_header_descriptor_s; 35 | typedef struct usb_cdc_call_management_descriptor usb_cdc_call_management_descriptor_s; 36 | typedef struct usb_cdc_acm_descriptor usb_cdc_acm_descriptor_s; 37 | typedef struct usb_cdc_union_descriptor usb_cdc_union_descriptor_s; 38 | 39 | typedef struct usb_cdc_line_coding usb_cdc_line_coding_s; 40 | typedef struct usb_cdc_notification usb_cdc_notification_s; 41 | 42 | typedef struct usb_dfu_descriptor usb_dfu_descriptor_s; 43 | 44 | typedef enum dfu_state dfu_state_e; 45 | 46 | #endif /* PLATFORMS_COMMON_USB_TYPES_H */ 47 | -------------------------------------------------------------------------------- /src/platforms/f072/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -Istm32/include -mcpu=cortex-m0 -mthumb \ 6 | -DSTM32F0 -I../libopencm3/include \ 7 | -DDFU_SERIAL_LENGTH=13 -Iplatforms/stm32 8 | 9 | LDFLAGS = --specs=nano.specs -lopencm3_stm32f0 \ 10 | -Wl,-T,platforms/stm32/stm32f07xzb.ld \ 11 | -nostartfiles -lc -lnosys -Wl,-Map=mapfile -mthumb \ 12 | -mcpu=cortex-m0 -Wl,-gc-sections -L../libopencm3/lib 13 | 14 | VPATH += platforms/stm32 15 | 16 | SRC += \ 17 | traceswodecode.c \ 18 | traceswo.c \ 19 | serialno.c \ 20 | timing.c \ 21 | timing_stm32.c \ 22 | 23 | all: blackmagic.bin 24 | blackmagic.elf: libopencm3_stm32f0 25 | 26 | libopencm3_stm32f0: 27 | $(Q)$(MAKE) $(MFLAGS) -C ../libopencm3 lib/stm32/f0 28 | 29 | host_clean: 30 | -$(Q)$(RM) blackmagic.bin 31 | 32 | .PHONY: libopencm3_stm32f0 33 | -------------------------------------------------------------------------------- /src/platforms/f072/README.md: -------------------------------------------------------------------------------- 1 | # BMP firmware for STM32F072 2 | 3 | ## System vs BMP Bootloader 4 | 5 | For the BMP bootloader, flashing was not reliable. As an easy workaround the 6 | system bootloader is used. This gives additional 4 kB for the BMP firmware. 7 | 8 | ## Connections 9 | 10 | * PA2: UART RX 11 | * PS3: UART TX 12 | * PA0: TDI 13 | * PA1: TMS/SWDIO 14 | * PA7: TCK/SWCLK 15 | * PA6: TDO/TRACESWO 16 | * PA5: TRST 17 | * PB5: LED green 18 | * PB6: LED yellow 19 | * PB7: LED red 20 | * PB0: VTARGET 21 | * PB1: VUSB 22 | 23 | ## Loading/updating BMP firmware 24 | 25 | Get into ST bootloader mode with reset or repower and BOOT pulled high. If BMP firmware is already loaded and running, dfu-util can also invoke the bootloader. 26 | 27 | ``` 28 | dfu-util -d 1d50:6018 -e 29 | ``` 30 | 31 | List the available devices 32 | 33 | ``` 34 | dfu-util -l 35 | ``` 36 | 37 | dfu-util should now list "[0483:df11]" and "@Internal Flash /0x08000000/064*0002Kg".Compilethe firmware with: 38 | 39 | ``` 40 | make PROBE_HOST=f072 clean && make PROBE_HOST=f072 41 | ``` 42 | 43 | Load firmware: 44 | 45 | ``` 46 | dfu-util -d 0483:df11 -a 0 -s 0x08000000:leave -D blackmagic.bin 47 | ``` 48 | 49 | Multiple BMP devices or STM devices on the USB bus may require additional dfu-util arguments for device selection. 50 | -------------------------------------------------------------------------------- /src/platforms/f3/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ 6 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 7 | -DSTM32F3 -I../libopencm3/include \ 8 | -DDFU_SERIAL_LENGTH=13 -Iplatforms/stm32 9 | 10 | LDFLAGS = --specs=nano.specs -lopencm3_stm32f3 \ 11 | -Wl,-T,platforms/stm32/stm32f303xc.ld -nostartfiles -lc -lnosys \ 12 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ 13 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 14 | -L../libopencm3/lib 15 | 16 | VPATH += platforms/stm32 17 | 18 | SRC += \ 19 | traceswodecode.c \ 20 | traceswo.c \ 21 | serialno.c \ 22 | timing.c \ 23 | timing_stm32.c \ 24 | 25 | all: blackmagic.bin 26 | blackmagic.elf: libopencm3_stm32f3 27 | 28 | libopencm3_stm32f3: 29 | $(Q)$(MAKE) $(MFLAGS) -C ../libopencm3 lib/stm32/f3 30 | 31 | host_clean: 32 | -$(Q)$(RM) blackmagic.bin 33 | 34 | .PHONY: libopencm3_stm32f3 35 | -------------------------------------------------------------------------------- /src/platforms/f3/README.md: -------------------------------------------------------------------------------- 1 | # BMP firmware for STM32F303 2 | 3 | ## System vs BMP Bootloader 4 | 5 | For simpicity, the ST system bootloader is used. This saves additional 4 kB for the BMP firmware. 6 | 7 | ## Connections 8 | 9 | * PA2: UART RX 10 | * PS3: UART TX 11 | * PA0: TDI 12 | * PA1: TMS/SWDIO 13 | * PA7: TCK/SWCLK 14 | * PA6: TDO/TRACESWO 15 | * PA5: TRST 16 | * PB5: LED green 17 | * PB6: LED yellow 18 | * PB7: LED red 19 | * PB0: VTARGET 20 | * PB1: VUSB 21 | 22 | ## Loading/updating BMP firmware 23 | 24 | Get into ST bootloader mode with reset or repower and BOOT pulled high. If BMP firmware is already loaded and running, dfu-util can also invoke the bootloader. 25 | 26 | ``` 27 | dfu-util -d 1d50:6018 -e 28 | ``` 29 | 30 | List the available devices 31 | 32 | ``` 33 | dfu-util -l 34 | ``` 35 | 36 | dfu-util should now list "[0483:df11]" "@Internal Flash /0x08000000/064*0002Kg" 37 | . Compile the firmware with 38 | 39 | ``` 40 | make PROBE_HOST=f3 clean && make PROBE_HOST=f3 41 | ``` 42 | 43 | Load firmware 44 | 45 | ``` 46 | dfu-util -d 0483:df11 -a 0 -s 0x08000000:leave -D blackmagic.bin 47 | 48 | ``` 49 | 50 | Multiple BMP devices or STM devices on the USB bus may require additional dfu-util arguments for device selection. 51 | -------------------------------------------------------------------------------- /src/platforms/f4discovery/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | BMP_BOOTLOADER ?= 3 | CC = $(CROSS_COMPILE)gcc 4 | OBJCOPY = $(CROSS_COMPILE)objcopy 5 | 6 | CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ 7 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 8 | -DSTM32F4 -I../libopencm3/include \ 9 | -Iplatforms/stm32 10 | 11 | LINKER_SCRIPT=platforms/stm32/f4discovery.ld 12 | 13 | LDFLAGS_BOOT = -lopencm3_stm32f4 \ 14 | -Wl,-T,$(LINKER_SCRIPT) -nostartfiles -lc -lnosys \ 15 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ 16 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 17 | -L../libopencm3/lib 18 | 19 | ifeq ($(BMP_BOOTLOADER), 1) 20 | $(info Load address 0x08004000 for BMPBootloader) 21 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000 22 | CFLAGS += -DDFU_SERIAL_LENGTH=9 23 | else 24 | LDFLAGS += $(LDFLAGS_BOOT) 25 | CFLAGS += -DDFU_SERIAL_LENGTH=13 26 | endif 27 | 28 | VPATH += platforms/stm32 29 | 30 | SRC += \ 31 | traceswodecode.c \ 32 | traceswo.c \ 33 | serialno.c \ 34 | timing.c \ 35 | timing_stm32.c \ 36 | 37 | ifneq ($(BMP_BOOTLOADER), 1) 38 | all: blackmagic.bin 39 | else 40 | all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 41 | blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f4.o serialno.o 42 | @echo " LD $@" 43 | $(Q)$(CC) $^ -o $@ $(LDFLAGS_BOOT) 44 | endif 45 | 46 | host_clean: 47 | -$(Q)$(RM) blackmagic.bin 48 | -------------------------------------------------------------------------------- /src/platforms/f4discovery/README.md: -------------------------------------------------------------------------------- 1 | # Firmware BMP for STM32F407 DISCO boards 2 | 3 | Allows the use of the STM32F407 Discovery board main cpu as a Black Magic Probe. Historically it was used to program the on board built in debugger before ST-Link bootloader use was possible. 4 | 5 | ## Connections 6 | 7 | * PC2: TDI 8 | * PC4: TMS/SWDIO 9 | * PC5: TCK/SWCLK 10 | * PC6: TDO/TRACESWO 11 | * PC1: TRST 12 | * PC8: nRST 13 | 14 | ## How to Flash with DFU 15 | 16 | After build: 17 | 18 | 1) `apt install dfu-util` 19 | 2) Force the F4 into system bootloader mode by jumpering "BOOT0" to "3V3" and "PB2/BOOT1" to 20 | "GND" and reset (RESET button). System bootloader should appear. 21 | 3) `dfu-util -a 0 --dfuse-address 0x08000000 -D blackmagic.bin` 22 | 23 | To exit from DFU mode press a "key" and "reset", release reset. BMP firmware should appear 24 | 25 | ## 10 pin male from pins 26 | 27 | | PB3/TDO | PB7/RX | PB6/TX | X | PA1/TDI | 28 | | -------- | ----------- | ---------- | ---------- | ------- | 29 | | PB4/nRST | +3V3/PB8 SW | PA13/SWDIO | PA14/SWCLK | GND | 30 | 31 | ## SWD/JTAG frequency setting 32 | 33 | https://github.com/blackmagic-debug/blackmagic/pull/783#issue-529197718 34 | 35 | `mon freq 900k` helps at most 36 | -------------------------------------------------------------------------------- /src/platforms/f4discovery/usbdfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "usbdfu.h" 27 | #include "general.h" 28 | #include "platform.h" 29 | 30 | uintptr_t app_address = 0x08004000U; 31 | extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) 32 | 33 | void dfu_detach(void) 34 | { 35 | scb_reset_system(); 36 | } 37 | 38 | int main(void) 39 | { 40 | volatile uint32_t *magic = &_ebss; 41 | rcc_periph_clock_enable(RCC_GPIOA); 42 | 43 | #pragma GCC diagnostic push 44 | #pragma GCC diagnostic ignored "-Warray-bounds" 45 | if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { 46 | magic[0] = 0; 47 | magic[1] = 0; 48 | } else 49 | dfu_jump_app_if_valid(); 50 | #pragma GCC diagnostic pop 51 | 52 | rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); 53 | 54 | /* Assert blue LED as indicator we are in the bootloader */ 55 | rcc_periph_clock_enable(RCC_GPIOD); 56 | gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER); 57 | gpio_set(LED_PORT, LED_BOOTLOADER); 58 | 59 | /* Enable peripherals */ 60 | rcc_periph_clock_enable(RCC_OTGFS); 61 | 62 | /* Set up USB Pins and alternate function*/ 63 | gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); 64 | gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); 65 | 66 | dfu_protect(false); 67 | dfu_init(&USB_DRIVER); 68 | dfu_main(); 69 | } 70 | 71 | void dfu_event(void) 72 | { 73 | } 74 | -------------------------------------------------------------------------------- /src/platforms/hosted/bmp_remote.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2020 Uwe Bonnes 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_HOSTED_BMP_REMOTE_H 21 | #define PLATFORMS_HOSTED_BMP_REMOTE_H 22 | 23 | #include "jtagtap.h" 24 | #include "adiv5.h" 25 | #include "target.h" 26 | #include "target_internal.h" 27 | 28 | #define REMOTE_MAX_MSG_SIZE 1024U 29 | 30 | bool platform_buffer_write(const void *data, size_t size); 31 | int platform_buffer_read(void *data, size_t size); 32 | 33 | int remote_init(const bool power_up); 34 | bool remote_swdptap_init(void); 35 | bool remote_jtagtap_init(void); 36 | bool remote_target_get_power(void); 37 | const char *remote_target_voltage(void); 38 | bool remote_target_set_power(bool power); 39 | void remote_nrst_set_val(bool assert); 40 | bool remote_nrst_get_val(void); 41 | void remote_max_frequency_set(uint32_t freq); 42 | uint32_t remote_max_frequency_get(void); 43 | void remote_target_clk_output_enable(bool enable); 44 | 45 | void remote_adiv5_dp_defaults(adiv5_debug_port_s *dp); 46 | void remote_add_jtag_dev(uint32_t i, const jtag_dev_s *jtag_dev); 47 | 48 | #endif /* PLATFORMS_HOSTED_BMP_REMOTE_H */ 49 | -------------------------------------------------------------------------------- /src/platforms/hosted/cli.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2019 - 2021 Uwe Bonnes 5 | * Written by Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de) 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /* This file implements the interface to command line command for PC-Hosted 22 | * platforms. 23 | */ 24 | #ifndef PLATFORMS_HOSTED_CLI_H 25 | #define PLATFORMS_HOSTED_CLI_H 26 | 27 | #include "cortexm.h" 28 | 29 | typedef enum bmda_cli_mode { 30 | BMP_MODE_DEBUG, 31 | BMP_MODE_TEST, 32 | BMP_MODE_RESET, 33 | BMP_MODE_RESET_HW, 34 | BMP_MODE_FLASH_ERASE, 35 | BMP_MODE_FLASH_WRITE, 36 | BMP_MODE_FLASH_WRITE_VERIFY, 37 | BMP_MODE_FLASH_READ, 38 | BMP_MODE_FLASH_VERIFY, 39 | BMP_MODE_SWJ_TEST, 40 | BMP_MODE_MONITOR, 41 | } bmda_cli_mode_e; 42 | 43 | typedef enum bmp_scan_mode { 44 | BMP_SCAN_JTAG, 45 | BMP_SCAN_SWD, 46 | BMP_SCAN_AUTO 47 | } bmp_scan_mode_e; 48 | 49 | typedef struct bmda_cli_options { 50 | bmda_cli_mode_e opt_mode; 51 | bmp_scan_mode_e opt_scanmode; 52 | bool opt_tpwr; 53 | bool opt_list_only; 54 | bool opt_connect_under_reset; 55 | bool external_resistor_swd; 56 | bool fast_poll; 57 | bool opt_no_hl; 58 | char *opt_flash_file; 59 | char *opt_device; 60 | char *opt_serial; 61 | uint32_t opt_targetid; 62 | char *opt_ident_string; 63 | size_t opt_position; 64 | char *opt_cable; 65 | char *opt_monitor; 66 | int opt_debuglevel; 67 | int opt_target_dev; 68 | uint32_t opt_flash_start; 69 | uint32_t opt_max_swj_frequency; 70 | size_t opt_flash_size; 71 | } bmda_cli_options_s; 72 | 73 | void cl_init(bmda_cli_options_s *opt, int argc, char **argv); 74 | int cl_execute(bmda_cli_options_s *opt); 75 | int serial_open(const bmda_cli_options_s *opt, const char *serial); 76 | void serial_close(void); 77 | 78 | #endif /* PLATFORMS_HOSTED_CLI_H */ 79 | -------------------------------------------------------------------------------- /src/platforms/hosted/cmsis_dap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2019-2021 Uwe Bonnes 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_HOSTED_CMSIS_DAP_H 21 | #define PLATFORMS_HOSTED_CMSIS_DAP_H 22 | 23 | #include "adiv5.h" 24 | #include "cli.h" 25 | 26 | #if defined(CMSIS_DAP) 27 | int dap_init(bmp_info_s *info); 28 | void dap_exit_function(void); 29 | void dap_adiv5_dp_defaults(adiv5_debug_port_s *dp); 30 | bool dap_jtagtap_init(void); 31 | bool dap_swdptap_init(adiv5_debug_port_s *dp); 32 | void dap_jtag_dp_init(adiv5_debug_port_s *dp); 33 | uint32_t dap_swj_clock(uint32_t clock); 34 | void dap_swd_configure(uint8_t cfg); 35 | void dap_nrst_set_val(bool assert); 36 | #else 37 | int dap_init(bmp_info_s *info) 38 | { 39 | DEBUG_WARN("FATAL: Missing hidapi-libusb\n"); 40 | (void)info; 41 | return -1; 42 | } 43 | 44 | #pragma GCC diagnostic push 45 | #pragma GCC diagnostic ignored "-Wunused-parameter" 46 | 47 | uint32_t dap_swj_clock(uint32_t clock) 48 | { 49 | return 0; 50 | } 51 | 52 | void dap_exit_function(void) 53 | { 54 | } 55 | 56 | void dap_adiv5_dp_defaults(adiv5_debug_port_s *dp) 57 | { 58 | } 59 | 60 | bool dap_jtagtap_init(void) 61 | { 62 | return -1; 63 | } 64 | 65 | bool dap_swdptap_init(adiv5_debug_port_s *dp) 66 | { 67 | return false; 68 | } 69 | 70 | void dap_jtag_dp_init(adiv5_debug_port_s *dp) 71 | { 72 | (void)dp; 73 | } 74 | 75 | void dap_swd_configure(uint8_t cfg) 76 | { 77 | } 78 | 79 | void dap_nrst_set_val(bool assert) 80 | { 81 | } 82 | 83 | #pragma GCC diagnostic pop 84 | #endif 85 | 86 | #endif /* PLATFORMS_HOSTED_CMSIS_DAP_H */ 87 | -------------------------------------------------------------------------------- /src/platforms/hosted/jlink.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2019 Uwe Bonnes 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_HOSTED_JLINK_H 21 | #define PLATFORMS_HOSTED_JLINK_H 22 | 23 | #include 24 | #include "bmp_hosted.h" 25 | 26 | /** @cond PRIVATE */ 27 | #define CMD_GET_VERSION 0x01U 28 | #define CMD_SET_SPEED 0x05U 29 | #define CMD_GET_HW_STATUS 0x07U 30 | #define CMD_GET_SPEEDS 0xc0U 31 | #define CMD_GET_SELECT_IF 0xc7U 32 | #define CMD_HW_JTAG3 0xcfU 33 | #define CMD_HW_RESET0 0xdcU 34 | #define CMD_HW_RESET1 0xddU 35 | #define CMD_GET_CAPS 0xe8U 36 | #define CMD_GET_EXT_CAPS 0xedU 37 | #define CMD_GET_HW_VERSION 0xf0U 38 | 39 | #define JLINK_IF_GET_ACTIVE 0xfeU 40 | #define JLINK_IF_GET_AVAILABLE 0xffU 41 | 42 | #define JLINK_CAP_GET_SPEEDS (1U << 9U) 43 | #define JLINK_CAP_GET_HW_VERSION (1U << 1U) 44 | #define JLINK_IF_JTAG 1U 45 | #define JLINK_IF_SWD 2U 46 | 47 | #define SELECT_IF_JTAG 0U 48 | #define SELECT_IF_SWD 1U 49 | 50 | #if HOSTED_BMP_ONLY == 1 51 | #pragma GCC diagnostic push 52 | #pragma GCC diagnostic ignored "-Wunused-parameter" 53 | 54 | bool jlink_init(bmp_info_s *info) 55 | { 56 | return false; 57 | } 58 | 59 | uint32_t jlink_swdp_scan(bmp_info_s *info) 60 | { 61 | return 0; 62 | } 63 | 64 | bool jlink_jtagtap_init(bmp_info_s *info) 65 | { 66 | return false; 67 | } 68 | 69 | const char *jlink_target_voltage(bmp_info_s *info) 70 | { 71 | return "ERROR"; 72 | } 73 | 74 | void jlink_nrst_set_val(bmp_info_s *info, bool assert) 75 | { 76 | } 77 | 78 | bool jlink_nrst_get_val(bmp_info_s *info) 79 | { 80 | return true; 81 | } 82 | 83 | void jlink_max_frequency_set(bmp_info_s *info, uint32_t freq) 84 | { 85 | } 86 | 87 | uint32_t jlink_max_frequency_get(bmp_info_s *info) 88 | { 89 | return 0; 90 | } 91 | 92 | #pragma GCC diagnostic pop 93 | #else 94 | bool jlink_init(bmp_info_s *info); 95 | uint32_t jlink_swdp_scan(bmp_info_s *info); 96 | bool jlink_jtagtap_init(bmp_info_s *info); 97 | const char *jlink_target_voltage(bmp_info_s *info); 98 | void jlink_nrst_set_val(bmp_info_s *info, bool assert); 99 | bool jlink_nrst_get_val(bmp_info_s *info); 100 | void jlink_max_frequency_set(bmp_info_s *info, uint32_t freq); 101 | uint32_t jlink_max_frequency_get(bmp_info_s *info); 102 | #endif 103 | 104 | #endif /* PLATFORMS_HOSTED_JLINK_H */ 105 | -------------------------------------------------------------------------------- /src/platforms/hosted/probe_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, this 12 | * list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 18 | * 3. Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef PLATFORMS_HOSTED_PROBE_INFO_H 35 | #define PLATFORMS_HOSTED_PROBE_INFO_H 36 | 37 | #include 38 | #include "platform.h" 39 | #include "bmp_hosted.h" 40 | 41 | typedef struct probe_info { 42 | bmp_type_t type; 43 | uint16_t vid; 44 | uint16_t pid; 45 | const char *manufacturer; 46 | const char *product; 47 | const char *serial; 48 | const char *version; 49 | 50 | struct probe_info *next; 51 | } probe_info_s; 52 | 53 | probe_info_s *probe_info_add_by_serial( 54 | probe_info_s *list, bmp_type_t type, const char *mfr, const char *product, const char *serial, const char *version); 55 | probe_info_s *probe_info_add_by_id(probe_info_s *const list, const bmp_type_t type, uint16_t vid, uint16_t pid, 56 | const char *const mfr, const char *const product, const char *const serial, const char *const version); 57 | size_t probe_info_count(const probe_info_s *list); 58 | void probe_info_list_free(const probe_info_s *list); 59 | 60 | const probe_info_s *probe_info_correct_order(probe_info_s *list); 61 | const probe_info_s *probe_info_filter(const probe_info_s *list, const char *serial, size_t position); 62 | void probe_info_to_bmp_info(const probe_info_s *probe, bmp_info_s *info); 63 | 64 | #endif /* PLATFORMS_HOSTED_PROBE_INFO_H */ 65 | -------------------------------------------------------------------------------- /src/platforms/hosted/stlinkv2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2019 Uwe Bonnes 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_HOSTED_STLINKV2_H 21 | #define PLATFORMS_HOSTED_STLINKV2_H 22 | 23 | #include "bmp_hosted.h" 24 | 25 | #define STLINK_ERROR_FAIL (-1) 26 | #define STLINK_ERROR_OK 0 27 | #define STLINK_ERROR_WAIT 1 28 | 29 | #define STLINK_DEBUG_PORT_ACCESS 0xffffU 30 | 31 | #if HOSTED_BMP_ONLY == 1 32 | #pragma GCC diagnostic push 33 | #pragma GCC diagnostic ignored "-Wunused-parameter" 34 | 35 | int stlink_init(bmp_info_s *info) 36 | { 37 | return -1; 38 | } 39 | 40 | int stlink_hwversion(void) 41 | { 42 | return -1; 43 | } 44 | 45 | const char *stlink_target_voltage(bmp_info_s *info) 46 | { 47 | return "ERROR"; 48 | } 49 | 50 | void stlink_nrst_set_val(bmp_info_s *info, bool assert) 51 | { 52 | } 53 | 54 | bool stlink_nrst_get_val(void) 55 | { 56 | return true; 57 | } 58 | 59 | uint32_t stlink_swdp_scan(bmp_info_s *info) 60 | { 61 | return 0; 62 | } 63 | 64 | void stlink_adiv5_dp_defaults(adiv5_debug_port_s *dp) 65 | { 66 | } 67 | 68 | void stlink_jtag_dp_init(adiv5_debug_port_s *dp) 69 | { 70 | (void)dp; 71 | } 72 | 73 | uint32_t jtag_scan_stlinkv2(bmp_info_s *info, const uint8_t *irlens) 74 | { 75 | return 0; 76 | } 77 | 78 | void stlink_exit_function(bmp_info_s *info) 79 | { 80 | } 81 | 82 | void stlink_max_frequency_set(bmp_info_s *info, uint32_t freq) 83 | { 84 | } 85 | 86 | uint32_t stlink_max_frequency_get(bmp_info_s *info) 87 | { 88 | return 0; 89 | } 90 | 91 | #pragma GCC diagnostic pop 92 | #else 93 | int stlink_init(bmp_info_s *info); 94 | int stlink_hwversion(void); 95 | const char *stlink_target_voltage(bmp_info_s *info); 96 | void stlink_nrst_set_val(bmp_info_s *info, bool assert); 97 | bool stlink_nrst_get_val(void); 98 | uint32_t stlink_swdp_scan(bmp_info_s *info); 99 | void stlink_adiv5_dp_defaults(adiv5_debug_port_s *dp); 100 | void stlink_jtag_dp_init(adiv5_debug_port_s *dp); 101 | uint32_t jtag_scan_stlinkv2(bmp_info_s *info, const uint8_t *irlens); 102 | void stlink_exit_function(bmp_info_s *info); 103 | void stlink_max_frequency_set(bmp_info_s *info, uint32_t freq); 104 | uint32_t stlink_max_frequency_get(bmp_info_s *info); 105 | #endif 106 | 107 | #endif /* PLATFORMS_HOSTED_STLINKV2_H */ 108 | -------------------------------------------------------------------------------- /src/platforms/hosted/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, this 12 | * list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 18 | * 3. Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef PLATFORMS_HOSTED_UTILS_H 35 | #define PLATFORMS_HOSTED_UTILS_H 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | bool begins_with(const char *str, size_t str_length, const char *value); 42 | bool ends_with(const char *str, size_t str_length, const char *value); 43 | bool contains_substring(const char *str, size_t str_len, const char *search); 44 | 45 | #endif /* PLATFORMS_HOSTED_UTILS_H */ 46 | -------------------------------------------------------------------------------- /src/platforms/hydrabus/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ 6 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 7 | -DSTM32F4 -I../libopencm3/include \ 8 | -Iplatforms/stm32 -DDFU_SERIAL_LENGTH=9 9 | 10 | LDFLAGS = -lopencm3_stm32f4 \ 11 | -Wl,-T,platforms/stm32/f4discovery.ld -nostartfiles -lc -lnosys \ 12 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ 13 | -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ 14 | -L../libopencm3/lib 15 | 16 | VPATH += platforms/stm32 17 | 18 | SRC += \ 19 | traceswodecode.c \ 20 | traceswo.c \ 21 | serialno.c \ 22 | timing.c \ 23 | timing_stm32.c \ 24 | 25 | all: blackmagic.bin blackmagic.hex 26 | 27 | blackmagic.dfu: blackmagic.hex 28 | @echo Creating $@ 29 | @python3 ../scripts/dfu-convert.py -i $< $@ 30 | 31 | host_clean: 32 | -$(Q)$(RM) blackmagic.bin blackmagic.hex blackmagic.dfu 33 | -------------------------------------------------------------------------------- /src/platforms/hydrabus/README.md: -------------------------------------------------------------------------------- 1 | Hydrabus 2 | ======== 3 | 4 | Connections 5 | ----------- 6 | 7 | * PA0: User button to force system bootloader entry with reset (enter USB DFU) 8 | 9 | * JTAG/SWD 10 | * PC0: TMS/SWDIO 11 | * PC1: TCK/SWCLK 12 | * PC2: TDI 13 | * PC3: TDO/TRACESWO 14 | * PC4: NRST (NRST / System Reset) 15 | * PC5: TRST (optional Test Reset) 16 | 17 | * Green Led(ULED/PA4): Indicator that system bootloader is entered via BMP 18 | 19 | * USB USART 20 | * PA9: USART1 TX (usbuart_xxx) 21 | * PA10: USART1 RX (usbuart_xxx) 22 | 23 | How to Build 24 | ------------ 25 | 26 | ```sh 27 | cd blackmagic 28 | make clean 29 | make PROBE_HOST=hydrabus 30 | ``` 31 | 32 | How to Flash the firmware with Windows 33 | -------------------------------------- 34 | 35 | * After build: 36 | * 1) Download files from https://github.com/hydrabus/hydrafw/tree/master/utils/windows_dfu_util 37 | * 2) Force the F4 into system bootloader mode by jumpering "BOOT0" to "3V3" and "PB2/BOOT1" to "GND" and reset (RESET button). System bootloader should appear. 38 | * 3) Run the command `DfuSeCommand.exe -c --de 0 -d --fn .\src\blackmagic.dfu` 39 | -------------------------------------------------------------------------------- /src/platforms/hydrabus/usbdfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * Copyright (C) 2015 Benjamin Vernoux 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include "general.h" 22 | #include "usbdfu.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | void dfu_detach(void) 30 | { 31 | /* USB device must detach, we just reset... */ 32 | scb_reset_system(); 33 | } 34 | 35 | int main(void) 36 | { 37 | /* Check the force bootloader pin*/ 38 | rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); 39 | if (!gpio_get(GPIOA, GPIO0)) 40 | dfu_jump_app_if_valid(); 41 | 42 | dfu_protect_enable(); 43 | 44 | /* Set up clock*/ 45 | rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); 46 | systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); 47 | systick_set_reload(2100000U); 48 | 49 | systick_interrupt_enable(); 50 | systick_counter_enable(); 51 | 52 | /* Handle LED */ 53 | rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); 54 | gpio_clear(GPIOA, GPIO4); 55 | gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4); 56 | 57 | /* Set up USB*/ 58 | rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); 59 | rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); 60 | 61 | /* Set up USB Pins and alternate function*/ 62 | gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10 | GPIO11 | GPIO12); 63 | gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO10 | GPIO11 | GPIO12); 64 | dfu_init(&stm32f107_usb_driver); 65 | 66 | dfu_main(); 67 | } 68 | 69 | void sys_tick_handler(void) 70 | { 71 | gpio_toggle(GPIOA, GPIO4); /* Green LED on/off */ 72 | } 73 | -------------------------------------------------------------------------------- /src/platforms/launchpad-icdi/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | SERIAL_NO ?= 1 3 | CC = $(CROSS_COMPILE)gcc 4 | OBJCOPY = $(CROSS_COMPILE)objcopy 5 | 6 | INCLUDES = -I../libopencm3/include 7 | 8 | CPU_FLAGS = -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard 9 | CFLAGS += $(INCLUDES) $(CPU_FLAGS) -DSERIAL_NO=$(SERIAL_NO) -DTARGET_IS_BLIZZARD_RB1 -DLM4F -DPART_TM4C123GH6PM 10 | CFLAGS += -DDFU_SERIAL_LENGTH=9 11 | 12 | LINKER_SCRIPT="platforms/tm4c/tm4c.ld" 13 | LDFLAGS = -nostartfiles -lc $(CPU_FLAGS) -nodefaultlibs -T$(LINKER_SCRIPT) -Wl,--gc-sections \ 14 | -L../libopencm3/lib -lopencm3_lm4f -lnosys -lm -lgcc 15 | 16 | VPATH += platforms/tm4c 17 | 18 | SRC += \ 19 | timing.c \ 20 | traceswo.o 21 | 22 | all: blackmagic.bin 23 | -------------------------------------------------------------------------------- /src/platforms/native/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -Istm32/include -mcpu=cortex-m3 -mthumb \ 6 | -DSTM32F1 -DBLACKMAGIC -I../libopencm3/include \ 7 | -Iplatforms/stm32 -DDFU_SERIAL_LENGTH=9 8 | 9 | LDFLAGS_BOOT := $(LDFLAGS) --specs=nano.specs -lopencm3_stm32f1 \ 10 | -Wl,-T,platforms/stm32/blackmagic.ld -nostartfiles -lc \ 11 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \ 12 | -L../libopencm3/lib 13 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8002000 14 | 15 | ifeq ($(ENABLE_DEBUG), 1) 16 | LDFLAGS += --specs=rdimon.specs 17 | else 18 | LDFLAGS += --specs=nosys.specs 19 | endif 20 | 21 | VPATH += platforms/stm32 22 | 23 | SRC += \ 24 | traceswodecode.c \ 25 | traceswo.c \ 26 | serialno.c \ 27 | timing.c \ 28 | timing_stm32.c \ 29 | 30 | all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 31 | 32 | blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f1.o serialno.o 33 | @echo " LD $@" 34 | $(Q)$(CC) $^ -o $@ $(LDFLAGS_BOOT) 35 | 36 | host_clean: 37 | -$(Q)$(RM) blackmagic.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex 38 | -------------------------------------------------------------------------------- /src/platforms/stlink/Bootloader_Upgrade: -------------------------------------------------------------------------------- 1 | Bootloader Upgrade on Stlink 2 | ============================ 3 | 4 | Beside accessing the SWD pins direct like explained on 5 | https://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe 6 | an updated bootloader can also be programmed via DFU. This requires three 7 | steps: 8 | 1. Prepare bootloader update 9 | Normal BMP has to be replaced by the upgrade program: 10 | dfu-util -s 0x08002000:leave -D dfu_upgrade.bin 11 | Wait until Dual color led flashes red, indicating DFU is active for the 12 | bootloader. 13 | 14 | 2. Flash new bootloader 15 | dfu-util -s 0x08000000 -D blackmagic_dfu.bin 16 | Wait until Dual color led flashes green, indicating bootloader is active. 17 | 18 | If not, unplug USB, keep black reset button pressed, replug USB. 19 | Wait until Dual color led flashes green. 20 | 21 | 3. Flash BMP 22 | dfu-util -s 0x08002000:leave:force -D blackmagic.bin 23 | -------------------------------------------------------------------------------- /src/platforms/stlink/Connector: -------------------------------------------------------------------------------- 1 | == SWD Connector CN2 -- 2 | 1 VDD_TARGET VDD from application 3 | 2 SWCLK SWD clock 4 | 3 GND Ground 5 | 4 SWDIO SWD data input/output 6 | 5 NRST RESET of target MCU 7 | 6 SWO Reserved 8 | -------------------------------------------------------------------------------- /src/platforms/stlink/Flashsize_F103: -------------------------------------------------------------------------------- 1 | Announced versus available Flash size on F103 2 | ============================================ 3 | Up to Stlink V2, the CPU soldered on the board was a F103C8 with 64 kiByte 4 | flash. Up to about version 280 of BMP, this limit was not hit when linked 5 | against nanolib. 6 | 7 | StlinkV2-1 has a STM32F103CB, like a genuine BMP. 8 | 9 | However with more and more devices supported, BMP at about version 282 hit 10 | this limit. There are two ways to work around: 11 | - Branch STlink V2-1 as separate platform and and care for the original STlink 12 | platform by restricting/omitting features. 13 | - Rely on uncertain upper flash on F103C8 14 | 15 | The first option needs more care as an additional platform is introduced and 16 | will restrict usage of older STlinks as BMPs. 17 | 18 | However F103C8 and F103CB have the same chip and upper flash exists on F103C8. 19 | This flash may have been tested bad, or not have been tested at all, 20 | or, in the best case, was tested good but market requirements made STM sell 21 | it as F103C8. 22 | 23 | Ignoring the chip marking and using an F103C8 blindly as a F103CB is done 24 | already with no known problems on many STM board and china boards (e.g. blue 25 | pill) with genuine STM32. China stlinks clones with a GD32F103x8 will 26 | probably not work. Best is to get new genuine ST hardware. The 27 | STLINK-Vmini is < 10 $ without VAT or to resolder a genuine 28 | STM32F103CB chip. Think also about using the pc-hosted pc-stlinkv2 29 | platform on stlinks with revent firmware. 30 | 31 | Flash above the announced size with recent bootloader/BMP: 32 | ========================================================== 33 | 34 | Use either the provided python tool, as script/stm32_mem.py 35 | does not care for the announced size and verifies: 36 | 37 | > ../scripts/stm32_mem.py blackmagic.bin 38 | 39 | or compile and use the upgrade executable. 40 | 41 | > cd upgrade 42 | > make (PROBE_HOST=...) 43 | > ./blackmagic_upgrade 44 | 45 | To long to read 46 | =============== 47 | Use the BMP provided upgrade tools: 48 | - scripts/stm32_mem.py 49 | - Compiled upgrade tool in upgrade 50 | Only if mismatch is reported, think further. 51 | -------------------------------------------------------------------------------- /src/platforms/stlink/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | ST_BOOTLOADER ?= 3 | CC = $(CROSS_COMPILE)gcc 4 | OBJCOPY = $(CROSS_COMPILE)objcopy 5 | 6 | OPT_FLAGS = -Os 7 | CFLAGS += -mcpu=cortex-m3 -mthumb -DSTM32F1 -I../libopencm3/include \ 8 | -I platforms/stm32 9 | LDFLAGS_BOOT := $(LDFLAGS) --specs=nano.specs -lopencm3_stm32f1 \ 10 | -Wl,-T,platforms/stm32/stlink.ld -nostartfiles -lc \ 11 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \ 12 | -L../libopencm3/lib 13 | ifeq ($(ST_BOOTLOADER), 1) 14 | $(info Load address 0x08004000 for original ST-LinkV2 Bootloader) 15 | CFLAGS += -DST_BOOTLOADER -DDFU_SERIAL_LENGTH=25 16 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000 17 | else 18 | CFLAGS += -DDFU_SERIAL_LENGTH=9 19 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8002000 20 | endif 21 | 22 | ifeq ($(ENABLE_DEBUG), 1) 23 | LDFLAGS += --specs=rdimon.specs 24 | else 25 | LDFLAGS += --specs=nosys.specs 26 | endif 27 | 28 | ifeq ($(SWIM_AS_UART), 1) 29 | CFLAGS += -DSWIM_AS_UART=1 30 | else 31 | SRC += traceswoasync.c 32 | endif 33 | 34 | ifeq ($(BLUEPILL), 1) 35 | CFLAGS += -DBLUEPILL=1 36 | endif 37 | 38 | VPATH += platforms/stm32 39 | 40 | SRC += \ 41 | serialno.c \ 42 | timing.c \ 43 | timing_stm32.c \ 44 | traceswodecode.c \ 45 | stlink_common.c \ 46 | 47 | ifeq ($(ST_BOOTLOADER), 1) 48 | all: blackmagic.bin 49 | else 50 | all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 51 | endif 52 | blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f1.o stlink_common.o serialno.o 53 | @echo " LD $@" 54 | $(Q)$(CC) $^ -o $@ $(LDFLAGS_BOOT) 55 | 56 | host_clean: 57 | -$(Q)$(RM) *.bin *elf *hex 58 | -------------------------------------------------------------------------------- /src/platforms/stlink/Readme: -------------------------------------------------------------------------------- 1 | Find a description how to modify a Discovery Board to use it's Stlink as 2 | black magic debug at 3 | http://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe 4 | 5 | Differences between V1/V2 6 | 7 | V1 V2 8 | ID Pins PC13/14 unconnected PC 13 pulled low 9 | LED STLINK PA8, active High PA9, Dual Led 10 | MCO Out NA PA8 11 | RESET(Target) T_JRST(PB1) NRST (PB0) 12 | 13 | On the NucleoXXXP boards, e.g. NUCLEO-L4R5ZI (144 pin) or 14 | NUCLEO-L452RE-P (64 pins), by default nRst is not connected. To reach the 15 | target nRST pin with the "mon connect_reset enable" option, the right NRST 16 | jumper must be placed. On Nucleo144-P boards it is JP3, on NUCLEO64-P 17 | boards it is JP4. 18 | -------------------------------------------------------------------------------- /src/platforms/stlink/usbdfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "usbdfu.h" 27 | #include "general.h" 28 | #include "platform.h" 29 | 30 | static uint32_t rev; 31 | static uint16_t led_bootloader; 32 | static uint16_t pin_nrst; 33 | static uint32_t led2_state = 0; 34 | 35 | uintptr_t app_address = 0x08002000U; 36 | 37 | static bool stlink_test_nrst(void) 38 | { 39 | uint16_t nrst; 40 | gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_nrst); 41 | gpio_set(GPIOB, pin_nrst); 42 | for (size_t i = 0; i < 10000U; ++i) 43 | nrst = gpio_get(GPIOB, pin_nrst); 44 | return nrst; 45 | } 46 | 47 | void dfu_detach(void) 48 | { 49 | scb_reset_system(); 50 | } 51 | 52 | int main(void) 53 | { 54 | rev = detect_rev(); 55 | if (rev == 0) { 56 | led_bootloader = GPIO8; 57 | pin_nrst = GPIO1; 58 | } else { 59 | led_bootloader = GPIO9; 60 | pin_nrst = GPIO0; 61 | } 62 | 63 | if ((GPIOA_CRL & 0x40U) == 0x40U && stlink_test_nrst()) 64 | dfu_jump_app_if_valid(); 65 | dfu_protect(false); 66 | 67 | rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); 68 | systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); 69 | systick_set_reload(900000); 70 | 71 | systick_interrupt_enable(); 72 | systick_counter_enable(); 73 | 74 | if (rev > 1U) 75 | gpio_set(GPIOA, GPIO15); 76 | dfu_init(&st_usbfs_v1_usb_driver); 77 | 78 | dfu_main(); 79 | } 80 | 81 | void dfu_event(void) 82 | { 83 | } 84 | 85 | void sys_tick_handler(void) 86 | { 87 | if (rev == 0) { 88 | gpio_toggle(GPIOA, led_bootloader); 89 | } else { 90 | if (led2_state & 1U) { 91 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, led_bootloader); 92 | gpio_clear(GPIOA, led_bootloader); 93 | } else 94 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, led_bootloader); 95 | 96 | ++led2_state; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/platforms/stlinkv3/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | OPT_FLAGS = -Og -g 6 | CFLAGS += -mcpu=cortex-m7 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard \ 7 | -DSTM32F7 -DDFU_SERIAL_LENGTH=25 -I../libopencm3/include \ 8 | -I platforms/stm32 9 | LDFLAGS_BOOT := $(LDFLAGS) -mfpu=fpv5-sp-d16 -mfloat-abi=hard \ 10 | --specs=nano.specs -lopencm3_stm32f7 \ 11 | -Wl,-T,platforms/stlinkv3/stlinkv3.ld -nostartfiles -lc \ 12 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m7 -Wl,-gc-sections \ 13 | -L../libopencm3/lib 14 | 15 | ifeq ($(NO_BOOTLOADER), 1) 16 | APP_START = 0x08000000 17 | else 18 | APP_START = 0x08020000 19 | endif 20 | 21 | LDFLAGS = $(LDFLAGS_BOOT) 22 | LDFLAGS += -Wl,-Ttext=$(APP_START) 23 | CFLAGS += -DAPP_START=$(APP_START) 24 | 25 | ifeq ($(ENABLE_DEBUG), 1) 26 | LDFLAGS += --specs=rdimon.specs 27 | else 28 | LDFLAGS += --specs=nosys.specs 29 | endif 30 | 31 | VPATH += platforms/stm32 32 | 33 | SRC += \ 34 | serialno.c \ 35 | timing.c \ 36 | timing_stm32.c \ 37 | traceswoasync_f723.c \ 38 | traceswodecode.c \ 39 | 40 | .PHONY: libopencm3_stm32f7 41 | 42 | ifeq ($(NO_BOOTLOADER), 1) 43 | all: libopencm3_stm32f7 blackmagic.bin 44 | else 45 | all: libopencm3_stm32f7 blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 46 | 47 | blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f4.o usb_f723.o serialno.o 48 | @echo " LD $@" 49 | $(Q)$(CC) $^ -o $@ $(LDFLAGS_BOOT) 50 | endif 51 | 52 | blackmagic.elf: libopencm3_stm32f7 53 | libopencm3_stm32f7: 54 | $(Q)$(MAKE) $(MFLAGS) -C ../libopencm3 lib/stm32/f7 55 | 56 | host_clean: 57 | -$(Q)$(RM) *.bin *elf *hex *.o 58 | 59 | # Add local libopencm3 patched usb driver. 60 | SRC += usb_f723.c 61 | 62 | # Note: below are provided some customized versions of 63 | # files from the libopencm3 libraries. It would be ideal 64 | # if some day these go in libopencm3 master, but for the 65 | # time being this is a convenient solution. 66 | # 67 | # Note that there is something very odd about linking these 68 | # libopencm3 replacement files and libopencm3 together. 69 | # If all of the externally visible symbols from these 70 | # replacement files are kept (e.g., no externally visible 71 | # functions or data objects are removed), then linking 72 | # mysteriously succeeds. However, removing some externally 73 | # symbols cause linking to (expectedly) fail, giving an 74 | # error of multiple symbol definitions. The strange 75 | # thing is why linking succeeds in the case described above. 76 | 77 | usb_f723.o: usb_f723.c 78 | @echo " CC $<" 79 | $(Q)$(CC) -I../libopencm3/lib/usb/ $(CFLAGS) $(OPT_FLAGS) -c $< -o $@ 80 | -------------------------------------------------------------------------------- /src/platforms/stlinkv3/README.md: -------------------------------------------------------------------------------- 1 | # Blackmagic firmware for STLINK-V3 Adapters 2 | 3 | Besides using STLINK-V3 as a debugger for the hosted build of the blackmagic, 4 | it is also possible to run the BMP firmware directly on the STLINK-V3 5 | hardware. 6 | 7 | ## Attention: Irreversible read protection activation starting with firmware V36 and up! 8 | If updated with ST firmware version V36 or higher, the STLINK-V3 enters 9 | read-out protection (RDP) level 2, which irreversibly makes SWD access 10 | to the chip impossible. The BMP firmware may still be flashed to the device, 11 | even though in a somewhat not-straightforward manner. Some unprotected bootloader 12 | on the net is available that is said to not set RDP2 with V36 and above. 13 | 14 | ## STLINK-V3 features 15 | The STLINK-V3 is built upon the STM32F723 device, which features a high-speed 16 | USB connection, 384 kBytes of flash space above the ST bootloader, 17 | 256 kBytes of RAM and lots of connections. STLINK-V3SET comes with 18 | a case and offering multiple connectors for the other functions. STLINK-V3MINI has 19 | a 14-pin 1.27mm JTAG/SWD/UART connector, compatible with the 10 pin BMP-Native 20 | connector when used with a 14-pin connector. The rest of the connection are 21 | castellated holes. The STLINK-V3MINI is now obsolete, 22 | but has been replaced with the STLINK-V3MINIE which is similar to the STLINK-V3MINI 23 | but replaces the micro USB port with a USB type-C port and does not have any 24 | castellated holes. STLINK-V3MODS remove the 14-pin connector and only has castellated 25 | holes, meant to be soldered on board. 26 | 27 | The ST firmware checks the Romtable and only allows access to STM32 devices. In 28 | some situations, Romtable access may also fail on STM32 device and so a debugger 29 | warm plug will fail. Cold plug should work with any STM32 device. 30 | 31 | ## Building. 32 | 33 | As simple as 34 | ``` 35 | make PROBE_HOST=stlinkv3 clean 36 | make PROBE_HOST=stlinkv3 37 | ``` 38 | 39 | ## Flashing 40 | Easiest is using the BMP bootloader. Load the BMP firmware easiest with 41 | scripts/stm32mem.py or dfu-utils. BMP bootloader must be flashed with SWD 42 | access. Use CN2 on the [STLINK-V3SET](https://www.st.com/resource/en/data_brief/stlink-v3set.pdf). 43 | For [STLINK-V3MINI](https://www.st.com/resource/en/data_brief/stlink-v3mini.pdf) 44 | and [STLINK-V3MODS](https://www.st.com/resource/en/data_brief/stlink-v3mods.pdf) 45 | use soldered connections to CN3. For [STLINK-V3MINI](https://www.st.com/resource/en/data_brief/stlink-v3minie.pdf) use soldered connections to CN5. 46 | 47 | It is a good idea to keep a full image of the original flash content as backup! 48 | 49 | If you want to keep the original bootloader or access via SWD is disabled, clone 50 | https://github.com/UweBonnes/stlink-tool/tree/stlinkv21 51 | make and use like 52 | `stlink-tool blackmagic.bin` 53 | Revert to original ST firmware with 54 | `java -jar STLinkUpgrade.jar` 55 | Try to use old version that do not disable SWD access. Expect newer ST firmware even to be more restrictive. 56 | 57 | ## What remains to be done? 58 | 59 | - Improve and document LED indication 60 | - ...and more, e.g. additional implement CAN, I2C, ... in the firmware 61 | -------------------------------------------------------------------------------- /src/platforms/stlinkv3/stlinkv3.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 256K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | -------------------------------------------------------------------------------- /src/platforms/stm32/96b_carbon.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 96K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | 30 | -------------------------------------------------------------------------------- /src/platforms/stm32/blackmagic.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | 30 | -------------------------------------------------------------------------------- /src/platforms/stm32/blackpillv2.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 96K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | -------------------------------------------------------------------------------- /src/platforms/stm32/f4discovery.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 1024K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | 30 | -------------------------------------------------------------------------------- /src/platforms/stm32/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_STM32_GPIO_H 22 | #define PLATFORMS_STM32_GPIO_H 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | static inline void bmp_gpio_set(const uint32_t gpioport, const uint16_t gpios) 29 | { 30 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 31 | GPIO_BSRR(gpioport) = gpios; 32 | #if defined(STM32F4) || defined(STM32F7) 33 | /* FIXME: Check if doubling is still needed */ 34 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 35 | GPIO_BSRR(gpioport) = gpios; 36 | #endif 37 | } 38 | 39 | #define gpio_set bmp_gpio_set 40 | 41 | static inline void bmp_gpio_clear(const uint32_t gpioport, const uint16_t gpios) 42 | { 43 | #ifdef GPIO_BRR 44 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 45 | GPIO_BRR(gpioport) = gpios; 46 | #else 47 | #if defined(STM32F4) || defined(STM32F7) 48 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 49 | GPIO_BSRR(gpioport) = gpios << 16U; 50 | #endif 51 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 52 | GPIO_BSRR(gpioport) = gpios << 16U; 53 | #endif 54 | } 55 | 56 | #define gpio_clear bmp_gpio_clear 57 | 58 | static inline uint16_t bmp_gpio_get(const uint32_t gpioport, const uint16_t gpios) 59 | { 60 | /* NOLINTNEXTLINE(clang-diagnostic-int-to-pointer-cast) */ 61 | return GPIO_IDR(gpioport) & gpios; 62 | } 63 | 64 | #define gpio_get bmp_gpio_get 65 | 66 | static inline void gpio_set_val(const uint32_t gpioport, const uint16_t gpios, const bool val) 67 | { 68 | if (val) 69 | gpio_set(gpioport, gpios); 70 | else 71 | gpio_clear(gpioport, gpios); 72 | } 73 | 74 | #endif /* PLATFORMS_STM32_GPIO_H */ 75 | -------------------------------------------------------------------------------- /src/platforms/stm32/serialno.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #include "general.h" 21 | #include 22 | 23 | char serial_no[DFU_SERIAL_LENGTH]; 24 | 25 | void read_serial_number(void) 26 | { 27 | #if DFU_SERIAL_LENGTH == 9 28 | const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE; 29 | const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2]; 30 | /* Fetch serial number from chip's unique ID */ 31 | for (size_t i = 0; i < 8U; ++i) { 32 | serial_no[7U - i] = ((unique_id >> (i * 4U)) & 0x0fU) + '0'; 33 | /* If the character is something above 9, then add the offset to make it ASCII A-F */ 34 | if (serial_no[7U - i] > '9') 35 | serial_no[7U - i] += 7; /* 'A' - '9' = 8, less 1 gives 7. */ 36 | } 37 | #elif DFU_SERIAL_LENGTH == 13 38 | /* Use the same serial number as the ST DFU Bootloader.*/ 39 | const volatile uint16_t *const uid = (uint16_t *)DESIG_UNIQUE_ID_BASE; 40 | #if defined(STM32F4) || defined(STM32F7) 41 | int offset = 3; 42 | #elif defined(STM32L0) || defined(STM32F0) || defined(STM32F3) 43 | int offset = 5; 44 | #endif 45 | sprintf(serial_no, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]); 46 | #elif DFU_SERIAL_LENGTH == 25 47 | const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE; 48 | uint32_t unique_id = 0; 49 | for (size_t i = 0; i < 24U; ++i) { 50 | const size_t chunk = i >> 3U; 51 | const size_t nibble = i & 7U; 52 | const size_t idx = (chunk << 3U) + (7U - nibble); 53 | if (nibble == 0) 54 | unique_id = unique_id_p[chunk]; 55 | serial_no[idx] = ((unique_id >> (nibble * 4U)) & 0xfU) + '0'; 56 | 57 | /* If the character is something above 9, then add the offset to make it ASCII A-F */ 58 | if (serial_no[idx] > '9') 59 | serial_no[idx] += 7; /* 'A' - '9' = 8, less 1 gives 7. */ 60 | } 61 | #else 62 | #WARNING "Unhandled DFU_SERIAL_LENGTH" 63 | #endif 64 | serial_no[DFU_SERIAL_LENGTH - 1] = '\0'; 65 | } 66 | -------------------------------------------------------------------------------- /src/platforms/stm32/stlink.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | -------------------------------------------------------------------------------- /src/platforms/stm32/stm32_can.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | -------------------------------------------------------------------------------- /src/platforms/stm32/stm32f07xzb.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopencm3 project. 3 | * 4 | * Copyright (C) 2015 Karl Palsson 5 | * 6 | * This library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this library. If not, see . 18 | */ 19 | 20 | /* Linker script for STM32F07xzB, 128k flash, 16k RAM. */ 21 | 22 | /* Define memory regions. */ 23 | MEMORY 24 | { 25 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K 26 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 16K 27 | } 28 | 29 | /* Include the common ld script. */ 30 | INCLUDE cortex-m-generic.ld 31 | -------------------------------------------------------------------------------- /src/platforms/stm32/stm32f303xc.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopencm3 project. 3 | * 4 | * Copyright (C) 2015 Karl Palsson 5 | * 6 | * This library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this library. If not, see . 18 | */ 19 | 20 | /* Linker script for the STM32F303xC chip. */ 21 | 22 | /* Define memory regions. */ 23 | MEMORY 24 | { 25 | rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K 26 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 40K 27 | } 28 | 29 | /* Include the common ld script. */ 30 | INCLUDE cortex-m-generic.ld 31 | -------------------------------------------------------------------------------- /src/platforms/stm32/timing_stm32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #include "general.h" 20 | #include "morse.h" 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | bool running_status = false; 27 | static volatile uint32_t time_ms = 0; 28 | uint32_t swd_delay_cnt = 0; 29 | 30 | static size_t morse_tick = 0; 31 | 32 | void platform_timing_init(void) 33 | { 34 | /* Setup heartbeat timer */ 35 | systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); 36 | /* Interrupt us at 100 Hz */ 37 | systick_set_reload(rcc_ahb_frequency / (8U * SYSTICKHZ)); 38 | /* SYSTICK_IRQ with low priority */ 39 | nvic_set_priority(NVIC_SYSTICK_IRQ, 14U << 4U); 40 | systick_interrupt_enable(); 41 | systick_counter_enable(); 42 | } 43 | 44 | void platform_delay(uint32_t ms) 45 | { 46 | platform_timeout_s timeout; 47 | platform_timeout_set(&timeout, ms); 48 | while (!platform_timeout_is_expired(&timeout)) 49 | continue; 50 | } 51 | 52 | void sys_tick_handler(void) 53 | { 54 | time_ms += SYSTICKMS; 55 | 56 | if (morse_tick >= MORSECNT) { 57 | if (running_status) 58 | gpio_toggle(LED_PORT, LED_IDLE_RUN); 59 | SET_ERROR_STATE(morse_update()); 60 | morse_tick = 0; 61 | } else 62 | ++morse_tick; 63 | } 64 | 65 | uint32_t platform_time_ms(void) 66 | { 67 | return time_ms; 68 | } 69 | 70 | /* 71 | * Assume some USED_SWD_CYCLES per clock and CYCLES_PER_CNT cycles 72 | * per delay loop count with 2 delay loops per clock 73 | */ 74 | 75 | /* Values for STM32F103 at 72 MHz */ 76 | #define USED_SWD_CYCLES 22 77 | #define CYCLES_PER_CNT 10 78 | 79 | void platform_max_frequency_set(uint32_t freq) 80 | { 81 | uint32_t divisor = rcc_ahb_frequency - USED_SWD_CYCLES * freq; 82 | /* If we now have an insanely big divisor, the above operation wrapped to a negative signed number. */ 83 | if (divisor >= 0x80000000U) { 84 | swd_delay_cnt = 0; 85 | return; 86 | } 87 | divisor /= 2U; 88 | swd_delay_cnt = divisor / (CYCLES_PER_CNT * freq); 89 | if (swd_delay_cnt * (CYCLES_PER_CNT * freq) < divisor) 90 | ++swd_delay_cnt; 91 | } 92 | 93 | uint32_t platform_max_frequency_get(void) 94 | { 95 | uint32_t ret = rcc_ahb_frequency; 96 | ret /= USED_SWD_CYCLES + CYCLES_PER_CNT * swd_delay_cnt; 97 | return ret; 98 | } 99 | -------------------------------------------------------------------------------- /src/platforms/stm32/timing_stm32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_STM32_TIMING_STM32_H 21 | #define PLATFORMS_STM32_TIMING_STM32_H 22 | 23 | #include 24 | #include 25 | 26 | extern uint32_t swd_delay_cnt; 27 | extern bool running_status; 28 | 29 | void platform_timing_init(void); 30 | 31 | #endif /* PLATFORMS_STM32_TIMING_STM32_H */ 32 | -------------------------------------------------------------------------------- /src/platforms/stm32/traceswodecode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /* Print decoded swo stream on the usb serial */ 20 | 21 | #include "general.h" 22 | #include "usb_serial.h" 23 | #include "traceswo.h" 24 | 25 | /* SWO decoding */ 26 | /* data is static in case swo packet is astride two buffers */ 27 | static uint8_t swo_buf[CDCACM_PACKET_SIZE]; 28 | static int swo_buf_len = 0; 29 | static uint32_t swo_decode = 0; /* bitmask of channels to print */ 30 | static int swo_pkt_len = 0; /* decoder state */ 31 | static bool swo_print = false; 32 | 33 | /* print decoded swo packet on usb serial */ 34 | uint16_t traceswo_decode(usbd_device *usbd_dev, uint8_t addr, const void *buf, uint16_t len) 35 | { 36 | if (usbd_dev == NULL) 37 | return 0; 38 | const uint8_t *const data = (const uint8_t *)buf; 39 | for (uint16_t i = 0; i < len; i++) { 40 | const uint8_t ch = data[i]; 41 | if (swo_pkt_len == 0) { /* header */ 42 | uint32_t channel = (uint32_t)ch >> 3U; /* channel number */ 43 | uint32_t size = ch & 0x7U; /* drop channel number */ 44 | if (size == 0x01U) 45 | swo_pkt_len = 1; /* SWO packet 0x01XX */ 46 | else if (size == 0x02U) 47 | swo_pkt_len = 2; /* SWO packet 0x02XXXX */ 48 | else if (size == 0x03U) 49 | swo_pkt_len = 4; /* SWO packet 0x03XXXXXXXX */ 50 | swo_print = (swo_pkt_len != 0) && ((swo_decode & (1UL << channel)) != 0UL); 51 | } else if (swo_pkt_len <= 4) { /* data */ 52 | if (swo_print) { 53 | swo_buf[swo_buf_len++] = ch; 54 | if (swo_buf_len == sizeof(swo_buf)) { 55 | if (usb_get_config() && gdb_serial_get_dtr()) /* silently drop if usb not ready */ 56 | usbd_ep_write_packet(usbd_dev, addr, swo_buf, swo_buf_len); 57 | swo_buf_len = 0; 58 | } 59 | } 60 | --swo_pkt_len; 61 | } else { /* recover */ 62 | swo_buf_len = 0; 63 | swo_pkt_len = 0; 64 | } 65 | } 66 | return len; 67 | } 68 | 69 | /* set bitmask of swo channels to be decoded */ 70 | void traceswo_setmask(uint32_t mask) 71 | { 72 | swo_decode = mask; 73 | } 74 | 75 | /* not truncated */ 76 | -------------------------------------------------------------------------------- /src/platforms/stm32/usbdfu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2013 Gareth McMullin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef PLATFORMS_STM32_USBDFU_H 21 | #define PLATFORMS_STM32_USBDFU_H 22 | 23 | #include 24 | #include 25 | 26 | /* Commands sent with wBlockNum == 0 as per ST implementation. */ 27 | #define CMD_SETADDR 0x21U 28 | #define CMD_ERASE 0x41U 29 | extern uintptr_t app_address; 30 | 31 | /* dfucore.c - DFU core, common to libopencm3 platforms. */ 32 | void dfu_init(const usbd_driver *driver); 33 | void dfu_main(void); 34 | 35 | /* Device specific functions */ 36 | void dfu_check_and_do_sector_erase(uint32_t sector); 37 | void dfu_flash_program_buffer(uint32_t baseaddr, const void *buf, size_t len); 38 | uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum); 39 | void dfu_protect(bool enable); 40 | void dfu_jump_app_if_valid(void); 41 | void dfu_event(void); 42 | 43 | /* Platform specific function */ 44 | void dfu_detach(void); 45 | 46 | #endif /* PLATFORMS_STM32_USBDFU_H */ 47 | -------------------------------------------------------------------------------- /src/platforms/swlink/Connectors: -------------------------------------------------------------------------------- 1 | CN5 JTAG Conn 2 | -------------- 3 | 4 | JRST - o o - GND 5 | JTDI - o o - JTMS/SWDIO 6 | JTCK/SWCLK - o o - JTDO 7 | o - +3v3 8 | 9 | 10 | CN7 SWIM Conn 11 | -------------- 12 | 13 | o - VDD - (V_target) - Unconnected/Floating if Jtag-PCB is broken away from STM8-Board 14 | o - USART1_RX - (SWIM_IN) 15 | STM8-Board <-- o - GND 16 | o - USART1_TX - (SWIM_RST) 17 | 18 | | 19 | v 20 | 21 | XTAL 22 | 23 | -------------------------------------------------------------------------------- /src/platforms/swlink/Makefile.inc: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | CC = $(CROSS_COMPILE)gcc 3 | OBJCOPY = $(CROSS_COMPILE)objcopy 4 | 5 | CFLAGS += -mcpu=cortex-m3 -mthumb \ 6 | -DSTM32F1 -DDFU_SERIAL_LENGTH=9 -I../libopencm3/include \ 7 | -I platforms/stm32 8 | LDFLAGS_BOOT := $(LDFLAGS) --specs=nano.specs -lopencm3_stm32f1 \ 9 | -Wl,-T,platforms/stm32/stlink.ld -nostartfiles -lc\ 10 | -Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \ 11 | -L../libopencm3/lib 12 | LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8002000 13 | 14 | ifeq ($(ENABLE_DEBUG), 1) 15 | LDFLAGS += --specs=rdimon.specs 16 | else 17 | LDFLAGS += --specs=nosys.specs 18 | endif 19 | 20 | VPATH += platforms/stm32 21 | 22 | SRC += \ 23 | serialno.c \ 24 | timing.c \ 25 | timing_stm32.c \ 26 | traceswodecode.c \ 27 | traceswoasync.c \ 28 | platform_common.c \ 29 | 30 | all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex 31 | 32 | blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f1.o platform_common.o serialno.o 33 | @echo " LD $@" 34 | $(Q)$(CC) $^ -o $@ $(LDFLAGS_BOOT) 35 | 36 | host_clean: 37 | -$(Q)$(RM) blackmagic.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex 38 | -------------------------------------------------------------------------------- /src/platforms/swlink/platform_common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2018 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de) 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "platform_common.h" 25 | 26 | /* Return 0 for the ST-Link on a STM8S Discovery board and 1 for Bluepill */ 27 | uint8_t detect_rev() 28 | { 29 | /* Enable peripherals used by both debugger and DFU. */ 30 | rcc_periph_clock_enable(RCC_GPIOA); 31 | rcc_periph_clock_enable(RCC_GPIOB); 32 | rcc_periph_clock_enable(RCC_USB); 33 | /* 34 | * Test connectivity between PB9 and PB10 needed for 35 | * original ST software to implement SWIM. 36 | */ 37 | gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO9); 38 | gpio_set(GPIOB, GPIO9); 39 | gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); 40 | while (!gpio_get(GPIOB, GPIO10)) 41 | gpio_set(GPIOB, GPIO10); 42 | while (gpio_get(GPIOB, GPIO10)) 43 | gpio_clear(GPIOB, GPIO10); 44 | /* Read PB9 as soon as we read PB10 low. */ 45 | const uint8_t revision = gpio_get(GPIOB, GPIO9) ? 1 : 0; 46 | /* Release PB9/10 */ 47 | gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO9 | GPIO10); 48 | gpio_set(GPIOB, GPIO9); 49 | switch (revision) { 50 | case 0: 51 | gpio_clear(GPIOA, GPIO8); 52 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); 53 | break; 54 | case 1: 55 | rcc_periph_clock_enable(RCC_GPIOC); 56 | gpio_set(GPIOC, GPIO13); /* LED on Blupill is active low! */ 57 | gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); 58 | break; 59 | } 60 | /* 61 | * Disconnect USB after reset: 62 | * Pull USB_DP low. Device will reconnect automatically 63 | * when USB is set up later, as Pull-Up is hard wired 64 | */ 65 | gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12); 66 | gpio_clear(GPIOA, GPIO12); 67 | rcc_periph_reset_pulse(RST_USB); 68 | rcc_periph_clock_enable(RCC_USB); 69 | 70 | return revision; 71 | } 72 | 73 | void platform_request_boot(void) 74 | { 75 | /* 76 | * Assert bootloader marker - enable internal pull-up/down on PA1. 77 | * We don't rely on the external pin really being pulled, but rather 78 | * on if the value of the CNF register is different from the reset value. 79 | */ 80 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO1); 81 | SCB_VTOR = 0; 82 | } 83 | -------------------------------------------------------------------------------- /src/platforms/swlink/platform_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef PLATFORMS_SWLINK_PLATFORM_COMMON_H 22 | #define PLATFORMS_SWLINK_PLATFORM_COMMON_H 23 | 24 | #include 25 | 26 | uint8_t detect_rev(); 27 | void platform_request_boot(void); 28 | 29 | #endif /*PLATFORMS_SWLINK_PLATFORM_COMMON_H*/ 30 | -------------------------------------------------------------------------------- /src/platforms/tm4c/tm4c.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the libopenstm32 project. 3 | * 4 | * Copyright (C) 2010 Thomas Otto 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | /* Define memory regions. */ 21 | MEMORY 22 | { 23 | rom (rx) : ORIGIN = 0x00000000, LENGTH = 256K 24 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 32K 25 | } 26 | 27 | /* Include the common ld script from libopenstm32. */ 28 | INCLUDE cortex-m-generic.ld 29 | 30 | -------------------------------------------------------------------------------- /src/target/flashstub/Makefile: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE ?= arm-none-eabi- 2 | AS = $(CROSS_COMPILE)as 3 | CC = $(CROSS_COMPILE)gcc 4 | OBJCOPY = $(CROSS_COMPILE)objcopy 5 | HEXDUMP = hexdump 6 | 7 | ifneq ($(V), 1) 8 | Q = @ 9 | endif 10 | 11 | CFLAGS=-Os -std=gnu99 -mcpu=cortex-m0 -mthumb -I../../../libopencm3/include 12 | ASFLAGS=-mcpu=cortex-m3 -mthumb 13 | 14 | all: lmi.stub stm32l4.stub efm32.stub 15 | 16 | %.o: %.c 17 | $(Q)echo " CC $<" 18 | $(Q)$(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | %.o: %.s 21 | $(Q)echo " AS $<" 22 | $(Q)$(AS) $(ASFLAGS) -o $@ $< 23 | 24 | %.bin: %.o 25 | $(Q)echo " OBJCOPY $@" 26 | $(Q)$(OBJCOPY) -O binary $< $@ 27 | 28 | %.stub: %.bin 29 | $(Q)echo " HEXDUMP $@" 30 | $(Q)$(HEXDUMP) -v -e '/2 "0x%04X, "' $< > $@ 31 | 32 | .PHONY: clean 33 | 34 | clean: 35 | $(Q)echo " CLEAN" 36 | -$(Q)rm -f *.o *.bin *.stub 37 | 38 | -------------------------------------------------------------------------------- /src/target/flashstub/README.md: -------------------------------------------------------------------------------- 1 | Flash Stubs 2 | =========== 3 | 4 | These are simple routines for programming the flash on various Cortex-M 5 | microcontrollers. The routines should be provided with the naked attribute 6 | as the stack may not be available, and must not make any function calls. 7 | The stub must call `stub_exit(code)` provided by `stub.h` to return control 8 | to the debugger. Up to 4 word sized parameters may be taken. 9 | 10 | These stubs are compiled instructions comma separated hex values in the 11 | resulting `*.stub` files here, which may be included in the drivers for the 12 | specific device. The drivers call these flash stubs on the target by calling 13 | `cortexm_run_stub` defined in `cortexm.h`. 14 | -------------------------------------------------------------------------------- /src/target/flashstub/efm32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Richard Meadows 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #include 20 | #include "stub.h" 21 | 22 | #define EFM32_MSC_WRITECTRL(msc) *((volatile uint32_t *)((msc) + 0x008U)) 23 | #define EFM32_MSC_WRITECMD(msc) *((volatile uint32_t *)((msc) + 0x00cU)) 24 | #define EFM32_MSC_ADDRB(msc) *((volatile uint32_t *)((msc) + 0x010U)) 25 | #define EFM32_MSC_WDATA(msc) *((volatile uint32_t *)((msc) + 0x018U)) 26 | #define EFM32_MSC_STATUS(msc) *((volatile uint32_t *)((msc) + 0x01cU)) 27 | #define EFM32_MSC_LOCK(msc) *((volatile uint32_t *)((msc) + ((msc) == 0x400c0000U ? 0x3cU : 0x40U))) 28 | #define EFM32_MSC_MASSLOCK(msc) *((volatile uint32_t *)((msc) + 0x054U)) 29 | 30 | #define EFM32_MSC_LOCK_LOCKKEY 0x1b71U 31 | 32 | #define EFM32_MSC_WRITECMD_LADDRIM (1U << 0U) 33 | #define EFM32_MSC_WRITECMD_ERASEPAGE (1U << 1U) 34 | #define EFM32_MSC_WRITECMD_WRITEEND (1U << 2U) 35 | #define EFM32_MSC_WRITECMD_WRITEONCE (1U << 3U) 36 | #define EFM32_MSC_WRITECMD_WRITETRIG (1U << 4U) 37 | #define EFM32_MSC_WRITECMD_ERASEABORT (1U << 5U) 38 | 39 | #define EFM32_MSC_STATUS_BUSY (1U << 0U) 40 | #define EFM32_MSC_STATUS_LOCKED (1U << 1U) 41 | #define EFM32_MSC_STATUS_INVADDR (1U << 2U) 42 | #define EFM32_MSC_STATUS_WDATAREADY (1U << 3U) 43 | #define EFM32_MSC_STATUS_WORDTIMEOUT (1U << 4U) 44 | 45 | void __attribute__((naked)) 46 | efm32_flash_write_stub(const uint32_t *const dest, const uint32_t *const src, uint32_t size, const uint32_t msc_addr) 47 | { 48 | const uintptr_t msc = msc_addr; 49 | EFM32_MSC_LOCK(msc) = EFM32_MSC_LOCK_LOCKKEY; 50 | EFM32_MSC_WRITECTRL(msc) = 1; 51 | 52 | for (uint32_t i = 0; i < size / 4U; i++) { 53 | EFM32_MSC_ADDRB(msc) = (uintptr_t)(dest + i); 54 | EFM32_MSC_WRITECMD(msc) = EFM32_MSC_WRITECMD_LADDRIM; 55 | 56 | /* Wait for WDATAREADY */ 57 | while (!(EFM32_MSC_STATUS(msc) & EFM32_MSC_STATUS_WDATAREADY)) 58 | continue; 59 | 60 | EFM32_MSC_WDATA(msc) = src[i]; 61 | EFM32_MSC_WRITECMD(msc) = EFM32_MSC_WRITECMD_WRITEONCE; 62 | 63 | /* Wait for BUSY */ 64 | while ((EFM32_MSC_STATUS(msc) & EFM32_MSC_STATUS_BUSY)) 65 | continue; 66 | } 67 | 68 | stub_exit(0); 69 | } 70 | -------------------------------------------------------------------------------- /src/target/flashstub/efm32.stub: -------------------------------------------------------------------------------- 1 | 0x4D11, 0x243C, 0x42AB, 0xD000, 0x3404, 0x4D10, 0x18E4, 0x6025, 0x2401, 0x2500, 0x2608, 0x0892, 0x609C, 0x0092, 0x4295, 0xD100, 0xBE00, 0x1947, 0x611F, 0x60DC, 0x271C, 0x46BC, 0x69DF, 0x449C, 0x4237, 0xD0F9, 0x594F, 0x619F, 0x60DE, 0x4667, 0x683F, 0x4227, 0xD1FB, 0x3504, 0xE7EA, 0x46C0, 0x0000, 0x400C, 0x1B71, 0x0000, -------------------------------------------------------------------------------- /src/target/flashstub/lmi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #include 21 | #include "stub.h" 22 | 23 | #define LMI_FLASH_BASE ((volatile uint32_t *)0x400fd000U) 24 | #define LMI_FLASH_FMA LMI_FLASH_BASE[0] 25 | #define LMI_FLASH_FMD LMI_FLASH_BASE[1] 26 | #define LMI_FLASH_FMC LMI_FLASH_BASE[2] 27 | 28 | #define LMI_FLASH_FMC_WRITE (1U << 0U) 29 | #define LMI_FLASH_FMC_ERASE (1U << 1U) 30 | #define LMI_FLASH_FMC_MERASE (1U << 2U) 31 | #define LMI_FLASH_FMC_COMT (1U << 3U) 32 | #define LMI_FLASH_FMC_WRKEY 0xa4420000U 33 | 34 | void __attribute__((naked)) 35 | stm32f1_flash_write_stub(const uint32_t *const dest, const uint32_t *const src, const uint32_t size) 36 | { 37 | for (uint32_t i; i < (size / 4U); ++i) { 38 | LMI_FLASH_FMA = (uintptr_t)(dest + i); 39 | LMI_FLASH_FMD = src[i]; 40 | LMI_FLASH_FMC = LMI_FLASH_FMC_WRKEY | LMI_FLASH_FMC_WRITE; 41 | while (LMI_FLASH_FMC & LMI_FLASH_FMC_WRITE) 42 | continue; 43 | } 44 | 45 | stub_exit(0); 46 | } 47 | -------------------------------------------------------------------------------- /src/target/flashstub/lmi.stub: -------------------------------------------------------------------------------- 1 | 0x2300, 0x009C, 0x1909, 0x1C1C, 0x0892, 0x4293, 0xD20E, 0x4E08, 0x1905, 0x6035, 0x590E, 0x4D07, 0x602E, 0x4D07, 0x4E07, 0x602E, 0x682E, 0x07F6, 0xD4FC, 0x3301, 0x3404, 0xE7EE, 0xBE00, 0x46C0, 0xD000, 0x400F, 0xD004, 0x400F, 0xD008, 0x400F, 0x0001, 0xA442, -------------------------------------------------------------------------------- /src/target/flashstub/nrf51.stub: -------------------------------------------------------------------------------- 1 | 0x2300, 0x2601, 0x4D06, 0x1AC9, 0x18C4, 0x429A, 0xD800, 0xBE00, 0x58CF, 0x6027, 0x682C, 0x4234, 0xD0FC, 0x3304, 0xE7F4, 0x46C0, 0xE400, 0x4001, -------------------------------------------------------------------------------- /src/target/flashstub/stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2015 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef TARGET_FLASHSTUB_STUB_H 22 | #define TARGET_FLASHSTUB_STUB_H 23 | 24 | static inline void __attribute__((always_inline)) stub_exit(const int code) 25 | { 26 | __asm__("bkpt %0" ::"i"(code)); 27 | } 28 | 29 | #endif /* TARGET_FLASHSTUB_STUB_H */ 30 | -------------------------------------------------------------------------------- /src/target/gdb_reg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022-2023 1BitSquared 5 | * Written by Mikaela Szekely 6 | * With contributions from Rachel Mant 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include "gdb_reg.h" 23 | 24 | const char *gdb_xml_preamble_first = "" 25 | "" 28 | "" 29 | " "; 30 | const char *gdb_xml_preamble_third = ""; 31 | 32 | const char *gdb_reg_type_strings[] = { 33 | "", // GDB_TYPE_UNSPECIFIED. 34 | " type=\"data_ptr\"", // GDB_TYPE_DATA_PTR. 35 | " type=\"code_ptr\"", // GDB_TYPE_CODE_PTR. 36 | }; 37 | 38 | const char *gdb_reg_save_restore_strings[] = { 39 | "", // GDB_SAVE_RESTORE_UNSPECIFIED. 40 | " save-restore=\"no\"" // GDB_SAVE_RESTORE_NO. 41 | }; 42 | -------------------------------------------------------------------------------- /src/target/gdb_reg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022-2023 1BitSquared 5 | * Written by Mikaela Szekely 6 | * With contributions from Rachel Mant 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef TARGET_GDB_REG_H 23 | #define TARGET_GDB_REG_H 24 | 25 | // The beginning XML for GDB target descriptions that are common to all targets, 26 | // save for one word: the word after DOCTYPE, which is "target" for Cortex-M, and "feature" 27 | // for Cortex-A. The "preamble" is thus split into three parts, with this single word missing 28 | // and as the split point. 29 | extern const char *gdb_xml_preamble_first; 30 | 31 | // The beginning XML for GDB target descriptions that are common to all targets, 32 | // save for one word: the word after DOCTYPE, which is "target" for Cortex-M, and "feature" 33 | // for Cortex-A. The "preamble" is thus split into three parts, with this single word missing 34 | // and as the split point. 35 | extern const char *gdb_xml_preamble_second; 36 | 37 | // The beginning XML for GDB target descriptions that are common to all targets, 38 | // save for one word: the word after , which is "arm" for Cortex-*, and "avr" 39 | // for AVR. The "preamble" is thus split into three parts, with this single word missing 40 | // and as the split point. 41 | extern const char *gdb_xml_preamble_third; 42 | 43 | // The "type" field of a register tag. 44 | typedef enum gdb_reg_type { 45 | GDB_TYPE_UNSPECIFIED = 0, 46 | GDB_TYPE_DATA_PTR, 47 | GDB_TYPE_CODE_PTR, 48 | } gdb_reg_type_e; 49 | 50 | // The strings for the "type" field of a register tag, respective to its gdb_reg_type_e value. 51 | extern const char *gdb_reg_type_strings[]; 52 | 53 | // The "save-restore" field of a register tag. 54 | typedef enum gdb_reg_save_restore { 55 | GDB_SAVE_RESTORE_UNSPECIFIED = 0, 56 | GDB_SAVE_RESTORE_NO, 57 | } gdb_reg_save_restore_e; 58 | 59 | // The strings for the "save-restore" field of a register tag, respective to its gdb_reg_save_restore_e value. 60 | extern const char *gdb_reg_save_restore_strings[]; 61 | 62 | #endif /* TARGET_GDB_REG_H */ 63 | -------------------------------------------------------------------------------- /src/target/jtag_devs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef TARGET_JTAG_DEVS_H 22 | #define TARGET_JTAG_DEVS_H 23 | 24 | #include 25 | 26 | typedef struct jtag_ir_quirks { 27 | uint16_t ir_value; 28 | uint8_t ir_length; 29 | } jtag_ir_quirks_s; 30 | 31 | typedef struct jtag_dev_descr { 32 | uint32_t idcode; 33 | uint32_t idmask; 34 | const char *descr; 35 | void (*handler)(uint8_t jd_index); 36 | jtag_ir_quirks_s ir_quirks; 37 | } jtag_dev_descr_s; 38 | 39 | extern const jtag_dev_descr_s dev_descr[]; 40 | 41 | #endif /* TARGET_JTAG_DEVS_H */ 42 | -------------------------------------------------------------------------------- /src/target/jtag_scan.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * Copyright (C) 2022-2023 1BitSquared 7 | * Modified by Rachel Mant 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #ifndef TARGET_JTAG_SCAN_H 24 | #define TARGET_JTAG_SCAN_H 25 | 26 | #include 27 | #include "jtagtap.h" 28 | 29 | #define JTAG_MAX_DEVS 32U 30 | #define JTAG_MAX_IR_LEN 16U 31 | 32 | typedef struct jtag_dev { 33 | const char *jd_descr; 34 | uint32_t jd_idcode; 35 | uint32_t current_ir; 36 | 37 | /* The DR prescan doubles as the device index */ 38 | uint8_t dr_prescan; 39 | uint8_t dr_postscan; 40 | 41 | uint8_t ir_len; 42 | uint8_t ir_prescan; 43 | uint8_t ir_postscan; 44 | } jtag_dev_s; 45 | 46 | extern jtag_dev_s jtag_devs[JTAG_MAX_DEVS]; 47 | extern uint32_t jtag_dev_count; 48 | extern const uint8_t ones[8]; 49 | 50 | void jtag_dev_write_ir(uint8_t jd_index, uint32_t ir); 51 | void jtag_dev_shift_dr(uint8_t jd_index, uint8_t *dout, const uint8_t *din, size_t ticks); 52 | void jtag_add_device(uint32_t dev_index, const jtag_dev_s *jtag_dev); 53 | 54 | #endif /* TARGET_JTAG_SCAN_H */ 55 | -------------------------------------------------------------------------------- /src/target/jtagtap_generic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /* This file provides generic forms of the low-level jtagtap functions 22 | * for platforms that don't require optimised forms. 23 | */ 24 | #include "general.h" 25 | #include "jtagtap.h" 26 | 27 | void jtagtap_tms_seq(const uint32_t tms_states, const size_t clock_cycles) 28 | { 29 | for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { 30 | const bool tms = (tms_states >> cycle) & 1U; 31 | jtag_proc.jtagtap_next(tms, true); 32 | } 33 | } 34 | 35 | void jtagtap_tdi_tdo_seq( 36 | uint8_t *const data_out, const uint8_t final_tms, const uint8_t *const data_in, const size_t clock_cycles) 37 | { 38 | uint8_t value = 0; 39 | for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { 40 | const size_t bit = cycle & 7U; 41 | const size_t byte = cycle >> 3U; 42 | const bool tms = cycle + 1U >= clock_cycles && final_tms; 43 | const bool tdi = data_in[byte] & (1U << bit); 44 | 45 | if (jtag_proc.jtagtap_next(tms, tdi)) 46 | value |= 1U << bit; 47 | 48 | if (bit == 7U) { 49 | data_out[byte] = value; 50 | value = 0; 51 | } 52 | } 53 | } 54 | 55 | void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *const data_in, const size_t clock_cycles) 56 | { 57 | for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { 58 | const size_t bit = cycle & 7U; 59 | const size_t byte = cycle >> 3U; 60 | const bool tms = cycle + 1U >= clock_cycles && final_tms; 61 | const bool tdi = data_in[byte] & (1U << bit); 62 | jtag_proc.jtagtap_next(tms, tdi); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/target/lpc15xx.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Mike Smith 5 | * Copyright (C) 2016 Gareth McMullin 6 | * Copyright (C) 2016 David Lawrence 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include "general.h" 23 | #include "target.h" 24 | #include "target_internal.h" 25 | #include "cortexm.h" 26 | #include "lpc_common.h" 27 | 28 | #define IAP_PGM_CHUNKSIZE 512U /* Should fit in RAM on any device */ 29 | 30 | #define MIN_RAM_SIZE 1024U 31 | #define RAM_USAGE_FOR_IAP_ROUTINES 32U /* IAP routines use 32 bytes at top of ram */ 32 | 33 | #define IAP_ENTRYPOINT 0x03000205U 34 | #define IAP_RAM_BASE 0x02000000U 35 | 36 | #define LPC15XX_DEVICE_ID 0x400743f8U 37 | 38 | static bool lpc15xx_read_uid(target_s *t, int argc, const char *argv[]) 39 | { 40 | (void)argc; 41 | (void)argv; 42 | struct lpc_flash *f = (struct lpc_flash *)t->flash; 43 | uint8_t uid[16]; 44 | if (lpc_iap_call(f, uid, IAP_CMD_READUID)) 45 | return false; 46 | tc_printf(t, "UID: 0x"); 47 | for (uint32_t i = 0; i < sizeof(uid); ++i) 48 | tc_printf(t, "%02x", uid[i]); 49 | tc_printf(t, "\n"); 50 | return true; 51 | } 52 | 53 | const command_s lpc15xx_cmd_list[] = { 54 | {"readuid", lpc15xx_read_uid, "Read out the 16-byte UID."}, 55 | {NULL, NULL, NULL}, 56 | }; 57 | 58 | static void lpc15xx_add_flash(target_s *target, uint32_t addr, size_t len, size_t erasesize) 59 | { 60 | struct lpc_flash *flash = lpc_add_flash(target, addr, len, IAP_PGM_CHUNKSIZE); 61 | flash->f.blocksize = erasesize; 62 | flash->f.write = lpc_flash_write_magic_vect; 63 | flash->iap_entry = IAP_ENTRYPOINT; 64 | flash->iap_ram = IAP_RAM_BASE; 65 | flash->iap_msp = IAP_RAM_BASE + MIN_RAM_SIZE - RAM_USAGE_FOR_IAP_ROUTINES; 66 | } 67 | 68 | bool lpc15xx_probe(target_s *t) 69 | { 70 | /* read the device ID register */ 71 | const uint32_t device_id = target_mem_read32(t, LPC15XX_DEVICE_ID); 72 | 73 | uint32_t ram_size = 0; 74 | switch (device_id) { 75 | case 0x00001549U: 76 | case 0x00001519U: 77 | ram_size = 0x9000U; 78 | break; 79 | case 0x00001548U: 80 | case 0x00001518U: 81 | ram_size = 0x5000U; 82 | break; 83 | case 0x00001547U: 84 | case 0x00001517U: 85 | ram_size = 0x3000U; 86 | break; 87 | default: 88 | return false; 89 | } 90 | 91 | t->driver = "LPC15xx"; 92 | target_add_ram(t, 0x02000000, ram_size); 93 | lpc15xx_add_flash(t, 0x00000000, 0x40000, 0x1000); 94 | target_add_commands(t, lpc15xx_cmd_list, "LPC15xx"); 95 | return true; 96 | } 97 | -------------------------------------------------------------------------------- /src/target/semihosting.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 Black Sphere Technologies Ltd. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef TARGET_SEMIHOSTING_H 21 | #define TARGET_SEMIHOSTING_H 22 | 23 | /* Semihosting support */ 24 | 25 | /* 26 | * If the target wants to read the special filename ":semihosting-features" 27 | * to know what semihosting features are supported, it's easiest to create 28 | * that file on the host in the directory where gdb runs, 29 | * or, if using pc-hosted, where blackmagic_hosted runs. 30 | * 31 | * $ echo -e 'SHFB\x03' > ":semihosting-features" 32 | * $ chmod 0444 ":semihosting-features" 33 | */ 34 | 35 | /* ARM Semihosting syscall numbers, from "Semihosting for AArch32 and AArch64 Version 3.0" */ 36 | 37 | #define SEMIHOSTING_SYS_CLOCK 0x10U 38 | #define SEMIHOSTING_SYS_CLOSE 0x02U 39 | #define SEMIHOSTING_SYS_ELAPSED 0x30U 40 | #define SEMIHOSTING_SYS_ERRNO 0x13U 41 | #define SEMIHOSTING_SYS_EXIT 0x18U 42 | #define SEMIHOSTING_SYS_EXIT_EXTENDED 0x20U 43 | #define SEMIHOSTING_SYS_FLEN 0x0cU 44 | #define SEMIHOSTING_SYS_GET_CMDLINE 0x15U 45 | #define SEMIHOSTING_SYS_HEAPINFO 0x16U 46 | #define SEMIHOSTING_SYS_ISERROR 0x08U 47 | #define SEMIHOSTING_SYS_ISTTY 0x09U 48 | #define SEMIHOSTING_SYS_OPEN 0x01U 49 | #define SEMIHOSTING_SYS_READ 0x06U 50 | #define SEMIHOSTING_SYS_READC 0x07U 51 | #define SEMIHOSTING_SYS_REMOVE 0x0eU 52 | #define SEMIHOSTING_SYS_RENAME 0x0fU 53 | #define SEMIHOSTING_SYS_SEEK 0x0aU 54 | #define SEMIHOSTING_SYS_SYSTEM 0x12U 55 | #define SEMIHOSTING_SYS_TICKFREQ 0x31U 56 | #define SEMIHOSTING_SYS_TIME 0x11U 57 | #define SEMIHOSTING_SYS_TMPNAM 0x0dU 58 | #define SEMIHOSTING_SYS_WRITE 0x05U 59 | #define SEMIHOSTING_SYS_WRITEC 0x03U 60 | #define SEMIHOSTING_SYS_WRITE0 0x04U 61 | 62 | #endif /* TARGET_SEMIHOSTING_H */ 63 | -------------------------------------------------------------------------------- /src/target/sfdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, this 12 | * list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 18 | * 3. Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef TARGET_SFDP_H 35 | #define TARGET_SFDP_H 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | #include "target.h" 42 | 43 | typedef struct spi_flash_id { 44 | uint8_t manufacturer; 45 | uint8_t type; 46 | uint8_t capacity; 47 | } spi_flash_id_s; 48 | 49 | typedef struct spi_parameters { 50 | uint32_t page_size; 51 | uint32_t sector_size; 52 | size_t capacity; 53 | uint8_t sector_erase_opcode; 54 | } spi_parameters_s; 55 | 56 | typedef void (*read_sfdp_func)(target_s *t, uint32_t address, void *buffer, size_t length); 57 | 58 | bool sfdp_read_parameters(target_s *t, spi_parameters_s *params, read_sfdp_func sfdp_read); 59 | 60 | #endif /* TARGET_SFDP_H */ 61 | -------------------------------------------------------------------------------- /src/target/stm32_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 1BitSquared 5 | * Written by Rachel Mant 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef TARGET_STM32_COMMON_H 22 | #define TARGET_STM32_COMMON_H 23 | 24 | #include "general.h" 25 | #include "target_internal.h" 26 | #include "adiv5.h" 27 | 28 | static inline const char *stm32_psize_to_string(const align_e psize) 29 | { 30 | switch (psize) { 31 | case ALIGN_DWORD: 32 | return "x64"; 33 | case ALIGN_WORD: 34 | return "x32"; 35 | case ALIGN_HALFWORD: 36 | return "x16"; 37 | default: 38 | return "x8"; 39 | } 40 | } 41 | 42 | static inline bool stm32_psize_from_string(target_s *t, const char *const str, align_e *psize) 43 | { 44 | if (strcasecmp(str, "x8") == 0) 45 | *psize = ALIGN_BYTE; 46 | else if (strcasecmp(str, "x16") == 0) 47 | *psize = ALIGN_HALFWORD; 48 | else if (strcasecmp(str, "x32") == 0) 49 | *psize = ALIGN_WORD; 50 | else if (strcasecmp(str, "x64") == 0) 51 | *psize = ALIGN_DWORD; 52 | else { 53 | tc_printf(t, "usage: monitor psize (x8|x16|x32|x32)\n"); 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | #endif /*TARGET_STM32_COMMON_H*/ 60 | -------------------------------------------------------------------------------- /src/target/swdptap_generic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2016 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #include "general.h" 21 | 22 | uint32_t swdptap_seq_in(int ticks) 23 | { 24 | uint32_t index = 1; 25 | uint32_t ret = 0; 26 | 27 | while (ticks--) { 28 | if (swdptap_bit_in()) 29 | ret |= index; 30 | index <<= 1U; 31 | } 32 | 33 | return ret; 34 | } 35 | 36 | bool swdptap_seq_in_parity(uint32_t *ret, int ticks) 37 | { 38 | uint32_t index = 1; 39 | uint8_t parity = 0; 40 | *ret = 0; 41 | 42 | while (ticks--) { 43 | if (swdptap_bit_in()) { 44 | *ret |= index; 45 | parity ^= 1U; 46 | } 47 | index <<= 1U; 48 | } 49 | if (swdptap_bit_in()) 50 | parity ^= 1U; 51 | 52 | return parity; 53 | } 54 | 55 | void swdptap_seq_out(uint32_t MS, int ticks) 56 | { 57 | while (ticks--) { 58 | swdptap_bit_out(MS & 1U); 59 | MS >>= 1U; 60 | } 61 | } 62 | 63 | void swdptap_seq_out_parity(uint32_t MS, int ticks) 64 | { 65 | uint8_t parity = 0; 66 | 67 | while (ticks--) { 68 | swdptap_bit_out(MS & 1U); 69 | parity ^= MS; 70 | MS >>= 1U; 71 | } 72 | swdptap_bit_out(parity & 1U); 73 | } 74 | -------------------------------------------------------------------------------- /src/target/target_probe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2022 Rafael Silva 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef TARGET_TARGET_PROBE_H 21 | #define TARGET_TARGET_PROBE_H 22 | 23 | #include "target.h" 24 | #include "adiv5.h" 25 | 26 | /* 27 | * Probe for various targets. 28 | * Actual functions implemented in their respective drivers. 29 | */ 30 | 31 | bool cortexa_probe(adiv5_access_port_s *apb, uint32_t debug_base); 32 | 33 | bool cortexm_probe(adiv5_access_port_s *ap); 34 | 35 | bool kinetis_mdm_probe(adiv5_access_port_s *ap); 36 | bool nrf51_mdm_probe(adiv5_access_port_s *ap); 37 | bool efm32_aap_probe(adiv5_access_port_s *ap); 38 | bool rp_rescue_probe(adiv5_access_port_s *ap); 39 | bool lpc55_dmap_probe(adiv5_access_port_s *ap); 40 | 41 | bool ch32f1_probe(target_s *t); // will catch all the clones 42 | bool at32fxx_probe(target_s *t); // STM32 clones from Artery 43 | bool mm32l0xx_probe(target_s *t); 44 | bool mm32f3xx_probe(target_s *t); 45 | bool gd32f1_probe(target_s *t); 46 | bool gd32f4_probe(target_s *t); 47 | bool stm32f1_probe(target_s *t); 48 | bool stm32f4_probe(target_s *t); 49 | bool stm32h7_probe(target_s *t); 50 | bool stm32l0_probe(target_s *t); 51 | bool stm32l1_probe(target_s *t); 52 | bool stm32l4_probe(target_s *t); 53 | bool stm32g0_probe(target_s *t); 54 | bool lmi_probe(target_s *t); 55 | bool lpc11xx_probe(target_s *t); 56 | bool lpc15xx_probe(target_s *t); 57 | bool lpc17xx_probe(target_s *t); 58 | bool lpc43xx_probe(target_s *t); 59 | bool lpc546xx_probe(target_s *t); 60 | bool lpc55xx_probe(target_s *t); 61 | bool samx7x_probe(target_s *t); 62 | bool sam3x_probe(target_s *t); 63 | bool sam4l_probe(target_s *t); 64 | bool nrf51_probe(target_s *t); 65 | bool samd_probe(target_s *t); 66 | bool samx5x_probe(target_s *t); 67 | bool kinetis_probe(target_s *t); 68 | bool efm32_probe(target_s *t); 69 | bool msp432_probe(target_s *t); 70 | bool ke04_probe(target_s *t); 71 | bool rp_probe(target_s *t); 72 | bool renesas_probe(target_s *t); 73 | 74 | void lpc55_dp_prepare(adiv5_debug_port_s *dp); 75 | 76 | #endif /* TARGET_TARGET_PROBE_H */ 77 | -------------------------------------------------------------------------------- /src/timing.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2016 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include "general.h" 22 | 23 | void platform_timeout_set(platform_timeout_s *const t, uint32_t ms) 24 | { 25 | if (ms < SYSTICKMS) 26 | ms = SYSTICKMS; 27 | t->time = platform_time_ms() + ms; 28 | } 29 | 30 | bool platform_timeout_is_expired(const platform_timeout_s *const t) 31 | { 32 | return platform_time_ms() > t->time; 33 | } 34 | -------------------------------------------------------------------------------- /upgrade/Makefile: -------------------------------------------------------------------------------- 1 | OUTFILE = blackmagic_upgrade 2 | 3 | PC_HOSTED = 4 | ifeq ($(PROBE_HOST), libftdi) 5 | PC_HOSTED = true 6 | endif 7 | ifeq ($(PROBE_HOST), pc-stlinkv2) 8 | PC_HOSTED = true 9 | endif 10 | 11 | CC = $(CROSS_COMPILE)gcc 12 | 13 | CFLAGS = -Wall -Wextra -std=gnu99 -O0 -g -MD -mno-ms-bitfields 14 | LDFLAGS = -lusb 15 | 16 | OBJ = bindata.o \ 17 | dfu.o \ 18 | stm32mem.o \ 19 | main.o 20 | 21 | ifndef $(PROBE_HOST) 22 | PROBE_HOST=native 23 | endif 24 | 25 | ifndef PC_HOSTED 26 | all: $(OUTFILE) 27 | else 28 | all: 29 | ifeq ($(PROBE_HOST), libftdi) 30 | @echo "Libftdi needs no firmware update" 31 | endif 32 | ifeq ($(PROBE_HOST), pc-stlinkv2) 33 | @echo "Pc-stlinkv2 use ST provided tools for firmware update" 34 | endif 35 | endif 36 | 37 | bindata.o: $(PROBE_HOST).d 38 | 39 | $(PROBE_HOST).d: $(wildcard ../src/blackmagic.bin) 40 | -rm *.d 41 | make -C ../src $0 clean 42 | make -C ../src $0 43 | touch $(PROBE_HOST).d 44 | 45 | $(OUTFILE) $(OUTFILE).exe: $(OBJ) 46 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 47 | 48 | .PHONY: clean 49 | clean: 50 | -rm -rf $(OUTFILE) $(OUTFILE).exe *.d *.o 51 | 52 | -include *.d 53 | 54 | -------------------------------------------------------------------------------- /upgrade/bindata.S: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | .section .rodata 21 | .global _bindata, _bindatalen 22 | 23 | _bindata: 24 | .incbin "../src/blackmagic.bin" 25 | 26 | _bindatalen: 27 | .long (. - _bindata) 28 | 29 | -------------------------------------------------------------------------------- /upgrade/bindata.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef BINDATA_H 22 | #define BINDATA_H 23 | 24 | #ifndef WIN32 25 | #define bindatalen _bindatalen 26 | #define bindata _bindata 27 | #endif 28 | 29 | extern const uint32_t bindatalen; 30 | extern const uint8_t bindata[]; 31 | 32 | #endif /* BINDATA_H */ 33 | -------------------------------------------------------------------------------- /upgrade/stm32mem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #include 21 | 22 | #ifdef WIN32 23 | # include 24 | # include 25 | #else 26 | # include 27 | # include 28 | #endif 29 | 30 | #include "dfu.h" 31 | #include "stm32mem.h" 32 | 33 | #define STM32_CMD_GETCOMMANDS 0x00 34 | #define STM32_CMD_SETADDRESSPOINTER 0x21 35 | #define STM32_CMD_ERASE 0x41 36 | 37 | static int stm32_download(usb_dev_handle *dev, uint16_t iface, 38 | uint16_t wBlockNum, void *data, int size) 39 | { 40 | dfu_status status; 41 | int i; 42 | 43 | if((i = dfu_dnload(dev, iface, wBlockNum, data, size)) < 0) return i; 44 | while(1) { 45 | if((i = dfu_getstatus(dev, iface, &status)) < 0) return i; 46 | switch(status.bState) { 47 | case STATE_DFU_DOWNLOAD_BUSY: 48 | #ifdef WIN32 49 | Sleep(status.bwPollTimeout); 50 | #else 51 | usleep(status.bwPollTimeout * 1000); 52 | #endif 53 | break; 54 | case STATE_DFU_DOWNLOAD_IDLE: 55 | return 0; 56 | default: 57 | return -1; 58 | } 59 | } 60 | } 61 | 62 | int stm32_mem_erase(usb_dev_handle *dev, uint16_t iface, uint32_t addr) 63 | { 64 | uint8_t request[5]; 65 | 66 | request[0] = STM32_CMD_ERASE; 67 | memcpy(request+1, &addr, sizeof(addr)); 68 | 69 | return stm32_download(dev, iface, 0, request, sizeof(request)); 70 | } 71 | 72 | int stm32_mem_write(usb_dev_handle *dev, uint16_t iface, void *data, int size, uint32_t addr) 73 | { 74 | uint8_t request[5]; 75 | 76 | request[0] = STM32_CMD_SETADDRESSPOINTER; 77 | memcpy(request+1, &addr, sizeof(addr)); 78 | stm32_download(dev, iface, 0, request, sizeof(request)); 79 | return stm32_download(dev, iface, 2, data, size); 80 | } 81 | 82 | int stm32_mem_manifest(usb_dev_handle *dev, uint16_t iface) 83 | { 84 | dfu_status status; 85 | int i; 86 | 87 | if((i = dfu_dnload(dev, iface, 0, NULL, 0)) < 0) return i; 88 | while(1) { 89 | if((i = dfu_getstatus(dev, iface, &status)) < 0) return 0; 90 | #ifdef WIN32 91 | Sleep(status.bwPollTimeout); 92 | #else 93 | usleep(status.bwPollTimeout * 1000); 94 | #endif 95 | switch(status.bState) { 96 | case STATE_DFU_MANIFEST: 97 | return 0; 98 | default: 99 | return -1; 100 | } 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /upgrade/stm32mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Black Magic Debug project. 3 | * 4 | * Copyright (C) 2011 Black Sphere Technologies Ltd. 5 | * Written by Gareth McMullin 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef STM32MEM_H 22 | #define STM32MEM_H 23 | 24 | #ifdef WIN32 25 | # include 26 | #else 27 | # include 28 | #endif 29 | 30 | int stm32_mem_erase(usb_dev_handle *dev, uint16_t iface, uint32_t addr); 31 | int stm32_mem_write(usb_dev_handle *dev, uint16_t iface, void *data, int size, uint32_t addr); 32 | int stm32_mem_manifest(usb_dev_handle *dev, uint16_t iface); 33 | 34 | #endif /* STM32MEM_H */ 35 | --------------------------------------------------------------------------------