MATLAB: How to get multiple (reference) models using the same c-code (RAM instance) variables during simulation.

reference modelssimulation targetsimulinksimulink coder

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!

Best Answer

I was able to get this working. It wasn't explicitly in the reference manual, but I was able to draw the resolve from some lines of an example. In a nutshell, all the variables are moved internal to the code module (i.e. into the source) and declared statically. The source module then becomes as follows:
interface.h
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef INTERFACE_H
#define INTERFACE_H
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
bool enabled;
uint32_t value;
} counter_t;
#define COUNTER_0 counter_object(0)
#define COUNTER_1 counter_object(1)
#define COUNTER_2 counter_object(2)
#define COUNTER_3 counter_object(3)
extern void initialize(void);
extern void counter_enable(counter_t *ptr);
extern void counter_disable(counter_t *ptr);
extern uint32_t counter_get_current(counter_t *ptr);
extern void counter_reset(counter_t *ptr);
extern counter_t *counter_object(uint8_t id);
#endif // INTERFACE_H
#ifdef __cplusplus
}
#endif
interface.c
#include <string.h>
#include "interface.h"
static counter_t counter_list[4];
void initialize(void)
{
memset(&counter_list, 0, sizeof(counter_list));
}
void counter_enable(counter_t *ptr)
{
if (NULL != ptr)
{
ptr->enabled = true;
}
}
void counter_disable(counter_t *ptr)
{
if (NULL != ptr)
{
ptr->enabled = false;
}
}
uint32_t counter_get_current(counter_t *ptr)
{
if (NULL != ptr)
{
return (ptr->value);
}
else
{
return (0);
}
}
void counter_reset(counter_t *ptr)
{
if (NULL != ptr)
{
ptr->value = 0;
}
}
counter_t *counter_object(uint8_t id)
{
if (id < 4)
{
return (&(counter_list[id]));
}
else
{
return (NULL);
}
}
All the syntax in the state charts then operate identical.
Related Question