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