]> rtime.felk.cvut.cz Git - mf624-simulink.git/commitdiff
New sfPWMwDirOutput combining PWM with digital output bit for direction.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Sun, 16 Feb 2014 18:29:17 +0000 (19:29 +0100)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Sun, 16 Feb 2014 18:29:17 +0000 (19:29 +0100)
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Makefile
sfPWMwDirOutput.c [new file with mode: 0644]

index cefc6928c3bec9b81e5982963f46f9d02b20a62d..620da701ed644602bb7d697cfacf40d17c8da438 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,11 @@
-MEXFLAGS ?= -f ./mexopts.sh
+MEXFLAGS ?= -f ./mexopts.sh -g
 
 all:
        mex $(MEXFLAGS) sfAnalogInput.c   mf624_SIMULINK.c
        mex $(MEXFLAGS) sfAnalogOutput.c  mf624_SIMULINK.c
        mex $(MEXFLAGS) sfDigitalOutput.c mf624_SIMULINK.c
        mex $(MEXFLAGS) sfDigitalInput.c  mf624_SIMULINK.c
-       mex $(MEXFLAGS) sfReadPWM.c       mf624_SIMULINK.c
-       mex $(MEXFLAGS) sfIRCInput.c      mf624_SIMULINK.c
-       mex $(MEXFLAGS) sfPWMOutput.c     mf624_SIMULINK.c
+       mex $(MEXFLAGS) sfReadPWM.c       mf624_SIMULINK.c
+       mex $(MEXFLAGS) sfIRCInput.c      mf624_SIMULINK.c
+       mex $(MEXFLAGS) sfPWMOutput.c     mf624_SIMULINK.c
+       mex $(MEXFLAGS) sfPWMwDirOutput.c mf624_SIMULINK.c
diff --git a/sfPWMwDirOutput.c b/sfPWMwDirOutput.c
new file mode 100644 (file)
index 0000000..ebae4c2
--- /dev/null
@@ -0,0 +1,479 @@
+/*
+ * S-function to support PWM with direction bit on Humusoft MF624 card
+ *
+ * (C) Copyright 2014 Pavel Pisa <pisa@cmp.felk.cvut.cz>
+ *
+ * Department of Control Engineering
+ * Faculty of Electrical Engineering
+ * Czech Technical University in Prague (CTU)
+ *
+ * The S-Function for ERT Linux can be distributed in compliance
+ * with GNU General Public License (GPL) version 2 or later.
+ * Other licence can negotiated with CTU.
+ *
+ * Next exception is granted in addition to GPL.
+ * Instantiating or linking compiled version of this code
+ * to produce an application image/executable, does not
+ * by itself cause the resulting application image/executable
+ * to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons
+ * why the executable file might be covered by the GNU Public License.
+ * Publication of enhanced or derived S-function files is required
+ * although.
+ *
+ * Linux ERT code is available from
+ *    http://rtime.felk.cvut.cz/gitweb/ert_linux.git
+ * More CTU Linux target for Simulink components are available at
+ *    http://lintarget.sourceforge.net/
+ *
+ * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
+ * required S-function structure.
+ */
+
+
+#define S_FUNCTION_NAME  sfPWMwDirOutput
+#define S_FUNCTION_LEVEL 2
+
+/*
+ * The S-function has next parameters
+ *
+ * PWM Channel number
+ * PWM Frequency   - value -1 for external input
+ * Associated digital output for direction
+ */
+
+#define PRM_CHANNEL(S)          (mxGetScalar(ssGetSFcnParam(S, 0)))
+#define PRM_FREQUENCY(S)        (mxGetScalar(ssGetSFcnParam(S, 1)))
+#define PRM_DIR_DO_BIT(S)       (mxGetScalar(ssGetSFcnParam(S, 2)))
+
+#define PRM_COUNT                      3
+
+#define IWORK_IDX_CHANNEL              0
+#define IWORK_IDX_DIR_DO_BIT           1
+#define IWORK_IDX_CTR_MODE             2
+#define IWORK_IDX_USE_FREQUENCY_INPUT  3
+#define IWORK_IDX_LAST_MODE            4
+enum {CTR_MODE_ZERO = 0, CTR_MODE_PLUS_PWM = 1, CTR_MODE_PLUS_FULL = 2,
+                          CTR_MODE_MINUS_PWM = -1, CTR_MODE_MINUS_FULL = -2};
+
+#define IWORK_COUNT                    4
+
+#define IWORK_CHANNEL(S)               (ssGetIWork(S)[IWORK_IDX_CHANNEL])
+#define IWORK_DIR_DO_BIT(S)            (ssGetIWork(S)[IWORK_IDX_DIR_DO_BIT])
+#define IWORK_CTR_MODE(S)              (ssGetIWork(S)[IWORK_IDX_CTR_MODE])
+#define IWORK_USE_FREQUENCY_INPUT(S)   (ssGetIWork(S)[IWORK_IDX_USE_FREQUENCY_INPUT])
+#define IWORK_LAST_MODE(S)             (ssGetIWork(S)[IWORK_IDX_LAST_MODE])
+
+#define RWORK_IDX_BASE_FREQUENCY       0
+
+#define RWORK_COUNT                    1
+
+#define RWORK_BASE_FREQUENCY(S)        (ssGetRWork(S)[RWORK_IDX_BASE_FREQUENCY])
+
+
+/*
+ * Need to include simstruc.h for the definition of the SimStruct and
+ * its associated macro definitions.
+ */
+#include "simstruc.h"
+#include "mf624_SIMULINK.h"
+
+#define CTR_MAX_PWM_CHANNEL            3
+
+typedef struct {
+    int32_T STATUS_reg;
+    int32_T MODE_reg;
+    int32_T CTR_reg;
+    int32_T A_reg;
+    int32_T B_reg;
+    int32_T CTRL_reg;
+} ctr_channel_regs_t;
+
+static const ctr_channel_regs_t ctr_channel2regs[] = {
+    {CTR0STATUS_reg, CTR0MODE_reg, CTR0_reg, CTR0A_reg, CTR0B_reg, CTRXCTRL_reg},
+    {CTR1STATUS_reg, CTR1MODE_reg, CTR1_reg, CTR1A_reg, CTR1B_reg, CTRXCTRL_reg},
+    {CTR2STATUS_reg, CTR2MODE_reg, CTR2_reg, CTR2A_reg, CTR2B_reg, CTRXCTRL_reg},
+    {CTR3STATUS_reg, CTR3MODE_reg, CTR3_reg, CTR3A_reg, CTR3B_reg, CTRXCTRL_reg},
+    {CTR4STATUS_reg, CTR4MODE_reg, CTR4_reg, CTR4A_reg, -1,        CTRXCTRL_reg}
+};
+
+/* Error handling
+ * --------------
+ *
+ * You should use the following technique to report errors encountered within
+ * an S-function:
+ *
+ *       ssSetErrorStatus(S,"Error encountered due to ...");
+ *       return;
+ *
+ * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
+ * It cannot be a local variable. For example the following will cause
+ * unpredictable errors:
+ *
+ *      mdlOutputs()
+ *      {
+ *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
+ *         sprintf(msg,"Error due to %s", string);
+ *         ssSetErrorStatus(S,msg);
+ *         return;
+ *      }
+ *
+ * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
+ */
+
+static void fncSetDirOutput(SimStruct *S, int_T direction_mode)
+{
+    if (mf624_check(S) != 0)
+        return;
+
+    /* FIXME consider locking there */
+    if(direction_mode >= 0) {
+        mfst->DOut |= (1 << IWORK_DIR_DO_BIT(S));
+    } else {
+        mfst->DOut &= ~(1 << IWORK_DIR_DO_BIT(S));
+    }
+
+    mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
+}
+
+/*====================*
+ * S-function methods *
+ *====================*/
+
+#define MDL_CHECK_PARAMETERS   /* Change to #undef to remove function */
+#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
+  /* Function: mdlCheckParameters =============================================
+   * Abstract:
+   *    mdlCheckParameters verifies new parameter settings whenever parameter
+   *    change or are re-evaluated during a simulation. When a simulation is
+   *    running, changes to S-function parameters can occur at any time during
+   *    the simulation loop.
+   */
+static void mdlCheckParameters(SimStruct *S)
+{
+    if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > CTR_MAX_PWM_CHANNEL))
+        ssSetErrorStatus(S, "valid PWM channel is 0, 1, 2, or 3");
+    if ((PRM_FREQUENCY(S) <= 0) && (PRM_FREQUENCY(S) != -1))
+        ssSetErrorStatus(S, "Frequency out of valid range");
+    if ((PRM_DIR_DO_BIT(S) < 0) || (PRM_DIR_DO_BIT(S) > 7) )
+        ssSetErrorStatus(S, "Invalid direction output specification (0 to 7 supported)");
+}
+#endif /* MDL_CHECK_PARAMETERS */
+
+
+
+/* Function: mdlInitializeSizes ===============================================
+ * Abstract:
+ *    The sizes information is used by Simulink to determine the S-function
+ *    block's characteristics (number of inputs, outputs, states, etc.).
+ */
+static void mdlInitializeSizes(SimStruct *S)
+{
+    int_T nInputPorts  = 1;
+
+    ssSetNumSFcnParams(S, PRM_COUNT);  /* Number of expected parameters */
+    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
+        /* Return if number of expected != number of actual parameters */
+        ssSetErrorStatus(S, "2-parameters requited: Channel, PWM Frequncy or -1");
+        return;
+    }
+
+    ssSetNumContStates(S, 0);
+    ssSetNumDiscStates(S, 0);
+
+  #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
+    mdlCheckParameters(S);
+    if (ssGetErrorStatus(S) != NULL) return;
+  #endif
+
+    if (PRM_FREQUENCY(S) == -1)
+        nInputPorts++;
+
+    if (!ssSetNumInputPorts(S, nInputPorts)) return;
+    ssSetInputPortWidth(S, 0, 1);
+    ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
+
+    if (PRM_FREQUENCY(S) == -1) {
+        ssSetInputPortWidth(S, 1, 1);
+        ssSetInputPortRequiredContiguous(S, 1, true); /*direct input signal access*/
+    }
+
+    /*
+     * Set direct feedthrough flag (1=yes, 0=no).
+     * A port has direct feedthrough if the input is used in either
+     * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
+     * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
+     */
+    ssSetInputPortDirectFeedThrough(S, 0, 0);
+
+    if (!ssSetNumOutputPorts(S, 0)) return;
+
+    ssSetNumSampleTimes(S, 1);
+    ssSetNumRWork(S, RWORK_COUNT);
+    ssSetNumIWork(S, IWORK_COUNT);
+    ssSetNumPWork(S, 0);
+    ssSetNumModes(S, 0);
+    ssSetNumNonsampledZCs(S, 0);
+
+    /* Specify the sim state compliance to be same as a built-in block */
+    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
+
+    ssSetOptions(S, 0);
+}
+
+
+
+/* Function: mdlInitializeSampleTimes =========================================
+ * Abstract:
+ *    This function is used to specify the sample time(s) for your
+ *    S-function. You must register the same number of sample times as
+ *    specified in ssSetNumSampleTimes.
+ */
+static void mdlInitializeSampleTimes(SimStruct *S)
+{
+    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
+    ssSetOffsetTime(S, 0, 0.0);
+}
+
+
+
+#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
+#if defined(MDL_INITIALIZE_CONDITIONS)
+  /* Function: mdlInitializeConditions ========================================
+   * Abstract:
+   *    In this function, you should initialize the continuous and discrete
+   *    states for your S-function block.  The initial states are placed
+   *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
+   *    You can also perform any other initialization activities that your
+   *    S-function may require. Note, this routine will be called at the
+   *    start of simulation and if it is present in an enabled subsystem
+   *    configured to reset states, it will be call when the enabled subsystem
+   *    restarts execution to reset the states.
+   */
+  static void mdlInitializeConditions(SimStruct *S)
+  {
+  }
+#endif /* MDL_INITIALIZE_CONDITIONS */
+
+
+
+#define MDL_START  /* Change to #undef to remove function */
+#if defined(MDL_START)
+  /* Function: mdlStart =======================================================
+   * Abstract:
+   *    This function is called once at start of model execution. If you
+   *    have states that should be initialized once, this is the place
+   *    to do it.
+   */
+static void mdlStart(SimStruct *S)
+{
+    int32_T ctr_mode;
+
+    if (mf624_init(NULL) != 0)
+        return;
+
+    IWORK_CHANNEL(S) = PRM_CHANNEL(S);
+    IWORK_DIR_DO_BIT(S) = PRM_DIR_DO_BIT(S);
+
+    IWORK_USE_FREQUENCY_INPUT(S) = (PRM_FREQUENCY(S) == -1)? 1: 0;
+
+    RWORK_BASE_FREQUENCY(S) = 50e6;
+
+    IWORK_CTR_MODE(S) = 0;
+
+    IWORK_LAST_MODE(S) = CTR_MODE_ZERO;
+
+    /* Force output low during startup */
+    ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
+    mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+}
+#endif /*  MDL_START */
+
+
+
+/* Function: mdlOutputs =======================================================
+ * Abstract:
+ *    In this function, you compute the outputs of your S-function
+ *    block.
+ */
+static void mdlOutputs(SimStruct *S, int_T tid)
+{
+    real_T duty = *(const real_T*) ssGetInputPortSignal(S, 0);
+    real_T frequency;
+    int32_T ctr_mode;
+    int32_T ctr_ctrl;
+    real_T T;
+    real_T T1;
+    uint32_T T1_uint;
+    uint32_T T2_uint;
+    int_T new_mode;
+
+    if (mf624_check(S) != 0)
+            return;
+
+    if (IWORK_USE_FREQUENCY_INPUT(S))
+        frequency = *(const real_T*) ssGetInputPortSignal(S, 1);
+    else
+        frequency = PRM_FREQUENCY(S);
+
+    if (duty >= 1)
+    {
+        if (IWORK_LAST_MODE(S) != CTR_MODE_PLUS_FULL) {
+            IWORK_LAST_MODE(S) = CTR_MODE_PLUS_FULL;
+            fncSetDirOutput(S, CTR_MODE_PLUS_FULL);
+            ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_HI);
+            mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+            /*printf("duty >=1: ctr_mode = 0x%08lx\n", ctr_mode);*/
+        }
+        return;
+    }
+
+    if (duty <= -1)
+    {
+        if (IWORK_LAST_MODE(S) != CTR_MODE_MINUS_FULL) {
+            IWORK_LAST_MODE(S) = CTR_MODE_MINUS_FULL;
+            fncSetDirOutput(S, CTR_MODE_MINUS_FULL);
+            ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_HI);
+            mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+            /*printf("duty >=1: ctr_mode = 0x%08lx\n", ctr_mode);*/
+        }
+        return;
+    }
+
+    new_mode = CTR_MODE_PLUS_PWM;
+    if (duty < 0) {
+      new_mode = CTR_MODE_MINUS_PWM;
+      duty = -duty;
+    }
+
+    T = RWORK_BASE_FREQUENCY(S) / frequency;
+    T1 = T * duty;
+
+    if ((T1 < 0.5) || (frequency <= 0) || (frequency > RWORK_BASE_FREQUENCY(S) / 2))
+    {
+        if (IWORK_LAST_MODE(S) != CTR_MODE_ZERO) {
+            IWORK_LAST_MODE(S) = CTR_MODE_ZERO;
+            fncSetDirOutput(S, CTR_MODE_ZERO);
+            ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
+            mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+            /*printf("duty <=0: ctr_mode = 0x%08lx\n", ctr_mode);*/
+        }
+        return;
+    }
+
+    if (T <= 0xffffffff)
+      T2_uint = T;
+    else
+      T2_uint = 0xffffffff;
+
+    if (T1 <= 0xffffffff)
+      T1_uint = T1;
+    else
+      T1_uint = 0xffffffff;
+
+    if (!T1_uint)
+        T1_uint = 1;
+    if (T1_uint >= T2_uint)
+        T1_uint = T2_uint - 1;
+
+    T2_uint -= T1_uint;
+
+    if (IWORK_LAST_MODE(S) == new_mode) {
+        mf624_write32(T1_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].A_reg));
+        mf624_write32(T2_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].B_reg));
+    } else {
+        IWORK_LAST_MODE(S) = new_mode;
+
+        ctr_ctrl = CTRXCTRL_CTR0STOP_mask | CTRXCTRL_CTR0LOAD_mask | CTRXCTRL_CTR0RESET_mask | CTRXCTRL_CTR0TRESET_mask;
+        ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
+        mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
+
+        fncSetDirOutput(S, new_mode);
+        mf624_write32(T1_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].A_reg));
+        mf624_write32(T2_uint, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].B_reg));
+
+        ctr_mode = IWORK_CTR_MODE(S) |
+                   __val2mfld(CTR_MODE_COUNT_DIR_mask, CTR_MODE_COUNT_DIR_DOWN) |
+                   __val2mfld(CTR_MODE_REPETITION_mask, CTR_MODE_REPETITION_ENABLED) |
+                   __val2mfld(CTR_MODE_LOAD_TOGGLE_mask, CTR_MODE_LOAD_TOGGLE_ENABLED) |
+                   __val2mfld(CTR_MODE_OUTPUT_TOGGLE_mask, CTR_MODE_OUTPUT_TOGGLE_ENABLED) |
+                   __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_DIRECT) |
+                   __val2mfld(CTR_MODE_TRIGGER_SOURCE_mask, CTR_MODE_TRIGGER_SOURCE_DISABLED) |
+                   __val2mfld(CTR_MODE_TRIGGER_TYPE_mask, CTR_MODE_TRIGGER_TYPE_DISABLED) |
+                   __val2mfld(CTR_MODE_RETRIGGER_mask, CTR_MODE_RETRIGGER_DISABLED);
+
+        /*printf("duty %e: ctr_mode = 0x%08lx T1_uint = 0x%08lx, T2_uint = 0x%08lx\n", duty, ctr_mode, T1_uint, T2_uint);*/
+
+        mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+
+        ctr_ctrl = CTRXCTRL_CTR0START_mask;
+        ctr_ctrl <<= IWORK_CHANNEL(S) * CTRXCTRL_CHANNEL_SHIFT;
+        mf624_write32(ctr_ctrl, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].CTRL_reg));
+    }
+}
+
+
+
+#undef MDL_UPDATE  /* Change to #undef to remove function */
+#if defined(MDL_UPDATE)
+  /* Function: mdlUpdate ======================================================
+   * Abstract:
+   *    This function is called once for every major integration time step.
+   *    Discrete states are typically updated here, but this function is useful
+   *    for performing any tasks that should only take place once per
+   *    integration step.
+   */
+  static void mdlUpdate(SimStruct *S, int_T tid)
+  {
+  }
+#endif /* MDL_UPDATE */
+
+
+
+#undef MDL_DERIVATIVES  /* Change to #undef to remove function */
+#if defined(MDL_DERIVATIVES)
+  /* Function: mdlDerivatives =================================================
+   * Abstract:
+   *    In this function, you compute the S-function block's derivatives.
+   *    The derivatives are placed in the derivative vector, ssGetdX(S).
+   */
+  static void mdlDerivatives(SimStruct *S)
+  {
+  }
+#endif /* MDL_DERIVATIVES */
+
+
+
+/* Function: mdlTerminate =====================================================
+ * Abstract:
+ *    In this function, you should perform any actions that are necessary
+ *    at the termination of a simulation.  For example, if memory was
+ *    allocated in mdlStart, this is the place to free it.
+ */
+static void mdlTerminate(SimStruct *S)
+{
+    int32_T ctr_mode;
+
+    if (mf624_check(S) == 0) {
+        /* Force output low when finished */
+        ctr_mode = __val2mfld(CTR_MODE_OUTPUT_CONTROL_mask, CTR_MODE_OUTPUT_CONTROL_FORCE_LO);
+        mf624_write32(ctr_mode, MFST2REG(mfst, 4, ctr_channel2regs[IWORK_CHANNEL(S)].MODE_reg));
+    }
+
+    mf624_done();
+}
+
+
+/*======================================================*
+ * See sfuntmpl_doc.c for the optional S-function methods *
+ *======================================================*/
+
+/*=============================*
+ * Required S-function trailer *
+ *=============================*/
+
+#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
+#include "simulink.c"      /* MEX-file interface mechanism */
+#else
+#include "cg_sfun.h"       /* Code generation registration function */
+#endif