]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blobdiff - rpp/blocks/tlc_c/sfunction_din.tlc
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / tlc_c / sfunction_din.tlc
index 04dd10a4fd2aeeb10f6deb7b90012ea3f14e29ec..907393204dc4d6a2b8c97d4561a21840d3a6f4eb 100644 (file)
@@ -1,13 +1,28 @@
-%% Copyright (C) 2013 Czech Technical University in Prague
+%% Copyright (C) 2013, 2014 Czech Technical University in Prague
 %%
 %% Authors:
 %%     - Carlos Jenkins <carlos@jenkins.co.cr>
-%%     - Michal Horn <hornmich@fel.cvut.cz>
 %%
-%% 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.
+
+%% 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.tlc
 %% Abstract:
 %include "common.tlc"
 
 
+%% Function: BlockInstanceSetup ================================================
+%function BlockInstanceSetup(block, system) void
+    %assign ::rpp_din_present=1
+    %assign pin_num = LibBlockParameterValue(p1, 0)
+    
+    %switch (%<pin_num>)
+        %case 10
+        %case 11
+            %if EXISTS("rpp_irc_1_used") == 1
+                %<LibBlockReportError(block, "Either IRC1 or Digital Input pin 10 and 11 blocks are allowed in one model, not both.")>
+            %else
+                %assign ::rpp_din_10_11_used = 1
+            %endif
+            %break
+        %case 14
+        %case 15
+            %if EXISTS("rpp_irc_2_used") == 1
+                %<LibBlockReportError(block, "Either IRC2 or Digital Input pin 14 and 15 blocks are allowed in one model, not both.")>
+            %else
+                %assign ::rpp_din_14_15_used = 1
+            %endif
+            %break
+    %endswitch
+
+    %if %<pin_num> < 8
+        %% Create array if not exist
+        %if EXISTS("rpp_din_in_model_array") == 0
+            %assign ::rpp_din_in_model_array = [0, 0, 0, 0, 0, 0, 0, 0]
+        %endif
+
+        %if ::rpp_din_in_model_array[%<pin_num>] == 0
+            %assign ::rpp_din_in_model_array[%<pin_num>] = 1
+        %else
+            %assign err_msg = "Only one Digital Input block of pin %<pin_num> is allowed in the model."
+            %<LibBlockReportError(block, err_msg)>
+        %endif
+    %endif
+
+%endfunction
+
 %% Function: BlockTypeSetup ====================================================
 %function BlockTypeSetup(block, system) void
 
-       %% Ensure required header files are included
-       %<RppCommonBlockTypeSetup(block, system)>
-       %<LibAddToCommonIncludes("rpp/gio.h")>
-       %assign ::rpp_din_present = 1
+    %% Ensure required header files are included
+    %<RppCommonBlockTypeSetup(block, system)>
+    %<LibAddToCommonIncludes("rpp/rpp.h")>
 
 %endfunction
 
-%function BlockInstanceSetup(block, system) void
-       %assign port_par = LibBlockParameterValue(port_type, 0)
-       %assign pin_number_par = LibBlockParameterValue(pin_number, 0)
-
-       %% Ensure that every pin is configured only once
-       %if EXISTS("::rpp_%<port_par>_%<pin_number_par>_present") == 0
-               %assign ::rpp_%<port_par>_%<pin_number_par>_present = 1
-       %else
-               %<LibBlockReportError(block, "GPIO pin %<pin_number_par> on port %<port_par> has already been configred.")>
-       %endif
-%endfunction
 
 %% Function: Start =============================================================
 %function Start(block, system) Output
-       %assign port_par = LibBlockParameterValue(port_type, 0)
-       %assign pin_number_par = LibBlockParameterValue(pin_number, 0)
-       %assign config = LibBlockParameterValue(input_type, 0)
-
-       %if !SLibCodeGenForSim()
-               %openfile buffer
-               uint32_t* din_%<port_par>_%<pin_number_par>_desc = NULL;
-               %closefile buffer
-               %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
-
-               uint32_t din_%<port_par>_%<pin_number_par>_cfg = PORT_CONF_FNC_GPIO|PORT_CONF_DIR_IN;
-               %if port_par == 1       %% GIOA
-                       %assign name = "GIOA%<pin_number_par>"
-               %elseif port_par == 2   %% GIOB
-                       %assign name = "GIOB%<pin_number_par>"
-               %elseif port_par == 3   %% NHET1
-                       %assign name = "NHET1%<pin_number_par>"
-               %else
-                       %<LibBlockReportError(block, "Bad port identifier: %<port_par>")>
-               %endif
-
-               din_%<port_par>_%<pin_number_par>_desc = hal_gpio_pin_get_dsc("%<name>", -1);
-
-               %if config == 1         %% Tri-state
-                       din_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_MODE_PDIS|PORT_CONF_OD_ON;
-               %elseif config == 2     %% Pull up
-                       din_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_MODE_PEN|PORT_CONF_OD_OFF|PORT_CONF_MODE_PU;
-               %elseif config == 3     %% Pull down
-                       din_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_MODE_PEN|PORT_CONF_OD_OFF|PORT_CONF_MODE_PD;
-               %else   %% error
-                       %<LibBlockReportError(block, "Bad configuration value: %<config>")>
-               %endif
-
-               hal_gpio_pin_conf_set(*din_%<port_par>_%<pin_number_par>_desc, din_%<port_par>_%<pin_number_par>_cfg);
-               
-       %endif
+
+    %if !SLibCodeGenForSim()
+        %assign pin_num = LibBlockParameterValue(p1, 0)
+        %if pin_num < 8
+            %assign pull_up = LibBlockParameterValue(p3, 0) - 1
+            %assign active = LibBlockParameterValue(p4, 0) - 1
+            rpp_din_setup(%<pin_num>, %<pull_up>, %<active>, FALSE);
+        %endif
+    %endif
 
 %endfunction
 
 %% Function: Outputs ===========================================================
 %function Outputs(block, system) Output
 
-       %if !SLibCodeGenForSim()
-               %assign digital_in = LibBlockOutputSignal(0, "", "", 0)
-               %assign port_par = LibBlockParameterValue(port_type, 0)
-               %assign pin_number_par = LibBlockParameterValue(pin_number, 0)
-               uint32_t din_%<port_par>_%<pin_number_par>_val = hal_gpio_pin_get_value(*din_%<port_par>_%<pin_number_par>_desc);
-               %<digital_in> = din_%<port_par>_%<pin_number_par>_val;
+    %if !SLibCodeGenForSim()
+
+        %% Declare temporal variables
+        %if EXISTS("_RPP_DIN_TMP_VARS_") == 0
+            %assign ::_RPP_DIN_TMP_VARS_ = 1
+            int8_t din_tmp;
+        %endif
+
+        %% Error flag
+        %assign err_flag = LibBlockOutputSignal(1, "", "", 0)
+
+        %% First executed block must update cached values
+        %if EXISTS("_RPP_DIN_UPDATE_") == 0
+            %assign ::_RPP_DIN_UPDATE_ = 1
+            if (rpp_din_update() != SUCCESS) {
+                %<err_flag> = TRUE;
+            }
+        %endif
+
+        %% Get pin number and if to use variable threshold
+        %assign pin_num = LibBlockParameterValue(p1, 0)
+        %assign var_thr = LibBlockParameterValue(p2, 0)
+
+        %% Get pin value
+       %if var_thr == 0
+         din_tmp = rpp_din_get(%<pin_num>);
+       %else
+         din_tmp = rpp_din_get_tr(%<pin_num>);
        %endif
+        if (din_tmp < 0) {
+            %<err_flag> = TRUE;
+            din_tmp = LOW;
+        }
+
+        %% Return input
+        %assign digital_in = LibBlockOutputSignal(0, "", "", 0)
+        %<digital_in> = din_tmp;
+
+    %endif
 
 %endfunction