]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfIRCInput.c
Example of use of PWM combined with digital direction output.
[mf624-simulink.git] / sfIRCInput.c
index 1cd43673d1406df48135e7ed1814ff8155f3d723..b9d4655dcaca0f57fd50b6f13c9d52df552155dd 100644 (file)
@@ -1,7 +1,33 @@
 /*
  * S-function to support IRC inputs on Humusoft MF624 card
  *
- * Based on sfuntmpl_basic.c by The MathWorks, Inc.
+ * Copyright (C) 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.
  */
 
 
@@ -24,8 +50,9 @@
 #define PRM_COUNTER_GATING(S)   (mxGetScalar(ssGetSFcnParam(S, 3)))
 #define PRM_RESET_CONTROL(S)    (mxGetScalar(ssGetSFcnParam(S, 4)))
 #define PRM_DIGITAL_FILTER(S)   (mxGetScalar(ssGetSFcnParam(S, 5)))
+#define PRM_RESET_AT_STARTUP(S) (mxGetScalar(ssGetSFcnParam(S, 6)))
 
-#define PRM_COUNT                   6
+#define PRM_COUNT                   7
 
 #define IWORK_IDX_CHANNEL           0
 #define IWORK_IDX_IRCCTRL           1
@@ -114,6 +141,8 @@ static void mdlCheckParameters(SimStruct *S)
     if ((PRM_DIGITAL_FILTER(S) < 0) || PRM_DIGITAL_FILTER(S) >
             __mfld2val(IRCCTRL_IRC0FILTER_mask, IRCCTRL_IRC0FILTER_mask))
         ssSetErrorStatus(S, "Digital Filter out of valid range");
+    if ((PRM_RESET_AT_STARTUP(S) != 0) && (PRM_RESET_AT_STARTUP(S) != 1))
+        ssSetErrorStatus(S, "Reset at startup can be only 0 or 1");
 }
 #endif /* MDL_CHECK_PARAMETERS */
 
@@ -213,9 +242,27 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    *    configured to reset states, it will be call when the enabled subsystem
    *    restarts execution to reset the states.
    */
-  static void mdlInitializeConditions(SimStruct *S)
-  {
-  }
+static void mdlInitializeConditions(SimStruct *S)
+{
+    int_T shift;
+    int_T mask;
+    int_T reset_mode;
+    int_T reset_mode_save;
+
+    if (mf624_check(S) != 0)
+        return;
+
+    if (PRM_RESET_AT_STARTUP(S)) {
+        shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
+
+        mask = IRCCTRL_IRC0RESET_mask << shift;
+        reset_mode = __val2mfld(IRCCTRL_IRC0RESET_mask, IRCCTRL_RESET_ALWAYS) << shift;
+
+        reset_mode_save = IRC_mode_change(mfst, 0, 0);
+        IRC_mode_change(mfst, mask, reset_mode);
+        IRC_mode_change(mfst, mask, reset_mode_save & mask);
+    }
+}
 #endif /* MDL_INITIALIZE_CONDITIONS */
 
 
@@ -261,6 +308,8 @@ static void mdlStart(SimStruct *S)
     shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
 
     IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
+
+    mdlInitializeConditions(S);
 }
 #endif /*  MDL_START */