]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/commitdiff
Implement simulink blocks and demo for GPIO
authorMichal Horn <hornmich@fel.cvut.cz>
Wed, 5 Nov 2014 16:33:36 +0000 (17:33 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 3 Dec 2014 16:49:03 +0000 (17:49 +0100)
Refs: #996, #997

rpp/blocks/rpp_lib.slx
rpp/blocks/sfunction_din.c [new file with mode: 0644]
rpp/blocks/sfunction_dout.c [new file with mode: 0644]
rpp/blocks/tlc_c/sfunction_din.tlc [new file with mode: 0644]
rpp/blocks/tlc_c/sfunction_dout.tlc [new file with mode: 0644]
rpp/demos/gio_demo.slx [new file with mode: 0644]
rpp/rpp/rpp_srmain.tlc

index f900a9fe584fbb0401c3606b11483a8c78b53294..1df78f61188d5a18b32fa1146ae3fcdede75e984 100644 (file)
Binary files a/rpp/blocks/rpp_lib.slx and b/rpp/blocks/rpp_lib.slx differ
diff --git a/rpp/blocks/sfunction_din.c b/rpp/blocks/sfunction_din.c
new file mode 100644 (file)
index 0000000..2b71e25
--- /dev/null
@@ -0,0 +1,165 @@
+/* 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.
+ *
+ * File : sfunction_ain.c
+ * Abstract:
+ *     C-MEX S-function block for reading from RPP GPIO ports.
+ *
+ * References:
+ *     header.c
+ *     trailer.c
+ *
+ * Compile with:
+ *     <matlabroot>/bin/mex sfunction_din.c
+ */
+
+/*
+%YAML 1.2
+---
+Name: Digital input
+Category: IO blocks
+Header: rpp/gio.h
+Mnemonic: DIN
+
+Inputs:
+
+Outputs:
+       - { name: "Digital Input value", type: "bool" }
+Parameters:
+       - { name: "Port type", type: "Choice", range: "GIOA, GIOB, NHET1" }
+       - { name: "Pin number", type: "int8", range: "Depends on selected port" }
+       - { name: "Input Type", type: "Choice", range: "Tri-state, Pull Up, Pull Down" }
+
+# Description is in Markdown mark-up
+Description:
+       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.
+
+Status:
+       Tested:
+       Untested:
+       Not working:
+
+RPP API functions used:
+       - hal_gpio_pin_get_dsc
+       - hal_gpio_pin_get_value
+       - hal_gpio_pin_conf_set
+
+Relevant demos:
+       - gio_demo
+...
+*/
+
+
+#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);
+}
+
+
+#ifdef MATLAB_MEX_FILE
+#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;
+       }
+}
+#endif
+
+
+#ifdef MATLAB_MEX_FILE
+#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);
+}
+#endif
+
+
+#define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
+#define UNUSED_MDLOUTPUTS
+#define UNUSED_MDLTERMINATE
+#include "trailer.c"
diff --git a/rpp/blocks/sfunction_dout.c b/rpp/blocks/sfunction_dout.c
new file mode 100644 (file)
index 0000000..b2f90e0
--- /dev/null
@@ -0,0 +1,177 @@
+/* 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.
+ *
+ * File : sfunction_ain.c
+ * Abstract:
+ *     C-MEX S-function block for writting to RPP GPIO ports.
+ *
+ * References:
+ *     header.c
+ *     trailer.c
+ *
+ * Compile with:
+ *     <matlabroot>/bin/mex sfunction_dout.c
+ */
+
+/*
+%YAML 1.2
+---
+Name: Digital output
+Category: IO blocks
+Header: rpp/gio.h
+Mnemonic: DOUT
+
+Inputs:
+       - { name: "Digital Output value", type: "bool" }
+Outputs:
+
+Parameters:
+       - { name: "Port type", type: "Choice", range: "GIOA, GIOB, NHET1" }
+       - { name: "Pin number", type: "int8", range: "Depends on selected port" }
+       - { name: "Initial output value", type: "bool" }
+       - { name: "Output Type", type: "Choice", range: "Open Drain, Push/Pull" }
+
+# Description is in Markdown mark-up
+Description:
+       Writes a value to a GPIO pin. The block supports GIOA, GIOB and NHET1 ports.
+       Any pin can be configured as open drain or push/pull and initial output value
+       can be specified. The initial value is a value, which will appear on the pin
+       after gpio module is initialized and persists here until the first write.
+
+Status:
+       Tested:
+       Untested:
+       Not working:
+
+RPP API functions used:
+       - hal_gpio_pin_get_dsc
+       - hal_gpio_pin_set_value
+       - hal_gpio_pin_conf_set
+
+Relevant demos:
+       - gio_demo
+...
+*/
+
+
+#define S_FUNCTION_NAME sfunction_dout
+#include "header.c"
+
+#define PARAM_NAME_PORT_TYPE           "port_type"
+#define PARAM_NAME_PIN_NUMBER          "pin_number"
+#define PARAM_NAME_DEFAULT_OUTPUT      "default_output"
+#define PARAM_NAME_OUTPUT_TYPE         "output_type"
+
+/** Identifiers of the block parameters */
+enum params{
+       PARAM_PORT_TYPE,
+       PARAM_PIN_NUMBER,
+       PARAM_DEFAULT_OUTPUT,
+       PARAM_OUTPUT_TYPE,
+       PARAMS_COUNT
+};
+
+enum port_types{
+       PORT_UNKNOWN,
+       PORT_GIOA,
+       PORT_GIOB,
+       PORT_NHET1
+};
+
+enum inputs{
+       IN_PIN_VALUE,
+       INPUTS_COUNT
+};
+
+static void mdlInitializeSizes(SimStruct *S)
+{
+       /*
+       * Configure parameters: 4
+       *  - Port type
+       *  - Pin number
+       *  - Default output value
+       *  - Output type
+       */
+       if (!rppSetNumParams(S, PARAMS_COUNT)) {
+               return;
+       }
+
+       /*
+       * Configure output ports: 0
+       */
+       if (!ssSetNumOutputPorts(S, 0)) {
+               return;
+       }
+
+       /*
+       * Configure input ports: 1
+       */
+       if (!ssSetNumInputPorts(S, INPUTS_COUNT)) {
+               return;
+       }
+
+       rppAddInputPort(S, IN_PIN_VALUE, SS_BOOLEAN);
+
+       /* Set standard options for this block */
+       rppSetStandardOptions(S);
+}
+
+
+#ifdef MATLAB_MEX_FILE
+#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;
+       }
+
+       if (!rppValidParamRange(S, PARAM_DEFAULT_OUTPUT, 0, 1)) {
+               return;
+       }
+}
+#endif
+
+
+#ifdef MATLAB_MEX_FILE
+#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_DEFAULT_OUTPUT, PARAM_DEFAULT_OUTPUT, PARAM_NAME_DEFAULT_OUTPUT, SS_BOOLEAN);
+       ssRegDlgParamAsRunTimeParam(S, PARAM_OUTPUT_TYPE,    PARAM_OUTPUT_TYPE,    PARAM_NAME_OUTPUT_TYPE,    SS_INT8);
+}
+#endif
+
+
+#define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
+#define UNUSED_MDLOUTPUTS
+#define UNUSED_MDLTERMINATE
+#include "trailer.c"
diff --git a/rpp/blocks/tlc_c/sfunction_din.tlc b/rpp/blocks/tlc_c/sfunction_din.tlc
new file mode 100644 (file)
index 0000000..04dd10a
--- /dev/null
@@ -0,0 +1,104 @@
+%% Copyright (C) 2013 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.
+%%
+%% File : sfunction_din.tlc
+%% Abstract:
+%%     TLC file for inlining RPP digital input block.
+%%
+%% References:
+%%     BlockTypeSetup() : rtw_tlc.pdf p. 277
+%%     Start()          : rtw_tlc.pdf p. 279
+%%     Outputs()        : rtw_tlc.pdf p. 281
+
+
+%implements sfunction_din "C"
+
+%include "common.tlc"
+
+
+%% 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
+
+%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
+
+%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;
+       %endif
+
+%endfunction
+
+%% [EOF]
diff --git a/rpp/blocks/tlc_c/sfunction_dout.tlc b/rpp/blocks/tlc_c/sfunction_dout.tlc
new file mode 100644 (file)
index 0000000..27b95b8
--- /dev/null
@@ -0,0 +1,112 @@
+%% Copyright (C) 2013 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.
+%%
+%% File : sfunction_dout.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_dout "C"
+
+%include "common.tlc"
+
+
+%% Function: BlockTypeSetup ====================================================
+%function BlockTypeSetup(block, system) void
+
+       %% Ensure required header files are included
+       %<RppCommonBlockTypeSetup(block, system)>
+       %<LibAddToCommonIncludes("rpp/gio.h")>
+       %assign ::rpp_dout_present = 1
+
+%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(output_type, 0)
+       %assign init_val = LibBlockParameterValue(default_output, 0)
+
+       %if !SLibCodeGenForSim()
+               %openfile buffer
+               uint32_t* dout_%<port_par>_%<pin_number_par>_desc = NULL;
+               %closefile buffer
+               %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
+
+
+
+               uint32_t dout_%<port_par>_%<pin_number_par>_cfg = PORT_CONF_FNC_GPIO|PORT_CONF_DIR_OUT;
+               %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
+
+               dout_%<port_par>_%<pin_number_par>_desc = hal_gpio_pin_get_dsc("%<name>", -1);
+
+               %if init_val == 0
+                       dout_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_INIT_LOW | PORT_CONF_MODE_PD;
+               %elseif init_val == 1
+                       dout_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_INIT_HIGH | PORT_CONF_MODE_PU;
+               %else
+                       %<LibBlockReportError(block, "Bad init output value: %<init_val>")>
+               %endif          
+
+               %if config == 1         %% Open drain
+                       dout_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_MODE_PDIS|PORT_CONF_OD_ON;
+               %elseif config == 2     %% push/pull
+                       dout_%<port_par>_%<pin_number_par>_cfg |= PORT_CONF_MODE_PEN|PORT_CONF_OD_OFF;
+               %else   %% error
+                       %<LibBlockReportError(block, "Bad configuration value: %<config>")>
+               %endif
+
+               hal_gpio_pin_conf_set(*dout_%<port_par>_%<pin_number_par>_desc, dout_%<port_par>_%<pin_number_par>_cfg);
+               
+       %endif
+
+%endfunction
+
+
+%% Function: Outputs ===========================================================
+%function Outputs(block, system) Output
+
+       %if !SLibCodeGenForSim()
+               %assign digital_out = LibBlockInputSignal(0, "", "", 0)
+               %assign port_par = LibBlockParameterValue(port_type, 0)
+               %assign pin_number_par = LibBlockParameterValue(pin_number, 0)
+               hal_gpio_pin_set_value(*dout_%<port_par>_%<pin_number_par>_desc, (uint32_t) %<digital_out>);
+       %endif
+
+%endfunction
+
+%% [EOF]
diff --git a/rpp/demos/gio_demo.slx b/rpp/demos/gio_demo.slx
new file mode 100644 (file)
index 0000000..41304f4
Binary files /dev/null and b/rpp/demos/gio_demo.slx differ
index 446e7da4de5b3b2e3dffa653d8a2b0fe316fed9b..93051f608c315d146619f7d0fefc5955c6d7cb85 100644 (file)
     void main(void)
     {
         /* Initialize RPP board */
-               %if EXISTS(::rpp_ain_present)
+        %if EXISTS(::rpp_ain_present)
             rpp_adc_init();
         %endif
-               rpp_sci_init();
+        %if EXISTS(::rpp_din_present) || EXISTS(::rpp_dout_present)
+            rpp_gio_init(RPP_GIO_PORT_ALL);
+        %endif
+        rpp_sci_init();
 
         // Speed up the SCI
         rpp_sci_setup(115200);