]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/commitdiff
Added new templates for Simulink blocks DIN and LOUT.
authorCarlos Jenkins <carlos@jenkins.co.cr>
Wed, 22 May 2013 14:58:57 +0000 (16:58 +0200)
committerCarlos Jenkins <carlos@jenkins.co.cr>
Wed, 22 May 2013 14:58:57 +0000 (16:58 +0200)
rpp/blocks/sfunction_din.c
rpp/blocks/sfunction_lout.c [new file with mode: 0644]
rpp/blocks/tlc_c/sfunction_din.tlc
rpp/blocks/tlc_c/sfunction_lout.tlc [new file with mode: 0644]

index 4e65557020cf1391e4674067642b36314e27d361..151e1cac75b357407aea3e3cfc6b1a4f6d3b0d94 100644 (file)
@@ -16,9 +16,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * File : sfunction_gpio_write.c
+ * File : sfunction_din.c
  * Abstract:
- *     sFunction block for RPP general purpose I/O.
+ *     C-MEX S-function block for RPP digital input.
  *
  * References:
  *     ????
@@ -32,7 +32,7 @@
 /*
  * Must specify the S_FUNCTION_NAME as the name of the S-function.
  */
-#define S_FUNCTION_NAME                sfunction_gpio_write
+#define S_FUNCTION_NAME                sfunction_din
 #define S_FUNCTION_LEVEL               2
 
 /*
diff --git a/rpp/blocks/sfunction_lout.c b/rpp/blocks/sfunction_lout.c
new file mode 100644 (file)
index 0000000..6f2c8a4
--- /dev/null
@@ -0,0 +1,346 @@
+/* Copyright (C) 2013 Czech Technical University in Prague
+ *
+ * Authors:
+ *     - Carlos Jenkins <carlos@jenkins.co.cr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * File : sfunction_lout.c
+ * Abstract:
+ *     C-MEX S-function block for RPP digital output.
+ *
+ * References:
+ *     ????
+ */
+
+
+/*
+ * Must specify the S_FUNCTION_NAME as the name of the S-function.
+ */
+#define S_FUNCTION_NAME                sfunction_lout
+#define S_FUNCTION_LEVEL               2
+
+/*
+ * Need to include simstruc.h for the definition of the SimStruct and
+ * its associated macro definitions.
+ */
+#include "simstruc.h"
+#define EDIT_OK(S, P_IDX) \
+ (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
+#define SAMPLE_TIME                    (ssGetSFcnParam(S, 1))
+
+/*
+ * Utility function prototypes.
+ */
+static bool IsRealMatrix(const mxArray * const m);
+
+#define MDL_CHECK_PARAMETERS
+#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)
+{
+  /*
+   * Check the parameter 1
+   */
+  if EDIT_OK(S, 0) {
+    int_T dimsArray[2] = { 1, 1 };
+
+    /* Check the parameter attributes */
+    ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
+  }
+
+  /*
+   * Check the parameter 2 (sample time)
+   */
+  if EDIT_OK(S, 1) {
+    const double * sampleTime = NULL;
+    const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
+
+    /* Sample time must be a real scalar value or 2 element array. */
+    if (IsRealMatrix(SAMPLE_TIME) &&
+        (stArraySize == 1 || stArraySize == 2) ) {
+      sampleTime = mxGetPr(SAMPLE_TIME);
+    } else {
+      ssSetErrorStatus(S,
+                       "Invalid sample time. Sample time must be a real scalar value or an array of two real values.");
+      return;
+    }
+
+    if (sampleTime[0] < 0.0 && sampleTime[0] != -1.0) {
+      ssSetErrorStatus(S,
+                       "Invalid sample time. Period must be non-negative or -1 (for inherited).");
+      return;
+    }
+
+    if (stArraySize == 2 && sampleTime[0] > 0.0 &&
+        sampleTime[1] >= sampleTime[0]) {
+      ssSetErrorStatus(S,
+                       "Invalid sample time. Offset must be smaller than period.");
+      return;
+    }
+
+    if (stArraySize == 2 && sampleTime[0] == -1.0 && sampleTime[1] != 0.0) {
+      ssSetErrorStatus(S,
+                       "Invalid sample time. When period is -1, offset must be 0.");
+      return;
+    }
+
+    if (stArraySize == 2 && sampleTime[0] == 0.0 &&
+        !(sampleTime[1] == 1.0)) {
+      ssSetErrorStatus(S,
+                       "Invalid sample time. When period is 0, offset must be 1.");
+      return;
+    }
+  }
+}
+
+#endif
+
+/* 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)
+{
+  /* Number of expected parameters */
+  ssSetNumSFcnParams(S, 2);
+
+#if defined(MATLAB_MEX_FILE)
+
+  if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
+    /*
+     * If the number of expected input parameters is not equal
+     * to the number of parameters entered in the dialog box return.
+     * Simulink will generate an error indicating that there is a
+     * parameter mismatch.
+     */
+    mdlCheckParameters(S);
+    if (ssGetErrorStatus(S) != NULL) {
+      return;
+    }
+  } else {
+    /* Return if number of expected != number of actual parameters */
+    return;
+  }
+
+#endif
+
+  /* Set the parameter's tunable status */
+  ssSetSFcnParamTunable(S, 0, 1);
+  ssSetSFcnParamTunable(S, 1, 0);
+
+  ssSetNumPWork(S, 0);
+
+  if (!ssSetNumDWork(S, 0))
+    return;
+
+  /*
+   * Set the number of input ports.
+   */
+  if (!ssSetNumInputPorts(S, 1))
+    return;
+
+  /*
+   * Configure the input port 1
+   */
+  ssSetInputPortDataType(S, 0, SS_UINT8);
+  ssSetInputPortWidth(S, 0, 1);
+  ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);
+  ssSetInputPortDirectFeedThrough(S, 0, 1);
+  ssSetInputPortAcceptExprInRTW(S, 0, 1);
+  ssSetInputPortOverWritable(S, 0, 1);
+  ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
+  ssSetInputPortRequiredContiguous(S, 0, 1);
+
+  /*
+   * Set the number of output ports.
+   */
+  if (!ssSetNumOutputPorts(S, 0))
+    return;
+
+  /*
+   * This S-function can be used in referenced model simulating in normal mode.
+   */
+  ssSetModelReferenceNormalModeSupport(S, MDL_START_AND_MDL_PROCESS_PARAMS_OK);
+
+  /*
+   * Set the number of sample time.
+   */
+  ssSetNumSampleTimes(S, 1);
+
+  /*
+   * All options have the form SS_OPTION_<name> and are documented in
+   * matlabroot/simulink/include/simstruc.h. The options should be
+   * combined with bitwise OR as in
+   *   ssSetOptions(S, (SS_OPTION_name1 | SS_OPTION_name2))
+   */
+  ssSetOptions(S,
+               SS_OPTION_USE_TLC_WITH_ACCELERATOR |
+               SS_OPTION_CAN_BE_CALLED_CONDITIONALLY |
+               SS_OPTION_EXCEPTION_FREE_CODE |
+               SS_OPTION_WORKS_WITH_CODE_REUSE |
+               SS_OPTION_SFUNCTION_INLINED_FOR_RTW |
+               SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
+}
+
+/* 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)
+{
+  double * const sampleTime = mxGetPr(SAMPLE_TIME);
+  const  size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
+  ssSetSampleTime(S, 0, sampleTime[0]);
+  if (stArraySize == 1) {
+    ssSetOffsetTime(S, 0, (sampleTime[0] == CONTINUOUS_SAMPLE_TIME?
+      FIXED_IN_MINOR_STEP_OFFSET: 0.0));
+  } else {
+    ssSetOffsetTime(S, 0, sampleTime[1]);
+  }
+
+#if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
+
+  ssSetModelReferenceSampleTimeDefaultInheritance(S);
+
+#endif
+
+}
+
+#define MDL_SET_WORK_WIDTHS
+#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
+
+/* Function: mdlSetWorkWidths =============================================
+ * Abstract:
+ *      The optional method, mdlSetWorkWidths is called after input port
+ *      width, output port width, and sample times of the S-function have
+ *      been determined to set any state and work vector sizes which are
+ *      a function of the input, output, and/or sample times.
+ *
+ *      Run-time parameters are registered in this method using methods
+ *      ssSetNumRunTimeParams, ssSetRunTimeParamInfo, and related methods.
+ */
+static void mdlSetWorkWidths(SimStruct *S)
+{
+  /* Set number of run-time parameters */
+  if (!ssSetNumRunTimeParams(S, 1))
+    return;
+
+  /*
+   * Register the run-time parameter 1
+   */
+  ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", ssGetDataTypeId(S, "uint8"));
+}
+
+#endif
+
+#define MDL_START
+#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)
+{
+    UNUSED_PARAMETER(S);
+}
+
+#endif
+
+/* Function: mdlOutputs ===================================================
+ * Abstract:
+ *    In this function, you compute the outputs of your S-function
+ *    block. Generally outputs are placed in the output vector(s),
+ *    ssGetOutputPortSignal.
+ */
+static void mdlOutputs(SimStruct *S, int_T tid)
+{
+    UNUSED_PARAMETER(S);
+    UNUSED_PARAMETER(tid);
+}
+
+/* Function: mdlTerminate =================================================
+ * Abstract:
+ *    In this function, you should perform any actions that are necessary
+ *    at the termination of a simulation.
+ */
+static void mdlTerminate(SimStruct *S)
+{
+    UNUSED_PARAMETER(S);
+}
+
+#define MDL_RTW
+#if defined(MATLAB_MEX_FILE) && defined(MDL_RTW)
+
+/* Function: mdlRTW =======================================================
+ * Abstract:
+ *    This function is called when the Real-Time Workshop is generating
+ *    the model.rtw file.
+ */
+static void mdlRTW(SimStruct *S)
+{
+    UNUSED_PARAMETER(S);
+}
+
+#endif
+
+/* Function: IsRealMatrix =================================================
+ * Abstract:
+ *      Verify that the mxArray is a real (double) finite matrix
+ */
+static bool IsRealMatrix(const mxArray * const m)
+{
+  if (mxIsNumeric(m) &&
+      mxIsDouble(m) &&
+      !mxIsLogical(m) &&
+      !mxIsComplex(m) &&
+      !mxIsSparse(m) &&
+      !mxIsEmpty(m) &&
+      mxGetNumberOfDimensions(m) == 2) {
+    const double * const data = mxGetPr(m);
+    const size_t numEl = mxGetNumberOfElements(m);
+    size_t i;
+    for (i = 0; i < numEl; i++) {
+      if (!mxIsFinite(data[i])) {
+        return(false);
+      }
+    }
+
+    return(true);
+  } else {
+    return(false);
+  }
+}
+
+/*
+ * Required S-function trailer
+ */
+#ifdef MATLAB_MEX_FILE
+# include "simulink.c"
+#else
+# include "cg_sfun.h"
+#endif
index 9c434ef10e5526bf60fe9df7c1c522ec50314ae7..e29bc37cbcfb2711d5d9646285b83895fb5de2c6 100644 (file)
@@ -16,9 +16,9 @@
 %% You should have received a copy of the GNU General Public License
 %% along with this program.  If not, see <http://www.gnu.org/licenses/>.
 %%
-%% File : sfunction_gpio_write.tlc
+%% File : sfunction_din.tlc
 %% Abstract:
-%%     TLC file for inlining RPP general purpose I/O block.
+%%     TLC file for inlining RPP digital input block.
 %%
 %% References:
 %%     BlockTypeSetup() : rtw_tlc.pdf p. 277
 %%     Outputs()        : rtw_tlc.pdf p. 281
 
 
-%%
-%% FIXME Implement real functions.
-%%
-
-%implements sfunction_gpio_write "C"
+%implements sfunction_din "C"
 
 %include "common.tlc"
 
 
-%% Function: BlockTypeSetup ===============================================
+%% Function: BlockTypeSetup ====================================================
 %function BlockTypeSetup(block, system) void
 
     %% Ensure required header files are included
@@ -44,7 +40,7 @@
 %endfunction
 
 
-%% Function: Start ========================================================
+%% Function: Start =============================================================
 %function Start(block, system) Output
 
     %if !SLibCodeGenForSim()
 %endfunction
 
 
-%% Function: Outputs ======================================================
+%% Function: Outputs ===========================================================
 %function Outputs(block, system) Output
 
     %if !SLibCodeGenForSim()
         %assign p1_val = LibBlockParameter(p1, "", "", 0)
         %assign u1_val = LibBlockInputSignal(0, "", "", 0)
 
-        //digitalWrite(%<p1_val>, %<u1_val>);
+        //rpp_din_get(%<p1_val>, %<u1_val>);
     %endif
 
 %endfunction
diff --git a/rpp/blocks/tlc_c/sfunction_lout.tlc b/rpp/blocks/tlc_c/sfunction_lout.tlc
new file mode 100644 (file)
index 0000000..75f41d2
--- /dev/null
@@ -0,0 +1,65 @@
+%% Copyright (C) 2013 Czech Technical University in Prague
+%%
+%% Authors:
+%%     - Carlos Jenkins <carlos@jenkins.co.cr>
+%%
+%% This program is free software; you can redistribute it and/or modify
+%% it under the terms of the GNU General Public License as published by
+%% the Free Software Foundation; either version 2 of the License, or
+%% (at your option) any later version.
+%%
+%% This program is distributed in the hope that it will be useful,
+%% but WITHOUT ANY WARRANTY; without even the implied warranty of
+%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%% GNU General Public License for more details.
+%%
+%% You should have received a copy of the GNU General Public License
+%% along with this program.  If not, see <http://www.gnu.org/licenses/>.
+%%
+%% File : sfunction_lout.tlc
+%% Abstract:
+%%     TLC file for inlining RPP digital output block.
+%%
+%% References:
+%%     BlockTypeSetup() : rtw_tlc.pdf p. 277
+%%     Start()          : rtw_tlc.pdf p. 279
+%%     Outputs()        : rtw_tlc.pdf p. 281
+
+
+%implements sfunction_lout "C"
+
+%include "common.tlc"
+
+
+%% Function: BlockTypeSetup ====================================================
+%function BlockTypeSetup(block, system) void
+
+    %% Ensure required header files are included
+    %<RppCommonBlockTypeSetup(block, system)>
+
+%endfunction
+
+
+%% Function: Start =============================================================
+%function Start(block, system) Output
+
+    %if !SLibCodeGenForSim()
+        %<RppCommonStart(block, system)>
+    %endif
+
+%endfunction
+
+
+%% Function: Outputs ===========================================================
+%function Outputs(block, system) Output
+
+    %if !SLibCodeGenForSim()
+        %assign p1_val = LibBlockParameter(p1, "", "", 0)
+        %assign u1_val = LibBlockInputSignal(0, "", "", 0)
+
+        //rpp_lout_set(%<p1_val>, %<u1_val>);
+    %endif
+
+%endfunction
+
+%% [EOF]