From 29dfb9027625d82cd9c5221edde03ee45e0dc323 Mon Sep 17 00:00:00 2001 From: sunbeam Date: Wed, 21 May 2025 15:35:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 22 ++ .gitignore | 1 + .vscode/c_cpp_properties.json | 20 -- .vscode/iar-vsc.json | 18 ++ .vscode/launch.json | 26 ++ .vscode/settings.json | 10 + .vscode/tasks.json | 14 + CMakeLists.txt | 75 +++++ CMakePresets.json | 60 ++++ Core/Inc/stm32l4xx_it.h | 1 + Core/Src/syscalls.c | 176 ++++++++++++ Core/Src/sysmem.c | 79 ++++++ Drivers/Norflash/norflash.c | 2 + Drivers/Norflash/norflash.h | 3 +- Drivers/Norflash/qspi.c | 2 +- EWARM/Project.code-workspace | 8 + EWARM/Project.eww | 16 +- EWARM/bootloader.ewp | 4 +- STM32L431XX_FLASH.ld | 208 ++++++++++++++ asw/bootapp.c | 118 ++++++++ asw/bootapp.h | 38 +++ asw/uartapp.c | 8 +- asw/uartapp.h | 1 + cmake/gcc-arm-none-eabi.cmake | 45 +++ cmake/stm32cubemx/CMakeLists.txt | 95 +++++++ startup_stm32l431xx.s | 461 +++++++++++++++++++++++++++++++ 26 files changed, 1481 insertions(+), 30 deletions(-) create mode 100644 .clang-format delete mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/iar-vsc.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 Core/Src/syscalls.c create mode 100644 Core/Src/sysmem.c create mode 100644 EWARM/Project.code-workspace create mode 100644 STM32L431XX_FLASH.ld create mode 100644 asw/bootapp.c create mode 100644 asw/bootapp.h create mode 100644 cmake/gcc-arm-none-eabi.cmake create mode 100644 cmake/stm32cubemx/CMakeLists.txt create mode 100644 startup_stm32l431xx.s diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e92e7c6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,22 @@ +Language: C +BasedOnStyle: LLVM # 或 Google / WebKit / Chromium / GNU + +IndentWidth: 4 # 每层缩进使用4个空格 +TabWidth: 4 +UseTab: Never # 或者 ForIndentation / Always + +BreakBeforeBraces: Allman # 大括号换行风格,如 Allman/K&R/Stroustrup +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly + +ColumnLimit: 100 # 最大列宽,超出会换行(可设为 0 表示不限制) +AlignAfterOpenBracket: Align # 函数参数对齐风格 + +PointerAlignment: Left # 指针符号 * 左对齐,如 `int* ptr` vs `int *ptr` +SpaceBeforeParens: ControlStatements # 控制语句加空格,函数调用不加空格 + +IndentCaseLabels: true # switch-case 中 case 分支缩进 +IndentGotoLabels: true + +SortIncludes: false # 是否排序 include 头文件 diff --git a/.gitignore b/.gitignore index 0ffa286..c19975f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /EWARM/build /EWARM/debug /EWARM/settings +/build/ \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index c8e8753..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**", - "D:\\Program Files\\IAR Systems\\Embedded Workbench 9.2\\arm\\inc\\c\\" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE", - "USE_HAL_DRIVER", - "STM32L431xx", - "__ICCARM__" - ] - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/iar-vsc.json b/.vscode/iar-vsc.json new file mode 100644 index 0000000..7d1ae0b --- /dev/null +++ b/.vscode/iar-vsc.json @@ -0,0 +1,18 @@ +{ + "workspace": { + "path": "${workspaceFolder}\\EWARM\\Project.eww" + }, + "workspaces": { + "${workspaceFolder}\\EWARM\\Project.eww": { + "configs": { + "${workspaceFolder}\\EWARM\\bootloader.ewp": "bootloader" + }, + "selected": { + "path": "${workspaceFolder}\\EWARM\\bootloader.ewp" + } + } + }, + "workbench": { + "path": "d:\\Program Files\\IAR Systems\\Embedded Workbench 9.2" + } +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d58c20e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + //测试会debug,已废弃,直接Preferences: Open Keyboard Shortcuts (JSON) 绑定F5快捷键 + /* + { + "key": "f5", + "command": "workbench.action.tasks.runTask", + "args": "pyocd-flash", + "when": "editorTextFocus" + } + */ + "version": "0.0.1", + "configurations": [ + { + "cwd": "${workspaceFolder}", + "executable": "${workspaceFolder}/EWARM/debug/Exe/bootloader.hex", + "name": "only download", + "request": "launch", + "type": "cortex-debug", + "runToEntryPoint": "main", + "showDevDebugOutput": "none", + "servertype": "pyocd", + "preLaunchTask": "pyocd-flash", + "device": "STM32L431RCTx", // 根据芯片型号调整 + }, + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 84f305e..0cbdb7b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,15 @@ "stm32l431xx.h": "c", "core_cm4.h": "c", "system_stm32l4xx.h": "c" + }, + "iar-build.c-stat.showInToolbar": false, + "clangd.arguments": [ + "--compile-commands-dir=build/Debug" + ], + "files.watcherExclude": { + "**/build/**": true + }, + "search.exclude": { + "**/build/**": true } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..3018536 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,14 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "pyocd-flash", + "type": "shell", + "command": "pyocd flash --erase chip --target stm32l431rctx -f 10m .\\EWARM\\debug\\Exe\\bootloader.hex -W", + "group": "build", + "problemMatcher": [] + } + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..02d536e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.22) + +# +# This file is generated only once, +# and is not re-generated if converter is called multiple times. +# +# User is free to modify the file as much as necessary +# + +# Setup compiler settings +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS ON) + + +# Define the build type +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") +endif() + +# Set the project name +set(CMAKE_PROJECT_NAME bootloader) + +# Include toolchain file +include("cmake/gcc-arm-none-eabi.cmake") + +# Enable compile command to ease indexing with e.g. clangd +set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) + +# Core project settings +project(${CMAKE_PROJECT_NAME}) +message("Build type: " ${CMAKE_BUILD_TYPE}) + +# Enable CMake support for ASM and C languages +enable_language(C ASM) + +# Create an executable object type +add_executable(${CMAKE_PROJECT_NAME}) + +# Add STM32CubeMX generated sources +add_subdirectory(cmake/stm32cubemx) + +# Link directories setup +target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE + # Add user defined library search paths +) + +file(GLOB USER_SOURCES_ASW "${PROJECT_SOURCE_DIR}/asw/*.c") +file(GLOB USER_SOURCES_DRV "${PROJECT_SOURCE_DIR}/Drivers/Norflash/*.c") + +# Add sources to executable +target_sources(${CMAKE_PROJECT_NAME} PRIVATE + # Add user sources here + ${USER_SOURCES_ASW} + ${USER_SOURCES_DRV} +) + +# Add include paths +target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE + # Add user defined include paths + ${PROJECT_SOURCE_DIR}/asw/ + ${PROJECT_SOURCE_DIR}/Drivers/Norflash/ +) + +# Add project symbols (macros) +target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE + # Add user defined symbols +) + +# Add linked libraries +target_link_libraries(${CMAKE_PROJECT_NAME} + stm32cubemx + + # Add user defined libraries +) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..1ed4e19 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,60 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "default", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/build/${presetName}", + "toolchainFile": "${sourceDir}/cmake/gcc-arm-none-eabi.cmake", + "cacheVariables": { + } + }, + { + "name": "Debug", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "RelWithDebInfo", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "RelWithDebInfo" + } + }, + { + "name": "Release", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "MinSizeRel", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "MinSizeRel" + } + } + ], + "buildPresets": [ + { + "name": "Debug", + "configurePreset": "Debug" + }, + { + "name": "RelWithDebInfo", + "configurePreset": "RelWithDebInfo" + }, + { + "name": "Release", + "configurePreset": "Release" + }, + { + "name": "MinSizeRel", + "configurePreset": "MinSizeRel" + } + ] +} \ No newline at end of file diff --git a/Core/Inc/stm32l4xx_it.h b/Core/Inc/stm32l4xx_it.h index ecc9e07..34336d5 100644 --- a/Core/Inc/stm32l4xx_it.h +++ b/Core/Inc/stm32l4xx_it.h @@ -56,6 +56,7 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void DMA1_Channel5_IRQHandler(void); +void USART1_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/syscalls.c b/Core/Src/syscalls.c new file mode 100644 index 0000000..e33a849 --- /dev/null +++ b/Core/Src/syscalls.c @@ -0,0 +1,176 @@ +/** + ****************************************************************************** + * @file syscalls.c + * @author Auto-generated by STM32CubeMX + * @brief Minimal System calls file + * + * For more information about which c-functions + * need which of these lowlevel functions + * please consult the Newlib libc-manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2020-2024 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include +#include +#include +#include +#include +#include +#include + + +/* Variables */ +extern int __io_putchar(int ch) __attribute__((weak)); +extern int __io_getchar(void) __attribute__((weak)); + + +char *__env[1] = { 0 }; +char **environ = __env; + + +/* Functions */ +void initialise_monitor_handles() +{ +} + +int _getpid(void) +{ + return 1; +} + +int _kill(int pid, int sig) +{ + (void)pid; + (void)sig; + errno = EINVAL; + return -1; +} + +void _exit (int status) +{ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ +} + +__attribute__((weak)) int _read(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + *ptr++ = __io_getchar(); + } + + return len; +} + +__attribute__((weak)) int _write(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + __io_putchar(*ptr++); + } + return len; +} + +int _close(int file) +{ + (void)file; + return -1; +} + + +int _fstat(int file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _isatty(int file) +{ + (void)file; + return 1; +} + +int _lseek(int file, int ptr, int dir) +{ + (void)file; + (void)ptr; + (void)dir; + return 0; +} + +int _open(char *path, int flags, ...) +{ + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; +} + +int _wait(int *status) +{ + (void)status; + errno = ECHILD; + return -1; +} + +int _unlink(char *name) +{ + (void)name; + errno = ENOENT; + return -1; +} + +int _times(struct tms *buf) +{ + (void)buf; + return -1; +} + +int _stat(char *file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _link(char *old, char *new) +{ + (void)old; + (void)new; + errno = EMLINK; + return -1; +} + +int _fork(void) +{ + errno = EAGAIN; + return -1; +} + +int _execve(char *name, char **argv, char **env) +{ + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; +} diff --git a/Core/Src/sysmem.c b/Core/Src/sysmem.c new file mode 100644 index 0000000..246470e --- /dev/null +++ b/Core/Src/sysmem.c @@ -0,0 +1,79 @@ +/** + ****************************************************************************** + * @file sysmem.c + * @author Generated by STM32CubeMX + * @brief System Memory calls file + * + * For more information about which C functions + * need which of these lowlevel functions + * please consult the newlib libc manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include + +/** + * Pointer to the current high watermark of the heap usage + */ +static uint8_t *__sbrk_heap_end = NULL; + +/** + * @brief _sbrk() allocates memory to the newlib heap and is used by malloc + * and others from the C library + * + * @verbatim + * ############################################################################ + * # .data # .bss # newlib heap # MSP stack # + * # # # # Reserved by _Min_Stack_Size # + * ############################################################################ + * ^-- RAM start ^-- _end _estack, RAM end --^ + * @endverbatim + * + * This implementation starts allocating at the '_end' linker symbol + * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack + * The implementation considers '_estack' linker symbol to be RAM end + * NOTE: If the MSP stack, at any point during execution, grows larger than the + * reserved size, please increase the '_Min_Stack_Size'. + * + * @param incr Memory size + * @return Pointer to allocated memory + */ +void *_sbrk(ptrdiff_t incr) +{ + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; + + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) + { + __sbrk_heap_end = &_end; + } + + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) + { + errno = ENOMEM; + return (void *)-1; + } + + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; + + return (void *)prev_heap_end; +} diff --git a/Drivers/Norflash/norflash.c b/Drivers/Norflash/norflash.c index 8d2e92a..367dcbb 100644 --- a/Drivers/Norflash/norflash.c +++ b/Drivers/Norflash/norflash.c @@ -393,3 +393,5 @@ void norflash_test(void) } printf("\r\n"); } + + diff --git a/Drivers/Norflash/norflash.h b/Drivers/Norflash/norflash.h index 4e5127d..84e85a9 100644 --- a/Drivers/Norflash/norflash.h +++ b/Drivers/Norflash/norflash.h @@ -25,7 +25,7 @@ extern "C"{ #define EX_FLASH_SIZE 0x00800000 #define EX_FLASH_END_ADDR (EX_FLASH_START_ADDR+EX_FLASH_SIZE) - +#define EX_FLASH_CRC_ADDR (EX_FLASH_END_ADDR - 0X100) extern u16 NORFLASH_TYPE; //定义W25QXX芯片型号 ////////////////////////////////////////////////////////////////////////////////// @@ -83,6 +83,7 @@ void NORFLASH_QuitMemroyMapMode(void); void norflash_test(void); uint8_t NORFLASH_Check_Busy(void); + #ifdef __cplusplus } #endif // __cplusplus diff --git a/Drivers/Norflash/qspi.c b/Drivers/Norflash/qspi.c index ca5417e..ecc126a 100644 --- a/Drivers/Norflash/qspi.c +++ b/Drivers/Norflash/qspi.c @@ -72,7 +72,7 @@ void QSPI_Send_CMD(u8 cmd,u32 addr,u8 mode,u8 dmcycle) //buf:接收数据缓冲区首地址 //datalen:要传输的数据长度 //返回值:0,正常 -// 其他,错误代码 +// 其他,错误代码 u8 QSPI_Receive(u8* buf,u32 datalen) { u32 tempreg=QUADSPI->CCR; diff --git a/EWARM/Project.code-workspace b/EWARM/Project.code-workspace new file mode 100644 index 0000000..1002e6b --- /dev/null +++ b/EWARM/Project.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "name": "bootloader", + "path": "bootloader" + } + ] +} \ No newline at end of file diff --git a/EWARM/Project.eww b/EWARM/Project.eww index 3b3f706..f4be637 100644 --- a/EWARM/Project.eww +++ b/EWARM/Project.eww @@ -1,7 +1,15 @@ - - $WS_DIR$\bootloader.ewp - - + + $WS_DIR$\bootloader.ewp + + + + test + + bootloader + bootloader + + + diff --git a/EWARM/bootloader.ewp b/EWARM/bootloader.ewp index a2546b0..c08e842 100644 --- a/EWARM/bootloader.ewp +++ b/EWARM/bootloader.ewp @@ -236,7 +236,7 @@