# Bootloader **Repository Path**: m163/bootloader ## Basic Information - **Project Name**: Bootloader - **Description**: 自制的Bootloader用于使用串口实现OTA升级 - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2025-04-30 - **Last Updated**: 2025-04-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Bootloader 此项目Bootloader实现了串口OTA,占用极小资源(6KB ROM & <6KB RAM) 内置串口DEBUG和LOG可选输出,具有灵活,小巧的特点。 ![Bootloader功能框图](images/bootloader.png) ## ARM中设置APP的FLASH 偏移地址 1. **Options 中的 Start ROM 偏移地址**变成对应的烧录 FLASH 地址。 2. **设置 `system_.c` 中的 `VECT_TAB_OFFSET`**变为对应的偏移地址。 3. **修改 `hex_pares.c` 中的 `void before_main()`**其中的 `NVIC_SetVectorTable(NVIC_VectTab_FLASH,` 对应的偏移地址。 4. **如果芯片的FLASH有变动可以修改 `hex_parse.h`中的内存块即可** 例如128K的FLASH直接修改为 ```c // 定义驱动表(内存块信息) static const FlashBlockInfo FlashBlockTable[] = { {16 * 1024, 0x00000000}, // Sector 0: 16KB --->BOOTLOADER所在位置 {16 * 1024, 0x00004000}, // Sector 1: 16KB --->BOOTLOADER信息 {16 * 1024, 0x00008000}, // Sector 2: 16KB - {16 * 1024, 0x0000C000}, // Sector 3: 16KB | {64 * 1024, 0x00010000}, // Sector 4: 64KB -- 用户程序 // {128 * 1024, 0x00020000}, // Sector 5: 128KB | // {128 * 1024, 0x00040000}, // Sector 6: 128KB | // {128 * 1024, 0x00060000}, // Sector 7: 128KB - }; ``` ## RISE-V中设置APP的FLASH 偏移地址 1. 修改链接文件的FLASH起始地址 ![img](images/ld.png) 修改 ```c MEMORY { FLASH (rx) : ORIGIN = 0x00006000, LENGTH = 232K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K } ``` 2. 修改APP的工程中跳转BOOTLOADER信息对应偏移存储的地址,通常偏移4K 3. 如果芯片的FLASH有变动可以修改 `hex_parse.h`中的内存块即可 ```c // 定义驱动表(内存块信息) static const uint16_t FlashBlockTable[] = { 4096, 4096, 4096, 4096, 4096, //--Bootloader内存 4096, //-----Bootloader信息 4096, 4096, 4096, 4096, 4096, //APP 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, ...... ``` ## Bootloader 使用说明 ### 0. Bootloader 无论裸机/OS 都可直接使用 ### 1. APP 端使用裸机 直接复制 `NoOS` 文件夹中即可。 ### 2. APP 端使用 RT-Thread 如果 APP 使用的是 RT-Thread 注意: 由于 RT-Thread 有在 `main` 函数运行之前的机制,在源码中代码是 (`component.c`): ```c /* re-define main function */ int $Sub$$main(void) { rtthread_startup(); return 0; } ``` 但是本 bootloader 的 APP 端也是使用在 `main` 函数之前 (`__attribute__((constructor)) void before_main(void)`),会导致矛盾,故需要修改 APP 端的 bootloader 的代码: 1. **将 `__attribute__((constructor)) void before_main(void)` 变为 `void before_main(void)`** 2. **在 RT-Thread (`component.c`) 前加入** ```c int rtthread_startup(void) { extern void before_main(void); before_main(); // 初始化 BOOTLOADER ... } ``` 3. 然后就可以正常使用其他功能了。 ### 注意 本 bootloader 的 APP 端的函数 `void before_main(void)` 必须在所有函数包括 RTT 的初始化函数之前,主要用于打开中断和重定位向量表! ### 在 SHELL 界面 如果使用的是 RT-Thread 中的 FINSH,则可以参考在文件夹 `rt-thread/main` 中的 `main.c` 文件。