2 * S-function to support PWM with direction bit 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 sfPWMwDirOutput
35 #define S_FUNCTION_LEVEL 2
38 * The S-function has next parameters
41 * PWM Frequency - value -1 for external input
42 * Associated digital output for direction
45 #define PRM_CHANNEL(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
46 #define PRM_FREQUENCY(S) (mxGetScalar(ssGetSFcnParam(S, 1)))
47 #define PRM_DIR_DO_BIT(S) (mxGetScalar(ssGetSFcnParam(S, 2)))
51 #define IWORK_IDX_CHANNEL 0
52 #define IWORK_IDX_DIR_DO_BIT 1
53 #define IWORK_IDX_CTR_MODE 2
54 #define IWORK_IDX_USE_FREQUENCY_INPUT 3
55 #define IWORK_IDX_LAST_MODE 4
56 enum {CTR_MODE_ZERO = 0, CTR_MODE_PLUS_PWM = 1, CTR_MODE_PLUS_FULL = 2,
57 CTR_MODE_MINUS_PWM = -1, CTR_MODE_MINUS_FULL = -2};
61 #define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL])
62 #define IWORK_DIR_DO_BIT(S) (ssGetIWork(S)[IWORK_IDX_DIR_DO_BIT])
63 #define IWORK_CTR_MODE(S) (ssGetIWork(S)[IWORK_IDX_CTR_MODE])
64 #define IWORK_USE_FREQUENCY_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_FREQUENCY_INPUT])
65 #define IWORK_LAST_MODE(S) (ssGetIWork(S)[IWORK_IDX_LAST_MODE])
67 #define RWORK_IDX_BASE_FREQUENCY 0
71 #define RWORK_BASE_FREQUENCY(S) (ssGetRWork(S)[RWORK_IDX_BASE_FREQUENCY])
75 * Need to include simstruc.h for the definition of the SimStruct and
76 * its associated macro definitions.
81 #include "mf624_SIMULINK.h"
84 #define CTR_MAX_PWM_CHANNEL 3
96 static const ctr_channel_regs_t ctr_channel2regs[] = {
97 {CTR0STATUS_reg, CTR0MODE_reg, CTR0_reg, CTR0A_reg, CTR0B_reg, CTRXCTRL_reg},
98 {CTR1STATUS_reg, CTR1MODE_reg, CTR1_reg, CTR1A_reg, CTR1B_reg, CTRXCTRL_reg},
99 {CTR2STATUS_reg, CTR2MODE_reg, CTR2_reg, CTR2A_reg, CTR2B_reg, CTRXCTRL_reg},
100 {CTR3STATUS_reg, CTR3MODE_reg, CTR3_reg, CTR3A_reg, CTR3B_reg, CTRXCTRL_reg},
101 {CTR4STATUS_reg, CTR4MODE_reg, CTR4_reg, CTR4A_reg, -1, CTRXCTRL_reg}
103 #endif /*WITHOUT_HW*/
108 * You should use the following technique to report errors encountered within
111 * ssSetErrorStatus(S,"Error encountered due to ...");
114 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
115 * It cannot be a local variable. For example the following will cause
116 * unpredictable errors:
120 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
121 * sprintf(msg,"Error due to %s", string);
122 * ssSetErrorStatus(S,msg);
126 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
130 static void fncSetDirOutput(SimStruct *S, int_T direction_mode)
132 if (mf624_check(S) != 0)
135 /* FIXME consider locking there */
136 if(direction_mode >= 0) {
137 mfst->DOut |= (1 << IWORK_DIR_DO_BIT(S));
139 mfst->DOut &= ~(1 << IWORK_DIR_DO_BIT(S));
142 mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
144 #endif /*WITHOUT_HW*/
146 /*====================*
147 * S-function methods *
148 *====================*/
150 #define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
151 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
152 /* Function: mdlCheckParameters =============================================
154 * mdlCheckParameters verifies new parameter settings whenever parameter
155 * change or are re-evaluated during a simulation. When a simulation is
156 * running, changes to S-function parameters can occur at any time during
157 * the simulation loop.
159 static void mdlCheckParameters(SimStruct *S)
161 if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > CTR_MAX_PWM_CHANNEL))
162 ssSetErrorStatus(S, "valid PWM channel is 0, 1, 2, or 3");
163 if ((PRM_FREQUENCY(S) <= 0) && (PRM_FREQUENCY(S) != -1))
164 ssSetErrorStatus(S, "Frequency out of valid range");
165 if ((PRM_DIR_DO_BIT(S) < 0) || (PRM_DIR_DO_BIT(S) > 7) )
166 ssSetErrorStatus(S, "Invalid direction output specification (0 to 7 supported)");
168 #endif /* MDL_CHECK_PARAMETERS */
172 /* Function: mdlInitializeSizes ===============================================
174 * The sizes information is used by Simulink to determine the S-function
175 * block's characteristics (number of inputs, outputs, states, etc.).
177 static void mdlInitializeSizes(SimStruct *S)
179 int_T nInputPorts = 1;
181 ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */
182 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
183 /* Return if number of expected != number of actual parameters */
184 ssSetErrorStatus(S, "2-parameters requited: Channel, PWM Frequncy or -1");
188 ssSetNumContStates(S, 0);
189 ssSetNumDiscStates(S, 0);
191 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
192 mdlCheckParameters(S);
193 if (ssGetErrorStatus(S) != NULL) return;
196 if (PRM_FREQUENCY(S) == -1)
199 if (!ssSetNumInputPorts(S, nInputPorts)) return;
200 ssSetInputPortWidth(S, 0, 1);
201 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
203 if (PRM_FREQUENCY(S) == -1) {
204 ssSetInputPortWidth(S, 1, 1);
205 ssSetInputPortRequiredContiguous(S, 1, true); /*direct input signal access*/
209 * Set direct feedthrough flag (1=yes, 0=no).
210 * A port has direct feedthrough if the input is used in either
211 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
212 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
214 ssSetInputPortDirectFeedThrough(S, 0, 0);
216 if (!ssSetNumOutputPorts(S, 0)) return;
218 ssSetNumSampleTimes(S, 1);
219 ssSetNumRWork(S, RWORK_COUNT);
220 ssSetNumIWork(S, IWORK_COUNT);
223 ssSetNumNonsampledZCs(S, 0);
225 /* Specify the sim state compliance to be same as a built-in block */
226 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
233 /* Function: mdlInitializeSampleTimes =========================================
235 * This function is used to specify the sample time(s) for your
236 * S-function. You must register the same number of sample times as
237 * specified in ssSetNumSampleTimes.
239 static void mdlInitializeSampleTimes(SimStruct *S)
241 ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
242 ssSetOffsetTime(S, 0, 0.0);
247 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
248 #if defined(MDL_INITIALIZE_CONDITIONS)
249 /* Function: mdlInitializeConditions ========================================
251 * In this function, you should initialize the continuous and discrete
252 * states for your S-function block. The initial states are placed
253 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
254 * You can also perform any other initialization activities that your
255 * S-function may require. Note, this routine will be called at the
256 * start of simulation and if it is present in an enabled subsystem
257 * configured to reset states, it will be call when the enabled subsystem
258 * restarts execution to reset the states.
260 static void mdlInitializeConditions(SimStruct *S)
263 #endif /* MDL_INITIALIZE_CONDITIONS */
267 #define MDL_START /* Change to #undef to remove function */
268 #if defined(MDL_START)
269 /* Function: mdlStart =======================================================
271 * This function is called once at start of model execution. If you
272 * have states that should be initialized once, this is the place
275 static void mdlStart(SimStruct *S)
280 if (mf624_init(NULL) != 0)
283 IWORK_CHANNEL(S) = PRM_CHANNEL(S);
284 IWORK_DIR_DO_BIT(S) = PRM_DIR_DO_BIT(S);
286 IWORK_USE_FREQUENCY_INPUT(S) = (PRM_FREQUENCY(S) == -1)? 1: 0;
288 RWORK_BASE_FREQUENCY(S) = 50e6;
290 IWORK_CTR_MODE(S) = 0;
292 IWORK_LAST_MODE(S) = CTR_MODE_ZERO;
294 /* Force output low during startup */
295 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
296 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
297 #endif /*WITHOUT_HW*/
299 #endif /* MDL_START */
303 /* Function: mdlOutputs =======================================================
305 * In this function, you compute the outputs of your S-function
308 static void mdlOutputs(SimStruct *S, int_T tid)
310 real_T duty = *(const real_T*) ssGetInputPortSignal(S, 0);
321 if (mf624_check(S) != 0)
324 if (IWORK_USE_FREQUENCY_INPUT(S))
325 frequency = *(const real_T*) ssGetInputPortSignal(S, 1);
327 frequency = PRM_FREQUENCY(S);
331 if (IWORK_LAST_MODE(S) != CTR_MODE_PLUS_FULL) {
332 IWORK_LAST_MODE(S) = CTR_MODE_PLUS_FULL;
333 fncSetDirOutput(S, CTR_MODE_PLUS_FULL);
334 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_HI);
335 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
336 /*printf("duty >=1: ctr_mode = 0x%08lx\n", ctr_mode);*/
343 if (IWORK_LAST_MODE(S) != CTR_MODE_MINUS_FULL) {
344 IWORK_LAST_MODE(S) = CTR_MODE_MINUS_FULL;
345 fncSetDirOutput(S, CTR_MODE_MINUS_FULL);
346 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_HI);
347 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
348 /*printf("duty >=1: ctr_mode = 0x%08lx\n", ctr_mode);*/
353 new_mode = CTR_MODE_PLUS_PWM;
355 new_mode = CTR_MODE_MINUS_PWM;
359 T = RWORK_BASE_FREQUENCY(S) / frequency;
362 if ((T1 < 0.5) || (frequency <= 0) || (frequency > RWORK_BASE_FREQUENCY(S) / 2))
364 if (IWORK_LAST_MODE(S) != CTR_MODE_ZERO) {
365 IWORK_LAST_MODE(S) = CTR_MODE_ZERO;
366 fncSetDirOutput(S, CTR_MODE_ZERO);
367 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
368 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
369 /*printf("duty <=0: ctr_mode = 0x%08lx\n", ctr_mode);*/
377 T2_uint = 0xffffffff;
379 if (T1 <= 0xffffffff)
382 T1_uint = 0xffffffff;
386 if (T1_uint >= T2_uint)
387 T1_uint = T2_uint - 1;
391 if (IWORK_LAST_MODE(S) == new_mode) {
392 mf624_write32(T1_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].A_reg));
393 mf624_write32(T2_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].B_reg));
395 IWORK_LAST_MODE(S) = new_mode;
397 ctr_ctrl = CTRXCTRL_CTR0STOP_mask | CTRXCTRL_CTR0LOAD_mask | CTRXCTRL_CTR0RESET_mask | CTRXCTRL_CTR0TRESET_mask;
398 ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
399 mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
401 fncSetDirOutput(S, new_mode);
402 mf624_write32(T1_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].A_reg));
403 mf624_write32(T2_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].B_reg));
405 ctr_mode = IWORK_CTR_MODE(S) |
406 __val2mfld(CTR_MODE_COUNT_DIR_mask, CTR_MODE_COUNT_DIR_DOWN) |
407 __val2mfld(CTR_MODE_REPETITION_mask, CTR_MODE_REPETITION_ENABLED) |
408 __val2mfld(CTR_MODE_LOAD_TOGGLE_mask, CTR_MODE_LOAD_TOGGLE_ENABLED) |
409 __val2mfld(CTR_MODE_OUTPUT_TOGGLE_mask, CTR_MODE_OUTPUT_TOGGLE_ENABLED) |
410 __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_DIRECT) |
411 __val2mfld(CTR_MODE_TRIGGER_SOURCE_mask, CTR_MODE_TRIGGER_SOURCE_DISABLED) |
412 __val2mfld(CTR_MODE_TRIGGER_TYPE_mask, CTR_MODE_TRIGGER_TYPE_DISABLED) |
413 __val2mfld(CTR_MODE_RETRIGGER_mask, CTR_MODE_RETRIGGER_DISABLED);
415 /*printf("duty %e: ctr_mode = 0x%08lx T1_uint = 0x%08lx, T2_uint = 0x%08lx\n", duty, ctr_mode, T1_uint, T2_uint);*/
417 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
419 ctr_ctrl = CTRXCTRL_CTR0START_mask;
420 ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
421 mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
423 #endif /*WITHOUT_HW*/
428 #undef MDL_UPDATE /* Change to #undef to remove function */
429 #if defined(MDL_UPDATE)
430 /* Function: mdlUpdate ======================================================
432 * This function is called once for every major integration time step.
433 * Discrete states are typically updated here, but this function is useful
434 * for performing any tasks that should only take place once per
437 static void mdlUpdate(SimStruct *S, int_T tid)
440 #endif /* MDL_UPDATE */
444 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
445 #if defined(MDL_DERIVATIVES)
446 /* Function: mdlDerivatives =================================================
448 * In this function, you compute the S-function block's derivatives.
449 * The derivatives are placed in the derivative vector, ssGetdX(S).
451 static void mdlDerivatives(SimStruct *S)
454 #endif /* MDL_DERIVATIVES */
458 /* Function: mdlTerminate =====================================================
460 * In this function, you should perform any actions that are necessary
461 * at the termination of a simulation. For example, if memory was
462 * allocated in mdlStart, this is the place to free it.
464 static void mdlTerminate(SimStruct *S)
469 if (mf624_check(S) == 0) {
470 /* Force output low when finished */
471 ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
472 mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
476 #endif /*WITHOUT_HW*/
480 /*======================================================*
481 * See sfuntmpl_doc.c for the optional S-function methods *
482 *======================================================*/
484 /*=============================*
485 * Required S-function trailer *
486 *=============================*/
488 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
489 #include "simulink.c" /* MEX-file interface mechanism */
491 #include "cg_sfun.h" /* Code generation registration function */