]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfDigitalOutput.c
mf624_blocks_demo adapt for IRC extension and include PWM blocks.
[mf624-simulink.git] / sfDigitalOutput.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  sfDigitalOutput
19 #define S_FUNCTION_LEVEL 2
20
21 #define CHNL_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
22
23 /*
24  * Need to include simstruc.h for the definition of the SimStruct and
25  * its associated macro definitions.
26  */
27 #include "simstruc.h"
28 #include "mf624_SIMULINK.h"
29
30 /* Error handling
31  * --------------
32  *
33  * You should use the following technique to report errors encountered within
34  * an S-function:
35  *
36  *       ssSetErrorStatus(S,"Error encountered due to ...");
37  *       return;
38  *
39  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
40  * It cannot be a local variable. For example the following will cause
41  * unpredictable errors:
42  *
43  *      mdlOutputs()
44  *      {
45  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
46  *         sprintf(msg,"Error due to %s", string);
47  *         ssSetErrorStatus(S,msg);
48  *         return;
49  *      }
50  *
51  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
52  */
53
54 /*====================*
55  * S-function methods *
56  *====================*/
57
58 /* Function: mdlInitializeSizes ===============================================
59  * Abstract:
60  *    The sizes information is used by Simulink to determine the S-function
61  *    block's characteristics (number of inputs, outputs, states, etc.).
62  */
63 static void mdlInitializeSizes(SimStruct *S)
64 {
65     /* See sfuntmpl_doc.c for more details on the macros below */
66
67     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
68     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
69         /* Return if number of expected != number of actual parameters */
70         return;
71     }
72
73     ssSetNumContStates(S, 0);
74     ssSetNumDiscStates(S, 0);
75
76     if (!ssSetNumInputPorts(S, 1)) return;
77     ssSetInputPortWidth(S, 0, 1);
78     ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
79     /*
80      * Set direct feedthrough flag (1=yes, 0=no).
81      * A port has direct feedthrough if the input is used in either
82      * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
83      * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
84      */
85     ssSetInputPortDirectFeedThrough(S, 0, 1);
86
87     if (!ssSetNumOutputPorts(S, 0)) return;
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, -1);
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     if (mf624_init(NULL) != 0)
150         return;
151     mfst->DOut=0;
152     
153   }
154 #endif /*  MDL_START */
155
156
157
158 /* Function: mdlOutputs =======================================================
159  * Abstract:
160  *    In this function, you compute the outputs of your S-function
161  *    block.
162  */
163 static void mdlOutputs(SimStruct *S, int_T tid)
164 {
165     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
166
167     if (mf624_check(S) != 0)
168             return;
169
170     if(u[0] > 0.5){
171         /*Update the "word" holding Output Values*/
172         mfst->DOut |= (1 << (int)(CHNL_PRM(S)-1));
173     }
174     else {
175         /*Update the "word" holding Output Values*/
176         mfst->DOut &= ~(1 << (int)(CHNL_PRM(S)-1));
177     }
178     
179     mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
180 }
181
182
183
184 #define MDL_UPDATE  /* Change to #undef to remove function */
185 #if defined(MDL_UPDATE)
186   /* Function: mdlUpdate ======================================================
187    * Abstract:
188    *    This function is called once for every major integration time step.
189    *    Discrete states are typically updated here, but this function is useful
190    *    for performing any tasks that should only take place once per
191    *    integration step.
192    */
193   static void mdlUpdate(SimStruct *S, int_T tid)
194   {
195   }
196 #endif /* MDL_UPDATE */
197
198
199
200 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
201 #if defined(MDL_DERIVATIVES)
202   /* Function: mdlDerivatives =================================================
203    * Abstract:
204    *    In this function, you compute the S-function block's derivatives.
205    *    The derivatives are placed in the derivative vector, ssGetdX(S).
206    */
207   static void mdlDerivatives(SimStruct *S)
208   {
209   }
210 #endif /* MDL_DERIVATIVES */
211
212
213
214 /* Function: mdlTerminate =====================================================
215  * Abstract:
216  *    In this function, you should perform any actions that are necessary
217  *    at the termination of a simulation.  For example, if memory was
218  *    allocated in mdlStart, this is the place to free it.
219  */
220 static void mdlTerminate(SimStruct *S)
221 {
222         mf624_done();
223 }
224
225
226 /*======================================================*
227  * See sfuntmpl_doc.c for the optional S-function methods *
228  *======================================================*/
229
230 /*=============================*
231  * Required S-function trailer *
232  *=============================*/
233
234 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
235 #include "simulink.c"      /* MEX-file interface mechanism */
236 #else
237 #include "cg_sfun.h"       /* Code generation registration function */
238 #endif