]> rtime.felk.cvut.cz Git - mf624-simulink.git/commitdiff
Implemented basic PWM output block for MF624.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 15 Feb 2014 16:11:31 +0000 (17:11 +0100)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 15 Feb 2014 16:11:31 +0000 (17:11 +0100)
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Makefile
sfPWMOutput.c [new file with mode: 0644]

index 927a3a3b175298c327e212fa42c4e27796aad90d..cefc6928c3bec9b81e5982963f46f9d02b20a62d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,3 +7,4 @@ all:
        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
diff --git a/sfPWMOutput.c b/sfPWMOutput.c
new file mode 100644 (file)
index 0000000..eaad4e9
--- /dev/null
@@ -0,0 +1,406 @@
+/*
+ * S-function to support IRC inputs on Humusoft MF624 card
+ *
+ * Based on sfuntmpl_basic.c by The MathWorks, Inc.
+ */
+
+
+#define S_FUNCTION_NAME  sfPWMOutput
+#define S_FUNCTION_LEVEL 2
+
+/*
+ * The S-function has next parameters
+ *
+ * PWM Channel number
+ * PWM Frequency   - value -1 for external input
+ */
+
+#define PRM_CHANNEL(S)          (mxGetScalar(ssGetSFcnParam(S, 0)))
+#define PRM_FREQUENCY(S)        (mxGetScalar(ssGetSFcnParam(S, 1)))
+
+#define PRM_COUNT                      2
+
+#define IWORK_IDX_CHANNEL              0
+#define IWORK_IDX_CTR_MODE             1
+#define IWORK_IDX_USE_FREQUENCY_INPUT  2
+#define IWORK_IDX_LAST_MODE            3
+enum {CTR_LAST_MODE_OUT0, CTR_LAST_MODE_OUT1, CTR_LAST_MODE_PWM};
+
+#define IWORK_COUNT                    4
+
+#define IWORK_CHANNEL(S)               (ssGetIWork(S)[IWORK_IDX_CHANNEL])
+#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.
+ */
+
+/*====================*
+ * 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");
+}
+#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_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_LAST_MODE_OUT0;
+
+    /* 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;
+
+    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_LAST_MODE_OUT1) {
+            IWORK_LAST_MODE(S) = CTR_LAST_MODE_OUT1;
+            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<=0) || (frequency <= 0) || (frequency > RWORK_BASE_FREQUENCY(S) / 2))
+    {
+        if (IWORK_LAST_MODE(S) != CTR_LAST_MODE_OUT0) {
+            IWORK_LAST_MODE(S) = CTR_LAST_MODE_OUT0;
+            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;
+    }
+
+    T = RWORK_BASE_FREQUENCY(S) / frequency;
+
+    T1 = T * duty;
+
+    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;
+
+    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));
+
+    if (IWORK_LAST_MODE(S) != CTR_LAST_MODE_PWM) {
+        IWORK_LAST_MODE(S) = CTR_LAST_MODE_PWM;
+
+        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));
+
+        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