File Exchange Pick of the Week

Our best user submissions

Generate Code for All the Things?

Posted by Greg Wolff,

Greg’s pick this week is A short introduction on using model based design approach in Embedded System by TAB.

Imagine a quiet summer evening at the beach.  There’s a warm fire crackling in the cool offshore breeze, and everyone is sitting around enjoying the company of friends, listening to the waves breaking in the twilight.

Too bad you’re not there. You were supposed to be, but this project came along that just had to be done.  “Of course, I’ll do this. I know how to program.  I took a course in C-code in college”

Now you find yourself in the cube trying to remember how to debug your pointer math to see if you are dereferencing the right location in memory.

“There has to be a better way to do this! Haven’t the robots figured out how to do this stuff yet?”

In a moment of frustration and curiosity you enter “automatic c code” into the search engine, and out pops “Automatically Generate your Embedded Code from Simulink”

“I’M SAVED!” you think. “I know Simulink, I already have the controller in a Simulink model”

Saved from Pointer Math

Arguably the key element that enables a model-based approach to design is the ability to automatically convert a high-level design description to language that can be implemented on hardware.

In 2006, code generation from Simulink enabled me to complete my master’s thesis and graduate. (I won’t say graduate “on-time”. That embellishment is taking things too far).

Well that, and the fixed-point math magician Jeff Miller.

Soooo… No More C-Code?

We should abandon writing C-code for our microprocessors by hand and just use Simulink to describe our algorithm, and let code generation to the rest… right?

No! We still need our software engineering skills.

Automatic code generation is excellent for translating designs that are simulated in Simulink to embeddable code.

In reality, most cases this is a subset of the overall embedded system design. There’re device drivers, hardware interrupts, serial communications that use some obnoxiously weird custom protocol because Bill needed to make sure he would be fully employed until he reached retirement age.

Though, it would be nice have our existing designs correctly translated into C-code. Then we could be on the beach already!

Make Learning Easy by Keeping it Simple

TAB has a nice, simple, write-up that describes where Simulink and C-code generation can fit into embedded systems development

TAB lays out a simple software architecture and algorithm to describe standard embedded software development practices and discusses the process of integrating automatically generated C-code with an existing embedded software application.

Perhaps this is an area of improvement for us here at MathWorks – make this process simpler to understand, and not so geared to power users.

The Value of Code Generation is Simulation

My focus as an Application Engineer is C-code generation from Simulink. I get a lot of questions about things like, “Will the code be efficient?” “Will it be too big to fit on my processor?” “Can I fire my software engineers?”

All of those are very much dependent on lots of factors.

One question that is much more definitive to answer is “What should I put in my Simulink model?”

Should I include device drivers, existing software code, everything?

My response: “Include what provides value to you in simulation”.

The key value of code generation isn’t so much the fact it can write C-code, but rather the C-code will accurately reflect a design that you have developed and tested using simulation.

Maybe that won’t save you from missing your weekend at the beach with friends. There are still lots of things that can go awry in the design process.  But it can certainly help mitigate that risk.

Is Code Generation and Model-Based Design Only for C-code?

In short: No.
Simulink models support generating:

  • C for deploying to microprocessors and desktop machines
  • C++ interfaces (for integrating with C++ projects)
  • VHDL, Verilog for deploying to FPGAs
  • Structured Text for deploying to PLCs

In addition, MATLAB Code supports generation of CUDA for deploying to GPUs

What do you think?

Let us know here.

31 views (last 30 days)  | |

Comments

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