]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfuntmpl_basic.c
Digital input for MF624 implemented.
[mf624-simulink.git] / sfuntmpl_basic.c
1 /*
2  * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
3  *
4  *  -------------------------------------------------------------------------
5  *  | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
6  *  -------------------------------------------------------------------------
7  *
8  * Copyright 1990-2002 The MathWorks, Inc.
9  * $Revision: 1.27.4.2 $
10  */
11
12
13 /*
14  * You must specify the S_FUNCTION_NAME as the name of your S-function
15  * (i.e. replace sfuntmpl_basic with the name of your S-function).
16  */
17
18 #define S_FUNCTION_NAME  sfuntmpl_basic
19 #define S_FUNCTION_LEVEL 2
20
21 /*
22  * Need to include simstruc.h for the definition of the SimStruct and
23  * its associated macro definitions.
24  */
25 #include "simstruc.h"
26
27
28
29 /* Error handling
30  * --------------
31  *
32  * You should use the following technique to report errors encountered within
33  * an S-function:
34  *
35  *       ssSetErrorStatus(S,"Error encountered due to ...");
36  *       return;
37  *
38  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
39  * It cannot be a local variable. For example the following will cause
40  * unpredictable errors:
41  *
42  *      mdlOutputs()
43  *      {
44  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
45  *         sprintf(msg,"Error due to %s", string);
46  *         ssSetErrorStatus(S,msg);
47  *         return;
48  *      }
49  *
50  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
51  */
52
53 /*====================*
54  * S-function methods *
55  *====================*/
56
57 /* Function: mdlInitializeSizes ===============================================
58  * Abstract:
59  *    The sizes information is used by Simulink to determine the S-function
60  *    block's characteristics (number of inputs, outputs, states, etc.).
61  */
62 static void mdlInitializeSizes(SimStruct *S)
63 {
64     /* See sfuntmpl_doc.c for more details on the macros below */
65
66     ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
67     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
68         /* Return if number of expected != number of actual parameters */
69         return;
70     }
71
72     ssSetNumContStates(S, 0);
73     ssSetNumDiscStates(S, 0);
74
75     if (!ssSetNumInputPorts(S, 1)) return;
76     ssSetInputPortWidth(S, 0, 1);
77     ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
78     /*
79      * Set direct feedthrough flag (1=yes, 0=no).
80      * A port has direct feedthrough if the input is used in either
81      * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
82      * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
83      */
84     ssSetInputPortDirectFeedThrough(S, 0, 1);
85
86     if (!ssSetNumOutputPorts(S, 1)) return;
87     ssSetOutputPortWidth(S, 0, 1);
88
89     ssSetNumSampleTimes(S, 1);
90     ssSetNumRWork(S, 0);
91     ssSetNumIWork(S, 0);
92     ssSetNumPWork(S, 0);
93     ssSetNumModes(S, 0);
94     ssSetNumNonsampledZCs(S, 0);
95
96     /* Specify the sim state compliance to be same as a built-in block */
97     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
98
99     ssSetOptions(S, 0);
100 }
101
102
103
104 /* Function: mdlInitializeSampleTimes =========================================
105  * Abstract:
106  *    This function is used to specify the sample time(s) for your
107  *    S-function. You must register the same number of sample times as
108  *    specified in ssSetNumSampleTimes.
109  */
110 static void mdlInitializeSampleTimes(SimStruct *S)
111 {
112     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
113     ssSetOffsetTime(S, 0, 0.0);
114
115 }
116
117
118
119 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
120 #if defined(MDL_INITIALIZE_CONDITIONS)
121   /* Function: mdlInitializeConditions ========================================
122    * Abstract:
123    *    In this function, you should initialize the continuous and discrete
124    *    states for your S-function block.  The initial states are placed
125    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
126    *    You can also perform any other initialization activities that your
127    *    S-function may require. Note, this routine will be called at the
128    *    start of simulation and if it is present in an enabled subsystem
129    *    configured to reset states, it will be call when the enabled subsystem
130    *    restarts execution to reset the states.
131    */
132   static void mdlInitializeConditions(SimStruct *S)
133   {
134   }
135 #endif /* MDL_INITIALIZE_CONDITIONS */
136
137
138
139 #define MDL_START  /* Change to #undef to remove function */
140 #if defined(MDL_START) 
141   /* Function: mdlStart =======================================================
142    * Abstract:
143    *    This function is called once at start of model execution. If you
144    *    have states that should be initialized once, this is the place
145    *    to do it.
146    */
147   static void mdlStart(SimStruct *S)
148   {
149   }
150 #endif /*  MDL_START */
151
152
153
154 /* Function: mdlOutputs =======================================================
155  * Abstract:
156  *    In this function, you compute the outputs of your S-function
157  *    block.
158  */
159 static void mdlOutputs(SimStruct *S, int_T tid)
160 {
161     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
162     real_T       *y = ssGetOutputPortSignal(S,0);
163     y[0] = u[0];
164 }
165
166
167
168 #define MDL_UPDATE  /* Change to #undef to remove function */
169 #if defined(MDL_UPDATE)
170   /* Function: mdlUpdate ======================================================
171    * Abstract:
172    *    This function is called once for every major integration time step.
173    *    Discrete states are typically updated here, but this function is useful
174    *    for performing any tasks that should only take place once per
175    *    integration step.
176    */
177   static void mdlUpdate(SimStruct *S, int_T tid)
178   {
179   }
180 #endif /* MDL_UPDATE */
181
182
183
184 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
185 #if defined(MDL_DERIVATIVES)
186   /* Function: mdlDerivatives =================================================
187    * Abstract:
188    *    In this function, you compute the S-function block's derivatives.
189    *    The derivatives are placed in the derivative vector, ssGetdX(S).
190    */
191   static void mdlDerivatives(SimStruct *S)
192   {
193   }
194 #endif /* MDL_DERIVATIVES */
195
196
197
198 /* Function: mdlTerminate =====================================================
199  * Abstract:
200  *    In this function, you should perform any actions that are necessary
201  *    at the termination of a simulation.  For example, if memory was
202  *    allocated in mdlStart, this is the place to free it.
203  */
204 static void mdlTerminate(SimStruct *S)
205 {
206 }
207
208
209 /*======================================================*
210  * See sfuntmpl_doc.c for the optional S-function methods *
211  *======================================================*/
212
213 /*=============================*
214  * Required S-function trailer *
215  *=============================*/
216
217 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
218 #include "simulink.c"      /* MEX-file interface mechanism */
219 #else
220 #include "cg_sfun.h"       /* Code generation registration function */
221 #endif