]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_gio_out.c
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_gio_out.c
1 /* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * File : sfunction_gio_out.c
28  * Abstract:
29  *     C-MEX S-function block for writting to RPP GPIO ports.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_gio_out.c
37  */
38
39 /*
40 %YAML 1.2
41 ---
42 Name: General Purpose Digital Output
43 Category: IO blocks
44 Header: rpp/gio.h
45 Mnemonic: GIOOUT
46
47 Inputs:
48   - { name: "Digital Output value", type: "bool" }
49 Outputs:
50
51 Parameters:
52   - { name: "Pin", type: "Choice", range: "Target dependent pin names or '---'" }
53   - { name: "Pin name", type: "String", note: "Pin name as defined in gio_def.h. This parameter is only visible if *Pin* is '---'." }
54   - { name: "Initial output value", type: "uint8", range: "[0,1]" }
55   - { name: "Output Type", type: "Choice", range: "Open Drain, Push/Pull" }
56
57 # Description is in Markdown mark-up
58 Description: &desc |
59
60   Sets a GIO pin to a given value. Which pins are supported depends on
61   the target board. Any pin can be configured as open drain or
62   push/pull and initial output value can be specified. The initial
63   value is a value, which will appear on the pin when the GPIO module
64   is initialized and stays there until the first block execution.
65
66   When the pin is used in multiple GIOIN or GIOOUT blocks, an error is
67   signaled.
68
69 Help: *desc
70
71 Status: Stable
72
73 RPP API functions used:
74   - rpp_gio_setup
75   - rpp_gio_set
76
77 Relevant demos:
78   - gio_demo
79   - board_init_hydctr
80 ...
81 */
82
83 /*
84  * Block mask documentation - see sfunction_gio_in.c
85  */
86
87 #define S_FUNCTION_NAME sfunction_gio_out
88 #include "header.c"
89
90 #define PARAM_NAME_PORT_TYPE            "port_type"
91 #define PARAM_NAME_PIN_NUMBER           "pin_number"
92 #define PARAM_NAME_DEFAULT_OUTPUT       "default_output"
93 #define PARAM_NAME_OUTPUT_TYPE          "output_type"
94
95 /** Identifiers of the block parameters */
96 enum params{
97         PARAM_PIN_NAME,
98         PARAM_DEFAULT_OUTPUT,
99         PARAM_OUTPUT_TYPE,
100         PARAMS_COUNT
101 };
102
103 enum inputs{
104         IN_PIN_VALUE,
105         INPUTS_COUNT
106 };
107
108 static void mdlInitializeSizes(SimStruct *S)
109 {
110         if (!rppSetNumParams(S, PARAMS_COUNT)) {
111                 return;
112         }
113
114         /*
115         * Configure output ports: 0
116         */
117         if (!ssSetNumOutputPorts(S, 0)) {
118                 return;
119         }
120
121         /*
122         * Configure input ports: 1
123         */
124         if (!ssSetNumInputPorts(S, INPUTS_COUNT)) {
125                 return;
126         }
127
128         rppAddInputPort(S, IN_PIN_VALUE, SS_BOOLEAN);
129
130         /* Set standard options for this block */
131         rppSetStandardOptions(S);
132 }
133
134
135 #ifdef MATLAB_MEX_FILE
136 #define MDL_CHECK_PARAMETERS
137 static void mdlCheckParameters(SimStruct *S)
138 {
139     if (!mxIsChar(ssGetSFcnParam(S, PARAM_PIN_NAME))) {
140         ssSetErrorStatus(S, "Parameter to S-function must be a string.");
141     }
142         if (!rppValidParamRange(S, PARAM_DEFAULT_OUTPUT, 0, 1)) {
143                 return;
144         }
145 }
146 #endif
147
148 #if defined(MATLAB_MEX_FILE)
149 #define MDL_RTW
150 static void mdlRTW(SimStruct *S)
151 {
152     char_T pin_name[80];
153     int8_T def_out = mxGetPr(ssGetSFcnParam(S, PARAM_DEFAULT_OUTPUT))[0];
154     int8_T out_type = mxGetPr(ssGetSFcnParam(S, PARAM_OUTPUT_TYPE))[0];
155
156     if (mxGetString(ssGetSFcnParam(S, PARAM_PIN_NAME), pin_name, sizeof(pin_name)) != 0) {
157         ssSetErrorStatus(S,"mxGetString error in mdlRTW");
158         return;
159     }
160
161     if (!ssWriteRTWParamSettings(S, 3,
162                                  SSWRITE_VALUE_STR, "PinName", pin_name,
163                                  SSWRITE_VALUE_DTYPE_NUM, "DefOut", &def_out, DTINFO(SS_INT8, COMPLEX_NO),
164                                  SSWRITE_VALUE_DTYPE_NUM, "OutType", &out_type, DTINFO(SS_INT8, COMPLEX_NO))) {
165         ssSetErrorStatus(S, "ssWriteRTWParamSettings failed");
166     }
167 }
168 #endif /* MDL_RTW */
169
170
171
172 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
173 #define UNUSED_MDLOUTPUTS
174 #define UNUSED_MDLTERMINATE
175 #include "trailer.c"