]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_dac.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_dac.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  *
30  * File : cmd_dac.c
31  *
32  * Abstract:
33  *      This file contains commands for DAC control. User can setup DIN pin as active or inactive, set voltage in mV units and set raw value on pin.
34  *
35  */
36
37 #include "cmd_dac.h"
38 #include "stdio.h"
39
40 #ifndef DOCGEN
41
42 #include "cmdproc_utils.h"
43 #include "rpp/rpp.h"
44
45
46 /**
47  * @brief       Setup DAC pin enable or disable
48  *
49  * @param[in]   cmd_io  Pointer to IO stack
50  * @param[in]   des             Pointer to command descriptor
51  * @param[in]   param   Parameters of command
52  * @return      0 when OK or error code
53  */
54 int cmd_do_dac_pin_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
55 {
56         int pin;
57         char *p = param[1];
58         char spareParams;
59         int enabled;
60         int ret;
61
62         if (sscanf(p, "%d %d %1s", &pin, &enabled, &spareParams) != 2)
63                 return -CMDERR_BADPAR;
64         ret = rpp_dac_setup(pin, enabled);
65         if (ret == -1) {
66                 rpp_sci_printf("Pin out of range.\n");
67                 return -CMDERR_BADPAR;
68         }
69
70         if (rpp_dac_update() == FAILURE) {
71                 rpp_sci_printf("DAC update failed.\n");
72                 return -CMDERR_EIO;
73         }
74
75         return cmd_opchar_replong(cmd_io, param, enabled, 0, 10);
76 }
77
78 /**
79  * @brief       Set DAC pin raw value
80  *
81  * @param[in]   cmd_io  Pointer to IO stack
82  * @param[in]   des             Pointer to command descriptor
83  * @param[in]   param   Parameters of command
84  * @return      0 when OK or error code
85  */
86 int cmd_do_dac_pin_set_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
87 {
88         int pin;
89         char *p = param[1];
90         char spareParams;
91         int val;
92         int ret;
93
94         if (sscanf(p, "%d %d %1s", &pin, &val, &spareParams) != 2)
95                 return -CMDERR_BADPAR;
96         ret = rpp_dac_set(pin, val);
97         if (ret == -1) {
98                 rpp_sci_printf("Pin out of range.\n");
99                 return -CMDERR_BADPAR;
100         }
101         else if (ret == -2) {
102                 rpp_sci_printf("Value out of range.\n");
103                 return -CMDERR_BADPAR;
104         }
105
106         if (rpp_dac_update() == FAILURE) {
107                 rpp_sci_printf("DAC update failed.\n");
108                 return -CMDERR_EIO;
109         }
110         return cmd_opchar_replong(cmd_io, param, val, 0, 10);
111 }
112
113 /**
114  * @brief       Set DAC pin voltage value in mV units
115  *
116  * @param[in]   cmd_io  Pointer to IO stack
117  * @param[in]   des             Pointer to command descriptor
118  * @param[in]   param   Parameters of command
119  * @return      0 when OK or error code
120  */
121 int cmd_do_dac_pin_set_voltage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
122 {
123         int pin;
124         char *p = param[1];
125         char spareParams;
126         int mV;
127         int ret;
128
129         if (sscanf(p, "%d %d %1s", &pin, &mV, &spareParams) != 2)
130                 return -CMDERR_BADPAR;
131         ret = rpp_dac_set_voltage(pin, mV);
132         if (ret == -1) {
133                 rpp_sci_printf("Pin out of range.\n");
134                 return -CMDERR_BADPAR;
135         }
136         else if (ret == -2) {
137                 rpp_sci_printf("Voltage out of range.\n");
138                 return -CMDERR_BADPAR;
139         }
140
141         if (rpp_dac_update() == FAILURE) {
142                 rpp_sci_printf("DAC update failed.\n");
143                 return -CMDERR_EIO;
144         }
145         return cmd_opchar_replong(cmd_io, param, mV, 0, 10);
146 }
147
148 #endif  /* DOCGEN */
149
150 /** Descriptor of command for dac pin setup */
151 cmd_des_t const cmd_des_dac_pin_setup = {
152         0, 0,
153         "dacpinenable*","Enable or disable a DAC pin",
154         "### Command syntax ###\n"
155         "\n"
156         "    dacpinenable<PIN> <VALUE>\n"
157         "where\n"
158         "\n"
159         "- `<PIN>` is a number in range 1-4\n"
160         "- `<VALUE>` is a number 0 (disable) or 1 (enable)\n"
161         "\n"
162         "### Description ###\n"
163         "\n"
164         "Command for enabling or disabling of a DAC pin.\n"
165         "\n"
166         "Command always prints the actual state of the selected pin.\n"
167         "\n"
168         "### Example ###\n"
169         "\n"
170         "    --> dacpinenable1 1\n"
171         "    dacpinenable1=1\n"
172         "\n"
173         "Enables pin DAC1 and prints its actual state (which will be 1)\n",
174         CMD_HANDLER(cmd_do_dac_pin_setup), (void *)&cmd_list_dac
175 };
176
177 /** Descriptor of command for dac pin value set */
178 cmd_des_t const cmd_des_dac_pin_set_val = {
179         0, 0,
180         "dacpinval*","Set raw value of a DAC register",
181         "### Command syntax ###\n"
182         "\n"
183         "    dacpinval<PIN> <VALUE>\n"
184         "where\n"
185         "\n"
186         "- `<PIN>` is a number in range 1-4\n"
187         "- `<VALUE>` is a number in range 0-4095\n"
188         "\n"
189         "### Description ###\n"
190         "\n"
191         "This command writes a raw value to DAC register that controls the DAC\n"
192         "output voltage according to the formula described in the datasheet.\n"
193         "`<PIN>` parameter selects which DAC pin to use.\n"
194         "\n"
195         "Command always prints the written raw value of the selected pin. There\n"
196         "is no way how to read the value out of the register.\n"
197         "\n"
198         "### Example ###\n"
199         "\n"
200         "    --> dacpinval1 4095\n"
201         "    dacpinval1 =4095\n"
202         "\n"
203         "Set pin DAC1 voltage to 12V, and prints the value (4095).\n",
204         CMD_HANDLER(cmd_do_dac_pin_set_val), (void *)&cmd_list_dac
205 };
206
207 /** Descriptor of command for dac pin voltage set */
208 cmd_des_t const cmd_des_dac_pin_set_voltage = {
209         0, 0,
210         "dacpinvoltage*","Set voltage in mV of a DAC pin",
211         "### Command syntax ###\n"
212         "\n"
213         "    dacpinvoltage<PIN> <VALUE>\n"
214         "where\n"
215         "\n"
216         "- `<PIN>` is a number in range 1-4\n"
217         "- `<VALUE>` is a number in range 0-12000\n"
218         "\n"
219         "### Description ###\n"
220         "\n"
221         "This command sets the voltage on a DAC pin.\n"
222         "\n"
223         "The command always prints the actually set voltage of selected pin.\n"
224         "There is no way how to read the value back out of the pin.\n"
225         "\n"
226         "### Example ###\n"
227         "\n"
228         "    --> dacpinvoltage1 8000\n"
229         "    dacpinvoltage1 =8000\n"
230         "\n"
231         "Sets pin DAC1 to 8V, prints the actual voltage (8000)\n"
232         "\n"
233         "    --> dacpinvoltage2 500\n"
234         "    dacpinvoltage2 =500\n"
235         "\n"
236         "Sets pin DAC2 to 500mV, prints actual voltage (500)\n",
237         CMD_HANDLER(cmd_do_dac_pin_set_voltage), (void *)&cmd_list_dac
238 };
239
240 /** List of commands for dac, defined as external */
241 cmd_des_t const *cmd_list_dac[] = {
242         &cmd_des_dac_pin_setup,
243         &cmd_des_dac_pin_set_val,
244         &cmd_des_dac_pin_set_voltage,
245         NULL
246 };