]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfDigitalInput.c
Digital input for MF624 implemented.
[mf624-simulink.git] / sfDigitalInput.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  sfDigitalInput
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          ssSetErrorStatus(S,"Parameter count mismatch");
71         return;
72     }
73
74     ssSetNumContStates(S, 0);
75     ssSetNumDiscStates(S, 0);
76
77     if (!ssSetNumInputPorts(S, 0)) return;
78
79     if (!ssSetNumOutputPorts(S, 1)) return;
80     ssSetOutputPortWidth(S, 0, 1);
81
82     ssSetNumSampleTimes(S, 1);
83     ssSetNumRWork(S, 0);
84     ssSetNumIWork(S, 0);
85     ssSetNumPWork(S, 0);
86     ssSetNumModes(S, 0);
87     ssSetNumNonsampledZCs(S, 0);
88
89     /* Specify the sim state compliance to be same as a built-in block */
90     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
91
92     ssSetOptions(S, 0);
93 }
94
95
96
97 /* Function: mdlInitializeSampleTimes =========================================
98  * Abstract:
99  *    This function is used to specify the sample time(s) for your
100  *    S-function. You must register the same number of sample times as
101  *    specified in ssSetNumSampleTimes.
102  */
103 static void mdlInitializeSampleTimes(SimStruct *S)
104 {
105     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
106     ssSetOffsetTime(S, 0, 0.0);
107 }
108
109
110
111 #undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
112 #if defined(MDL_INITIALIZE_CONDITIONS)
113   /* Function: mdlInitializeConditions ========================================
114    * Abstract:
115    *    In this function, you should initialize the continuous and discrete
116    *    states for your S-function block.  The initial states are placed
117    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
118    *    You can also perform any other initialization activities that your
119    *    S-function may require. Note, this routine will be called at the
120    *    start of simulation and if it is present in an enabled subsystem
121    *    configured to reset states, it will be call when the enabled subsystem
122    *    restarts execution to reset the states.
123    */
124   static void mdlInitializeConditions(SimStruct *S)
125   {
126   }
127 #endif /* MDL_INITIALIZE_CONDITIONS */
128
129
130
131 #define MDL_START  /* Change to #undef to remove function */
132 #if defined(MDL_START)
133   /* Function: mdlStart =======================================================
134    * Abstract:
135    *    This function is called once at start of model execution. If you
136    *    have states that should be initialized once, this is the place
137    *    to do it.
138    */
139   static void mdlStart(SimStruct *S)
140   {
141     if (mf624_init(NULL) != 0)
142           return;
143   }
144 #endif /*  MDL_START */
145
146
147
148 /* Function: mdlOutputs =======================================================
149  * Abstract:
150  *    In this function, you compute the outputs of your S-function
151  *    block.
152  */
153 static void mdlOutputs(SimStruct *S, int_T tid)
154 {
155     real_T *y;
156     uint16_t din;
157
158     if (mf624_check(S) != 0)
159             return;
160
161     y = (real_T *)ssGetOutputPortSignal(S, 0);
162
163     din = mf624_read16(MFST2REG(mfst, 2, DIN_reg));
164
165     *y = din & (1 << (int)(CHNL_PRM(S)-1)) ? 0.0 : 1.0;
166 }
167
168
169
170 #undef MDL_UPDATE  /* Change to #undef to remove function */
171 #if defined(MDL_UPDATE)
172   /* Function: mdlUpdate ======================================================
173    * Abstract:
174    *    This function is called once for every major integration time step.
175    *    Discrete states are typically updated here, but this function is useful
176    *    for performing any tasks that should only take place once per
177    *    integration step.
178    */
179   static void mdlUpdate(SimStruct *S, int_T tid)
180   {
181   }
182 #endif /* MDL_UPDATE */
183
184
185
186 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
187 #if defined(MDL_DERIVATIVES)
188   /* Function: mdlDerivatives =================================================
189    * Abstract:
190    *    In this function, you compute the S-function block's derivatives.
191    *    The derivatives are placed in the derivative vector, ssGetdX(S).
192    */
193   static void mdlDerivatives(SimStruct *S)
194   {
195   }
196 #endif /* MDL_DERIVATIVES */
197
198
199
200 /* Function: mdlTerminate =====================================================
201  * Abstract:
202  *    In this function, you should perform any actions that are necessary
203  *    at the termination of a simulation.  For example, if memory was
204  *    allocated in mdlStart, this is the place to free it.
205  */
206 static void mdlTerminate(SimStruct *S)
207 {
208         mf624_done();
209 }
210
211
212 /*======================================================*
213  * See sfuntmpl_doc.c for the optional S-function methods *
214  *======================================================*/
215
216 /*=============================*
217  * Required S-function trailer *
218  *=============================*/
219
220 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
221 #include "simulink.c"      /* MEX-file interface mechanism */
222 #else
223 #include "cg_sfun.h"       /* Code generation registration function */
224 #endif