{"id":4260,"date":"2013-01-14T10:02:53","date_gmt":"2013-01-14T09:02:53","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=4260"},"modified":"2013-01-14T10:02:53","modified_gmt":"2013-01-14T09:02:53","slug":"write-only-ports","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2013\/01\/14\/write-only-ports\/","title":{"rendered":"Write-only ports"},"content":{"rendered":"<p>I often joke that my job puts me in a difficult position, as I am a software guy and a very large proportion of Mentor Graphics employees are hardware design specialists; I am consorting with the &#8220;the enemy&#8221;. Although I am making a light-hearted comment, there is, sadly, something of an &#8220;us and them&#8221; attitude between software and hardware teams in many companies. It is in everybody&#8217;s interest to make this a thing of the past.<\/p>\n<p>Sometimes, it seems as if this view is reasonable, when hardware seems to be designed specifically to make life hard for software developers &#8230;<!--more--><\/p>\n<p>The issue of hardware being challenging to control from software is well known. The whole idea of device drivers was conceived, as much as anything, to hide the nastiness of such interfaces from non-experts. My favorite example of hardware that seems to have designed for intentional aggravation is a write-only port.<\/p>\n<p>This type of interface has a register, to which data may be written, but from which the current value may not be read back. This may be problematic because the 8-, 16- or 32-bit port may consist of a number of single- or multi-bit registers with unrelated functionality. This means that different parts of the software may need to access the port and there are plenty of opportunities for unintentional interference. For example, to set the bottom 2 bits of the port without affecting the other bits, the following code would seem obvious:<\/p>\n<pre>WOPORT |= 0x03;<\/pre>\n<p>&nbsp;<\/p>\n<p>However, this construct will not work, as the <strong>|=<\/strong> operator generates code which will read the port [and get invalid data or trigger an exception] before writing an updated value. The solution is simple enough: keep a &#8220;shadow&#8221; copy of the port data and use that each time an update is required, like this:<\/p>\n<pre>woport_shadow |= 0x03;<\/pre>\n<pre>WOPORT = woport_shadow;<\/pre>\n<p>&nbsp;<\/p>\n<p>This is quite straightforward, but there are a few things to take care of:<\/p>\n<ol>\n<li>The port and the shadow data must be correctly initialized.<\/li>\n<li>Code which accesses the port must <strong>always<\/strong> utilize the shadow.<\/li>\n<li>Re-entrancy may be an issue. If an interrupt occurred between the updating of the shadow and the writing to the port, another thread [task or ISR] might utilize the port and the shadow could get out of synch.<\/li>\n<\/ol>\n<p>All of these issues may be addressed by careful code design. Probably the best approach is to encapsulate the access to a port inside a function, thus providing a common API. Another approach might be to use C++ to encapsulate the details of write-only port access. I will look at that approach on another occasion.<\/p>\n<p>Incidentally, the code above might be an ideal application for my approach to binary constants, which I wrote about <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2009\/06\/08\/there-are-10-kinds-of-people-in-the-world\/\" target=\"_blank\" rel=\"noopener noreferrer\">some time ago<\/a>; instead of <strong>0x03<\/strong> you would use <strong>b00000011<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often joke that my job puts me in a difficult position, as I am a software guy and a&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,511,300],"industry":[],"product":[],"coauthors":[],"class_list":["post-4260","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-drivers","tag-embedded-software"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/4260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=4260"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/4260\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=4260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=4260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=4260"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=4260"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=4260"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=4260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}