]> rtime.felk.cvut.cz Git - mf624-simulink.git/commitdiff
Created s-functions for DigitalOutputs and for reading PWM via counters.
authorkrecmich <krecmich@emotor.felk.cvut.cz>
Mon, 1 Jul 2013 12:48:43 +0000 (14:48 +0200)
committerkrecmich <krecmich@emotor.felk.cvut.cz>
Mon, 1 Jul 2013 12:48:43 +0000 (14:48 +0200)
Signed-off-by: krecmich <krecmich@emotor.felk.cvut.cz>
sfDigitalOutput.c [new file with mode: 0644]
sfReadPWM.c [new file with mode: 0644]

diff --git a/sfDigitalOutput.c b/sfDigitalOutput.c
new file mode 100644 (file)
index 0000000..61dc5b1
--- /dev/null
@@ -0,0 +1,255 @@
+/*
+ * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
+ *
+ *  -------------------------------------------------------------------------
+ *  | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
+ *  -------------------------------------------------------------------------
+ *
+ * Copyright 1990-2002 The MathWorks, Inc.
+ * $Revision: 1.27.4.2 $
+ */
+
+
+/*
+ * You must specify the S_FUNCTION_NAME as the name of your S-function
+ * (i.e. replace sfuntmpl_basic with the name of your S-function).
+ */
+
+#define S_FUNCTION_NAME  sfDigitalOutput
+#define S_FUNCTION_LEVEL 2
+
+#define CHNL_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
+
+/*
+ * Need to include simstruc.h for the definition of the SimStruct and
+ * its associated macro definitions.
+ */
+#include "simstruc.h"
+#include "mf624_SIMULINK.h"
+
+/* 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 *
+ *====================*/
+
+/* 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)
+{
+    /* See sfuntmpl_doc.c for more details on the macros below */
+
+    ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
+    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
+        /* Return if number of expected != number of actual parameters */
+        return;
+    }
+
+    ssSetNumContStates(S, 0);
+    ssSetNumDiscStates(S, 0);
+
+    if (!ssSetNumInputPorts(S, 1)) return;
+    ssSetInputPortWidth(S, 0, 1);
+    ssSetInputPortRequiredContiguous(S, 0, 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, 1);
+
+    if (!ssSetNumOutputPorts(S, 0)) return;
+
+    ssSetNumSampleTimes(S, 1);
+    ssSetNumRWork(S, 0);
+    ssSetNumIWork(S, 0);
+    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, -1);
+    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)
+  {
+    if(mfst==NULL){
+        #define BUFF_SMALL             32
+        mfst = malloc(sizeof(mf624_state_t));
+        char buff[BUFF_SMALL];
+        memset(buff, '\0', BUFF_SMALL);
+        mfst->uio_dev = "uio0";
+
+        strncat(buff, "/dev/", 5);
+        strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
+
+        mfst->device_fd = open_device(buff);
+        if (mfst->device_fd < 0) {
+            ssSetErrorStatus(S,"open failed");
+            return;
+        }
+        if (mmap_regions(mfst) < 0) {
+            ssSetErrorStatus(S,"mmap_regions failed");
+            return;
+        }
+    }
+    mfst->DOut=0;
+    
+  }
+#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)
+{
+    const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
+    mf624_state_t* mfst = ssGetPWorkValue(S,0);
+    if(u[0] > 0.5){
+        /*Update the "word" holding Output Values*/
+        mfst->DOut |= (1 << (int)(CHNL_PRM(S)-1));
+    }
+    else {
+        /*Update the "word" holding Output Values*/
+        mfst->DOut &= ~(1 << (int)(CHNL_PRM(S)-1));
+    }
+    
+    mf624_write16(mfst->DOut, MFST2REG(mfst, 2, DOUT_reg));
+}
+
+
+
+#define 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 */
+
+
+
+#define 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)
+{
+    if(mfst!=NULL){
+        free(ssGetPWorkValue(S,0));
+    }
+}
+
+
+/*======================================================*
+ * 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
diff --git a/sfReadPWM.c b/sfReadPWM.c
new file mode 100644 (file)
index 0000000..1f7e01b
--- /dev/null
@@ -0,0 +1,282 @@
+
+#define S_FUNCTION_NAME  sfReadPWM
+#define S_FUNCTION_LEVEL 2
+
+#define PERIOD_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
+#define CTRX_MODE 2083 //=100000100011, count up, repeat, outpu low,gate by ctrIn
+#define CTR4_MODE 35 //100011, count upm, repeat,output low
+#define CTR_START ((1 << 0) | (1 << 6) | (1 << 12) | (1 << 24)) //start counters 0,1,2,4
+#define CTRCLOCK 50000000
+
+/*
+ * Need to include simstruc.h for the definition of the SimStruct and
+ * its associated macro definitions.
+ */
+#include "simstruc.h"
+
+#include "mf624_SIMULINK.h"
+
+/* 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 *
+ *====================*/
+
+/* 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)
+{
+    /* See sfuntmpl_doc.c for more details on the macros below */
+
+    ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
+    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
+        /* Return if number of expected != number of actual parameters */
+        return;
+    }
+
+    ssSetNumContStates(S, 0);
+    ssSetNumDiscStates(S, 0);
+
+    if (!ssSetNumInputPorts(S, 1)) return;
+    ssSetInputPortWidth(S, 0, 1);
+    ssSetInputPortRequiredContiguous(S, 0, 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, 1);
+
+    if (!ssSetNumOutputPorts(S, 3)) return;
+    ssSetOutputPortWidth(S, 0, 1);
+    ssSetOutputPortWidth(S, 1, 1);
+    ssSetOutputPortWidth(S, 2, 1);
+
+    ssSetNumSampleTimes(S, 1);
+    ssSetNumRWork(S, 0);
+    ssSetNumIWork(S, 4);
+    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, CONTINUOUS_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)
+  {
+    if(mfst==NULL){
+        #define BUFF_SMALL             32
+        mfst = malloc(sizeof(mf624_state_t));
+        char buff[BUFF_SMALL];
+        memset(buff, '\0', BUFF_SMALL);
+        mfst->uio_dev = "uio0";
+
+        strncat(buff, "/dev/", 5);
+        strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
+
+        mfst->device_fd = open_device(buff);
+    
+        if (mfst->device_fd < 0) {
+            ssSetErrorStatus(S,"open failed");
+            return;
+        }
+    
+        if (mmap_regions(mfst) < 0) {
+            ssSetErrorStatus(S,"mmap_regions failed");
+               return;
+        }
+    
+    }
+            
+    /*Configuration of desired counter modes*/
+    mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE));
+    mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE));
+    mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE));
+    mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE));
+    
+    /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
+    mf624_write32(0,MFST2REG(mfst,4,CTR0));
+    mf624_write32(0,MFST2REG(mfst,4,CTR1));
+    mf624_write32(0,MFST2REG(mfst,4,CTR2));
+    mf624_write32(0,MFST2REG(mfst,4,CTR4));
+    
+    /*Read values from counters and initialize IWork values with them*/
+    ssSetIWorkValue(S,0,(uint)mf624_read32(MFST2REG(mfst,4,CTR0)));
+    ssSetIWorkValue(S,1,(uint)mf624_read32(MFST2REG(mfst,4,CTR1)));
+    ssSetIWorkValue(S,2,(uint)mf624_read32(MFST2REG(mfst,4,CTR2)));
+    ssSetIWorkValue(S,3,(uint)mf624_read32(MFST2REG(mfst,4,CTR4)));
+    
+    /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
+    mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL));
+            
+    
+  }
+#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)
+{
+    const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
+    real_T       *y0 = ssGetOutputPortSignal(S,0);
+    real_T       *y1 = ssGetOutputPortSignal(S,1);
+    real_T       *y2 = ssGetOutputPortSignal(S,2);
+    uint period;
+    uint c0,c1,c2,c4;
+    
+    
+    c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
+    c1 = mf624_read32(MFST2REG(mfst,4,CTR1));
+    c2 = mf624_read32(MFST2REG(mfst,4,CTR2));
+    c4 = mf624_read32(MFST2REG(mfst,4,CTR4));
+    
+    period = (uint)(c4-(uint)ssGetIWorkValue(S,3));
+    
+    y0[0] = (c0-(uint)ssGetIWorkValue(S,0))/period;
+    y1[0] = (c1-(uint)ssGetIWorkValue(S,1))/period;
+    y2[0] = (c2-(uint)ssGetIWorkValue(S,2))/period;
+    
+    ssSetIWorkValue(S,0,c0);
+    ssSetIWorkValue(S,1,c1);
+    ssSetIWorkValue(S,2,c2);
+    ssSetIWorkValue(S,3,c4);
+}
+
+
+
+#define 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 */
+
+
+
+#define 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)
+{
+    if(mfst!=NULL){
+        free(mfst);
+    }
+}
+
+
+/*======================================================*
+ * 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