{"id":2002,"date":"2010-12-20T19:03:25","date_gmt":"2010-12-20T17:03:25","guid":{"rendered":"http:\/\/www.final-memory.org\/?p=2002"},"modified":"2010-12-20T19:03:25","modified_gmt":"2010-12-20T17:03:25","slug":"memory-mapped-io-made-easy-with-ansi-c","status":"publish","type":"post","link":"https:\/\/www.final-memory.org\/?p=2002","title":{"rendered":"Memory mapped I\/O made easy with ANSI C"},"content":{"rendered":"<p>Ich habe hier mal in einem einzigen Demo-C-Source das geballte Knowhow zusammengefasst, wie man f\u00fcr einen Rechner mit Memorymapped I\/O (z.B. ARM oder Motorola 68000\/ColdFire) korrekt den Zugriff auf ein Hardwareregister via Pointer realisiert:<\/p>\n<pre>\/*\r\n * Memory mapped I\/O made easy with ANSI C\r\n * commented by Matthias Arndt &lt;marndt@asmsoftware.de&gt;\r\n *\/\r\n#include &lt;stdint.h&gt;<\/pre>\n<pre>\/* temporary valid location for demonstration purposes *\/\r\nuint8_t storage;<\/pre>\n<pre>\/* Now the magic declaration pointer to a hw register:\r\n * We point it to some known storage but ofcourse for pointing\r\n * to a real I\/O hardware register, one would supply a constant\r\n * register address.\r\n *\r\n * a) declare it <em><span style=\"color: #ff0000;\">volatile<\/span><\/em> because hardware I\/O locations may change inside\r\n *    a different context (interrupt and\/or hardware event)\r\n * b) make the pointer address <em><span style=\"color: #008000;\">const<\/span><\/em> between register name and the type\r\n *    definition so that noone may modify the pointer\r\n * c) adding const before the declaration will declare a read\/only register\r\n *\/<\/pre>\n<pre><em><span style=\"color: #ff0000;\">volatile<\/span><\/em> uint8_t * <em><span style=\"color: #008000;\">const<\/span><\/em> HWREG = &amp;storage;\r\n\/* to point to real I\/O you would use the following syntax: *\/\r\n\/* volatile uint8_t * const HWREG = (uint8_t *)0xF000; *\/<\/pre>\n<pre>\/* just some demo calls *\/\r\nvoid task(void);\r\nuint8_t access(void);<\/pre>\n<pre>void task()\r\n{\r\n\t*HWREG = 0x5a; \/* set I\/O for demo purpose *\/<\/pre>\n<pre>\t\/* access to alter the pointer is forbidden! Uncomment to try! *\/\r\n\t\/* HWREG = (uint8_t *) 0xaaaa; *\/\r\n}<\/pre>\n<pre>uint8_t access()\r\n{\r\n\treturn(*HWREG); \/* read I\/O *\/\r\n}<\/pre>\n<p>F\u00fcr 8051 oder andere Plattformen, die \u00e4hnlich verquerte Speicherbereiche haben, sollten zus\u00e4tzliche Angaben\u00a0 verwendet werden, die den Zugriff auf den richtigen Speicherbereich mappen, etwa XRAM beim 8051.<\/p>\n<div id=\"facebook_like\"><iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.final-memory.org%2F%3Fp%3D2002&amp;layout=standard&amp;show_faces=true&amp;width=500&amp;action=like&amp;font=segoe+ui&amp;colorscheme=light&amp;height=80\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:500px; height:80px;\" allowTransparency=\"true\"><\/iframe><\/div>","protected":false},"excerpt":{"rendered":"<p>Ich habe hier mal in einem einzigen Demo-C-Source das geballte Knowhow zusammengefasst, wie man f\u00fcr einen Rechner mit Memorymapped I\/O (z.B. ARM oder Motorola 68000\/ColdFire) korrekt den Zugriff auf ein Hardwareregister via Pointer realisiert: \/* * Memory mapped I\/O made easy with ANSI C * commented by Matthias Arndt &lt;marndt@asmsoftware.de&gt; *\/ #include &lt;stdint.h&gt; \/* temporary &hellip; <a href=\"https:\/\/www.final-memory.org\/?p=2002\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Memory mapped I\/O made easy with ANSI C&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65,34,57,74,35,36],"tags":[],"class_list":["post-2002","post","type-post","status-publish","format-standard","hentry","category-68hc11","category-34","category-avr","category-embedded-systems","category-mikrocontroller","category-sdcc"],"_links":{"self":[{"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/posts\/2002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2002"}],"version-history":[{"count":4,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/posts\/2002\/revisions"}],"predecessor-version":[{"id":2006,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=\/wp\/v2\/posts\/2002\/revisions\/2006"}],"wp:attachment":[{"href":"https:\/\/www.final-memory.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.final-memory.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}