]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blobdiff - rpp/blocks/sfunction_din.c
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_din.c
index b3dfe19159f1ee52c2835797078b2a354d727720..9889ca319fddba281cd6eb403c855442e4596e62 100644 (file)
@@ -1,18 +1,32 @@
-/* TODO: Přejmenovat na sfunction_gio_in.c */
-
-/* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
+/* Copyright (C) 2013, 2014 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>
  *
- * This document contains proprietary information belonging to Czech
- * Technical University in Prague. Passing on and copying of this
- * document, and communication of its contents is not permitted
- * without prior written authorization.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
- * File : sfunction_ain.c
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * File : sfunction_din.c
  * Abstract:
- *     C-MEX S-function block for reading from RPP GPIO ports.
+ *     C-MEX S-function block for RPP digital input.
  *
  * References:
  *     header.c
 ---
 Name: Digital Input
 Category: IO blocks
-Header: rpp/gio.h
+Header: rpp/din.h
 Mnemonic: DIN
 
 Inputs:
 
 Outputs:
-  - { name: "Digital Input value", type: "bool" }
+  - { name: "Digital Input", type: "bool" }
+  - { name: "ErrFlag",       type: "bool" }
+
 Parameters:
-  - { name: "Port type", type: "Choice", range: "GIOA, GIOB, NHET1" }
-  - { name: "Pin number", type: "int8",   range: "[0–7]", note: "(depends on selected port)" }
-  - { name: "Input Type", type: "Choice", range: "Tri-state, Pull Up, Pull Down" }
+  - { name: "Pin number",             type: "uint8", range: "[0-15]"           }
+  - { name: "Use variable threshold", type: "bool"                             }
+  - { name: "Pull",                   type: "Choice from Pull-down or Pull-up" }
+  - { name: "Active",                 type: "Choice from Active or Tri-stated" }
+
+# Description and Help is in Markdown mark-up
+Description: |
+
+  Gets the digital value of the specified digital input pin on the RPP board.
+
+  If pin is high the output is 1, 0 if the pin is low.
 
-# Description is in Markdown mark-up
-Description: &desc |
-  Reads a value from a GPIO pin. The block supports GIOA, GIOB and NHET1 ports.
-  Any pin can be configured as tri-state, pull up or pull down.
+  If an error is detected while getting the pin, the ErrFlag is set high.
 
-  It is not allowed to read from one pin by using more then one DIN blocks.
-  It is not allowed to use DIN and DOUT blocks together configured for one GPIO pin.
+Help: |
 
-Help: *desc
+  This block allows to read the digital inputs on the RPP board. For pins number 0-7 is not
+  relevant and not visible parameter 'Use variable threshold' and only one block for each pin
+  can be in model. And oppositely for other pins (8-15) are not relevant and visible parameters
+  of names 'Pull' and 'Active'. The variable threshold check change the read mode of the pin. For
+  setting it see Digital Input Configure block.
+
+  The ErrFlag should raise if `rpp_din_update()` or `rpp_din_get()` returns error. `rpp_din_update()`
+  is called just by the first DIN block in the model and thus only the first block could raise the
+  flag because of this. In case an errors occurs the return value will always be LOW (0). Because the
+  ErrFlag should never set, once set the following steps will never clear it back.
 
 Status: Stable
 
 RPP API functions used:
-  - hal_gpio_pin_get_dsc
-  - hal_gpio_pin_get_value
-  - hal_gpio_pin_conf_set
+    - rpp_din_setup()
+    - rpp_din_update()
+    - rpp_din_get()
 
 Relevant demos:
-  - gio_demo
+    - digital_passthrough
+    - hbridge_digital_control
 ...
 */
 
-
 #define S_FUNCTION_NAME sfunction_din
 #include "header.c"
 
-#define PARAM_NAME_PORT_TYPE           "port_type"
-#define PARAM_NAME_PIN_NUMBER          "pin_number"
-#define PARAM_NAME_INPUT_TYPE          "input_type"
-
-/** Identifiers of the block parameters */
-enum params{
-       PARAM_PORT_TYPE,
-       PARAM_PIN_NUMBER,
-       PARAM_INPUT_TYPE,
-       PARAMS_COUNT
-};
-
-enum port_types{
-       PORT_UNKNOWN,
-       PORT_GIOA,
-       PORT_GIOB,
-       PORT_NHET1
-};
-
-enum outputs{
-       OUT_PIN_VALUE,
-       OUTPUTS_COUNT
-};
 
 static void mdlInitializeSizes(SimStruct *S)
 {
-       /*
-       * Configure parameters: 3
-       *  - Port type
-       *  - Pin number
-       *  - Input type
-       */
-       if (!rppSetNumParams(S, PARAMS_COUNT)) {
-               return;
-       }
-
-       /*
-       * Configure input ports: 0
-       */
-       if (!ssSetNumInputPorts(S, 0)) {
-               return;
-       }
-
-       /*
-       * Configure output ports: 1
-       */
-       if (!ssSetNumOutputPorts(S, OUTPUTS_COUNT)) {
-               return;
-       }
-       rppAddOutputPort(S, OUT_PIN_VALUE, SS_BOOLEAN);
-
-       /* Set standard options for this block */
-       rppSetStandardOptions(S);
+    /*
+     * Configure parameters: 1
+     *  - Pin number: [1-16]
+     *  - Use variable threshold: [true|false]
+     *  - Pull: [Pull-up|Pull-down]
+     *  - Active: [Active|Tristate]
+     */
+    if (!rppSetNumParams(S, 4)) {
+        return;
+    }
+
+    /*
+     * Configure input ports: 0
+     */
+    if (!ssSetNumInputPorts(S, 0)) {
+        return;
+    }
+
+    /*
+     * Configure output ports: 2
+     *  - Digital input.
+     *  - Error flag.
+     */
+    if (!ssSetNumOutputPorts(S, 2)) {
+        return;
+    }
+    rppAddOutputPort(S, 0, SS_BOOLEAN);
+    rppAddOutputPort(S, 1, SS_BOOLEAN);
+
+    /* Set standard options for this block */
+    rppSetStandardOptions(S);
 }
 
 
@@ -125,24 +135,25 @@ static void mdlInitializeSizes(SimStruct *S)
 #define MDL_CHECK_PARAMETERS
 static void mdlCheckParameters(SimStruct *S)
 {
-       if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_GIOA) {
-               if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 7)) {
-                       return;
-               }
-       }
-       else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_GIOB) {
-               if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 7)) {
-                       return;
-               }
-       }
-       else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_NHET1) {
-               if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 31)) {
-                       return;
-               }
-       }
-       else {
-               return;
-       }
+    /* Check the parameter 1 */
+    if (!rppValidParamRange(S, 0, 0, 15)) {
+        return;
+    }
+
+    /* Check the parameter 2 */
+    if (!rppValidParamBoolean(S, 1)) {
+        return;
+    }
+
+    /* Check the parameter 3 */
+    if (!rppValidParamRange(S, 2, 0, 2)) {
+        return;
+    }
+
+    /* Check the parameter 4 */
+    if (!rppValidParamRange(S, 3, 0, 2)) {
+        return;
+    }
 }
 #endif
 
@@ -151,14 +162,22 @@ static void mdlCheckParameters(SimStruct *S)
 #define MDL_SET_WORK_WIDTHS
 static void mdlSetWorkWidths(SimStruct *S)
 {
-       /* Set number of run-time parameters */
-       if (!ssSetNumRunTimeParams(S, PARAMS_COUNT)) {
-               return;
-       }
-       /* Register the run-time parameter 1 */
-       ssRegDlgParamAsRunTimeParam(S, PARAM_PORT_TYPE,      PARAM_PORT_TYPE,      PARAM_NAME_PORT_TYPE,      SS_INT8);
-       ssRegDlgParamAsRunTimeParam(S, PARAM_PIN_NUMBER,     PARAM_PIN_NUMBER,     PARAM_NAME_PIN_NUMBER,     SS_INT8);
-       ssRegDlgParamAsRunTimeParam(S, PARAM_INPUT_TYPE,     PARAM_INPUT_TYPE,     PARAM_NAME_INPUT_TYPE,     SS_INT8);
+    /* Set number of run-time parameters */
+    if (!ssSetNumRunTimeParams(S, 4)) {
+        return;
+    }
+
+    /* Register the run-time parameter 1 */
+    ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_INT8);
+
+    /* Register the run-time parameter 2 */
+    ssRegDlgParamAsRunTimeParam(S, 1, 1, "p2", SS_BOOLEAN);
+
+    /* Register the run-time parameter 3 */
+    ssRegDlgParamAsRunTimeParam(S, 2, 2, "p3", SS_UINT8);
+
+    /* Register the run-time parameter 4 */
+    ssRegDlgParamAsRunTimeParam(S, 3, 3, "p4", SS_UINT8);
 }
 #endif