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

[Beta] Jump to system memory boot from user application #710

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Commits on Sep 18, 2020

  1. Clean declaration

    Signed-off-by: Frederic Pillon <[email protected]>
    fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    59a0936 View commit details
    Browse the repository at this point in the history
  2. Jump to system memory boot from user application

    Fixes stm32duino#706
    
    Signed-off-by: Frederic Pillon <[email protected]>
    fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    13945e3 View commit details
    Browse the repository at this point in the history
  3. Add upload.use_1200bps_touch=true

    The upload.use_1200bps_touch key tells the IDE to open the user-selected
    serial port at 1200 baud, and then close it, before attempting an upload.
    This allow to jump in BL mode.
    
    Signed-off-by: Frederic Pillon <[email protected]>
    fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    6c081f4 View commit details
    Browse the repository at this point in the history
  4. Do not use the RTC to track a bootloader request

    Instead, use a .noinit variable, which survives across reboots.
    
    Note: This file uses the bool type, so needs to include stdbool.h.
    It seems that for the F4, this was indirectly included elsewhere,
    but for e.g. F0, this did not seem to be the case, causing the build
    to fail.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    b32a37a View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    c40cfdd View commit details
    Browse the repository at this point in the history
  6. Use handcoded delay in USBD_reenumerate

    This now runs in early startup, so the normal delay will not work
    (systick is not running yet).
    
    Note: Seems that on F0, inline assembler is compiled using the classic
    rather than unified syntax (which *is* used on F4 it seems). This
    explicitly selects the unified syntax in the inline assembly block, to
    fix building for F0.
    
    TODO: There is a variant which also overrides the enumeration.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    c0ef636 View commit details
    Browse the repository at this point in the history
  7. Clear reset flags before starting bootloader

    Since the bootloader does not touch these flags, but can start the
    sketch if instructed by the programmer (without a reset), clearing is
    needed to prevent a start from the bootloader looking like a
    software-reset. Otherwise, the startup code would be looking at the
    BootIntoBootloaderAfterReset variable to decide whether to jump to the
    bootloader. This variable should have been cleared before jumping to the
    bootloader the last time, but the bootloader might have overwritten it
    (or a sketch was uploaded with a different address for that variable).
    
    Additionally, clearing the reset flags allows a sketch to see that the
    they are started through the bootloader, when all reset flags are
    cleared.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    93de06d View commit details
    Browse the repository at this point in the history
  8. Simplify system flash address detection

    On most CPUs, detecting the system flash address is not needed, since
    they can remap the system flash onto adress 0x0 and the bootloader reset
    vector can be read from there. This approach also more closely mimics a
    bootloader start using the BOOTx pins.
    
    For CPUs that cannot remap flash, this hardcodes the address of system
    flash and jumps there instead. For H7 and F7, this is also how it works
    when using the BOOT pins. For F1, remapping can be done using the BOOT
    pins, but not from software, resulting in a difference with a
    BOOT-activated bootloader.
    
    This puts the code that figures out where the bootloader lives back in a
    separate function. This makes the code easier to read, and makes it
    easier to jump to custom bootloaders (e.g. in normal flash instead of in
    system flash).
    
    TODO: Check value for H7, AN2606 and reference manual disagree
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    c6edf2b View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    0871fee View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    6179810 View commit details
    Browse the repository at this point in the history
  11. Delay autoreset to bootloader

    This waits for 250ms, just like the Arduino SAM and SAMD cores do (AVR
    waits only 120ms), to allow the USB host and application a bit more time
    to close the port and clean up.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    8e5ad31 View commit details
    Browse the repository at this point in the history
  12. HACK: Do not use USB_DevDisconnect in early startup

    This function calls HAL_Delay, which relies on the systick timer to be
    running, so this results in an infinite loop. This should probably be
    fixed in HAL_Delay, but for now just remove the calls to
    USB_DevDisconnect and USB_DevConnect and replace it with the bit
    twiddles needed for some chip families (e.g. the F4), breaking
    compilation and/or functionality for others.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    63c49b0 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    2f148bf View commit details
    Browse the repository at this point in the history
  14. [USB] Support DFU runtime protocol along CDC

    This adds support for the DFU runtime protocol, which allows resetting
    into the bootloader using a DFU command. This allows e.g. dfu-util to
    handle the complete firmware upload, including the needed reset.
    
    This consists of a number of changes:
     - An extra interface is added to the USB configuration descriptor. This
       descriptor has two parts (interface descriptor and functional
       descriptor) which together indicate to a host that this device
       supports DFU.
     - Control packets to this new interface are detected by the CDC code an
       forwarded to a new USBD_DFU_Runtime_Control() function.
     - This new function handles the DFU GET_STATE, GET_STATUS and
       DFU_DETACH commands. The former are optional, but simple enough, the
       latter is mandatory and handles resetting into the bootloader.
     - The CDC device descriptor is changed to become a composite device
       (CDC and DFU). This allows operating systems (in particular Windows,
       Linux did not really need this) to identify two different subdevices,
       and install different drivers for each (on Windows, this is serusb
       for the CDC part and WinUSB/libusb for the DFU part). Without this,
       dfu-util on Windows could not access the DFU commands when the serial
       driver was loaded.
    
       Because the CDC functionality already exposes two interfaces (which
       together form a single serial port), an IAD (Interface Association
       Descriptor) is inserted before these interfaces to group them
       together in a single subdevice. No IAD is needed for the DFU
       interface, since it is just a single interface.
    
       To become a composite device, the device class must be changed from
       CDC to a composite device class. This was originally class 0/0/0, but
       together with the IAD, a new EF/2/1 deviceclass was also introduced,
       which is used now.
    
    Note that this only adds descriptors and a command handler on the
    default control endpoint, so no extra (scarce) endpoints are used by
    this, just a bit of memory.
    
    This commit is still a bit rough, because:
     - The DFU descriptors and code are now pulled in directly by the CDC
       code (and HID is not supported yet). Ideally, there should be some
       kind of pluggable USB library where different interfaces can be
       registered independent of each other (see also
       stm32duino#687).
     - The interface number is hardcoded in the DFU descriptor.
     - The reset to bootloader happens immediately, while it might be better
       to wait a short while to allow the current USB transaction to
       complete.
     - DFU support is unconditionally advertised, while not all boards might
       support DFU.
    matthijskooijman authored and fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    3da8f2d View commit details
    Browse the repository at this point in the history
  15. Do not build for STM32MP1xx

    Bootloader management is not applicable for this serie.
    
    Signed-off-by: Frederic Pillon <[email protected]>
    fpistm committed Sep 18, 2020
    Configuration menu
    Copy the full SHA
    a37e822 View commit details
    Browse the repository at this point in the history