{"id":109,"date":"2026-05-12T19:48:53","date_gmt":"2026-05-12T11:48:53","guid":{"rendered":"https:\/\/blog.shufue.top\/?p=109"},"modified":"2026-05-12T19:48:53","modified_gmt":"2026-05-12T11:48:53","slug":"rust_emb%e5%a4%96%e8%ae%beperipherals","status":"publish","type":"post","link":"https:\/\/blog.shufue.top\/?p=109","title":{"rendered":"[Rust_emb]\u5916\u8bbe(Peripherals)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5916\u8bbe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MCU\u4e0d\u662f\u53ea\u6709CPU,Flash,Ram\u3002\u4e5f\u5305\u542b\u4f20\u611f\u5668\uff0c\u663e\u793a\u5668\u7b49\u5e2e\u52a9MCU\u548c\u5916\u754c\u4ea4\u4e92\u7684\u8bbe\u5907\u8fd9\u4e9b\u7edf\u4e00\u79f0\u4e4b\u4e3a\u5916\u8bbePeripherals\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5916\u8bbe\u7684\u4f5c\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5916\u8bbe\u53ef\u4ee5\u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u4efb\u52a1\u5904\u7406\u7684\u5de5\u4f5c\u5378\u8f7d\u5728\u5916\u8bbe\u4ece\u800c\u51cf\u5c11CPU\u7684\u8d1f\u8f7d\u6216\u8005\u8282\u7701\u7535\u91cf\u3002\u7c7b\u4f3c\u4e8ePC\u5c06\u56fe\u5f62\u5904\u7406\u4efb\u52a1\u5378\u8f7d\u5230GPU\u4e0a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/docs.rust-embedded.org\/book\/peripherals\/singletons.html#singletons\">Singletons<\/a><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0d\u5efa\u8bae\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5bb9\u8bbe\u4e3a\u516c\u5171\u5168\u5c40\u9759\u6001\uff0c\u5c31\u50cf\u8fd9\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static mut THE_SERIAL_PORT: SerialPort = SerialPort;\n\nfn main() {\n    let _ = unsafe {\n        THE_SERIAL_PORT.read_speed();\n    };\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u610f\u5473\u7740\u5728\u591a\u7ebf\u7a0b\u6216\u8005\u591a\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u5bf9\u516c\u5171\u5168\u5c40\u53d8\u91cf\u7684\u8bfb\u5199\u7ade\u4e89\uff0c\u56e0\u4e3a\u4e0d\u53ef\u80fd\u6240\u6709\u5bf9\u516c\u5171\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u539f\u5b50\u64cd\u4f5c\u3002\u6240\u4ee5\u8fd9\u6837\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u65e0\u6cd5\u8ddf\u8e2a\u53d8\u91cf\u7684\u5f15\u7528\u6743\u548c\u6240\u6709\u6743\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u505a\u5728Rust<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u5176\u5c06\u5916\u8bbe\u8bbe\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u8fd9\u91cc\u79f0\u4e4b\u4e3a\u00a0<code>PERIPHERALS<\/code>\u00a0\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5916\u8bbe\u7684\u00a0<code>Option&lt;T><\/code>\u00a0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Peripherals {\n    serial: Option&lt;SerialPort>,\n}\nimpl Peripherals {\n    fn take_serial(&amp;mut self) -> SerialPort {\n        let p = replace(&amp;mut self.serial, None);\n        p.unwrap()\n    }\n}\nstatic mut PERIPHERALS: Peripherals = Peripherals {\n    serial: Some(SerialPort),\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u73b0\u6709\u5e93\u7684\u652f\u6301<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u7ba1\u4e0a\u9762\u521b\u5efa\u4e86\u00a0<code>Peripherals<\/code>\u00a0\u7ed3\u6784\u4f53\uff0c\u4f46\u4e3a\u4f60\u7684\u4ee3\u7801\u5e76\u975e\u5fc5\u8981\u8fd9\u6837\u505a\u3002\u00a0<code>cortex_m<\/code>\u00a0\u5e93\u5305\u542b\u4e00\u4e2a\u540d\u4e3a\u00a0<code>singleton!()<\/code>\u00a0\u7684\u5b8f\uff0c\u4e3a\u4f60\u6267\u884c\u8fd9\u4e2a\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use cortex_m::singleton;\n\nfn main() {\n    \/\/ OK if `main` is executed only once\n    let x: &amp;'static mut bool =\n        singleton!(: bool = false).unwrap();\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>let x : &amp; &#8216;static mut bool  \u58f0\u660ex\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u5f15\u7528\u7684\u662f&#8217;static mut bool\u8fd9\u4e2a\u683c\u5f0f\u7684<\/li>\n\n\n\n<li>singleton! \u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u552f\u4e00\u7684\u53d8\u91cf,\u7b2c\u4e00\u6b21\u8c03\u7528\u5c31\u8fd4\u56de\u00a0<code>Some(&amp;'static mut bool)<\/code>\u3002<\/li>\n\n\n\n<li>(: bool =false)  \u58f0\u660e\u521b\u5efa\u7684\u53d8\u91cf\u662f\u5e03\u5c14\u503c\u521d\u59cb\u5316\u4e3afalse\u3002<\/li>\n\n\n\n<li>\u603b\u7ed3\uff1a\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u552f\u4e00\u7684\u5e03\u5c14\u503c\uff0c\u521d\u59cb\u503c\u662f false\uff0c\u5e76\u8fd4\u56de\u5b83\u7684\u53ef\u5199\u5f15\u7528\u7ed9 x\u3002\u201d<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5916\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u00a0<code>cortex-m-rtic<\/code>\u00a0\uff0c\u62bd\u8c61\u5316\u5b9a\u4e49\u548c\u83b7\u53d6\u8fd9\u4e9b\u5916\u8bbe\uff0c\u4f60\u5c06\u5f97\u5230\u4e00\u4e2a\u00a0<code>Peripherals<\/code>\u00a0\u7ed3\u6784\u4f53\uff0c\u5176\u4e2d\u5305\u542b\u4f60\u5b9a\u4e49\u7684\u6240\u6709\u9879\u7684\u975e\u00a0<code>Option&lt;T><\/code>\u00a0\u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ cortex-m-rtic v0.5.x\n#&#91;rtic::app(device = lm3s6965, peripherals = true)]\nconst APP: () = {\n    #&#91;init]\n    fn init(cx: init::Context) {\n        static mut X: u32 = 0;\n         \n        \/\/ Cortex-M peripherals\n        let core: cortex_m::Peripherals = cx.core;\n        \n        \/\/ Device specific peripherals\n        let device: lm3s6965::Peripherals = cx.device;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5916\u8bbe MCU\u4e0d\u662f\u53ea\u6709CPU,Flash,Ram\u3002\u4e5f\u5305\u542b\u4f20\u611f\u5668\uff0c\u663e\u793a\u5668\u7b49\u5e2e\u52a9MCU\u548c\u5916\u754c\u4ea4\u4e92\u7684\u8bbe\u5907\u8fd9\u4e9b\u7edf\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":1,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.shufue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}