]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogOutput.c
Minor test model updates.
[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(mfst==NULL){
150         mfst = malloc(sizeof(mf624_state_t));
151         #define BUFF_SMALL              32
152         char buff[BUFF_SMALL];
153         memset(buff, '\0', BUFF_SMALL);
154         mfst->uio_dev = "uio0";
155
156         strncat(buff, "/dev/", 5);
157         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
158
159         mfst->device_fd = open_device(buff);
160         if (mfst->device_fd < 0) {
161             ssSetErrorStatus(S,"open failed");
162             return;
163         }
164         if (mmap_regions(mfst) < 0) {
165             ssSetErrorStatus(S,"mmap_regions failed");
166             return;
167         }
168
169     }
170         DAC_enable(mfst);
171     //ssSetPWorkValue(S, 0, mfst);
172   }
173 #endif /*  MDL_START */
174
175
176
177 /* Function: mdlOutputs =======================================================
178  * Abstract:
179  *    In this function, you compute the outputs of your S-function
180  *    block.
181  */
182 static void mdlOutputs(SimStruct *S, int_T tid)
183 {
184     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
185     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
186     int out;
187     if(u[0] > 9.9988){
188         out = 0x3FFF;
189     }
190     else if(u[0] < -10){
191         out = 0;
192     }
193     else {
194         out = (int) ((u[0] + 10) * 8192 / 10 + 0.5);
195     }
196     mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1]));
197 }
198
199
200
201 #define MDL_UPDATE  /* Change to #undef to remove function */
202 #if defined(MDL_UPDATE)
203   /* Function: mdlUpdate ======================================================
204    * Abstract:
205    *    This function is called once for every major integration time step.
206    *    Discrete states are typically updated here, but this function is useful
207    *    for performing any tasks that should only take place once per
208    *    integration step.
209    */
210   static void mdlUpdate(SimStruct *S, int_T tid)
211   {
212   }
213 #endif /* MDL_UPDATE */
214
215
216
217 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
218 #if defined(MDL_DERIVATIVES)
219   /* Function: mdlDerivatives =================================================
220    * Abstract:
221    *    In this function, you compute the S-function block's derivatives.
222    *    The derivatives are placed in the derivative vector, ssGetdX(S).
223    */
224   static void mdlDerivatives(SimStruct *S)
225   {
226   }
227 #endif /* MDL_DERIVATIVES */
228
229
230
231 /* Function: mdlTerminate =====================================================
232  * Abstract:
233  *    In this function, you should perform any actions that are necessary
234  *    at the termination of a simulation.  For example, if memory was
235  *    allocated in mdlStart, this is the place to free it.
236  */
237 static void mdlTerminate(SimStruct *S)
238 {
239     //mf624_state_t* mfst = ssGetPWorkValue(S,0);
240     /*At the end of simulation disable D/A outputs*/
241     if(mfst != NULL){
242         mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
243             & ~GPIOC_DACEN_mask), // disable output,
244             MFST2REG(mfst, 0, GPIOC_reg));
245         free(mfst);
246         mfst=NULL;
247     } else {
248         mfst = malloc(sizeof(mf624_state_t));
249         #define BUFF_SMALL              32
250         char buff[BUFF_SMALL];
251         memset(buff, '\0', BUFF_SMALL);
252         mfst->uio_dev = "uio0";
253
254         strncat(buff, "/dev/", 5);
255         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
256
257         mfst->device_fd = open_device(buff);
258         if (mfst->device_fd < 0) {
259             ssSetErrorStatus(S,"open failed");
260             return;
261         }
262         if (mmap_regions(mfst) < 0) {
263             ssSetErrorStatus(S,"mmap_regions failed");
264             return;
265         }   
266         mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
267             & ~GPIOC_DACEN_mask), // disable output,
268             MFST2REG(mfst, 0, GPIOC_reg));
269         free(mfst);
270         mfst=NULL;
271     }
272     
273 }
274
275
276 /*======================================================*
277  * See sfuntmpl_doc.c for the optional S-function methods *
278  *======================================================*/
279
280 /*=============================*
281  * Required S-function trailer *
282  *=============================*/
283
284 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
285 #include "simulink.c"      /* MEX-file interface mechanism */
286 #else
287 #include "cg_sfun.h"       /* Code generation registration function */
288 #endif