2 * S-function to support PWM Output on Humusoft MF624 card
4 * Copyright (C) 2014 Pavel Pisa <pisa@cmp.felk.cvut.cz>
6 * Department of Control Engineering
7 * Faculty of Electrical Engineering
8 * Czech Technical University in Prague (CTU)
10 * The S-Function for ERT Linux can be distributed in compliance
11 * with GNU General Public License (GPL) version 2 or later.
12 * Other licence can negotiated with CTU.
14 * Next exception is granted in addition to GPL.
15 * Instantiating or linking compiled version of this code
16 * to produce an application image/executable, does not
17 * by itself cause the resulting application image/executable
18 * to be covered by the GNU General Public License.
19 * This exception does not however invalidate any other reasons
20 * why the executable file might be covered by the GNU Public License.
21 * Publication of enhanced or derived S-function files is required
24 * Linux ERT code is available from
25 * http://rtime.felk.cvut.cz/gitweb/ert_linux.git
26 * More CTU Linux target for Simulink components are available at
27 * http://lintarget.sourceforge.net/
29 * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
30 * required S-function structure.
34 #define S_FUNCTION_NAME sfPWMOutput
35 #define S_FUNCTION_LEVEL 2
38 * The S-function has next parameters
41 * PWM Frequency - value -1 for external input
44 #define PRM_CHANNEL(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
45 #define PRM_FREQUENCY(S) (mxGetScalar(ssGetSFcnParam(S, 1)))
49 #define IWORK_IDX_CHANNEL 0
50 #define IWORK_IDX_CTR_MODE 1
51 #define IWORK_IDX_USE_FREQUENCY_INPUT 2
52 #define IWORK_IDX_LAST_MODE 3
53 enum {CTR_LAST_MODE_OUT0, CTR_LAST_MODE_OUT1, CTR_LAST_MODE_PWM};
57 #define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL])
58 #define IWORK_CTR_MODE(S) (ssGetIWork(S)[IWORK_IDX_CTR_MODE])
59 #define IWORK_USE_FREQUENCY_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_FREQUENCY_INPUT])
60 #define IWORK_LAST_MODE(S) (ssGetIWork(S)[IWORK_IDX_LAST_MODE])
62 #define RWORK_IDX_BASE_FREQUENCY 0
66 #define RWORK_BASE_FREQUENCY(S) (ssGetRWork(S)[RWORK_IDX_BASE_FREQUENCY])
70 * Need to include simstruc.h for the definition of the SimStruct and
71 * its associated macro definitions.
77 #include "mf624_SIMULINK.h"
80 #define CTR_MAX_PWM_CHANNEL 3
92 static const ctr_channel_regs_t ctr_channel2regs[] = {
93 {CTR0STATUS_reg, CTR0MODE_reg, CTR0_reg, CTR0A_reg, CTR0B_reg, CTRXCTRL_reg},
94 {CTR1STATUS_reg, CTR1MODE_reg, CTR1_reg, CTR1A_reg, CTR1B_reg, CTRXCTRL_reg},
95 {CTR2STATUS_reg, CTR2MODE_reg, CTR2_reg, CTR2A_reg, CTR2B_reg, CTRXCTRL_reg},
96 {CTR3STATUS_reg, CTR3MODE_reg, CTR3_reg, CTR3A_reg, CTR3B_reg, CTRXCTRL_reg},
97 {CTR4STATUS_reg, CTR4MODE_reg, CTR4_reg, CTR4A_reg, -1, CTRXCTRL_reg}
104 * You should use the following technique to report errors encountered within
107 * ssSetErrorStatus(S,"Error encountered due to ...");
110 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
111 * It cannot be a local variable. For example the following will cause
112 * unpredictable errors:
116 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
117 * sprintf(msg,"Error due to %s", string);
118 * ssSetErrorStatus(S,msg);
122 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
125 /*====================*
126 * S-function methods *
127 *====================*/
129 #define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
130 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
131 /* Function: mdlCheckParameters =============================================
133 * mdlCheckParameters verifies new parameter settings whenever parameter
134 * change or are re-evaluated during a simulation. When a simulation is
135 * running, changes to S-function parameters can occur at any time during
136 * the simulation loop.
138 static void mdlCheckParameters(SimStruct *S)
140 if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > CTR_MAX_PWM_CHANNEL))
141 ssSetErrorStatus(S, "valid PWM channel is 0, 1, 2, or 3");
142 if ((PRM_FREQUENCY(S) <= 0) && (PRM_FREQUENCY(S) != -1))
143 ssSetErrorStatus(S, "Frequency out of valid range");
145 #endif /* MDL_CHECK_PARAMETERS */
149 /* Function: mdlInitializeSizes ===============================================
151 * The sizes information is used by Simulink to determine the S-function
152 * block's characteristics (number of inputs, outputs, states, etc.).
154 static void mdlInitializeSizes(SimStruct *S)
156 int_T nInputPorts = 1;
158 ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */
159 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
160 /* Return if number of expected != number of actual parameters */
161 ssSetErrorStatus(S, "2-parameters requited: Channel, PWM Frequncy or -1");
165 ssSetNumContStates(S, 0);
166 ssSetNumDiscStates(S, 0);
168 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
169 mdlCheckParameters(S);
170 if (ssGetErrorStatus(S) != NULL) return;
173 if (PRM_FREQUENCY(S) == -1)
176 if (!ssSetNumInputPorts(S, nInputPorts)) return;
177 ssSetInputPortWidth(S, 0, 1);
178 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
180 if (PRM_FREQUENCY(S) == -1) {
181 ssSetInputPortWidth(S, 1, 1);
182 ssSetInputPortRequiredContiguous(S, 1, true); /*direct input signal access*/
186 * Set direct feedthrough flag (1=yes, 0=no).
187 * A port has direct feedthrough if the input is used in either
188 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
189 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
191 ssSetInputPortDirectFeedThrough(S, 0, 0);
193 if (!ssSetNumOutputPorts(S, 0)) return;
195 ssSetNumSampleTimes(S, 1);
196 ssSetNumRWork(S, RWORK_COUNT);
197 ssSetNumIWork(S, IWORK_COUNT);
200 ssSetNumNonsampledZCs(S, 0);
202 /* Specify the sim state compliance to be same as a built-in block */
203 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
210 /* Function: mdlInitializeSampleTimes =========================================
212 * This function is used to specify the sample time(s) for your
213 * S-function. You must register the same number of sample times as
214 * specified in ssSetNumSampleTimes.
216 static void mdlInitializeSampleTimes(SimStruct *S)
218 ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
219 ssSetOffsetTime(S, 0, 0.0);
224 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
225 #if defined(MDL_INITIALIZE_CONDITIONS)
226 /* Function: mdlInitializeConditions ========================================
228 * In this function, you should initialize the continuous and discrete
229 * states for your S-function block. The initial states are placed
230 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
231 * You can also perform any other initialization activities that your
232 * S-function may require. Note, this routine will be called at the
233 * start of simulation and if it is present in an enabled subsystem
234 * configured to reset states, it will be call when the enabled subsystem
235 * restarts execution to reset the states.
237 static void mdlInitializeConditions(SimStruct *S)
240 #endif /* MDL_INITIALIZE_CONDITIONS */
244 #define MDL_START /* Change to #undef to remove function */
245 #if defined(MDL_START)
246 /* Function: mdlStart =======================================================
248 * This function is called once at start of model execution. If you
249 * have states that should be initialized once, this is the place
252 static void mdlStart(SimStruct *S)
257 if (mf624_init(NULL) != 0)
260 IWORK_CHANNEL(S) = PRM_CHANNEL(S);
262 IWORK_USE_FREQUENCY_INPUT(S) = (PRM_FREQUENCY(S) == -1)? 1: 0;
264 RWORK_BASE_FREQUENCY(S) = 50e6;
266 IWORK_CTR_MODE(S) = 0;
268 IWORK_LAST_MODE(S) = CTR_LAST_MODE_OUT0;
270 /* Force output low during startup */
271 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
272 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
273 #endif /*WITHOUT_HW*/
275 #endif /* MDL_START */
279 /* Function: mdlOutputs =======================================================
281 * In this function, you compute the outputs of your S-function
284 static void mdlOutputs(SimStruct *S, int_T tid)
286 real_T duty = *(const real_T*) ssGetInputPortSignal(S, 0);
296 if (mf624_check(S) != 0)
299 if (IWORK_USE_FREQUENCY_INPUT(S))
300 frequency = *(const real_T*) ssGetInputPortSignal(S, 1);
302 frequency = PRM_FREQUENCY(S);
307 if (IWORK_LAST_MODE(S) != CTR_LAST_MODE_OUT1) {
308 IWORK_LAST_MODE(S) = CTR_LAST_MODE_OUT1;
309 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_HI);
310 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
311 /*printf("duty >=1: ctr_mode = 0x%08lx\n", ctr_mode);*/
315 if ((duty<=0) || (frequency <= 0) || (frequency > RWORK_BASE_FREQUENCY(S) / 2))
317 if (IWORK_LAST_MODE(S) != CTR_LAST_MODE_OUT0) {
318 IWORK_LAST_MODE(S) = CTR_LAST_MODE_OUT0;
319 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
320 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
321 /*printf("duty <=0: ctr_mode = 0x%08lx\n", ctr_mode);*/
326 T = RWORK_BASE_FREQUENCY(S) / frequency;
333 T2_uint = 0xffffffff;
335 if (T1 <= 0xffffffff)
338 T1_uint = 0xffffffff;
342 if (T1_uint >= T2_uint)
343 T1_uint = T2_uint - 1;
347 mf624_write32(T1_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].A_reg));
348 mf624_write32(T2_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].B_reg));
350 if (IWORK_LAST_MODE(S) != CTR_LAST_MODE_PWM) {
351 IWORK_LAST_MODE(S) = CTR_LAST_MODE_PWM;
353 ctr_ctrl = CTRXCTRL_CTR0STOP_mask | CTRXCTRL_CTR0LOAD_mask | CTRXCTRL_CTR0RESET_mask | CTRXCTRL_CTR0TRESET_mask;
354 ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
355 mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
357 ctr_mode = IWORK_CTR_MODE(S) |
358 __val2mfld(CTR_MODE_COUNT_DIR_mask, CTR_MODE_COUNT_DIR_DOWN) |
359 __val2mfld(CTR_MODE_REPETITION_mask, CTR_MODE_REPETITION_ENABLED) |
360 __val2mfld(CTR_MODE_LOAD_TOGGLE_mask, CTR_MODE_LOAD_TOGGLE_ENABLED) |
361 __val2mfld(CTR_MODE_OUTPUT_TOGGLE_mask, CTR_MODE_OUTPUT_TOGGLE_ENABLED) |
362 __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_DIRECT) |
363 __val2mfld(CTR_MODE_TRIGGER_SOURCE_mask, CTR_MODE_TRIGGER_SOURCE_DISABLED) |
364 __val2mfld(CTR_MODE_TRIGGER_TYPE_mask, CTR_MODE_TRIGGER_TYPE_DISABLED) |
365 __val2mfld(CTR_MODE_RETRIGGER_mask, CTR_MODE_RETRIGGER_DISABLED);
367 /*printf("duty %e: ctr_mode = 0x%08lx T1_uint = 0x%08lx, T2_uint = 0x%08lx\n", duty, ctr_mode, T1_uint, T2_uint);*/
369 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
371 ctr_ctrl = CTRXCTRL_CTR0START_mask;
372 ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
373 mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
375 #endif /*WITHOUT_HW*/
380 #undef MDL_UPDATE /* Change to #undef to remove function */
381 #if defined(MDL_UPDATE)
382 /* Function: mdlUpdate ======================================================
384 * This function is called once for every major integration time step.
385 * Discrete states are typically updated here, but this function is useful
386 * for performing any tasks that should only take place once per
389 static void mdlUpdate(SimStruct *S, int_T tid)
392 #endif /* MDL_UPDATE */
396 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
397 #if defined(MDL_DERIVATIVES)
398 /* Function: mdlDerivatives =================================================
400 * In this function, you compute the S-function block's derivatives.
401 * The derivatives are placed in the derivative vector, ssGetdX(S).
403 static void mdlDerivatives(SimStruct *S)
406 #endif /* MDL_DERIVATIVES */
410 /* Function: mdlTerminate =====================================================
412 * In this function, you should perform any actions that are necessary
413 * at the termination of a simulation. For example, if memory was
414 * allocated in mdlStart, this is the place to free it.
416 static void mdlTerminate(SimStruct *S)
421 if (mf624_check(S) == 0) {
422 /* Force output low when finished */
423 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
424 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
428 #endif /*WITHOUT_HW*/
432 /*======================================================*
433 * See sfuntmpl_doc.c for the optional S-function methods *
434 *======================================================*/
436 /*=============================*
437 * Required S-function trailer *
438 *=============================*/
440 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
441 #include "simulink.c" /* MEX-file interface mechanism */
443 #include "cg_sfun.h" /* Code generation registration function */