2 * (c) Michal Kreč, Czech Technical University in Prague, 2013
7 * You must specify the S_FUNCTION_NAME as the name of your S-function
8 * (i.e. replace sfuntmpl_basic with the name of your S-function).
11 #define S_FUNCTION_NAME sfAnalogInput
12 #define S_FUNCTION_LEVEL 2
13 #define MASK_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
16 * Need to include simstruc.h for the definition of the SimStruct and
17 * its associated macro definitions.
21 #include "mf624_SIMULINK.h"
27 * You should use the following technique to report errors encountered within
30 * ssSetErrorStatus(S,"Error encountered due to ...");
33 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
34 * It cannot be a local variable. For example the following will cause
35 * unpredictable errors:
39 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
40 * sprintf(msg,"Error due to %s", string);
41 * ssSetErrorStatus(S,msg);
45 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
48 /*====================*
49 * S-function methods *
50 *====================*/
52 /* Function: mdlInitializeSizes ===============================================
54 * The sizes information is used by Simulink to determine the S-function
55 * block's characteristics (number of inputs, outputs, states, etc.).
57 static void mdlInitializeSizes(SimStruct *S)
59 /* See sfuntmpl_doc.c for more details on the macros below */
61 ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
62 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
63 /* Return if number of expected != number of actual parameters */
64 ssSetErrorStatus(S,"Parameter mismatch");
68 ssSetNumContStates(S, 0);
69 ssSetNumDiscStates(S, 0);
71 int ADCCMask = (int)MASK_PRM(S);
73 if(ADCCMask > 255 || ADCCMask < 0) {
74 ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
77 int ADCChannels = __builtin_popcount((uint32_t)ADCCMask); //Counts number of set bits in ADCCMask
80 if (!ssSetNumInputPorts(S, 0)) return;
84 if (!ssSetNumOutputPorts(S, ADCChannels)) return;
85 for(i=0; i < ADCChannels;i++){
86 ssSetOutputPortWidth(S, i, 1);
88 ssSetNumSampleTimes(S, 1);
93 ssSetNumNonsampledZCs(S, 0);
95 /* Specify the sim state compliance to be same as a built-in block */
96 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
105 /* Function: mdlInitializeSampleTimes =========================================
107 * This function is used to specify the sample time(s) for your
108 * S-function. You must register the same number of sample times as
109 * specified in ssSetNumSampleTimes.
111 static void mdlInitializeSampleTimes(SimStruct *S)
113 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
114 ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
120 #undef MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
121 #if defined(MDL_INITIALIZE_CONDITIONS)
122 /* Function: mdlInitializeConditions ========================================
124 * In this function, you should initialize the continuous and discrete
125 * states for your S-function block. The initial states are placed
126 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
127 * You can also perform any other initialization activities that your
128 * S-function may require. Note, this routine will be called at the
129 * start of simulation and if it is present in an enabled subsystem
130 * configured to reset states, it will be call when the enabled subsystem
131 * restarts execution to reset the states.
133 static void mdlInitializeConditions(SimStruct *S)
136 #endif /* MDL_INITIALIZE_CONDITIONS */
140 #define MDL_START /* Change to #undef to remove function */
141 #if defined(MDL_START)
142 /* Function: mdlStart =======================================================
144 * This function is called once at start of model execution. If you
145 * have states that should be initialized once, this is the place
148 static void mdlStart(SimStruct *S)
150 if (mf624_init(NULL) != 0)
153 int ADCCMask = (int)MASK_PRM(S);
155 if(ADCCMask > 255 || ADCCMask < 0) {
156 ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
159 int ADCChannels = __builtin_popcount((uint32_t)ADCCMask); //Counts number of set bits in ADCCMask
161 ssSetIWorkValue(S, 0, ADCChannels);
163 mfst->ADC_enabled = ADCCMask;
164 mfst->ADC_enabled &= 0xFF;
166 mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
171 #endif /* MDL_START */
175 /* Function: mdlOutputs =======================================================
177 * In this function, you compute the outputs of your S-function
180 static void mdlOutputs(SimStruct *S, int_T tid)
183 int ADCChannels = ssGetIWorkValue(S, 0);
184 real_T* y[ADCChannels];
188 if (mf624_check(S) != 0)
191 // Activate trigger to start conversion
192 mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
194 for(i=0; i < ADCChannels;i++){
195 y[i]=ssGetOutputPortSignal(S,i);
198 // Check if conversion has finished
199 while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) {
200 for (i = 0; i < 1000; i++) {} // small wait
204 for(i=1; i < ADCChannels;i+=2){
205 res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg));
208 *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
209 *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000);
212 if(i == ADCChannels){
213 res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
214 *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
220 #undef MDL_UPDATE /* Change to #undef to remove function */
221 #if defined(MDL_UPDATE)
222 /* Function: mdlUpdate ======================================================
224 * This function is called once for every major integration time step.
225 * Discrete states are typically updated here, but this function is useful
226 * for performing any tasks that should only take place once per
229 static void mdlUpdate(SimStruct *S, int_T tid)
232 #endif /* MDL_UPDATE */
236 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
237 #if defined(MDL_DERIVATIVES)
238 /* Function: mdlDerivatives =================================================
240 * In this function, you compute the S-function block's derivatives.
241 * The derivatives are placed in the derivative vector, ssGetdX(S).
243 static void mdlDerivatives(SimStruct *S)
246 #endif /* MDL_DERIVATIVES */
250 /* Function: mdlTerminate =====================================================
252 * In this function, you should perform any actions that are necessary
253 * at the termination of a simulation. For example, if memory was
254 * allocated in mdlStart, this is the place to free it.
256 static void mdlTerminate(SimStruct *S)
262 /*======================================================*
263 * See sfuntmpl_doc.c for the optional S-function methods *
264 *======================================================*/
266 /*=============================*
267 * Required S-function trailer *
268 *=============================*/
270 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
271 #include "simulink.c" /* MEX-file interface mechanism */
273 #include "cg_sfun.h" /* Code generation registration function */