{"id":45603,"date":"2021-06-18T09:40:45","date_gmt":"2021-06-18T13:40:45","guid":{"rendered":"http:\/\/pappp.net\/?guid=2fcd140594be135ec2c48e83588a913c"},"modified":"2021-06-18T09:40:45","modified_gmt":"2021-06-18T13:40:45","slug":"honey-i-shrunk-the-arduino-core-arduino-programming","status":"publish","type":"post","link":"https:\/\/pappp.net\/?p=45603","title":{"rendered":"Honey, I shrunk the Arduino core! #Arduino #Programming"},"content":{"rendered":"<p class=\"syndicated-attribution\">Source: <a href=\"https:\/\/blog.adafruit.com\/2021\/06\/18\/honey-i-shrunk-the-arduino-core-arduino-programming\/\">adafruit industries blog<\/a><\/p>\n<div style=\"background-color : #fff7d5;\n\t\t\tborder-width : 1px; padding : 5px; border-style : dashed; border-color : #e7d796;margin-bottom : 1em; color : #9a8c59;\">Article note: Very cool. \nArduino is already relatively low-overhead on size and performance compared to a lot of the rapid-prototyping environments (Lookin' at you micropython, I've seen 500x performance hits relative to C there), but it's nice to see the lower layers get better, particularly since improving the underlying tooling makes everything built on top of it better.<\/div><p><img loading=\"lazy\" src=\"https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144.jpg\" alt=\"\" srcset=\"https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144.jpg 683w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-300x88.jpg 300w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-600x177.jpg 600w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-150x44.jpg 150w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144.jpg 683w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-300x88.jpg 300w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-600x177.jpg 600w,https:\/\/cdn-blog.adafruit.com\/uploads\/2021\/06\/Untitled-144-150x44.jpg 150w\" sizes=\"(max-width: 683px) 100vw, 683px\" referrerpolicy=\"no-referrer\"\/><\/p><p>Nerd Ralph <a href=\"http:\/\/nerdralph.blogspot.com\/2021\/04\/honey-i-shrunk-arduino-core.html\"  rel=\"noopener noreferrer\">blogs<\/a> about shrinking the Arduino core for ATmega168 and 328 MCUs at 4 to 16 MHz:<\/p><blockquote><p>One of my gripes about&nbsp;<a href=\"https:\/\/github.com\/arduino\/ArduinoCore-avr\"  rel=\"noopener noreferrer\">the Arduino AVR core<\/a>&nbsp;is that it is not an example of efficient embedded programming.&nbsp; One of the&nbsp;foundations of C++ is zero-overhead abstractions, yet the Arduino core has a very significant overhead.&nbsp; The Arduino basic blink example compiles to almost 1kB, with most of that space taken up by code that is never used.&nbsp; Rewriting the AVR core is a task I&rsquo;m not ready to tackle, but after writing&nbsp;<a href=\"https:\/\/github.com\/nerdralph\/picoCore\"  rel=\"noopener noreferrer\">picoCore<\/a>, I realized I could use many of the same optimization techniques in an Arduino library.&nbsp; The result is&nbsp;<a href=\"https:\/\/github.com\/nerdralph\/ArduinoShrink\"  rel=\"noopener noreferrer\">ArduinoShrink<\/a>, a library that can dramatically reduce the compiled size of Arduino projects.&nbsp; In this post, I&rsquo;ll explain some of the techniques I used to achieve the coding trifecta of faster, better, and smaller.<\/p><\/blockquote><p>The Arduino core is actually a static library that is linked with the project code.&nbsp; As Eli explains in&nbsp;<a href=\"https:\/\/eli.thegreenplace.net\/2013\/07\/09\/library-order-in-static-linking\"  rel=\"noopener noreferrer\">this post on static linking<\/a>, libraries like libc usually have only one function per .o in order to avoid linking in unnecessary code.&nbsp; The Arduino doesn&rsquo;t use that kind of modular approach, however by making use of gcc&rsquo;s &ldquo;-ffunction-sections&rdquo; option, it does mitigate the amount of code bloat due to the non-modular approach.<\/p><blockquote><p>With&nbsp;ArduinoShrink, I wrote more modular, self-contained code.&nbsp; For example, the&nbsp;Arduino delay()&nbsp;function calls micros(), which relies on the 32-bit timer0 interrupt overflow counter.&nbsp; I simplified the delay function so that it only needs the 8-bit timer value.&nbsp; If the user code never calls micros() or millis(), the timer0 ISR code never gets linked in.&nbsp; By using a more efficient algorithm and writing the code in AVR assembler, I reduced the size of&nbsp;the delay function&nbsp;to 12 instructions taking 24 bytes of flash.<\/p><\/blockquote><p>See the post <a href=\"http:\/\/nerdralph.blogspot.com\/2021\/04\/honey-i-shrunk-arduino-core.html\"  rel=\"noopener noreferrer\">here<\/a> and <a href=\"https:\/\/github.com\/nerdralph\/ArduinoShrink\"  rel=\"noopener noreferrer\">ArduinoShrink on GitHub<\/a>.<\/p><div><\/div><div><\/div><div><\/div>\n<span><!-- --><\/span>","protected":false},"excerpt":{"rendered":"<p>Nerd Ralph blogs about shrinking the Arduino core for ATmega168 and 328 MCUs at 4 to 16 MHz:One of m&#8230;<\/p>\n<p> <a href=\"https:\/\/pappp.net\/?p=45603\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[226],"tags":[],"class_list":["post-45603","post","type-post","status-publish","format-standard","hentry","category-news-2"],"_links":{"self":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/45603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45603"}],"version-history":[{"count":0,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/45603\/revisions"}],"wp:attachment":[{"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}