File Exchange Pick of the Week

Our best user submissions

Device Drivers

Richard is a Consultant at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace industry.

Richard's pick this week is Device Drivers by Giampiero Campa.


My pick this week is Developing Simulink Device Driver Blocks: Step-by-Step Guide and Examples .

One of the many capabilities of Simulink is the ability to generate code for an embedded application. However, when hardware is involved, the complexity goes up exponentially. As with any embedded system, the core algorithm must interface with hardware components to be effective. These can range from simple I/O, to complex sensors such as a GPS. In addition, there are myriad communication channels that can be used such as serial, A2D/D2A, and other communication protocols (i.e. I2C). In all of these cases, there are some low-level hardware routines that provide a bridge between the hardware and the core algorithm - device drivers.

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 by hand. This is not very efficient and not recommended. Another would be to write the base code for the application by-hand, including the interface to the hardware, and calling the algorithmic code produced via Simulink. In reality, this is an approach adobted by many users, particularly where it is a highly complex system with many aspects, with a limited number being generated from Simulink. However, for our case, we want to be able to generate code from our Simulink model and run it directly on our hardware, especially if we are using the Run on Target Hardware feature in Simulink rather than using one of the Coder products. To do this, we need a block in Simulink that will translate into a call to the appropriate device driver.

That is where this post comes into play. This post walks you through the steps needed to create these device driver blocks. In particular, the post provides information on the various approaches that can be used such as S-Function blocks and MATLAB Function blocks, and the pros/cons of each approach. The post provides examples for creating blocks for an Arduino board, but the approach is applicable to any board. This is supported by the number of additional posts that were inspired by this post for a wide range of boards and applications.

Once a device driver block is created, the user can place it in a Simulink library for reuse. In this way, various device driver blocks can be packaged and shared for use in different applications. Many of the Target Support Packages leverage this capability.


If you need to interface with hardware, this post can provide the necessary guidance to allow you to create a set of hardware specific blocks. Give it a try and let us know what you think here or leave a comment for Giampiero.

Published with MATLAB® R2016a

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.