{"id":52,"date":"2023-06-13T14:41:26","date_gmt":"2023-06-13T06:41:26","guid":{"rendered":"https:\/\/xuebuwan.cn\/?p=52"},"modified":"2023-06-13T17:41:51","modified_gmt":"2023-06-13T09:41:51","slug":"stm32g070-%e4%b8%b2%e5%8f%a3-ota-%e5%8d%87%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/xuebuwan.cn\/index.php\/2023\/06\/13\/stm32g070-%e4%b8%b2%e5%8f%a3-ota-%e5%8d%87%e7%ba%a7\/","title":{"rendered":"STM32G070 \u4e32\u53e3 OTA \u5347\u7ea7"},"content":{"rendered":"<h1>STM32G070 \u4e32\u53e3 OTA \u5347\u7ea7<\/h1>\n<p>\u7531\u4e8e\u6ca1\u6709\u5916\u6302Flash\uff0c\u5185\u5b58\u53c8\u6bd4\u8f83\u5c0f\uff0c\u6240\u4ee5\u5728\u6b64\u4f7f\u7528\u5185\u7f6eFlash\u4f5c\u4e3a\u7f13\u5b58\uff0c<\/p>\n<p>G070CBT6\u6574\u4f53flash\u4f4d128K,\u4e3aflash\u89c4\u5212\u5206\u533a\uff0c\u5206\u533a\u8868\u5982\u4e0b\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/image.xuebuwan.cn\/i\/2023\/06\/05\/647d4e4f98c2a.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/image.xuebuwan.cn\/i\/2023\/06\/05\/647d4e4f98c2a.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230605105405232\" style=\"zoom:50%;\" \/><\/div><\/p>\n<p>\u4eceSTM32G070\u5bc4\u5b58\u5668\u624b\u518c\u53ef\u4ee5\u770b\u5230\uff0c\u5185\u90e8flash\u662f2K\u5bf9\u9f50\u603b\u5171\u670964\u4e2a\u9875\uff0c\u540c\u65f6\u5199\u64cd\u4f5cflash\u65f6\u8981\u6ce8\u610f\u5730\u5740\u4e3a4\u5b57\u8282\u5bf9\u9f50\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u529f\u80fd<\/th>\n<th style=\"text-align: center;\">\u5730\u5740<\/th>\n<th style=\"text-align: center;\">\u5360\u7528\u7a7a\u95f4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">bootloader<\/td>\n<td style=\"text-align: center;\">0x8000000 - 0x8005000<\/td>\n<td style=\"text-align: center;\">20K<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">config<\/td>\n<td style=\"text-align: center;\">0x8005000 - 0x8005800<\/td>\n<td style=\"text-align: center;\">2K<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">app<\/td>\n<td style=\"text-align: center;\">0x8005800 - 0x8012000<\/td>\n<td style=\"text-align: center;\">50K<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ota<\/td>\n<td style=\"text-align: center;\">0x8012000 - 0x801E800<\/td>\n<td style=\"text-align: center;\">50K<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u672a\u4f7f\u7528<\/td>\n<td style=\"text-align: center;\">0x801E800 - 0x801FFFF<\/td>\n<td style=\"text-align: center;\">5K<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e32\u53e3\u66f4\u65b0\u534f\u8bae<\/h2>\n<h3>YModem\u534f\u8bae\u683c\u5f0f<\/h3>\n<p>\u6b64\u90e8\u5206\u53c2\u8003<a href=\"https:\/\/www.cnblogs.com\/acuity\/p\/12154114.html\">Ymodem\u534f\u8bae\u8981\u70b9 - Acuity - \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/p>\n<h4>\u534f\u8bae\u5934<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u7b80\u5199<\/th>\n<th style=\"text-align: center;\">\u547d\u4ee4\u7801<\/th>\n<th style=\"text-align: center;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">SOH<\/td>\n<td style=\"text-align: center;\">0x01<\/td>\n<td style=\"text-align: center;\">128\u5b57\u8282\u6570\u636e\u957f\u5ea6\uff0c\u6574\u5e27\u957f\u5ea6133\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">STX<\/td>\n<td style=\"text-align: center;\">0x02<\/td>\n<td style=\"text-align: center;\">1024\u5b57\u8282\u6570\u636e\u957f\u5ea6\uff0c\u6574\u5e27\u957f\u5ea61029\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">EOT<\/td>\n<td style=\"text-align: center;\">0x04<\/td>\n<td style=\"text-align: center;\">\u6587\u4ef6\u4f20\u8f93\u7ed3\u675f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ACK<\/td>\n<td style=\"text-align: center;\">0x06<\/td>\n<td style=\"text-align: center;\">\u6b63\u786e\u63a5\u6536\u5e94\u7b54<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">NAK<\/td>\n<td style=\"text-align: center;\">0x15<\/td>\n<td style=\"text-align: center;\">\u91cd\u4f20\u5f53\u524d\u6570\u636e\u5305<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">CAN<\/td>\n<td style=\"text-align: center;\">0x18<\/td>\n<td style=\"text-align: center;\">\u8fde\u7eed\u53d1\u90015\u6761\u6b64\u547d\u4ee4\u53d6\u6d88\u4f20\u8f93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">CC<\/td>\n<td style=\"text-align: center;\">0x43<\/td>\n<td style=\"text-align: center;\">\u5b57\u7b26C<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u63e1\u624b\u4fe1\u606f<\/h4>\n<p>\u63e1\u624b\u662f\u65f6\u9996\u5148Client\u7aef\u5411Server\u7aef\u53d1\u9001\u5b57\u7b26\u201cC\u201d(ASCII\u7801\u201c43\u201d)<\/p>\n<p>Server\u7aef\u63a5\u6536\u540e\u53d1\u9001\u8d77\u59cb\u5e27\uff0c\u683c\u5f0f\u5982\u4e0b<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5e27\u5934<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7\u53cd\u7801<\/th>\n<th style=\"text-align: center;\">\u6587\u4ef6\u540d\u79f0<\/th>\n<th style=\"text-align: center;\">\u6587\u4ef6\u5927\u5c0f<\/th>\n<th style=\"text-align: center;\">\u586b\u5145\u533a<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u9ad8\u4f4d<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u4f4e\u4f4d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">SOH<\/td>\n<td style=\"text-align: center;\">0x00<\/td>\n<td style=\"text-align: center;\">0xff<\/td>\n<td style=\"text-align: center;\">File name+0x00<\/td>\n<td style=\"text-align: center;\">File size+0x00<\/td>\n<td style=\"text-align: center;\">NULL\uff080x00\uff09<\/td>\n<td style=\"text-align: center;\">CRC-H<\/td>\n<td style=\"text-align: center;\">CRC-L<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u6570\u636e\u5305<\/h4>\n<p>Client\u7aef\u63a5\u6536\u8d77\u59cb\u5e27\u5e76\u89e3\u7801\u83b7\u5f97\u6587\u4ef6\u540d\u79f0\u4e0e\u6587\u4ef6\u5927\u5c0f\u540e\u5411Server\u7aef\u53d1\u9001<strong>ACK<\/strong><\/p>\n<p>Server\u7aef\u63a5\u6536<strong>ACK<\/strong>\u540e\u56de\u590d\u7b2c\u4e00\u5e27\u6570\u636e\uff0c\u6b64\u65f6\u7b2c\u4e00\u5e27\u6570\u636e\u5305\u53f7\u4e3a1\uff0c\u6570\u636e\u683c\u5f0f\u5982\u4e0b<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5e27\u5934<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7\u53cd\u7801<\/th>\n<th style=\"text-align: center;\">\u6709\u6548\u6570\u636e<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u9ad8\u4f4d<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u4f4e\u4f4d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">SOH<\/td>\n<td style=\"text-align: center;\">PN<\/td>\n<td style=\"text-align: center;\">XPN<\/td>\n<td style=\"text-align: center;\">128\u5b57\u8282DATA<\/td>\n<td style=\"text-align: center;\">CRC-H<\/td>\n<td style=\"text-align: center;\">CRC-L<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u4f7f\u7528<strong>STX<\/strong>\u6a21\u5f0f\u53d1\u9001\uff0c\u6570\u636e\u5219\u4e3a1024\u5b57\u8282\u3002<\/p>\n<p>Client\u7aef\u6b63\u786e\u63a5\u6536\u540e\u56de\u590d<strong>ACK<\/strong>\uff0cServer\u7aef\u56de\u590d\u7b2c\u4e8c\u5e27\u6570\u636e\u3002<\/p>\n<p>\u4e4b\u540e\u6570\u636e\u5305\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><u>\u6b64\u5904\u6709\u4e9b\u7591\u60d1\uff0c\u5728\u4f7f\u7528Xshell\u4f5c\u4e3aServer\u7aef\u6d4b\u8bd5STM32\u7684Client\u7aef\u65f6\u6b64\u5904\u53d1\u9001<strong>ACK<\/strong>\u6216<strong>CC+ACK<\/strong>\u5747\u53ef\u63a5\u6536\u6b63\u786e\u56de\u590d<\/u><\/p>\n<p>\u5305\u53f7\u7531\u4e8e\u662f\u4e00\u5b57\u8282\uff0c\u6240\u4ee5\u8303\u56f4\u4e3a<strong>0-255<\/strong>\uff0c\u82e5\u6570\u636e\u5305\u603b\u6570\u8d85\u8fc7255\u5219\u5305\u53f7\u5230\u8fbe255\u540e<strong>\u5f52\u96f6<\/strong>\u91cd\u65b0\u5f00\u59cb\u8ba1\u6570\u3002<\/p>\n<p>Client\u6237\u7aef\u63a5\u6536\u6570\u636e\u5305\u540e<strong>CRC<\/strong>\u6821\u9a8c\u51fa\u9519\uff0c\u53ef\u5411Server\u7aef\u6062\u590d<strong>NAK<\/strong>\uff0cServer\u7aef\u63a5\u6536<strong>NAK<\/strong>\u540e\u4f1a\u91cd\u53d1\u6b64\u6b21\u6570\u636e\u5305\u3002<\/p>\n<p>\u5728Server\u7aef\u53d1\u9001\u81f3\u6700\u540e\u4e00\u5e27\u6570\u636e\uff0c\u82e5\u6b64\u65f6\u6570\u636e\u4e0d\u8db3128\u5b57\u8282\u62161024\u5b57\u8282\uff0c\u5219\u7528<strong>0x1A<\/strong>\u8865\u9f50\u5269\u4f59\u5b57\u8282\u6570\uff0c\u4f7f\u6570\u636e\u5bf9\u9f50\u3002<\/p>\n<p>\u6b64\u65f6Client\u7aef\u5728\u63a5\u6536\u5b8c\u6210\u6700\u540e\u4e00\u5e27\u6570\u636e\u540e\u82e5\u7ee7\u7eed\u53d1\u9001<strong>ACK<\/strong>\uff0cServer\u7aef\u5219\u6062\u590d<strong>EOT<\/strong>(\u6587\u4ef6\u53d1\u9001\u7ed3\u675f)\u3002<\/p>\n<h4>\u7ed3\u675f\u5e27<\/h4>\n<p>\u7ed3\u675f\u5e27\u4e3a133\u5b57\u8282\u957f\u5ea6\uff0c\u683c\u5f0f\u5982\u4e0b<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5e27\u5934<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7<\/th>\n<th style=\"text-align: center;\">\u5305\u53f7\u53cd\u7801<\/th>\n<th style=\"text-align: center;\">\u6570\u636e\u533a<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u9ad8\u4f4d<\/th>\n<th style=\"text-align: center;\">\u6821\u9a8c\u4f4e\u4f4d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">SOH<\/td>\n<td style=\"text-align: center;\">0x00<\/td>\n<td style=\"text-align: center;\">0xff<\/td>\n<td style=\"text-align: center;\">NULL\uff080x00\uff09<\/td>\n<td style=\"text-align: center;\">CRC-H<\/td>\n<td style=\"text-align: center;\">CRC-L<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><u>\u6b64\u5904\u7591\u60d1\uff0c\u5728\u4f7f\u7528Xshell\u4f5c\u4e3aServer\u6d4b\u8bd5\u65f6\uff0c\u7ed3\u675f\u5e27\u53ef\u4ee5\u8df3\u8fc7\uff0c\u5728Client\u63a5\u6536\u5230<strong>EOT<\/strong>\u4e4b\u540e\u76f4\u63a5\u8fde\u53d16\u4e2a<strong>CAN<\/strong>,Xshell\u4e5f\u4f1a\u9ed8\u8ba4Client\u6587\u4ef6\u63a5\u6536\u5b8c\u6210\u3002\u800c\u4f7f\u7528Xshell\u4f5c\u4e3aClient\u65f6\u5219Client\u63a5\u6536<strong>EOT<\/strong>\u5e27\u4e4b\u540e\u4f1a\u53d1\u9001\u4e00\u6b21<strong>NAK<\/strong>\u518d\u6b21\u6062\u590d<strong>EOT<\/strong>\u4e4b\u540eClient\u4f1a\u56de\u590d\u4e00\u6b21<strong>CC+ACK<\/strong>\uff0c\u6b64\u65f6Server\u5411Client\u53d1\u9001\u7ed3\u675f\u5e27\u540eClient\u5224\u65ad\u63a5\u6536\u5b8c\u6210\uff0c\u5411Server\u53d1\u9001\u516d\u4e2a<strong>CAN<\/strong>\u3002<\/u><\/p>\n<p>Client\u63a5\u6536\u6587\u4ef6\u53d1\u9001\u7ed3\u675f\u540e\u8fde\u7eed\u53d1\u90016\u4e2a<strong>CAN<\/strong>\u81f3Server\u8868\u793a\u63a5\u6536\u5b8c\u6210\uff0c\u65ad\u5f00\u63a5\u6536\uff0cServer\u63a5\u6536\u540e\u5224\u65ad\u53d1\u9001\u5b8c\u6210\u3002<\/p>\n<h3>CRC\u6821\u9a8c<\/h3>\n<p>Ymodem\u534f\u8bae\u6240\u7528\u7684CRC\u516c\u5f0f\u4e0eXmodem\u4e00\u81f4\uff0c\u4e3a0x1021,C\u8bed\u8a00\u5199\u6cd5\u5982\u4e0b<\/p>\n<p>data\u4e3a\u6570\u636e(\u9664\u53bb\u5e27\u5934\uff0c\u5305\u53f7\u3001\u5305\u53f7\u53cd\u7801\u3001\u5e27\u5c3e\u9ad8\u4f4e\u6821\u9a8c\u4f4d)\uff0clen\u4e3a\u957f\u5ea6<\/p>\n<pre><code class=\"language-c\">static uint16_t Ymodem_CRC16(const uint8_t* data, uint16_t len) {\n    uint16_t crc = 0;\n\n    for (uint16_t i = 0; i &lt; len; i++) {\n        crc ^= (uint16_t)data[i] &lt;&lt; 8;\n        for (uint8_t j = 0; j &lt; 8; j++) {\n            if (crc &amp; 0x8000) {\n                crc = (crc &lt;&lt; 1) ^ 0x1021;\n            } else {\n                crc &lt;&lt;= 1;\n            }\n        }\n    }\n    return crc;\n}<\/code><\/pre>\n<p>\u53d6\u9ad8\u4f4e\u4f4d<\/p>\n<pre><code class=\"language-c\">crc_buf\/\/crc\u8ba1\u7b97\u7ed3\u679c\n(uint8_t)(crc_buf&gt;&gt;8)\/\/CRC_H\n(uint8_t)crc_buf\/\/CRC_L<\/code><\/pre>\n<h4>Ymodem\u63a5\u6536\u90e8\u5206\u4ee3\u7801<\/h4>\n<p>\u53ea\u5199\u4e86128\u5b57\u8282\u6a21\u5f0f\uff0c1024\u5b57\u8282\u6a21\u5f0f\u53ef\u4ee5\u81ea\u5df1\u8865\u5145\uff0c\u8fd9\u91cc\u61d2\u5f97\u5199\u4e86\ud83e\udd23<\/p>\n<pre><code class=\"language-c\">\/\/YMODEM\u534f\u8bae\u5904\u7406\nvoid Ymodem(const uint8_t *data) {\n    uint8_t data_buf[1030];\n    memcpy(data_buf,data,sizeof(data_buf));\n    switch(YMODEM_FLAG) {\n    case 0: { \/\/\u5904\u7406\u7b2c0\u5e27\u6570\u636e\n        uint8_t *p = &amp;data_buf[3];\n        uint8_t num = 0;\n        if((data_buf[0] == SOH) &amp;&amp; (data_buf[1] == 0)) {\n            \/\/CRC\u6821\u9a8c\n            uint16_t crc_buf = Ymodem_CRC16(p,128);\n            debug_print(&quot;%02X\\t%02X\\t%04X\\n&quot;,p[128],p[129],crc_buf);\n            if((p[128] != (uint8_t)(crc_buf&gt;&gt;8)) &amp;&amp; (p[129] != (uint8_t)crc_buf)) {\n                debug_print(&quot;CRC ERROR\\n&quot;);\n                HAL_Delay(5);\n                Ymodem_Send_Data(NAK);\/\/\u6821\u9a8c\u5931\u8d25\u91cd\u4f20\u5f53\u524d\u5305\n                goto ERROR;\n            }\n            for(uint16_t i = 0; i&lt;128-3; i++) { \/\/\u83b7\u53d6\u6587\u4ef6\u540d\n                if(data_buf[i] == 0) { \/\/\u6587\u4ef6\u540d\u7ed3\u675f\uff0c\u8df3\u51fa\u5faa\u73af\n                    break;\n                }\n                if(i&lt;sizeof(file_name)\/sizeof(file_name[0])) { \/\/\u8d85\u51fa\u7f13\u5b58\u957f\u5ea6\u8fdb\u884c\u622a\u65ad\u5904\u7406\n                    file_name[i] = p[i];\n                }\n                num = i;\n            }\n            num ++;\n            for(uint16_t i = 0; i&lt;128-num; i++) { \/\/\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\n                if(p[num+i] == 0) { \/\/\u6587\u4ef6\u5927\u5c0f\u7ed3\u675f\n                    break;\n                }\n                file_len[i] = p[num+i];\n            }\n            YMODEM_FLAG = 1;\n            Ymodem_Send_Data(ACK);\/\/\u5904\u7406\u5b8c\u6210\u53d1\u9001\u5e94\u7b54\u4fe1\u53f7\n            Ymodem_Send_Data(CC);\n            break;\n        } else {\n            Ymodem_Send_Data(CC);\n        }\n        break;\n    }\n    case 1: { \/\/\u5904\u7406\u6570\u636e\u5e27\n        if(data_buf[0] == SOH) { \/\/128\u5b57\u8282\u6570\u636e\u7c7b\u578b\n            uint8_t *p = &amp;data_buf[3];\n            uint8_t data[130] = {0};\n            \/\/CRC\u6821\u9a8c\n            uint16_t crc_buf = Ymodem_CRC16(p,128);\n            debug_print(&quot;%02X\\t%02X\\t%04X\\n&quot;,p[128],p[129],crc_buf);\n            if((p[128] != (uint8_t)(crc_buf&gt;&gt;8)) &amp;&amp; (p[129] != (uint8_t)crc_buf)) {\n                debug_print(&quot;CRC ERROR\\n&quot;);\n                HAL_Delay(10);\n                Ymodem_Send_Data(NAK);\/\/\u6821\u9a8c\u5931\u8d25\u91cd\u4f20\u5f53\u524d\u5305\n                goto ERROR;\n            }\n            for(uint8_t i = 0; i&lt;128; i++) {\n                data[i] = p[i];\n            }\n                        \/\/\u5199flash\n            Write_Flash_Data(addr,data,128);\n\n            HAL_Delay(1);\n            Ymodem_Send_Data(ACK);\/\/\u5904\u7406\u5b8c\u6210\u53d1\u9001\u5e94\u7b54\u4fe1\u53f7\n            break;\n        } else if(data_buf[0] == STX) { \/\/1024\u5b57\u8282\u6570\u636e\u7c7b\u578b\n\/\/            uint8_t *p = &amp;data_buf[3];\n\/\/            \/\/CRC\u6821\u9a8c\n\/\/            uint16_t crc_buf = Ymodem_CRC16(p,1024);\n\/\/            debug_print(&quot;%02X\\t%02X\\t%04X\\n&quot;,p[1023],p[1024],crc_buf);\n\/\/            if((p[1023] != (uint8_t)(crc_buf&gt;&gt;8)) &amp;&amp; (p[1024] != (uint8_t)crc_buf)) {\n\/\/                debug_print(&quot;CRC ERROR\\n&quot;);\n\/\/                HAL_Delay(5);\n\/\/                Ymodem_Send_Data(NAK);\/\/\u6821\u9a8c\u5931\u8d25\u91cd\u4f20\u5f53\u524d\u5305\n\/\/                goto ERROR;\n\/\/            }\n\/\/            \/\/\u6570\u636e\u5904\u7406\n\n\/\/            HAL_Delay(5);\n\/\/            Ymodem_Send_Data(ACK);\/\/\u5904\u7406\u5b8c\u6210\u53d1\u9001\u5e94\u7b54\u4fe1\u53f7\n            break;\n        } else if(data_buf[0] == CAN) { \/\/\u4e3b\u673a\u53d6\u6d88\u53d1\u9001\n            debug_print(&quot;CANCELL\\n&quot;);\n            addr = FLASH_ADDR;\n            YMODEM_FLAG = 0;\n            break;\n        } else if((data_buf[0] == EOT)) {\/\/\u56de\u590d\u6587\u4ef6\u63a5\u6536\u7ed3\u675f\n            for(uint8_t i = 0; i&lt;=5; i++) {\n                HAL_Delay(5);\n                Ymodem_Send_Data(CAN);\n            }\n            addr = FLASH_ADDR;\n            Write_Config(UPDATE_FLAG);\n            printf(&quot;[REST START]\\r\\n&quot;);\n            HAL_NVIC_SystemReset();\n            YMODEM_FLAG = 0;\n            break;\n        }\n        Ymodem_Send_Data(NAK);\/\/\u6821\u9a8c\u5931\u8d25\u91cd\u4f20\u5f53\u524d\u5305\n        break;\n    }\n    default: {\n\n        break;\n    }\n\n    }\nERROR:\n    return;\n}<\/code><\/pre>\n<h2>\u5199Flash<\/h2>\n<p>\u534f\u8bae\u63a5\u6536\u540e\u6bcf\u63a5\u6536\u4e00\u6b21\u6570\u636e\u76f4\u63a5\u5199\u5165flash\u5206\u533a\u8fdb\u884c\u7f13\u5b58\u3002\u5168\u90e8\u63a5\u6536\u5b8c\u6210\u540e\u5728flash\u7684config\u5206\u533a\u5199\u5165\u5347\u7ea7\u6807\u5fd7\uff08\u6b64\u5904\u5e94\u8be5\u8981\u8fdb\u884c\u5b8c\u6574\u6027\u6821\u9a8c\u7684\uff0c\u5077\u61d2\u6ca1\u5199\uff09\uff0c\u4e4b\u540e\u8fdb\u884c\u8f6f\u91cd\u542f\uff0c\u63a5\u4e0b\u6765\u7684\u642c\u8fd0\u5206\u533a\u8986\u76d6\u6389\u539fapp\u5206\u533a\u4ea4\u7ed9bootloader\u8fdb\u884c\u5b8c\u6210\u3002\u6b64\u7bc7\u53ea\u5199\u4e86app\u5185\u7a0b\u5e8f\uff0cbootloader\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u6316\u4e2a\u5751\u653e\u4e0b\u7bc7\u6587\u7ae0\u518d\u5199\ud83e\udee0<\/p>\n<pre><code class=\"language-c\">\/*\n * @Author: Memory 1619005172@qq.com\n * @Date: 2023-06-06 15:03:55\n * @LastEditors: Memory 1619005172@qq.com\n * @LastEditTime: 2023-06-06 15:05:23\n * @FilePath:\n * @Description:\n *\/\n#include &quot;update.h&quot;\n#define TGA &quot;UPDATE&quot;\n\nuint8_t file_name[200] = {0};\nuint8_t file_len[50] = {0};\n\nuint32_t addr = FLASH_ADDR;\n\nstatic FLASH_SATAE_T Erase_Flash(uint32_t addr,uint8_t num);\nstatic uint32_t GetPage(uint32_t Addr);\nstatic uint16_t Ymodem_CRC16(const uint8_t* data, uint16_t len);\n\nFLASH_SATAE_T Update_Init(void) {\n    HAL_FLASH_Unlock();\/\/\u89e3\u9501flash\n    if(Erase_Flash(FLASH_ADDR,ERASE_LEN\/2) != FLASH_OK) { \/\/\u64e6\u9664flash\n        goto ERROR;\n    }\n    HAL_FLASH_Lock();\n    return FLASH_OK;\nERROR:\n    HAL_FLASH_Lock();\n    return FLASH_ERROR;\n}\n\nFLASH_SATAE_T Write_Flash_DoubleWord(uint32_t addr,uint64_t data) {\n    HAL_FLASH_Unlock();\n    if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addr,data) != HAL_OK) {\n        debug_print(&quot;WRITE FLASH ERROR\\n&quot;);\n        goto ERROR;\n    }\n    HAL_FLASH_Lock();\n    return FLASH_OK;\nERROR:\n    HAL_FLASH_Lock();\n    return FLASH_ERROR;\n}\n\nFLASH_SATAE_T Write_Config(uint64_t data) {\n    HAL_FLASH_Unlock();\/\/\u89e3\u9501flash\n    if(Erase_Flash(CONFIG_ADDR,1) != FLASH_OK) { \/\/\u64e6\u9664flash\n        goto ERROR;\n    }\n    if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,CONFIG_ADDR,data) != HAL_OK) {\n        debug_print(&quot;WRITE FLASH ERROR\\n&quot;);\n        goto ERROR;\n    }\n    debug_print(&quot;SUCCESS\\n&quot;);\nERROR:\n    HAL_FLASH_Lock();\/\/\u4e0a\u9501\n    return FLASH_ERROR;\n}\n\nstatic FLASH_SATAE_T Erase_Flash(uint32_t addr,uint8_t num) {\n    uint32_t SectorError=0;\n\n    UPDATE_FLASH.TypeErase = FLASH_TYPEERASE_PAGES;\n    UPDATE_FLASH.Page = GetPage(addr);\n    UPDATE_FLASH.NbPages = num;\n    if(HAL_FLASHEx_Erase(&amp;UPDATE_FLASH,&amp;SectorError) != HAL_OK) {\n        debug_print(&quot;ERASE ERROR %x\\n&quot;,SectorError);\n        goto ERROR;\n    }\n    debug_print(&quot;ERASE SUCCESS %x\\n&quot;,SectorError);\n    return FLASH_OK;\nERROR:\n    return FLASH_ERROR;\n}\n\nstatic uint32_t GetPage(uint32_t Addr)\n{\n    uint32_t page = 0;\n    page = (Addr-FLASH_BASE) \/ FLASH_PAGE_SIZE;\n    return page;\n}\n\nFLASH_SATAE_T Write_Flash_Data(uint32_t address, uint8_t *buf, uint32_t length)\n{\n    uint32_t i = 0;\n    uint32_t start_address = address;\n    uint64_t data = 0;\n    uint32_t data1 = 0, data2 = 0;\n    \/\/\u957f\u5ea6\u4e3a0\u65f6\u76f4\u63a5\u8fd4\u56de\n    if(length == 0)\n    {\n        goto ERROR;\n    }\n    \/\/\u957f\u5ea6\u975e8\u5b57\u8282\u500d\u6570\u65f6\u5219\u8865\u9f50\u65b0\u76848\u5b57\u8282\n    if(length%8 != 0)\n    {\n        length = (length\/8)*8 + 8;\n    }\n\n    for (i=0; i&lt;(length\/8); i++)\n    {\n        \/\/\u5c0f\u7aef\u683c\u5f0f\u5b58\u653e\n        data1 = buf[i*8 + 0];\n        data1 |= buf[i*8 + 1] &lt;&lt; 8;\n        data1 |= buf[i*8 + 2] &lt;&lt; 16;\n        data1 |= buf[i*8 + 3] &lt;&lt; 24;\n        data2 = buf[i*8 + 4];\n        data2 |= buf[i*8 + 5] &lt;&lt; 8;\n        data2 |= buf[i*8 + 6] &lt;&lt; 16;\n        data2 |= buf[i*8 + 7] &lt;&lt; 24;\n        data = (uint64_t)data2*256*256*256*256 + (uint64_t)data1;\n\n        Write_Flash_DoubleWord(start_address, data);\n        start_address += 8;\n    }\n    addr = start_address;\n    return FLASH_OK;\nERROR:\n    return FLASH_ERROR;\n}<\/code><\/pre>\n<p>update.h<\/p>\n<pre><code class=\"language-c\">#ifndef __UPDATE_H__\n#define __UPDATE_H__\n\n#include &quot;comm.h&quot;\n\n#define JUMP_ADDR       0x8005800       \/\/\u8df3\u8f6c\u5730\u5740\n#define CONFIG_ADDR     0X8005000       \/\/\u5347\u7ea7\u4fe1\u606f\u5b58\u50a8\u5206\u533a\n#define FLASH_ADDR      0x8012000       \/\/Flash\u8bfb\u53d6\u5730\u5740\n#define ERASE_LEN           50                  \/\/\u64e6\u9664\u957f\u5ea6\n#define UPDATE_FLAG   0x123456      \/\/\u5347\u7ea7\u6807\u5fd7\n\n#define YMODEM_UART huart1\n\nenum {\n    SOH = 0x01,\n    STX = 0x02,\n    EOT = 0x04,\/\/\u6587\u4ef6\u4f20\u8f93\u7ed3\u675f\n    ACK = 0x06,\/\/\u6b63\u786e\u63a5\u6536\n    NAK = 0x15,\/\/\u91cd\u4f20\u5f53\u524d\u5305\n    CAN = 0x18,\/\/\u53d6\u6d88\u4f20\u8f93\u547d\u4ee4\n    CC = 0x43,\/\/\u5b57\u7b26C\n};\n\nFLASH_SATAE_T Update_Init(void);\nFLASH_SATAE_T Write_Flash(void);\nvoid Ymodem(const uint8_t *data);<\/code><\/pre>\n<p>END<\/p>\n","protected":false},"excerpt":{"rendered":"<p>STM32G070 \u4e32\u53e3 OTA \u5347\u7ea7 \u7531\u4e8e\u6ca1\u6709\u5916\u6302Flash\uff0c\u5185\u5b58\u53c8\u6bd4\u8f83\u5c0f\uff0c\u6240\u4ee5\u5728\u6b64\u4f7f\u7528\u5185\u7f6eFlash\u4f5c\u4e3a\u7f13 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[16,17,18,15],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-stm32","tag-ota","tag-uart","tag-ymodem","tag-15"],"_links":{"self":[{"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":7,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuebuwan.cn\/index.php\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}