How to get multiple (reference) models using the same c-code (RAM instance) variables during simulation.
1 view (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/312641/image.jpeg)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/312644/image.jpeg)
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 Comments
James Tursa
on 8 Jun 2020
Edited: James Tursa
on 8 Jun 2020
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).
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!