]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/sfunction_dout.c
Implement simulink blocks and demo for GPIO
[jenkicar/rpp-simulink.git] / rpp / blocks / sfunction_dout.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 writting to RPP GPIO ports.
14  *
15  * References:
16  *     header.c
17  *     trailer.c
18  *
19  * Compile with:
20  *     <matlabroot>/bin/mex sfunction_dout.c
21  */
22
23 /*
24 %YAML 1.2
25 ---
26 Name: Digital output
27 Category: IO blocks
28 Header: rpp/gio.h
29 Mnemonic: DOUT
30
31 Inputs:
32         - { name: "Digital Output value", type: "bool" }
33 Outputs:
34
35 Parameters:
36         - { name: "Port type", type: "Choice", range: "GIOA, GIOB, NHET1" }
37         - { name: "Pin number", type: "int8", range: "Depends on selected port" }
38         - { name: "Initial output value", type: "bool" }
39         - { name: "Output Type", type: "Choice", range: "Open Drain, Push/Pull" }
40
41 # Description is in Markdown mark-up
42 Description:
43         Writes a value to a GPIO pin. The block supports GIOA, GIOB and NHET1 ports.
44         Any pin can be configured as open drain or push/pull and initial output value
45         can be specified. The initial value is a value, which will appear on the pin
46         after gpio module is initialized and persists here until the first write.
47
48 Status:
49         Tested:
50         Untested:
51         Not working:
52
53 RPP API functions used:
54         - hal_gpio_pin_get_dsc
55         - hal_gpio_pin_set_value
56         - hal_gpio_pin_conf_set
57
58 Relevant demos:
59         - gio_demo
60 ...
61 */
62
63
64 #define S_FUNCTION_NAME sfunction_dout
65 #include "header.c"
66
67 #define PARAM_NAME_PORT_TYPE            "port_type"
68 #define PARAM_NAME_PIN_NUMBER           "pin_number"
69 #define PARAM_NAME_DEFAULT_OUTPUT       "default_output"
70 #define PARAM_NAME_OUTPUT_TYPE          "output_type"
71
72 /** Identifiers of the block parameters */
73 enum params{
74         PARAM_PORT_TYPE,
75         PARAM_PIN_NUMBER,
76         PARAM_DEFAULT_OUTPUT,
77         PARAM_OUTPUT_TYPE,
78         PARAMS_COUNT
79 };
80
81 enum port_types{
82         PORT_UNKNOWN,
83         PORT_GIOA,
84         PORT_GIOB,
85         PORT_NHET1
86 };
87
88 enum inputs{
89         IN_PIN_VALUE,
90         INPUTS_COUNT
91 };
92
93 static void mdlInitializeSizes(SimStruct *S)
94 {
95         /*
96         * Configure parameters: 4
97         *  - Port type
98         *  - Pin number
99         *  - Default output value
100         *  - Output type
101         */
102         if (!rppSetNumParams(S, PARAMS_COUNT)) {
103                 return;
104         }
105
106         /*
107         * Configure output ports: 0
108         */
109         if (!ssSetNumOutputPorts(S, 0)) {
110                 return;
111         }
112
113         /*
114         * Configure input ports: 1
115         */
116         if (!ssSetNumInputPorts(S, INPUTS_COUNT)) {
117                 return;
118         }
119
120         rppAddInputPort(S, IN_PIN_VALUE, SS_BOOLEAN);
121
122         /* Set standard options for this block */
123         rppSetStandardOptions(S);
124 }
125
126
127 #ifdef MATLAB_MEX_FILE
128 #define MDL_CHECK_PARAMETERS
129 static void mdlCheckParameters(SimStruct *S)
130 {
131         if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_GIOA) {
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_GIOB) {
137                 if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 7)) {
138                         return;
139                 }
140         }
141         else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_PORT_TYPE))[0] == PORT_NHET1) {
142                 if (!rppValidParamRange(S, PARAM_PIN_NUMBER, 0, 31)) {
143                         return;
144                 }
145         }
146         else {
147                 return;
148         }
149
150         if (!rppValidParamRange(S, PARAM_DEFAULT_OUTPUT, 0, 1)) {
151                 return;
152         }
153 }
154 #endif
155
156
157 #ifdef MATLAB_MEX_FILE
158 #define MDL_SET_WORK_WIDTHS
159 static void mdlSetWorkWidths(SimStruct *S)
160 {
161         /* Set number of run-time parameters */
162         if (!ssSetNumRunTimeParams(S, PARAMS_COUNT)) {
163                 return;
164         }
165         /* Register the run-time parameter 1 */
166         ssRegDlgParamAsRunTimeParam(S, PARAM_PORT_TYPE,      PARAM_PORT_TYPE,      PARAM_NAME_PORT_TYPE,      SS_INT8);
167         ssRegDlgParamAsRunTimeParam(S, PARAM_PIN_NUMBER,     PARAM_PIN_NUMBER,     PARAM_NAME_PIN_NUMBER,     SS_INT8);
168         ssRegDlgParamAsRunTimeParam(S, PARAM_DEFAULT_OUTPUT, PARAM_DEFAULT_OUTPUT, PARAM_NAME_DEFAULT_OUTPUT, SS_BOOLEAN);
169         ssRegDlgParamAsRunTimeParam(S, PARAM_OUTPUT_TYPE,    PARAM_OUTPUT_TYPE,    PARAM_NAME_OUTPUT_TYPE,    SS_INT8);
170 }
171 #endif
172
173
174 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
175 #define UNUSED_MDLOUTPUTS
176 #define UNUSED_MDLTERMINATE
177 #include "trailer.c"