-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfbuilder_bus.c
More file actions
470 lines (408 loc) · 15 KB
/
sfbuilder_bus.c
File metadata and controls
470 lines (408 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* File: sfbuilder_bus.c
*
*
*
* --- THIS FILE GENERATED BY S-FUNCTION BUILDER: 3.0 ---
*
* This file is an S-function produced by the S-Function
* Builder which only recognizes certain fields. Changes made
* outside these fields will be lost the next time the block is
* used to load, edit, and resave this file. This file will be overwritten
* by the S-function Builder block. If you want to edit this file by hand,
* you must change it only in the area defined as:
*
* %%%-SFUNWIZ_defines_Changes_BEGIN
* #define NAME 'replacement text'
* %%% SFUNWIZ_defines_Changes_END
*
* DO NOT change NAME--Change the 'replacement text' only.
*
* For better compatibility with the Simulink Coder, the
* "wrapper" S-function technique is used. This is discussed
* in the Simulink Coder's Manual in the Chapter titled,
* "Wrapper S-functions".
*
* -------------------------------------------------------------------------
* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
* -------------------------------------------------------------------------
* Created: Fri Aug 16 18:25:11 2013
*
*
*/
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfbuilder_bus
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/* %%%-SFUNWIZ_defines_Changes_BEGIN --- EDIT HERE TO _END */
#define NUM_INPUTS 2
/* Input Port 0 */
#define IN_PORT_0_NAME u0
#define INPUT_0_WIDTH 1
#define INPUT_DIMS_0_COL 1
#define INPUT_0_DTYPE real_T
#define INPUT_0_COMPLEX COMPLEX_NO
#define IN_0_FRAME_BASED FRAME_NO
#define IN_0_BUS_BASED 1
#define IN_0_BUS_NAME SFB_COUNTERBUS
#define IN_0_DIMS 1-D
#define INPUT_0_FEEDTHROUGH 1
#define IN_0_ISSIGNED 0
#define IN_0_WORDLENGTH 8
#define IN_0_FIXPOINTSCALING 1
#define IN_0_FRACTIONLENGTH 9
#define IN_0_BIAS 0
#define IN_0_SLOPE 0.125
/* Input Port 1 */
#define IN_PORT_1_NAME u1
#define INPUT_1_WIDTH 1
#define INPUT_DIMS_1_COL 1
#define INPUT_1_DTYPE int32_T
#define INPUT_1_COMPLEX COMPLEX_NO
#define IN_1_FRAME_BASED FRAME_NO
#define IN_1_BUS_BASED 0
#define IN_1_BUS_NAME
#define IN_1_DIMS 1-D
#define INPUT_1_FEEDTHROUGH 1
#define IN_1_ISSIGNED 0
#define IN_1_WORDLENGTH 8
#define IN_1_FIXPOINTSCALING 1
#define IN_1_FRACTIONLENGTH 9
#define IN_1_BIAS 0
#define IN_1_SLOPE 0.125
#define NUM_OUTPUTS 2
/* Output Port 0 */
#define OUT_PORT_0_NAME y0
#define OUTPUT_0_WIDTH 1
#define OUTPUT_DIMS_0_COL 1
#define OUTPUT_0_DTYPE real_T
#define OUTPUT_0_COMPLEX COMPLEX_NO
#define OUT_0_FRAME_BASED FRAME_NO
#define OUT_0_BUS_BASED 1
#define OUT_0_BUS_NAME SFB_COUNTERBUS
#define OUT_0_DIMS 1-D
#define OUT_0_ISSIGNED 1
#define OUT_0_WORDLENGTH 8
#define OUT_0_FIXPOINTSCALING 1
#define OUT_0_FRACTIONLENGTH 3
#define OUT_0_BIAS 0
#define OUT_0_SLOPE 0.125
/* Output Port 1 */
#define OUT_PORT_1_NAME y1
#define OUTPUT_1_WIDTH 1
#define OUTPUT_DIMS_1_COL 1
#define OUTPUT_1_DTYPE int32_T
#define OUTPUT_1_COMPLEX COMPLEX_NO
#define OUT_1_FRAME_BASED FRAME_NO
#define OUT_1_BUS_BASED 0
#define OUT_1_BUS_NAME
#define OUT_1_DIMS 1-D
#define OUT_1_ISSIGNED 1
#define OUT_1_WORDLENGTH 8
#define OUT_1_FIXPOINTSCALING 1
#define OUT_1_FRACTIONLENGTH 3
#define OUT_1_BIAS 0
#define OUT_1_SLOPE 0.125
#define NPARAMS 0
#define SAMPLE_TIME_0 INHERITED_SAMPLE_TIME
#define NUM_DISC_STATES 0
#define DISC_STATES_IC [0]
#define NUM_CONT_STATES 0
#define CONT_STATES_IC [0]
#define SFUNWIZ_GENERATE_TLC 1
#define SOURCEFILES "__SFB__"
#define PANELINDEX 6
#define USE_SIMSTRUCT 0
#define SHOW_COMPILE_STEPS 1
#define CREATE_DEBUG_MEXFILE 0
#define SAVE_CODE_ONLY 0
#define SFUNWIZ_REVISION 3.0
/* %%%-SFUNWIZ_defines_Changes_END --- EDIT HERE TO _BEGIN */
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
#include "simstruc.h"
#include "sfbuilder_bus_bus.h"
/*
* Code Generation Environment flag (simulation or standalone target).
*/
static int_T isSimulationTarget;
/* Utility function prototypes. */
static int_T GetRTWEnvironmentMode(SimStruct *S);
/* Macro used to check if Simulation mode is set to accelerator */
#define isDWorkPresent !(ssRTWGenIsCodeGen(S) && !isSimulationTarget)
extern void sfbuilder_bus_Outputs_wrapper(const SFB_COUNTERBUS *u0,
const int32_T *u1,
SFB_COUNTERBUS *y0,
int32_T *y1);
/*====================*
* S-function methods *
*====================*/
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
DECL_AND_INIT_DIMSINFO(inputDimsInfo);
DECL_AND_INIT_DIMSINFO(outputDimsInfo);
ssSetNumSFcnParams(S, NPARAMS);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
ssSetNumContStates(S, NUM_CONT_STATES);
ssSetNumDiscStates(S, NUM_DISC_STATES);
if (!ssSetNumInputPorts(S, NUM_INPUTS)) return;
/*Input Port 0 */
/* Register SFB_COUNTERBUS datatype for Input port 0 */
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY)
{
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "SFB_COUNTERBUS", &dataTypeIdReg);
if(dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetInputPortDataType(S,0, dataTypeIdReg);
}
#endif
ssSetInputPortWidth(S, 0, INPUT_0_WIDTH);
ssSetInputPortComplexSignal(S, 0, INPUT_0_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 0, INPUT_0_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 0, 1); /*direct input signal access*/
ssSetBusInputAsStruct(S, 0,IN_0_BUS_BASED);
ssSetInputPortBusMode(S, 0, SL_BUS_MODE); /*Input Port 1 */
ssSetInputPortWidth(S, 1, INPUT_1_WIDTH); /* */
ssSetInputPortDataType(S, 1, SS_INT32);
ssSetInputPortComplexSignal(S, 1, INPUT_1_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 1, INPUT_1_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 1, 1); /*direct input signal access*/
if (!ssSetNumOutputPorts(S, NUM_OUTPUTS)) return;
/* Output Port 0 */
/* Register SFB_COUNTERBUS datatype for Output port 0 */
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY)
{
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "SFB_COUNTERBUS", &dataTypeIdReg);
if(dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetOutputPortDataType(S,0, dataTypeIdReg);
}
#endif
ssSetBusOutputObjectName(S, 0, (void *) "SFB_COUNTERBUS");
ssSetOutputPortWidth(S, 0, OUTPUT_0_WIDTH);
ssSetOutputPortComplexSignal(S, 0, OUTPUT_0_COMPLEX);
ssSetBusOutputAsStruct(S, 0,OUT_0_BUS_BASED);
ssSetOutputPortBusMode(S, 0, SL_BUS_MODE); /* Output Port 1 */
ssSetOutputPortWidth(S, 1, OUTPUT_1_WIDTH);
ssSetOutputPortDataType(S, 1, SS_INT32);
ssSetOutputPortComplexSignal(S, 1, OUTPUT_1_COMPLEX);
if (ssRTWGenIsCodeGen(S)) {
isSimulationTarget = GetRTWEnvironmentMode(S);
if (isSimulationTarget==-1) {
ssSetErrorStatus(S, " Unable to determine a valid code generation environment mode");
return;
}
isSimulationTarget |= ssRTWGenIsModelReferenceSimTarget(S);
}
/* Set the number of dworks */
if (!isDWorkPresent) {
if (!ssSetNumDWork(S, 0)) return;
} else {
if (!ssSetNumDWork(S, 2)) return;
}
if (isDWorkPresent) {
/*
* Configure the dwork 0 (u0."BUS")
*/
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "SFB_COUNTERBUS", &dataTypeIdReg);
if (dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetDWorkDataType(S, 0, dataTypeIdReg);
}
#endif
ssSetDWorkUsageType(S, 0, SS_DWORK_USED_AS_DWORK);
ssSetDWorkName(S, 0, "u0BUS");
ssSetDWorkWidth(S, 0, DYNAMICALLY_SIZED);
ssSetDWorkComplexSignal(S, 0, COMPLEX_NO);
/*
* Configure the dwork 1 (y0BUS)
*/
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "SFB_COUNTERBUS", &dataTypeIdReg);
if (dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetDWorkDataType(S, 1, dataTypeIdReg);
}
#endif
ssSetDWorkUsageType(S, 1, SS_DWORK_USED_AS_DWORK);
ssSetDWorkName(S, 1, "y0BUS");
ssSetDWorkWidth(S, 1, DYNAMICALLY_SIZED);
ssSetDWorkComplexSignal(S, 1, COMPLEX_NO);
}
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Take care when specifying exception free code - see sfuntmpl_doc.c */
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR |
SS_OPTION_WORKS_WITH_CODE_REUSE));
}
# define MDL_SET_INPUT_PORT_FRAME_DATA
static void mdlSetInputPortFrameData(SimStruct *S,
int_T port,
Frame_T frameData)
{
ssSetInputPortFrameData(S, port, frameData);
}
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* Specifiy the sample time.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, SAMPLE_TIME_0);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart =======================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
/* Bus Information */
slDataTypeAccess *dta = ssGetDataTypeAccess(S);
const char *bpath = ssGetPath(S);
DTypeId SFB_COUNTERBUSId = ssGetDataTypeId(S, "SFB_COUNTERBUS");
DTypeId SFB_LIMITBUSId = ssGetDataTypeId(S, "SFB_LIMITBUS");
DTypeId SFB_SIGNALBUSId = ssGetDataTypeId(S, "SFB_SIGNALBUS");
int_T *busInfo = (int_T *)malloc(6*sizeof(int_T));
if(busInfo==NULL) {
ssSetErrorStatus(S, "Memory allocation failure");
return;
}
/* Calculate offsets of all primitive elements of the bus */
busInfo[0] = dtaGetDataTypeElementOffset(dta, bpath,SFB_COUNTERBUSId,0) +dtaGetDataTypeElementOffset(dta, bpath,SFB_SIGNALBUSId,0);
busInfo[1] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "int32"));
busInfo[2] = dtaGetDataTypeElementOffset(dta, bpath,SFB_COUNTERBUSId,1) +dtaGetDataTypeElementOffset(dta, bpath,SFB_LIMITBUSId,0);
busInfo[3] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "int32"));
busInfo[4] = dtaGetDataTypeElementOffset(dta, bpath,SFB_COUNTERBUSId,1) +dtaGetDataTypeElementOffset(dta, bpath,SFB_LIMITBUSId,1);
busInfo[5] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "int32"));
ssSetUserData(S, busInfo);
}
#endif /* MDL_START */
#define MDL_SET_INPUT_PORT_DATA_TYPE
static void mdlSetInputPortDataType(SimStruct *S, int port, DTypeId dType)
{
ssSetInputPortDataType( S, 0, dType);
}
#define MDL_SET_OUTPUT_PORT_DATA_TYPE
static void mdlSetOutputPortDataType(SimStruct *S, int port, DTypeId dType)
{
ssSetOutputPortDataType(S, 0, dType);
}
#define MDL_SET_DEFAULT_PORT_DATA_TYPES
static void mdlSetDefaultPortDataTypes(SimStruct *S)
{
ssSetInputPortDataType( S, 0, SS_DOUBLE);
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
}
#define MDL_SET_WORK_WIDTHS
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
static void mdlSetWorkWidths(SimStruct *S)
{
/* Set the width of DWork(s) used for marshalling the IOs */
if (isDWorkPresent) {
/* Update dwork 0 */
ssSetDWorkWidth(S, 0, ssGetInputPortWidth(S, 0));
/* Update dwork 1 */
ssSetDWorkWidth(S, 1, ssGetOutputPortWidth(S, 0));
}
}
#endif
/* Function: mdlOutputs =======================================================
*
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
const char *u0 = (char *) ssGetInputPortSignal(S,0);
const int32_T *u1 = (const int32_T*) ssGetInputPortSignal(S,1);
char *y0 = (char *) ssGetOutputPortSignal(S,0);
int32_T *y1 = (int32_T *)ssGetOutputPortRealSignal(S,1);
int_T* busInfo = (int_T *) ssGetUserData(S);
/* Temporary bus copy declarations */
SFB_COUNTERBUS _u0BUS;
SFB_COUNTERBUS _y0BUS;
/*Copy input bus into temporary structure*/
(void) memcpy(&_u0BUS.inputsignal.input,u0 + busInfo[0], busInfo[1]);
(void) memcpy(&_u0BUS.limits.upper_saturation_limit,u0 + busInfo[2], busInfo[3]);
(void) memcpy(&_u0BUS.limits.lower_saturation_limit,u0 + busInfo[4], busInfo[5]);
sfbuilder_bus_Outputs_wrapper(&_u0BUS, u1, &_y0BUS, y1);
/*Copy temporary structure into output bus*/
(void) memcpy(y0+ busInfo[0], &_y0BUS.inputsignal.input, busInfo[1]);
(void) memcpy(y0+ busInfo[2], &_y0BUS.limits.upper_saturation_limit, busInfo[3]);
(void) memcpy(y0+ busInfo[4], &_y0BUS.limits.lower_saturation_limit, busInfo[5]);
}
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
/*Free stored bus information*/
int_T *busInfo = (int_T *) ssGetUserData(S);
if(busInfo!=NULL) {
free(busInfo);
}
}
static int_T GetRTWEnvironmentMode(SimStruct *S)
{
int_T status;
mxArray *plhs[1];
mxArray *prhs[1];
int_T err;
/*
* Get the name of the Simulink block diagram
*/
prhs[0] = mxCreateString(ssGetModelName(ssGetRootSS(S)));
plhs[0] = NULL;
/*
* Call "isSimulationTarget = rtwenvironmentmode(modelName)" in MATLAB
*/
mexSetTrapFlag(1);
err = mexCallMATLAB(1, plhs, 1, prhs, "rtwenvironmentmode");
mexSetTrapFlag(0);
mxDestroyArray(prhs[0]);
/*
* Set the error status if an error occurred
*/
if (err) {
if (plhs[0]) {
mxDestroyArray(plhs[0]);
plhs[0] = NULL;
}
ssSetErrorStatus(S, "Unknow error during call to 'rtwenvironmentmode'.");
return -1;
}
/*
* Get the value returned by rtwenvironmentmode(modelName)
*/
if (plhs[0]) {
status = (int_T) (mxGetScalar(plhs[0]) != 0);
mxDestroyArray(plhs[0]);
plhs[0] = NULL;
}
return (status);
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif