How to get multiple (reference) models using the same c-code (RAM instance) variables during simulation.
이전 댓글 표시
I am struggling to setup a model that uses a reference model such that when they call c-code, they are accessing the same RAM (variable instances). The hierarchy is as follows:

In both "main_model" and "function_model" the Simulation Target is set to use "interface.[ch]", which very simply contains an array of 4 structures to allow both global and accessor functions for access. The header is as follows:
typedef struct
{
bool enabled;
uint32_t value;
} counter_t;
counter_t counter_list[4];
#define COUNTER_0 ((counter_t *)&(counter_list[0]))
#define COUNTER_1 ((counter_t *)&(counter_list[1]))
#define COUNTER_2 ((counter_t *)&(counter_list[2]))
#define COUNTER_3 ((counter_t *)&(counter_list[3]))
void initialize(void);
void counter_enable(counter_t *ptr);
void counter_disable(counter_t *ptr);
uint32_t counter_get_current(counter_t *ptr);
void counter_reset(counter_t *ptr);
I do understand in regards to C-code, choose either the global access or accessor functions, however, this scenerio is setup to mimic that of a hardware and firmware relationship special function register (SFR) access to something like an input compare using vendor supplied packages (such as NXP or STM) and maintain the call footprint for code generation.
The problem is when this setup is run, the "main_model" and the "function_model" each have their own internal values for "counter_list" rather than sharing a single instance of them. The "main_model" will initialize the counter value to 100 and the "function_model" increments by 1 per function call (occuring every 0.1 seconds). Looking at the values during simulation from both the "main_model" and "function_model" the following is observed:

The expectation would be that they both have the same value, starting at 100 and incrementing up to 200 over the 10 seconds. Is this possible? If so, how what am I missing? or help point me to the proper documentation (I haven't been able to find this in the user guide... yet). I've also attached the project and all files (2019b). Appreciate any guidance, thanks!
댓글 수: 7
James Tursa
2020년 6월 8일
I haven't looked at your setup code, but I would note this this code
#define COUNTER_0 ((counter_t *)&(counter_list[0]))
#define COUNTER_1 ((counter_t *)&(counter_list[1]))
#define COUNTER_2 ((counter_t *)&(counter_list[2]))
#define COUNTER_3 ((counter_t *)&(counter_list[3]))
is basically the convoluted equivalent of this code
#define COUNTER_0 counter_list
#define COUNTER_1 (counter_list+1)
#define COUNTER_2 (counter_list+2)
#define COUNTER_3 (counter_list+3)
James Tursa
2020년 6월 8일
I am reluctant to open a zip file. Can you post relevant parts of code that shows where counter_list lives at the top level, and how the function(s) are called and with what inputs?
Jeremy Quandt
2020년 6월 8일
James Tursa
2020년 6월 8일
I don't see how the functions are being called. Do you have multiple source files that are including interface.h? Where is the code that is calling these functions?
Jeremy Quandt
2020년 6월 8일
James Tursa
2020년 6월 8일
편집: James Tursa
2020년 6월 8일
So main_model and function_model are two different s-functions? That is, you have two different sets of source code that include interface.h, and these two different sets of source code inlude interface.h? If that is the case, then yes the s-functions do not share anything. You would have to set up communication between them at the model level (e.g., have main_model feed function_model the starting value).
Jeremy Quandt
2020년 6월 8일
편집: Jeremy Quandt
2020년 6월 8일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Simulink Coder에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










