{"id":6870,"date":"2016-04-29T09:00:37","date_gmt":"2016-04-29T13:00:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6870"},"modified":"2016-12-22T11:37:25","modified_gmt":"2016-12-22T16:37:25","slug":"device-drivers","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/29\/device-drivers\/","title":{"rendered":"Device Drivers"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n\r\n      <p><i>Richard is a Consultant at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace\r\n            industry.<\/i><\/p>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\">Richard<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers\">Device Drivers<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1692307-giampiero-campa\">Giampiero Campa<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Pick<a name=\"1\"><\/a><\/h3>\r\n   <p>My pick this week is <b><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers\">Developing Simulink Device Driver Blocks: Step-by-Step Guide and Examples<\/a><\/b> .\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/arduino.png\"> <\/p>\r\n   <p>One of the many capabilities of Simulink is the ability to generate code for an embedded application.  However, when hardware\r\n      is involved, the complexity goes up exponentially.  As with any embedded system, the core algorithm must interface with hardware\r\n      components to be effective. These can range from simple I\/O, to complex sensors such as a GPS.  In addition, there are myriad\r\n      communication channels that can be used such as serial, A2D\/D2A, and other communication protocols (i.e. I2C).  In all of\r\n      these cases, there are some low-level hardware routines that provide a bridge between the hardware and the core algorithm\r\n      - device drivers.\r\n   <\/p>\r\n   <p>That being said, the issue then becomes how do you call these low-level routines?  One way would be to write all of the code\r\n      by hand.  This is not very efficient and not recommended.  Another would be to write the base code for the application by-hand,\r\n      including the interface to the hardware, and calling the algorithmic code produced via Simulink.  In reality, this is an approach\r\n      adobted by many users, particularly where it is a highly complex system with many aspects, with a limited number being generated\r\n      from Simulink.  However, for our case, we want to be able to generate code from our Simulink model and run it directly on\r\n      our hardware, especially if we are using the Run on Target Hardware feature in Simulink rather than using one of the Coder\r\n      products. To do this, we need a block in Simulink that will translate into a call to the appropriate device driver.\r\n   <\/p>\r\n   <p>That is where this post comes into play.  This post walks you through the steps needed to create these device driver blocks.\r\n       In particular, the post provides information on the various approaches that can be used such as S-Function blocks and MATLAB\r\n      Function blocks, and the pros\/cons of each approach. The post provides examples for creating blocks for an Arduino board,\r\n      but the approach is applicable to any board. This is supported by the number of additional posts that were inspired by this\r\n      post for a wide range of boards and applications.\r\n   <\/p>\r\n   <p>Once a device driver block is created, the user can place it in a Simulink library for reuse.  In this way, various device\r\n      driver blocks can be packaged and shared for use in different applications.  Many of the Target Support Packages leverage\r\n      this capability.\r\n   <\/p>\r\n   <h3>Comments<a name=\"2\"><\/a><\/h3>\r\n   <p>If you need to interface with hardware, this post can provide the necessary guidance to allow you to create a set of hardware\r\n      specific blocks. Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6870#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers#comments\">comment<\/a> for Giampiero.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_3cfdb5bd5bbf47e8b87dee65848e7459() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='3cfdb5bd5bbf47e8b87dee65848e7459 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3cfdb5bd5bbf47e8b87dee65848e7459';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Richard Ruff';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_3cfdb5bd5bbf47e8b87dee65848e7459()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p>\r\n<\/div>\r\n<!--\r\n3cfdb5bd5bbf47e8b87dee65848e7459 ##### SOURCE BEGIN #####\r\n%% Device Driver\r\n%\r\n% Posted by *Richard Ruff* , April 29, 2016\r\n%\r\n% _Richard is a Consultant at MathWorks focused on the Embedded\r\n% Coder product for code generation, primarily in the Aerospace industry._\r\n% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\r\n% Richard's> pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers Device Drivers> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1692307-giampiero-campa Giampiero Campa>.\r\n% \r\n%% Pick\r\n% My pick this week is\r\n% *<https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers\r\n% Developing Simulink Device Driver Blocks: Step-by-Step\r\n% Guide and Examples>* .\r\n%\r\n% <<arduino.png>>\r\n%\r\n% One of the many capabilities of Simulink is the ability to generate code\r\n% for an embedded application.  However, when hardware is involved, the\r\n% complexity goes up exponentially.  As with any embedded system, the core\r\n% algorithm must interface with hardware components to be effective. These\r\n% can range from simple I\/O, to complex sensors such as a GPS.  In\r\n% addition, there are myriad communication channels that can be used such\r\n% as serial, A2D\/D2A, and other communication protocols (i.e. I2C).  In all\r\n% of these cases, there are some low-level hardware routines that provide a\r\n% bridge between the hardware and the core algorithm - device drivers.\r\n%\r\n% That being said, the issue then becomes how do you call these low-level\r\n% routines?  One way would be to write all of the code by hand.  This is\r\n% not very efficient and not recommended.  Another would be to write the\r\n% base code for the application by-hand, including the interface to the\r\n% hardware, and calling the algorithmic code produced via Simulink.  In\r\n% reality, this is an approach adobted by many users, particularly where it\r\n% is a highly complex system with many aspects, with a limited number being\r\n% generated from Simulink.  However, for our case, we want to be able to\r\n% generate code from our Simulink model and run it directly on our\r\n% hardware, especially if we are using the Run on Target Hardware feature\r\n% in Simulink rather than using one of the Coder products. To do this, we\r\n% need a block in Simulink that will translate into a call to the\r\n% appropriate device driver.\r\n%\r\n% That is where this post comes into play.  This post walks you through the\r\n% steps needed to create these device driver blocks.  In particular, the\r\n% post provides information on the various approaches that can be used such\r\n% as S-Function blocks and MATLAB Function blocks, and the pros\/cons of\r\n% each approach. The post provides examples for creating blocks for an\r\n% Arduino board, but the approach is applicable to any board. This is\r\n% supported by the number of additional posts that were inspired by this\r\n% post for a wide range of boards and applications.\r\n%\r\n% Once a device driver block is created, the user can place it in a\r\n% Simulink library for reuse.  In this way, various device driver blocks\r\n% can be packaged and shared for use in different applications.  Many of\r\n% the Target Support Packages leverage this capability.\r\n%\r\n\r\n%% Comments\r\n% \r\n% If you need to interface with hardware, this post can provide the\r\n% necessary guidance to allow you to create a set of hardware specific\r\n% blocks. Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6870#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers#comments\r\n% comment> for Giampiero.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 3cfdb5bd5bbf47e8b87dee65848e7459\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/arduino.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n\r\n      Richard is a Consultant at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace\r\n            industry.\r\n      Richard's pick this week... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/29\/device-drivers\/\">read more >><\/a><\/p>","protected":false},"author":131,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,24],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6870"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6870"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6870\/revisions"}],"predecessor-version":[{"id":8215,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6870\/revisions\/8215"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}