How to import C #defines into MATLAB/ Simulink

조회 수: 14 (최근 30일)
Christopher Hack
Christopher Hack 2015년 6월 8일
답변: Jianning Dong 2018년 9월 24일
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

답변 (2개)

Drew Davis
Drew Davis 2015년 6월 9일
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
Christopher Hack
Christopher Hack 2015년 6월 9일
Drew,
Thank you for your reply. Unfortunately, this doesn't solve the problem I'm having of actually including the pre-defined macro in the simulink build. When I include
#define TIMER0
In the location you provided then try to generate the code for the model, I get the error
Invalid setting in 'GenTest/Constant34' for parameter 'Value'.
Error evaluating parameter 'Value' in 'GenTest/Constant34'
Undefined function or variable 'TIMER0'.
This is essentially the same problem that I am trying to rectify, as I want to use the pre-defined macro as one of the inputs in my state charts.
Specifically, I have a function that takes the Timer type as an input to determine which timer we're referring to. I want the generated code to pass that defined constant as a parameter to the custom C function so that it works correctly. Is this something that is possible in the current context?
Christopher Hack
Christopher Hack 2015년 6월 9일
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);

댓글을 달려면 로그인하십시오.


Jianning Dong
Jianning Dong 2018년 9월 24일
Is the problem solved? I run into the same problem.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by