How to import C #defines into MATLAB/ Simulink

6 views (last 30 days)
Christopher Hack
Christopher Hack on 8 Jun 2015
Answered: Jianning Dong on 24 Sep 2018
Hi,
I am trying to use Simulink and MATLAB coder to generate C code that I will use as MCU firmware on a product. I'm running into a couple of issues doing this, however. For the actual hardware interaction, I'm using some hand-written C functions, which I'm calling by using Simulink generated S-functions (I used the S-Function builder to create them). Some of these functions were written by the MCU manufacturer and they rely on things like #defines which occur in specific header files in order to work. Those defines are used in the function calls.
What I would like to be able to do is import the #defines into MATLAB so that when the code is generated it uses them appropriately. For instance, if I want to call the function
InitTimerFor1mSTimer(HFRCO_SpeedHz, Timer[0]);
and I want the MATLAB variable Timer[0] to contain TIMER0, TIMER1, etc.
I have defined the constants in a type def, however I get errors at compile time complaining that TIMER0 is undefined. Here is the typedef I'm referring to:
classdef TimerTypeDefs
properties(Constant)
Timer0 = TIMER0;
Timer1 = TIMER1;
Timer2 = TIMER2;
Timer3 = TIMER3;
end
end
Any ideas how to achieve this? Thanks in advance for your help.
-Chris Hack

Answers (2)

Drew Davis
Drew Davis on 9 Jun 2015
The best way to do this is to use the custom code settings in a Simulink model to add any custom macros you may want to add. For instance if you wanted to add a #define macro to the generated header file, you can add the following line of code to "Model Configuration Parameters -> Code Generation -> Custom Code -> Header file":
#define MY_MACRO
  2 Comments
Christopher Hack
Christopher Hack on 9 Jun 2015
To be more specific, the input to the block is TIMER0, and the S-Function should generate this code to call the custom C function:
InitTimerFor1mSTimer(HFRCO_SpeedHz, TIMER0);

Sign in to comment.


Jianning Dong
Jianning Dong on 24 Sep 2018
Is the problem solved? I run into the same problem.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!