-
Notifications
You must be signed in to change notification settings - Fork 12
feat(firmware/c_board): Introduce bootloader and DFU flashing pipeline #28
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import struct | ||
| import sys | ||
|
|
||
| IMAGE_HASH_MAGIC = 0x48415348 # "HASH" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="Append ImageHash suffix (magic + SHA-256) to a firmware binary." | ||
| ) | ||
| parser.add_argument("input", help="Input binary file") | ||
| parser.add_argument( | ||
| "-o", | ||
| "--output", | ||
| required=True, | ||
| help="Output file with ImageHash suffix appended", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| with open(args.input, "rb") as f: | ||
| firmware = f.read() | ||
|
|
||
| sha256 = hashlib.sha256(firmware).digest() | ||
| suffix = struct.pack("<I", IMAGE_HASH_MAGIC) + sha256 | ||
|
|
||
| with open(args.output, "wb") as f: | ||
| f.write(firmware) | ||
| f.write(suffix) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,3 +92,4 @@ endfunction() | |
|
|
||
|
|
||
| add_subdirectory(app) | ||
| add_subdirectory(bootloader) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <type_traits> | ||
|
|
||
| namespace librmcs::firmware::utility { | ||
|
|
||
| struct BootMailbox { | ||
| static constexpr uint32_t kMailboxMagic = 0x524D4353; // "RMCS" | ||
| static constexpr uint32_t kMailboxRequestEnterDfu = 0x44465530; // "DFU0" | ||
|
|
||
| volatile uint32_t magic; | ||
| volatile uint32_t request; | ||
|
|
||
| void clear() { | ||
| magic = 0; | ||
| request = 0; | ||
| } | ||
|
|
||
| void request_enter_dfu() { | ||
| magic = kMailboxMagic; | ||
| request = kMailboxRequestEnterDfu; | ||
| } | ||
| }; | ||
| inline BootMailbox boot_mailbox __attribute__((section(".boot_mailbox"), aligned(4), used)); | ||
|
|
||
| static_assert(std::is_standard_layout_v<BootMailbox> && sizeof(BootMailbox) <= 64); | ||
|
|
||
| } // namespace librmcs::firmware::utility |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| add_executable(c_board_bootloader) | ||
|
|
||
| set(C_BOARD_BOOTLOADER_TINYUSB_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../bsp/tinyusb") | ||
| cmake_path(ABSOLUTE_PATH C_BOARD_BOOTLOADER_TINYUSB_ROOT NORMALIZE) | ||
| set(C_BOARD_BOOTLOADER_TINYUSB_COMMON_SOURCES | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/tusb.c" | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/common/tusb_fifo.c" | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/device/usbd.c" | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/device/usbd_control.c" | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/portable/synopsys/dwc2/dwc2_common.c" | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/portable/synopsys/dwc2/dcd_dwc2.c" | ||
| ) | ||
|
|
||
| add_library(tinyusb_bootloader OBJECT) | ||
| target_sources(tinyusb_bootloader PRIVATE | ||
| ${C_BOARD_BOOTLOADER_TINYUSB_COMMON_SOURCES} | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src/class/dfu/dfu_device.c" | ||
| ) | ||
| target_include_directories(tinyusb_bootloader SYSTEM PUBLIC | ||
| "${C_BOARD_BOOTLOADER_TINYUSB_ROOT}/src" | ||
| ) | ||
| target_include_directories(tinyusb_bootloader PUBLIC | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/include" | ||
| ) | ||
| target_compile_definitions(tinyusb_bootloader PUBLIC | ||
| CFG_TUSB_MCU=OPT_MCU_STM32F4 | ||
| ) | ||
| target_link_libraries(tinyusb_bootloader PUBLIC | ||
| stm32cubemx | ||
| ) | ||
|
|
||
| file(GLOB_RECURSE C_BOARD_BOOTLOADER_SOURCES CONFIGURE_DEPENDS | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c" | ||
| ) | ||
| target_sources(c_board_bootloader PRIVATE | ||
| ${C_BOARD_BOOTLOADER_SOURCES} | ||
| ) | ||
|
|
||
| target_include_directories(c_board_bootloader PRIVATE | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/include" | ||
| ) | ||
|
|
||
| c_board_apply_target_options(c_board_bootloader "STM32F407XX_BOOTLOADER.ld") | ||
| target_link_libraries(c_board_bootloader PRIVATE | ||
| tinyusb_bootloader | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #pragma once | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #ifndef CFG_TUSB_MCU | ||
| # error CFG_TUSB_MCU must be defined | ||
| #endif | ||
|
|
||
| #ifndef BOARD_DEVICE_RHPORT_NUM | ||
| # define BOARD_DEVICE_RHPORT_NUM 0 | ||
| #endif | ||
|
|
||
| #ifndef BOARD_DEVICE_RHPORT_SPEED | ||
| # define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED | ||
| #endif | ||
|
|
||
| #if BOARD_DEVICE_RHPORT_NUM == 0 | ||
| # define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) | ||
| #elif BOARD_DEVICE_RHPORT_NUM == 1 | ||
| # define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) | ||
| #else | ||
| # error "Incorrect RHPort configuration" | ||
| #endif | ||
|
|
||
| #define CFG_TUSB_OS OPT_OS_NONE | ||
|
|
||
| #ifndef CFG_TUD_ENDPOINT0_SIZE | ||
| # define CFG_TUD_ENDPOINT0_SIZE 64 | ||
| #endif | ||
|
|
||
| #define CFG_TUD_CDC 0 | ||
| #define CFG_TUD_MSC 0 | ||
| #define CFG_TUD_HID 0 | ||
| #define CFG_TUD_MIDI 0 | ||
| #define CFG_TUD_VENDOR 0 | ||
| #define CFG_TUD_DFU_RUNTIME 0 | ||
| #define CFG_TUD_DFU 1 | ||
|
|
||
| #define CFG_TUD_DFU_XFER_BUFSIZE 1024 | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.