]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_din.c
Add TODOs etc.
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_din.c
1 /* TODO: Přejmenovat na sfunction_gio_in.c */
2
3 /* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
4  *
5  * Authors:
6  *     - Carlos Jenkins <carlos@jenkins.co.cr>
7  *
8  * This document contains proprietary information belonging to Czech
9  * Technical University in Prague. Passing on and copying of this
10  * document, and communication of its contents is not permitted
11  * without prior written authorization.
12  *
13  * File : sfunction_ain.c
14  * Abstract:
15  *     C-MEX S-function block for reading from RPP GPIO ports.
16  *
17  * References:
18  *     header.c
19  *     trailer.c
20  *
21  * Compile with:
22  *     <matlabroot>/bin/mex sfunction_din.c
23  */
24
25 /*
26 %YAML 1.2
27 ---
28 Name: Digital Input
29 Category: IO blocks
30 Header: rpp/gio.h
31 Mnemonic: DIN
32
33 Inputs:
34
35 Outputs:
36   - { name: "Digital Input value", type: "bool" }
37 Parameters:
38   - { name: "Port type", type: "Choice", range: "GIOA, GIOB, NHET1" }
39   - { name: "Pin number", type: "int8",   range: "[0–7]", note: "(depends on selected port)" }
40   - { name: "Input Type", type: "Choice", range: "Tri-state, Pull Up, Pull Down" }
41
42 # Description is in Markdown mark-up
43 Description: &desc |
44   Reads a value from a GPIO pin. The block supports GIOA, GIOB and NHET1 ports.
45   Any pin can be configured as tri-state, pull up or pull down.
46
47   It is not allowed to read from one pin by using more then one DIN blocks.
48   It is not allowed to use DIN and DOUT blocks together configured for one GPIO pin.
49
50 Help: *desc
51
52 Status: Stable
53
54 RPP API functions used:
55   - hal_gpio_pin_get_dsc
56   - hal_gpio_pin_get_value
57   - hal_gpio_pin_conf_set
58
59 Relevant demos:
60   - gio_demo
61 ...
62 */
63
64
65 #define S_FUNCTION_NAME sfunction_din
66 #include "header.c"
67
68 #define PARAM_NAME_PORT_TYPE            "port_type"
69 #define PARAM_NAME_PIN_NUMBER           "pin_number"
70 #define PARAM_NAME_INPUT_TYPE           "input_type"
71
72 /** Identifiers of the block parameters */
73 enum params{
74         PARAM_PORT_TYPE,
75         PARAM_PIN_NUMBER,
76         PARAM_INPUT_TYPE,
77         PARAMS_COUNT
78 };
79
80 enum port_types{
81         PORT_UNKNOWN,
82         PORT_GIOA,
83         PORT_GIOB,
84         PORT_NHET1
85 };
86
87 enum outputs{
88         OUT_PIN_VALUE,
89         OUTPUTS_COUNT
90 };
91
92 static void mdlInitializeSizes(SimStruct *S)
93 {
94         /*
95         * Configure parameters: 3
96         *  - Port type
97         *  - Pin number
98         *  - Input type
99         */
100         if (!rppSetNumParams(S, PARAMS_COUNT)) {
101                 return;
102         }
103
104         /*
105         * Configure input ports: 0
106         */
107         if (!ssSetNumInputPorts(S, 0)) {
108                 return;
109         }
110
111         /*
112         * Configure output ports: 1
113         */
114         if (!ssSetNumOutputPorts(S, OUTPUTS_COUNT)) {
115                 return;
116         }
117         rppAddOutputPort(S, OUT_PIN_VALUE, SS_BOOLEAN);
118
119         /* Set standard options for this block */
120         rppSetStandardOptions(S);
121 }
122
123
124 #ifdef MATLAB_MEX_FILE
125 #define MDL_CHECK_PARAMETERS
126 static void mdlCheckParameters(SimStruct *S)
127 {
128         if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_GIOA) {
129                 if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 7)) {
130                         return;
131                 }
132         }
133         else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_GIOB) {
134                 if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 7)) {
135                         return;
136                 }
137         }
138         else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_NHET1) {
139                 if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 31)) {
140                         return;
141                 }
142         }
143         else {
144                 return;
145         }
146 }
147 #endif
148
149
150 #ifdef MATLAB_MEX_FILE
151 #define MDL_SET_WORK_WIDTHS
152 static void mdlSetWorkWidths(SimStruct *S)
153 {
154         /* Set number of run-time parameters */
155         if (!ssSetNumRunTimeParams(S, PARAMS_COUNT)) {
156                 return;
157         }
158         /* Register the run-time parameter 1 */
159         ssRegDlgParamAsRunTimeParam(S, PARAM_PORT_TYPE,      PARAM_PORT_TYPE,      PARAM_NAME_PORT_TYPE,      SS_INT8);
160         ssRegDlgParamAsRunTimeParam(S, PARAM_PIN_NUMBER,     PARAM_PIN_NUMBER,     PARAM_NAME_PIN_NUMBER,     SS_INT8);
161         ssRegDlgParamAsRunTimeParam(S, PARAM_INPUT_TYPE,     PARAM_INPUT_TYPE,     PARAM_NAME_INPUT_TYPE,     SS_INT8);
162 }
163 #endif
164
165
166 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
167 #define UNUSED_MDLOUTPUTS
168 #define UNUSED_MDLTERMINATE
169 #include "trailer.c"