2 * S-function to support analog outputs on Humusoft MF624 card
4 * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
5 * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
7 * Department of Control Engineering
8 * Faculty of Electrical Engineering
9 * Czech Technical University in Prague (CTU)
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.
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
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/
30 * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
31 * required S-function structure.
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).
40 #define S_FUNCTION_NAME sfAnalogOutput
41 #define S_FUNCTION_LEVEL 2
43 #define CHNL_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
46 * Need to include simstruc.h for the definition of the SimStruct and
47 * its associated macro definitions.
52 #include "mf624_SIMULINK.h"
58 * You should use the following technique to report errors encountered within
61 * ssSetErrorStatus(S,"Error encountered due to ...");
64 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
65 * It cannot be a local variable. For example the following will cause
66 * unpredictable errors:
70 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
71 * sprintf(msg,"Error due to %s", string);
72 * ssSetErrorStatus(S,msg);
76 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
79 /*====================*
80 * S-function methods *
81 *====================*/
83 /* Function: mdlInitializeSizes ===============================================
85 * The sizes information is used by Simulink to determine the S-function
86 * block's characteristics (number of inputs, outputs, states, etc.).
88 static void mdlInitializeSizes(SimStruct *S)
90 /* See sfuntmpl_doc.c for more details on the macros below */
92 ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
93 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
94 /* Return if number of expected != number of actual parameters */
98 ssSetNumContStates(S, 0);
99 ssSetNumDiscStates(S, 0);
101 if (!ssSetNumInputPorts(S, 1)) return;
102 ssSetInputPortWidth(S, 0, 1);
103 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
105 * Set direct feedthrough flag (1=yes, 0=no).
106 * A port has direct feedthrough if the input is used in either
107 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
108 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
110 ssSetInputPortDirectFeedThrough(S, 0, 1);
112 if (!ssSetNumOutputPorts(S, 0)) return;
114 ssSetNumSampleTimes(S, 1);
119 ssSetNumNonsampledZCs(S, 0);
121 /* Specify the sim state compliance to be same as a built-in block */
122 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
129 /* Function: mdlInitializeSampleTimes =========================================
131 * This function is used to specify the sample time(s) for your
132 * S-function. You must register the same number of sample times as
133 * specified in ssSetNumSampleTimes.
135 static void mdlInitializeSampleTimes(SimStruct *S)
137 ssSetSampleTime(S, 0, -1);
138 ssSetOffsetTime(S, 0, 0.0);
144 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
145 #if defined(MDL_INITIALIZE_CONDITIONS)
146 /* Function: mdlInitializeConditions ========================================
148 * In this function, you should initialize the continuous and discrete
149 * states for your S-function block. The initial states are placed
150 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
151 * You can also perform any other initialization activities that your
152 * S-function may require. Note, this routine will be called at the
153 * start of simulation and if it is present in an enabled subsystem
154 * configured to reset states, it will be call when the enabled subsystem
155 * restarts execution to reset the states.
157 static void mdlInitializeConditions(SimStruct *S)
160 #endif /* MDL_INITIALIZE_CONDITIONS */
164 #define MDL_START /* Change to #undef to remove function */
165 #if defined(MDL_START)
166 /* Function: mdlStart =======================================================
168 * This function is called once at start of model execution. If you
169 * have states that should be initialized once, this is the place
172 static void mdlStart(SimStruct *S)
175 if (mf624_init(NULL) != 0)
178 #endif /*WITHOUT_HW*/
179 //ssSetPWorkValue(S, 0, mfst);
181 #endif /* MDL_START */
185 /* Function: mdlOutputs =======================================================
187 * In this function, you compute the outputs of your S-function
190 static void mdlOutputs(SimStruct *S, int_T tid)
192 const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
193 //mf624_state_t* mfst = ssGetPWorkValue(S,0);
197 if (mf624_check(S) != 0)
207 out = (int) ((u[0] + 10) * 8192 / 10 + 0.5);
209 mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1]));
210 #endif /*WITHOUT_HW*/
215 #define MDL_UPDATE /* Change to #undef to remove function */
216 #if defined(MDL_UPDATE)
217 /* Function: mdlUpdate ======================================================
219 * This function is called once for every major integration time step.
220 * Discrete states are typically updated here, but this function is useful
221 * for performing any tasks that should only take place once per
224 static void mdlUpdate(SimStruct *S, int_T tid)
227 #endif /* MDL_UPDATE */
231 #define MDL_DERIVATIVES /* Change to #undef to remove function */
232 #if defined(MDL_DERIVATIVES)
233 /* Function: mdlDerivatives =================================================
235 * In this function, you compute the S-function block's derivatives.
236 * The derivatives are placed in the derivative vector, ssGetdX(S).
238 static void mdlDerivatives(SimStruct *S)
241 #endif /* MDL_DERIVATIVES */
245 /* Function: mdlTerminate =====================================================
247 * In this function, you should perform any actions that are necessary
248 * at the termination of a simulation. For example, if memory was
249 * allocated in mdlStart, this is the place to free it.
251 static void mdlTerminate(SimStruct *S)
254 //mf624_state_t* mfst = ssGetPWorkValue(S,0);
255 if (mf624_check(NULL) != 0)
258 /*At the end of simulation disable D/A outputs*/
259 mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
260 & ~GPIOC_DACEN_mask), // disable output,
261 MFST2REG(mfst, 0, GPIOC_reg));
264 #endif /*WITHOUT_HW*/
268 /*======================================================*
269 * See sfuntmpl_doc.c for the optional S-function methods *
270 *======================================================*/
272 /*=============================*
273 * Required S-function trailer *
274 *=============================*/
276 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
277 #include "simulink.c" /* MEX-file interface mechanism */
279 #include "cg_sfun.h" /* Code generation registration function */