Writing Maltab code for code generation when using structures and custom data types

1 view (last 30 days)
Hi all,
I am currently trouble with embedded coder. I would like to generate a C header file that looks a bit like this: foo.h
ifndef _foo_INCLUDED
#define _foo_INCLUDED
#include "custom_types.h"
typedef struct fooDef
{
custom_uint32_t variable1;
custom_float32_t variable2;
}fooStr;
...
extern void fooSet(fooStr *paramPtr);
extern void fooReset(fooStr *paramPtr);
...
Similarly the source file will look like: foo.c
#include "custom_types.h"
#include "foo.h"
...
void fooSet(fooStr *paramPtr)
{
paramPtr->variable1 = 30;
paramPtr->variable2 = 5.0;
}
void fooReset(fooStr *paramPtr)
{
}
...
However I am not sure how to write (/architect) my Matlab code to produce this result. I am using coder.opaque and coder.cstructname.
variable1 = coder.opaque('custom_uint32_t', '0', 'HeaderFile', '"custom_types.h"');
variable2 = coder.opaque('custom_float32_t', '0.0', 'HeaderFile', '"custom_types.h"');
fooStr = struct( 'variable1', uint32(zeros(1,1)),...
'variable2', single(zeros(1,1)));
coder.cstructname(fooStr, 'fooStr');
Could someone comment or link to a guide please?
I am not sure if I need a number of Matlab functions and how to split them up.
I suppose I need more than one Matlab function to produce this result but can't tell how.
I thought it would be relatively straitforward but it's troubling me and looking for some online guide for help. Any help much appreciated.
Thanks Alex

Answers (1)

Mordechai Rorvig
Mordechai Rorvig on 31 Aug 2018
You were close here, but you cannot set or access variables that you declare with coder.opaque. Use coder.opaque for passing variables in and out of coder.ceval (external C function) calls.
What you can do is define something like the following:
function [myfooStr] = foo
x = struct('p',int32(0));
coder.cstructname(x,'custom_uint32_t');
y = struct('p', single(0));
coder.cstructname(y,'custom_float32_t');
myfooStr.variable1 = x;
myfooStr.variable2 = y;
coder.cstructname(myfooStr,'fooStr');
Then, you find the type definition as expected in the generated file, foo_types.h:
typedef struct {
custom_uint32_t variable1;
custom_float32_t variable2;
} fooStr;

Community Treasure Hunt

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

Start Hunting!