Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Blackpill-F4 and BMP DFU #1508

Merged
merged 4 commits into from
Jun 13, 2023

Conversation

ALTracer
Copy link
Contributor

@ALTracer ALTracer commented Jun 7, 2023

Detailed description

  • Stock STM32F4 BootROM USB DFU implementation is slow and doesn't leave DFU mode on warm resets or detach requests.
  • BMP codebase has a dfu_f4 implementation, let's make it available as a faster alternative to BootROM DFU. (~2.4x faster uploads on local testing, performance increased from 8KiB/s to 18KiB/s). Changes to makefile, .text offset of 16k, of which 8k used. This bootloader is not mandatory.
  • blackpill-f4 inherited the usbdfu.c from f4-discovery with wrong clocks (different HSE 25/8, different core clock 84/96/168), let's stick with 96MHz
  • I also took the liberty to pull-up the floating PA0 button for robustness. Now the KEY press on boot actually matters. However, LED_BOOTLOADER which is mapped to PC15 is not the sole user LED on PC13.

Your checklist for this pull request

  • I've read the Code of Conduct
  • I've read the guidelines for contributing to this repository
  • It builds for hardware native (make PROBE_HOST=native) -- doesn't break
  • It builds as BMDA (make PROBE_HOST=hosted) -- not applicable
  • I've tested it to the best of my ability
  • My commit messages provide a useful short description of what the commits do

Closing issues

Not closes per se, but I'd like to mention #1152 from which I took the idea. That was abandoned less than a year ago, so before the big blackpill-f4 migration by @lenvm. Note that the DFU flash sectors' description string is composed at runtime, and the first 16K sector appears read-only (not actually write-protected, though).

Problems

Seeing as there are F401 users, we need to decide on a method of selecting RCC configurations from libopencm3, maybe a macro. F401 will run at 84Mhz with 2ws, F411 at 96Mhz with 3ws (90nm 30Mhz Flash with ART). Not 100 because of USB48. The core is decently powerful, I wouldn't want to limit it especially since the three platform variants have separate headers and linkerscripts.

Upon unlocking the BMP_BOOTLOADER=1 DFU code I faced weird issues with USBOTG DWC core, like the issues I had when building NuttX for this board. On one patch variant it fails to set up address from PC, on another it doesn't appear at all. Before the patch both BMP firmware and ST BootROM worked and enumerated immediately, after the patch maybe only the DFU works and nothing else can use the DWC USB on jump_to_application or soft-reset into ST BootROM. I have to physically unplug the board and remove power from external debugger. I think it has to do with VBus B-session sensing, PA9/PA10 AF mux and proper reset between consecutive usbd_init()'s (or upon leaving dfu_f4) instead of simply adjusting SP and jumping to application.

I need advice from the reviewers on which #if 0-hidden code blocks to drop for good, I simply kept these for context.

Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Principally this looks good. There are a few items that need fixing before we can consider merging this, but they're easy enough to fix.

Thank you for the contribution!

src/platforms/blackpill-f401cc/platform.h Outdated Show resolved Hide resolved
src/platforms/common/blackpill-f4/blackpill-f4.c Outdated Show resolved Hide resolved
src/platforms/common/blackpill-f4/blackpill-f4.c Outdated Show resolved Hide resolved
src/platforms/common/blackpill-f4/blackpill-f4.c Outdated Show resolved Hide resolved
src/platforms/common/blackpill-f4/usbdfu.c Outdated Show resolved Hide resolved
@dragonmux dragonmux added Bug Confirmed bug Foreign Host Board Non Native hardware to runing Black Magic firmware on labels Jun 7, 2023
@dragonmux dragonmux added this to the v1.10 milestone Jun 7, 2023
@dragonmux
Copy link
Member

We need to check through one or two things in the makefile changes for sanity, but this looks much better. Please apply your cleanup commit as a fixup to the commit that introduced those #if 0 blocks and rebase. If you're happy that the code presented here works even if it's not perfect, we'd be interested in getting this merged.

ALTracer added 3 commits June 13, 2023 08:14
* BMP DFU uploads are ~2.4x faster than STM32F4x1 BootROM DFU
* Use 84MHz clocks in DFU (and in APP), revisit for F411 & 96MHz
* Use the PA0 pushbutton active-low and pulled-up to control DFU entry

platforms/blackpill-f4: Makefile-switchable BMP DFU

* Use makeflags to adjust CFLAGS to pass a macro definition

platforms/blackpill-f4: Move bootloader flags to .noinit section

* blackpill-f4 inherited _ebss and related hacks from f4discovery
* This will not cooperate with BMP DFU, but libopencm3 has a .noinit
* Keep #ifdef guards for pure ST DFU by default
V-Bus Sense Disable is required both in DFU and in FW.
* libopencm3 has presets for 84 & 96mhz, use them
  for f401 and f411 respectively. Same for DFU.
@ALTracer
Copy link
Contributor Author

I rebased the entire branch, re-shuffling commits with respective cleanups/fixups, slightly editing commit contents.

I decided to drop the ENABLE_DEBUG & rdimon change from this patchset. That stuff needs revisiting in coordination with #1507. Testing the change on my blackpill didn't solve the probe hanging on initalization, moreover the upstream bluepill debugging up hung too (don't know where, it remaps its SWJ-DP).

Also, I incorporated the change placing bootloader magic into the .noinit section implemented by libopencm3 linkerscripts. arm-none-eabi-nm shows the same symbol of 20000000 B magic in both src/blackmagic.elf and src/blackmagic_dfu. This allowed me to drop a bunch of #pragmas which sole existence was caused by *magic=&_ebss trickery.

The original branch and a few unrelated changes on top of it will stay in my fork for a while.

@ALTracer ALTracer marked this pull request as ready for review June 13, 2023 05:35
Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the bootloader for these platforms fixed, this is looking much better. Thank you! We have just one review comment on this new code and with that fixed it'll be ready for merge.

src/platforms/common/blackpill-f4/usbdfu.c Outdated Show resolved Hide resolved
* Reset unnecessary braces change
* Drop last unneeded `#pragma`s
* Add a clarification on reboot flows
Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, merging as soon as the build completes

@dragonmux dragonmux merged commit df014ff into blackmagic-debug:main Jun 13, 2023
@ALTracer ALTracer deleted the blackpill-f4-dfu branch April 26, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bug Foreign Host Board Non Native hardware to runing Black Magic firmware on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants