]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_aout.c
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_aout.c
1 /* Copyright (C) 2013, 2014 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_aout.c
28  * Abstract:
29  *     C-MEX S-function block for RPP analog output.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_aout.c
37  */
38
39 /*
40 %YAML 1.2
41 ---
42 Name: Analog Output
43 Category: IO blocks
44 Header: rpp/adc.h
45 Mnemonic: DAC
46
47 Inputs:
48   - { name: "Analog Output", type: "uint16" }
49
50 Outputs:
51   - { name: "ErrFlag", type: "bool" }
52
53 Parameters:
54   - { name: "Pin number [1-4]", type: "uint8" }
55   - { name: "Input in millivolts", type: "bool"  }
56
57 # Description and Help is in Markdown mark-up
58 Description: |
59
60   Sets the analog value of the specified analog output pin on the RPP
61   board.
62
63   If 'input is voltage' is set the value must be an unsigned 16 bit
64   integer between 0-12000 (millivolts to set the analog output). If
65   'input is voltage' is NOT set the value must be and unsigned 16 bit
66   integer between 0-4095 (DAC is 12 bit).
67
68   If an error is detected while setting the value, the ErrFlag is set
69   high.
70
71 Help: |
72
73   Sets the analog value of the specified analog output pin on the RPP board. If an error is detected 
74   while setting the value, the ErrFlag is set high.  
75
76   This block allows to write to the analog outputs on the RPP board. The UseVoltage flag allows the 
77   user to configure if block inputs should be interpreted as raw DAC value or millivolts. The ErrFlag 
78   should raise it `rpp_dac_update()` or `rpp_dac_set()` (or `rpp_dac_set_voltage()` 
79   depending on block configuration) returns error. Because the ErrFlag should never set, once set the 
80   following steps will never clear it back.
81
82   `rpp_dac_update()` is called on each block but the implementation provides this to be efficient.
83
84   There is a know bug on the RPP Library, check `rpp_dac_update()` on the RPP API for details. 
85   Because of this, the outputs of the DACs are initialized on the first step of the model and not on 
86   the model initialization.
87
88 Status: Stable
89
90 RPP API functions used:
91     - rpp_dac_setup()
92     - rpp_dac_set()
93     - rpp_dac_set_voltage()
94     - rpp_dac_update()
95
96 Relevant demos:
97     - analog_passthrough
98     - analog_sinewave
99 ...
100 */
101
102
103 #define S_FUNCTION_NAME sfunction_aout
104 #include "header.c"
105
106
107 static void mdlInitializeSizes(SimStruct *S)
108 {
109     /*
110      * Configure parameters: 1
111      *  - Pin number: [1-4]
112      *  - Use voltage.
113      */
114     if (!rppSetNumParams(S, 2)) {
115         return;
116     }
117
118     /*
119      * Configure input ports: 1
120      *  - Analog output.
121      */
122     if (!ssSetNumInputPorts(S, 1)) {
123         return;
124     }
125     rppAddInputPort(S, 0, SS_UINT16);
126
127     /*
128      * Configure output ports: 1
129      *  - Error flag.
130      */
131     if (!ssSetNumOutputPorts(S, 1)) {
132         return;
133     }
134     rppAddOutputPort(S, 0, SS_BOOLEAN);
135
136     /* Set standard options for this block */
137     rppSetStandardOptions(S);
138 }
139
140
141 #ifdef MATLAB_MEX_FILE
142 #define MDL_CHECK_PARAMETERS
143 static void mdlCheckParameters(SimStruct *S)
144 {
145     /* Check the parameter 1 */
146     if (!rppValidParamRange(S, 0, 1, 4)) {
147         return;
148     }
149
150     /* Check the parameter 2 */
151     if (!rppValidParamBoolean(S, 1)) {
152         return;
153     }
154 }
155 #endif
156
157
158 #ifdef MATLAB_MEX_FILE
159 #define MDL_SET_WORK_WIDTHS
160 static void mdlSetWorkWidths(SimStruct *S)
161 {
162     /* Set number of run-time parameters */
163     if (!ssSetNumRunTimeParams(S, 2)) {
164         return;
165     }
166
167     /* Register the run-time parameter 1 */
168     ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
169
170     /* Register the run-time parameter 2 */
171     ssRegDlgParamAsRunTimeParam(S, 1, 1, "p2", SS_BOOLEAN);
172 }
173 #endif
174
175
176 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
177 #define UNUSED_MDLOUTPUTS
178 #define UNUSED_MDLTERMINATE
179 #include "trailer.c"