2 * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
4 * -------------------------------------------------------------------------
5 * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
6 * -------------------------------------------------------------------------
8 * Copyright 1990-2002 The MathWorks, Inc.
9 * $Revision: 1.27.4.2 $
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).
18 #define S_FUNCTION_NAME sfDigitalOutput
19 #define S_FUNCTION_LEVEL 2
21 #define CHNL_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
24 * Need to include simstruc.h for the definition of the SimStruct and
25 * its associated macro definitions.
28 #include "mf624_SIMULINK.h"
33 * You should use the following technique to report errors encountered within
36 * ssSetErrorStatus(S,"Error encountered due to ...");
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:
45 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
46 * sprintf(msg,"Error due to %s", string);
47 * ssSetErrorStatus(S,msg);
51 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
54 /*====================*
55 * S-function methods *
56 *====================*/
58 /* Function: mdlInitializeSizes ===============================================
60 * The sizes information is used by Simulink to determine the S-function
61 * block's characteristics (number of inputs, outputs, states, etc.).
63 static void mdlInitializeSizes(SimStruct *S)
65 /* See sfuntmpl_doc.c for more details on the macros below */
67 ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
68 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
69 /* Return if number of expected != number of actual parameters */
73 ssSetNumContStates(S, 0);
74 ssSetNumDiscStates(S, 0);
76 if (!ssSetNumInputPorts(S, 1)) return;
77 ssSetInputPortWidth(S, 0, 1);
78 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
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.
85 ssSetInputPortDirectFeedThrough(S, 0, 1);
87 if (!ssSetNumOutputPorts(S, 0)) return;
89 ssSetNumSampleTimes(S, 1);
94 ssSetNumNonsampledZCs(S, 0);
96 /* Specify the sim state compliance to be same as a built-in block */
97 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
104 /* Function: mdlInitializeSampleTimes =========================================
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.
110 static void mdlInitializeSampleTimes(SimStruct *S)
112 ssSetSampleTime(S, 0, -1);
113 ssSetOffsetTime(S, 0, 0.0);
119 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
120 #if defined(MDL_INITIALIZE_CONDITIONS)
121 /* Function: mdlInitializeConditions ========================================
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.
132 static void mdlInitializeConditions(SimStruct *S)
135 #endif /* MDL_INITIALIZE_CONDITIONS */
139 #define MDL_START /* Change to #undef to remove function */
140 #if defined(MDL_START)
141 /* Function: mdlStart =======================================================
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
147 static void mdlStart(SimStruct *S)
150 #define BUFF_SMALL 32
151 mfst = malloc(sizeof(mf624_state_t));
152 char buff[BUFF_SMALL];
153 memset(buff, '\0', BUFF_SMALL);
154 mfst->uio_dev = "uio0";
156 strncat(buff, "/dev/", 5);
157 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
159 mfst->device_fd = open_device(buff);
160 if (mfst->device_fd < 0) {
161 ssSetErrorStatus(S,"open failed");
164 if (mmap_regions(mfst) < 0) {
165 ssSetErrorStatus(S,"mmap_regions failed");
172 #endif /* MDL_START */
176 /* Function: mdlOutputs =======================================================
178 * In this function, you compute the outputs of your S-function
181 static void mdlOutputs(SimStruct *S, int_T tid)
183 const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
185 /*Update the "word" holding Output Values*/
186 mfst->DOut |= (1 << (int)(CHNL_PRM(S)-1));
189 /*Update the "word" holding Output Values*/
190 mfst->DOut &= ~(1 << (int)(CHNL_PRM(S)-1));
193 mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
198 #define MDL_UPDATE /* Change to #undef to remove function */
199 #if defined(MDL_UPDATE)
200 /* Function: mdlUpdate ======================================================
202 * This function is called once for every major integration time step.
203 * Discrete states are typically updated here, but this function is useful
204 * for performing any tasks that should only take place once per
207 static void mdlUpdate(SimStruct *S, int_T tid)
210 #endif /* MDL_UPDATE */
214 #define MDL_DERIVATIVES /* Change to #undef to remove function */
215 #if defined(MDL_DERIVATIVES)
216 /* Function: mdlDerivatives =================================================
218 * In this function, you compute the S-function block's derivatives.
219 * The derivatives are placed in the derivative vector, ssGetdX(S).
221 static void mdlDerivatives(SimStruct *S)
224 #endif /* MDL_DERIVATIVES */
228 /* Function: mdlTerminate =====================================================
230 * In this function, you should perform any actions that are necessary
231 * at the termination of a simulation. For example, if memory was
232 * allocated in mdlStart, this is the place to free it.
234 static void mdlTerminate(SimStruct *S)
243 /*======================================================*
244 * See sfuntmpl_doc.c for the optional S-function methods *
245 *======================================================*/
247 /*=============================*
248 * Required S-function trailer *
249 *=============================*/
251 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
252 #include "simulink.c" /* MEX-file interface mechanism */
254 #include "cg_sfun.h" /* Code generation registration function */