]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_dac.c
Apply uncrustify
[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  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * File : cmd_dac.c
23  *
24  * Abstract:
25  *      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.
26  *
27  */
28
29 #include "cmd_dac.h"
30 #include "stdio.h"
31
32 #ifndef DOCGEN
33
34 #include "cmdproc_utils.h"
35 #include "rpp/rpp.h"
36
37
38 /**
39  * @brief       Setup DAC pin enable or disable
40  *
41  * @param[in]   cmd_io  Pointer to IO stack
42  * @param[in]   des             Pointer to command descriptor
43  * @param[in]   param   Parameters of command
44  * @return      0 when OK or error code
45  */
46 int cmd_do_dac_pin_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
47 {
48         int pin;
49         char *p = param[1];
50         char spareParams;
51         int enabled;
52         int ret;
53
54         if (sscanf(p, "%d %d %1s", &pin, &enabled, &spareParams) != 2)
55                 return -CMDERR_BADPAR;
56         ret = rpp_dac_setup(pin, enabled);
57         if (ret == -1) {
58                 rpp_sci_printf("Pin out of range.\n");
59                 return -CMDERR_BADPAR;
60         }
61
62         if (rpp_dac_update() == FAILURE) {
63                 rpp_sci_printf("DAC update failed.\n");
64                 return -CMDERR_EIO;
65         }
66
67         return cmd_opchar_replong(cmd_io, param, enabled, 0, 10);
68 }
69
70 /**
71  * @brief       Set DAC pin raw value
72  *
73  * @param[in]   cmd_io  Pointer to IO stack
74  * @param[in]   des             Pointer to command descriptor
75  * @param[in]   param   Parameters of command
76  * @return      0 when OK or error code
77  */
78 int cmd_do_dac_pin_set_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
79 {
80         int pin;
81         char *p = param[1];
82         char spareParams;
83         int val;
84         int ret;
85
86         if (sscanf(p, "%d %d %1s", &pin, &val, &spareParams) != 2)
87                 return -CMDERR_BADPAR;
88         ret = rpp_dac_set(pin, val);
89         if (ret == -1) {
90                 rpp_sci_printf("Pin out of range.\n");
91                 return -CMDERR_BADPAR;
92         }
93         else if (ret == -2) {
94                 rpp_sci_printf("Value out of range.\n");
95                 return -CMDERR_BADPAR;
96         }
97
98         if (rpp_dac_update() == FAILURE) {
99                 rpp_sci_printf("DAC update failed.\n");
100                 return -CMDERR_EIO;
101         }
102         return cmd_opchar_replong(cmd_io, param, val, 0, 10);
103 }
104
105 /**
106  * @brief       Set DAC pin voltage value in mV units
107  *
108  * @param[in]   cmd_io  Pointer to IO stack
109  * @param[in]   des             Pointer to command descriptor
110  * @param[in]   param   Parameters of command
111  * @return      0 when OK or error code
112  */
113 int cmd_do_dac_pin_set_voltage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
114 {
115         int pin;
116         char *p = param[1];
117         char spareParams;
118         int mV;
119         int ret;
120
121         if (sscanf(p, "%d %d %1s", &pin, &mV, &spareParams) != 2)
122                 return -CMDERR_BADPAR;
123         ret = rpp_dac_set_voltage(pin, mV);
124         if (ret == -1) {
125                 rpp_sci_printf("Pin out of range.\n");
126                 return -CMDERR_BADPAR;
127         }
128         else if (ret == -2) {
129                 rpp_sci_printf("Voltage out of range.\n");
130                 return -CMDERR_BADPAR;
131         }
132
133         if (rpp_dac_update() == FAILURE) {
134                 rpp_sci_printf("DAC update failed.\n");
135                 return -CMDERR_EIO;
136         }
137         return cmd_opchar_replong(cmd_io, param, mV, 0, 10);
138 }
139
140 #endif  /* DOCGEN */
141
142 /** Descriptor of command for dac pin setup */
143 cmd_des_t const cmd_des_dac_pin_setup = {
144         0, 0,
145         "dacpinenable*","Enable or disable a DAC pin",
146         "### Command syntax ###\n"
147         "\n"
148         "    dacpinenable<PIN> <VALUE>\n"
149         "where\n"
150         "\n"
151         "- `<PIN>` is a number in range 1-4\n"
152         "- `<VALUE>` is a number 0 (disable) or 1 (enable)\n"
153         "\n"
154         "### Description ###\n"
155         "\n"
156         "Command for enabling or disabling of a DAC pin.\n"
157         "\n"
158         "Command always prints the actual state of the selected pin.\n"
159         "\n"
160         "### Example ###\n"
161         "\n"
162         "    --> dacpinenable1 1\n"
163         "    dacpinenable1=1\n"
164         "\n"
165         "Enables pin DAC1 and prints its actual state (which will be 1)\n",
166         CMD_HANDLER(cmd_do_dac_pin_setup), (void *)&cmd_list_dac
167 };
168
169 /** Descriptor of command for dac pin value set */
170 cmd_des_t const cmd_des_dac_pin_set_val = {
171         0, 0,
172         "dacpinval*","Set raw value of a DAC register",
173         "### Command syntax ###\n"
174         "\n"
175         "    dacpinval<PIN> <VALUE>\n"
176         "where\n"
177         "\n"
178         "- `<PIN>` is a number in range 1-4\n"
179         "- `<VALUE>` is a number in range 0-4095\n"
180         "\n"
181         "### Description ###\n"
182         "\n"
183         "This command writes a raw value to DAC register that controls the DAC\n"
184         "output voltage according to the formula described in the datasheet.\n"
185         "`<PIN>` parameter selects which DAC pin to use.\n"
186         "\n"
187         "Command always prints the written raw value of the selected pin. There\n"
188         "is no way how to read the value out of the register.\n"
189         "\n"
190         "### Example ###\n"
191         "\n"
192         "    --> dacpinval1 4095\n"
193         "    dacpinval1 =4095\n"
194         "\n"
195         "Set pin DAC1 voltage to 12V, and prints the value (4095).\n",
196         CMD_HANDLER(cmd_do_dac_pin_set_val), (void *)&cmd_list_dac
197 };
198
199 /** Descriptor of command for dac pin voltage set */
200 cmd_des_t const cmd_des_dac_pin_set_voltage = {
201         0, 0,
202         "dacpinvoltage*","Set voltage in mV of a DAC pin",
203         "### Command syntax ###\n"
204         "\n"
205         "    dacpinvoltage<PIN> <VALUE>\n"
206         "where\n"
207         "\n"
208         "- `<PIN>` is a number in range 1-4\n"
209         "- `<VALUE>` is a number in range 0-12000\n"
210         "\n"
211         "### Description ###\n"
212         "\n"
213         "This command sets the voltage on a DAC pin.\n"
214         "\n"
215         "The command always prints the actually set voltage of selected pin.\n"
216         "There is no way how to read the value back out of the pin.\n"
217         "\n"
218         "### Example ###\n"
219         "\n"
220         "    --> dacpinvoltage1 8000\n"
221         "    dacpinvoltage1 =8000\n"
222         "\n"
223         "Sets pin DAC1 to 8V, prints the actual voltage (8000)\n"
224         "\n"
225         "    --> dacpinvoltage2 500\n"
226         "    dacpinvoltage2 =500\n"
227         "\n"
228         "Sets pin DAC2 to 500mV, prints actual voltage (500)\n",
229         CMD_HANDLER(cmd_do_dac_pin_set_voltage), (void *)&cmd_list_dac
230 };
231
232 /** List of commands for dac, defined as external */
233 cmd_des_t const *cmd_list_dac[] = {
234         &cmd_des_dac_pin_setup,
235         &cmd_des_dac_pin_set_val,
236         &cmd_des_dac_pin_set_voltage,
237         NULL
238 };