]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogOutput.c
Digital input for MF624 implemented.
[mf624-simulink.git] / sfAnalogOutput.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  sfAnalogOutput
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         DAC_enable(mfst);
152     //ssSetPWorkValue(S, 0, mfst);
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     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
167     int out;
168
169     if (mf624_check(S) != 0)
170             return;
171
172     if(u[0] > 9.9988){
173         out = 0x3FFF;
174     }
175     else if(u[0] < -10){
176         out = 0;
177     }
178     else {
179         out = (int) ((u[0] + 10) * 8192 / 10 + 0.5);
180     }
181     mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1]));
182 }
183
184
185
186 #define MDL_UPDATE  /* Change to #undef to remove function */
187 #if defined(MDL_UPDATE)
188   /* Function: mdlUpdate ======================================================
189    * Abstract:
190    *    This function is called once for every major integration time step.
191    *    Discrete states are typically updated here, but this function is useful
192    *    for performing any tasks that should only take place once per
193    *    integration step.
194    */
195   static void mdlUpdate(SimStruct *S, int_T tid)
196   {
197   }
198 #endif /* MDL_UPDATE */
199
200
201
202 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
203 #if defined(MDL_DERIVATIVES)
204   /* Function: mdlDerivatives =================================================
205    * Abstract:
206    *    In this function, you compute the S-function block's derivatives.
207    *    The derivatives are placed in the derivative vector, ssGetdX(S).
208    */
209   static void mdlDerivatives(SimStruct *S)
210   {
211   }
212 #endif /* MDL_DERIVATIVES */
213
214
215
216 /* Function: mdlTerminate =====================================================
217  * Abstract:
218  *    In this function, you should perform any actions that are necessary
219  *    at the termination of a simulation.  For example, if memory was
220  *    allocated in mdlStart, this is the place to free it.
221  */
222 static void mdlTerminate(SimStruct *S)
223 {
224     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
225     if (mf624_check(NULL) != 0)
226             return;
227
228     /*At the end of simulation disable D/A outputs*/
229     mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
230                    & ~GPIOC_DACEN_mask), // disable output,
231                   MFST2REG(mfst, 0, GPIOC_reg));
232
233     mf624_done();
234 }
235
236
237 /*======================================================*
238  * See sfuntmpl_doc.c for the optional S-function methods *
239  *======================================================*/
240
241 /*=============================*
242  * Required S-function trailer *
243  *=============================*/
244
245 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
246 #include "simulink.c"      /* MEX-file interface mechanism */
247 #else
248 #include "cg_sfun.h"       /* Code generation registration function */
249 #endif