{"id":6336,"date":"2017-02-09T16:53:13","date_gmt":"2017-02-09T21:53:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6336"},"modified":"2017-02-09T16:53:13","modified_gmt":"2017-02-09T21:53:13","slug":"using-simulink-functions-to-simulate-hardware-services","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2017\/02\/09\/using-simulink-functions-to-simulate-hardware-services\/","title":{"rendered":"Using Simulink Functions to Simulate Hardware Services"},"content":{"rendered":"<p>This week, I want to introduce another feature that becomes useful when combined with the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/initializefunction.html\">Initialize Function<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/resetfunction.html\">Reset Function<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/terminatefunction.html\">Terminate Function<\/a> blocks.<\/p>\n<p>In R2014b, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulinkfunction.html\">Simulink Functions<\/a> got introduced. In <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/12\/11\/simulink-functions-intro\">this post<\/a>, we highlighted how Simulink Functions can be used to create an <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/export-function-models.html\">Export-Function model<\/a>. In this case, the Simulink Functions are placed inside a model. In simulation, you can reference the model and call the functions using the Function Caller block.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/functionLibrary.png\" alt=\"Simulink functions used as function library\" \/><\/p>\n<p>You can then generate code for the function library model and call those functions in any way you want from hand-written code.<\/p>\n<p><strong>The opposite<\/strong><\/p>\n<p>One thing we did not mention at that time, is that it is possible to use Simulink Functions and the Function Caller block in completely opposite way, where the referenced model has a Function Caller block, calling Simulink Functions present in the top model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/simulateFunction.png\" alt=\"Simulation harness\" \/><\/p>\n<p>Why would someone want to do that? In short, the answer is to simulate custom code that is not directly available for simulation. Let's see how this works.<\/p>\n<p><strong>Using the Function Caller block to call external code<\/strong><\/p>\n<p>If we generate code for the child codeGenModel.slx in the above picture model, the code will look like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/callerCode.png\" alt=\"Generated Code\" \/><\/p>\n<p>By default, this code would not build, since the compiler would not know where to find the function <tt>timesTwo<\/tt>. However, if you configure your model properly, the function <tt>timesTwo<\/tt> can come from anywhere you want. For example, it could be in a dynamic library against which you will link to on your embedded target. For this example, imagine that there is a <tt>timesTwo<\/tt> service available in this <tt>timesTwo.c<\/tt> file:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/timesTwo.png\" alt=\"Custom Code\" \/><\/p>\n<p>In the model configuration, I specify that this file should be included in the build process:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/modelConfigSourceCode.png\" alt=\"Custom Code Configuration\" \/><\/p>\n<p>This allows me to generate an executable that calls my custom <tt>timesTwo.c<\/tt> implementation.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>To summarize, the idea is to create a Simulink Function that emulates the behavior of external software. You can build a simulation harness model that references your code generation model and it will see the Simulink Function. When time comes to generate code for the child model, it will have no idea the Simulink Function exists,  but instead will link against any external code you specify.<\/p>\n<p>It is important to note that this technique is just one way to include custom code in the code generated from Simulink.<\/p>\n<p>If the custom code was available on the host, I would recommend going with an <a href=\"https:\/\/www.mathworks.com\/help\/rtw\/ug\/write-s-function-and-tlc-files-by-hand.html#responsive_offcanvas\">S-Function<\/a> to wrap and reuse the same custom code both in simulation and code generation.<\/p>\n<p>However if the code is unavailable, for example because it is available as an OS service on the target embedded processor, this approach can be interesting.<\/p>\n<p><strong>What's next?<\/strong><\/p>\n<p>Now that we have introduced the concepts of <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/12\/05\/export-function-models\/\">Export Function models<\/a>, the <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/01\/20\/state-reader-and-state-writer-blocks\/\">State Reader and Writer blocks<\/a>, and this way of using Simulink Functions, I believe we have all the pieces to make a realistic example illustrating how we see the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/initializefunction.html\">Initialize Function<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/resetfunction.html\">Reset Function<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/terminatefunction.html\">Terminate Function<\/a> blocks being used... next week.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q1\/modelConfigSourceCode.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>This week, I want to introduce another feature that becomes useful when combined with the Initialize Function, Reset Function and Terminate Function blocks.<br \/>\nIn R2014b, Simulink Functions got... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/02\/09\/using-simulink-functions-to-simulate-hardware-services\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,24],"tags":[492,491,490],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6336"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=6336"}],"version-history":[{"count":28,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6336\/revisions"}],"predecessor-version":[{"id":6375,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6336\/revisions\/6375"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}