]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfIRCInput.c
Use correct header file for alloca for Linux build.
[mf624-simulink.git] / sfIRCInput.c
index 1cd43673d1406df48135e7ed1814ff8155f3d723..86ae04d4fe8fbcb8e5d46a801703c4257bac123f 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
@@ -48,6 +75,8 @@
  * its associated macro definitions.
  */
 #include "simstruc.h"
+
+#ifndef WITHOUT_HW
 #include "mf624_SIMULINK.h"
 
 static const int32_T irc_channel2reg[] = {
@@ -56,6 +85,7 @@ static const int32_T irc_channel2reg[] = {
     IRC2_reg,
     IRC3_reg
 };
+#endif /*WITHOUT_HW*/
 
 /* Error handling
  * --------------
@@ -100,6 +130,7 @@ static void mdlCheckParameters(SimStruct *S)
         ssSetErrorStatus(S, "Ts has to be positive or -1 for automatic step");
     if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > 3))
         ssSetErrorStatus(S, "valid IRC channel is 0, 1, 2, or 3");
+  #ifndef WITHOUT_HW
     if ((PRM_COUNTER_MODE(S) < 0) || PRM_COUNTER_MODE(S) >
             __mfld2val(IRCCTRL_IRC0MODE_mask, IRCCTRL_IRC0MODE_mask))
         ssSetErrorStatus(S, "Counter Mode out of valid range");
@@ -114,6 +145,9 @@ 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");
+  #endif /*WITHOUT_HW*/
+    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 +247,29 @@ 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;
+
+  #ifndef WITHOUT_HW
+    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 /*WITHOUT_HW*/
+}
 #endif /* MDL_INITIALIZE_CONDITIONS */
 
 
@@ -235,6 +289,7 @@ static void mdlStart(SimStruct *S)
     int_T filter;
     int_T mask;
 
+  #ifndef WITHOUT_HW
     if (mf624_init(NULL) != 0)
         return;
 
@@ -261,6 +316,10 @@ static void mdlStart(SimStruct *S)
     shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
 
     IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
+
+  #endif /*WITHOUT_HW*/
+
+    mdlInitializeConditions(S);
 }
 #endif /*  MDL_START */
 
@@ -275,10 +334,14 @@ static void mdlOutputs(SimStruct *S, int_T tid)
 {
     int32_T *y = ssGetOutputPortSignal(S,0);
 
+  #ifndef WITHOUT_HW
     if (mf624_check(S) != 0)
         return;
 
     y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
+  #else /*WITHOUT_HW*/
+    y[0] = 0;
+  #endif /*WITHOUT_HW*/
 }
 
 
@@ -298,6 +361,7 @@ static void mdlUpdate(SimStruct *S, int_T tid)
     int_T shift;
     int_T inp_num = 0;
 
+  #ifndef WITHOUT_HW
     if (mf624_check(S) != 0)
         return;
 
@@ -327,6 +391,7 @@ static void mdlUpdate(SimStruct *S, int_T tid)
         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
         IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
     }
+  #endif /*WITHOUT_HW*/
 }
 #endif /* MDL_UPDATE */
 
@@ -354,7 +419,9 @@ static void mdlUpdate(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
+  #ifndef WITHOUT_HW
     mf624_done();
+  #endif /*WITHOUT_HW*/
 }