How to use structure pointer as an argument of function in code generation?

23 次查看(过去 30 天)
First, I defined several bus signals in workspace, and I know these bus signals would be generated as structs in code generation.
For example, I defined a bus signal named PARA, then I added some bus elements like A, B, C to PARA. Eventually, in code generation, SIMULINK would generate
typedef struct{
int32 A;
int32 B;
int32 C;
} PARA;
And then it would define
PARA PARA_T;
My question is, how can I use the pointer of struct PARA_T to be an argument of a function, and to be clear, this function is being generated by SIMULINK code generation through a subsystem.
For example, I created a subsystem and set his code generation page as reusable function and name LPF, and so on.
Then in the code generation process, I want this subsystem to be code generated as
void LPF (PARA *k)
{
K->C = K->A + K->B;
}
And then in the main.c
LPF(&PARA_T);
My current strategy is like this,
I defined some bus signals in workspace like Bus Motor, and then I used some Data Store Memory modules and set their Date Type as Bus Motor.
Finally I used Data Store Read modules to read these data in Bus signals ( which were structs in code generation. ).
But this whole thing doesn't work.
So, how should I build and set these modules, then I can get functions which their arguments are pointers of structs, in code generation, like I said before?

回答(5 个)

Mark McBroom
Mark McBroom 2019-3-22
You should be able to get a pointer to the struct passed to your function if you configure the subsystem to be atomic and set the code generation properties to be reusable subsystem. See this example.
  1 个评论
Chess
Chess 2019-3-22
编辑:Chess 2019-3-22
I read this example before, but how should I be able to get a pointer to the struct passed to my funtion?
I know that in code generation all the Bus Signals will be generated as structs, so how can I get pointers that contain the address of those structs?
And in this example, the generated code is
myfun(rtwdemo_ssreuse_U.In1, rtwdemo_ssreuse_U.In2, &rtwdemo_ssreuse_B.SS2,
rtwdemo_ssreuse_P.T2Data, rtwdemo_ssreuse_P.T2Break);
and I want it to be like this
myfun(&rtwdemo_ssreuse_U, &rtwdemo_ssreuse_B.SS2, &rtwdemo_ssreuse_P);
I mean rtwdemo_ssreuse_P.T2Data and rtwdemo_ssreuse_P.T2Break belong to the same struct, I don't have to pass them directly to myfun. I could just pass the address of this struct to myfun. It would be a little more concise and efficient, wouldn't it?
Anyway thanks a lot, buddy.

请先登录,再进行评论。


Chess
Chess 2019-3-26
PLEASE HELP

Chess
Chess 2019-4-2
...

David Missael Maciel Morones
编辑:David Missael Maciel Morones 2019-8-9
I really need this answer as well and I'm desesperate. Somewhere in a forum I read that embedded coder cannot translate any simulink model as pointer of a structure due to the data-flow rules. Having a pointer to a structure you can theorical write an input. That's why is not allowed.
For me this is stupid because in embedded programming working with pointers is one of the most commonly practices.
  1 个评论
Emanuele Pancani
Emanuele Pancani 2019-12-24
I agree with you! It is stupid!
I'm a firmware designer and I work on DSP programming since 2006.
I started to use Simulink models for code generation 3 years ago and this is one of the major issue I faced. In my projects, genereted code works with hand written code and it is normal having arguments passed to function by pointers. Moreover it is normal having "in-out" arguments; I mean that an arguments passed by reference could be changed by the function itself.
Another stupid thing is that you could not have a pointer field in a bus or in a struct storage class...Why?
I hope that mathworks will solve this issue in future releases; I would like to have more options to manage storage classes.
In some cases I used the legacy code tool to import hand written C code; in those cases you should be able to pass arguments by pointer

请先登录,再进行评论。


Vit Valek
Vit Valek 2020-1-30
Hello Chess,
I have solution for your issue. First, it's necessary to create bus in Matlab (it will be structure in generated C code). Then you must create reusable subsystem. I attached 2 files - script (for create structure) and model (with correct configuration). I hope it will be helpful.
Vit

类别

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