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