From 2bf23f9f887926d43aad35cc3decebabcad17b7d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Dec 2023 10:59:35 +0700 Subject: [PATCH 1/5] tweak ci, only run if needed --- .github/workflows/build_arm.yml | 15 ++++++++++++++- .github/workflows/build_cmake.yml | 15 ++++++++++++++- .github/workflows/build_esp32.yml | 18 ++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml index 7f1480e2..78c45aa8 100644 --- a/.github/workflows/build_arm.yml +++ b/.github/workflows/build_arm.yml @@ -1,8 +1,21 @@ name: Build ARM on: - pull_request: push: + paths: + - 'src/**' + - 'ports/**' + - '!ports/espressif/**' + - 'lib/**' + - '.github/workflows/build_arm.yml' + pull_request: + branches: [ master ] + paths: + - 'src/**' + - 'ports/**' + - '!ports/espressif/**' + - 'lib/**' + - '.github/workflows/build_arm.yml' repository_dispatch: release: types: diff --git a/.github/workflows/build_cmake.yml b/.github/workflows/build_cmake.yml index bf14a778..922b4cfb 100644 --- a/.github/workflows/build_cmake.yml +++ b/.github/workflows/build_cmake.yml @@ -1,8 +1,21 @@ name: Build CMake on: - pull_request: push: + paths: + - 'src/**' + - 'ports/**' + - '!ports/espressif/**' + - 'lib/**' + - '.github/workflows/build_cmake.yml' + pull_request: + branches: [ master ] + paths: + - 'src/**' + - 'ports/**' + - '!ports/espressif/**' + - 'lib/**' + - '.github/workflows/build_cmake.yml' repository_dispatch: release: types: diff --git a/.github/workflows/build_esp32.yml b/.github/workflows/build_esp32.yml index f7fab81e..7f4ae4b7 100644 --- a/.github/workflows/build_esp32.yml +++ b/.github/workflows/build_esp32.yml @@ -1,15 +1,29 @@ name: Build ESP32 on: - pull_request: push: + paths: + - 'src/**' + - 'ports/espressif/**' + - 'lib/tinyusb' + - '.github/workflows/build_esp32.yml' + pull_request: + branches: [ master ] + paths: + - 'src/**' + - 'ports/espressif/**' + - 'lib/tinyusb' + - '.github/workflows/build_esp32.yml' repository_dispatch: release: types: - created -jobs: +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true +jobs: # --------------------------------------- # Build ESP32SX family # --------------------------------------- From 612eb2003aaad8f61f191d1dca7b1fd34293c965 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Dec 2023 15:15:19 +0700 Subject: [PATCH 2/5] add cmake for stm32f4 --- .github/workflows/build_cmake.yml | 1 + ports/mimxrt10xx/CMakeLists.txt | 2 +- ports/mimxrt10xx/family.cmake | 4 -- ports/stm32f3/CMakeLists.txt | 4 +- ports/stm32f3/family.cmake | 4 -- ports/stm32f4/CMakeLists.txt | 26 +++++++ ports/stm32f4/apps/self_update/CMakeLists.txt | 33 +++++++++ .../boards/stm32f411ve_discovery/board.cmake | 11 +++ ports/stm32f4/family.cmake | 70 +++++++++++++++++++ 9 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 ports/stm32f4/CMakeLists.txt create mode 100644 ports/stm32f4/apps/self_update/CMakeLists.txt create mode 100644 ports/stm32f4/boards/stm32f411ve_discovery/board.cmake create mode 100644 ports/stm32f4/family.cmake diff --git a/.github/workflows/build_cmake.yml b/.github/workflows/build_cmake.yml index 922b4cfb..ddf0c6c4 100644 --- a/.github/workflows/build_cmake.yml +++ b/.github/workflows/build_cmake.yml @@ -38,6 +38,7 @@ jobs: # Alphabetical order by family - 'metro_m7_1011' - 'stm32f303disco' + - 'stm32f411ve_discovery' steps: - name: Setup Python diff --git a/ports/mimxrt10xx/CMakeLists.txt b/ports/mimxrt10xx/CMakeLists.txt index 77a6fb51..6874101e 100644 --- a/ports/mimxrt10xx/CMakeLists.txt +++ b/ports/mimxrt10xx/CMakeLists.txt @@ -3,11 +3,11 @@ cmake_minimum_required(VERSION 3.17) include(${CMAKE_CURRENT_LIST_DIR}/../family_support.cmake) project(tinyuf2 C ASM) +set(CMAKE_EXECUTABLE_SUFFIX .elf) #------------------------------------ # TinyUF2 #------------------------------------ -set(CMAKE_EXECUTABLE_SUFFIX .elf) add_executable(tinyuf2 board_flash.c boards.c diff --git a/ports/mimxrt10xx/family.cmake b/ports/mimxrt10xx/family.cmake index 4a4bbafb..411f4be2 100644 --- a/ports/mimxrt10xx/family.cmake +++ b/ports/mimxrt10xx/family.cmake @@ -1,9 +1,5 @@ include_guard() -if (NOT BOARD) - message(FATAL_ERROR "BOARD not specified") -endif () - #------------------------------------ # Config #------------------------------------ diff --git a/ports/stm32f3/CMakeLists.txt b/ports/stm32f3/CMakeLists.txt index 7c9310fa..61722e3b 100644 --- a/ports/stm32f3/CMakeLists.txt +++ b/ports/stm32f3/CMakeLists.txt @@ -3,18 +3,16 @@ cmake_minimum_required(VERSION 3.17) include(${CMAKE_CURRENT_LIST_DIR}/../family_support.cmake) project(tinyuf2 C ASM) +set(CMAKE_EXECUTABLE_SUFFIX .elf) #------------------------------------ # TinyUF2 #------------------------------------ -set(CMAKE_EXECUTABLE_SUFFIX .elf) add_executable(tinyuf2 board_flash.c boards.c - boards.h ${TOP}/lib/tinyusb/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c ) -#target_compile_options(tinyuf2 PUBLIC) target_link_options(tinyuf2 PUBLIC "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/linker/stm32f3_boot.ld" ) diff --git a/ports/stm32f3/family.cmake b/ports/stm32f3/family.cmake index 75cc77d1..15c699df 100644 --- a/ports/stm32f3/family.cmake +++ b/ports/stm32f3/family.cmake @@ -1,9 +1,5 @@ include_guard() -if (NOT BOARD) - message(FATAL_ERROR "BOARD not specified") -endif () - #------------------------------------ # Config #------------------------------------ diff --git a/ports/stm32f4/CMakeLists.txt b/ports/stm32f4/CMakeLists.txt new file mode 100644 index 00000000..952bd1c7 --- /dev/null +++ b/ports/stm32f4/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.17) + +include(${CMAKE_CURRENT_LIST_DIR}/../family_support.cmake) + +project(tinyuf2 C ASM) +set(CMAKE_EXECUTABLE_SUFFIX .elf) + +#------------------------------------ +# TinyUF2 +#------------------------------------ +add_executable(tinyuf2 + board_flash.c + boards.c + ${TOP}/lib/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c + ) +target_link_options(tinyuf2 PUBLIC + "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/linker/stm32f4_boot.ld" + ) + +family_configure_tinyuf2(tinyuf2 OPT_MCU_STM32F4) +family_flash_jlink(tinyuf2) + +#------------------------------------ +# Application (e.g self update) +#------------------------------------ +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/apps/self_update) diff --git a/ports/stm32f4/apps/self_update/CMakeLists.txt b/ports/stm32f4/apps/self_update/CMakeLists.txt new file mode 100644 index 00000000..cede7969 --- /dev/null +++ b/ports/stm32f4/apps/self_update/CMakeLists.txt @@ -0,0 +1,33 @@ +#------------------------------------ +# Self Update +# This file is meant to be include by add_subdirectory() in the root CMakeLists.txt +#------------------------------------ + +# Generate bootloader_bin.c +add_custom_command(OUTPUT bootloader_bin.c + COMMAND ${Python_EXECUTABLE} ${UF2CONV_PY} --carray -o bootloader_bin.c $/tinyuf2.bin + DEPENDS tinyuf2 + ) + +# self_update target +add_executable(self_update + ${TOP}/apps/self_update/self_update.c + ${PORT_DIR}/boards.c + ${PORT_DIR}/board_flash.c + ${CMAKE_CURRENT_BINARY_DIR}/bootloader_bin.c + ) + +target_include_directories(self_update PUBLIC + ${TOP}/src + ) +target_compile_definitions(self_update PUBLIC + TINYUF2_SELF_UPDATE + BUILD_NO_TINYUSB + BUILD_APPLICATION + ) +target_link_options(self_update PUBLIC + "LINKER:--script=${PORT_DIR}/linker/stm32f4_app.ld" + ) + +family_configure_common(self_update) +family_add_uf2(self_update ${UF2_FAMILY_ID}) diff --git a/ports/stm32f4/boards/stm32f411ve_discovery/board.cmake b/ports/stm32f4/boards/stm32f411ve_discovery/board.cmake new file mode 100644 index 00000000..99618043 --- /dev/null +++ b/ports/stm32f4/boards/stm32f411ve_discovery/board.cmake @@ -0,0 +1,11 @@ +set(JLINK_DEVICE stm32f411ve) + +function(update_board TARGET) + target_sources(${TARGET} PUBLIC + ${ST_CMSIS}/Source/Templates/gcc/startup_stm32f411xe.s + ) + target_compile_definitions(${TARGET} PUBLIC + STM32F411xE + HSE_VALUE=8000000U + ) +endfunction() diff --git a/ports/stm32f4/family.cmake b/ports/stm32f4/family.cmake new file mode 100644 index 00000000..7346630c --- /dev/null +++ b/ports/stm32f4/family.cmake @@ -0,0 +1,70 @@ +include_guard() + +#------------------------------------ +# Config +#------------------------------------ + +set(UF2_FAMILY_ID 0x57755a57) +set(ST_HAL_DRIVER ${TOP}/lib/st/stm32f4xx_hal_driver) +set(ST_CMSIS ${TOP}/lib/st/cmsis_device_f4) +set(CMSIS_5 ${TOP}/lib/CMSIS_5) +set(PORT_DIR ${CMAKE_CURRENT_LIST_DIR}) +set(FAMILY_SUBMODULE_DEPS ${ST_CMSIS} ${ST_HAL_DRIVER}) + +include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) + +# enable LTO +set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + +set(CMAKE_SYSTEM_PROCESSOR cortex-m4 CACHE INTERNAL "System Processor") +set(CMAKE_TOOLCHAIN_FILE ${TOP}/cmake/toolchain/arm_${TOOLCHAIN}.cmake) + + +#------------------------------------ +# BOARD_TARGET +#------------------------------------ +# used by all executable targets + +function(family_add_board_target BOARD_TARGET) + if (TARGET ${BOARD_TARGET}) + return() + endif () + + add_library(${BOARD_TARGET} STATIC + ${ST_CMSIS}/Source/Templates/system_stm32f4xx.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_cortex.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_rcc.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_rcc_ex.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_gpio.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_flash.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_flash_ex.c + ${ST_HAL_DRIVER}/Src/stm32f4xx_hal_uart.c + ) + target_include_directories(${BOARD_TARGET} PUBLIC + # port & board + ${CMAKE_CURRENT_FUNCTION_LIST_DIR} + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/boards/${BOARD} + # sdk + ${CMSIS_5}/CMSIS/Core/Include + ${ST_CMSIS}/Include + ${ST_HAL_DRIVER}/Inc + ) + + update_board(${BOARD_TARGET}) + + target_compile_definitions(${BOARD_TARGET} PUBLIC + CFG_TUSB_MCU=OPT_MCU_STM32F4 + BOARD_UF2_FAMILY_ID=${UF2_FAMILY_ID} + ) + target_compile_options(${BOARD_TARGET} PUBLIC + ) + target_link_options(${BOARD_TARGET} PUBLIC + -nostartfiles + --specs=nosys.specs --specs=nano.specs + ) +endfunction() + +#------------------------------------ +# Main target +#------------------------------------ From c637001be39667b63a121958d4dff3f019a063b4 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Dec 2023 15:19:44 +0700 Subject: [PATCH 3/5] minor formatting --- src/ghostfat.c | 120 +++++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 80 deletions(-) diff --git a/src/ghostfat.c b/src/ghostfat.c index 4f2e6c81..9ced0815 100644 --- a/src/ghostfat.c +++ b/src/ghostfat.c @@ -33,8 +33,6 @@ #include "board_api.h" #include "uf2.h" - - //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ @@ -194,7 +192,6 @@ STATIC_ASSERT( CLUSTER_COUNT >= 0x0FF5 && CLUSTER_COUNT < 0xFFF5 ); // So, avoid being within 32 of those limits for even greater compatibility. STATIC_ASSERT( CLUSTER_COUNT >= 0x1015 && CLUSTER_COUNT < 0xFFD5 ); - #define FS_START_FAT0_SECTOR BPB_RESERVED_SECTORS #define FS_START_FAT1_SECTOR (FS_START_FAT0_SECTOR + BPB_SECTORS_PER_FAT) #define FS_START_ROOTDIR_SECTOR (FS_START_FAT1_SECTOR + BPB_SECTORS_PER_FAT) @@ -229,8 +226,7 @@ static FAT_BootBlock const BootBlock = { // //--------------------------------------------------------------------+ -static inline bool is_uf2_block (UF2_Block const *bl) -{ +static inline bool is_uf2_block (UF2_Block const *bl) { return (bl->magicStart0 == UF2_MAGIC_START0) && (bl->magicStart1 == UF2_MAGIC_START1) && (bl->magicEnd == UF2_MAGIC_END) && @@ -240,20 +236,16 @@ static inline bool is_uf2_block (UF2_Block const *bl) // cache the cluster start offset for each file // this allows more flexible algorithms w/o O(n) time -static void init_starting_clusters(void) -{ +static void init_starting_clusters(void) { // +2 because FAT decided first data sector would be in cluster number 2, rather than zero uint16_t start_cluster = 2; - for (uint16_t i = 0; i < NUM_FILES; i++) - { + for (uint16_t i = 0; i < NUM_FILES; i++) { info[i].cluster_start = start_cluster; info[i].cluster_end = start_cluster + UF2_DIV_CEIL(info[i].size, BPB_SECTOR_SIZE*BPB_SECTORS_PER_CLUSTER) - 1; start_cluster = info[i].cluster_end + 1; } - - return; } // get file index for file that uses the cluster @@ -263,15 +255,12 @@ static void init_starting_clusters(void) // contains data from the file's contents, as there // are often padding sectors, including all the unused // sectors past the end of the media. -static uint32_t info_index_of(uint32_t cluster) -{ +static uint32_t info_index_of(uint32_t cluster) { // default results for invalid requests is the index of the last file (CURRENT.UF2) if (cluster >= 0xFFF0) return FID_UF2; - for (uint32_t i = 0; i < NUM_FILES; i++) - { - if ( (info[i].cluster_start <= cluster) && (cluster <= info[i].cluster_end) ) - { + for (uint32_t i = 0; i < NUM_FILES; i++) { + if ( (info[i].cluster_start <= cluster) && (cluster <= info[i].cluster_end) ) { return i; } } @@ -289,8 +278,7 @@ static void u32_to_hexstr(uint32_t value, char* buffer) { buffer[i] = '\0'; } -void uf2_init(void) -{ +void uf2_init(void) { // TODO maybe limit to application size only if possible board_flash_app_size() _flash_size = board_flash_size(); @@ -317,10 +305,8 @@ void uf2_init(void) /*------------------------------------------------------------------*/ /* Read CURRENT.UF2 *------------------------------------------------------------------*/ -void padded_memcpy (char *dst, char const *src, int len) -{ - for ( int i = 0; i < len; ++i ) - { +void padded_memcpy (char *dst, char const *src, int len) { + for ( int i = 0; i < len; ++i ) { if ( *src ) { *dst = *src++; } else { @@ -330,31 +316,26 @@ void padded_memcpy (char *dst, char const *src, int len) } } -void uf2_read_block (uint32_t block_no, uint8_t *data) -{ +void uf2_read_block (uint32_t block_no, uint8_t *data) { memset(data, 0, BPB_SECTOR_SIZE); uint32_t sectionRelativeSector = block_no; - if ( block_no == 0 ) - { + if ( block_no == 0 ) { // Request was for the Boot block memcpy(data, &BootBlock, sizeof(BootBlock)); data[510] = 0x55; // Always at offsets 510/511, even when BPB_SECTOR_SIZE is larger data[511] = 0xaa; // Always at offsets 510/511, even when BPB_SECTOR_SIZE is larger } - else if ( block_no < FS_START_ROOTDIR_SECTOR ) - { + else if ( block_no < FS_START_ROOTDIR_SECTOR ) { // Request was for a FAT table sector sectionRelativeSector -= FS_START_FAT0_SECTOR; // second FAT is same as the first... use sectionRelativeSector to write data - if ( sectionRelativeSector >= BPB_SECTORS_PER_FAT ) - { + if ( sectionRelativeSector >= BPB_SECTORS_PER_FAT ) { sectionRelativeSector -= BPB_SECTORS_PER_FAT; } uint16_t* data16 = (uint16_t*) (void*) data; - uint32_t sectorFirstCluster = sectionRelativeSector * FAT_ENTRIES_PER_SECTOR; uint32_t firstUnusedCluster = info[FID_UF2].cluster_end + 1; @@ -370,44 +351,36 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) // // Set default FAT values first. - for (uint16_t i = 0; i < FAT_ENTRIES_PER_SECTOR; i++) - { + for (uint16_t i = 0; i < FAT_ENTRIES_PER_SECTOR; i++) { uint32_t cluster = i + sectorFirstCluster; - if (cluster >= firstUnusedCluster) - { + if (cluster >= firstUnusedCluster) { data16[i] = 0; } - else - { + else { data16[i] = cluster + 1; } } // Exception #1: clusters 0 and 1 need special handling - if (sectionRelativeSector == 0) - { + if (sectionRelativeSector == 0) { data[0] = BPB_MEDIA_DESCRIPTOR_BYTE; data[1] = 0xff; data16[1] = FAT_END_OF_CHAIN; // cluster 1 is reserved } // Exception #2: the final cluster of each file must be set to END_OF_CHAIN - for (uint32_t i = 0; i < NUM_FILES; i++) - { + for (uint32_t i = 0; i < NUM_FILES; i++) { uint32_t lastClusterOfFile = info[i].cluster_end; - if (lastClusterOfFile >= sectorFirstCluster) - { + if (lastClusterOfFile >= sectorFirstCluster) { uint32_t idx = lastClusterOfFile - sectorFirstCluster; - if (idx < FAT_ENTRIES_PER_SECTOR) - { + if (idx < FAT_ENTRIES_PER_SECTOR) { // that last cluster of the file is in this sector data16[idx] = FAT_END_OF_CHAIN; } } } } - else if ( block_no < FS_START_CLUSTERS_SECTOR ) - { + else if ( block_no < FS_START_CLUSTERS_SECTOR ) { // Request was for a (root) directory sector .. root because not supporting subdirectories (yet) sectionRelativeSector -= FS_START_ROOTDIR_SECTOR; @@ -416,8 +389,7 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) uint32_t startingFileIndex; - if ( sectionRelativeSector == 0 ) - { + if ( sectionRelativeSector == 0 ) { // volume label is first directory entry padded_memcpy(d->name, (char const*) BootBlock.VolumeLabel, 11); d->attrs = 0x28; @@ -425,16 +397,14 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) remainingEntries--; startingFileIndex = 0; - }else - { + }else { // -1 to account for volume label in first sector startingFileIndex = DIRENTRIES_PER_SECTOR * sectionRelativeSector - 1; } for ( uint32_t fileIndex = startingFileIndex; remainingEntries > 0 && fileIndex < NUM_FILES; // while space remains in buffer and more files to add... - fileIndex++, d++ ) - { + fileIndex++, d++ ) { // WARNING -- code presumes all files take exactly one directory entry (no long file names!) uint32_t const startCluster = info[fileIndex].cluster_start; @@ -451,8 +421,7 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) d->size = (inf->content ? inf->size : UF2_BYTE_COUNT); } } - else if ( block_no < BPB_TOTAL_SECTORS ) - { + else if ( block_no < BPB_TOTAL_SECTORS ) { // Request was to read from the data area (files, unused space, ...) sectionRelativeSector -= FS_START_CLUSTERS_SECTOR; @@ -462,8 +431,7 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) uint32_t fileRelativeSector = sectionRelativeSector - (info[fid].cluster_start-2) * BPB_SECTORS_PER_CLUSTER; - if ( fid != FID_UF2 ) - { + if ( fid != FID_UF2 ) { // Handle all files other than CURRENT.UF2 size_t fileContentStartOffset = fileRelativeSector * BPB_SECTOR_SIZE; size_t fileContentLength = inf->size; @@ -481,12 +449,10 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) memcpy(data, dataStart, bytesToCopy); } } - else - { + else { // CURRENT.UF2: generate data on-the-fly uint32_t addr = BOARD_FLASH_APP_START + (fileRelativeSector * UF2_FIRMWARE_BYTES_PER_SECTOR); - if ( addr < _flash_size ) // TODO abstract this out - { + if ( addr < _flash_size ) /* TODO abstract this out */ { UF2_Block *bl = (void*) data; bl->magicStart0 = UF2_MAGIC_START0; bl->magicStart1 = UF2_MAGIC_START1; @@ -515,51 +481,45 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) * 512 : write is successful (BPB_SECTOR_SIZE == 512) * 0 : is busy with flashing, tinyusb stack will call write_block again with the same parameters later on */ -int uf2_write_block (uint32_t block_no, uint8_t *data, WriteState *state) -{ +int uf2_write_block (uint32_t block_no, uint8_t *data, WriteState *state) { (void) block_no; UF2_Block *bl = (void*) data; if ( !is_uf2_block(bl) ) return -1; - if (bl->familyID == BOARD_UF2_FAMILY_ID) - { + if (bl->familyID == BOARD_UF2_FAMILY_ID) { // generic family ID board_flash_write(bl->targetAddr, bl->data, bl->payloadSize); - }else - { + }else { // TODO family matches VID/PID return -1; } //------------- Update written blocks -------------// - if ( bl->numBlocks ) - { + if ( bl->numBlocks ) { // Update state num blocks if needed - if ( state->numBlocks != bl->numBlocks ) - { - if ( bl->numBlocks >= MAX_BLOCKS || state->numBlocks ) + if ( state->numBlocks != bl->numBlocks ) { + if ( bl->numBlocks >= MAX_BLOCKS || state->numBlocks ) { state->numBlocks = 0xffffffff; - else + } + else { state->numBlocks = bl->numBlocks; + } } - if ( bl->blockNo < MAX_BLOCKS ) - { + if ( bl->blockNo < MAX_BLOCKS ) { uint8_t const mask = 1 << (bl->blockNo % 8); uint32_t const pos = bl->blockNo / 8; // only increase written number with new write (possibly prevent overwriting from OS) - if ( !(state->writtenMask[pos] & mask) ) - { + if ( !(state->writtenMask[pos] & mask) ) { state->writtenMask[pos] |= mask; state->numWritten++; } // flush last blocks // TODO numWritten can be smaller than numBlocks if return early - if ( state->numWritten >= state->numBlocks ) - { + if ( state->numWritten >= state->numBlocks ) { board_flash_flush(); } } From 1ec8ebe75cec9bf504f29d27d25e0b98a2647daa Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Dec 2023 15:56:44 +0700 Subject: [PATCH 4/5] add BOARD_FLASH_ADDR_ZERO, fix CURRENT.UF2 always blank for stm32 mcu --- ports/stm32f3/apps/self_update/CMakeLists.txt | 6 +++--- ports/stm32f3/boards.h | 2 ++ ports/stm32f3/family.cmake | 1 - ports/stm32f4/apps/self_update/CMakeLists.txt | 6 +++--- ports/stm32f4/boards.h | 2 ++ ports/stm32f4/family.cmake | 2 -- ports/stm32h7/boards.h | 3 +++ ports/stm32l4/boards.h | 2 ++ src/board_api.h | 4 ++++ src/ghostfat.c | 2 +- 10 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ports/stm32f3/apps/self_update/CMakeLists.txt b/ports/stm32f3/apps/self_update/CMakeLists.txt index 53dd4827..94c61501 100644 --- a/ports/stm32f3/apps/self_update/CMakeLists.txt +++ b/ports/stm32f3/apps/self_update/CMakeLists.txt @@ -12,8 +12,8 @@ add_custom_command(OUTPUT bootloader_bin.c # self_update target add_executable(self_update ${TOP}/apps/self_update/self_update.c - ${PORT_DIR}/boards.c - ${PORT_DIR}/board_flash.c + ${CMAKE_CURRENT_LIST_DIR}/../../boards.c + ${CMAKE_CURRENT_LIST_DIR}/../../board_flash.c ${CMAKE_CURRENT_BINARY_DIR}/bootloader_bin.c ) @@ -26,7 +26,7 @@ target_compile_definitions(self_update PUBLIC BUILD_APPLICATION ) target_link_options(self_update PUBLIC - "LINKER:--script=${PORT_DIR}/linker/stm32f3_app.ld" + "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/../../linker/stm32f3_app.ld" ) family_configure_common(self_update) diff --git a/ports/stm32f3/boards.h b/ports/stm32f3/boards.h index 61bc2d08..fc3f6de5 100644 --- a/ports/stm32f3/boards.h +++ b/ports/stm32f3/boards.h @@ -34,6 +34,8 @@ #include "board.h" +#define BOARD_FLASH_ADDR_ZERO 0x08000000 + // Flash Start Address of Application #ifndef BOARD_FLASH_APP_START #define BOARD_FLASH_APP_START 0x08004000 diff --git a/ports/stm32f3/family.cmake b/ports/stm32f3/family.cmake index 15c699df..e523062a 100644 --- a/ports/stm32f3/family.cmake +++ b/ports/stm32f3/family.cmake @@ -8,7 +8,6 @@ set(UF2_FAMILY_ID 0x6b846188) set(ST_HAL_DRIVER ${TOP}/lib/st/stm32f3xx_hal_driver) set(ST_CMSIS ${TOP}/lib/st/cmsis_device_f3) set(CMSIS_5 ${TOP}/lib/CMSIS_5) -set(PORT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(FAMILY_SUBMODULE_DEPS ${ST_CMSIS} ${ST_HAL_DRIVER}) include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) diff --git a/ports/stm32f4/apps/self_update/CMakeLists.txt b/ports/stm32f4/apps/self_update/CMakeLists.txt index cede7969..1a1d54c4 100644 --- a/ports/stm32f4/apps/self_update/CMakeLists.txt +++ b/ports/stm32f4/apps/self_update/CMakeLists.txt @@ -12,8 +12,8 @@ add_custom_command(OUTPUT bootloader_bin.c # self_update target add_executable(self_update ${TOP}/apps/self_update/self_update.c - ${PORT_DIR}/boards.c - ${PORT_DIR}/board_flash.c + ${CMAKE_CURRENT_LIST_DIR}/../../boards.c + ${CMAKE_CURRENT_LIST_DIR}/../../board_flash.c ${CMAKE_CURRENT_BINARY_DIR}/bootloader_bin.c ) @@ -26,7 +26,7 @@ target_compile_definitions(self_update PUBLIC BUILD_APPLICATION ) target_link_options(self_update PUBLIC - "LINKER:--script=${PORT_DIR}/linker/stm32f4_app.ld" + "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/../../linker/stm32f4_app.ld" ) family_configure_common(self_update) diff --git a/ports/stm32f4/boards.h b/ports/stm32f4/boards.h index 0af82cd5..e899cde5 100644 --- a/ports/stm32f4/boards.h +++ b/ports/stm32f4/boards.h @@ -34,6 +34,8 @@ #include "board.h" +#define BOARD_FLASH_ADDR_ZERO 0x08000000 + // Flash Start Address of Application #ifndef BOARD_FLASH_APP_START #define BOARD_FLASH_APP_START 0x08010000 diff --git a/ports/stm32f4/family.cmake b/ports/stm32f4/family.cmake index 7346630c..37e13db2 100644 --- a/ports/stm32f4/family.cmake +++ b/ports/stm32f4/family.cmake @@ -8,7 +8,6 @@ set(UF2_FAMILY_ID 0x57755a57) set(ST_HAL_DRIVER ${TOP}/lib/st/stm32f4xx_hal_driver) set(ST_CMSIS ${TOP}/lib/st/cmsis_device_f4) set(CMSIS_5 ${TOP}/lib/CMSIS_5) -set(PORT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(FAMILY_SUBMODULE_DEPS ${ST_CMSIS} ${ST_HAL_DRIVER}) include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) @@ -54,7 +53,6 @@ function(family_add_board_target BOARD_TARGET) update_board(${BOARD_TARGET}) target_compile_definitions(${BOARD_TARGET} PUBLIC - CFG_TUSB_MCU=OPT_MCU_STM32F4 BOARD_UF2_FAMILY_ID=${UF2_FAMILY_ID} ) target_compile_options(${BOARD_TARGET} PUBLIC diff --git a/ports/stm32h7/boards.h b/ports/stm32h7/boards.h index 90878f9c..a3b1fb75 100644 --- a/ports/stm32h7/boards.h +++ b/ports/stm32h7/boards.h @@ -35,6 +35,9 @@ #include "board.h" +// depending on internal or external flash +// #define BOARD_FLASH_ADDR_ZERO 0x08000000 + // Flash Start Address of Application // #define BOARD_FLASH_APP_START 0x08010000 // #define BOARD_FLASH_APP_START 0x90000000 diff --git a/ports/stm32l4/boards.h b/ports/stm32l4/boards.h index f9875410..4c826a10 100644 --- a/ports/stm32l4/boards.h +++ b/ports/stm32l4/boards.h @@ -34,6 +34,8 @@ #include "board.h" +#define BOARD_FLASH_ADDR_ZERO 0x08000000 + // Flash Start Address of Application #ifndef BOARD_FLASH_APP_START #define BOARD_FLASH_APP_START 0x08010000 diff --git a/src/board_api.h b/src/board_api.h index 6e696939..e6d8bf35 100644 --- a/src/board_api.h +++ b/src/board_api.h @@ -34,6 +34,10 @@ // Features //--------------------------------------------------------------------+ +#ifndef BOARD_FLASH_ADDR_ZERO +#define BOARD_FLASH_ADDR_ZERO 0 +#endif + // Flash Start Address of Application #ifndef BOARD_FLASH_APP_START #define BOARD_FLASH_APP_START 0 diff --git a/src/ghostfat.c b/src/ghostfat.c index 9ced0815..46ca1fcd 100644 --- a/src/ghostfat.c +++ b/src/ghostfat.c @@ -452,7 +452,7 @@ void uf2_read_block (uint32_t block_no, uint8_t *data) { else { // CURRENT.UF2: generate data on-the-fly uint32_t addr = BOARD_FLASH_APP_START + (fileRelativeSector * UF2_FIRMWARE_BYTES_PER_SECTOR); - if ( addr < _flash_size ) /* TODO abstract this out */ { + if ( addr < (BOARD_FLASH_ADDR_ZERO + _flash_size) ) { UF2_Block *bl = (void*) data; bl->magicStart0 = UF2_MAGIC_START0; bl->magicStart1 = UF2_MAGIC_START1; From 9e2cb35a7bd5619a019d7c7d9b95a79c155ec277 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Dec 2023 15:59:30 +0700 Subject: [PATCH 5/5] add clion idea file --- .idea/cmake.xml | 18 +++++++++++++----- .idea/runConfigurations/imxrt1011.xml | 10 ++++++++++ .idea/runConfigurations/stm32f303vc.xml | 10 ++++++++++ .idea/runConfigurations/stm32f411ve.xml | 10 ++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 .idea/runConfigurations/imxrt1011.xml create mode 100644 .idea/runConfigurations/stm32f303vc.xml create mode 100644 .idea/runConfigurations/stm32f411ve.xml diff --git a/.idea/cmake.xml b/.idea/cmake.xml index 33078935..bb739a08 100644 --- a/.idea/cmake.xml +++ b/.idea/cmake.xml @@ -9,14 +9,21 @@ - + + + + + + + + - + @@ -30,7 +37,7 @@ - + @@ -44,10 +51,11 @@ - - + + + \ No newline at end of file diff --git a/.idea/runConfigurations/imxrt1011.xml b/.idea/runConfigurations/imxrt1011.xml new file mode 100644 index 00000000..2e29c211 --- /dev/null +++ b/.idea/runConfigurations/imxrt1011.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/stm32f303vc.xml b/.idea/runConfigurations/stm32f303vc.xml new file mode 100644 index 00000000..258e2c26 --- /dev/null +++ b/.idea/runConfigurations/stm32f303vc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/stm32f411ve.xml b/.idea/runConfigurations/stm32f411ve.xml new file mode 100644 index 00000000..73c5d670 --- /dev/null +++ b/.idea/runConfigurations/stm32f411ve.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file