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