{"id":32,"date":"2025-05-08T20:50:25","date_gmt":"2025-05-08T12:50:25","guid":{"rendered":"http:\/\/kysdfh.xyz\/?p=32"},"modified":"2025-05-08T20:50:25","modified_gmt":"2025-05-08T12:50:25","slug":"stm32-bootloader-cortex-m3-%e6%96%b0%e6%89%8b%e5%90%91","status":"publish","type":"post","link":"http:\/\/kysdfh.xyz\/index.php\/2025\/05\/08\/stm32-bootloader-cortex-m3-%e6%96%b0%e6%89%8b%e5%90%91\/","title":{"rendered":"STM32 BootLoader (Cortex-M3) \u65b0\u624b\u5411"},"content":{"rendered":"\n<p><strong>\u8bfb\u524d\u987b\u77e5:<\/strong><\/p>\n\n\n\n<p><strong>PC : Win10<\/strong><\/p>\n\n\n\n<p><strong>Keil5 Version :5.30<\/strong><\/p>\n\n\n\n<p><strong>STM32CubeMX Version:6.2.1<\/strong><\/p>\n\n\n\n<p><strong>\u82af\u7247 : STM32F103ZET6<\/strong><\/p>\n\n\n\n<p>\u4e0d\u6307\u5b9a \u677f\u5b50 , M3 \u82af\u7247 \u90fd\u53ef , \u5728\u8fd9\u91cc \u4f7f\u7528 HAL \u5e93 ,\u6807\u51c6\u5e93\u4e0d\u505a\u8bf4\u660e.<\/p>\n\n\n\n<p><strong>M0 \u5185\u6838 \u6ce8\u610f ,M0 \u7684\u82af\u7247 \u5185\u90e8 \u6ca1\u6709 \u5411\u91cf\u8868 \u504f\u79fb\u5bc4\u5b58\u5668 VTOR ,\u9700\u8981\u624b\u52a8 \u4fee\u6539 \u542f\u52a8\u6587\u4ef6\u5b9e\u73b0(xxx.s)<\/strong><\/p>\n\n\n\n<p><strong>\u7279\u6b8a\u8bf4\u660e:<\/strong>(\u7528\u4f5c\u4e86\u89e3)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.\u4e3a\u4e86 \u6b63\u5e38 \u6267\u884c \u6211\u4eec\u7684\u4ee3\u7801 ,\u6211\u4eec\u5148 \u5bf9 \u677f\u5b50\u7684 <strong>\u542f\u52a8 \u65b9\u5f0f<\/strong>\u8fdb\u884c\u68c0\u67e5<\/h4>\n\n\n\n\n\n<p>\u9009\u62e9: <strong>BOOT1 = 0 , BOOT0 = 0<\/strong> ( \u4e3b\u95ea\u5b58\u5b58\u50a8\u5668 )(ref :STM32\u4e2d\u6587\u53c2\u8003\u624b\u518c_V10 ,P33)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.\u4e3a\u4e86\u6b63\u5e38\u914d\u7f6e Boot \u5206\u533a \u6211\u4eec\u9700\u8981\u67e5\u9700\u8981 \u4e00\u4e0b Datasheet\u4e2d\u7684 \u95ea\u5b58 \u7ec4\u7ec7 ( \u4ee5\u4e0b\u662f \u6211\u6240\u4f7f\u7528\u7684\u82af\u7247\u5bf9\u5e94\u7684\u95ea\u5b58\u7ec4\u7ec7 )<\/h4>\n\n\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2aPage \u662f 2K \u5b57\u8282 \u5927\u5c0f ,\u5171 255+1 \u9875 , \u603b Flash \u5927\u5c0f :512K . \u7528\u6765\u786e\u5b9a Boot\u5206\u533a\u5927\u5c0f (\u4e0d\u4ea7\u751f\u51b2\u7a81\u7684\u60c5\u51b5\u4e0b)(ref:TM32\u4e2d\u6587\u53c2\u8003\u624b\u518c_V10 , P30 )<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.\u5411\u91cf\u8868\u9009\u62e9\u4f4d\u7f6e<\/h4>\n\n\n\n\n\n<p>\u6211\u4eec\u9009\u5b9a\u7684\u7684\u5730\u5740 \u5fc5\u9700\u662f <strong>\u53ef\u4ee5\u88ab 64 * 4 (\u6bcf\u4e2a\u5411\u91cf \u5360\u7528 \u4e00\u4e2a Word ,\u4e5f\u5c31\u662f 4 \u5b57\u8282,\u6240\u4ee5 \u6211\u4eec\u53ef\u77e5 \u4e00\u4e2a\u5411\u91cf\u8868 \u5360\u7528 256 \u5b57\u8282 ) \u6574\u9664<\/strong> \u7684\u5730\u5740 .<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.\u90e8\u5206 \u77e5\u8bc6 :<\/h4>\n\n\n\n<p>Boot Loader ( \u5f15\u5bfc \u52a0\u8f7d ) \u662f \u8fd9\u4e2a \u5355\u8bcd\u7684 \u542b\u4e49 . Bootloader \u4ee3\u7801 \u6240\u4ee5 \u6211\u4eec\u4e00\u822c \u53ef\u4ee5 \u79f0\u4e4b\u4e3a \u5f15\u5bfc\u52a0\u8f7d \u7a0b\u5e8f.<\/p>\n\n\n\n<p>\u5173\u4e8e Bootloader \u7531\u6765 ,\u4e92\u8054\u7f51\u4e0a \u6709\u7740 \u8bb8\u591a \u6587\u732e ,\u81ea\u884c \u67e5\u627e ( \u60f3\u8981\u4e86\u89e3\u7684)<\/p>\n\n\n\n<p>\u4ee5\u53ca \u5173\u4e8e 0x0800 0000 \u4f5c\u4e3a \u7a0b\u5e8f \u5f00\u59cb\u7684 \u7531\u6765<\/p>\n\n\n\n\n\n<p>BootLoader \u6d41\u7a0b :<\/p>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\">Url:<br>https:\/\/bbs.huaweicloud.com\/blogs\/233359<\/pre>\n\n\n\n<p>\u6211\u4eec\u7b80\u5355 \u8bb2\u8ff0 \u4e00\u4e0b .\u4ee5 \u6587\u672c\u7684\u65b9\u5f0f(\u6c34\u5e73\u6709\u9650, \u5982\u6709\u9519\u8bef\u8bf7\u6279\u8bc4\u6307\u6b63):<\/p>\n\n\n\n<p>1.(BOOT0 = 0 ,BOOT1 = X )\u82af\u7247 \u4e0a\u7535 ,cpu \u4ece\u6808\u9876 \u83b7\u53d6 \u4e3b\u7a0b\u5e8f \u7684(0x0000_0000) \u5806\u6808\u5730\u5740(MSP Main Stack Pointer) ,\u7136\u540e\u8df3\u5230 Reset Handler \u5f02\u5e38 \u6267\u884c (0x0000_0004), \u8fd9\u4e2a\u4f7f\u7528 \u4e3b\u95ea\u5b58\u6620\u5c04(0x0800_0000)\u5230 0x0000_0000\u8fd9\u4e2a\u5730\u5740,\u90a3\u4e48 ,\u4e0a\u7535\u6267\u884c\u5c31\u662f 0x0800_0000 \u91cc\u9762\u7684\u5185\u5bb9 ,\u4e4b\u540e\u5c31\u6267\u884c Reset Handler(0x0800_0004) \u5f02\u5e38\u6267\u884c\u7684\u51fd\u6570.( <strong>DCD<\/strong> \u662f \u4e00\u4e2a \u5b57 \u572832 \u4e2d \u4ee3\u88684\u5b57\u8282\u5927\u5c0f )<\/p>\n\n\n\n\n\n<p>\u8fd9\u662f Reset Handler \u4e2d\u7684\u7a0b\u5e8f ,\u52a0\u8f7d ,\u8df3\u8f6c\u6267\u884c ,\u8fd4\u56de ,\u52a0\u8f7d _main \u51fd\u6570 ,\u7136\u540e ,\u65e0\u8fd4\u56de\u503c\u7684\u8df3\u8f6c\u5230 __main \u51fd\u6570\u4e2d .\u5373\u8df3\u5165 \u4f60\u5728main \u51fd\u6570\u4e2d\u5199\u7684\u4ee3\u7801<\/p>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/__Main \u6267\u884c\u4e86\u4ec0\u4e48<\/pre>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/Doc <br>\/\/Url : https:\/\/developer.arm.com\/documentation\/100748\/0618\/Embedded-Software-Development\/Application-startup<br>\/\/\u56fd\u5185\u53ef\u80fd\u8bbf\u95ee\u4e0d\u4e86<br>\/\/ \u66f4\u8be6\u7ec6 \u4e00\u70b9 \u5728  &lt;&lt; Arm Compiler C Library Startup and Initialization &gt;&gt; (Arm \u7f16\u8bd1\u5668 C \u5e93\u7684\u542f\u52a8\u548c\u521d\u59cb\u5316)<br>\/\/url:https:\/\/developer.arm.com\/documentation\/dai0241\/latest<\/pre>\n\n\n\n\n\n<p>\u56e0\u4e3a\u8981\u4f7f\u7528 C \u8bed\u8a00 ,\u8fd9\u4e9b\u90fd\u662f \u6267\u884c C\u8bed\u8a00 \u6240\u9700\u8981\u7684 \u73af\u5883\u8bbe\u7f6e,(\u4e5f\u53ef\u4ee5\u66f4\u6539\u81ea\u5df1\u7684__main \u51fd\u6570 )<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u5f0f\u8fdb\u5165 Bootloader \u7684\u7f16\u5199 (\u7b14\u8005 \u4f7f\u7528 J-Link \u4e0b\u8f7d\u5668 ,\u4f7f\u7528 \u5176\u4ed6\u7684 ,\u5728\u4e0b\u8f7d\u5668\u8bbe\u7f6e\u7684\u4f4d\u7f6e \u81ea\u884c\u8bbe\u7f6e)<\/h4>\n\n\n\n<p><strong>(\u8fd9\u91cc\u518d\u6b21\u8bf4\u660e,\u672c\u6b21\u4f7f\u7528\u7684 \u662f Cortex-M3 \u7684\u5904\u7406\u5668 , \u8fd9\u4e2a\u5904\u7406\u5668 \u662f\u6709 \u5411\u91cf\u504f\u79fb\u5bc4\u5b58\u5668\u7684, \u5730\u5740 :0xE000_ED08,\u597d\u5904\u5c31\u662f\u6211\u4eec\u4e0d\u9700\u8981,\u5728\u6267\u884c \u5e94\u7528\u7a0b\u5e8f\u65f6 ,\u518d\u6b21\u5728 \u5e94\u7528\u7a0b\u5e8f flash \u5934 \u524d \u91cd\u5199\u4e00\u904d \u8fd9\u4e9b\u5f02\u5e38 handler \u7684\u6267\u884c\u5730\u5740)<\/strong><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u9879\u76ee\u914d\u7f6e<\/h5>\n\n\n\n<p>\u521b\u5efa\u9879\u76ee<\/p>\n\n\n\n<p>\u2460 \u9009\u62e9\u82af\u7247<\/p>\n\n\n\n\n\n<p>\u2461\u914d\u7f6e\u65f6\u949f (\u5916\u90e8\u9ad8\u901f\u65f6\u949f,\u4f7f\u7528\u6676\u632f)<\/p>\n\n\n\n\n\n<p>\u2462\u914d\u7f6e\u4e0b\u8f7d\u53e3(\u6211\u8fd9\u91cc\u65f6J-Link,\u6211\u914d\u7f6e \u56db\u53e3 ,\u5982\u679c\u4f60\u4eec\u65f6 DAP ,\u6216\u8005\u662f STLink ,\u53ef\u4ee5 Serial Wire )<\/p>\n\n\n\n<p><strong>J-LINK :<\/strong><\/p>\n\n\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>DAP,ST-LINK:<\/strong><\/p>\n\n\n\n\n\n<p>\u2463 \u914d\u7f6e \u81ea\u5df1\u7684 LED \u5f15\u811a:<\/p>\n\n\n\n\n\n<p>\u2464\u914d\u7f6e\u65f6\u949f\u6811<\/p>\n\n\n\n\n\n<p>\u2465 \u914d\u7f6e\u9879\u76ee\u751f\u6210\u8def\u5f84 ,\u4ee5\u53ca\u9879\u76ee\u9700\u8981\u751f\u6210\u7684\u4f9d\u8d56<\/p>\n\n\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n\n\n<p>\u2466 \u751f\u6210 \u5e76 \u6253\u5f00 \u9879\u76ee<\/p>\n\n\n\n\n\n<p>\u9879\u76ee\u4e2d\u4ee3\u7801<\/p>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\">\/* Private user code ---------------------------------------------------------*\/<br>\/* USER CODE BEGIN 0 *\/<br>uint32_t JumpAddress &nbsp;= 0; \/\/ <br>\u200b<br>typedef void ( *pFunction )(void); &nbsp;\/\/ <br>\u200b<br>pFunction JumpToApplication; <br>\u200b<br>#define FLASH_BASE_ADDRESS  (( uint32_t ) 0x8000000)<br>#define BOOTLOADER_OFFSET &nbsp; (( uint32_t ) 0x0004000)<br>#define VERTION_CONTROL &nbsp; &nbsp; (( uint32_t ) 0x0001000)<br>#define APPLICATION_ADDRESS  VERTION_CONTROL+BOOTLOADER_OFFSET+FLASH_BASE_ADDRESS\/\/<br>\u200b<br>unsigned char ucJumpToAppcationFlag = 0;<br>\u200b<br>\u200b<br>\/* USER CODE END 0 *\/<\/pre>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;\/* USER CODE BEGIN 1 *\/<br>    unsigned char ucI = 0;<br> &nbsp;\/* USER CODE END 1 *\/<\/pre>\n\n\n\n<p>\u5728 Main \u51fd\u6570\u7684 while \u7684 <strong>\/* USER CODE BEGIN 3 *\/<\/strong> \u5faa\u73af\u4e2d\u5199\u5165<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;\/* USER CODE END WHILE *\/<br>    <br> &nbsp; &nbsp;\/* USER CODE BEGIN 3 *\/<br>        <br>        HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5);<br>        <br>        HAL_Delay(300);<br>        <br>        if( ucJumpToAppcationFlag == 1)<br>        {<br>            ucJumpToAppcationFlag = 0;<br>        <br>            if (((*(__IO uint32_t*)FLASH_BASE_ADDRESS) &amp; 0x2FFE0000 ) == 0x20000000) \/\/ <br>            {<br>                <br>                JumpAddress = *(__IO uint32_t* ) (APPLICATION_ADDRESS + 4 ); \/\/ <br>                <br>                JumpToApplication = (pFunction) JumpAddress; \/\/ <br>                __set_MSP(*(__IO uint32_t *) APPLICATION_ADDRESS); \/\/ <br>                __disable_irq(); &nbsp;<br>                <br>            <br>                JumpToApplication();<br>                    <br>\u200b<br>            <br>            }<br>        }<br>        if(ucI &gt; 30) ucJumpToAppcationFlag = 1 ;<br>        ucI++;<br>        <br>        <br>\u200b<\/pre>\n\n\n\n\n\n<h5 class=\"wp-block-heading\">\u6b65\u9aa4 1<\/h5>\n\n\n\n<p>\u6253\u5f00\u9b54\u6cd5\u68d2<\/p>\n\n\n\n\n\n\n\n<p>\u8bbe\u7f6e \u5bf9\u5e94 \u7a0b\u5e8f \u5b58\u50a8 \u7684 ROM\u5730\u5740 ,RAM \u4e0d\u9700\u8981\u8c03\u6574 \u9ed8\u8ba4 .<\/p>\n\n\n\n<p>\u56e0\u4e3a Bootloader \u662f \u9ed8\u8ba4 \u4e0a\u7535\u6267\u884c\u7684\u7a0b\u5e8f BOOT0 = 0 \u65f6.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6b65\u9aa4 2<\/h5>\n\n\n\n\n\n<pre class=\"wp-block-preformatted\">fromelf --bin -o \".\\bin_file\\Bootloader.bin\" \"#L\"<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u5730\u65b9\u65f6\u5199\u5165 \u4e0a\u56fe\u7684 \u6267\u884c\u6307\u4ee4 ( <strong>\u5728\u7f16\u8bd1\u4e4b\u540e\u8fd0\u884c<\/strong> )<\/p>\n\n\n\n<p>\u5c31\u4f1a\u5728\u4f60\u7684 \u9879\u76ee \u5730\u5740 &#8211; &gt; MDK-ARM &#8211; &gt; bin_file \u6587\u4ef6\u5939\u4e2d\u751f\u6210\u4f60\u8bbe\u5b9a\u7684\u540d\u5b57\u7684bin \u6587\u4ef6<\/p>\n\n\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u4e86 BootLoader \u7684\u6587\u4ef6 \u5927\u5c0f \u5c31\u53ef\u4ee5\u8bbe\u7f6e \u5bf9\u5e94 \u7a0b\u5e8f \u5b58\u653e \u5730\u5740\u4e86<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6b65\u9aa43<\/h5>\n\n\n\n<p>\u4e0b\u8f7d\u5668\u8bbe\u7f6e<\/p>\n\n\n\n\n\n<p>\u81f3\u6b64,Bootloader \u7684\u9879\u76ee\u73af\u5883 \u914d\u7f6e\u5b8c\u6210<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6211\u4eec\u53ef\u4ee5 \u91cd\u65b0\u751f\u6210\u4e00\u4efd\u65b0\u7684\u4e5f\u53ef\u4ee5 ,\u590d\u5236\u73b0\u5728\u7684\u9879\u76ee \u7528\u4f5c Application \u9879\u76ee ,\u63a8\u8350\u91cd\u65b0\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u9879\u76ee.\u8fd9\u6837\u4e24\u4e2a\u9879\u76ee\u5c31\u6ca1\u6709\u91cd\u53e0\u7684\u5730\u65b9 \u72ec\u7acb\u8bbe\u7f6e(\u653e\u5728boot loader \u8bbe\u7f6e\u4e86\u7684\u4e1c\u897f ,\u4f60\u5fd8\u8bb0\u6539\u56de\u6765)<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">Application \u9879\u76ee\u7684\u73af\u5883\u8bbe\u7f6e<\/h5>\n\n\n\n<p>\u9664\u4e86 Bootloader \u4e2d\u7684\u6b65\u9aa4 1 \u4e0d\u540c,\u5176\u4ed6 \u4fee\u6539 \u5982\u4e0a<\/p>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f :<\/p>\n\n\n\n<p><strong>\u4e00\u5b9a\u8981\u770b\u4f60\u7684Bootloader \u7684\u5927\u5c0f , \u7136\u540e\u5212\u5206\u51fa\u5927\u4e8e\u4f60Bootloader \u7a0b\u5e8f\u7684 Flash \u7ed9 Bootloader \u5b58\u50a8<\/strong><\/p>\n\n\n\n<p>\u5e76\u4e14 \u7a0b\u5e8f \u7684 \u9996\u5730\u5740 \u4e00\u5b9a\u8981\u7b26\u5408 <strong>X \/ 254 = ( Int)<\/strong> \u662f \u4e2a\u6574\u6570 , \u539f\u56e0 \u5728\u8fd9\u91cc \u8bb2\u6e05\u9664 \u4e86 <strong>&#8212;-&gt; 3.\u5411\u91cf\u8868\u9009\u62e9\u4f4d\u7f6e<\/strong><\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 1 \u4fee\u6539\u6210 \u4e0b\u9762\u7684\u503c<\/strong><\/p>\n\n\n\n\n\n<p>Application \u7684\u7a0b\u5e8f :<\/p>\n\n\n\n\n\n<p>\u81f3\u6b64 \u5206\u522b\u628a\u4ed6\u4eec\u70e7\u5f55\u8fdb\u53bb\u5c31\u53ef\u4ee5\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Bootloader Main \u51fd\u6570 \u4ee3\u7801<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">#include \"main.h\"<br>#include \"gpio.h\"<br>\u200b<br>\/* Private includes ----------------------------------------------------------*\/<br>\/* USER CODE BEGIN Includes *\/<br>\u200b<br>\/* USER CODE END Includes *\/<br>\u200b<br>\/* Private typedef -----------------------------------------------------------*\/<br>\/* USER CODE BEGIN PTD *\/<br>\u200b<br>\/* USER CODE END PTD *\/<br>\u200b<br>\/* Private define ------------------------------------------------------------*\/<br>\/* USER CODE BEGIN PD *\/<br>\/* USER CODE END PD *\/<br>\u200b<br>\/* Private macro -------------------------------------------------------------*\/<br>\/* USER CODE BEGIN PM *\/<br>\u200b<br>\/* USER CODE END PM *\/<br>\u200b<br>\/* Private variables ---------------------------------------------------------*\/<br>\u200b<br>\/* USER CODE BEGIN PV *\/<br>\u200b<br>\/* USER CODE END PV *\/<br>\u200b<br>\/* Private function prototypes -----------------------------------------------*\/<br>void SystemClock_Config(void);<br>\/* USER CODE BEGIN PFP *\/<br>\u200b<br>\/* USER CODE END PFP *\/<br>\u200b<br>\/* Private user code ---------------------------------------------------------*\/<br>\/* USER CODE BEGIN 0 *\/<br>uint32_t JumpAddress &nbsp;= 0; \/\/ <br>\u200b<br>typedef void ( *pFunction )(void); &nbsp;\/\/ \u51fd\u6570 \u6307\u9488<br>\u200b<br>pFunction JumpToApplication; &nbsp;\/\/\u5b9a\u4e49\u4e00\u4e2a \u51fd\u6570\u6307\u9488 \u53d8\u91cf<br>\u200b<br>#define FLASH_BASE_ADDRESS  (( uint32_t ) 0x8000000) &nbsp;\/\/ Flash \u9996\u5730\u5740 (\u7528\u6765\u5b58\u653e Bootloader )<br>#define BOOTLOADER_OFFSET &nbsp; (( uint32_t ) 0x0001000) &nbsp;\/\/ bootloader \u7684\u5927\u5c0f<br>#define VERTION_CONTROL &nbsp; &nbsp; (( uint32_t ) 0x0000000) &nbsp;\/\/ \u7ed9\u4ea7\u54c1 ,\u4ee5\u53ca \u5347\u7ea7\u505a \u8bb0\u5f55\u7684\u533a\u57df <br>#define APPLICATION_ADDRESS  VERTION_CONTROL+BOOTLOADER_OFFSET+FLASH_BASE_ADDRESS \/\/ \u4e3b\u7a0b\u5e8f \u5f00\u59cb\u5730\u5740<br>\u200b<br>unsigned char ucJumpToAppcationFlag = 0; \/\/\u7528\u6765\u505a \u5ef6\u8fdf \u8fdb\u5165\u4e3b\u7a0b\u5e8f\u7684\u53d8\u91cf , <br>\/\/ \u5728\u5b9e\u9645\u5f00\u53d1\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528 flash \u6765\u5b58\u50a8<br>\/**<br>*uint32_t BOOTLOADER_UPLOADING __attribute__((aligned(4), at(BOOTLOADER_OFFSET + FLASH_BASE_ADDRESS + 0))) = 0; \/\/ \u5b57\u8282\u5bf9\u9f50 <br>**\/<br>\/* USER CODE END 0 *\/<br>\u200b<br>\/**<br> &nbsp;* @brief  The application entry point.<br> &nbsp;* @retval int<br> &nbsp;*\/<br>int main(void)<br>{<br> &nbsp;\/* USER CODE BEGIN 1 *\/<br>    unsigned char ucI = 0;<br> &nbsp;\/* USER CODE END 1 *\/<br>\u200b<br> &nbsp;\/* MCU Configuration--------------------------------------------------------*\/<br>\u200b<br> &nbsp;\/* Reset of all peripherals, Initializes the Flash interface and the Systick. *\/<br> &nbsp;HAL_Init();<br>\u200b<br> &nbsp;\/* USER CODE BEGIN Init *\/<br>\u200b<br> &nbsp;\/* USER CODE END Init *\/<br>\u200b<br> &nbsp;\/* Configure the system clock *\/<br> &nbsp;SystemClock_Config();<br>\u200b<br> &nbsp;\/* USER CODE BEGIN SysInit *\/<br>\u200b<br> &nbsp;\/* USER CODE END SysInit *\/<br>\u200b<br> &nbsp;\/* Initialize all configured peripherals *\/<br> &nbsp;MX_GPIO_Init();<br> &nbsp;\/* USER CODE BEGIN 2 *\/<br>\u200b<br> &nbsp;\/* USER CODE END 2 *\/<br>\u200b<br> &nbsp;\/* Infinite loop *\/<br> &nbsp;\/* USER CODE BEGIN WHILE *\/<br> &nbsp;while (1)<br>  {<br> &nbsp; &nbsp;\/* USER CODE END WHILE *\/<br>    <br> &nbsp; &nbsp;\/* USER CODE BEGIN 3 *\/<br>        <br>        HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5);<br>        <br>        HAL_Delay(300);<br>        <br>        if( ucJumpToAppcationFlag == 1)<br>        {<br>            ucJumpToAppcationFlag = 0;<br>        <br>            if (((*(__IO uint32_t*)FLASH_BASE_ADDRESS) &amp; 0x2FFE0000 ) == 0x20000000) \/\/ \u5224\u65ad\u5806\u6808\u6307\u9488\u662f\u5426\u5728 \u4e3b \u5806\u6808<br>            {<br>                <br>                JumpAddress = *(__IO uint32_t* ) (APPLICATION_ADDRESS + 4 ); \/\/  \u83b7\u53d6 Reset handler \u7684\u6267\u884c\u5730\u5740<br>                <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/(__IO uint32_t* ) (APPLICATION_ADDRESS + 4 ) -- &gt;\u8fd9\u4e2a \u8bf4\u7684\u662f \u5c06\u5b83 \u5f3a\u5236 \u8f6c\u6210 __IO uint32_t* \u53d8\u91cf , __IO  \u4e00\u4e2a\u91cd\u5b9a\u4e49volatile ,\u544a\u8bc9\u7f16\u8bd1\u5668 \u4e0d\u9700\u8981 \u4f18\u5316 \u6211\u8fd9\u4e2a\u53d8\u91cf. \u5bf9\u4e8e\u654f\u611f\u7684 \u53d8\u91cf ,\u4e00\u5b9a\u8981 \u7528 volatile .<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ *(__IO uint32_t* ) (APPLICATION_ADDRESS + 4 ) \u5c31\u662f \u53d6 \u8fd9\u4e2a\u5730\u5740 \u4e2d\u7684 \u503c <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>                JumpToApplication = (pFunction) JumpAddress; \/\/ &nbsp;<br>                __set_MSP(*(__IO uint32_t *) APPLICATION_ADDRESS); \/\/ \u8bbe\u7f6e\u5806\u6808\u6307\u9488 ,\u6211\u4eec\u4ece .s \u6587\u4ef6\u4e2d ,\u5c31\u77e5\u9053 \u7a0b\u5e8f\u7684 \u9996\u5730\u5740 \u5c31\u662f \u5806\u6808\u9876 \u5730\u5740<br>                __disable_irq(); &nbsp;\/\/ \u5173\u95ed\u4e2d\u65ad <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fd9\u91cc\u5176\u5b9e\u8fd8\u8981 \u6e05\u9664\u4e00\u4e0b \u4e2d\u65ad\u6807\u5fd7\u4f4d ,\u9632\u6b62 \u540e\u7eed \u89e6\u53d1<br>                <br>            <br>                JumpToApplication(); \/\/\u8df3\u8f6c\u5230 Application<br>                    <br>\u200b<br>            <br>            }<br>        }<br>        if(ucI &gt; 30) ucJumpToAppcationFlag = 1 ;<br>        ucI++;<br>        <br>        <br>  }<br> &nbsp;\/* USER CODE END 3 *\/<br>}<br>\u200b<br>\/**<br> &nbsp;* @brief System Clock Configuration<br> &nbsp;* @retval None<br> &nbsp;*\/<br>void SystemClock_Config(void)<br>{<br> &nbsp;RCC_OscInitTypeDef RCC_OscInitStruct = {0};<br> &nbsp;RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};<br>\u200b<br> &nbsp;\/** Initializes the RCC Oscillators according to the specified parameters<br> &nbsp;* in the RCC_OscInitTypeDef structure.<br> &nbsp;*\/<br> &nbsp;RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;<br> &nbsp;RCC_OscInitStruct.HSEState = RCC_HSE_ON;<br> &nbsp;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;<br> &nbsp;RCC_OscInitStruct.HSIState = RCC_HSI_ON;<br> &nbsp;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;<br> &nbsp;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;<br> &nbsp;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;<br> &nbsp;if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) != HAL_OK)<br>  {<br> &nbsp; &nbsp;Error_Handler();<br>  }<br> &nbsp;\/** Initializes the CPU, AHB and APB buses clocks<br> &nbsp;*\/<br> &nbsp;RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;<br> &nbsp;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;<br> &nbsp;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;<br> &nbsp;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;<br> &nbsp;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;<br>\u200b<br> &nbsp;if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)<br>  {<br> &nbsp; &nbsp;Error_Handler();<br>  }<br>}<br>\u200b<br>\/* USER CODE BEGIN 4 *\/<br>\u200b<br>\/* USER CODE END 4 *\/<br>\u200b<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/Application Main \u51fd\u6570<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">#include \"main.h\"<br>#include \"gpio.h\"<br>\u200b<br>\/* Private includes ----------------------------------------------------------*\/<br>\/* USER CODE BEGIN Includes *\/<br>\u200b<br>\/* USER CODE END Includes *\/<br>\u200b<br>\/* Private typedef -----------------------------------------------------------*\/<br>\/* USER CODE BEGIN PTD *\/<br>\u200b<br>\/* USER CODE END PTD *\/<br>\u200b<br>\/* Private define ------------------------------------------------------------*\/<br>\/* USER CODE BEGIN PD *\/<br>\/* USER CODE END PD *\/<br>\u200b<br>\/* Private macro -------------------------------------------------------------*\/<br>\/* USER CODE BEGIN PM *\/<br>\u200b<br>\/* USER CODE END PM *\/<br>\u200b<br>\/* Private variables ---------------------------------------------------------*\/<br>\u200b<br>\/* USER CODE BEGIN PV *\/<br>\u200b<br>\/* USER CODE END PV *\/<br>\u200b<br>\/* Private function prototypes -----------------------------------------------*\/<br>void SystemClock_Config(void);<br>\/* USER CODE BEGIN PFP *\/<br>\u200b<br>\/* USER CODE END PFP *\/<br>\u200b<br>\/* Private user code ---------------------------------------------------------*\/<br>\/* USER CODE BEGIN 0 *\/<br>\u200b<br>\u200b<br>\/\/ BOOTLOADER 3KB &nbsp; 3*1024 = 3072  0xC00  \/\/ \u8fd9\u4e2a\u5730\u5740\u5fc5\u987b\u8981\u88ab 256 \u6574\u9664 <br>\/\/ \u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5 \u7a0d\u5fae \u5728\u5927\u4e00\u70b9 ,\u7ed9 Bootloader  4k \u7684\u5927\u5c0f  \u4e5f\u5c31\u662f 0x1000<br>\u200b<br>#define FLASH_BASE_ADDRESS  (( uint32_t ) 0x8000000) <br>#define BOOTLOADER_OFFSET &nbsp; (( uint32_t ) 0x0001000)<br>#define VERTION_CONTROL &nbsp; &nbsp; (( uint32_t ) 0x0000000)<br>\u200b<br>\u200b<br>#define APPLICATION_ADDRESS  VERTION_CONTROL+BOOTLOADER_OFFSET+FLASH_BASE_ADDRESS <br>\u200b<br>\/* USER CODE END 0 *\/<br>\u200b<br>\/**<br> &nbsp;* @brief  The application entry point.<br> &nbsp;* @retval int<br> &nbsp;*\/<br>int main(void)<br>{<br> &nbsp;\/* USER CODE BEGIN 1 *\/<br>    \/\/ \u5411\u91cf\u8868\u504f\u79fb <br>    SCB-&gt;VTOR = APPLICATION_ADDRESS; \/\/ \u8bbe\u7f6e\u5411\u91cf\u8868\u7684\u504f\u79fb <br> &nbsp; &nbsp;\/\/\u6709\u4e9b\u4eba \u7591\u60d1 \u4e3a\u4ec0\u4e48\u8fd9\u4e2a \u5411\u91cf\u8868\u7684\u504f\u79fb \u53ef\u4ee5\u5199\u5728 Application \u4e2d ,\u6216\u8005\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u65f6\u5019 \u624d\u6267\u884c<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp;* \u6211\u7684\u7406\u89e3 (\u6709\u8bef\u8bf7\u6307\u9488):<br> &nbsp; &nbsp;* MSP PUSH \u662f --SP \u7684, \u5f53\u8df3\u8f6c\u5230\u8fd9\u4e2a \u4e3b\u51fd\u6570 \u65f6,\u76f4\u63a5\u8fd0\u884c Reset Handle ( \u4e3b\u7a0b\u5e8f ),\u4e3b\u7a0b\u5e8f\u4e2d\u7684 Reset Handler \u91cd\u65b0 \u6267\u884c SystemInit(),\u548c__main<br> &nbsp; &nbsp;* __main \u6267\u884c\u5b8c \u4e4b\u540e \u5c31 \u8c03\u7528 c main \u51fd\u6570 ,\u8fd9\u4e2a\u65f6\u5019\u5c31\u5230\u4e86 \u6267\u884c\u4e0a\u9762\u7684\u8bed\u53e5 .\u8fd9\u4e2a\u671f\u95f4 MSP \u4e00\u76f4\u5728 Reset Handler (\u8fdb\u5165 \u4e3b\u7a0b\u5e8f \u5f00\u59cb),\u4e2d\u95f4 \u4e2d\u65ad\u6211\u4eec\u4e5f    * \u5173\u95ed\u4e86,\u6240\u4ee5,\u53ef\u4ee5\u653e\u5230\u8fd9\u91cc.\u80fd\u4e0d\u80fd\u653e BootLoader \u533a \u4f60\u4eec \u53ef\u4ee5 \u81ea\u5df1 \u641c\u7d22\u4e0b .(\u6211\u6ca1\u6709\u601d\u8003\u8fc7)<br> &nbsp; &nbsp;*<br> &nbsp; &nbsp;**\/<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br>    __enable_irq(); \/\/\u4f7f\u80fd \u4e2d\u65ad<br> &nbsp;\/* USER CODE END 1 *\/<br>\u200b<br> &nbsp;\/* MCU Configuration--------------------------------------------------------*\/<br>\u200b<br> &nbsp;\/* Reset of all peripherals, Initializes the Flash interface and the Systick. *\/<br> &nbsp;HAL_Init();<br>\u200b<br> &nbsp;\/* USER CODE BEGIN Init *\/<br>\u200b<br> &nbsp;\/* USER CODE END Init *\/<br>\u200b<br> &nbsp;\/* Configure the system clock *\/<br> &nbsp;SystemClock_Config();<br>\u200b<br> &nbsp;\/* USER CODE BEGIN SysInit *\/<br>\u200b<br> &nbsp;\/* USER CODE END SysInit *\/<br>\u200b<br> &nbsp;\/* Initialize all configured peripherals *\/<br> &nbsp;MX_GPIO_Init();<br> &nbsp;\/* USER CODE BEGIN 2 *\/<br>\u200b<br> &nbsp;\/* USER CODE END 2 *\/<br>\u200b<br> &nbsp;\/* Infinite loop *\/<br> &nbsp;\/* USER CODE BEGIN WHILE *\/<br> &nbsp;while (1)<br>  {<br> &nbsp; &nbsp;\/* USER CODE END WHILE *\/<br>\u200b<br> &nbsp; &nbsp;\/* USER CODE BEGIN 3 *\/<br>        <br>        HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5);<br>        HAL_Delay(100);<br>        <br>  }<br> &nbsp;\/* USER CODE END 3 *\/<br>}<br>\u200b<br>\/**<br> &nbsp;* @brief System Clock Configuration<br> &nbsp;* @retval None<br> &nbsp;*\/<br>void SystemClock_Config(void)<br>{<br> &nbsp;RCC_OscInitTypeDef RCC_OscInitStruct = {0};<br> &nbsp;RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};<br>\u200b<br> &nbsp;\/** Initializes the RCC Oscillators according to the specified parameters<br> &nbsp;* in the RCC_OscInitTypeDef structure.<br> &nbsp;*\/<br> &nbsp;RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;<br> &nbsp;RCC_OscInitStruct.HSEState = RCC_HSE_ON;<br> &nbsp;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;<br> &nbsp;RCC_OscInitStruct.HSIState = RCC_HSI_ON;<br> &nbsp;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;<br> &nbsp;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;<br> &nbsp;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;<br> &nbsp;if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) != HAL_OK)<br>  {<br> &nbsp; &nbsp;Error_Handler();<br>  }<br> &nbsp;\/** Initializes the CPU, AHB and APB buses clocks<br> &nbsp;*\/<br> &nbsp;RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;<br> &nbsp;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;<br> &nbsp;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;<br> &nbsp;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;<br> &nbsp;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;<br>\u200b<br> &nbsp;if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)<br>  {<br> &nbsp; &nbsp;Error_Handler();<br>  }<br>}<br>\u200b<br>\/* USER CODE BEGIN 4 *\/<br>\u200b<br>\/* USER CODE END 4 *\/<\/pre>\n\n\n\n<p><strong>** \u4ee5\u4e0a\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u8fd0\u884c,\u9700\u8981\u914d\u5408\u786c\u4ef6 ,\u4fee\u6539<\/strong><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfb\u524d\u987b\u77e5: PC : Win10 Keil5 Version :5.30 STM32CubeMX Versio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"zakra_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-stm32"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":1,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":34,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions\/34"}],"wp:attachment":[{"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kysdfh.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}