{"id":99680,"date":"2026-09-03T00:37:06","date_gmt":"2026-09-02T16:37:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/99680.html"},"modified":"2026-09-03T00:37:06","modified_gmt":"2026-09-02T16:37:06","slug":"%e5%b5%8c%e5%85%a5%e5%bc%8f%e7%b3%bb%e7%bb%9f%e6%9e%b6%e6%9e%84%e4%b9%8b%e9%81%93%ef%bc%9a%e5%91%8a%e5%88%ab%e6%84%8f%e5%a4%a7%e5%88%a9%e9%9d%a2%e6%9d%a1%ef%bc%8c%e6%8b%a5%e6%8a%b1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/99680.html","title":{"rendered":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8"},"content":{"rendered":"<hr \/>\n<h2>\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053&#xff1a;\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d&#xff0c;\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8<\/h2>\n<p>\u7248\u672c&#xff1a; 1.0 \u4f5c\u8005&#xff1a; AI \u67b6\u6784\u5e08 \u76ee\u6807\u8bfb\u8005&#xff1a; \u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u7cfb\u7edf\u67b6\u6784\u5e08\u3001\u6280\u672f\u8d1f\u8d23\u4eba \u7bc7\u5e45&#xff1a; \u7ea6 60,000 \u5b57<\/p>\n<hr \/>\n<h3>\u76ee\u5f55<\/h3>\n<ul>\n<li>\u7b2c\u4e00\u7ae0&#xff1a;\u5f15\u8a00\u2014\u2014\u4ece\u201c\u903b\u8f91\u5730\u72f1\u201d\u5230\u201c\u67b6\u6784\u5929\u5802\u201d<\/li>\n<li>\u7b2c\u4e8c\u7ae0&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u77f3\u2014\u2014\u4e2d\u65ad\u4e0e\u5e76\u53d1<\/li>\n<li>\u7b2c\u4e09\u7ae0&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u52a8\u8109\u2014\u2014\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;<\/li>\n<li>\u7b2c\u56db\u7ae0&#xff1a;\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u2014\u2014\u7ed3\u6784\u5316\u903b\u8f91\u7684\u6838\u5fc3<\/li>\n<li>\u7b2c\u4e94\u7ae0&#xff1a;\u5b9e\u6218\u91cd\u6784\u2014\u2014\u7528FSM\u91cd\u5851\u5145\u7535\u7ba1\u7406\u7cfb\u7edf<\/li>\n<li>\u7b2c\u516d\u7ae0&#xff1a;\u5b9e\u6218\u91cd\u6784\u2014\u2014\u7528FSM\u89e3\u6790\u901a\u4fe1\u534f\u8bae<\/li>\n<li>\u7b2c\u4e03\u7ae0&#xff1a;\u72b6\u6001\u673a\u4e0eRTOS\u7684\u534f\u594f\u66f2<\/li>\n<li>\u7b2c\u516b\u7ae0&#xff1a;\u9ad8\u7ea7\u4e3b\u9898\u4e0e\u67b6\u6784\u6a21\u5f0f<\/li>\n<li>\u7b2c\u4e5d\u7ae0&#xff1a;\u603b\u7ed3\u4e0e\u5c55\u671b<\/li>\n<li>\u9644\u5f55<\/li>\n<\/ul>\n<h3>\u7b2c\u4e00\u7ae0&#xff1a;\u5f15\u8a00\u2014\u2014\u4ece\u201c\u903b\u8f91\u5730\u72f1\u201d\u5230\u201c\u67b6\u6784\u5929\u5802\u201d<\/h3>\n<h4>1.1 \u201c\u610f\u5927\u5229\u9762\u6761\u201d\u4ee3\u7801\u7684\u8bc5\u5492<\/h4>\n<p>\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u7684\u6f2b\u957f\u5f81\u9014\u4e2d&#xff0c;\u6bcf\u4e00\u4f4d\u5de5\u7a0b\u5e08\u90fd\u53ef\u80fd\u906d\u9047\u4e00\u4e2a\u5171\u540c\u7684\u68a6\u9b47&#xff1a;\u4e00\u4e2a\u5de8\u5927\u7684\u3001\u5145\u6ee1\u4e86 if-else \u5d4c\u5957\u3001\u5168\u5c40\u6807\u5fd7\u4f4d\u6ee1\u5929\u98de\u3001\u903b\u8f91\u7ea0\u7f20\u4e0d\u6e05\u7684 main.c \u6587\u4ef6\u3002\u6211\u4eec\u5f62\u8c61\u5730\u79f0\u4e4b\u4e3a\u201c\u610f\u5927\u5229\u9762\u6761\u5f0f\u4ee3\u7801\u201d\u6216\u201c\u903b\u8f91\u5730\u72f1\u201d\u3002\u8fd9\u79cd\u4ee3\u7801\u4e0d\u4ec5\u96be\u4ee5\u9605\u8bfb\u548c\u7406\u89e3&#xff0c;\u66f4\u662f\u7ef4\u62a4\u548c\u6269\u5c55\u7684\u707e\u96be\u3002\u6bcf\u4e00\u6b21\u4fee\u6539\u90fd\u50cf\u662f\u5728\u4e00\u4e2a\u7cbe\u5fc3\u642d\u5efa\u7684\u79ef\u6728\u5854\u4e2d\u62bd\u8d70\u4e00\u5757\u79ef\u6728&#xff0c;\u968f\u65f6\u53ef\u80fd\u5f15\u53d1\u96ea\u5d29\u5f0f\u7684\u8fde\u9501\u5d29\u6e83\u3002<\/p>\n<p>\u8fd9\u79cd\u4ee3\u7801\u7684\u8bde\u751f\u5f80\u5f80\u4e0d\u662f\u56e0\u4e3a\u5de5\u7a0b\u5e08\u7684\u65e0\u80fd&#xff0c;\u800c\u662f\u6e90\u4e8e\u9879\u76ee\u521d\u671f\u7684\u201c\u5feb\u901f\u539f\u578b\u201d\u5fc3\u6001\u3002\u4e3a\u4e86\u5c3d\u5feb\u70b9\u4eae\u4e00\u4e2aLED\u3001\u8bfb\u51fa\u4e00\u4e2a\u4f20\u611f\u5668\u6570\u636e&#xff0c;\u6211\u4eec\u7528\u6700\u76f4\u63a5\u3001\u6700\u7ebf\u6027\u7684\u65b9\u5f0f\u5806\u780c\u903b\u8f91\u3002\u5f53\u529f\u80fd\u4ece\u4e00\u4e2a\u589e\u52a0\u5230\u5341\u4e2a\u3001\u4e00\u767e\u4e2a\u65f6&#xff0c;\u6700\u521d\u7684\u7b80\u5355\u7ed3\u6784\u5c31\u65e0\u6cd5\u627f\u8f7d\u65e5\u76ca\u589e\u957f\u7684\u590d\u6742\u6027&#xff0c;\u6700\u7ec8\u6f14\u53d8\u6210\u4e00\u573a\u65e0\u6cd5\u6536\u62fe\u7684\u6df7\u4e71\u3002<\/p>\n<h4>1.2 \u4e00\u4e2a\u5178\u578b\u7684\u53cd\u6a21\u5f0f&#xff1a;\u5145\u7535\u7ba1\u7406\u903b\u8f91<\/h4>\n<p>\u8ba9\u6211\u4eec\u518d\u6b21\u5ba1\u89c6\u60a8\u63d0\u4f9b\u7684\u8fd9\u4e2a\u7ecf\u5178\u7684\u201c\u53cd\u6a21\u5f0f\u201d\u4ee3\u7801\u3002\u5b83\u5b8c\u7f8e\u5730\u8be0\u91ca\u4e86\u201c\u903b\u8f91\u5730\u72f1\u201d\u7684\u6240\u6709\u7279\u5f81&#xff1a;<\/p>\n<p>\/\/ \u26a0\ufe0f \u4e00\u4e2a\u5178\u578b\u7684\u5145\u7535\u7ba1\u7406\u903b\u8f91\u5730\u72f1<br \/>\nvoid charge_manager() {<br \/>\n    if (is_charger_plugged_in) {<br \/>\n        if (!is_charging_finished) {<br \/>\n            if (!is_battery_temp_high) {<br \/>\n                \/\/ \u5f00\u59cb\u5145\u7535&#8230;<br \/>\n                if (is_charging_current_ok) {<br \/>\n                    \/\/ &#8230;<br \/>\n                } else {<br \/>\n                    \/\/ &#8230;<br \/>\n                }<br \/>\n            } else {<br \/>\n                \/\/ \u505c\u6b62\u5145\u7535&#xff0c;\u6e29\u5ea6\u8fc7\u9ad8<br \/>\n            }<br \/>\n        } else {<br \/>\n            \/\/ \u5145\u7535\u5df2\u5b8c\u6210<br \/>\n        }<br \/>\n    } else {<br \/>\n        \/\/ \u672a\u8fde\u63a5\u5145\u7535\u5668<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4ee3\u7801\u7684\u201c\u7f6a\u6076\u201d\u5206\u6790&#xff1a;<\/p>\n<li>\u6df1\u5ea6\u5d4c\u5957&#xff1a;if-else \u7684\u5d4c\u5957\u5c42\u7ea7\u6df1\u4e0d\u89c1\u5e95&#xff0c;\u4ee3\u7801\u7684\u201c\u5708\u590d\u6742\u5ea6\u201d\u6781\u9ad8\u3002\u9605\u8bfb\u8005\u9700\u8981\u5728\u5927\u8111\u4e2d\u7ef4\u62a4\u4e00\u4e2a\u590d\u6742\u7684\u4e0a\u4e0b\u6587\u6808\u624d\u80fd\u7406\u89e3\u5f53\u524d\u6267\u884c\u7684\u662f\u54ea\u4e2a\u5206\u652f\u7684\u903b\u8f91\u3002<\/li>\n<li>\u9690\u5f0f\u72b6\u6001&#xff1a;\u7cfb\u7edf\u7684\u72b6\u6001&#xff08;\u5982\u201c\u5145\u7535\u4e2d\u201d\u3001\u201c\u5df2\u5b8c\u6210\u201d\u3001\u201c\u6e29\u5ea6\u8fc7\u9ad8\u201d&#xff09;\u5e76\u6ca1\u6709\u88ab\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u7ba1\u7406&#xff0c;\u800c\u662f\u9690\u542b\u5728\u4e00\u7cfb\u5217\u5e03\u5c14\u6807\u5fd7\u4f4d&#xff08;is_charging_finished, is_battery_temp_high&#xff09;\u7684\u7ec4\u5408\u4e2d\u3002\u8fd9\u79cd\u9690\u5f0f\u72b6\u6001\u6781\u6613\u51fa\u9519&#xff0c;\u6bd4\u5982\u4e24\u4e2a\u6807\u5fd7\u4f4d\u7684\u7ec4\u5408\u53ef\u80fd\u5bf9\u5e94\u4e00\u4e2a\u672a\u5b9a\u4e49\u7684\u7cfb\u7edf\u72b6\u6001\u3002<\/li>\n<li>\u72b6\u6001\u8f6c\u6362\u903b\u8f91\u5206\u6563&#xff1a;\u4ece\u4e00\u4e2a\u72b6\u6001\u5230\u53e6\u4e00\u4e2a\u72b6\u6001\u7684\u8f6c\u6362\u6761\u4ef6&#xff08;\u4f8b\u5982&#xff0c;\u4ece\u201c\u5145\u7535\u4e2d\u201d\u5230\u201c\u6e29\u5ea6\u8fc7\u9ad8\u201d&#xff09;\u6563\u843d\u5728\u5404\u4e2a if \u8bed\u53e5\u4e2d&#xff0c;\u6ca1\u6709\u96c6\u4e2d\u7ba1\u7406\u3002\u5f53\u9700\u8981\u589e\u52a0\u6216\u4fee\u6539\u4e00\u4e2a\u8f6c\u6362\u89c4\u5219\u65f6&#xff0c;\u4f60\u5fc5\u987b\u901a\u8bfb\u6574\u4e2a\u51fd\u6570&#xff0c;\u6781\u6613\u9057\u6f0f\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u6027\u5dee&#xff1a;\u5982\u679c\u73b0\u5728\u8981\u589e\u52a0\u4e00\u4e2a\u201c\u7535\u6c60\u7535\u538b\u8fc7\u4f4e\u4fdd\u62a4\u201d\u7684\u72b6\u6001&#xff0c;\u6216\u8005\u4e00\u4e2a\u201c\u9884\u5145\u7535\u201d\u9636\u6bb5&#xff0c;\u4f60\u9700\u8981\u5728\u8fd9\u4e2a\u5df2\u7ecf\u81c3\u80bf\u7684 if-else \u8ff7\u5bab\u4e2d\u627e\u5230\u5408\u9002\u7684\u4f4d\u7f6e\u63d2\u5165\u65b0\u7684\u5224\u65ad&#xff0c;\u8fd9\u51e0\u4e4e\u5fc5\u7136\u4f1a\u7834\u574f\u73b0\u6709\u7684\u903b\u8f91&#xff0c;\u5f15\u5165\u65b0\u7684Bug\u3002<\/li>\n<li>\u96be\u4ee5\u6d4b\u8bd5&#xff1a;\u5982\u4f55\u6d4b\u8bd5\u201c\u5728\u5145\u7535\u4e2d\u4e14\u6e29\u5ea6\u8fc7\u9ad8\u65f6\u505c\u6b62\u5145\u7535\u201d\u8fd9\u4e2a\u573a\u666f&#xff1f;\u4f60\u9700\u8981\u7cbe\u5fc3\u6784\u9020\u4e00\u7cfb\u5217\u7684\u8f93\u5165\u6761\u4ef6&#xff08;is_charger_plugged_in &#061; true, is_charging_finished &#061; false, is_battery_temp_high &#061; true&#xff09;&#xff0c;\u5e76\u786e\u4fdd\u4ee3\u7801\u80fd\u7cbe\u786e\u5730\u8d70\u5230\u90a3\u4e2a\u6df1\u5c42\u5d4c\u5957\u7684 else \u5206\u652f\u3002\u6d4b\u8bd5\u7528\u4f8b\u7684\u7f16\u5199\u672c\u8eab\u5c31\u662f\u4e00\u573a\u5669\u68a6\u3002<\/li>\n<h4>1.3 \u4e3a\u4ec0\u4e48 if-else \u4f1a\u5931\u63a7&#xff1f;<\/h4>\n<p>if-else \u672c\u8eab\u662f\u7f16\u7a0b\u7684\u57fa\u672c\u6784\u4ef6&#xff0c;\u95ee\u9898\u4e0d\u5728\u4e8e\u5b83&#xff0c;\u800c\u5728\u4e8e\u6211\u4eec\u5982\u4f55\u4f7f\u7528\u5b83\u3002\u5f53 if-else \u88ab\u7528\u6765\u6a21\u62df\u72b6\u6001\u65f6&#xff0c;\u5b83\u5c31\u6210\u4e86\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n<ul>\n<li>\u7ebf\u6027\u601d\u7ef4 vs. \u72b6\u6001\u601d\u7ef4&#xff1a;if-else \u672c\u8d28\u4e0a\u662f\u7ebf\u6027\u7684\u3001\u8fc7\u7a0b\u5f0f\u7684\u3002\u5b83\u63cf\u8ff0\u7684\u662f\u201c\u5982\u679cA\u53d1\u751f&#xff0c;\u5219\u505aB&#xff1b;\u5426\u5219&#xff0c;\u5982\u679cC\u53d1\u751f&#xff0c;\u5219\u505aD\u201d\u3002\u800c\u5d4c\u5165\u5f0f\u7cfb\u7edf\u672c\u8d28\u4e0a\u662f\u4e8b\u4ef6\u9a71\u52a8\u548c\u72b6\u6001\u5316\u7684\u3002\u7cfb\u7edf\u5728\u5927\u90e8\u5206\u65f6\u95f4\u91cc\u662f\u201c\u9759\u6b62\u201d\u5728\u67d0\u4e2a\u72b6\u6001\u7684&#xff0c;\u53ea\u6709\u5f53\u4e00\u4e2a\u4e8b\u4ef6&#xff08;\u5982\u6309\u94ae\u6309\u4e0b\u3001\u6570\u636e\u5230\u8fbe\u3001\u5b9a\u65f6\u5668\u8d85\u65f6&#xff09;\u53d1\u751f\u65f6&#xff0c;\u5b83\u624d\u4f1a\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u72b6\u6001\u3002\u7528\u7ebf\u6027\u7684\u5de5\u5177\u53bb\u63cf\u8ff0\u72b6\u6001\u5316\u7684\u884c\u4e3a&#xff0c;\u672c\u8eab\u5c31\u662f\u4e00\u79cd\u9519\u914d\u3002<\/li>\n<li>\u72b6\u6001\u7206\u70b8&#xff1a;\u968f\u7740\u72b6\u6001\u548c\u4e8b\u4ef6\u7684\u589e\u52a0&#xff0c;if-else \u7684\u5206\u652f\u6570\u91cf\u4f1a\u5448\u6307\u6570\u7ea7\u589e\u957f\u3002\u5bf9\u4e8e N \u4e2a\u72b6\u6001\u548c M \u4e2a\u4e8b\u4ef6&#xff0c;\u7406\u8bba\u4e0a\u53ef\u80fd\u9700\u8981 N * M \u4e2a\u5224\u65ad\u5206\u652f\u3002\u8fd9\u79cd\u7ec4\u5408\u7206\u70b8\u662f if-else \u65e0\u6cd5\u627f\u53d7\u4e4b\u91cd\u3002<\/li>\n<li>\u7f3a\u4e4f\u5168\u5c40\u89c6\u56fe&#xff1a;if-else \u4ee3\u7801\u53ea\u5c55\u793a\u4e86\u5c40\u90e8\u7684\u903b\u8f91&#xff0c;\u65e0\u6cd5\u63d0\u4f9b\u4e00\u4e2a\u5173\u4e8e\u7cfb\u7edf\u6240\u6709\u53ef\u80fd\u72b6\u6001\u548c\u8f6c\u6362\u8def\u5f84\u7684\u5168\u5c40\u89c6\u56fe\u3002\u65b0\u63a5\u624b\u7684\u5de5\u7a0b\u5e08\u65e0\u6cd5\u901a\u8fc7\u9605\u8bfb\u4ee3\u7801\u5feb\u901f\u7406\u89e3\u7cfb\u7edf\u7684\u6574\u4f53\u884c\u4e3a\u6a21\u578b\u3002<\/li>\n<\/ul>\n<h4>1.4 \u7834\u5c40\u4e4b\u9053&#xff1a;\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;<\/h4>\n<p>\u6709\u9650\u72b6\u6001\u673a&#xff08;Finite State Machine, FSM&#xff09;\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u800c\u751f\u7684\u3002\u5b83\u662f\u4e00\u4e2a\u6570\u5b66\u6a21\u578b&#xff0c;\u4e5f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u5176\u6838\u5fc3\u601d\u60f3\u4e0e\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u884c\u4e3a\u6a21\u5f0f\u5929\u7136\u5951\u5408&#xff1a;<\/p>\n<p>\u4e00\u4e2a\u7cfb\u7edf\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u523b&#xff0c;\u53ea\u80fd\u5904\u4e8e\u6709\u9650\u4e2a\u72b6\u6001\u4e2d\u7684\u4e00\u4e2a\u3002\u5f53\u67d0\u4e2a\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u6839\u636e\u9884\u8bbe\u7684\u89c4\u5219&#xff0c;\u4ece\u5f53\u524d\u72b6\u6001\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u72b6\u6001&#xff0c;\u5e76\u53ef\u80fd\u4f34\u968f\u76f8\u5e94\u7684\u52a8\u4f5c\u3002<\/p>\n<p>FSM \u5c06\u590d\u6742\u7684\u903b\u8f91\u68b3\u7406\u5f97\u4e95\u4e95\u6709\u6761&#xff0c;\u5b83\u5e26\u6765\u4e86\u4ee5\u4e0b\u9769\u547d\u6027\u7684\u4f18\u52bf&#xff1a;<\/p>\n<li>\u7ed3\u6784\u5316\u4e0e\u6e05\u6670\u5316&#xff1a;\u5c06\u9690\u5f0f\u7684\u72b6\u6001\u663e\u5f0f\u5316&#xff0c;\u7528 enum \u5b9a\u4e49\u72b6\u6001&#xff0c;\u7528 switch-case \u6216\u72b6\u6001\u8868\u6765\u7ba1\u7406\u8f6c\u6362\u3002\u903b\u8f91\u53d8\u5f97\u6241\u5e73\u3001\u6e05\u6670&#xff0c;\u72b6\u6001\u548c\u8f6c\u6362\u4e00\u76ee\u4e86\u7136\u3002<\/li>\n<li>\u9ad8\u5185\u805a&#xff0c;\u4f4e\u8026\u5408&#xff1a;\u6bcf\u4e2a\u72b6\u6001\u7684\u903b\u8f91\u88ab\u5c01\u88c5\u5728\u4e00\u8d77&#xff08;\u4f8b\u5982&#xff0c;\u5728\u4e00\u4e2a case \u8bed\u53e5\u4e2d&#xff09;\u3002\u589e\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u4e00\u4e2a\u72b6\u6001&#xff0c;\u53ea\u9700\u6539\u52a8\u5c40\u90e8\u4ee3\u7801&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u72b6\u6001&#xff0c;\u6781\u5927\u5730\u964d\u4f4e\u4e86\u6a21\u5757\u95f4\u7684\u8026\u5408\u3002<\/li>\n<li>\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55&#xff1a;\u589e\u52a0\u65b0\u529f\u80fd&#xff08;\u5982\u65b0\u72b6\u6001&#xff09;\u53d8\u5f97\u50cf\u5728 enum \u4e2d\u52a0\u4e00\u884c\u3001\u5728 switch \u4e2d\u52a0\u4e00\u4e2a case \u4e00\u6837\u7b80\u5355\u3002<\/li>\n<li>\u5065\u58ee\u6027\u4e0e\u53ef\u9760\u6027&#xff1a;\u901a\u8fc7\u660e\u786e\u5b9a\u4e49\u6240\u6709\u5408\u6cd5\u7684\u72b6\u6001\u548c\u8f6c\u6362&#xff0c;\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u975e\u6cd5\u4e8b\u4ef6\u548c\u975e\u6cd5\u72b6\u6001\u8f6c\u6362&#xff0c;\u4f7f\u7cfb\u7edf\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u4e5f\u80fd\u4fdd\u6301\u7a33\u5b9a\u6216\u8fdb\u5165\u5b89\u5168\u72b6\u6001\u3002<\/li>\n<li>\u6587\u6863\u5316\u4e0e\u53ef\u6c9f\u901a\u6027&#xff1a;\u72b6\u6001\u8f6c\u6362\u56fe&#xff08;State Transition Diagram, STD&#xff09;\u672c\u8eab\u5c31\u662f\u4e00\u4efd\u5b8c\u7f8e\u7684\u8bbe\u8ba1\u6587\u6863\u3002\u5b83\u53ef\u4ee5\u88ab\u786c\u4ef6\u5de5\u7a0b\u5e08\u3001\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u751a\u81f3\u4ea7\u54c1\u7ecf\u7406\u5171\u540c\u7406\u89e3&#xff0c;\u6210\u4e3a\u56e2\u961f\u6c9f\u901a\u7684\u6865\u6881\u3002<\/li>\n<li>\u6613\u4e8e\u6d4b\u8bd5&#xff1a;\u53ef\u4ee5\u9488\u5bf9\u6bcf\u4e2a\u72b6\u6001\u548c\u6bcf\u4e2a\u4e8b\u4ef6\u72ec\u7acb\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b&#xff0c;\u6d4b\u8bd5\u8986\u76d6\u7387\u9ad8&#xff0c;\u5b9a\u4f4d\u95ee\u9898\u7cbe\u51c6\u3002<\/li>\n<h4>1.5 \u672c\u6587\u7684\u8def\u7ebf\u56fe<\/h4>\n<p>\u672c\u6587\u5c06\u4e0d\u4ec5\u4ec5\u505c\u7559\u5728FSM\u7684\u7406\u8bba\u5c42\u9762&#xff0c;\u800c\u662f\u5c06\u5176\u7f6e\u4e8e\u771f\u5b9e\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u73af\u5883\u4e2d&#xff0c;\u7ed3\u5408\u4e2d\u65ad\u3001\u73af\u5f62\u961f\u5217\u7b49\u5e95\u5c42\u673a\u5236&#xff0c;\u63d0\u4f9b\u4e00\u5957\u5b8c\u6574\u7684\u3001\u53ef\u843d\u5730\u7684\u9ad8\u7ea7\u7f16\u7a0b\u65b9\u6cd5\u8bba\u3002<\/p>\n<ul>\n<li>\u7b2c\u4e8c\u7ae0\u5c06\u6df1\u5165\u63a2\u8ba8\u4e2d\u65ad&#xff0c;\u8fd9\u662f\u5d4c\u5165\u5f0f\u7cfb\u7edf\u54cd\u5e94\u5916\u90e8\u4e16\u754c\u7684\u57fa\u7840&#xff0c;\u4e5f\u662f\u5e76\u53d1\u95ee\u9898\u7684\u6e90\u5934\u3002<\/li>\n<li>\u7b2c\u4e09\u7ae0\u5c06\u8be6\u89e3\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;&#xff0c;\u5b83\u662f\u89e3\u51b3\u4e2d\u65ad\u4e0e\u4e3b\u5faa\u73af\u3001\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005\u4e4b\u95f4\u6570\u636e\u5b89\u5168\u4f20\u9012\u7684\u5173\u952e\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li>\u7b2c\u56db\u7ae0\u5c06\u7cfb\u7edf\u4ecb\u7ecdFSM\u7684\u7406\u8bba\u4e0e\u5b9e\u73b0&#xff0c;\u4ece\u6700\u57fa\u7840\u7684switch-case\u5230\u9ad8\u7ea7\u7684\u51fd\u6570\u6307\u9488\u548c\u5206\u5c42\u72b6\u6001\u673a\u3002<\/li>\n<li>\u7b2c\u4e94\u3001\u516d\u7ae0\u5c06\u901a\u8fc7\u5145\u7535\u7ba1\u7406\u548c\u534f\u8bae\u89e3\u6790\u4e24\u4e2a\u5b9e\u6218\u6848\u4f8b&#xff0c;\u624b\u628a\u624b\u6559\u4f60\u5982\u4f55\u7528FSM\u91cd\u6784\u201c\u903b\u8f91\u5730\u72f1\u201d\u3002<\/li>\n<li>\u7b2c\u4e03\u7ae0\u5c06\u5c55\u793aFSM\u5982\u4f55\u4e0eRTOS\u7ed3\u5408&#xff0c;\u6784\u5efa\u66f4\u5f3a\u5927\u7684\u591a\u4efb\u52a1\u7cfb\u7edf\u3002<\/li>\n<li>\u7b2c\u516b\u7ae0\u5c06\u63a2\u7d22\u66f4\u9ad8\u7ea7\u7684\u67b6\u6784\u6a21\u5f0f&#xff0c;\u5982\u6d3b\u8dc3\u5bf9\u8c61\u7b49\u3002<\/li>\n<\/ul>\n<p>\u51c6\u5907\u597d\u4e86\u5417&#xff1f;\u8ba9\u6211\u4eec\u4e00\u8d77\u8e0f\u4e0a\u8fd9\u573a\u4ece\u201c\u7801\u519c\u201d\u5230\u201c\u67b6\u6784\u5e08\u201d\u7684\u8715\u53d8\u4e4b\u65c5\u3002<\/p>\n<hr \/>\n<h3>\u7b2c\u4e8c\u7ae0&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u77f3\u2014\u2014\u4e2d\u65ad\u4e0e\u5e76\u53d1<\/h3>\n<p>\u5728\u6df1\u5165\u72b6\u6001\u673a\u4e4b\u524d&#xff0c;\u6211\u4eec\u5fc5\u987b\u5148\u7406\u89e3\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u4e00\u4e2a\u6838\u5fc3\u7279\u6027&#xff1a;\u5e76\u53d1&#xff0c;\u4ee5\u53ca\u5b9e\u73b0\u5e76\u53d1\u7684\u4e3b\u8981\u673a\u5236\u2014\u2014\u4e2d\u65ad\u3002\u72b6\u6001\u673a\u5e38\u5e38\u9700\u8981\u4e0e\u4e2d\u65ad\u914d\u5408&#xff0c;\u5904\u7406\u5f02\u6b65\u4e8b\u4ef6&#xff0c;\u56e0\u6b64\u7406\u89e3\u4e2d\u65ad\u7684\u672c\u8d28\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>2.1 \u4ec0\u4e48\u662f\u4e2d\u65ad&#xff1f;\u2014\u2014CPU\u7684\u201c\u7d27\u6025\u4e8b\u52a1\u201d\u5904\u7406\u673a\u5236<\/h4>\n<p>\u60f3\u8c61\u4f60\u6b63\u5728\u4e13\u5fc3\u9605\u8bfb\u4e00\u672c\u4e66&#xff08;\u4e3b\u7a0b\u5e8f\u6b63\u5728\u6267\u884c&#xff09;&#xff0c;\u7a81\u7136\u7535\u8bdd\u94c3\u54cd\u4e86&#xff08;\u4e2d\u65ad\u4fe1\u53f7\u4ea7\u751f&#xff09;\u3002\u4f60\u4f1a\u600e\u4e48\u505a&#xff1f;<\/p>\n<li>\u5728\u5f53\u524d\u8bfb\u5230\u7684\u4f4d\u7f6e\u653e\u4e00\u4e2a\u4e66\u7b7e&#xff08;\u4fdd\u5b58\u5f53\u524d\u4e0a\u4e0b\u6587&#xff0c;\u5982PC\u3001\u5bc4\u5b58\u5668\u7b49&#xff09;\u3002<\/li>\n<li>\u53bb\u63a5\u542c\u7535\u8bdd&#xff08;\u8df3\u8f6c\u5230\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8fISR\u6267\u884c&#xff09;\u3002<\/li>\n<li>\u901a\u8bdd\u7ed3\u675f\u540e&#xff0c;\u6302\u65ad\u7535\u8bdd&#xff08;ISR\u6267\u884c\u5b8c\u6bd5&#xff09;\u3002<\/li>\n<li>\u56de\u5230\u4e66\u684c\u524d&#xff0c;\u62ff\u8d77\u4e66\u7b7e&#xff0c;\u4ece\u521a\u624d\u4e2d\u65ad\u7684\u5730\u65b9\u7ee7\u7eed\u9605\u8bfb&#xff08;\u6062\u590d\u4e0a\u4e0b\u6587&#xff0c;\u8fd4\u56de\u4e3b\u7a0b\u5e8f&#xff09;\u3002<\/li>\n<p>\u8fd9\u5c31\u662f\u4e2d\u65ad\u7684\u672c\u8d28&#xff1a;\u4e00\u79cd\u8ba9CPU\u6682\u505c\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u4e3b\u7a0b\u5e8f&#xff0c;\u53bb\u5904\u7406\u4e00\u4e2a\u66f4\u7d27\u6025\u3001\u66f4\u77ed\u6682\u7684\u4e8b\u4ef6&#xff0c;\u5904\u7406\u5b8c\u540e\u518d\u8fd4\u56de\u4e3b\u7a0b\u5e8f\u7684\u673a\u5236\u3002<\/p>\n<ul>\n<li>\u5f02\u6b65\u6027&#xff1a;\u4e2d\u65ad\u7684\u53d1\u751f\u662f\u4e0d\u53ef\u9884\u6d4b\u7684&#xff0c;\u4e0e\u4e3b\u7a0b\u5e8f\u7684\u6267\u884c\u65e0\u5173\u3002\u5b83\u7531\u786c\u4ef6&#xff08;\u5982GPIO\u7535\u5e73\u53d8\u5316\u3001\u5b9a\u65f6\u5668\u6ea2\u51fa\u3001UART\u6570\u636e\u5230\u8fbe&#xff09;\u6216\u8f6f\u4ef6&#xff08;\u5982\u6267\u884c\u4e00\u6761\u7279\u6b8a\u6307\u4ee4&#xff09;\u89e6\u53d1\u3002<\/li>\n<li>\u62a2\u5360\u6027&#xff1a;\u4e2d\u65ad\u53ef\u4ee5\u201c\u62a2\u5360\u201d\u4e3b\u7a0b\u5e8f\u7684\u6267\u884c&#xff0c;\u5177\u6709\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u751a\u81f3\u53ef\u4ee5\u62a2\u5360\u4f4e\u4f18\u5148\u7ea7\u4e2d\u65ad\u7684\u6267\u884c&#xff08;\u4e2d\u65ad\u5d4c\u5957&#xff09;\u3002<\/li>\n<li>\u77ed\u6682\u6027&#xff1a;\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f&#xff08;ISR&#xff09;\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u77ed\u5c0f\u7cbe\u608d&#xff0c;\u56e0\u4e3a\u5b83\u4f1a\u6682\u65f6\u201c\u51bb\u7ed3\u201d\u4e3b\u7a0b\u5e8f\u548c\u5176\u4ed6\u4f4e\u4f18\u5148\u7ea7\u4e2d\u65ad\u3002<\/li>\n<\/ul>\n<h4>2.2 \u4e2d\u65ad\u7684\u751f\u547d\u5468\u671f&#xff1a;\u4ece\u89e6\u53d1\u5230\u8fd4\u56de<\/h4>\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684\u4e2d\u65ad\u5904\u7406\u8fc7\u7a0b\u5305\u542b\u786c\u4ef6\u548c\u8f6f\u4ef6\u4e24\u4e2a\u5c42\u9762\u7684\u64cd\u4f5c&#xff1a;<\/p>\n<li>\u4e2d\u65ad\u8bf7\u6c42&#xff08;Request&#xff09;&#xff1a;\u5916\u8bbe\u6216\u5185\u90e8\u6a21\u5757\u5411\u4e2d\u65ad\u63a7\u5236\u5668&#xff08;\u5982NVIC in ARM Cortex-M&#xff09;\u53d1\u9001\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\u3002<\/li>\n<li>\u4e2d\u65ad\u88c1\u51b3&#xff08;Arbitration&#xff09;&#xff1a;\u4e2d\u65ad\u63a7\u5236\u5668\u6839\u636e\u9884\u8bbe\u7684\u4f18\u5148\u7ea7&#xff0c;\u51b3\u5b9a\u662f\u5426\u54cd\u5e94\u8fd9\u4e2a\u4e2d\u65ad\u3002\u5982\u679c\u5f53\u524d\u6709\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u6b63\u5728\u5904\u7406&#xff0c;\u65b0\u7684\u4e2d\u65ad\u4f1a\u88ab\u6302\u8d77&#xff08;Pending&#xff09;\u3002<\/li>\n<li>\u4e0a\u4e0b\u6587\u4fdd\u5b58&#xff08;Context Saving&#xff09;&#xff1a;CPU\u5728\u6267\u884c\u5b8c\u5f53\u524d\u6307\u4ee4\u540e&#xff0c;\u54cd\u5e94\u4e2d\u65ad\u3002\u786c\u4ef6\u81ea\u52a8\u5c06\u5173\u952e\u7684\u5bc4\u5b58\u5668&#xff08;\u5982\u7a0b\u5e8f\u8ba1\u6570\u5668PC\u3001\u72b6\u6001\u5bc4\u5b58\u5668PSW\u3001\u901a\u7528\u5bc4\u5b58\u5668\u7b49&#xff09;\u538b\u5165\u5f53\u524d\u4efb\u52a1\u7684\u5806\u6808\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u900f\u660e\u7684&#xff0c;\u7531\u786c\u4ef6\u5b8c\u6210\u3002<\/li>\n<li>\u5411\u91cf\u8df3\u8f6c&#xff08;Vector Fetch&#xff09;&#xff1a;CPU\u6839\u636e\u4e2d\u65ad\u53f7&#xff0c;\u4ece\u4e2d\u65ad\u5411\u91cf\u8868\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f&#xff08;ISR&#xff09;\u7684\u5165\u53e3\u5730\u5740&#xff0c;\u5e76\u8df3\u8f6c\u8fc7\u53bb\u3002<\/li>\n<li>\u6267\u884cISR&#xff1a;\u5f00\u59cb\u6267\u884c\u7528\u6237\u7f16\u5199\u7684C\/C&#043;&#043;\u4e2d\u65ad\u5904\u7406\u51fd\u6570\u3002\u8fd9\u662f\u552f\u4e00\u9700\u8981\u7a0b\u5e8f\u5458\u4ecb\u5165\u7684\u90e8\u5206\u3002<\/li>\n<li>\u4e0a\u4e0b\u6587\u6062\u590d&#xff08;Context Restoring&#xff09;&#xff1a;ISR\u6267\u884c\u5b8c\u6bd5&#xff08;\u901a\u5e38\u901a\u8fc7\u4e00\u4e2a\u7279\u6b8a\u7684\u8fd4\u56de\u6307\u4ee4&#xff0c;\u5982 BX LR \u6216 RETI&#xff09;\u3002\u786c\u4ef6\u81ea\u52a8\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u4e4b\u524d\u4fdd\u5b58\u7684\u5bc4\u5b58\u5668&#xff0c;\u6062\u590d\u4e3b\u7a0b\u5e8f\u7684\u6267\u884c\u73b0\u573a\u3002<\/li>\n<li>\u8fd4\u56de\u4e3b\u7a0b\u5e8f&#xff1a;CPU\u4ece\u88ab\u4e2d\u65ad\u7684\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u6307\u4ee4\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<h4>2.3 \u4e2d\u65ad\u4f18\u5148\u7ea7\u4e0e\u5d4c\u5957&#xff1a;\u8c01\u66f4\u7d27\u6025&#xff1f;<\/h4>\n<p>\u5728\u73b0\u4ee3MCU\u4e2d&#xff0c;\u901a\u5e38\u90fd\u6709\u4e00\u4e2a\u5d4c\u5957\u5411\u91cf\u4e2d\u65ad\u63a7\u5236\u5668&#xff08;NVIC&#xff09;&#xff0c;\u5b83\u7ba1\u7406\u7740\u6570\u5341\u751a\u81f3\u4e0a\u767e\u4e2a\u4e2d\u65ad\u6e90\u3002<\/p>\n<ul>\n<li>\u4f18\u5148\u7ea7&#xff1a;\u6bcf\u4e2a\u4e2d\u65ad\u90fd\u53ef\u4ee5\u88ab\u8d4b\u4e88\u4e00\u4e2a\u4f18\u5148\u7ea7\u3002\u6570\u503c\u8d8a\u5c0f&#xff0c;\u4f18\u5148\u7ea7\u8d8a\u9ad8\u3002\u5f53\u591a\u4e2a\u4e2d\u65ad\u540c\u65f6\u53d1\u751f\u65f6&#xff0c;CPU\u4f1a\u5148\u54cd\u5e94\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u90a3\u4e2a\u3002<\/li>\n<li>\u62a2\u5360\u4f18\u5148\u7ea7 vs. \u5b50\u4f18\u5148\u7ea7&#xff1a;\u5728ARM Cortex-M\u4e2d&#xff0c;\u4f18\u5148\u7ea7\u88ab\u5206\u4e3a\u62a2\u5360\u4f18\u5148\u7ea7\u548c\u5b50\u4f18\u5148\u7ea7\u3002\n<ul>\n<li>\u62a2\u5360\u4f18\u5148\u7ea7&#xff08;Preemption Priority&#xff09;&#xff1a;\u51b3\u5b9a\u4e86\u4e2d\u65ad\u662f\u5426\u53ef\u4ee5\u5d4c\u5957\u3002\u9ad8\u62a2\u5360\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u53ef\u4ee5\u6253\u65ad\u4f4e\u62a2\u5360\u4f18\u5148\u7ea7\u4e2d\u65ad\u7684ISR\u3002<\/li>\n<li>\u5b50\u4f18\u5148\u7ea7&#xff08;Subpriority&#xff09;&#xff1a;\u5f53\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u62a2\u5360\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u540c\u65f6\u6302\u8d77\u65f6&#xff0c;\u5b50\u4f18\u5148\u7ea7\u9ad8\u7684\u4f1a\u5148\u88ab\u5904\u7406\u3002\u4f46\u5b83\u4eec\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92\u5d4c\u5957\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e2d\u65ad\u5d4c\u5957&#xff1a;\u5982\u679c\u4e00\u4e2a\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u5728ISR\u6267\u884c\u671f\u95f4\u53d1\u751f&#xff0c;CPU\u4f1a\u518d\u6b21\u6682\u505c\u5f53\u524d\u7684ISR&#xff0c;\u53bb\u6267\u884c\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684ISR\u3002\u8fd9\u5f62\u6210\u4e86\u4e2d\u65ad\u7684\u5d4c\u5957\u3002<\/li>\n<\/ul>\n<p>\u8bbe\u8ba1\u8b66\u793a&#xff1a;\u8fc7\u5ea6\u7684\u4e2d\u65ad\u5d4c\u5957\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u548c\u7cfb\u7edf\u884c\u4e3a\u4e0d\u53ef\u9884\u6d4b\u3002\u901a\u5e38\u5efa\u8bae\u4fdd\u6301\u4e2d\u65ad\u7cfb\u7edf\u7684\u7b80\u5355\u6027&#xff0c;\u6216\u8005\u5728RTOS\u4e2d\u901a\u8fc7\u4f18\u5148\u7ea7\u5929\u82b1\u677f\u534f\u8bae\u7b49\u673a\u5236\u6765\u7ba1\u7406\u3002<\/p>\n<h4>2.4 \u7ade\u6001\u6761\u4ef6\u4e0e\u539f\u5b50\u64cd\u4f5c&#xff1a;\u5e76\u53d1\u4e16\u754c\u7684\u201c\u5e7d\u7075\u201d<\/h4>\n<p>\u5f53\u4e3b\u7a0b\u5e8f\u548cISR&#xff08;\u6216\u591a\u4e2aISR&#xff09;\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u4e2a\u5171\u4eab\u8d44\u6e90&#xff08;\u5982\u5168\u5c40\u53d8\u91cf\u3001\u5916\u8bbe\u5bc4\u5b58\u5668&#xff09;\u65f6&#xff0c;\u5c31\u4f1a\u51fa\u73b0\u7ade\u6001\u6761\u4ef6&#xff08;Race Condition&#xff09;\u3002<\/p>\n<p>\u7ecf\u5178\u4f8b\u5b50&#xff1a; \u5047\u8bbe\u4e00\u4e2a32\u4f4d\u7684\u5168\u5c40\u8ba1\u6570\u5668 g_tick_count \u5728\u4e3b\u5faa\u73af\u4e2d\u88ab\u8bfb\u53d6&#xff0c;\u5728\u4e00\u4e2a1ms\u7684\u5b9a\u65f6\u5668ISR\u4e2d\u88ab\u589e\u52a0\u3002<\/p>\n<p>\/\/ \u4e3b\u5faa\u73af<br \/>\nuint32_t current_ticks &#061; g_tick_count; \/\/ \u8bfb\u53d6\u64cd\u4f5c&#xff0c;\u53ef\u80fd\u9700\u8981\u591a\u6761\u6307\u4ee4<\/p>\n<p>\/\/ \u5b9a\u65f6\u5668ISR (\u6bcf1ms\u89e6\u53d1\u4e00\u6b21)<br \/>\nvoid SysTick_Handler(void) {<br \/>\n    g_tick_count&#043;&#043;; \/\/ &#034;\u8bfb-\u6539-\u5199&#034;\u64cd\u4f5c&#xff0c;\u4e5f\u9700\u8981\u591a\u6761\u6307\u4ee4<br \/>\n}<\/p>\n<p>\u572832\u4f4dMCU\u4e0a&#xff0c;g_tick_count&#043;&#043; \u53ef\u80fd\u88ab\u7f16\u8bd1\u6210\u4e09\u6761\u6307\u4ee4&#xff1a;<\/p>\n<li>LDR R0, [g_tick_count_addr] ; \u4ece\u5185\u5b58\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668<\/li>\n<li>ADD R0, R0, #1 ; \u5bc4\u5b58\u5668\u52a01<\/li>\n<li>STR R0, [g_tick_count_addr] ; \u5199\u56de\u5185\u5b58<\/li>\n<p>\u5982\u679c\u4e3b\u5faa\u73af\u5728\u6267\u884c\u5b8c LDR \u4e4b\u540e\u3001STR \u4e4b\u524d\u88abISR\u6253\u65ad&#xff0c;ISR\u5b8c\u6574\u5730\u6267\u884c\u4e86 g_tick_count&#043;&#043;&#xff0c;\u90a3\u4e48\u5f53\u4e3b\u5faa\u73af\u6062\u590d\u65f6&#xff0c;\u5b83\u4f1a\u7528\u81ea\u5df1\u5bc4\u5b58\u5668\u4e2d\u7684\u65e7\u503c\u8986\u76d6\u6389ISR\u5df2\u7ecf\u66f4\u65b0\u7684\u503c&#xff0c;\u5bfc\u81f4\u4e00\u6b21\u8ba1\u6570\u4e22\u5931\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u539f\u5b50\u64cd\u4f5c&#xff08;Atomic Operation&#xff09;<\/p>\n<p>\u539f\u5b50\u64cd\u4f5c\u662f\u6307\u4e00\u4e2a\u4e0d\u53ef\u88ab\u4e2d\u65ad\u7684\u64cd\u4f5c\u3002\u8981\u5b9e\u73b0\u5b83&#xff0c;\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5&#xff1a;<\/p>\n<li>\n<p>\u4f7f\u7528\u539f\u5b50\u6307\u4ee4&#xff1a;\u73b0\u4ee3CPU\u63d0\u4f9b\u4e86\u7279\u6b8a\u7684\u539f\u5b50\u6307\u4ee4\u3002\u5728ARM Cortex-M\u4e2d&#xff0c;\u6709 LDREX\/STREX \u6307\u4ee4\u5bf9&#xff0c;\u53ef\u4ee5\u5b9e\u73b0\u201c\u94fe\u63a5\/\u72ec\u5360\u201d\u8bbf\u95ee&#xff0c;\u786e\u4fdd\u201c\u8bfb-\u6539-\u5199\u201d\u7684\u539f\u5b50\u6027\u3002CMSIS\u5e93\u63d0\u4f9b\u4e86\u5c01\u88c5\u597d\u7684\u51fd\u6570&#xff0c;\u5982 atomic_add, atomic_sub \u7b49\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6807\u5fd7\u4f4d&#xff0c;\u53ef\u4ee5\u4f7f\u7528 LDREXB\/STREXB\u3002<\/p>\n<p>#include &#034;cmsis_compiler.h&#034;<\/p>\n<p>\/\/ \u539f\u5b50\u5730\u589e\u52a0\u4e00\u4e2a\u53d8\u91cf<br \/>\nvoid atomic_increment(uint32_t *var) {<br \/>\n    uint32_t val;<br \/>\n    do {<br \/>\n        val &#061; __LDREXW(var); \/\/ \u94fe\u63a5\u52a0\u8f7d<br \/>\n    } while (__STREXW(val &#043; 1, var) !&#061; 0); \/\/ \u5c1d\u8bd5\u72ec\u5360\u5b58\u50a8&#xff0c;\u5982\u679c\u5931\u8d25\u5219\u91cd\u8bd5<br \/>\n}<\/p>\n<\/li>\n<li>\n<p>\u5173\u4e2d\u65ad\/\u5173\u8c03\u5ea6&#xff1a;\u6700\u7b80\u5355\u7c97\u66b4\u4f46\u6709\u6548\u7684\u65b9\u6cd5\u3002\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u524d&#xff0c;\u5173\u95ed\u5168\u5c40\u4e2d\u65ad&#xff08;\u6216\u5728RTOS\u4e2d\u5173\u95ed\u4efb\u52a1\u8c03\u5ea6&#xff09;&#xff0c;\u8bbf\u95ee\u7ed3\u675f\u540e\u518d\u6062\u590d\u3002\u8fd9\u80fd\u4fdd\u8bc1\u4ee3\u7801\u6bb5\u4e0d\u88ab\u6253\u65ad\u3002<\/p>\n<p>\/\/ \u7b80\u5355\u5b9e\u73b0<br \/>\n__disable_irq(); \/\/ \u5173\u95ed\u5168\u5c40\u4e2d\u65ad<br \/>\ng_shared_variable&#043;&#043;;<br \/>\n__enable_irq();  \/\/ \u6062\u590d\u5168\u5c40\u4e2d\u65ad<\/p>\n<p>\/\/ RTOS\u4e2d\u66f4\u4f18\u7684\u5b9e\u73b0<br \/>\nvTaskSuspendAll(); \/\/ \u6302\u8d77\u8c03\u5ea6\u5668&#xff0c;\u9632\u6b62\u4efb\u52a1\u5207\u6362<br \/>\ng_shared_variable&#043;&#043;;<br \/>\nxTaskResumeAll();  \/\/ \u6062\u590d\u8c03\u5ea6\u5668<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u4f1a\u589e\u52a0\u7cfb\u7edf\u7684\u4e2d\u65ad\u5ef6\u8fdf&#xff08;Latency&#xff09;&#xff0c;\u5f71\u54cd\u5b9e\u65f6\u6027\u3002\u5e94\u5c3d\u91cf\u7f29\u77ed\u5173\u4e2d\u65ad\u7684\u65f6\u95f4\u3002<\/p>\n<\/li>\n<h4>2.5 \u4e34\u754c\u533a&#xff08;Critical Section&#xff09;&#xff1a;\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u7684\u201c\u5723\u6240\u201d<\/h4>\n<p>\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u4ee3\u7801\u6bb5\u88ab\u79f0\u4e3a\u4e34\u754c\u533a\u3002\u8fdb\u5165\u4e34\u754c\u533a\u524d\u5fc5\u987b\u201c\u4e0a\u9501\u201d&#xff08;\u5982\u5173\u4e2d\u65ad&#xff09;&#xff0c;\u79bb\u5f00\u540e\u5fc5\u987b\u201c\u89e3\u9501\u201d&#xff08;\u5982\u5f00\u4e2d\u65ad&#xff09;\u3002<\/p>\n<p>\u8bbe\u8ba1\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\u5feb\u8fdb\u5feb\u51fa&#xff1a;\u4e34\u754c\u533a\u5185\u7684\u4ee3\u7801\u5fc5\u987b\u6781\u5176\u7b80\u77ed&#xff0c;\u53ea\u5305\u542b\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u76f4\u63a5\u8bbf\u95ee\u3002\u7edd\u5bf9\u4e0d\u80fd\u5728\u4e34\u754c\u533a\u5185\u8c03\u7528\u53ef\u80fd\u963b\u585e\u7684\u51fd\u6570&#xff08;\u5982 HAL_Delay(), vTaskDelay(), printf()&#xff09;\u3002<\/li>\n<li>\u907f\u514d\u5d4c\u5957&#xff1a;\u5c3d\u91cf\u907f\u514d\u5728\u4e34\u754c\u533a\u5185\u518d\u8fdb\u5165\u53e6\u4e00\u4e2a\u4e34\u754c\u533a&#xff08;\u5982\u5728\u4e00\u4e2a\u5df2\u5173\u4e2d\u65ad\u7684\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u4e5f\u4f1a\u5173\u4e2d\u65ad\u7684\u51fd\u6570&#xff09;&#xff0c;\u8fd9\u4f1a\u4f7f\u7cfb\u7edf\u72b6\u6001\u53d8\u5f97\u590d\u6742\u3002<\/li>\n<li>\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898&#xff1a;\u5728RTOS\u4e2d&#xff0c;\u5982\u679c\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u6301\u6709\u9501&#xff08;\u8fdb\u5165\u4e34\u754c\u533a&#xff09;&#xff0c;\u800c\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u8bd5\u56fe\u83b7\u53d6\u540c\u4e00\u4e2a\u9501&#xff0c;\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u4f1a\u88ab\u963b\u585e&#xff0c;\u7b49\u5f85\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u91ca\u653e\u9501\u3002\u5982\u679c\u6b64\u65f6\u6709\u4e2d\u7b49\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u5c31\u7eea&#xff0c;\u5b83\u4f1a\u62a2\u5360\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1&#xff0c;\u5bfc\u81f4\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u88ab\u65e0\u9650\u671f\u5ef6\u8fdf\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528\u4f18\u5148\u7ea7\u7ee7\u627f\u534f\u8bae\u3002<\/li>\n<\/ul>\n<h4>2.6 \u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f&#xff08;ISR&#xff09;\u7684\u8bbe\u8ba1\u54f2\u5b66&#xff1a;\u5feb\u8fdb\u5feb\u51fa<\/h4>\n<p>ISR\u662f\u201c\u7279\u7b49\u516c\u6c11\u201d&#xff0c;\u4f46\u4e5f\u80cc\u8d1f\u7740\u201c\u5fc5\u987b\u9ad8\u6548\u201d\u7684\u6c89\u91cd\u67b7\u9501\u3002\u4e00\u4e2a\u7cdf\u7cd5\u7684ISR\u4f1a\u62d6\u6162\u6574\u4e2a\u7cfb\u7edf&#xff0c;\u751a\u81f3\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/p>\n<p>ISR\u7684\u201c\u4e09\u4e0d\u505a\u201d\u539f\u5219&#xff1a;<\/p>\n<li>\u4e0d\u505a\u8017\u65f6\u64cd\u4f5c&#xff1a;\u4e0d\u8981\u4f7f\u7528\u5faa\u73af\u7b49\u5f85\u3001\u4e0d\u8981\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u3001\u4e0d\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u62f7\u8d1d\u3002<\/li>\n<li>\u4e0d\u8c03\u7528\u963b\u585e\u51fd\u6570&#xff1a;\u7edd\u5bf9\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u53ef\u80fd\u5bfc\u81f4\u4efb\u52a1\u5207\u6362\u6216\u7b49\u5f85\u7684\u51fd\u6570&#xff0c;\u5982RTOS\u7684\u5ef6\u65f6\u3001\u4fe1\u53f7\u91cf\u7b49\u5f85\u3001\u4e92\u65a5\u9501\u83b7\u53d6\u7b49\u3002<\/li>\n<li>\u4e0d\u8fdb\u884c\u590d\u6742\u7684I\/O&#xff1a;\u907f\u514d\u5728ISR\u4e2d\u76f4\u63a5\u8c03\u7528 printf \u6216\u8fdb\u884c\u6162\u901f\u7684SPI\/I2C\u901a\u4fe1\u3002<\/li>\n<p>ISR\u7684\u201c\u4e09\u8981\u505a\u201d\u539f\u5219&#xff1a;<\/p>\n<li>\u8981\u505a\u6700\u5c11\u7684\u5de5\u4f5c&#xff1a;\u53ea\u505a\u6700\u6838\u5fc3\u3001\u6700\u7d27\u6025\u7684\u4e8b\u60c5\u3002\u4f8b\u5982&#xff0c;\u4eceUART\u6570\u636e\u5bc4\u5b58\u5668\u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282&#xff0c;\u7136\u540e\u7acb\u5373\u9000\u51fa\u3002<\/li>\n<li>\u8981\u6e05\u9664\u4e2d\u65ad\u6807\u5fd7&#xff1a;\u786e\u4fdd\u5728ISR\u9000\u51fa\u524d&#xff0c;\u6e05\u9664\u89e6\u53d1\u8be5\u4e2d\u65ad\u7684\u6807\u5fd7\u4f4d&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u4e2d\u65ad\u65e0\u9650\u89e6\u53d1\u3002<\/li>\n<li>\u8981\u4e0e\u4e3b\u5faa\u73af\/\u4efb\u52a1\u901a\u4fe1&#xff1a;\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u4f4d\u3001\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u91cf\u3001\u6216\u8005&#xff08;\u6700\u5e38\u7528\u7684&#xff09;\u5411\u4e00\u4e2a\u73af\u5f62\u961f\u5217\u4e2d\u653e\u5165\u6570\u636e&#xff0c;\u5c06\u201c\u5269\u4f59\u7684\u5de5\u4f5c\u201d\u63a8\u8fdf\u5230\u4e3b\u5faa\u73af\u6216\u4e00\u4e2a\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u4e2d\u53bb\u5b8c\u6210\u3002\u8fd9\u88ab\u79f0\u4e3a**\u201c\u9876\u534a\u90e8\/\u5e95\u534a\u90e8\u201d**\u5904\u7406\u673a\u5236\u3002ISR\u662f\u201c\u9876\u534a\u90e8\u201d&#xff0c;\u4e3b\u5faa\u73af\/\u4efb\u52a1\u662f\u201c\u5e95\u534a\u90e8\u201d\u3002<\/li>\n<h4>2.7 \u5b9e\u8df5&#xff1a;\u5982\u4f55\u5b89\u5168\u5730\u5728ISR\u4e0e\u4e3b\u5faa\u73af\u95f4\u4f20\u9012\u6570\u636e&#xff1f;<\/h4>\n<p>\u8fd9\u6b63\u662f\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;\u5927\u663e\u8eab\u624b\u7684\u573a\u666f\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8be6\u7ec6\u63a2\u8ba8\u5b83\u3002\u8fd9\u91cc\u5148\u7ed9\u51fa\u4e00\u4e2a\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u4f20\u9012\u5355\u4e2a\u6807\u5fd7\u4f4d\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\/\/ \u4f7f\u7528 volatile \u5173\u952e\u5b57\u544a\u8bc9\u7f16\u8bd1\u5668&#xff0c;\u8fd9\u4e2a\u53d8\u91cf\u53ef\u80fd\u5728\u4efb\u4f55\u65f6\u5019\u88ab\u5916\u90e8&#xff08;\u5982ISR&#xff09;\u4fee\u6539<br \/>\n\/\/ \u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u6389\u5bf9\u5b83\u7684\u8bfb\u5199\u64cd\u4f5c<br \/>\nvolatile bool g_data_ready_flag &#061; false;<\/p>\n<p>\/\/ \u4e3b\u5faa\u73af<br \/>\nvoid main_loop() {<br \/>\n    while (1) {<br \/>\n        if (g_data_ready_flag) {<br \/>\n            \/\/ 1. \u8fdb\u5165\u4e34\u754c\u533a&#xff0c;\u5b89\u5168\u5730\u8bfb\u53d6\u5e76\u6e05\u9664\u6807\u5fd7<br \/>\n            __disable_irq();<br \/>\n            g_data_ready_flag &#061; false;<br \/>\n            __enable_irq();<\/p>\n<p>            \/\/ 2. \u5904\u7406\u6570\u636e&#xff08;\u8fd9\u90e8\u5206\u53ef\u4ee5\u8017\u65f6&#xff09;<br \/>\n            process_data();<br \/>\n        }<br \/>\n    \/\/ &#8230; \u5176\u4ed6\u4efb\u52a1<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ UART\u63a5\u6536\u5b8c\u6210\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f<br \/>\nvoid USART_RX_IRQHandler(void) {<br \/>\n    \/\/ 1. \u8bfb\u53d6\u63a5\u6536\u5230\u7684\u5b57\u8282\u5e76\u5b58\u5165\u7f13\u51b2\u533a&#xff08;\u8fd9\u4e2a\u64cd\u4f5c\u8981\u5feb&#xff09;<br \/>\n    uint8_t byte &#061; USART_DR;<\/p>\n<p>    \/\/ 2. \u8bbe\u7f6e\u6807\u5fd7\u4f4d&#xff0c;\u901a\u77e5\u4e3b\u5faa\u73af<br \/>\n    \/\/ \u5bf9\u4e8e\u7b80\u5355\u7684\u5e03\u5c14\u6807\u5fd7&#xff0c;\u5728\u67d0\u4e9b\u67b6\u6784\u4e0a\u76f4\u63a5\u8d4b\u503c\u662f\u539f\u5b50\u7684<br \/>\n    \/\/ \u4f46\u4e3a\u4e86\u53ef\u79fb\u690d\u6027\u548c\u5b89\u5168\u6027&#xff0c;\u6700\u597d\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u6216\u5728\u4e34\u754c\u533a\u5185\u64cd\u4f5c<br \/>\n    g_data_ready_flag &#061; true;<\/p>\n<p>    \/\/ 3. \u6e05\u9664\u4e2d\u65ad\u6807\u5fd7\u4f4d<br \/>\n    USART_CLEAR_RX_FLAG();<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u7b80\u5355\u7684\u6a21\u5f0f\u662f\u8bb8\u591a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u7840\u3002\u4f46\u5f53\u9700\u8981\u4f20\u9012\u7684\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6807\u5fd7&#xff0c;\u800c\u662f\u4e00\u4e2a\u6570\u636e\u6d41\u65f6&#xff0c;\u6211\u4eec\u5c31\u9700\u8981\u66f4\u5f3a\u5927\u7684\u5de5\u5177\u2014\u2014\u73af\u5f62\u961f\u5217\u3002<\/p>\n<hr \/>\n<h3>\u7b2c\u4e09\u7ae0&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u52a8\u8109\u2014\u2014\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;<\/h3>\n<p>\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d&#xff0c;\u6570\u636e\u751f\u4ea7\u548c\u6d88\u8d39\u7684\u901f\u7387\u5f80\u5f80\u4e0d\u5339\u914d\u3002\u4f8b\u5982&#xff0c;UART\u53ef\u80fd\u4ee5115200bps\u7684\u901f\u7387\u8fde\u7eed\u4ea7\u751f\u6570\u636e&#xff0c;\u800c\u4e3b\u5faa\u73af\u53ef\u80fd\u9700\u8981\u51e0\u5341\u6beb\u79d2\u624d\u80fd\u5904\u7406\u5b8c\u4e00\u5e27\u6570\u636e\u3002\u5982\u679c\u6ca1\u6709\u7f13\u51b2\u533a&#xff0c;\u6570\u636e\u5c31\u4f1a\u4e22\u5931\u3002\u73af\u5f62\u961f\u5217&#xff08;Circular Buffer&#xff09;&#xff0c;\u4e5f\u79f0\u4e3a\u5148\u8fdb\u5148\u51fa&#xff08;FIFO&#xff09;\u961f\u5217&#xff0c;\u662f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u5b8c\u7f8e\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<h4>3.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u7f13\u51b2\u533a&#xff1f;\u2014\u2014\u89e3\u8026\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005<\/h4>\n<p>\u60f3\u8c61\u4e00\u4e2a\u6c34\u69fd&#xff0c;\u8fdb\u6c34\u7ba1&#xff08;\u751f\u4ea7\u8005&#xff0c;\u5982ISR&#xff09;\u548c\u51fa\u6c34\u7ba1&#xff08;\u6d88\u8d39\u8005&#xff0c;\u5982\u4e3b\u5faa\u73af&#xff09;\u8fde\u63a5\u5728\u4e0a\u9762\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u8fdb\u6c34\u6bd4\u51fa\u6c34\u5feb&#xff0c;\u6c34\u69fd&#xff08;\u7f13\u51b2\u533a&#xff09;\u53ef\u4ee5\u6682\u5b58\u591a\u4f59\u7684\u6c34&#xff0c;\u9632\u6b62\u6ea2\u51fa&#xff08;\u6570\u636e\u4e22\u5931&#xff09;\u3002<\/li>\n<li>\u5982\u679c\u51fa\u6c34\u6bd4\u8fdb\u6c34\u5feb&#xff0c;\u6c34\u69fd\u53ef\u4ee5\u4fdd\u8bc1\u51fa\u6c34\u7ba1\u4e0d\u4f1a\u65ad\u6d41&#xff08;\u7a7a\u8f6c\u7b49\u5f85&#xff09;\u3002<\/li>\n<li>\u6c34\u69fd\u7684\u5b58\u5728\u4f7f\u5f97\u8fdb\u6c34\u7ba1\u548c\u51fa\u6c34\u7ba1\u53ef\u4ee5\u72ec\u7acb\u5de5\u4f5c&#xff0c;\u4e92\u4e0d\u5f71\u54cd&#xff0c;\u5b9e\u73b0\u4e86\u89e3\u8026\u3002<\/li>\n<\/ul>\n<p>\u5728\u8f6f\u4ef6\u4e2d&#xff0c;\u7f13\u51b2\u533a\u5c31\u662f\u4e00\u5757\u5185\u5b58\u533a\u57df&#xff0c;\u7528\u4e8e\u5e73\u6ed1\u6570\u636e\u751f\u4ea7\u548c\u6d88\u8d39\u901f\u7387\u7684\u5dee\u5f02\u3002<\/p>\n<h4>3.2 \u73af\u5f62\u961f\u5217&#xff08;Circular Buffer\/FIFO&#xff09;\u539f\u7406\u6df1\u5ea6\u5256\u6790<\/h4>\n<p>\u4e3a\u4ec0\u4e48\u53eb\u201c\u73af\u5f62\u201d&#xff1f;\u56e0\u4e3a\u5f53\u6211\u4eec\u7528\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u65f6&#xff0c;\u5f53\u961f\u5c3e\u6307\u9488&#xff08;tail&#xff09;\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6&#xff0c;\u5b83\u4e0d\u662f\u505c\u6b62&#xff0c;\u800c\u662f\u201c\u7ed5\u56de\u201d\u5230\u6570\u7ec4\u7684\u5f00\u5934&#xff0c;\u524d\u63d0\u662f\u90a3\u4e2a\u4f4d\u7f6e\u662f\u7a7a\u7684\u3002\u8fd9\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u903b\u8f91\u4e0a\u7684\u73af\u3002<\/p>\n<p>\u6838\u5fc3\u8981\u7d20&#xff1a;<\/p>\n<ul>\n<li>buffer[]: \u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u3002<\/li>\n<li>head: \u961f\u5934\u6307\u9488&#xff0c;\u6307\u5411\u4e0b\u4e00\u4e2a\u8981\u88ab\u8bfb\u53d6\u7684\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>tail: \u961f\u5c3e\u6307\u9488&#xff0c;\u6307\u5411\u4e0b\u4e00\u4e2a\u8981\u88ab\u5199\u5165\u7684\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>size: \u961f\u5217\u7684\u603b\u5bb9\u91cf\u3002<\/li>\n<li>count (\u53ef\u9009): \u961f\u5217\u4e2d\u5f53\u524d\u5143\u7d20\u7684\u6570\u91cf\u3002\u4f7f\u7528 count \u53ef\u4ee5\u7b80\u5316\u6ee1\/\u7a7a\u5224\u65ad&#xff0c;\u4f46\u4f1a\u589e\u52a0\u4e00\u6b21\u539f\u5b50\u64cd\u4f5c\u7684\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p>\u64cd\u4f5c&#xff1a;<\/p>\n<ul>\n<li>\u5165\u961f&#xff08;Enqueue\/Put&#xff09;: \u5c06\u6570\u636e\u5199\u5165 buffer[tail]&#xff0c;\u7136\u540e tail &#061; (tail &#043; 1) % size\u3002<\/li>\n<li>\u51fa\u961f&#xff08;Dequeue\/Get&#xff09;: \u4ece buffer[head] \u8bfb\u53d6\u6570\u636e&#xff0c;\u7136\u540e head &#061; (head &#043; 1) % size\u3002<\/li>\n<\/ul>\n<p>\u6ee1\/\u7a7a\u5224\u65ad\u7684\u4e24\u79cd\u7ecf\u5178\u65b9\u6cd5&#xff1a;<\/p>\n<li>\n<p>\u4f7f\u7528 count \u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>\u7a7a&#xff1a;count &#061;&#061; 0<\/li>\n<li>\u6ee1&#xff1a;count &#061;&#061; size<\/li>\n<li>\u8fd9\u662f\u6700\u7b80\u5355\u3001\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u3002count \u7684\u589e\u51cf\u9700\u8981\u539f\u5b50\u64cd\u4f5c\u4fdd\u62a4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0d\u4f7f\u7528 count&#xff0c;\u727a\u7272\u4e00\u4e2a\u5b58\u50a8\u5355\u5143&#xff1a;<\/p>\n<ul>\n<li>\u7a7a&#xff1a;head &#061;&#061; tail<\/li>\n<li>\u6ee1&#xff1a;(tail &#043; 1) % size &#061;&#061; head<\/li>\n<li>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6d6a\u8d39\u4e00\u4e2a\u6570\u7ec4\u5355\u5143\u6765\u533a\u5206\u201c\u7a7a\u201d\u548c\u201c\u6ee1\u201d\u4e24\u79cd head &#061;&#061; tail \u7684\u60c5\u51b5\u3002\u5b83\u7684\u597d\u5904\u662f\u4e0d\u9700\u8981\u989d\u5916\u7684 count \u53d8\u91cf&#xff0c;\u4f46\u961f\u5217\u7684\u5b9e\u9645\u53ef\u7528\u5bb9\u91cf\u662f size &#8211; 1\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>3.3 \u73af\u5f62\u961f\u5217\u7684C\u8bed\u8a00\u5b9e\u73b0&#xff1a;\u4ece\u88f8\u673a\u5230RTOS<\/h4>\n<p>\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u901a\u7528\u7684\u3001\u7ebf\u7a0b\/\u4e2d\u65ad\u5b89\u5168\u7684\u73af\u5f62\u961f\u5217\u3002<\/p>\n<p>\u7248\u672c1&#xff1a;\u88f8\u673a&#xff0c;\u5355\u751f\u4ea7\u8005&#xff0c;\u5355\u6d88\u8d39\u8005&#xff08;\u975e\u7ebf\u7a0b\u5b89\u5168&#xff09;<\/p>\n<p>#define FIFO_SIZE 128<\/p>\n<p>typedef struct {<br \/>\n    uint8_t buffer[FIFO_SIZE];<br \/>\n    uint16_t head;<br \/>\n    uint16_t tail;<br \/>\n} RingBuffer_t;<\/p>\n<p>void fifo_init(RingBuffer_t *fifo) {<br \/>\n    fifo-&gt;head &#061; 0;<br \/>\n    fifo-&gt;tail &#061; 0;<br \/>\n}<\/p>\n<p>bool fifo_is_empty(RingBuffer_t *fifo) {<br \/>\n    return (fifo-&gt;head  fifo-&gt;tail);<br \/>\n}<\/p>\n<p>bool fifo_is_full(RingBuffer_t *fifo) {<br \/>\n    \/\/ \u727a\u7272\u4e00\u4e2a\u5355\u5143\u7684\u5224\u65ad\u65b9\u6cd5<br \/>\n    return ((fifo-&gt;tail &#043; 1) % FIFO_SIZE  fifo-&gt;head);<br \/>\n}<\/p>\n<p>bool fifo_put(RingBuffer_t *fifo, uint8_t data) {<br \/>\n    if (fifo_is_full(fifo)) {<br \/>\n        return false; \/\/ \u961f\u5217\u5df2\u6ee1<br \/>\n    }<br \/>\n    fifo-&gt;buffer[fifo-&gt;tail] &#061; data;<br \/>\n    fifo-&gt;tail &#061; (fifo-&gt;tail &#043; 1) % FIFO_SIZE;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>bool fifo_get(RingBuffer_t *fifo, uint8_t *data) {<br \/>\n    if (fifo_is_empty(fifo)) {<br \/>\n        return false; \/\/ \u961f\u5217\u4e3a\u7a7a<br \/>\n    }<br \/>\n    *data &#061; fifo-&gt;buffer[fifo-&gt;head];<br \/>\n    fifo-&gt;head &#061; (fifo-&gt;head &#043; 1) % FIFO_SIZE;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u7248\u672c\u7b80\u5355&#xff0c;\u4f46\u5728\u591a\u7ebf\u7a0b\/\u4e2d\u65ad\u73af\u5883\u4e0b\u662f\u5371\u9669\u7684\u3002\u4f8b\u5982&#xff0c;fifo_put \u53ef\u80fd\u5728 fifo_is_full \u68c0\u67e5\u540e\u3001\u5199\u5165\u6570\u636e\u524d\u88ab\u4e2d\u65ad&#xff0c;\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u3002<\/p>\n<p>\u7248\u672c2&#xff1a;\u7ebf\u7a0b\/\u4e2d\u65ad\u5b89\u5168\u7684\u5b9e\u73b0&#xff08;\u4f7f\u7528\u4e34\u754c\u533a&#xff09;<\/p>\n<p>#include &#034;cmsis_compiler.h&#034; \/\/ \u5047\u8bbe\u662fARM Cortex-M<\/p>\n<p>\/\/ &#8230; RingBuffer_t \u5b9a\u4e49\u540c\u4e0a &#8230;<br \/>\n\/\/ \u4e3a\u4e86\u6548\u7387&#xff0c;\u6211\u4eec\u4f7f\u7528\u4e0d\u5e26count\u7684\u7248\u672c&#xff0c;\u5e76\u7528\u4e34\u754c\u533a\u4fdd\u62a4<\/p>\n<p>bool fifo_put_safe(RingBuffer_t *fifo, uint8_t data) {<br \/>\n    bool success;<br \/>\n    __disable_irq(); \/\/ \u8fdb\u5165\u4e34\u754c\u533a<\/p>\n<p>    if (((fifo-&gt;tail &#043; 1) % FIFO_SIZE) &#061;&#061; fifo-&gt;head) {<br \/>\n        success &#061; false; \/\/ \u961f\u5217\u5df2\u6ee1<br \/>\n    } else {<br \/>\n        fifo-&gt;buffer[fifo-&gt;tail] &#061; data;<br \/>\n        fifo-&gt;tail &#061; (fifo-&gt;tail &#043; 1) % FIFO_SIZE;<br \/>\n        success &#061; true;<br \/>\n    }<\/p>\n<p>    __enable_irq(); \/\/ \u9000\u51fa\u4e34\u754c\u533a<br \/>\n    return success;<br \/>\n}<\/p>\n<p>bool fifo_get_safe(RingBuffer_t *fifo, uint8_t *data) {<br \/>\n    bool success;<br \/>\n    __disable_irq(); \/\/ \u8fdb\u5165\u4e34\u754c\u533a<\/p>\n<p>    if (fifo-&gt;head &#061;&#061; fifo-&gt;tail) {<br \/>\n        success &#061; false; \/\/ \u961f\u5217\u4e3a\u7a7a<br \/>\n    } else {<br \/>\n        *data &#061; fifo-&gt;buffer[fifo-&gt;head];<br \/>\n        fifo-&gt;head &#061; (fifo-&gt;head &#043; 1) % FIFO_SIZE;<br \/>\n        success &#061; true;<br \/>\n    }<\/p>\n<p>    __enable_irq(); \/\/ \u9000\u51fa\u4e34\u754c\u533a<br \/>\n    return success;<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u7248\u672c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u4f46\u4ee3\u4ef7\u662f\u6bcf\u6b21\u5165\u961f\/\u51fa\u961f\u90fd\u8981\u5f00\u5173\u4e2d\u65ad&#xff0c;\u5bf9\u4e8e\u9ad8\u9891\u64cd\u4f5c\u6765\u8bf4\u5f00\u9500\u8f83\u5927\u3002<\/p>\n<p>\u7248\u672c3&#xff1a;\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u7684\u65e0\u9501\u5b9e\u73b0&#xff08;Lock-Free&#xff09;<\/p>\n<p>\u5bf9\u4e8e\u5355\u751f\u4ea7\u8005\u3001\u5355\u6d88\u8d39\u8005&#xff08;SPSC&#xff09;\u573a\u666f&#xff0c;\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u65e0\u9501\u961f\u5217\u3002\u8fd9\u9700\u8981\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u5728\u4e0d\u540c\u7684\u6838\u4e0a&#xff0c;\u6216\u8005\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u907f\u514d\u7ade\u4e89\u3002\u4f46\u5728\u5355\u6838MCU\u4e0a&#xff0c;ISR\u548c\u4e3b\u5faa\u73af\u7684\u573a\u666f&#xff0c;\u4f7f\u7528\u4e34\u754c\u533a\u901a\u5e38\u662f\u8db3\u591f\u4e14\u66f4\u5b89\u5168\u7684\u9009\u62e9\u3002<\/p>\n<h4>3.4 \u961f\u5217\u7684\u6ee1\/\u7a7a\u5224\u65ad&#xff1a;\u7ecf\u5178\u7b97\u6cd5\u4e0e\u9677\u9631<\/h4>\n<p>\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e24\u79cd\u5224\u65ad\u65b9\u6cd5\u3002\u518d\u6df1\u5165\u63a2\u8ba8\u4e00\u4e0b&#xff1a;<\/p>\n<ul>\n<li>head \u548c tail \u7684\u6570\u636e\u7c7b\u578b&#xff1a;\u4e3a\u4e86\u9632\u6b62 head \u548c tail \u5728\u6bd4\u8f83\u65f6\u56e0\u4e3a\u6ea2\u51fa\u800c\u51fa\u9519&#xff0c;\u901a\u5e38\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570&#xff08;\u5982 uint16_t, uint32_t&#xff09;\u3002\u6a21\u8fd0\u7b97 % \u4f1a\u81ea\u52a8\u5904\u7406\u6ea2\u51fa\u3002\u4f8b\u5982&#xff0c;uint16_t \u7684 65535 &#043; 1 \u4f1a\u53d8\u6210 0\u3002<\/li>\n<li>volatile \u5173\u952e\u5b57&#xff1a;head \u548c tail \u6307\u9488\u4f1a\u88abISR\u548c\u4e3b\u5faa\u73af\u540c\u65f6\u4fee\u6539&#xff0c;\u56e0\u6b64\u5fc5\u987b\u58f0\u660e\u4e3a volatile&#xff0c;\u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u5bfc\u81f4\u4e00\u4e2a\u7ebf\u7a0b\u770b\u4e0d\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u4fee\u6539\u3002\n<p>typedef struct {<br \/>\n    uint8_t buffer[FIFO_SIZE];<br \/>\n    volatile uint16_t head;<br \/>\n    volatile uint16_t tail;<br \/>\n} RingBuffer_t;<\/p>\n<\/li>\n<li>\u5185\u5b58\u5c4f\u969c&#xff08;Memory Barrier&#xff09;&#xff1a;\u5728\u67d0\u4e9b\u9ad8\u7ea7\u67b6\u6784\u4e2d&#xff0c;\u4ec5\u4ec5\u4f7f\u7528 volatile \u53ef\u80fd\u4e0d\u8db3\u4ee5\u4fdd\u8bc1\u5185\u5b58\u64cd\u4f5c\u7684\u987a\u5e8f\u3002\u53ef\u80fd\u9700\u8981\u663e\u5f0f\u7684\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u6765\u786e\u4fdd\u5199\u5165\u64cd\u4f5c\u7684\u987a\u5e8f\u6027\u3002\u4f46\u5728\u5927\u591a\u6570\u5d4c\u5165\u5f0fMCU&#xff08;\u5982Cortex-M&#xff09;\u4e0a&#xff0c;volatile \u7ed3\u5408\u4e34\u754c\u533a\u5df2\u7ecf\u8db3\u591f\u3002<\/li>\n<\/ul>\n<h4>3.5 \u7ebf\u7a0b\/\u4e2d\u65ad\u5b89\u5168\u7684\u73af\u5f62\u961f\u5217&#xff1a;\u9501\u7684\u827a\u672f<\/h4>\n<p>\u5728\u591a\u751f\u4ea7\u8005\/\u591a\u6d88\u8d39\u8005&#xff08;MPMC&#xff09;\u573a\u666f&#xff0c;\u6216\u8005\u5728RTOS\u4e2d&#xff0c;\u7b80\u5355\u7684\u5f00\u5173\u4e2d\u65ad\u5c31\u4e0d\u591f\u4e86&#xff0c;\u9700\u8981\u4f7f\u7528\u4e92\u65a5\u9501&#xff08;Mutex&#xff09;\u6216\u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;\u3002<\/p>\n<p>\u4f7f\u7528\u4e92\u65a5\u9501\u7684\u5b9e\u73b0\u601d\u8def&#xff1a;<\/p>\n<li>\u961f\u5217\u7ed3\u6784\u4f53\u4e2d\u589e\u52a0\u4e00\u4e2a\u4e92\u65a5\u9501\u53e5\u67c4\u3002<\/li>\n<li>fifo_put \u64cd\u4f5c\u524d&#xff0c;\u83b7\u53d6\u9501\u3002\u64cd\u4f5c\u5b8c\u6210\u540e&#xff0c;\u91ca\u653e\u9501\u3002<\/li>\n<li>fifo_get \u64cd\u4f5c\u524d&#xff0c;\u83b7\u53d6\u9501\u3002\u64cd\u4f5c\u5b8c\u6210\u540e&#xff0c;\u91ca\u653e\u9501\u3002<\/li>\n<p>\u95ee\u9898&#xff1a;\u5982\u679c\u961f\u5217\u6ee1\u4e86&#xff0c;\u751f\u4ea7\u8005\u83b7\u53d6\u9501\u540e\u53d1\u73b0\u6ee1\u4e86&#xff0c;\u7136\u540e\u91ca\u653e\u9501&#xff0c;\u8fd9\u671f\u95f4\u6d88\u8d39\u8005\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u9501\u6765\u53d6\u6570\u636e\u3002\u8fd9\u4f1a\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u9501\u7ade\u4e89\u3002<\/p>\n<p>\u66f4\u4f18\u7684\u65b9\u6848&#xff1a;\u4f7f\u7528\u4fe1\u53f7\u91cf\u8fdb\u884c\u963b\u585e<\/p>\n<ul>\n<li>\u7a7a\u4fe1\u53f7\u91cf&#xff1a;\u521d\u59cb\u503c\u4e3a0\u3002\u6d88\u8d39\u8005\u5c1d\u8bd5\u83b7\u53d6\u6570\u636e\u65f6&#xff0c;\u5982\u679c\u961f\u5217\u4e3a\u7a7a&#xff0c;\u5c31\u963b\u585e\u5728\u8fd9\u4e2a\u4fe1\u53f7\u91cf\u4e0a\u3002\u751f\u4ea7\u8005\u6bcf\u653e\u5165\u4e00\u4e2a\u6570\u636e&#xff0c;\u5c31\u91ca\u653e&#xff08;give&#xff09;\u4e00\u6b21\u4fe1\u53f7\u91cf&#xff0c;\u5524\u9192\u6d88\u8d39\u8005\u3002<\/li>\n<li>\u6ee1\u4fe1\u53f7\u91cf&#xff1a;\u521d\u59cb\u503c\u4e3a\u961f\u5217\u5bb9\u91cf\u3002\u751f\u4ea7\u8005\u5c1d\u8bd5\u653e\u5165\u6570\u636e\u65f6&#xff0c;\u5982\u679c\u961f\u5217\u5df2\u6ee1&#xff0c;\u5c31\u963b\u585e\u5728\u8fd9\u4e2a\u4fe1\u53f7\u91cf\u4e0a\u3002\u6d88\u8d39\u8005\u6bcf\u53d6\u51fa\u4e00\u4e2a\u6570\u636e&#xff0c;\u5c31\u91ca\u653e\u4e00\u6b21\u4fe1\u53f7\u91cf&#xff0c;\u5524\u9192\u751f\u4ea7\u8005\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e86\u963b\u585e\u5f0f\u7684\u751f\u4ea7\u8005\/\u6d88\u8d39\u8005\u6a21\u578b&#xff0c;CPU\u5728\u6ca1\u6709\u5de5\u4f5c\u65f6\u4f1a\u8fdb\u5165\u4f11\u7720&#xff0c;\u800c\u4e0d\u662f\u7a7a\u8f6c\u67e5\u8be2&#xff0c;\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6548\u7387\u3002\u8fd9\u662fRTOS\u73af\u5883\u4e0b\u7684\u6807\u51c6\u505a\u6cd5\u3002<\/p>\n<h4>3.6 \u5b9e\u6218\u6848\u4f8b&#xff1a;UART\u63a5\u6536\u7f13\u51b2\u533a\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0<\/h4>\n<p>UART\u662f\u4f7f\u7528\u73af\u5f62\u961f\u5217\u6700\u7ecf\u5178\u7684\u573a\u666f\u3002<\/p>\n<p>\u9700\u6c42&#xff1a;<\/p>\n<ul>\n<li>UART\u4ee5115200bps\u63a5\u6536\u6570\u636e&#xff0c;\u7ea6\u6bcf87\u5fae\u79d2\u4e00\u4e2a\u5b57\u8282\u3002<\/li>\n<li>ISR\u5fc5\u987b\u5728\u4e0b\u4e00\u4e2a\u5b57\u8282\u5230\u8fbe\u524d\u5b8c\u6210\u8bfb\u53d6&#xff0c;\u5426\u5219\u4f1a\u53d1\u751f\u6ea2\u51fa\u9519\u8bef&#xff08;Overrun Error&#xff09;\u3002<\/li>\n<li>\u4e3b\u5faa\u73af\u4ee5\u4e0d\u56fa\u5b9a\u7684\u9891\u7387\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\u5e27\u3002<\/li>\n<\/ul>\n<p>\u8bbe\u8ba1&#xff1a;<\/p>\n<li>\u521b\u5efa\u4e00\u4e2a\u8db3\u591f\u5927\u7684\u73af\u5f62\u961f\u5217&#xff0c;\u4f8b\u5982256\u5b57\u8282\u3002<\/li>\n<li>\u5728UART\u63a5\u6536\u4e2d\u65ad&#xff08;USART_RX_IRQHandler&#xff09;\u4e2d&#xff0c;\u53ea\u505a\u4e00\u4ef6\u4e8b&#xff1a;\u5c06\u63a5\u6536\u5230\u7684\u5b57\u8282\u653e\u5165\u961f\u5217\u3002\u5982\u679c\u961f\u5217\u6ee1\u4e86&#xff0c;\u5c31\u8bbe\u7f6e\u4e00\u4e2a\u6ea2\u51fa\u9519\u8bef\u6807\u5fd7\u3002<\/li>\n<li>\u5728\u4e3b\u5faa\u73af\u4e2d&#xff0c;\u4e0d\u65ad\u5730\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u5b57\u8282&#xff0c;\u8fdb\u884c\u534f\u8bae\u89e3\u6790\u3002<\/li>\n<p>\u4ee3\u7801\u5b9e\u73b0 (\u57fa\u4e8e\u7248\u672c2\u7684\u4e34\u754c\u533a\u4fdd\u62a4)<\/p>\n<p>\/\/ uart_buffer.h<br \/>\n#define UART_RX_BUFFER_SIZE 256<\/p>\n<p>typedef struct {<br \/>\n    uint8_t buffer[UART_RX_BUFFER_SIZE];<br \/>\n    volatile uint16_t head;<br \/>\n    volatile uint16_t tail;<br \/>\n    volatile bool overflow_flag;<br \/>\n} UartRxFifo_t;<\/p>\n<p>void uart_rx_fifo_init(UartRxFifo_t *fifo);<br \/>\nbool uart_rx_fifo_put(UartRxFifo_t *fifo, uint8_t byte);<br \/>\nbool uart_rx_fifo_get(UartRxFifo_t *fifo, uint8_t *byte);<br \/>\nbool uart_rx_fifo_is_empty(UartRxFifo_t *fifo);<\/p>\n<p>\/\/ uart.c<br \/>\n#include &#034;uart_buffer.h&#034;<br \/>\n#include &#034;cmsis_compiler.h&#034;<\/p>\n<p>static UartRxFifo_t g_uart_rx_fifo;<\/p>\n<p>void uart_rx_fifo_init(UartRxFifo_t *fifo) {<br \/>\n    fifo-&gt;head &#061; 0;<br \/>\n    fifo-&gt;tail &#061; 0;<br \/>\n    fifo-&gt;overflow_flag &#061; false;<br \/>\n}<\/p>\n<p>\/\/ \u6b64\u51fd\u6570\u5728ISR\u4e2d\u8c03\u7528<br \/>\nbool uart_rx_fifo_put(UartRxFifo_t *fifo, uint8_t byte) {<br \/>\n    uint16_t next_tail &#061; (fifo-&gt;tail &#043; 1) % UART_RX_BUFFER_SIZE;<\/p>\n<p>    \/\/ \u68c0\u67e5\u662f\u5426\u5df2\u6ee1<br \/>\n    if (next_tail &#061;&#061; fifo-&gt;head) {<br \/>\n        fifo-&gt;overflow_flag &#061; true; \/\/ \u6807\u8bb0\u6ea2\u51fa<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    fifo-&gt;buffer[fifo-&gt;tail] &#061; byte;<br \/>\n    fifo-&gt;tail &#061; next_tail;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\/\/ \u6b64\u51fd\u6570\u5728\u4e3b\u5faa\u73af\u4e2d\u8c03\u7528<br \/>\nbool uart_rx_fifo_get(UartRxFifo_t *fifo, uint8_t *byte) {<br \/>\n    if (fifo-&gt;head &#061;&#061; fifo-&gt;tail) {<br \/>\n        return false; \/\/ \u961f\u5217\u4e3a\u7a7a<br \/>\n    }<\/p>\n<p>    *byte &#061; fifo-&gt;buffer[fifo-&gt;head];<br \/>\n    fifo-&gt;head &#061; (fifo-&gt;head &#043; 1) % UART_RX_BUFFER_SIZE;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\/\/ UART\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f<br \/>\nvoid USART1_IRQHandler(void) {<br \/>\n    if (USART_GetITStatus(USART1, USART_IT_RXNE) !&#061; RESET) {<br \/>\n        uint8_t rx_byte &#061; USART_ReceiveData(USART1);<\/p>\n<p>        \/\/ \u5feb\u901f\u653e\u5165FIFO&#xff0c;\u8fd9\u4e2a\u64cd\u4f5c\u672c\u8eab\u5f88\u5feb&#xff0c;\u4e0d\u4f1a\u957f\u65f6\u95f4\u5360\u7528\u4e2d\u65ad<br \/>\n        if (!uart_rx_fifo_put(&amp;g_uart_rx_fifo, rx_byte)) {<br \/>\n            \/\/ FIFO\u5df2\u6ee1&#xff0c;\u53ef\u4ee5\u5728\u8fd9\u91cc\u505a\u4e00\u4e9b\u9519\u8bef\u5904\u7406&#xff0c;\u6bd4\u5982\u70b9\u4eae\u4e00\u4e2a\u9519\u8befLED<br \/>\n        }<\/p>\n<p>        USART_ClearITPendingBit(USART1, USART_IT_RXNE);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u4e3b\u5faa\u73af\u4e2d\u7684\u6570\u636e\u5904\u7406<br \/>\nvoid main_loop() {<br \/>\n    uint8_t byte;<br \/>\n    while (uart_rx_fifo_get(&amp;g_uart_rx_fifo, &amp;byte)) {<br \/>\n        \/\/ \u5c06\u5b57\u8282\u4ea4\u7ed9\u534f\u8bae\u89e3\u6790\u72b6\u6001\u673a<br \/>\n        protocol_parse_byte(byte);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d&#xff0c;uart_rx_fifo_put \u6ca1\u6709\u4f7f\u7528\u4e34\u754c\u533a&#xff0c;\u56e0\u4e3a\u5b83\u53ea\u5728\u4e00\u4e2aISR\u4e2d\u88ab\u8c03\u7528&#xff0c;\u4e0d\u5b58\u5728\u4e0e\u5176\u4ed6\u6267\u884c\u6d41\u7684\u7ade\u4e89\u3002head \u53ea\u88ab\u4e3b\u5faa\u73af\u4fee\u6539&#xff0c;tail \u53ea\u88abISR\u4fee\u6539&#xff0c;\u6240\u4ee5\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7ade\u4e89\u3002\u8fd9\u662f\u4e00\u4e2aSPSC&#xff08;\u5355\u751f\u4ea7\u8005\u5355\u6d88\u8d39\u8005&#xff09;\u6a21\u578b&#xff0c;\u662f\u4e2d\u65ad\u4e0e\u4e3b\u5faa\u73af\u901a\u4fe1\u6700\u5e38\u89c1\u7684\u6a21\u5f0f\u3002<\/p>\n<h4>3.7 \u6269\u5c55&#xff1a;\u591a\u751f\u4ea7\u8005\/\u591a\u6d88\u8d39\u8005\u6a21\u578b<\/h4>\n<p>\u5728\u66f4\u590d\u6742\u7684\u7cfb\u7edf\u4e2d&#xff0c;\u53ef\u80fd\u6709\u591a\u4e2a\u4efb\u52a1\u6216ISR\u5411\u540c\u4e00\u4e2a\u961f\u5217\u53d1\u9001\u6570\u636e&#xff0c;\u6216\u8005\u6709\u591a\u4e2a\u4efb\u52a1\u4ece\u540c\u4e00\u4e2a\u961f\u5217\u63a5\u6536\u6570\u636e\u3002\u8fd9\u65f6\u5c31\u5fc5\u987b\u4f7f\u7528RTOS\u63d0\u4f9b\u7684\u7ebf\u7a0b\u5b89\u5168\u961f\u5217API&#xff0c;\u6216\u8005\u81ea\u5df1\u7528\u4e92\u65a5\u9501\u548c\u4fe1\u53f7\u91cf\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728FreeRTOS\u4e2d&#xff0c;xQueueSend \u548c xQueueReceive \u51fd\u6570\u672c\u8eab\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u5185\u90e8\u5df2\u7ecf\u5904\u7406\u597d\u4e86\u6240\u6709\u7684\u540c\u6b65\u95ee\u9898\u3002\u8fd9\u6b63\u662fRTOS\u5e26\u6765\u7684\u5de8\u5927\u4fbf\u5229\u3002<\/p>\n<hr \/>\n<h3>\u7b2c\u56db\u7ae0&#xff1a;\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u2014\u2014\u7ed3\u6784\u5316\u903b\u8f91\u7684\u6838\u5fc3<\/h3>\n<p>\u73b0\u5728&#xff0c;\u6211\u4eec\u5df2\u7ecf\u638c\u63e1\u4e86\u5904\u7406\u5e76\u53d1&#xff08;\u4e2d\u65ad&#xff09;\u548c\u6570\u636e\u4f20\u9012&#xff08;\u73af\u5f62\u961f\u5217&#xff09;\u7684\u5de5\u5177&#xff0c;\u662f\u65f6\u5019\u5c06\u5b83\u4eec\u4e0eFSM\u7ed3\u5408\u8d77\u6765&#xff0c;\u6784\u5efa\u5f3a\u5927\u7684\u5d4c\u5165\u5f0f\u5e94\u7528\u4e86\u3002<\/p>\n<h4>4.1 FSM \u7684\u6570\u5b66\u6a21\u578b\u4e0e\u6838\u5fc3\u4e09\u8981\u7d20<\/h4>\n<p>\u5982\u524d\u6240\u8ff0&#xff0c;FSM\u7531\u4e09\u4e2a\u6838\u5fc3\u8981\u7d20\u7ec4\u6210&#xff1a;<\/p>\n<li>\u72b6\u6001&#xff08;State&#xff09;&#xff1a;\u7cfb\u7edf\u5728\u67d0\u4e00\u65f6\u523b\u7684\u884c\u4e3a\u6a21\u5f0f\u3002\u4f8b\u5982 STATE_IDLE, STATE_RUNNING, STATE_ERROR\u3002\u72b6\u6001\u662f\u79bb\u6563\u7684\u3001\u6709\u9650\u7684\u3002<\/li>\n<li>\u4e8b\u4ef6&#xff08;Event&#xff09;&#xff1a;\u89e6\u53d1\u72b6\u6001\u8f6c\u6362\u7684\u6761\u4ef6\u3002\u4e8b\u4ef6\u53ef\u4ee5\u662f\u5916\u90e8\u7684&#xff08;\u5982 EVENT_BUTTON_PRESS&#xff09;&#xff0c;\u4e5f\u53ef\u4ee5\u662f\u5185\u90e8\u7684&#xff08;\u5982 EVENT_TIMER_EXPIRED, EVENT_DATA_RECEIVED&#xff09;\u3002<\/li>\n<li>\u8f6c\u6362&#xff08;Transition&#xff09;&#xff1a;\u4ece\u4e00\u4e2a\u72b6\u6001\u5230\u53e6\u4e00\u4e2a\u72b6\u6001\u7684\u89c4\u5219\u3002\u5b83\u7531\u201c\u5f53\u524d\u72b6\u6001\u201d&#043;\u201c\u4e8b\u4ef6\u201d -&gt; \u201c\u4e0b\u4e00\u4e2a\u72b6\u6001\u201d&#043;\u201c\u52a8\u4f5c\u201d\u5b9a\u4e49\u3002<\/li>\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684FSM\u8fd8\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u521d\u59cb\u72b6\u6001&#xff08;Initial State&#xff09;&#xff1a;\u7cfb\u7edf\u542f\u52a8\u65f6\u6240\u5904\u7684\u72b6\u6001\u3002<\/li>\n<li>\u6700\u7ec8\u72b6\u6001&#xff08;Final State&#xff09;&#xff08;\u53ef\u9009&#xff09;&#xff1a;\u8868\u793a\u6d41\u7a0b\u7ed3\u675f\u7684\u72b6\u6001\u3002<\/li>\n<li>\u52a8\u4f5c&#xff08;Action&#xff09;&#xff1a;\u5728\u8fdb\u5165\u72b6\u6001\u3001\u9000\u51fa\u72b6\u6001\u6216\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h4>4.2 Moore \u578b vs. Mealy \u578b&#xff1a;\u8f93\u51fa\u4f9d\u8d56\u7684\u54f2\u5b66\u601d\u8fa8<\/h4>\n<p>\u8fd9\u662fFSM\u7406\u8bba\u4e2d\u4e00\u4e2a\u7ecf\u5178\u7684\u5206\u7c7b&#xff0c;\u7406\u89e3\u5b83\u4eec\u7684\u533a\u522b\u6709\u52a9\u4e8e\u8bbe\u8ba1\u51fa\u66f4\u6e05\u6670\u7684\u72b6\u6001\u673a\u3002<\/p>\n<ul>\n<li>\n<p>Moore \u578b\u72b6\u6001\u673a<\/p>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u8f93\u51fa\u53ea\u4f9d\u8d56\u4e8e\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>\u7ed3\u6784&#xff1a;\u8f93\u51fa\u903b\u8f91\u5728\u72b6\u6001\u5185\u90e8\u3002<\/li>\n<li>\u7279\u70b9&#xff1a;\n<ul>\n<li>\u8f93\u51fa\u4e0e\u8f93\u5165&#xff08;\u4e8b\u4ef6&#xff09;\u540c\u6b65&#xff0c;\u53ea\u5728\u72b6\u6001\u6539\u53d8\u65f6\u6539\u53d8\u3002<\/li>\n<li>\u72b6\u6001\u4e0e\u8f93\u51fa\u4e00\u4e00\u5bf9\u5e94&#xff0c;\u903b\u8f91\u6e05\u6670\u3002<\/li>\n<li>\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u72b6\u6001\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u201c\u7b49\u5f85\u6309\u94ae\u6309\u4e0b\u5e76\u70b9\u4eaeLED\u201d\u7684\u529f\u80fd&#xff0c;Moore\u578b\u53ef\u80fd\u9700\u8981\u4e24\u4e2a\u72b6\u6001&#xff1a;\u201c\u7b49\u5f85\u4e2d&#xff08;LED\u706d&#xff09;\u201d\u548c\u201c\u6309\u94ae\u6309\u4e0b&#xff08;LED\u4eae&#xff09;\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b&#xff1a;\u4ea4\u901a\u706f\u3002\u7ea2\u706f\u72b6\u6001\u7684\u8f93\u51fa\u5c31\u662f\u201c\u7ea2\u706f\u4eae\u201d&#xff0c;\u4e0e\u662f\u5426\u6709\u8f66&#xff08;\u4e8b\u4ef6&#xff09;\u65e0\u5173&#xff08;\u5047\u8bbe\u6ca1\u6709\u4f20\u611f\u5668&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Mealy \u578b\u72b6\u6001\u673a<\/p>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u8f93\u51fa\u4f9d\u8d56\u4e8e\u5f53\u524d\u72b6\u6001\u548c\u5f53\u524d\u8f93\u5165\u4e8b\u4ef6\u3002<\/li>\n<li>\u7ed3\u6784&#xff1a;\u8f93\u51fa\u903b\u8f91\u5728\u8f6c\u6362\u8def\u5f84\u4e0a\u3002<\/li>\n<li>\u7279\u70b9&#xff1a;\n<ul>\n<li>\u8f93\u51fa\u53ef\u4ee5\u7acb\u5373\u54cd\u5e94\u8f93\u5165\u4e8b\u4ef6&#xff0c;\u65e0\u9700\u7b49\u5f85\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002<\/li>\n<li>\u901a\u5e38\u53ef\u4ee5\u7528\u66f4\u5c11\u7684\u72b6\u6001\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/li>\n<li>\u8f93\u51fa\u903b\u8f91\u53ef\u80fd\u5206\u6563\u5728\u591a\u4e2a\u8f6c\u6362\u4e2d&#xff0c;\u4e0d\u5982Moore\u578b\u96c6\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b&#xff1a;\u81ea\u52a8\u552e\u8d27\u673a\u3002\u5728\u201c\u5f85\u673a\u201d\u72b6\u6001\u4e0b&#xff0c;\u5982\u679c\u6295\u5165\u201c1\u5143\u786c\u5e01\u201d&#xff08;\u4e8b\u4ef6&#xff09;&#xff0c;\u8f93\u51fa\u201c\u627e\u96f69\u89d2\u201d&#xff1b;\u5982\u679c\u6295\u5165\u201c10\u5143\u201d&#xff08;\u4e8b\u4ef6&#xff09;&#xff0c;\u8f93\u51fa\u201c\u51fa\u8d27\u5e76\u627e\u96f6\u201d\u3002\u8f93\u51fa\u53d6\u51b3\u4e8e\u5f53\u524d\u72b6\u6001\u548c\u6295\u5165\u7684\u786c\u5e01\u9762\u989d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5728\u5d4c\u5165\u5f0fC\u8bed\u8a00\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u6df7\u5408\u4f7f\u7528\u8fd9\u4e24\u79cd\u601d\u60f3\u3002 \u4f8b\u5982&#xff0c;\u72b6\u6001\u5185\u90e8\u7684\u52a8\u4f5c&#xff08;Entry\/Exit Action&#xff09;\u662fMoore\u578b\u7684&#xff0c;\u800c\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u52a8\u4f5c&#xff08;Transition Action&#xff09;\u662fMealy\u578b\u7684\u3002\u4e0d\u5fc5\u4e25\u683c\u533a\u5206&#xff0c;\u5173\u952e\u662f\u8ba9\u903b\u8f91\u6700\u6e05\u6670\u3002<\/p>\n<h4>4.3 \u72b6\u6001\u673a\u7684\u53ef\u89c6\u5316&#xff1a;\u72b6\u6001\u8f6c\u6362\u56fe&#xff08;STD&#xff09;\u7684\u7ed8\u5236\u4e0e\u610f\u4e49<\/h4>\n<p>\u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d\u3002\u5728\u7f16\u5199\u4efb\u4f55\u4ee3\u7801\u4e4b\u524d&#xff0c;\u90fd\u5e94\u8be5\u5148\u7ed8\u5236\u72b6\u6001\u8f6c\u6362\u56fe&#xff08;State Transition Diagram, STD&#xff09;\u3002<\/p>\n<ul>\n<li>\u8282\u70b9&#xff08;\u5706\u5708&#xff09;&#xff1a;\u4ee3\u8868\u72b6\u6001\u3002<\/li>\n<li>\u6709\u5411\u8fb9&#xff08;\u7bad\u5934&#xff09;&#xff1a;\u4ee3\u8868\u8f6c\u6362\u3002<\/li>\n<li>\u8fb9\u7684\u6807\u7b7e&#xff1a;\u683c\u5f0f\u901a\u5e38\u662f \u4e8b\u4ef6 \/ \u52a8\u4f5c\u3002\u4f8b\u5982 BUTTON_PRESS \/ turn_on_led()\u3002<\/li>\n<\/ul>\n<p>\u7ed8\u5236STD\u7684\u597d\u5904&#xff1a;<\/p>\n<ul>\n<li>\u5f3a\u8feb\u601d\u8003&#xff1a;\u5b83\u5f3a\u8feb\u4f60\u5728\u5199\u4ee3\u7801\u524d\u5c31\u5b8c\u6574\u5730\u601d\u8003\u6240\u6709\u53ef\u80fd\u7684\u72b6\u6001\u548c\u8f6c\u6362&#xff0c;\u907f\u514d\u9057\u6f0f\u3002<\/li>\n<li>\u6c9f\u901a\u5de5\u5177&#xff1a;\u5b83\u662f\u4e0e\u56e2\u961f\u6210\u5458&#xff08;\u5305\u62ec\u975e\u7a0b\u5e8f\u5458&#xff09;\u6c9f\u901a\u7cfb\u7edf\u884c\u4e3a\u7684\u6700\u4f73\u65b9\u5f0f\u3002<\/li>\n<li>\u6587\u6863&#xff1a;STD\u672c\u8eab\u5c31\u662f\u4e00\u4efd\u6d3b\u7684\u3001\u7cbe\u786e\u7684\u7cfb\u7edf\u6587\u6863\u3002<\/li>\n<li>\u4ee3\u7801\u751f\u6210&#xff1a;\u4e00\u4e9b\u5de5\u5177\u53ef\u4ee5\u4eceSTD\u76f4\u63a5\u751f\u6210C\u4ee3\u7801\u6846\u67b6\u3002<\/li>\n<\/ul>\n<p>\u5de5\u5177\u63a8\u8350&#xff1a;draw.io (\u514d\u8d39\u5728\u7ebf), PlantUML (\u6587\u672c\u751f\u6210\u56fe\u8868), Visio, StarUML\u3002<\/p>\n<h4>4.4 FSM \u5728\u5d4c\u5165\u5f0fC\u8bed\u8a00\u4e2d\u7684\u56db\u79cd\u7ecf\u5178\u5b9e\u73b0<\/h4>\n<h5>4.4.1 Switch-Case&#xff1a;\u7b80\u5355\u76f4\u63a5&#xff0c;\u4e2d\u5c0f\u578b\u7cfb\u7edf\u7684\u9996\u9009<\/h5>\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u3001\u6700\u76f4\u89c2\u7684\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u5c24\u5176\u9002\u5408\u72b6\u6001\u548c\u4e8b\u4ef6\u4e0d\u592a\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<ul>\n<li>\u7528 enum \u5b9a\u4e49\u72b6\u6001\u548c\u4e8b\u4ef6\u3002<\/li>\n<li>\u7528\u4e00\u4e2a\u5168\u5c40\u6216\u9759\u6001\u53d8\u91cf current_state \u4fdd\u5b58\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>\u5728\u4e3b\u5faa\u73af\u6216\u4e00\u4e2a\u4e13\u95e8\u7684\u51fd\u6570\u4e2d&#xff0c;\u4f7f\u7528 switch(current_state)\u3002<\/li>\n<li>\u5728\u6bcf\u4e2a case \u4e2d&#xff0c;\u5904\u7406\u7279\u5b9a\u4e8e\u8be5\u72b6\u6001\u7684\u903b\u8f91&#xff0c;\u5e76\u6839\u636e\u4e8b\u4ef6\u8fdb\u884c\u72b6\u6001\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u4e00\u4e2a\u7b80\u5355\u7684\u6309\u952e\u63a7\u5236LED&#xff08;\u652f\u6301\u5355\u51fb\u3001\u53cc\u51fb&#xff09;<\/p>\n<p>\/\/ 1. \u5b9a\u4e49\u72b6\u6001\u548c\u4e8b\u4ef6<br \/>\ntypedef enum {<br \/>\n    STATE_IDLE,<br \/>\n    STATE_DEBOUNCE_PRESS,<br \/>\n    STATE_SINGLE_CLICK,<br \/>\n    STATE_DOUBLE_CLICK,<br \/>\n} ButtonState_t;<\/p>\n<p>typedef enum {<br \/>\n    EVENT_NONE,<br \/>\n    EVENT_PRESS,<br \/>\n    EVENT_RELEASE,<br \/>\n    EVENT_TIMEOUT,<br \/>\n} ButtonEvent_t;<\/p>\n<p>\/\/ 2. \u72b6\u6001\u673a\u53d8\u91cf<br \/>\nstatic ButtonState_t g_btn_state &#061; STATE_IDLE;<br \/>\nstatic uint32_t g_press_time &#061; 0;<\/p>\n<p>\/\/ 3. \u72b6\u6001\u673a\u9a71\u52a8\u51fd\u6570<br \/>\nvoid button_fsm_run(ButtonEvent_t event) {<br \/>\n    switch (g_btn_state) {<br \/>\n        case STATE_IDLE:<br \/>\n            if (event &#061;&#061; EVENT_PRESS) {<br \/>\n                g_btn_state &#061; STATE_DEBOUNCE_PRESS;<br \/>\n                g_press_time &#061; get_current_time_ms(); \/\/ \u8bb0\u5f55\u6309\u4e0b\u65f6\u95f4<br \/>\n                start_debounce_timer(20); \/\/ \u542f\u52a820ms\u53bb\u6296\u5b9a\u65f6\u5668<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case STATE_DEBOUNCE_PRESS:<br \/>\n            if (event &#061;&#061; EVENT_TIMEOUT) { \/\/ \u53bb\u6296\u5b9a\u65f6\u5668\u8d85\u65f6<br \/>\n                if (is_button_still_pressed()) { \/\/ \u786e\u8ba4\u4ecd\u88ab\u6309\u4e0b<br \/>\n                    \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u52a0\u5165\u957f\u6309\u68c0\u6d4b\u903b\u8f91<br \/>\n                    g_btn_state &#061; STATE_SINGLE_CLICK; \/\/ \u6682\u5b9a\u4e3a\u5355\u51fb<br \/>\n                    start_single_click_timer(300); \/\/ \u542f\u52a8\u5355\u51fb\u8d85\u65f6\u5b9a\u65f6\u5668&#xff0c;\u7b49\u5f85\u53cc\u51fb<br \/>\n                } else {<br \/>\n                    g_btn_state &#061; STATE_IDLE; \/\/ \u6296\u52a8&#xff0c;\u8fd4\u56de\u7a7a\u95f2<br \/>\n                }<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case STATE_SINGLE_CLICK:<br \/>\n            if (event &#061;&#061; EVENT_PRESS) { \/\/ \u5728\u5355\u51fb\u7b49\u5f85\u671f\u5185\u518d\u6b21\u6309\u4e0b<br \/>\n                g_btn_state &#061; STATE_DOUBLE_CLICK;<br \/>\n                cancel_timer(single_click_timer); \/\/ \u53d6\u6d88\u5355\u51fb\u5b9a\u65f6\u5668<br \/>\n                handle_double_click(); \/\/ \u5904\u7406\u53cc\u51fb\u4e8b\u4ef6<br \/>\n            } else if (event &#061;&#061; EVENT_TIMEOUT) { \/\/ \u5355\u51fb\u5b9a\u65f6\u5668\u8d85\u65f6<br \/>\n                handle_single_click(); \/\/ \u5904\u7406\u5355\u51fb\u4e8b\u4ef6<br \/>\n                g_btn_state &#061; STATE_IDLE;<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case STATE_DOUBLE_CLICK:<br \/>\n            \/\/ \u7b49\u5f85\u6240\u6709\u6309\u952e\u91ca\u653e<br \/>\n            if (event &#061;&#061; EVENT_RELEASE) {<br \/>\n                 g_btn_state &#061; STATE_IDLE;<br \/>\n            }<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4ee3\u7801\u7ed3\u6784\u6e05\u6670&#xff0c;\u6613\u4e8e\u7406\u89e3\u548c\u8c03\u8bd5\u3002<\/li>\n<li>\u6240\u6709\u903b\u8f91\u96c6\u4e2d\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d&#xff0c;\u65b9\u4fbf\u67e5\u770b\u72b6\u6001\u8f6c\u6362\u3002<\/li>\n<li>\u6027\u80fd\u597d&#xff0c;\u6ca1\u6709\u51fd\u6570\u8c03\u7528\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u72b6\u6001\u548c\u4e8b\u4ef6\u589e\u591a\u65f6&#xff0c;switch \u8bed\u53e5\u4f1a\u53d8\u5f97\u975e\u5e38\u5e9e\u5927\u548c\u590d\u6742\u3002<\/li>\n<li>\u6dfb\u52a0\u65b0\u72b6\u6001\u9700\u8981\u4fee\u6539\u8fd9\u4e2a\u6838\u5fc3\u51fd\u6570&#xff0c;\u8fdd\u53cd\u4e86\u201c\u5f00\u95ed\u539f\u5219\u201d\u3002<\/li>\n<\/ul>\n<h5>4.4.2 \u72b6\u6001\u8868\u9a71\u52a8&#xff08;Table-Driven&#xff09;&#xff1a;\u6570\u636e\u4e0e\u903b\u8f91\u7684\u5206\u79bb<\/h5>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u72b6\u6001\u8f6c\u6362\u89c4\u5219\u4ece\u4ee3\u7801\u4e2d\u62bd\u79bb\u51fa\u6765&#xff0c;\u5b58\u653e\u5728\u4e00\u4e2a\u201c\u8f6c\u6362\u8868\u201d\u4e2d\u3002\u72b6\u6001\u673a\u5f15\u64ce\u53d8\u6210\u4e00\u4e2a\u901a\u7528\u7684\u201c\u89e3\u91ca\u5668\u201d&#xff0c;\u5b83\u53ea\u662f\u67e5\u8be2\u8868\u683c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u52a8\u4f5c\u3002<\/p>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u8868\u793a\u4e00\u6761\u8f6c\u6362\u89c4\u5219\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6240\u6709\u89c4\u5219\u7684\u5e38\u91cf\u6570\u7ec4&#xff08;\u5373\u72b6\u6001\u8868&#xff09;\u3002<\/li>\n<li>\u72b6\u6001\u673a\u51fd\u6570\u904d\u5386\u6216\u67e5\u627e\u8fd9\u4e2a\u8868&#xff0c;\u627e\u5230\u5339\u914d\u7684\u89c4\u5219\u5e76\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u4ea4\u901a\u706f\u72b6\u6001\u673a<\/p>\n<p>\/\/ 1. \u5b9a\u4e49\u72b6\u6001\u548c\u4e8b\u4ef6<br \/>\ntypedef enum { STATE_RED, STATE_GREEN, STATE_YELLOW } LightState_t;<br \/>\ntypedef enum { EVENT_TIMER_RED, EVENT_TIMER_GREEN, EVENT_TIMER_YELLOW } LightEvent_t;<\/p>\n<p>\/\/ 2. \u5b9a\u4e49\u52a8\u4f5c\u51fd\u6570<br \/>\nvoid action_red_on() { turn_red_led_on(); turn_green_led_off(); turn_yellow_led_off(); }<br \/>\nvoid action_green_on() { turn_red_led_off(); turn_green_led_on(); turn_yellow_led_off(); }<br \/>\nvoid action_yellow_on() { turn_red_led_off(); turn_green_led_off(); turn_yellow_led_on(); }<\/p>\n<p>\/\/ 3. \u5b9a\u4e49\u8f6c\u6362\u8868\u6761\u76ee<br \/>\ntypedef struct {<br \/>\n    LightState_t current_state;<br \/>\n    LightEvent_t event;<br \/>\n    void (*action_func)(void);<br \/>\n    LightState_t next_state;<br \/>\n} LightTransition_t;<\/p>\n<p>\/\/ 4. \u5b9a\u4e49\u72b6\u6001\u8868<br \/>\nconst LightTransition_t light_transition_table[] &#061; {<br \/>\n    \/\/ From RED state<br \/>\n    { STATE_RED,    EVENT_TIMER_RED,    action_green_on,  STATE_GREEN },<\/p>\n<p>    \/\/ From GREEN state<br \/>\n    { STATE_GREEN,  EVENT_TIMER_GREEN,  action_yellow_on, STATE_YELLOW },<\/p>\n<p>    \/\/ From YELLOW state<br \/>\n    { STATE_YELLOW, EVENT_TIMER_YELLOW, action_red_on,    STATE_RED },<\/p>\n<p>    \/\/ Add illegal transition handlers if needed<br \/>\n    \/\/ { STATE_RED, EVENT_TIMER_GREEN, handle_illegal_transition, STATE_RED },<br \/>\n};<\/p>\n<p>#define TABLE_SIZE (sizeof(light_transition_table) \/ sizeof(LightTransition_t))<\/p>\n<p>\/\/ 5. \u901a\u7528\u7684\u72b6\u6001\u673a\u5f15\u64ce<br \/>\nvoid light_fsm_run(LightState_t *current_state, LightEvent_t event) {<br \/>\n    for (int i &#061; 0; i &lt; TABLE_SIZE; &#043;&#043;i) {<br \/>\n        if (light_transition_table[i].current_state  *current_state &amp;&amp;<br \/>\n            light_transition_table[i].event  event) {<\/p>\n<p>            \/\/ \u6267\u884c\u52a8\u4f5c<br \/>\n            if (light_transition_table[i].action_func) {<br \/>\n                light_transition_table[i].action_func();<br \/>\n            }<\/p>\n<p>            \/\/ \u8f6c\u6362\u72b6\u6001<br \/>\n            *current_state &#061; light_transition_table[i].next_state;<br \/>\n            return; \/\/ \u627e\u5230\u5e76\u6267\u884c&#xff0c;\u9000\u51fa<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u8f6c\u6362&#xff0c;\u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u975e\u6cd5\u4e8b\u4ef6<br \/>\n    \/\/ handle_illegal_transition(*current_state, event);<br \/>\n}<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u9ad8\u5ea6\u53ef\u914d\u7f6e&#xff1a;\u4fee\u6539\u72b6\u6001\u673a\u884c\u4e3a\u53ea\u9700\u4fee\u6539\u8868\u683c\u6570\u636e&#xff0c;\u65e0\u9700\u6539\u52a8\u5f15\u64ce\u4ee3\u7801\u3002\u53ef\u4ee5\u5c06\u8868\u683c\u5b58\u50a8\u5728Flash\u6216\u4ece\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u3002<\/li>\n<li>\u903b\u8f91\u4e0e\u6570\u636e\u5206\u79bb&#xff1a;\u72b6\u6001\u673a\u5f15\u64ce\u975e\u5e38\u901a\u7528&#xff0c;\u53ef\u4ee5\u88ab\u591a\u4e2a\u4e0d\u540c\u7684\u72b6\u6001\u673a\u590d\u7528\u3002<\/li>\n<li>\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55&#xff1a;\u6dfb\u52a0\u65b0\u72b6\u6001\u6216\u8f6c\u6362\u53ea\u9700\u5728\u8868\u683c\u4e2d\u589e\u52a0\u4e00\u884c\u3002<\/li>\n<li>\u53ef\u9a8c\u8bc1\u6027&#xff1a;\u53ef\u4ee5\u7f16\u5199\u5de5\u5177\u6765\u9759\u6001\u5206\u6790\u8868\u683c\u7684\u5b8c\u6574\u6027&#xff08;\u5982\u662f\u5426\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5bf9\u6240\u6709\u4e8b\u4ef6\u7684\u5904\u7406&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u6027\u80fd\u5f00\u9500&#xff1a;\u7ebf\u6027\u67e5\u627e\u8868\u683c\u6bd4 switch-case \u6162\u3002\u5bf9\u4e8e\u5927\u578b\u8868\u683c&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u6216\u54c8\u5e0c\u8868\u4f18\u5316\u3002<\/li>\n<li>\u5185\u5b58\u5f00\u9500&#xff1a;\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u6765\u5b58\u50a8\u8868\u683c\u3002<\/li>\n<li>\u4e0d\u9002\u5408\u590d\u6742\u52a8\u4f5c&#xff1a;\u5982\u679c\u6bcf\u4e2a\u8f6c\u6362\u7684\u52a8\u4f5c\u975e\u5e38\u590d\u6742&#xff0c;\u7528\u51fd\u6570\u6307\u9488\u8868\u793a\u4f1a\u663e\u5f97\u7b28\u62d9\u3002<\/li>\n<\/ul>\n<h5>4.4.3 \u51fd\u6570\u6307\u9488\u6cd5&#xff1a;\u6781\u81f4\u7684\u6a21\u5757\u5316\u4e0e\u6269\u5c55\u6027<\/h5>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u6bcf\u4e2a\u72b6\u6001\u90fd\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\u3002\u72b6\u6001\u673a\u7684\u6838\u5fc3\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488&#xff0c;\u6307\u5411\u5f53\u524d\u72b6\u6001\u7684\u5904\u7406\u51fd\u6570\u3002\u6bcf\u4e2a\u72b6\u6001\u51fd\u6570\u6267\u884c\u5b8c\u81ea\u5df1\u7684\u903b\u8f91\u540e&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u72b6\u6001\u51fd\u6570\u7684\u6307\u9488\u3002<\/p>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<ul>\n<li>\u4e3a\u6bcf\u4e2a\u72b6\u6001\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570&#xff0c;\u51fd\u6570\u7b7e\u540d\u76f8\u540c&#xff08;\u4f8b\u5982 StateFunc_t StateName(void)&#xff09;\u3002<\/li>\n<li>\u7528\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u53d8\u91cf current_state_func \u6765\u4fdd\u5b58\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>\u4e3b\u5faa\u73af\u4e0d\u65ad\u8c03\u7528 current_state_func&#xff0c;\u5e76\u7528\u5176\u8fd4\u56de\u503c\u66f4\u65b0 current_state_func\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u8bbe\u5907\u542f\u52a8\u6d41\u7a0b<\/p>\n<p>\/\/ 1. \u58f0\u660e\u72b6\u6001\u51fd\u6570\u7c7b\u578b\u548c\u51fd\u6570<br \/>\ntypedef void* (*StateFunc_t)(void); \/\/ \u8fd4\u56de\u4e0b\u4e00\u4e2a\u72b6\u6001\u7684\u51fd\u6570\u6307\u9488<\/p>\n<p>void* state_initializing(void);<br \/>\nvoid* state_self_test(void);<br \/>\nvoid* state_ready(void);<br \/>\nvoid* state_error(void);<\/p>\n<p>\/\/ 2. \u5f53\u524d\u72b6\u6001\u51fd\u6570\u6307\u9488<br \/>\nStateFunc_t g_current_state_func &#061; state_initializing;<\/p>\n<p>\/\/ 3. \u4e3b\u5faa\u73af<br \/>\nvoid main_loop() {<br \/>\n    while (1) {<br \/>\n        if (g_current_state_func) {<br \/>\n            g_current_state_func &#061; (StateFunc_t)g_current_state_func();<br \/>\n        } else {<br \/>\n            \/\/ \u6700\u7ec8\u72b6\u6001&#xff0c;\u53ef\u4ee5\u5728\u6b64\u4f11\u7720<br \/>\n            enter_sleep_mode();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ 4. \u72b6\u6001\u51fd\u6570\u7684\u5b9e\u73b0<br \/>\nvoid* state_initializing(void) {<br \/>\n    printf(&#034;Initializing&#8230;\\\\n&#034;);<br \/>\n    \/\/ &#8230; \u521d\u59cb\u5316\u786c\u4ef6 &#8230;<br \/>\n    if (init_success) {<br \/>\n        return state_self_test; \/\/ \u8f6c\u6362\u5230\u81ea\u68c0\u72b6\u6001<br \/>\n    } else {<br \/>\n        return state_error; \/\/ \u8f6c\u6362\u5230\u9519\u8bef\u72b6\u6001<br \/>\n    }<br \/>\n}<\/p>\n<p>void* state_self_test(void) {<br \/>\n    printf(&#034;Self-testing&#8230;\\\\n&#034;);<br \/>\n    \/\/ &#8230; \u6267\u884cRAM\/Flash\u6d4b\u8bd5\u7b49 &#8230;<br \/>\n    if (test_passed) {<br \/>\n        return state_ready; \/\/ \u8f6c\u6362\u5230\u5c31\u7eea\u72b6\u6001<br \/>\n    } else {<br \/>\n        return state_error; \/\/ \u8f6c\u6362\u5230\u9519\u8bef\u72b6\u6001<br \/>\n    }<br \/>\n}<\/p>\n<p>void* state_ready(void) {<br \/>\n    printf(&#034;Ready. Waiting for commands.\\\\n&#034;);<br \/>\n    \/\/ \u5728\u8fd9\u4e2a\u72b6\u6001\u4e0b&#xff0c;\u53ef\u4ee5\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u7f51\u7edc\u547d\u4ee4<br \/>\n    \/\/ \u8fd9\u4e2a\u72b6\u6001\u51fd\u6570\u53ef\u80fd\u4f1a\u4e00\u76f4\u8fd4\u56de\u81ea\u8eab&#xff0c;\u76f4\u5230\u4e00\u4e2a\u5916\u90e8\u4e8b\u4ef6\u53d1\u751f<br \/>\n    \/\/ \u4f8b\u5982&#xff0c;\u901a\u8fc7\u4e00\u4e2a\u5168\u5c40\u547d\u4ee4\u53d8\u91cf\u6765\u6539\u53d8\u72b6\u6001<br \/>\n    if (g_user_command &#061;&#061; CMD_START_TASK) {<br \/>\n        \/\/ start_task();<br \/>\n        g_user_command &#061; CMD_NONE;<br \/>\n    }<br \/>\n    \/\/ \u4fdd\u6301\u5728\u5c31\u7eea\u72b6\u6001<br \/>\n    return state_ready;<br \/>\n}<\/p>\n<p>void* state_error(void) {<br \/>\n    printf(&#034;Error! System halted.\\\\n&#034;);<br \/>\n    \/\/ \u70b9\u4eae\u9519\u8befLED&#xff0c;\u505c\u6b62\u7cfb\u7edf<br \/>\n    while(1) { \/* Halt *\/ }<br \/>\n    return NULL; \/\/ \u4e0d\u4f1a\u8fd4\u56de<br \/>\n}<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u6781\u9ad8\u7684\u6a21\u5757\u5316&#xff1a;\u6bcf\u4e2a\u72b6\u6001\u7684\u903b\u8f91\u5b8c\u5168\u72ec\u7acb&#xff0c;\u5c01\u88c5\u5728\u81ea\u5df1\u7684\u51fd\u6570\u4e2d&#xff0c;\u4ee3\u7801\u975e\u5e38\u5e72\u51c0\u3002<\/li>\n<li>\u6613\u4e8e\u6269\u5c55&#xff1a;\u589e\u52a0\u65b0\u72b6\u6001\u5c31\u662f\u589e\u52a0\u4e00\u4e2a\u65b0\u51fd\u6570&#xff0c;\u5bf9\u73b0\u6709\u4ee3\u7801\u65e0\u4efb\u4f55\u5f71\u54cd\u3002<\/li>\n<li>\u53ef\u8bfb\u6027\u5f3a&#xff1a;\u4e3b\u5faa\u73af\u6781\u5176\u7b80\u5355&#xff0c;\u6e05\u6670\u5730\u8868\u8fbe\u4e86\u72b6\u6001\u673a\u7684\u9a71\u52a8\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u51fd\u6570\u8c03\u7528\u5f00\u9500&#xff1a;\u6bcf\u6b21\u72b6\u6001\u8f6c\u6362\u90fd\u6709\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u548c\u8fd4\u56de&#xff0c;\u6bd4 switch-case \u6162\u3002<\/li>\n<li>\u72b6\u6001\u95f4\u901a\u4fe1\u56f0\u96be&#xff1a;\u72b6\u6001\u51fd\u6570\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u6bd4\u8f83\u9ebb\u70e6&#xff0c;\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf&#xff0c;\u8fd9\u53ef\u80fd\u7834\u574f\u5c01\u88c5\u6027\u3002<\/li>\n<li>\u4e0d\u9002\u5408\u5171\u4eab\u4e0a\u4e0b\u6587&#xff1a;\u5982\u679c\u6240\u6709\u72b6\u6001\u90fd\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u5927\u7684\u4e0a\u4e0b\u6587\u7ed3\u6784&#xff0c;\u6bcf\u6b21\u90fd\u8981\u4f20\u9012\u6307\u9488&#xff0c;\u4f1a\u4f7f\u51fd\u6570\u7b7e\u540d\u53d8\u5f97\u590d\u6742\u3002<\/li>\n<\/ul>\n<h5>4.4.4 \u5206\u5c42\u72b6\u6001\u673a&#xff08;HSM&#xff09;&#xff1a;\u7ba1\u7406\u590d\u6742\u6027\u7684\u7ec8\u6781\u6b66\u5668<\/h5>\n<p>\u5f53\u72b6\u6001\u6570\u91cf\u7206\u70b8\u5f0f\u589e\u957f\u65f6&#xff0c;\u5373\u4f7f\u662f switch-case \u4e5f\u4f1a\u53d8\u5f97\u96be\u4ee5\u7ba1\u7406\u3002\u5206\u5c42\u72b6\u6001\u673a&#xff08;Hierarchical State Machine, HSM&#xff09;\u901a\u8fc7\u5f15\u5165\u201c\u7236\u72b6\u6001\u201d\u548c\u201c\u5b50\u72b6\u6001\u201d\u7684\u6982\u5ff5\u6765\u7ec4\u7ec7\u72b6\u6001\u3002<\/p>\n<ul>\n<li>\u7236\u72b6\u6001&#xff08;Superstate&#xff09;&#xff1a;\u4e00\u4e2a\u5305\u542b\u5176\u4ed6\u72b6\u6001&#xff08;\u5b50\u72b6\u6001&#xff09;\u7684\u72b6\u6001\u3002<\/li>\n<li>\u5b50\u72b6\u6001&#xff08;Substate&#xff09;&#xff1a;\u5728\u7236\u72b6\u6001\u5185\u90e8\u7684\u72b6\u6001\u3002<\/li>\n<li>\u7ee7\u627f&#xff1a;\u5b50\u72b6\u6001\u7ee7\u627f\u7236\u72b6\u6001\u7684\u884c\u4e3a\u3002\u5982\u679c\u4e00\u4e2a\u4e8b\u4ef6\u5728\u5b50\u72b6\u6001\u4e2d\u6ca1\u6709\u88ab\u5904\u7406&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u201c\u5192\u6ce1\u201d\u5230\u7236\u72b6\u6001\u4e2d\u53bb\u5904\u7406\u3002<\/li>\n<li>\u5386\u53f2\u72b6\u6001&#xff1a;\u5f53\u9000\u51fa\u4e00\u4e2a\u7236\u72b6\u6001\u518d\u8fd4\u56de\u65f6&#xff0c;\u53ef\u4ee5\u9009\u62e9\u8fdb\u5165\u5176\u9ed8\u8ba4\u7684\u5b50\u72b6\u6001&#xff0c;\u6216\u8005\u8fdb\u5165\u4e0a\u6b21\u79bb\u5f00\u65f6\u7684\u5b50\u72b6\u6001&#xff08;\u5386\u53f2\u72b6\u6001&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u4e00\u4e2a\u590d\u6742\u7684\u7535\u673a\u63a7\u5236\u7cfb\u7edf<\/p>\n<ul>\n<li>STATE_OPERATIONAL (\u7236\u72b6\u6001)\n<ul>\n<li>SUBSTATE_IDLE (\u5b50\u72b6\u6001)<\/li>\n<li>SUBSTATE_RUNNING (\u5b50\u72b6\u6001)\n<ul>\n<li>SUBSTATE_FORWARD<\/li>\n<li>SUBSTATE_BACKWARD<\/li>\n<\/ul>\n<\/li>\n<li>SUBSTATE_FAULT (\u5b50\u72b6\u6001)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5982\u679c\u7cfb\u7edf\u5728 STATE_OPERATIONAL \u72b6\u6001\u4e0b\u6536\u5230 EVENT_STOP \u4e8b\u4ef6&#xff0c;\u5b83\u4f1a\u8f6c\u6362\u5230 SUBSTATE_IDLE\u3002\u5982\u679c\u5728 SUBSTATE_RUNNING -&gt; SUBSTATE_FORWARD \u72b6\u6001\u4e0b\u6536\u5230 EVENT_STOP&#xff0c;\u5b83\u4e5f\u4f1a\u8f6c\u6362\u5230 SUBSTATE_IDLE\u3002\u8fd9\u4e2a EVENT_STOP \u7684\u5904\u7406\u903b\u8f91\u53ef\u4ee5\u53ea\u5728\u7236\u72b6\u6001 STATE_OPERATIONAL \u4e2d\u5b9a\u4e49\u4e00\u6b21&#xff0c;\u6240\u6709\u5b50\u72b6\u6001\u90fd\u4f1a\u7ee7\u627f\u8fd9\u4e2a\u884c\u4e3a\u3002<\/p>\n<p>\u5b9e\u73b0&#xff1a; HSM\u7684\u624b\u52a8\u5b9e\u73b0\u975e\u5e38\u590d\u6742&#xff0c;\u901a\u5e38\u9700\u8981\u501f\u52a9\u4e13\u95e8\u7684\u6846\u67b6&#xff0c;\u5982 QP\/C (Quantum Programming in C) \u6216 UML State Machine frameworks\u3002\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8f\u548cAPI\u6765\u5b9a\u4e49\u72b6\u6001\u3001\u4e8b\u4ef6\u3001\u8f6c\u6362\u548c\u5c42\u6b21\u7ed3\u6784&#xff0c;\u5e76\u81ea\u52a8\u5904\u7406\u4e8b\u4ef6\u5192\u6ce1\u3001\u5386\u53f2\u72b6\u6001\u7b49\u590d\u6742\u903b\u8f91\u3002<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u6781\u5927\u5730\u51cf\u5c11\u72b6\u6001\u6570\u91cf&#xff1a;\u901a\u8fc7\u5171\u4eab\u884c\u4e3a&#xff0c;\u907f\u514d\u4e86\u72b6\u6001\u7206\u70b8\u3002<\/li>\n<li>\u9ad8\u5ea6\u7ed3\u6784\u5316&#xff1a;\u80fd\u591f\u6e05\u6670\u5730\u5efa\u6a21\u590d\u6742\u7684\u3001\u5d4c\u5957\u7684\u7cfb\u7edf\u884c\u4e3a\u3002<\/li>\n<li>\u53ef\u590d\u7528\u6027&#xff1a;\u7236\u72b6\u6001\u53ca\u5176\u884c\u4e3a\u53ef\u4ee5\u5728\u4e0d\u540c\u7684HSM\u4e2d\u590d\u7528\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5b66\u4e60\u66f2\u7ebf\u9661\u5ced&#xff1a;\u7406\u89e3\u548c\u4f7f\u7528HSM\u6846\u67b6\u9700\u8981\u65f6\u95f4\u3002<\/li>\n<li>\u5f00\u9500\u8f83\u5927&#xff1a;\u6846\u67b6\u672c\u8eab\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u4ee3\u7801\u548c\u6027\u80fd\u5f00\u9500\u3002<\/li>\n<li>\u8fc7\u5ea6\u8bbe\u8ba1\u98ce\u9669&#xff1a;\u5bf9\u4e8e\u7b80\u5355\u7cfb\u7edf&#xff0c;\u4f7f\u7528HSM\u662f\u6740\u9e21\u7528\u725b\u5200\u3002<\/li>\n<\/ul>\n<h4>4.5 \u72b6\u6001\u673a\u8bbe\u8ba1\u7684\u6700\u4f73\u5b9e\u8df5\u4e0e\u201c\u907f\u5751\u6307\u5357\u201d<\/h4>\n<p>\u2705 \u660e\u786e\u5b9a\u4e49\u72b6\u6001\u548c\u4e8b\u4ef6&#xff1a;\u4f7f\u7528 enum \u4e3a\u6240\u6709\u72b6\u6001\u548c\u4e8b\u4ef6\u547d\u540d&#xff0c;\u5e76\u6dfb\u52a0\u524d\u7f00&#xff08;\u5982 STATE_, EVENT_&#xff09;&#xff0c;\u589e\u5f3a\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u2705 \u5148\u753b\u56fe&#xff0c;\u518d\u7f16\u7801&#xff1a;\u575a\u6301\u4f7f\u7528STD\u3002\u5b83\u662f\u4f60\u601d\u8003\u3001\u8bbe\u8ba1\u3001\u6c9f\u901a\u548c\u6587\u6863\u5316\u7684\u6838\u5fc3\u5de5\u5177\u3002<\/p>\n<p>\u2705 \u5904\u7406\u975e\u6cd5\u4e8b\u4ef6&#xff1a;\u5728\u6bcf\u4e2a case \u7684 default \u5206\u652f\u6216\u72b6\u6001\u8868\u7684\u672b\u5c3e&#xff0c;\u5fc5\u987b\u6709\u5904\u7406\u975e\u6cd5\u4e8b\u4ef6\u7684\u903b\u8f91\u3002\u53ef\u4ee5\u662f\u65ad\u8a00&#xff08;assert(0)&#xff09;\u3001\u8fdb\u5165\u9519\u8bef\u72b6\u6001\u3001\u6216\u8005\u4ec5\u4ec5\u662f\u5ffd\u7565\u5e76\u6253\u5370\u4e00\u6761\u8b66\u544a\u65e5\u5fd7\u3002\u7edd\u4e0d\u80fd\u8ba9\u975e\u6cd5\u4e8b\u4ef6\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/p>\n<p>\u2705 \u6dfb\u52a0\u8c03\u8bd5\u65e5\u5fd7&#xff1a;\u5728\u6bcf\u6b21\u72b6\u6001\u8f6c\u6362\u65f6\u6253\u5370\u65e5\u5fd7\u662f\u8c03\u8bd5\u72b6\u6001\u673a\u7684\u6551\u547d\u7a3b\u8349\u3002 printf(&#034;State: %s -&gt; %s, Event: %s\\\\r\\\\n&#034;, state_to_str(old_state), state_to_str(new_state), event_to_str(event)); \u7f16\u5199\u4e00\u4e2a\u8f85\u52a9\u51fd\u6570\u5c06 enum \u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32&#xff0c;\u4f1a\u6781\u5927\u65b9\u4fbf\u8c03\u8bd5\u3002<\/p>\n<p>\u2705 \u4f7f\u7528\u8d85\u65f6\u673a\u5236&#xff1a;\u4e3a\u53ef\u80fd\u201c\u5361\u6b7b\u201d\u7684\u72b6\u6001&#xff08;\u5982\u7b49\u5f85\u786c\u4ef6\u54cd\u5e94&#xff09;\u6dfb\u52a0\u8d85\u65f6\u5b9a\u65f6\u5668\u3002\u5982\u679c\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u6536\u5230\u9884\u671f\u4e8b\u4ef6&#xff0c;\u5c31\u5f3a\u5236\u8f6c\u6362\u5230\u9519\u8bef\u6216\u7a7a\u95f2\u72b6\u6001\u3002\u8fd9\u80fd\u6781\u5927\u63d0\u9ad8\u7cfb\u7edf\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<p>\u2705 \u533a\u5206Entry\/Exit\/Do\u52a8\u4f5c&#xff1a;<\/p>\n<ul>\n<li>Entry Action&#xff1a;\u8fdb\u5165\u65f6\u6267\u884c\u4e00\u6b21\u7684\u52a8\u4f5c&#xff08;\u5982\u70b9\u4eae\u4e00\u4e2a\u201c\u8fd0\u884c\u4e2d\u201d\u7684LED&#xff09;\u3002<\/li>\n<li>Exit Action&#xff1a;\u9000\u51fa\u72b6\u6001\u65f6\u6267\u884c\u4e00\u6b21\u7684\u52a8\u4f5c&#xff08;\u5982\u5173\u95ed\u201c\u8fd0\u884c\u4e2d\u201d\u7684LED&#xff09;\u3002<\/li>\n<li>Do Action&#xff1a;\u5728\u72b6\u6001\u671f\u95f4\u6301\u7eed\u6267\u884c\u7684\u52a8\u4f5c&#xff08;\u5982\u5728 CHARGING \u72b6\u6001\u6301\u7eed\u68c0\u6d4b\u7535\u6d41&#xff09;\u3002 \u5728 switch-case \u5b9e\u73b0\u4e2d&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u7ec4\u7ec7&#xff1a;<\/li>\n<\/ul>\n<p>switch (state) {<br \/>\n    case STATE_A:<br \/>\n        \/\/ Entry Action (\u5982\u679c\u662f\u4ece\u5176\u4ed6\u72b6\u6001\u8f6c\u6362\u6765\u7684)<br \/>\n        if (prev_state !&#061; STATE_A) { \/* do entry action *\/ }<\/p>\n<p>        \/\/ Do Action<br \/>\n        while (event  EVENT_NONE) { \/* do something *\/ }<\/p>\n<p>        \/\/ Transition logic<br \/>\n        if (event  EVENT_X) {<br \/>\n            \/\/ Exit Action<br \/>\n            \/* do exit action *\/<br \/>\n            state &#061; STATE_B;<br \/>\n        }<br \/>\n        break;<br \/>\n    \/\/ &#8230;<br \/>\n}<\/p>\n<p>\u6216\u8005&#xff0c;\u66f4\u6e05\u6670\u7684\u65b9\u5f0f\u662f\u628aEntry\/Exit\u52a8\u4f5c\u653e\u5728\u8f6c\u6362\u903b\u8f91\u4e2d\u3002<\/p>\n<p>\u26a0\ufe0f \u907f\u514d\u8017\u65f6\u64cd\u4f5c&#xff1a;\u72b6\u6001\u673a\u51fd\u6570\u5e94\u8be5\u5feb\u901f\u6267\u884c\u5e76\u8fd4\u56de\u3002\u7edd\u5bf9\u4e0d\u8981\u5728\u72b6\u6001\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528 HAL_Delay() \u6216\u8fdb\u884c\u957f\u65f6\u95f4\u7684\u5faa\u73af\u3002\u8017\u65f6\u7684\u64cd\u4f5c\u5e94\u8be5\u88ab\u5206\u89e3\u6210\u4e00\u4e2a\u65b0\u7684\u72b6\u6001&#xff0c;\u6216\u8005\u4ea4\u7ed9\u4e00\u4e2a\u4e13\u95e8\u7684\u4efb\u52a1\u53bb\u5904\u7406\u3002<\/p>\n<p>\u26a0\ufe0f \u4fdd\u6301\u72b6\u6001\u7eaf\u7cb9&#xff1a;\u72b6\u6001\u5904\u7406\u51fd\u6570\u53ea\u5e94\u8be5\u5173\u5fc3\u72b6\u6001\u8f6c\u6362\u548c\u4e0e\u8f6c\u6362\u76f8\u5173\u7684\u52a8\u4f5c\u3002\u4e0d\u8981\u5728\u91cc\u9762\u505a\u4e0e\u72b6\u6001\u903b\u8f91\u65e0\u5173\u7684\u8ba1\u7b97\u6216I\/O\u64cd\u4f5c\u3002<\/p>\n<p>\u26a0\ufe0f \u8b66\u60d5\u5168\u5c40\u53d8\u91cf&#xff1a;\u867d\u7136\u72b6\u6001\u673a\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u4fdd\u5b58 current_state&#xff0c;\u4f46\u5e94\u5c3d\u91cf\u51cf\u5c11\u5176\u4ed6\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u3002\u72b6\u6001\u673a\u6240\u9700\u7684\u6570\u636e\u6700\u597d\u5c01\u88c5\u5728\u4e00\u4e2a\u4e0a\u4e0b\u6587\u4e2d&#xff08;context \u7ed3\u6784\u4f53&#xff09;&#xff0c;\u5e76\u901a\u8fc7\u6307\u9488\u4f20\u9012\u3002<\/p>\n<hr \/>\n<h3>\u7b2c\u4e94\u7ae0&#xff1a;\u5b9e\u6218\u91cd\u6784\u2014\u2014\u7528FSM\u91cd\u5851\u5145\u7535\u7ba1\u7406\u7cfb\u7edf<\/h3>\n<p>\u8ba9\u6211\u4eec\u56de\u5230\u6700\u521d\u7684\u201c\u903b\u8f91\u5730\u72f1\u201d&#xff0c;\u7528FSM\u5c06\u5176\u5f7b\u5e95\u6539\u9020\u3002<\/p>\n<h4>5.1 \u9700\u6c42\u5206\u6790 &amp; \u72b6\u6001\u56fe<\/h4>\n<p>\u9700\u6c42&#xff1a;<\/p>\n<li>\u7cfb\u7edf\u4e0a\u7535\u6216\u672a\u63d2\u5165\u5145\u7535\u5668\u65f6&#xff0c;\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u3002<\/li>\n<li>\u63d2\u5165\u5145\u7535\u5668\u540e&#xff0c;\u5982\u679c\u7535\u6c60\u6e29\u5ea6\u6b63\u5e38\u4e14\u672a\u5145\u6ee1&#xff0c;\u5219\u5f00\u59cb\u5145\u7535\u3002<\/li>\n<li>\u5145\u7535\u8fc7\u7a0b\u4e2d&#xff0c;\u5982\u679c\u7535\u6c60\u6e29\u5ea6\u8fc7\u9ad8&#xff0c;\u5219\u505c\u6b62\u5145\u7535\u5e76\u8fdb\u5165\u9519\u8bef\u72b6\u6001\u3002<\/li>\n<li>\u5145\u7535\u8fc7\u7a0b\u4e2d&#xff0c;\u5982\u679c\u7535\u6c60\u5145\u6ee1&#xff0c;\u5219\u505c\u6b62\u5145\u7535\u5e76\u8fdb\u5165\u5145\u7535\u5b8c\u6210\u72b6\u6001\u3002<\/li>\n<li>\u5728\u5145\u7535\u5b8c\u6210\u6216\u9519\u8bef\u72b6\u6001\u4e0b&#xff0c;\u62d4\u51fa\u5145\u7535\u5668&#xff0c;\u7cfb\u7edf\u8fd4\u56de\u7a7a\u95f2\u72b6\u6001\u3002<\/li>\n<li>\u5728\u9519\u8bef\u72b6\u6001\u4e0b&#xff0c;\u9700\u8981\u624b\u52a8\u5e72\u9884\u6216\u7cfb\u7edf\u81ea\u52a8\u6062\u590d\u540e\u624d\u80fd\u518d\u6b21\u5145\u7535\u3002<\/li>\n<p>\u72b6\u6001&#xff08;State&#xff09;\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>CHG_STATE_IDLE: \u7a7a\u95f2&#xff0c;\u672a\u8fde\u63a5\u5145\u7535\u5668\u3002<\/li>\n<li>CHG_STATE_CHARGING: \u6b63\u5728\u5145\u7535\u3002<\/li>\n<li>CHG_STATE_DONE: \u5145\u7535\u5b8c\u6210\u3002<\/li>\n<li>CHG_STATE_ERROR: \u5145\u7535\u9519\u8bef&#xff08;\u5982\u6e29\u5ea6\u8fc7\u9ad8&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u4e8b\u4ef6&#xff08;Event&#xff09;\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>EV_PLUG_IN: \u63d2\u5165\u5145\u7535\u5668\u3002<\/li>\n<li>EV_PLUG_OUT: \u62d4\u51fa\u5145\u7535\u5668\u3002<\/li>\n<li>EV_CHARGE_COMPLETE: \u7535\u6c60\u5145\u6ee1&#xff08;\u7531ADC\u548c\u7b97\u6cd5\u5224\u65ad&#xff09;\u3002<\/li>\n<li>EV_TEMP_HIGH: \u7535\u6c60\u6e29\u5ea6\u8fc7\u9ad8&#xff08;\u7531\u6e29\u5ea6\u4f20\u611f\u5668\u548c\u9608\u503c\u5224\u65ad&#xff09;\u3002<\/li>\n<li>EV_ERROR_CLEARED: \u9519\u8bef\u88ab\u6e05\u9664&#xff08;\u4f8b\u5982&#xff0c;\u6e29\u5ea6\u6062\u590d\u6b63\u5e38&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u72b6\u6001\u8f6c\u6362\u56fe&#xff08;STD&#xff09;&#xff1a; &#xff08;\u6b64\u5904\u5e94\u6709\u4e00\u5f20\u56fe&#xff0c;\u7528\u6587\u5b57\u63cf\u8ff0&#xff09;<\/p>\n<ul>\n<li>IDLE &#8212; PLUG_IN &#8211;&gt; CHARGING<\/li>\n<li>CHARGING &#8212; PLUG_OUT &#8211;&gt; IDLE<\/li>\n<li>CHARGING &#8212; CHARGE_COMPLETE &#8211;&gt; DONE<\/li>\n<li>CHARGING &#8212; TEMP_HIGH &#8211;&gt; ERROR<\/li>\n<li>DONE &#8212; PLUG_OUT &#8211;&gt; IDLE<\/li>\n<li>ERROR &#8212; PLUG_OUT &#8211;&gt; IDLE<\/li>\n<li>ERROR &#8212; ERROR_CLEARED &#8211;&gt; IDLE (\u6216\u5176\u4ed6\u6062\u590d\u903b\u8f91)<\/li>\n<\/ul>\n<h4>5.2 \u4ee3\u7801\u5b9e\u73b0 (Switch-Case\u7248)<\/h4>\n<p>\u8fd9\u662f\u6700\u76f4\u63a5\u3001\u6700\u6e05\u6670\u7684\u91cd\u6784\u65b9\u5f0f\u3002<\/p>\n<p>\/\/ chg_fsm.h<br \/>\n#ifndef CHG_FSM_H<br \/>\n#define CHG_FSM_H<\/p>\n<p>#include &lt;stdint.h&gt;<br \/>\n#include &lt;stdbool.h&gt;<\/p>\n<p>\/\/ 1. \u5b9a\u4e49\u4e8b\u4ef6<br \/>\ntypedef enum {<br \/>\n    EV_NONE,<br \/>\n    EV_PLUG_IN,<br \/>\n    EV_PLUG_OUT,<br \/>\n    EV_CHARGE_COMPLETE,<br \/>\n    EV_TEMP_HIGH,<br \/>\n    EV_ERROR_CLEARED,<br \/>\n} ChargeEvent_t;<\/p>\n<p>\/\/ 2. \u5b9a\u4e49\u72b6\u6001<br \/>\ntypedef enum {<br \/>\n    CHG_STATE_IDLE,<br \/>\n    CHG_STATE_CHARGING,<br \/>\n    CHG_STATE_DONE,<br \/>\n    CHG_STATE_ERROR,<br \/>\n} ChargeState_t;<\/p>\n<p>\/\/ 3. \u516c\u5f00\u7684API<br \/>\nvoid charge_fsm_init(void);<br \/>\nvoid charge_fsm_process_event(ChargeEvent_t event);<br \/>\nChargeState_t charge_fsm_get_state(void);<\/p>\n<p>#endif \/\/ CHG_FSM_H<\/p>\n<p>\/\/ chg_fsm.c<br \/>\n#include &#034;chg_fsm.h&#034;<br \/>\n#include &lt;stdio.h&gt; \/\/ \u7528\u4e8e\u8c03\u8bd5\u65e5\u5fd7<\/p>\n<p>\/\/ \u5185\u90e8\u72b6\u6001\u53d8\u91cf<br \/>\nstatic ChargeState_t g_charge_state &#061; CHG_STATE_IDLE;<\/p>\n<p>\/\/ \u8f85\u52a9\u51fd\u6570&#xff1a;\u5c06\u72b6\u6001\/\u4e8b\u4ef6\u8f6c\u4e3a\u5b57\u7b26\u4e32\u7528\u4e8e\u6253\u5370<br \/>\nconst char* state_to_str(ChargeState_t state) {<br \/>\n    switch (state) {<br \/>\n        case CHG_STATE_IDLE: return &#034;IDLE&#034;;<br \/>\n        case CHG_STATE_CHARGING: return &#034;CHARGING&#034;;<br \/>\n        case CHG_STATE_DONE: return &#034;DONE&#034;;<br \/>\n        case CHG_STATE_ERROR: return &#034;ERROR&#034;;<br \/>\n        default: return &#034;UNKNOWN&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<p>const char* event_to_str(ChargeEvent_t event) {<br \/>\n    switch (event) {<br \/>\n        case EV_NONE: return &#034;NONE&#034;;<br \/>\n        case EV_PLUG_IN: return &#034;PLUG_IN&#034;;<br \/>\n        case EV_PLUG_OUT: return &#034;PLUG_OUT&#034;;<br \/>\n        case EV_CHARGE_COMPLETE: return &#034;CHARGE_COMPLETE&#034;;<br \/>\n        case EV_TEMP_HIGH: return &#034;TEMP_HIGH&#034;;<br \/>\n        case EV_ERROR_CLEARED: return &#034;ERROR_CLEARED&#034;;<br \/>\n        default: return &#034;UNKNOWN&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<p>void charge_fsm_init(void) {<br \/>\n    g_charge_state &#061; CHG_STATE_IDLE;<br \/>\n    printf(&#034;FSM Initialized. State: %s\\\\n&#034;, state_to_str(g_charge_state));<br \/>\n}<\/p>\n<p>void charge_fsm_process_event(ChargeEvent_t event) {<br \/>\n    if (event &#061;&#061; EV_NONE) return;<\/p>\n<p>    ChargeState_t old_state &#061; g_charge_state;<\/p>\n<p>    \/\/ \u8bb0\u5f55\u5f53\u524d\u72b6\u6001&#xff0c;\u7528\u4e8e\u8c03\u8bd5<br \/>\n    printf(&#034;Event: %s, Current State: %s\\\\n&#034;, event_to_str(event), state_to_str(g_charge_state));<\/p>\n<p>    switch (g_charge_state) {<br \/>\n        case CHG_STATE_IDLE:<br \/>\n            if (event &#061;&#061; EV_PLUG_IN) {<br \/>\n                printf(&#034;  Transition: IDLE -&gt; CHARGING\\\\n&#034;);<br \/>\n                \/\/ \u52a8\u4f5c&#xff1a;\u542f\u52a8\u5145\u7535<br \/>\n                \/\/ hardware_start_charging();<br \/>\n                g_charge_state &#061; CHG_STATE_CHARGING;<br \/>\n            }<br \/>\n            \/\/ \u5176\u4ed6\u4e8b\u4ef6\u5728\u6b64\u72b6\u6001\u4e0b\u88ab\u5ffd\u7565<br \/>\n            break;<\/p>\n<p>        case CHG_STATE_CHARGING:<br \/>\n            if (event &#061;&#061; EV_PLUG_OUT) {<br \/>\n                printf(&#034;  Transition: CHARGING -&gt; IDLE\\\\n&#034;);<br \/>\n                \/\/ \u52a8\u4f5c&#xff1a;\u505c\u6b62\u5145\u7535<br \/>\n                \/\/ hardware_stop_charging();<br \/>\n                g_charge_state &#061; CHG_STATE_IDLE;<br \/>\n            } else if (event &#061;&#061; EV_CHARGE_COMPLETE) {<br \/>\n                printf(&#034;  Transition: CHARGING -&gt; DONE\\\\n&#034;);<br \/>\n                \/\/ \u52a8\u4f5c&#xff1a;\u505c\u6b62\u5145\u7535<br \/>\n                \/\/ hardware_stop_charging();<br \/>\n                g_charge_state &#061; CHG_STATE_DONE;<br \/>\n            } else if (event &#061;&#061; EV_TEMP_HIGH) {<br \/>\n                printf(&#034;  Transition: CHARGING -&gt; ERROR\\\\n&#034;);<br \/>\n                \/\/ \u52a8\u4f5c&#xff1a;\u505c\u6b62\u5145\u7535&#xff0c;\u8bbe\u7f6e\u9519\u8bef\u6807\u5fd7<br \/>\n                \/\/ hardware_stop_charging();<br \/>\n                \/\/ g_error_code &#061; ERROR_OVERTEMP;<br \/>\n                g_charge_state &#061; CHG_STATE_ERROR;<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case CHG_STATE_DONE:<br \/>\n            if (event &#061;&#061; EV_PLUG_OUT) {<br \/>\n                printf(&#034;  Transition: DONE -&gt; IDLE\\\\n&#034;);<br \/>\n                g_charge_state &#061; CHG_STATE_IDLE;<br \/>\n            }<br \/>\n            \/\/ \u5982\u679c\u5728DONE\u72b6\u6001\u4e0b\u53c8\u63d2\u5165\u5145\u7535\u5668&#xff0c;\u53ef\u4ee5\u8bbe\u8ba1\u4e3a\u91cd\u65b0\u5f00\u59cb\u5145\u7535<br \/>\n            \/\/ else if (event  EV_PLUG_IN) { &#8230; }<br \/>\n            break;<\/p>\n<p>        case CHG_STATE_ERROR:<br \/>\n            if (event  EV_PLUG_OUT) {<br \/>\n                printf(&#034;  Transition: ERROR -&gt; IDLE\\\\n&#034;);<br \/>\n                \/\/ \u52a8\u4f5c&#xff1a;\u6e05\u9664\u9519\u8bef\u6807\u5fd7<br \/>\n                \/\/ g_error_code &#061; ERROR_NONE;<br \/>\n                g_charge_state &#061; CHG_STATE_IDLE;<br \/>\n            } else if (event &#061;&#061; EV_ERROR_CLEARED) {<br \/>\n                \/\/ \u4f8b\u5982&#xff0c;\u6e29\u5ea6\u6062\u590d\u6b63\u5e38\u540e&#xff0c;\u53ef\u4ee5\u56de\u5230IDLE\u6216CHARGING<br \/>\n                printf(&#034;  Transition: ERROR -&gt; IDLE (Cleared)\\\\n&#034;);<br \/>\n                g_charge_state &#061; CHG_STATE_IDLE;<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        default:<br \/>\n            \/\/ \u975e\u6cd5\u72b6\u6001&#xff0c;\u8fdb\u5165\u9519\u8bef\u5904\u7406<br \/>\n            printf(&#034;  Error: Illegal state detected!\\\\n&#034;);<br \/>\n            g_charge_state &#061; CHG_STATE_ERROR;<br \/>\n            break;<br \/>\n    }<\/p>\n<p>    \/\/ \u7edf\u4e00\u7684Exit\/Entry\u52a8\u4f5c\u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406&#xff0c;\u6216\u8005\u5728\u6bcf\u4e2acase\u91cc\u5904\u7406<br \/>\n    if (old_state !&#061; g_charge_state) {<br \/>\n        printf(&#034;New State: %s\\\\n&#034;, state_to_str(g_charge_state));<br \/>\n        \/\/ \u4f8b\u5982&#xff0c;\u6839\u636e\u65b0\u72b6\u6001\u66f4\u65b0\u4e00\u4e2a\u5168\u5c40\u7684\u72b6\u6001\u6807\u5fd7&#xff0c;\u4f9b\u5176\u4ed6\u6a21\u5757\u67e5\u8be2<br \/>\n        \/\/ update_system_status_indicator(g_charge_state);<br \/>\n    }<br \/>\n}<\/p>\n<p>ChargeState_t charge_fsm_get_state(void) {<br \/>\n    return g_charge_state;<br \/>\n}<\/p>\n<h4>5.3 \u4ee3\u7801\u5b9e\u73b0&#xff08;\u72b6\u6001\u8868\u9a71\u52a8\u7248&#xff09;<\/h4>\n<p>\u4e3a\u4e86\u5c55\u793a\u5176\u53ef\u914d\u7f6e\u6027&#xff0c;\u6211\u4eec\u7528\u72b6\u6001\u8868\u6765\u5b9e\u73b0\u540c\u4e00\u4e2a\u903b\u8f91\u3002<\/p>\n<p>\/\/ chg_fsm_table.c<br \/>\n#include &#034;chg_fsm.h&#034;<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>\/\/ &#8230; (\u72b6\u6001\u548c\u4e8b\u4ef6\u7684enum\u5b9a\u4e49\u540c\u4e0a) &#8230;<\/p>\n<p>\/\/ \u52a8\u4f5c\u51fd\u6570<br \/>\nvoid action_start_charging() { printf(&#034;Action: Start Charging\\\\n&#034;); \/* hardware_start_charging(); *\/ }<br \/>\nvoid action_stop_charging() { printf(&#034;Action: Stop Charging\\\\n&#034;); \/* hardware_stop_charging(); *\/ }<br \/>\nvoid action_set_error_overtemp() { printf(&#034;Action: Set Overtemp Error\\\\n&#034;); \/* g_error_code &#061; ERROR_OVERTEMP; *\/ }<br \/>\nvoid action_clear_error() { printf(&#034;Action: Clear Error\\\\n&#034;); \/* g_error_code &#061; ERROR_NONE; *\/ }<\/p>\n<p>\/\/ \u8f6c\u6362\u8868\u6761\u76ee<br \/>\ntypedef struct {<br \/>\n    ChargeState_t current_state;<br \/>\n    ChargeEvent_t event;<br \/>\n    void (*action)(void);<br \/>\n    ChargeState_t next_state;<br \/>\n} ChargeTransition_t;<\/p>\n<p>\/\/ \u72b6\u6001\u8f6c\u6362\u8868<br \/>\nconst ChargeTransition_t charge_transition_table[] &#061; {<br \/>\n    \/\/ From IDLE<br \/>\n    { CHG_STATE_IDLE, EV_PLUG_IN, action_start_charging, CHG_STATE_CHARGING },<\/p>\n<p>    \/\/ From CHARGING<br \/>\n    { CHG_STATE_CHARGING, EV_PLUG_OUT, action_stop_charging, CHG_STATE_IDLE },<br \/>\n    { CHG_STATE_CHARGING, EV_CHARGE_COMPLETE, action_stop_charging, CHG_STATE_DONE },<br \/>\n    { CHG_STATE_CHARGING, EV_TEMP_HIGH, action_stop_charging, CHG_STATE_ERROR },<br \/>\n    \/\/ \u4e5f\u53ef\u4ee5\u5c06\u52a8\u4f5c\u5408\u5e76<br \/>\n    \/\/ { CHG_STATE_CHARGING, EV_TEMP_HIGH, action_set_error_overtemp, CHG_STATE_ERROR },<\/p>\n<p>    \/\/ From DONE<br \/>\n    { CHG_STATE_DONE, EV_PLUG_OUT, NULL, CHG_STATE_IDLE }, \/\/ NULL\u8868\u793a\u65e0\u52a8\u4f5c<\/p>\n<p>    \/\/ From ERROR<br \/>\n    { CHG_STATE_ERROR, EV_PLUG_OUT, action_clear_error, CHG_STATE_IDLE },<br \/>\n    { CHG_STATE_ERROR, EV_ERROR_CLEARED, action_clear_error, CHG_STATE_IDLE },<br \/>\n};<\/p>\n<p>#define TRANSITION_COUNT (sizeof(charge_transition_table) \/ sizeof(ChargeTransition_t))<\/p>\n<p>static ChargeState_t g_charge_state &#061; CHG_STATE_IDLE;<\/p>\n<p>void charge_fsm_process_event_table(ChargeEvent_t event) {<br \/>\n    if (event &#061;&#061; EV_NONE) return;<\/p>\n<p>    printf(&#034;Event: %s, Current State: %s\\\\n&#034;, event_to_str(event), state_to_str(g_charge_state));<\/p>\n<p>    for (int i &#061; 0; i &lt; TRANSITION_COUNT; i&#043;&#043;) {<br \/>\n        if (charge_transition_table[i].current_state  g_charge_state &amp;&amp;<br \/>\n            charge_transition_table[i].event  event) {<\/p>\n<p>            \/\/ \u6267\u884c\u52a8\u4f5c<br \/>\n            if (charge_transition_table[i].action) {<br \/>\n                charge_transition_table[i].action();<br \/>\n            }<\/p>\n<p>            \/\/ \u8f6c\u6362\u72b6\u6001<br \/>\n            g_charge_state &#061; charge_transition_table[i].next_state;<br \/>\n            printf(&#034;  Transition: %s -&gt; %s\\\\n&#034;, state_to_str(charge_transition_table[i].current_state), state_to_str(g_charge_state));<br \/>\n            return; \/\/ \u627e\u5230\u5e76\u6267\u884c&#xff0c;\u9000\u51fa<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u8f6c\u6362<br \/>\n    printf(&#034;  Warning: Unhandled event &#039;%s&#039; in state &#039;%s&#039;\\\\n&#034;, event_to_str(event), state_to_str(g_charge_state));<br \/>\n    \/\/ \u53ef\u4ee5\u9009\u62e9\u8fdb\u5165\u4e00\u4e2a\u9ed8\u8ba4\u7684\u9519\u8bef\u72b6\u6001<br \/>\n    \/\/ g_charge_state &#061; CHG_STATE_ERROR;<br \/>\n}<\/p>\n<h4>5.4 \u5f15\u5165\u8d85\u65f6\u4e0e\u9519\u8bef\u5904\u7406&#xff1a;\u6784\u5efa\u9c81\u68d2\u7684\u7cfb\u7edf<\/h4>\n<p>\u771f\u5b9e\u4e16\u754c\u7684\u7cfb\u7edf\u6c38\u8fdc\u4e0d\u4f1a\u50cf\u7406\u8bba\u6a21\u578b\u90a3\u6837\u5b8c\u7f8e\u3002\u6211\u4eec\u9700\u8981\u8003\u8651&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u5145\u7535\u7535\u6d41\u4e00\u76f4\u4e0d\u6b63\u5e38\u600e\u4e48\u529e&#xff1f;<\/li>\n<li>\u5982\u679c\u6e29\u5ea6\u4f20\u611f\u5668\u6545\u969c&#xff0c;\u4e00\u76f4\u8bfb\u5230\u9ad8\u6e29\u600e\u4e48\u529e&#xff1f;<\/li>\n<li>\u5982\u679c CHARGE_COMPLETE \u4e8b\u4ef6\u6c38\u8fdc\u4e0d\u6765\u600e\u4e48\u529e&#xff1f;<\/li>\n<\/ul>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4e3a CHARGING \u72b6\u6001\u6dfb\u52a0\u4e00\u4e2a\u8d85\u65f6\u5b9a\u65f6\u5668\u3002<\/p>\n<p>\/\/ \u5728\u5145\u7535\u7ba1\u7406\u6a21\u5757\u4e2d<br \/>\n#define CHARGING_TIMEOUT_MS (3 * 60 * 60 * 1000) \/\/ 3\u5c0f\u65f6\u8d85\u65f6<\/p>\n<p>static uint32_t g_charging_start_time &#061; 0;<\/p>\n<p>\/\/ \u5728\u72b6\u6001\u673a\u4e2d<br \/>\nvoid charge_fsm_process_event(ChargeEvent_t event) {<br \/>\n    \/\/ &#8230;<br \/>\n    switch (g_charge_state) {<br \/>\n        case CHG_STATE_CHARGING:<br \/>\n            \/\/ \u68c0\u67e5\u8d85\u65f6<br \/>\n            if (get_time_since(g_charging_start_time) &gt; CHARGING_TIMEOUT_MS) {<br \/>\n                printf(&#034;  Timeout in CHARGING state!\\\\n&#034;);<br \/>\n                \/\/ \u4ea7\u751f\u4e00\u4e2a\u8d85\u65f6\u4e8b\u4ef6<br \/>\n                charge_fsm_process_event(EV_CHARGE_TIMEOUT); \/\/ \u9012\u5f52\u8c03\u7528\u6216\u8bbe\u7f6e\u6807\u5fd7<br \/>\n                \/\/ \u6216\u8005\u76f4\u63a5\u5904\u7406<br \/>\n                \/\/ action_stop_charging();<br \/>\n                \/\/ g_charge_state &#061; CHG_STATE_ERROR;<br \/>\n                break;<br \/>\n            }<br \/>\n            \/\/ &#8230; \u5176\u4ed6\u4e8b\u4ef6\u5904\u7406 &#8230;<br \/>\n            if (event &#061;&#061; EV_PLUG_IN) { \/\/ \u5047\u8bbe\u8fd9\u662f\u8fdb\u5165CHARGING\u7684\u4e8b\u4ef6<br \/>\n                 g_charging_start_time &#061; get_current_time_ms();<br \/>\n            }<br \/>\n            break;<br \/>\n        \/\/ &#8230;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u662f&#xff0c;\u5728\u4e3b\u5faa\u73af\u4e2d\u7528\u4e00\u4e2a\u5b9a\u65f6\u5668\u5468\u671f\u6027\u5730\u5411\u72b6\u6001\u673a\u53d1\u9001 EV_TIMER_TICK \u4e8b\u4ef6&#xff0c;\u72b6\u6001\u673a\u5185\u90e8\u81ea\u5df1\u8ba1\u65f6\u3002<\/p>\n<h4>5.5 \u8c03\u8bd5\u4e0e\u6d4b\u8bd5&#xff1a;\u8ba9\u72b6\u6001\u673a\u201c\u8bf4\u8bdd\u201d<\/h4>\n<p>FSM\u7684\u8c03\u8bd5\u65e5\u5fd7\u662f\u5176\u6700\u5927\u7684\u4f18\u52bf\u4e4b\u4e00\u3002\u901a\u8fc7\u4e0a\u9762\u4ee3\u7801\u4e2d\u52a0\u5165\u7684 printf&#xff0c;\u4f60\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u7cfb\u7edf\u5728\u4efb\u4f55\u65f6\u523b\u7684\u72b6\u6001\u6d41\u8f6c\u3002<\/p>\n<p>\u6d4b\u8bd5\u7528\u4f8b\u8bbe\u8ba1&#xff1a;<\/p>\n<li>\u6b63\u5e38\u6d41\u7a0b&#xff1a;\u63d2\u5165 -&gt; \u5145\u7535 -&gt; \u5145\u6ee1 -&gt; \u62d4\u51fa\u3002<\/li>\n<li>\u4e2d\u9014\u62d4\u51fa&#xff1a;\u63d2\u5165 -&gt; \u5145\u7535 -&gt; \u62d4\u51fa\u3002<\/li>\n<li>\u5f02\u5e38\u6d41\u7a0b&#xff1a;\u63d2\u5165 -&gt; \u5145\u7535 -&gt; \u6a21\u62df\u9ad8\u6e29 -&gt; \u9519\u8bef -&gt; \u62d4\u51fa\u3002<\/li>\n<li>\u8fb9\u754c\u6761\u4ef6&#xff1a;\u5728 IDLE \u72b6\u6001\u4e0b\u62d4\u51fa\u5145\u7535\u5668&#xff08;\u5e94\u65e0\u53cd\u5e94&#xff09;\u3002\u5728 ERROR \u72b6\u6001\u4e0b\u63d2\u5165\u5145\u7535\u5668&#xff08;\u5e94\u65e0\u53cd\u5e94\u6216\u6709\u7279\u5b9a\u903b\u8f91&#xff09;\u3002<\/li>\n<li>\u5e76\u53d1\u6d4b\u8bd5&#xff1a;\u5728\u5145\u7535\u8fc7\u7a0b\u4e2d&#xff0c;\u5feb\u901f\u63d2\u62d4\u5145\u7535\u5668&#xff0c;\u6a21\u62df\u6296\u52a8&#xff0c;\u770b\u72b6\u6001\u673a\u662f\u5426\u80fd\u6b63\u786e\u5904\u7406\u3002<\/li>\n<hr \/>\n<h3>\u7b2c\u516d\u7ae0&#xff1a;\u5b9e\u6218\u91cd\u6784\u2014\u2014\u7528FSM\u89e3\u6790\u901a\u4fe1\u534f\u8bae<\/h3>\n<p>\u8fd9\u662fFSM\u7684\u53e6\u4e00\u4e2a\u7ecf\u5178\u5e94\u7528\u573a\u666f&#xff0c;\u5c24\u5176\u9002\u5408\u5904\u7406\u6d41\u5f0f\u3001\u6709\u56fa\u5b9a\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<h4>6.1 \u534f\u8bae\u5206\u6790&#xff1a;\u5e27\u7ed3\u6784\u4e0e\u89e3\u6790\u96be\u70b9<\/h4>\n<p>\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u6790\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u534f\u8bae\u5e27&#xff1a; [\u5e27\u5934 (1 byte: 0xAA)] [\u957f\u5ea6 (1 byte: N)] [\u6570\u636e (N bytes)] [\u6821\u9a8c\u548c (1 byte: XOR)]<\/p>\n<p>\u96be\u70b9&#xff1a;<\/p>\n<li>\u6d41\u5f0f\u6570\u636e&#xff1a;\u6570\u636e\u4e00\u4e2a\u5b57\u8282\u4e00\u4e2a\u5b57\u8282\u5730\u4eceUART\u5230\u8fbe&#xff0c;\u4f60\u4e0d\u77e5\u9053\u4e00\u5e27\u4f55\u65f6\u5f00\u59cb&#xff0c;\u4f55\u65f6\u7ed3\u675f\u3002<\/li>\n<li>\u7c98\u5305\/\u534a\u5305&#xff1a;\u53ef\u80fd\u4e00\u6b21\u6536\u5230\u591a\u4e2a\u5e27&#xff0c;\u4e5f\u53ef\u80fd\u4e00\u4e2a\u5e27\u5206\u51e0\u6b21\u624d\u6536\u5b8c\u3002<\/li>\n<li>\u9519\u8bef\u6062\u590d&#xff1a;\u5982\u679c\u6536\u5230\u4e00\u4e2a\u975e\u6cd5\u7684\u5e27\u5934\u6216\u957f\u5ea6&#xff0c;\u5982\u4f55\u5feb\u901f\u56de\u5230\u6b63\u786e\u7684\u89e3\u6790\u72b6\u6001&#xff0c;\u800c\u4e0d\u662f\u6c38\u8fdc\u5361\u6b7b&#xff1f;<\/li>\n<h4>6.2 \u72b6\u6001\u8bbe\u8ba1&#xff1a;\u4e3a\u534f\u8bae\u7684\u6bcf\u4e2a\u9636\u6bb5\u5efa\u7acb\u72b6\u6001<\/h4>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5929\u7136\u7684\u72b6\u6001\u673a\u95ee\u9898\u3002<\/p>\n<ul>\n<li>PROTO_STATE_WAIT_HEADER: \u7b49\u5f85\u5e27\u5934 0xAA\u3002<\/li>\n<li>PROTO_STATE_WAIT_LENGTH: \u6536\u5230\u5e27\u5934\u540e&#xff0c;\u7b49\u5f85\u957f\u5ea6\u5b57\u8282\u3002<\/li>\n<li>PROTO_STATE_WAIT_DATA: \u6536\u5230\u957f\u5ea6\u540e&#xff0c;\u7b49\u5f85\u6570\u636e\u4f53\u3002<\/li>\n<li>PROTO_STATE_WAIT_CHECKSUM: \u6536\u5230\u6240\u6709\u6570\u636e\u540e&#xff0c;\u7b49\u5f85\u6821\u9a8c\u548c\u3002<\/li>\n<\/ul>\n<p>\u4e8b\u4ef6&#xff1a;<\/p>\n<ul>\n<li>EV_BYTE_RECEIVED: \u4eceUART FIFO\u6536\u5230\u4e00\u4e2a\u5b57\u8282\u3002<\/li>\n<li>EV_TIMEOUT: \u5728\u67d0\u4e2a\u72b6\u6001\u7b49\u5f85\u8d85\u65f6\u3002<\/li>\n<\/ul>\n<h4>6.3 \u5b9e\u73b0\u4e00\u4e2a\u5065\u58ee\u7684\u534f\u8bae\u89e3\u6790\u5668<\/h4>\n<h5>6.3.1 \u72b6\u6001\u673a\u4e0e\u73af\u5f62\u961f\u5217\u7684\u5b8c\u7f8e\u914d\u5408<\/h5>\n<p>UART ISR\u5c06\u6536\u5230\u7684\u5b57\u8282\u653e\u5165\u73af\u5f62\u961f\u5217&#xff0c;\u4e3b\u5faa\u73af\u4e2d\u7684\u534f\u8bae\u89e3\u6790\u72b6\u6001\u673a\u4ece\u961f\u5217\u4e2d\u53d6\u5b57\u8282\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\/\/ protocol_fsm.h<br \/>\ntypedef enum {<br \/>\n    PROTO_STATE_WAIT_HEADER,<br \/>\n    PROTO_STATE_WAIT_LENGTH,<br \/>\n    PROTO_STATE_WAIT_DATA,<br \/>\n    PROTO_STATE_WAIT_CHECKSUM,<br \/>\n} ProtocolState_t;<\/p>\n<p>void protocol_fsm_init(void);<br \/>\nvoid protocol_fsm_process_byte(uint8_t byte);<br \/>\nvoid protocol_fsm_process_timeout(void);<\/p>\n<p>\/\/ protocol_fsm.c<br \/>\n#include &#034;protocol_fsm.h&#034;<br \/>\n#include &#034;uart_buffer.h&#034; \/\/ \u5f15\u5165\u6211\u4eec\u4e4b\u524d\u5b9e\u73b0\u7684UART FIFO<\/p>\n<p>static ProtocolState_t g_proto_state &#061; PROTO_STATE_WAIT_HEADER;<br \/>\nstatic uint8_t g_rx_buffer[256];<br \/>\nstatic uint8_t g_data_len &#061; 0;<br \/>\nstatic uint8_t g_data_idx &#061; 0;<br \/>\nstatic uint8_t g_checksum &#061; 0;<\/p>\n<p>#define PROTOCOL_HEADER 0xAA<br \/>\n#define MAX_FRAME_SIZE 256<\/p>\n<p>void protocol_fsm_init(void) {<br \/>\n    g_proto_state &#061; PROTO_STATE_WAIT_HEADER;<br \/>\n    g_data_len &#061; 0;<br \/>\n    g_data_idx &#061; 0;<br \/>\n    g_checksum &#061; 0;<br \/>\n}<\/p>\n<p>\/\/ \u8fd9\u4e2a\u51fd\u6570\u7531\u4e3b\u5faa\u73af\u8c03\u7528&#xff0c;\u6216\u8005\u7531\u4e00\u4e2a\u5b9a\u65f6\u5668\u4efb\u52a1\u8c03\u7528\u6765\u5904\u7406\u8d85\u65f6<br \/>\nvoid protocol_fsm_process_timeout(void) {<br \/>\n    if (g_proto_state !&#061; PROTO_STATE_WAIT_HEADER) {<br \/>\n        printf(&#034;Protocol FSM Timeout! Resetting to WAIT_HEADER.\\\\n&#034;);<br \/>\n        \/\/ \u8d85\u65f6\u610f\u5473\u7740\u5e27\u4e0d\u5b8c\u6574\u6216\u9519\u8bef&#xff0c;\u5fc5\u987b\u590d\u4f4d<br \/>\n        protocol_fsm_init();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u8fd9\u4e2a\u51fd\u6570\u5904\u7406\u5355\u4e2a\u5b57\u8282<br \/>\nvoid protocol_fsm_process_byte(uint8_t byte) {<br \/>\n    switch (g_proto_state) {<br \/>\n        case PROTO_STATE_WAIT_HEADER:<br \/>\n            if (byte &#061;&#061; PROTOCOL_HEADER) {<br \/>\n                g_proto_state &#061; PROTO_STATE_WAIT_LENGTH;<br \/>\n                g_checksum &#061; byte; \/\/ \u5e27\u5934\u4e5f\u53c2\u4e0e\u6821\u9a8c<br \/>\n            }<br \/>\n            \/\/ \u5426\u5219&#xff0c;\u5ffd\u7565\u5b57\u8282&#xff0c;\u7ee7\u7eed\u7b49\u5f85<br \/>\n            break;<\/p>\n<p>        case PROTO_STATE_WAIT_LENGTH:<br \/>\n            g_data_len &#061; byte;<br \/>\n            g_data_idx &#061; 0;<br \/>\n            g_checksum &#061; byte; \/\/ \u957f\u5ea6\u4e5f\u53c2\u4e0e\u6821\u9a8c<\/p>\n<p>            if (g_data_len &gt; 0 &amp;&amp; g_data_len &lt;&#061; MAX_FRAME_SIZE) {<br \/>\n                g_proto_state &#061; PROTO_STATE_WAIT_DATA;<br \/>\n            } else {<br \/>\n                \/\/ \u957f\u5ea6\u975e\u6cd5&#xff08;0\u6216\u8fc7\u5927&#xff09;&#xff0c;\u590d\u4f4d\u72b6\u6001\u673a<br \/>\n                printf(&#034;Protocol Error: Invalid length %d\\\\n&#034;, g_data_len);<br \/>\n                protocol_fsm_init();<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case PROTO_STATE_WAIT_DATA:<br \/>\n            g_rx_buffer[g_data_idx&#043;&#043;] &#061; byte;<br \/>\n            g_checksum &#061; byte; \/\/ \u6570\u636e\u53c2\u4e0e\u6821\u9a8c<\/p>\n<p>            if (g_data_idx &gt;&#061; g_data_len) {<br \/>\n                \/\/ \u6570\u636e\u63a5\u6536\u5b8c\u6bd5&#xff0c;\u7b49\u5f85\u6821\u9a8c\u548c<br \/>\n                g_proto_state &#061; PROTO_STATE_WAIT_CHECKSUM;<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case PROTO_STATE_WAIT_CHECKSUM:<br \/>\n            if (byte &#061;&#061; g_checksum) {<br \/>\n                \/\/ \u6821\u9a8c\u6210\u529f&#xff01;<br \/>\n                printf(&#034;Frame received successfully! Len: %d\\\\n&#034;, g_data_len);<br \/>\n                \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u4e00\u5e27\u5b8c\u6574\u7684\u6570\u636e<br \/>\n                process_full_frame(g_rx_buffer, g_data_len);<br \/>\n            } else {<br \/>\n                \/\/ \u6821\u9a8c\u5931\u8d25<br \/>\n                printf(&#034;Protocol Error: Checksum mismatch! Expected 0x%02X, got 0x%02X\\\\n&#034;, g_checksum, byte);<br \/>\n            }<br \/>\n            \/\/ \u65e0\u8bba\u6210\u529f\u4e0e\u5426&#xff0c;\u90fd\u56de\u5230\u521d\u59cb\u72b6\u6001&#xff0c;\u51c6\u5907\u63a5\u6536\u4e0b\u4e00\u5e27<br \/>\n            protocol_fsm_init();<br \/>\n            break;<\/p>\n<p>        default:<br \/>\n            \/\/ \u975e\u6cd5\u72b6\u6001&#xff0c;\u590d\u4f4d<br \/>\n            protocol_fsm_init();<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u4e3b\u5faa\u73af\u4e2d\u7684\u8c03\u7528\u65b9\u5f0f<br \/>\nvoid main_loop() {<br \/>\n    uint8_t byte;<br \/>\n    while (uart_rx_fifo_get(&amp;g_uart_rx_fifo, &amp;byte)) {<br \/>\n        protocol_fsm_process_byte(byte);<br \/>\n    }<br \/>\n    \/\/ \u53ef\u4ee5\u7528\u4e00\u4e2a\u5b9a\u65f6\u5668\u5468\u671f\u6027\u8c03\u7528 protocol_fsm_process_timeout()<br \/>\n}<\/p>\n<h5>6.3.2 \u8d85\u65f6\u5904\u7406&#xff1a;\u9632\u6b62\u201c\u5047\u6b7b\u201d\u7684\u5173\u952e<\/h5>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d&#xff0c;protocol_fsm_process_timeout() \u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5982\u679c\u8bbe\u5907\u53ea\u6536\u5230\u4e86 0xAA 0x05&#xff0c;\u7136\u540e\u53d1\u9001\u65b9\u5c31\u6389\u7ebf\u4e86&#xff0c;\u6216\u8005\u6570\u636e\u5728\u4f20\u8f93\u4e2d\u4e22\u5931&#xff0c;\u72b6\u6001\u673a\u5c06\u6c38\u8fdc\u505c\u7559\u5728 WAIT_DATA \u72b6\u6001\u3002\u8d85\u65f6\u673a\u5236\u53ef\u4ee5\u5c06\u5176\u5f3a\u5236\u590d\u4f4d&#xff0c;\u4fdd\u8bc1\u7cfb\u7edf\u80fd\u7ee7\u7eed\u5de5\u4f5c\u3002<\/p>\n<h5>6.3.3 \u9519\u8bef\u6062\u590d&#xff1a;\u4ece\u9519\u8bef\u72b6\u6001\u4e2d\u4f18\u96c5\u5730\u56de\u5f52<\/h5>\n<p>\u5f53\u68c0\u6d4b\u5230\u9519\u8bef&#xff08;\u5982\u975e\u6cd5\u957f\u5ea6\u3001\u6821\u9a8c\u5931\u8d25&#xff09;\u65f6&#xff0c;\u6700\u7b80\u5355\u7684\u6062\u590d\u65b9\u5f0f\u5c31\u662f\u8c03\u7528 protocol_fsm_init() \u590d\u4f4d\u6574\u4e2a\u72b6\u6001\u673a\u3002\u8fd9\u662f\u4e00\u79cd\u201c\u786c\u590d\u4f4d\u201d\u3002<\/p>\n<p>\u66f4\u9ad8\u7ea7\u7684\u6062\u590d\u7b56\u7565\u662f\u201c\u5b57\u7b26\u586b\u5145\u201d\u6216\u201c\u6a21\u5f0f\u5339\u914d\u201d\u3002\u4f8b\u5982&#xff0c;\u5728 WAIT_DATA \u72b6\u6001\u6536\u5230\u4e00\u4e2a\u975e\u6cd5\u5b57\u8282&#xff0c;\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u5f53\u524d\u4f4d\u7f6e\u5f00\u59cb\u5bfb\u627e\u4e0b\u4e00\u4e2a 0xAA \u5e27\u5934&#xff0c;\u800c\u4e0d\u662f\u5b8c\u5168\u4ece\u5934\u5f00\u59cb\u3002\u8fd9\u4f1a\u4f7f\u72b6\u6001\u673a\u53d8\u5f97\u66f4\u590d\u6742&#xff0c;\u4f46\u5bf9\u4e8e\u4e0d\u53ef\u9760\u7684\u4fe1\u9053\u66f4\u6709\u6548\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5d4c\u5165\u5f0f\u5e94\u7528&#xff0c;\u7b80\u5355\u7684\u590d\u4f4d\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<h4>6.4 \u6027\u80fd\u8003\u91cf\u4e0e\u4f18\u5316<\/h4>\n<ul>\n<li>\u907f\u514d\u5728ISR\u4e2d\u505aFSM&#xff1a;ISR\u5e94\u8be5\u53ea\u505a fifo_put&#xff0c;\u5c06FSM\u7684\u8ba1\u7b97\u8d1f\u62c5\u653e\u5230\u4e3b\u5faa\u73af\u6216\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u4e2d\u3002<\/li>\n<li>\u8ba1\u7b97\u5f00\u9500&#xff1a;XOR\u6821\u9a8c\u548c\u7684\u8ba1\u7b97\u5f00\u9500\u5f88\u5c0f\u3002\u5982\u679c\u662fCRC\u6821\u9a8c&#xff0c;\u53ef\u80fd\u4f1a\u6bd4\u8f83\u8017\u65f6\u3002\u5bf9\u4e8e\u975e\u5e38\u9ad8\u901f\u7684\u6570\u636e\u6d41&#xff0c;\u53ef\u80fd\u9700\u8981\u4f18\u5316\u6821\u9a8c\u7b97\u6cd5\u6216\u4f7f\u7528\u786c\u4ef6CRC\u6a21\u5757\u3002<\/li>\n<li>\u7f13\u51b2\u533a\u5927\u5c0f&#xff1a;g_rx_buffer \u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u80fd\u63a5\u6536\u7684\u6700\u5927\u5e27\u957f\u3002\u9700\u8981\u6839\u636e\u534f\u8bae\u89c4\u8303\u6765\u5b9a\u4e49&#xff0c;\u5e76\u505a\u597d\u8fb9\u754c\u68c0\u67e5\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u7b2c\u4e03\u7ae0&#xff1a;\u72b6\u6001\u673a\u4e0eRTOS\u7684\u534f\u594f\u66f2<\/h3>\n<p>\u5f53\u7cfb\u7edf\u53d8\u5f97\u590d\u6742&#xff0c;\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u4efb\u52a1\u65f6&#xff08;\u5982\u7f51\u7edc\u901a\u4fe1\u3001UI\u5237\u65b0\u3001\u6570\u636e\u8bb0\u5f55&#xff09;&#xff0c;RTOS\u5c31\u6210\u4e3a\u4e86\u5fc5\u7136\u9009\u62e9\u3002\u72b6\u6001\u673a\u5728RTOS\u73af\u5883\u4e2d\u80fd\u53d1\u6325\u66f4\u5927\u7684\u5a01\u529b\u3002<\/p>\n<h4>7.1 RTOS \u4efb\u52a1\u4f5c\u4e3a\u72b6\u6001\u673a\u7684\u8f7d\u4f53<\/h4>\n<p>\u5728RTOS\u4e2d&#xff0c;\u4e00\u4e2a\u5e38\u89c1\u7684\u6a21\u5f0f\u662f\u4e3a\u4e00\u4e2a\u590d\u6742\u7684\u8bbe\u5907\u6216\u529f\u80fd\u521b\u5efa\u4e00\u4e2a\u4e13\u5c5e\u7684\u4efb\u52a1&#xff0c;\u8fd9\u4e2a\u4efb\u52a1\u7684\u4e3b\u4f53\u5c31\u662f\u4e00\u4e2a\u72b6\u6001\u673a\u3002<\/p>\n<p>\/\/ FreeRTOS \u793a\u4f8b<br \/>\nvoid device_manager_task(void *pvParameters) {<br \/>\n    DeviceState_t current_state &#061; DEVICE_IDLE;<br \/>\n    Event_t event;<\/p>\n<p>    for (;;) {<br \/>\n        \/\/ \u963b\u585e\u7b49\u5f85\u4e8b\u4ef6&#xff0c;\u800c\u4e0d\u662f\u7a7a\u8017CPU<br \/>\n        \/\/ xQueueReceive\u4f1a\u4f7f\u4efb\u52a1\u8fdb\u5165\u963b\u585e\u6001&#xff0c;\u76f4\u5230\u961f\u5217\u4e2d\u6709\u4e8b\u4ef6<br \/>\n        if (xQueueReceive(g_device_event_queue, &amp;event, portMAX_DELAY)  pdPASS) {<\/p>\n<p>            \/\/ \u6536\u5230\u4e8b\u4ef6&#xff0c;\u9a71\u52a8\u72b6\u6001\u673a<br \/>\n            switch (current_state) {<br \/>\n                case DEVICE_IDLE:<br \/>\n                    \/\/ &#8230;<br \/>\n                    if (event  EV_START) {<br \/>\n                        current_state &#061; DEVICE_RUNNING;<br \/>\n                    }<br \/>\n                    break;<br \/>\n                case DEVICE_RUNNING:<br \/>\n                    \/\/ &#8230;<br \/>\n                    if (event &#061;&#061; EV_STOP) {<br \/>\n                        current_state &#061; DEVICE_IDLE;<br \/>\n                    }<br \/>\n                    break;<br \/>\n                \/\/ &#8230;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>7.2 \u4e8b\u4ef6\u961f\u5217&#xff1a;\u89e3\u8026\u4e8b\u4ef6\u6e90\u4e0e\u72b6\u6001\u673a<\/h4>\n<p>\u8fd9\u662fRTOS\u4e2d\u6700\u6838\u5fc3\u7684\u89e3\u8026\u601d\u60f3\u3002<\/p>\n<ul>\n<li>\u4e8b\u4ef6\u751f\u4ea7\u8005&#xff1a;\u53ef\u4ee5\u662fISR&#xff08;\u901a\u8fc7 xQueueSendFromISR&#xff09;\u3001\u5176\u4ed6\u4efb\u52a1\u3001\u7f51\u7edc\u5305\u63a5\u6536\u4efb\u52a1\u7b49\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u77e5\u9053\u8c01\u4f1a\u5904\u7406\u8fd9\u4e2a\u4e8b\u4ef6&#xff0c;\u53ea\u9700\u8981\u628a\u4e8b\u4ef6\u6254\u8fdb\u961f\u5217\u3002<\/li>\n<li>\u4e8b\u4ef6\u6d88\u8d39\u8005&#xff1a;\u5c31\u662f\u6211\u4eec\u7684\u72b6\u6001\u673a\u4efb\u52a1\u3002\u5b83\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\u4e8b\u4ef6\u4ece\u4f55\u800c\u6765&#xff0c;\u53ea\u9700\u8981\u4ece\u961f\u5217\u91cc\u53d6\u3002<\/li>\n<li>\u4e8b\u4ef6\u961f\u5217&#xff08;Queue&#xff09;&#xff1a;\u4f5c\u4e3a\u4e2d\u95f4\u7684\u7f13\u51b2\u533a&#xff0c;\u5e73\u6ed1\u4e86\u751f\u4ea7\u548c\u6d88\u8d39\u7684\u901f\u7387\u5dee\u5f02&#xff0c;\u5e76\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u901a\u4fe1\u673a\u5236\u3002<\/li>\n<\/ul>\n<p>\u4e8b\u4ef6\u7684\u5b9a\u4e49&#xff1a; \u4e8b\u4ef6\u4e0d\u518d\u662f\u7b80\u5355\u7684\u679a\u4e3e&#xff0c;\u901a\u5e38\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u5305\u542b\u4e8b\u4ef6\u7c7b\u578b\u548c\u76f8\u5173\u6570\u636e\u3002<\/p>\n<p>typedef struct {<br \/>\n    EventType_t type;<br \/>\n    uint32_t timestamp;<br \/>\n    void* data; \/\/ \u6307\u5411\u9644\u52a0\u6570\u636e\u7684\u6307\u9488<br \/>\n} Event_t;<\/p>\n<h4>7.3 \u6d88\u606f\u90ae\u7bb1\u4e0e\u4fe1\u53f7\u91cf&#xff1a;\u8f7b\u91cf\u7ea7\u4e8b\u4ef6\u901a\u77e5<\/h4>\n<ul>\n<li>\u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;&#xff1a;\u9002\u5408\u7528\u4e8e\u201c\u53d1\u751f\u4e86\u67d0\u4ef6\u4e8b\u201d\u7684\u901a\u77e5&#xff0c;\u4e0d\u9700\u8981\u4f20\u9012\u590d\u6742\u6570\u636e\u3002\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u201c\u6570\u636e\u5c31\u7eea\u201d\u7684\u4fe1\u53f7\u91cf\u3002<\/li>\n<li>\u6d88\u606f\u90ae\u7bb1&#xff08;Mailbox\/Direct Task Notification&#xff09;&#xff1a;\u9002\u5408\u4f20\u9012\u4e00\u4e2a\u7b80\u5355\u7684\u503c\u6216\u6307\u9488\u3002<\/li>\n<\/ul>\n<p>\u5b83\u4eec\u6bd4\u4e8b\u4ef6\u961f\u5217\u66f4\u8f7b\u91cf\u3001\u66f4\u5feb&#xff0c;\u4f46\u529f\u80fd\u4e5f\u66f4\u5355\u4e00\u3002<\/p>\n<h4>7.4 \u5b9e\u8df5&#xff1a;\u5728FreeRTOS\u4e2d\u6784\u5efa\u4e8b\u4ef6\u9a71\u52a8\u7684\u72b6\u6001\u673a<\/h4>\n<p>\u8ba9\u6211\u4eec\u628a\u5145\u7535\u7ba1\u7406\u7cfb\u7edf\u6539\u9020\u4e3aFreeRTOS\u7248\u672c\u3002<\/p>\n<p>\/\/ charge_manager_task.c<br \/>\n#include &#034;FreeRTOS.h&#034;<br \/>\n#include &#034;task.h&#034;<br \/>\n#include &#034;queue.h&#034;<br \/>\n#include &#034;charge_fsm.h&#034; \/\/ \u6211\u4eec\u4e4b\u524d\u5b9a\u4e49\u7684FSM\u903b\u8f91<\/p>\n<p>\/\/ \u4e8b\u4ef6\u961f\u5217\u53e5\u67c4<br \/>\nstatic QueueHandle_t g_charge_event_queue;<\/p>\n<p>\/\/ \u4efb\u52a1\u53e5\u67c4<br \/>\nstatic TaskHandle_t g_charge_task_handle;<\/p>\n<p>\/\/ \u516c\u5f00API&#xff1a;\u5176\u4ed6\u4efb\u52a1\u6216ISR\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\u53d1\u9001\u4e8b\u4ef6<br \/>\nvoid charge_manager_send_event(ChargeEvent_t event) {<br \/>\n    if (g_charge_event_queue) {<br \/>\n        \/\/ \u53d1\u9001\u5230\u961f\u5217\u672b\u5c3e&#xff0c;\u4e0d\u963b\u585e<br \/>\n        xQueueSend(g_charge_event_queue, &amp;event, 0);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5145\u7535\u7ba1\u7406\u4efb\u52a1<br \/>\nstatic void charge_manager_task(void *pvParameters) {<br \/>\n    ChargeEvent_t event;<\/p>\n<p>    \/\/ \u521d\u59cb\u5316FSM<br \/>\n    charge_fsm_init();<\/p>\n<p>    for (;;) {<br \/>\n        \/\/ \u963b\u585e\u7b49\u5f85\u4e8b\u4ef6<br \/>\n        if (xQueueReceive(g_charge_event_queue, &amp;event, portMAX_DELAY)  pdPASS) {<br \/>\n            \/\/ \u9a71\u52a8FSM<br \/>\n            charge_fsm_process_event(event);<br \/>\n        }<\/p>\n<p>        \/\/ \u4e5f\u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u5468\u671f\u6027\u4efb\u52a1&#xff0c;\u6bd4\u5982\u68c0\u67e5\u8d85\u65f6<br \/>\n        \/\/ if (xQueueReceive(g_charge_event_queue, &amp;event, 1000)  pdPASS) { &#8230; }<br \/>\n        \/\/ 1000ms\u8d85\u65f6\u540e&#xff0c;event\u7684\u503c\u662f\u672a\u5b9a\u4e49\u7684&#xff0c;\u53ef\u4ee5\u7528\u6765\u505a\u5b9a\u65f6\u5668<br \/>\n    }<br \/>\n}<\/p>\n<p>void charge_manager_start(void) {<br \/>\n    \/\/ \u521b\u5efa\u4e8b\u4ef6\u961f\u5217<br \/>\n    g_charge_event_queue &#061; xQueueCreate(16, sizeof(ChargeEvent_t));<br \/>\n    if (g_charge_event_queue &#061;&#061; NULL) {<br \/>\n        \/\/ \u5904\u7406\u9519\u8bef<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ \u521b\u5efa\u4efb\u52a1<br \/>\n    xTaskCreate(charge_manager_task, &#034;ChargeMgr&#034;, 1024, NULL, 5, &amp;g_charge_task_handle);<br \/>\n}<\/p>\n<p>\/\/ \u5728\u67d0\u4e2a\u786c\u4ef6\u68c0\u6d4b\u4efb\u52a1\u6216ISR\u4e2d<br \/>\nvoid hardware_detection_isr(void) {<br \/>\n    BaseType_t xHigherPriorityTaskWoken &#061; pdFALSE;<\/p>\n<p>    if (is_charger_plugged()) {<br \/>\n        ChargeEvent_t event &#061; EV_PLUG_IN;<br \/>\n        \/\/ \u4eceISR\u5b89\u5168\u5730\u53d1\u9001\u4e8b\u4ef6<br \/>\n        xQueueSendFromISR(g_charge_event_queue, &amp;event, &amp;xHigherPriorityTaskWoken);<br \/>\n    } else {<br \/>\n        ChargeEvent_t event &#061; EV_PLUG_OUT;<br \/>\n        xQueueSendFromISR(g_charge_event_queue, &amp;event, &amp;xHigherPriorityTaskWoken);<br \/>\n    }<\/p>\n<p>    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u67b6\u6784\u975e\u5e38\u6e05\u6670\u3001\u53ef\u6269\u5c55\u3002\u6dfb\u52a0\u65b0\u7684\u4e8b\u4ef6\u6e90&#xff08;\u5982\u4e00\u4e2a\u7f51\u7edc\u547d\u4ee4&#xff09;\u53ea\u9700\u8981\u8c03\u7528 charge_manager_send_event() \u5373\u53ef&#xff0c;\u5b8c\u5168\u4e0d\u9700\u8981\u4fee\u6539\u5145\u7535\u7ba1\u7406\u4efb\u52a1\u7684\u4ee3\u7801\u3002<\/p>\n<h4>7.5 tick \u94a9\u5b50\u51fd\u6570\u4e2d\u7684\u72b6\u6001\u673a&#xff1a;\u65f6\u95f4\u9a71\u52a8\u7684\u903b\u8f91<\/h4>\n<p>FreeRTOS\u63d0\u4f9b\u4e86\u4e00\u4e2a vApplicationTickHook() \u51fd\u6570&#xff0c;\u5b83\u4f1a\u5728\u6bcf\u4e2aRTOS tick\u4e2d\u65ad\u4e2d\u88ab\u8c03\u7528&#xff08;\u5982\u679c\u914d\u7f6e\u4e86&#xff09;\u3002\u8fd9\u5bf9\u4e8e\u5b9e\u73b0\u65f6\u95f4\u9a71\u52a8\u7684\u72b6\u6001\u673a\u903b\u8f91\u975e\u5e38\u6709\u7528&#xff0c;\u6bd4\u5982\u8d85\u65f6\u68c0\u6d4b\u3001\u5468\u671f\u6027\u72b6\u6001\u68c0\u67e5\u7b49\u3002<\/p>\n<p>void vApplicationTickHook(void) {<br \/>\n    \/\/ \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5168\u5c40\u7684\u72b6\u6001\u673a\u5b9e\u4f8b<br \/>\n    \/\/ static StateMachine_t g_my_sm;<\/p>\n<p>    \/\/ \u6bcf\u4e2atick\u90fd\u7ed9\u72b6\u6001\u673a\u53d1\u9001\u4e00\u4e2a\u5b9a\u65f6\u5668\u4e8b\u4ef6<br \/>\n    Event_t tick_event &#061; { .type &#061; EVENT_TIMER_TICK };<br \/>\n    state_machine_process_event(&amp;g_my_sm, tick_event);<br \/>\n}<\/p>\n<hr \/>\n<h3>\u7b2c\u516b\u7ae0&#xff1a;\u9ad8\u7ea7\u4e3b\u9898\u4e0e\u67b6\u6784\u6a21\u5f0f<\/h3>\n<h4>8.1 \u961f\u5217\u72b6\u6001\u673a&#xff08;QSM&#xff09;&#xff1a;\u5c06\u4e8b\u4ef6\u961f\u5217\u5185\u5d4c\u4e8e\u72b6\u6001\u673a<\/h4>\n<p>\u5728\u67d0\u4e9b\u9ad8\u6027\u80fd\u6216\u7279\u5b9a\u67b6\u6784\u4e2d&#xff0c;\u72b6\u6001\u673a\u4e0d\u4ece\u5916\u90e8\u961f\u5217\u83b7\u53d6\u4e8b\u4ef6&#xff0c;\u800c\u662f\u81ea\u5df1\u7ba1\u7406\u4e00\u4e2a\u5185\u90e8\u4e8b\u4ef6\u961f\u5217\u3002\u8fd9\u88ab\u79f0\u4e3a\u961f\u5217\u72b6\u6001\u673a&#xff08;Queue State Machine&#xff09;\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0b&#xff0c;\u72b6\u6001\u673a\u7684 run \u51fd\u6570\u4f1a\u5148\u68c0\u67e5\u81ea\u5df1\u7684\u5185\u90e8\u961f\u5217&#xff0c;\u5982\u679c\u6709\u4e8b\u4ef6\u5c31\u5904\u7406&#xff0c;\u6ca1\u6709\u5c31\u8fd4\u56de\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u4efb\u52a1\u5207\u6362\u7684\u5f00\u9500\u3002<\/p>\n<h4>8.2 \u6d3b\u8dc3\u5bf9\u8c61&#xff08;Active Object&#xff09;\u6a21\u5f0f&#xff1a;\u5e76\u53d1\u72b6\u6001\u673a\u7684\u5c01\u88c5<\/h4>\n<p>\u6d3b\u8dc3\u5bf9\u8c61\u662f\u4e00\u79cd\u5c06\u72b6\u6001\u673a\u3001\u4e8b\u4ef6\u961f\u5217\u548c\u6267\u884c\u7ebf\u7a0b&#xff08;\u4efb\u52a1&#xff09;\u5c01\u88c5\u5728\u4e00\u8d77\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b83\u662f\u9762\u5411\u5bf9\u8c61\u601d\u60f3\u5728\u5d4c\u5165\u5f0fC\u4e2d\u7684\u4f53\u73b0\u3002<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u6d3b\u8dc3\u5bf9\u8c61\u90fd\u6709\u81ea\u5df1\u7684\u79c1\u6709\u4e8b\u4ef6\u961f\u5217\u3002<\/li>\n<li>\u5916\u90e8\u901a\u8fc7\u4e00\u4e2a\u516c\u5171\u7684\u63a5\u53e3\u51fd\u6570\u5411\u5176\u53d1\u9001\u4e8b\u4ef6&#xff08;\u5f02\u6b65\u8c03\u7528&#xff09;\u3002<\/li>\n<li>\u5bf9\u8c61\u5185\u90e8\u7684\u4efb\u52a1\u5faa\u73af\u4ece\u81ea\u5df1\u7684\u961f\u5217\u4e2d\u53d6\u4e8b\u4ef6&#xff0c;\u5e76\u9a71\u52a8\u81ea\u5df1\u7684\u72b6\u6001\u673a\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u5b9e\u73b0\u4e86\u201c\u57fa\u4e8e\u4e8b\u4ef6\u7684\u5e76\u53d1\u201d&#xff0c;\u907f\u514d\u4e86\u4f20\u7edf\u7684\u5171\u4eab\u5185\u5b58\u548c\u9501\u5e26\u6765\u7684\u590d\u6742\u6027\u3002QP\/C\u6846\u67b6\u5c31\u662f\u6d3b\u8dc3\u5bf9\u8c61\u6a21\u5f0f\u7684\u7ecf\u5178\u5b9e\u73b0\u3002<\/p>\n<h4>8.3 \u72b6\u6001\u673a\u4e0e\u786c\u4ef6\u62bd\u8c61\u5c42&#xff08;HAL&#xff09;\u7684\u7ed3\u5408<\/h4>\n<p>\u826f\u597d\u7684\u8bbe\u8ba1\u5e94\u8be5\u5c06\u72b6\u6001\u673a\u4e0e\u5177\u4f53\u7684\u786c\u4ef6\u64cd\u4f5c\u89e3\u8026\u3002\u72b6\u6001\u673a\u53d1\u51fa\u7684\u52a8\u4f5c\u547d\u4ee4&#xff08;\u5982 start_charging()&#xff09;\u4e0d\u5e94\u8be5\u76f4\u63a5\u64cd\u4f5c\u5bc4\u5b58\u5668&#xff0c;\u800c\u5e94\u8be5\u8c03\u7528HAL\u5c42\u7684\u51fd\u6570\u3002<\/p>\n<p>&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;       &#043;&#8212;&#8212;&#8212;&#8212;&#8212;-&#043;       &#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;<br \/>\n|  Charge FSM     |&#8212;&#8212;&gt;|  Charge Driver |&#8212;&#8212;&gt;|  MCU Hardware   |<br \/>\n| (Logic)         |       |  (HAL)         |       |  (GPIO, ADC, PWM)|<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;       &#043;&#8212;&#8212;&#8212;&#8212;&#8212;-&#043;       &#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;<\/p>\n<p>\u8fd9\u6837&#xff0c;\u5f53\u66f4\u6362MCU\u578b\u53f7\u65f6&#xff0c;\u53ea\u9700\u8981\u91cd\u5199HAL\u5c42&#xff0c;\u72b6\u6001\u673a\u903b\u8f91\u5b8c\u5168\u4e0d\u7528\u53d8\u3002<\/p>\n<h4>8.4 \u4ee3\u7801\u751f\u6210\u5de5\u5177&#xff1a;\u4eceUML\u56fe\u5230C\u4ee3\u7801\u7684\u81ea\u52a8\u5316<\/h4>\n<p>\u5bf9\u4e8e\u6781\u5176\u590d\u6742\u7684\u72b6\u6001\u673a&#xff0c;\u624b\u52a8\u7f16\u5199\u548c\u7ef4\u62a4 switch-case \u6216\u72b6\u6001\u8868\u662f\u7e41\u7410\u4e14\u5bb9\u6613\u51fa\u9519\u7684\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7801\u751f\u6210\u5de5\u5177\u3002<\/p>\n<ul>\n<li>\u5de5\u5177&#xff1a;Enterprise Architect, Visual Paradigm, Yakindu Statechart Tools\u7b49\u3002<\/li>\n<li>\u6d41\u7a0b&#xff1a;\u5728\u5de5\u5177\u4e2d\u7ed8\u5236UML\u72b6\u6001\u56fe -&gt; \u5b9a\u4e49\u72b6\u6001\u3001\u4e8b\u4ef6\u3001\u52a8\u4f5c -&gt; \u5de5\u5177\u751f\u6210C\/C&#043;&#043;\u4ee3\u7801\u6846\u67b6\u3002<\/li>\n<li>\u4f18\u52bf&#xff1a;\u4fdd\u8bc1\u6a21\u578b\u4e0e\u4ee3\u7801\u7684\u4e00\u81f4\u6027&#xff0c;\u81ea\u52a8\u751f\u6210\u7e41\u7410\u7684 switch-case \u6216\u72b6\u6001\u8868&#xff0c;\u51cf\u5c11\u4eba\u4e3a\u9519\u8bef\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u7b2c\u4e5d\u7ae0&#xff1a;\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<h4>9.1 \u6838\u5fc3\u601d\u60f3\u56de\u987e<\/h4>\n<p>\u6211\u4eec\u4ece\u4e00\u4e2a\u5145\u6ee1 if-else \u5d4c\u5957\u7684\u201c\u903b\u8f91\u5730\u72f1\u201d\u51fa\u53d1&#xff0c;\u901a\u8fc7\u5f15\u5165\u4e2d\u65ad\u3001\u73af\u5f62\u961f\u5217\u548c\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09; \u8fd9\u4e09\u5927\u652f\u67f1&#xff0c;\u6784\u5efa\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u3001\u5065\u58ee\u7684\u3001\u53ef\u6269\u5c55\u7684\u5d4c\u5165\u5f0f\u8f6f\u4ef6\u67b6\u6784\u3002<\/p>\n<ul>\n<li>\u4e2d\u65ad\u8ba9\u6211\u4eec\u80fd\u53ca\u65f6\u54cd\u5e94\u5916\u90e8\u4e16\u754c\u7684\u5f02\u6b65\u4e8b\u4ef6&#xff0c;\u4f46\u4e5f\u5e26\u6765\u4e86\u5e76\u53d1\u7684\u6311\u6218\u3002<\/li>\n<li>\u73af\u5f62\u961f\u5217\u662f\u89e3\u51b3\u4e2d\u65ad\u4e0e\u4e3b\u5faa\u73af\u3001\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005\u4e4b\u95f4\u5b89\u5168\u3001\u9ad8\u6548\u6570\u636e\u4f20\u9012\u7684\u5229\u5668\u3002<\/li>\n<li>\u6709\u9650\u72b6\u6001\u673a\u5219\u662f\u9a6f\u670d\u590d\u6742\u903b\u8f91\u3001\u5c06\u9690\u6027\u72b6\u6001\u663e\u6027\u5316\u3001\u4f7f\u4ee3\u7801\u7ed3\u6784\u5316\u3001\u53ef\u6d4b\u8bd5\u3001\u53ef\u7ef4\u62a4\u7684\u7ec8\u6781\u601d\u60f3\u6b66\u5668\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e09\u8005\u4e0d\u662f\u5b64\u7acb\u7684&#xff0c;\u800c\u662f\u76f8\u8f85\u76f8\u6210\u7684&#xff1a;\u4e2d\u65ad\u8d1f\u8d23\u6355\u83b7\u4e8b\u4ef6\u5e76\u5c06\u5176\u653e\u5165\u961f\u5217&#xff0c;\u72b6\u6001\u673a\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e8b\u4ef6\u5e76\u9a71\u52a8\u7cfb\u7edf\u72b6\u6001\u7684\u6d41\u8f6c&#xff0c;\u4ece\u800c\u6267\u884c\u76f8\u5e94\u7684\u52a8\u4f5c\u3002\u8fd9\u662f\u4e00\u79cd\u4f18\u96c5\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u3002<\/p>\n<h4>9.2 \u4ece\u201c\u7801\u519c\u201d\u5230\u201c\u5de5\u7a0b\u5e08\u201d\u7684\u601d\u7ef4\u8f6c\u53d8<\/h4>\n<p>\u638c\u63e1FSM\u4e0d\u4ec5\u4ec5\u662f\u5b66\u4f1a\u4e86\u4e00\u79cd\u7f16\u7a0b\u6280\u5de7&#xff0c;\u66f4\u662f\u4e00\u6b21\u601d\u7ef4\u65b9\u5f0f\u7684\u5347\u7ea7&#xff1a;<\/p>\n<ul>\n<li>\u4ece\u7ebf\u6027\u601d\u7ef4\u5230\u72b6\u6001\u601d\u7ef4&#xff1a;\u4e0d\u518d\u7528\u201c\u5982\u679cA\u5219B\u201d\u7684\u7ebf\u6027\u6d41\u7a0b\u53bb\u601d\u8003&#xff0c;\u800c\u662f\u7528\u201c\u5728\u72b6\u6001S\u4e0b&#xff0c;\u53d1\u751f\u4e8b\u4ef6E&#xff0c;\u5219\u8f6c\u6362\u5230\u72b6\u6001S&#039;\u201d\u7684\u6a21\u578b\u53bb\u5ba1\u89c6\u7cfb\u7edf\u3002<\/li>\n<li>\u4ece\u88ab\u52a8\u54cd\u5e94\u5230\u4e3b\u52a8\u8bbe\u8ba1&#xff1a;\u5728\u5199\u7b2c\u4e00\u884c\u4ee3\u7801\u524d&#xff0c;\u5148\u7528\u72b6\u6001\u8f6c\u6362\u56fe&#xff08;STD&#xff09;\u5b8c\u6574\u5730\u8bbe\u8ba1\u7cfb\u7edf\u7684\u884c\u4e3a&#xff0c;\u8ba9\u903b\u8f91\u7684\u6f0f\u6d1e\u5728\u56fe\u7eb8\u9636\u6bb5\u5c31\u66b4\u9732\u51fa\u6765\u3002<\/li>\n<li>\u4ece\u5173\u6ce8\u5b9e\u73b0\u5230\u5173\u6ce8\u67b6\u6784&#xff1a;\u5f00\u59cb\u601d\u8003\u7cfb\u7edf\u7684\u53ef\u6269\u5c55\u6027\u3001\u53ef\u6d4b\u8bd5\u6027\u3001\u9c81\u68d2\u6027&#xff0c;\u800c\u4e0d\u4ec5\u4ec5\u662f\u529f\u80fd\u7684\u5b9e\u73b0\u3002<\/li>\n<li>\u62e5\u62b1\u590d\u6742\u6027&#xff1a;\u9762\u5bf9\u590d\u6742\u9700\u6c42\u4e0d\u518d\u6050\u60e7&#xff0c;\u56e0\u4e3a\u4f60\u77e5\u9053\u5982\u4f55\u7528\u5206\u5c42\u3001\u89e3\u8026\u3001\u72b6\u6001\u673a\u7b49\u5de5\u5177\u5c06\u5176\u5206\u89e3\u548c\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<h4>9.3 \u6301\u7eed\u5b66\u4e60\u7684\u8def\u5f84<\/h4>\n<p>\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u4e16\u754c\u5e7f\u9614\u800c\u6df1\u9083\u3002\u5728\u638c\u63e1\u4e86FSM\u4e4b\u540e&#xff0c;\u4f60\u53ef\u4ee5\u7ee7\u7eed\u63a2\u7d22&#xff1a;<\/p>\n<ul>\n<li>\u5b9e\u65f6\u64cd\u4f5c\u7cfb\u7edf&#xff08;RTOS&#xff09;\u7684\u9ad8\u7ea7\u7279\u6027&#xff1a;\u5982\u5185\u5b58\u7ba1\u7406\u3001\u4f18\u5148\u7ea7\u7ee7\u627f\u3001\u6b7b\u9501\u68c0\u6d4b\u7b49\u3002<\/li>\n<li>\u8f6f\u4ef6\u67b6\u6784\u6a21\u5f0f&#xff1a;\u5982\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u3001\u89c2\u5bdf\u8005\u6a21\u5f0f\u3001\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668&#xff08;MVC&#xff09;\u5728\u5d4c\u5165\u5f0fUI\u4e2d\u7684\u5e94\u7528\u3002<\/li>\n<li>\u5f62\u5f0f\u5316\u9a8c\u8bc1&#xff1a;\u4f7f\u7528\u6570\u5b66\u65b9\u6cd5\u8bc1\u660e\u72b6\u6001\u673a\u7684\u67d0\u4e9b\u5c5e\u6027&#xff08;\u5982\u65e0\u6b7b\u9501&#xff09;\u3002<\/li>\n<li>\u9886\u57df\u7279\u5b9a\u8bed\u8a00&#xff08;DSL&#xff09;&#xff1a;\u4e3a\u72b6\u6001\u673a\u8bbe\u8ba1\u4e13\u95e8\u7684\u63cf\u8ff0\u8bed\u8a00&#xff0c;\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/li>\n<\/ul>\n<p>\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\u4ee3\u7801&#xff0c;\u62e5\u62b1\u72b6\u6001\u673a&#xff0c;\u4f60\u7684\u5d4c\u5165\u5f0f\u7f16\u7a0b\u4e4b\u8def\u5c06\u4ece\u6b64\u53d8\u5f97\u6e05\u6670\u3001\u4f18\u96c5\u800c\u5145\u6ee1\u521b\u9020\u7684\u4e50\u8da3\u3002\u8fd9\u7bc7\u8d85\u8fc750000\u5b57\u7684\u6307\u5357&#xff0c;\u5e0c\u671b\u80fd\u6210\u4e3a\u4f60\u5728\u8fd9\u6761\u9053\u8def\u4e0a\u7684\u575a\u5b9e\u57fa\u77f3\u548c\u5f97\u529b\u52a9\u624b\u3002\u795d\u4f60\u7f16\u7801\u6109\u5feb&#xff01;<\/p>\n<hr \/>\n<h3>\u9644\u5f55<\/h3>\n<h4>A. \u5e38\u89c1\u72b6\u6001\u673a\u5e93\u63a8\u8350<\/h4>\n<li>QP\/C \/ QP\/C&#043;&#043;: \u6700\u8457\u540d\u7684\u6d3b\u8dc3\u5bf9\u8c61\u6846\u67b6&#xff0c;\u4e13\u4e3a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u8bbe\u8ba1&#xff0c;\u652f\u6301UML\u72b6\u6001\u56fe&#xff0c;\u975e\u5e38\u5f3a\u5927\u3002<\/li>\n<li>SCXML-C: \u4e00\u4e2a\u57fa\u4e8eW3C SCXML\u6807\u51c6\u7684C\u8bed\u8a00\u89e3\u91ca\u5668&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7d\u548c\u6267\u884cSCXML\u6587\u4ef6\u5b9a\u4e49\u7684\u72b6\u6001\u673a\u3002<\/li>\n<li>libfsm: \u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684C\u8bed\u8a00FSM\u5e93&#xff0c;\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u6267\u884cFSM\u7684API\u3002<\/li>\n<li>Boost.Statechart (C&#043;&#043;): \u5982\u679c\u4f60\u4f7f\u7528C&#043;&#043;&#xff0c;Boost\u5e93\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u72b6\u6001\u673a\u529f\u80fd\u3002<\/li>\n<h4>B. \u73af\u5f62\u961f\u5217\u5b9e\u73b0\u4ee3\u7801\u6a21\u677f&#xff08;\u591a\u7248\u672c&#xff09;<\/h4>\n<p>\u7248\u672c1&#xff1a;\u88f8\u673a&#xff0c;\u5355\u751f\u4ea7\u8005\u5355\u6d88\u8d39\u8005&#xff0c;\u65e0\u9501 &#xff08;\u89c1\u7b2c\u4e09\u7ae0 3.3&#xff09;<\/p>\n<p>\u7248\u672c2&#xff1a;\u901a\u7528\u7ebf\u7a0b\u5b89\u5168\u7248\u672c&#xff08;\u57fa\u4e8e\u4e34\u754c\u533a&#xff09; &#xff08;\u89c1\u7b2c\u4e09\u7ae0 3.3&#xff09;<\/p>\n<p>\u7248\u672c3&#xff1a;FreeRTOS \u7248\u672c&#xff08;\u4f7f\u7528\u961f\u5217API&#xff09; FreeRTOS\u7684 xQueue \u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7ebf\u7a0b\u5b89\u5168\u73af\u5f62\u961f\u5217\u3002<\/p>\n<p>\/\/ \u521b\u5efa\u961f\u5217<br \/>\nQueueHandle_t my_queue &#061; xQueueCreate(QUEUE_LENGTH, sizeof(ItemType));<\/p>\n<p>\/\/ \u751f\u4ea7\u8005&#xff08;\u4efb\u52a1\u6216ISR&#xff09;\u53d1\u9001<br \/>\nItemType item_to_send;<br \/>\nxQueueSend(my_queue, &amp;item_to_send, portMAX_DELAY); \/\/ \u4efb\u52a1\u4e2d\u53d1\u9001<br \/>\n\/\/ xQueueSendFromISR(my_queue, &amp;item_to_send, &amp;xHigherPriorityTaskWoken); \/\/ ISR\u4e2d\u53d1\u9001<\/p>\n<p>\/\/ \u6d88\u8d39\u8005&#xff08;\u4efb\u52a1&#xff09;\u63a5\u6536<br \/>\nItemType received_item;<br \/>\nxQueueReceive(my_queue, &amp;received_item, portMAX_DELAY);<\/p>\n<h4>C. \u72b6\u6001\u673a\u8c03\u8bd5\u6280\u5de7\u6c47\u603b<\/h4>\n<li>\u72b6\u6001\u8f6c\u6362\u65e5\u5fd7&#xff1a;\u5982\u524d\u6587\u6240\u8ff0&#xff0c;\u8fd9\u662f\u6700\u91cd\u8981\u7684\u8c03\u8bd5\u624b\u6bb5\u3002<\/li>\n<li>\u72b6\u6001\u65ad\u8a00&#xff1a;\u5728\u72b6\u6001\u8f6c\u6362\u7684\u5173\u952e\u8def\u5f84\u4e0a\u52a0\u5165 assert()&#xff0c;\u68c0\u67e5\u662f\u5426\u53d1\u751f\u4e86\u975e\u6cd5\u8f6c\u6362\u3002 assert(g_state !&#061; ILLEGAL_STATE);<\/li>\n<li>\u53ef\u89c6\u5316\u5de5\u5177&#xff1a;\u4e00\u4e9bIDE&#xff08;\u5982IAR, Keil&#xff09;\u6216\u7b2c\u4e09\u65b9\u5de5\u5177&#xff08;\u5982Tracealyzer&#xff09;\u53ef\u4ee5\u53ef\u89c6\u5316\u5730\u5c55\u793a\u4efb\u52a1\u548c\u4e2d\u65ad\u7684\u6267\u884c\u8f68\u8ff9&#xff0c;\u5bf9\u4e8e\u5206\u6790\u5e76\u53d1\u95ee\u9898\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/li>\n<li>\u903b\u8f91\u5206\u6790\u4eea\/\u793a\u6ce2\u5668&#xff1a;\u5f53\u8f6f\u4ef6\u8c03\u8bd5\u9677\u5165\u50f5\u5c40\u65f6&#xff0c;\u7528\u786c\u4ef6\u5de5\u5177\u89c2\u5bdfGPIO\u5f15\u811a\u7684\u7535\u5e73\u53d8\u5316\u3002\u4f8b\u5982&#xff0c;\u5728\u8fdb\u5165\/\u9000\u51fa\u5173\u952e\u72b6\u6001\u65f6\u7ffb\u8f6c\u4e00\u4e2aGPIO\u5f15\u811a&#xff0c;\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u72b6\u6001\u673a\u7684\u65f6\u95f4\u884c\u4e3a\u3002<\/li>\n<li>\u5355\u5143\u6d4b\u8bd5&#xff1a;\u4e3a\u6bcf\u4e2a\u72b6\u6001\u548c\u6bcf\u4e2a\u8f6c\u6362\u7f16\u5199\u72ec\u7acb\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u6846\u67b6&#xff0c;\u6a21\u62df\u4e8b\u4ef6\u8f93\u5165&#xff0c;\u5e76\u65ad\u8a00\u72b6\u6001\u673a\u7684\u8f93\u51fa\u548c\u4e0b\u4e00\u4e2a\u72b6\u6001\u662f\u5426\u7b26\u5408\u9884\u671f\u3002<\/li>\n<li>\u4ee3\u7801\u8986\u76d6\u7387\u5206\u6790&#xff1a;\u4f7f\u7528\u5de5\u5177&#xff08;\u5982gcov&#xff09;\u5206\u6790\u6d4b\u8bd5\u7528\u4f8b\u5bf9\u72b6\u6001\u673a\u4ee3\u7801\u7684\u8986\u76d6\u7387&#xff0c;\u786e\u4fdd\u6240\u6709\u72b6\u6001\u548c\u8f6c\u6362\u90fd\u88ab\u6d4b\u8bd5\u5230\u3002<\/li>\n<li>\u6545\u969c\u6ce8\u5165&#xff1a;\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d&#xff0c;\u6a21\u62df\u5404\u79cd\u5f02\u5e38\u4e8b\u4ef6&#xff08;\u5982\u63a5\u6536\u9519\u8bef\u6570\u636e\u3001\u6a21\u62df\u786c\u4ef6\u6545\u969c&#xff09;&#xff0c;\u89c2\u5bdf\u72b6\u6001\u673a\u7684\u53cd\u5e94\u662f\u5426\u7b26\u5408\u8bbe\u8ba1\u9884\u671f\u3002<\/li>\n<hr \/>\n<p>(\u6587\u6863\u7ed3\u675f)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053&#xff1a;\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d&#xff0c;\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u7248\u672c&#xff1a; 1.0 \u4f5c\u8005&#xff1a; AI \u67b6\u6784\u5e08 \u76ee\u6807\u8bfb\u8005&#xff1a; \u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u7cfb\u7edf\u67b6\u6784\u5e08\u3001\u6280\u672f\u8d1f\u8d23\u4eba \u7bc7\u5e45&#xff1a; \u7ea6 60,000 \u5b57\u76ee\u5f55\u7b2c\u4e00\u7ae0&#xff1a;\u5f15\u8a00\u2014\u2014\u4ece\u201c\u903b\u8f91\u5730\u72f1\u201d\u5230\u201c\u67b6\u6784\u5929\u5802\u201d\u7b2c\u4e8c\u7ae0&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u77f3\u2014\u2014\u4e2d\u65ad\u4e0e\u5e76\u53d1\u7b2c\u4e09\u7ae0&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u52a8\u8109\u2014\u2014\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;\u7b2c\u56db\u7ae0&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,99,3750,269,190],"topic":[],"class_list":["post-99680","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-java","tag-mfc","tag-269","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/99680.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053&#xff1a;\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d&#xff0c;\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u7248\u672c&#xff1a; 1.0 \u4f5c\u8005&#xff1a; AI \u67b6\u6784\u5e08 \u76ee\u6807\u8bfb\u8005&#xff1a; \u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u7cfb\u7edf\u67b6\u6784\u5e08\u3001\u6280\u672f\u8d1f\u8d23\u4eba \u7bc7\u5e45&#xff1a; \u7ea6 60,000 \u5b57\u76ee\u5f55\u7b2c\u4e00\u7ae0&#xff1a;\u5f15\u8a00\u2014\u2014\u4ece\u201c\u903b\u8f91\u5730\u72f1\u201d\u5230\u201c\u67b6\u6784\u5929\u5802\u201d\u7b2c\u4e8c\u7ae0&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u77f3\u2014\u2014\u4e2d\u65ad\u4e0e\u5e76\u53d1\u7b2c\u4e09\u7ae0&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u52a8\u8109\u2014\u2014\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;\u7b2c\u56db\u7ae0&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/99680.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-02T16:37:06+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"27 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99680.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/99680.html\",\"name\":\"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-02T16:37:06+00:00\",\"dateModified\":\"2026-09-02T16:37:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/99680.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/99680.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99680.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/99680.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053&#xff1a;\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d&#xff0c;\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u7248\u672c&#xff1a; 1.0 \u4f5c\u8005&#xff1a; AI \u67b6\u6784\u5e08 \u76ee\u6807\u8bfb\u8005&#xff1a; \u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u7cfb\u7edf\u67b6\u6784\u5e08\u3001\u6280\u672f\u8d1f\u8d23\u4eba \u7bc7\u5e45&#xff1a; \u7ea6 60,000 \u5b57\u76ee\u5f55\u7b2c\u4e00\u7ae0&#xff1a;\u5f15\u8a00\u2014\u2014\u4ece\u201c\u903b\u8f91\u5730\u72f1\u201d\u5230\u201c\u67b6\u6784\u5929\u5802\u201d\u7b2c\u4e8c\u7ae0&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u57fa\u77f3\u2014\u2014\u4e2d\u65ad\u4e0e\u5e76\u53d1\u7b2c\u4e09\u7ae0&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u52a8\u8109\u2014\u2014\u73af\u5f62\u961f\u5217&#xff08;FIFO&#xff09;\u7b2c\u56db\u7ae0&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/99680.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-02T16:37:06+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"27 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/99680.html","url":"https:\/\/www.wsisp.com\/helps\/99680.html","name":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-02T16:37:06+00:00","dateModified":"2026-09-02T16:37:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/99680.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/99680.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/99680.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5d4c\u5165\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e4b\u9053\uff1a\u544a\u522b\u201c\u610f\u5927\u5229\u9762\u6761\u201d\uff0c\u62e5\u62b1\u72b6\u6001\u673a\u4e0e\u4e8b\u4ef6\u9a71\u52a8"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/99680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=99680"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/99680\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=99680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=99680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=99680"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=99680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}