]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogOutput.c
Include license header to prepare code for publication.
[mf624-simulink.git] / sfAnalogOutput.c
1 /*
2  * S-function to support analog outputs on Humusoft MF624 card
3  *
4  * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
5  * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
6  *
7  * Department of Control Engineering
8  * Faculty of Electrical Engineering
9  * Czech Technical University in Prague (CTU)
10  *
11  * The S-Function for ERT Linux can be distributed in compliance
12  * with GNU General Public License (GPL) version 2 or later.
13  * Other licence can negotiated with CTU.
14  *
15  * Next exception is granted in addition to GPL.
16  * Instantiating or linking compiled version of this code
17  * to produce an application image/executable, does not
18  * by itself cause the resulting application image/executable
19  * to be covered by the GNU General Public License.
20  * This exception does not however invalidate any other reasons
21  * why the executable file might be covered by the GNU Public License.
22  * Publication of enhanced or derived S-function files is required
23  * although.
24  *
25  * Linux ERT code is available from
26  *    http://rtime.felk.cvut.cz/gitweb/ert_linux.git
27  * More CTU Linux target for Simulink components are available at
28  *    http://lintarget.sourceforge.net/
29  *
30  * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
31  * required S-function structure.
32  */
33
34
35 /*
36  * You must specify the S_FUNCTION_NAME as the name of your S-function
37  * (i.e. replace sfuntmpl_basic with the name of your S-function).
38  */
39
40 #define S_FUNCTION_NAME  sfAnalogOutput
41 #define S_FUNCTION_LEVEL 2
42
43 #define CHNL_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
44
45 /*
46  * Need to include simstruc.h for the definition of the SimStruct and
47  * its associated macro definitions.
48  */
49 #include "simstruc.h"
50 #include "mf624_SIMULINK.h"
51
52 /* Error handling
53  * --------------
54  *
55  * You should use the following technique to report errors encountered within
56  * an S-function:
57  *
58  *       ssSetErrorStatus(S,"Error encountered due to ...");
59  *       return;
60  *
61  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
62  * It cannot be a local variable. For example the following will cause
63  * unpredictable errors:
64  *
65  *      mdlOutputs()
66  *      {
67  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
68  *         sprintf(msg,"Error due to %s", string);
69  *         ssSetErrorStatus(S,msg);
70  *         return;
71  *      }
72  *
73  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
74  */
75
76 /*====================*
77  * S-function methods *
78  *====================*/
79
80 /* Function: mdlInitializeSizes ===============================================
81  * Abstract:
82  *    The sizes information is used by Simulink to determine the S-function
83  *    block's characteristics (number of inputs, outputs, states, etc.).
84  */
85 static void mdlInitializeSizes(SimStruct *S)
86 {
87     /* See sfuntmpl_doc.c for more details on the macros below */
88
89     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
90     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
91         /* Return if number of expected != number of actual parameters */
92         return;
93     }
94
95     ssSetNumContStates(S, 0);
96     ssSetNumDiscStates(S, 0);
97
98     if (!ssSetNumInputPorts(S, 1)) return;
99     ssSetInputPortWidth(S, 0, 1);
100     ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
101     /*
102      * Set direct feedthrough flag (1=yes, 0=no).
103      * A port has direct feedthrough if the input is used in either
104      * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
105      * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
106      */
107     ssSetInputPortDirectFeedThrough(S, 0, 1);
108
109     if (!ssSetNumOutputPorts(S, 0)) return;
110
111     ssSetNumSampleTimes(S, 1);
112     ssSetNumRWork(S, 0);
113     ssSetNumIWork(S, 0);
114     ssSetNumPWork(S, 0);
115     ssSetNumModes(S, 0);
116     ssSetNumNonsampledZCs(S, 0);
117
118     /* Specify the sim state compliance to be same as a built-in block */
119     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
120
121     ssSetOptions(S, 0);
122 }
123
124
125
126 /* Function: mdlInitializeSampleTimes =========================================
127  * Abstract:
128  *    This function is used to specify the sample time(s) for your
129  *    S-function. You must register the same number of sample times as
130  *    specified in ssSetNumSampleTimes.
131  */
132 static void mdlInitializeSampleTimes(SimStruct *S)
133 {
134     ssSetSampleTime(S, 0, -1);
135     ssSetOffsetTime(S, 0, 0.0);
136
137 }
138
139
140
141 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
142 #if defined(MDL_INITIALIZE_CONDITIONS)
143   /* Function: mdlInitializeConditions ========================================
144    * Abstract:
145    *    In this function, you should initialize the continuous and discrete
146    *    states for your S-function block.  The initial states are placed
147    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
148    *    You can also perform any other initialization activities that your
149    *    S-function may require. Note, this routine will be called at the
150    *    start of simulation and if it is present in an enabled subsystem
151    *    configured to reset states, it will be call when the enabled subsystem
152    *    restarts execution to reset the states.
153    */
154   static void mdlInitializeConditions(SimStruct *S)
155   {
156   }
157 #endif /* MDL_INITIALIZE_CONDITIONS */
158
159
160
161 #define MDL_START  /* Change to #undef to remove function */
162 #if defined(MDL_START) 
163   /* Function: mdlStart =======================================================
164    * Abstract:
165    *    This function is called once at start of model execution. If you
166    *    have states that should be initialized once, this is the place
167    *    to do it.
168    */
169   static void mdlStart(SimStruct *S)
170   {
171         if (mf624_init(NULL) != 0)
172             return;
173         DAC_enable(mfst);
174     //ssSetPWorkValue(S, 0, mfst);
175   }
176 #endif /*  MDL_START */
177
178
179
180 /* Function: mdlOutputs =======================================================
181  * Abstract:
182  *    In this function, you compute the outputs of your S-function
183  *    block.
184  */
185 static void mdlOutputs(SimStruct *S, int_T tid)
186 {
187     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
188     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
189     int out;
190
191     if (mf624_check(S) != 0)
192             return;
193
194     if(u[0] > 9.9988){
195         out = 0x3FFF;
196     }
197     else if(u[0] < -10){
198         out = 0;
199     }
200     else {
201         out = (int) ((u[0] + 10) * 8192 / 10 + 0.5);
202     }
203     mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1]));
204 }
205
206
207
208 #define MDL_UPDATE  /* Change to #undef to remove function */
209 #if defined(MDL_UPDATE)
210   /* Function: mdlUpdate ======================================================
211    * Abstract:
212    *    This function is called once for every major integration time step.
213    *    Discrete states are typically updated here, but this function is useful
214    *    for performing any tasks that should only take place once per
215    *    integration step.
216    */
217   static void mdlUpdate(SimStruct *S, int_T tid)
218   {
219   }
220 #endif /* MDL_UPDATE */
221
222
223
224 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
225 #if defined(MDL_DERIVATIVES)
226   /* Function: mdlDerivatives =================================================
227    * Abstract:
228    *    In this function, you compute the S-function block's derivatives.
229    *    The derivatives are placed in the derivative vector, ssGetdX(S).
230    */
231   static void mdlDerivatives(SimStruct *S)
232   {
233   }
234 #endif /* MDL_DERIVATIVES */
235
236
237
238 /* Function: mdlTerminate =====================================================
239  * Abstract:
240  *    In this function, you should perform any actions that are necessary
241  *    at the termination of a simulation.  For example, if memory was
242  *    allocated in mdlStart, this is the place to free it.
243  */
244 static void mdlTerminate(SimStruct *S)
245 {
246     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
247     if (mf624_check(NULL) != 0)
248             return;
249
250     /*At the end of simulation disable D/A outputs*/
251     mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
252                    & ~GPIOC_DACEN_mask), // disable output,
253                   MFST2REG(mfst, 0, GPIOC_reg));
254
255     mf624_done();
256 }
257
258
259 /*======================================================*
260  * See sfuntmpl_doc.c for the optional S-function methods *
261  *======================================================*/
262
263 /*=============================*
264  * Required S-function trailer *
265  *=============================*/
266
267 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
268 #include "simulink.c"      /* MEX-file interface mechanism */
269 #else
270 #include "cg_sfun.h"       /* Code generation registration function */
271 #endif