{"id":58,"date":"2012-11-23T23:38:00","date_gmt":"2012-11-23T23:38:00","guid":{"rendered":"https:\/\/blafaselblub.wordpress.com\/2012\/11\/23\/debugging-the-garage-door-remote"},"modified":"2012-11-23T23:38:00","modified_gmt":"2012-11-23T23:38:00","slug":"debugging-the-garage-door-remote","status":"publish","type":"post","link":"https:\/\/blog.alexander-tuxen.de\/index.php\/2012\/11\/23\/debugging-the-garage-door-remote\/","title":{"rendered":"Debugging the garage door remote"},"content":{"rendered":"<p>A while ago I built a <a href=\"http:\/\/testblog.arles-electrique.de\/2012\/08\/garage-door-remote-for-real.html\">little remote<\/a> to open my garage door (well, not actually <i>my<\/i>\u00a0garage door but the one of the house I live in, but who cares). But the greedy kid I am, I wasn&#8217;t satisfied with just opening the garage door with it. After all it has four buttons, and so I planned on also using it as a remote for my\u00a0yet to be built\u00a0home automation system (a few parts of it already exist, but there is a lot of work to be done before I can really call it that).<\/p>\n<p>The problem was that it had a nasty bug which was really hard to track down: It would work as intended for some time (sometimes hours, sometimes even days), but then, all of a sudden it would hang. And not only did it seize to work, it also stayed in some state where it consumed lots of power and ate up the battery in no time. This was far from usable in real life.<\/p>\n<p>I had heard of a little thing called watchdog timer, which could reset the MCU if it hung. So the first thing I did was to add this watchdog timer to my code.\u00a0Sure enough that didn&#8217;t fix my problem, so I tried everything I could think of from incrementing counters in EEPROM after every &#8220;real&#8221; instruction in the code to hooking up oscilloscopes and logic analyzers. But the only thing I found out was that it somehow, sometimes would get stuck in some weird kind of a reset loop.<\/p>\n<p>When I ran out of ideas of what else to try I started badgering people on various internet forums. And after trying all sorts of changes to the code that people suggested, someone came up with the idea of\u00a0deliberately causing a reset (among a few other things). I implemented that idea in my code and what happened was, that now every time I caused the reset, the MCU would hang. That may not sound like a good thing at first, but at least now I could reproduce the bug!<\/p>\n<p>After that all it took was a bit of searching the web until I found out about a register called MCUSR, the MCU status register. Turns out that if the watchdog wants to reset the MCU, it writes some value to this register that would cause the MCU to perform the reset. But for some reason it doesn&#8217;t get cleared <i>after<\/i>\u00a0the reset is done, causing the MCU to keep resetting itself until the battery is dead or the world ends, whichever comes first.<\/p>\n<p>So in the end all I had to do was to set the MCUSB to zero right at the beginning of the setup() routine and all of a sudden the reset would be performed as intended and the MCU would work again.\u00a0For now. I&#8217;ll have to give it a few weeks to really call this case closed, but I&#8217;m pretty confident that this was it.<\/p>\n<p>All that&#8217;s left to do is to say a big THANK YOU to all the people who tried to help me solve this mystery, and especially JohnO on the <a href=\"http:\/\/forum.jeelabs.net\/node\/1537\">Jeelabs<\/a> forum, who came up with the idea of causing that reset on purpose (although he might have had something completely different in mind than crashing the MCU every time the reset fired).<\/p>\n<p>The updated code can be downloaded <a href=\"https:\/\/github.com\/alibenpeng\/keyfob\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while ago I built a little remote to open my garage door (well, not actually my\u00a0garage door but the one of the house I live in, but who cares). But the greedy kid I am, I wasn&#8217;t satisfied with &hellip; <a href=\"https:\/\/blog.alexander-tuxen.de\/index.php\/2012\/11\/23\/debugging-the-garage-door-remote\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,6,3,7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":0,"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alexander-tuxen.de\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}