{"id":1027,"date":"2010-10-15T19:40:27","date_gmt":"2010-10-15T17:40:27","guid":{"rendered":"http:\/\/blog.fh-kaernten.at\/wehr\/?p=1027"},"modified":"2010-10-15T19:40:27","modified_gmt":"2010-10-15T17:40:27","slug":"phytec-8564-rtc-i%c2%b2c-interface-library-used-in-interrupt-driven-env","status":"publish","type":"post","link":"https:\/\/blog.fh-kaernten.at\/wehr\/2010\/10\/15\/phytec-8564-rtc-i%c2%b2c-interface-library-used-in-interrupt-driven-env\/","title":{"rendered":"Phytec 8564-RTC I\u00b2C interface library used in interrupt driven env."},"content":{"rendered":"<p id=\"top\" \/>\n<p>A few posts before I wrote about about the project &#8220;Campus Door System &#8220;. In our real time environment part of the project we are using the miniMODUL-167 which is equipt with an RTC-8583.<\/p>\n<p>For I\u00b2C\u00a0 &lt; -&gt; RTC interfacing we are using sources which can be <a href=\"ftp:\/\/217.6.246.43\/pub\/Products\/phyCORE-TC1775\/DCM-010\/Software\/test\/\">downloaded <\/a>from the Phytec ftp servers. For the whole project we have been using this source without problems \ud83d\ude42<\/p>\n<p>After rolling out the system to one of our customers\u00a0a few weeks later the system suddenly stopped, only a reset could clear the situation. Our in-house systems have never shown this kind of symptoms. So what the hell was going on?<\/p>\n<p>After some days I found out that the problem might be located on the RTC part. After the crash the RTC delivers such funny things like 56 h 78 m 99 s , lol, ok so the RTC counters only compare for equals thats for sure :-). But what part of our code is setting the RTC\u00a0 ?<\/p>\n<p>To make it short, the problem was a combination of our system architecture and the way the used I\u00b2C library is working.<\/p>\n<p>Because the I\u00b2C library has a low level layer which is responsible for generating the bit pattern of the I\u00b2C data, this part of the code is prone to wrong designed interrupt architecture.<\/p>\n<p>Sample of the low level I\u00b2C:<\/p>\n<pre class=\"brush: c\">\/****************************************************************************\/\r\n\/* sends one bit to I2C-bus, also used for sending ACKNOWLEDGE condition    *\/\r\n\r\nvoid I2CSendBit(BYTE State) {\r\n\/\/ I2CDelay();\r\n   I2CPutSCL(0);                       \/* setup SCL (redundant)             *\/\r\n\/\/ I2CDelay();\r\n   I2CPutSDA(State);                   \/* setup SDA                         *\/\r\n\/\/ I2CDelay();\r\n   I2CPutSCL(1);                       \/* enable SDA writing                *\/\r\n\r\n\/\/ I2CDelay();                         \/* let SCL get stabilized High       *\/\r\n\r\n   while(!I2CGetSCL());                \/* wait until SCL-line is released   *\/\r\n\r\n\/\/ I2CDelay();\r\n   I2CPutSCL(0);                       \/* disable SDA writing (SDA is now   *\/\r\n                                       \/* clocked)                          *\/\r\n\/\/ I2CDelay();\r\n   I2CPutSDA(1);                       \/* release SDA *\/\r\n}\r\n\r\n\/****************************************************************************\/\r\n\/* set SCL-line to desired state\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n\r\nvoid I2CPutSCL(BYTE State)\r\n{\r\n if(!State)\r\n {\r\n I2C_PORT &amp;= ~I2C_SCL_MSK;\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 \/* set SCL-Line to ZERO\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 *\/\r\n#ifdef __C166__\r\n I2C_PORT_DIR |= I2C_SCL_MSK;\u00a0\u00a0\u00a0\u00a0 \/* set SCL-Pin for OUTPUT Mode\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 *\/\r\n#endif\r\n \/* enable output AFTER setting of\u00a0\u00a0\u00a0 *\/\r\n \/* correct level to avoid '1'-level\u00a0 *\/\r\n \/* spikes\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n }\r\n else\r\n {\r\n#ifdef __C166__\r\n I2C_PORT_DIR &amp;= ~I2C_SCL_MSK;\u00a0\u00a0\u00a0 \/* set SCL-Pin for INPUT Mode\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 *\/\r\n#endif\r\n \/* this is possible because of the\u00a0\u00a0 *\/\r\n \/* external pull-up resistor !\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n \/* set SCL-line to ONE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n I2C_PORT |= I2C_SCL_MSK;\r\n }\r\n}\r\n\r\n\/****************************************************************************\/\r\n\/* read state of\u00a0 SCL-line\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n\/* WARNING: This function should be used only if SCL-line is in High-level !*\/\r\n\/* (This function is for used only for realizing the Slow-Down-Mode)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n\r\nBYTE I2CGetSCL(void)\r\n{\r\n #ifdef __C166__\r\n I2C_PORT_DIR &amp;= ~I2C_SCL_MSK;\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \/* set SCL-Pin for INPUT Mode\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 *\/\r\n #endif\r\n\r\n if(I2C_PORT&amp;I2C_SCL_MSK)\r\n return(1);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \/* read SCL-line status\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 *\/\r\n else\r\n return(0);\r\n}\r\n<\/pre>\n<p>This is exactly what happend. In-house we have only a certain amount of readers connected to the external buses of the access system. But our customer has over 100 readers installed. So the interrupt load was at a level where the situation could arise that at the moment where the I\u00b2C bit pattern is generated a few interrupts suspended the pattern generation. This leads in the worst case to a write instead of a read flag \ud83d\ude42<\/p>\n<p>A quick fix was to globally disable interrupts in I\u00b2C write situations or a better solution would be to move the accourding code to a high priojority interrupt.<\/p>\n<p>I&#8217;m also writing this because some of my colleagues also use this lib &#8211; and yes they also do not read manuals \ud83d\ude42<\/p>\n<p>have fun<\/p>\n<p>Mario<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few posts before I wrote about about the project &#8220;Campus Door System &#8220;. In our real time environment part of the project we are using the miniMODUL-167 which is equipt with an RTC-8583. For I\u00b2C\u00a0 &lt; -&gt; RTC interfacing we are using sources which can be downloaded from the Phytec ftp servers. For the &hellip; <a href=\"https:\/\/blog.fh-kaernten.at\/wehr\/2010\/10\/15\/phytec-8564-rtc-i%c2%b2c-interface-library-used-in-interrupt-driven-env\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Phytec 8564-RTC I\u00b2C interface library used in interrupt driven env.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":0,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}