]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_hout.c
Merge branch 'master' of ssh://rtime.felk.cvut.cz/rpp-test-sw
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_hout.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_hout.c
23  *
24  * Abstract:
25  *      This file contains commands for HOUT control.
26  *
27  */
28
29 #include "cmd_hout.h"
30 #include "stdio.h"
31
32 #ifndef DOCGEN
33
34 #include "rpp/rpp.h"
35 #include "hal/hal.h"
36 #include "drv/drv.h"
37 #include "cmdproc_utils.h"
38
39
40 /**
41  * @brief       Reads values from HOUT_IFBK pins (subset of ADC)
42  *
43  * @param[in]   cmd_io  Pointer to IO stack
44  * @param[in]   des             Pointer to command descriptor
45  * @param[in]   param   Parameters of command
46  * @return      0 when OK or error code
47  */
48 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
49 {
50     uint32_t i;                                         // Cycle control variable
51     port_desc_t* desc;
52     uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
53     int ret;
54
55     desc = hal_port_get_dsc(PORT_NAME_HOUTIFBK, -1);
56
57     ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
58     if (ret < 0)
59         return ret;
60
61     for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
62         rpp_sci_printf("HOUT%d: %d\n", i+1, values[i]);
63     }
64     return 0;
65 }
66
67 /**
68  * @brief       Runs test of selected HOUT pin.
69  *
70  * @param[in]   cmd_io  Pointer to IO stack
71  * @param[in]   des             Pointer to command descriptor
72  * @param[in]   param   Parameters of command
73  * @return      0 when OK or error code
74  */
75 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
76 {
77         int pin;
78         char *p=param[1];
79         char spareParams;
80
81         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
82                 return -CMDERR_BADPAR;
83         }
84         pin--;
85         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
86
87         switch (hout_fail(pin)) {
88         case HOUT_OK:
89                 rpp_sci_printf("OK");
90                 break;
91         case HOUT_FAILED:
92                 rpp_sci_printf("FAIL");
93                 break;
94         case HOUT_NOT_ON:
95                 rpp_sci_printf("NOT RUNNING");
96                 break;
97         default:
98                 rpp_sci_printf("Bad pin selected");
99                 break;
100         }
101         rpp_sci_printf("\n");
102         return 0;
103 }
104
105 /**
106  *      @brief  Set or get actual pwm parameters
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_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
114 {
115           char *p;
116           uint32_t values[MAX_PARAM_VALUES_NUM];
117           char spareParams;
118           int pin;
119
120           p=param[1];
121           if (sscanf(p, "%d", &pin) != 1) {
122                   return -CMDERR_BADPAR;
123           }
124           pin--;
125           if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
126
127           if(param[2] != NULL){         // More parameters = set values
128             p=param[2];
129             if (sscanf(p, "%d %d %1s", &values[0], &values[1], &spareParams) != 2) {
130                 return -CMDERR_BADPAR;
131             }
132                 if (values[1] > 100) return -CMDERR_BADPAR;
133                 hout_pwm_set_signal(pin, (double)values[0], values[1]);
134             return 0;
135           }
136           else{ // No more parameters = get values
137                 double period = hout_pwm_get_period(pin);
138                 uint32_t duty = hout_pwm_get_duty(pin);
139
140                 rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%", pin+1, period, pin+1, duty);
141                 return 0;
142           }
143 }
144
145 /**
146  *      @brief  Start PWM, if it was previously set by houtpwm command
147  *
148  * @param[in]   cmd_io  Pointer to IO stack
149  * @param[in]   des             Pointer to command descriptor
150  * @param[in]   param   Parameters of command
151  * @return      0 when OK or error code
152  */
153 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
154 {
155         int pin;
156         char *p=param[1];
157         char spareParams;
158
159         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
160                 return -CMDERR_BADPAR;
161         }
162         pin--;
163         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
164         int ret = hout_pwm_start(pin);
165         if (ret == -1) {
166                 rpp_sci_printf("PWM was not initialized.\r\n");
167                 return -CMDERR_BADCFG;
168         }
169         else {
170                 return 0;
171         }
172 }
173
174 /**
175  *      @brief  Stop PWM
176  *
177  * @param[in]   cmd_io  Pointer to IO stack
178  * @param[in]   des             Pointer to command descriptor
179  * @param[in]   param   Parameters of command
180  * @return      0 when OK or error code
181  */
182 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
183 {
184         int pin;
185         char *p=param[1];
186         char spareParams;
187
188         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
189                 return -CMDERR_BADPAR;
190         }
191         pin--;
192         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
193         hout_pwm_stop(pin);
194         return 0;
195 }
196
197 #endif  /* DOCGEN */
198
199 /** Command descriptor for test hout fault state command */
200 cmd_des_t const cmd_des_test_hout_fail = {
201     0, 0,
202     "houtfail*","Test if some HOUT pin is in the fault state",
203     "### Command syntax ###\n"
204     "\n"
205     "    houtfail<PIN>\n"
206     "where `<PIN>` is in range 1-6\n"
207     "\n"
208     "### Description ###\n"
209     "\n"
210     "This command tests, if HOUT pin is in a good condition. When the\n"
211     "circuit controlling HOUT pin detects some failure, it signals that on\n"
212     "HOUT_DIAG output. This command is supposed to read this output and\n"
213     "print its state.\n"
214     "\n"
215     "Possible outputs of this command:\n"
216     "\n"
217     "- OK - no failure detected\n"
218     "- FAIL - a failure detected\n"
219     "- NOT RUNNING - PWM was set set up and started\n"
220     "\n"
221     "Note: Before using this command, houtpwmstart and houtpwm commands\n"
222     "should be called.\n"
223     "\n"
224     "### Example ###\n"
225     "\n"
226     "    --> houtpwm6 1000 25\n"
227     "    --> houtpwmstart6\n"
228     "    --> houtfail6\n"
229     "    OK\n"
230     "\n"
231     "Detects the state of the HOUT1 and prints OK, FAIL or NOT RUNNING.\n",
232     CMD_HANDLER(cmd_do_test_hout_fault), (void *)&cmd_list_hout
233 };
234
235 /** Command descriptor for hout read IFBK command */
236 cmd_des_t const cmd_des_read_hifbk={
237     0, 0,
238     "houtifbk","Read values from HOUT current feedback",
239     "### Command syntax ###\n"
240     "\n"
241     "    houtifbk\n"
242     "\n"
243     "### Description ###\n"
244     "\n"
245     "The command reads analog values from HOUT_IFBK pins and prints\n"
246     "them in a table.\n"
247     "\n"
248     "### Example ###\n"
249     "\n"
250     "  --> houtifbk\n"
251     "  HOUT1: 0\n"
252     "  HOUT2: 134223784\n"
253     "  HOUT3: 134223784\n"
254     "  HOUT4: 0\n"
255     "  HOUT5: 38924\n"
256     "  HOUT6: 1342231444\n",
257    CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
258 };
259
260 /** Command descriptor for HOUT set PWM command */
261 cmd_des_t const cmd_des_hout_pwm={
262     0, 0,
263     "houtpwm*","Set or get actual PWM parameters",
264     "### Command syntax ###\n"
265     "\n"
266     "    houtpwm<PIN> <PER> <DUTY>\n"
267     "    houtpwm<PIN>\n"
268     "where\n"
269     "\n"
270     "- `<PIN>` is a number in range 1-6\n"
271     "- `<PER>` is a length of the PWM period in microseconds\n"
272     "- `<DUTY>` is a the PWM duty cycle in percent (0-100)\n"
273     "\n"
274     "### Description ###\n"
275     "\n"
276     "This command can be used to set or get HOUT PWM parameters.\n"
277     "\n"
278     "### Example ###\n"
279     "\n"
280     "    --> houtpwm1 1000 25\n"
281     "\n"
282     "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
283     "this period.\n"
284     "\n"
285     "    --> houtpwm1\n"
286     "    houtpwm1_period=1000\n"
287     "    houtpwm1_duty=25\n"
288     "\n"
289     "Prints the actual period of HOUT1 PWM in microseconds and the duty\n"
290     "cycle in percents.\n",
291     CMD_HANDLER(cmd_do_hout_pwm), (void *)&cmd_list_hout
292 };
293
294 /** Command descriptor for PWM start command */
295 cmd_des_t const cmd_des_hout_pwm_start={
296     0, 0,
297     "houtstartpwm*","Start generating PWM signal on HOUT",
298     "### Command syntax ###\n"
299     "\n"
300     "    houtstartpwm<PIN>\n"
301     "where `<PIN>` is a number in range 1-6\n"
302     "\n"
303     "### Description ###\n"
304     "\n"
305     "This command starts to generate the PWM signal on the specified HOUT\n"
306     "pin. The HOUT PWM has to be previously set by the houtpwm command,\n"
307     "otherwise an error is printed.\n"
308     "\n"
309     "### Example ###\n"
310     "\n"
311     "    --> houtpwm1 1000 25\n"
312     "    --> houtstartpwm1\n"
313     "\n"
314     "HOUT1 PWM generation will be started.\n",
315     CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
316 };
317
318 /** Command descriptor for PWM stop command */
319 cmd_des_t const cmd_des_hout_pwm_stop={
320     0, 0,
321     "houtstoppwm*","Stop generating of PWM signal on HOUT",
322     "### Command syntax ###\n"
323     "\n"
324     "    houtstoppwm<PIN>\n"
325     "where `<PIN>` is a number in range 1-6\n"
326     "\n"
327     "### Description ###\n"
328     "\n"
329     "This command stops generating the PWM signal on the selected pin.\n"
330     "\n"
331     "### Example ###\n"
332     "\n"
333     "    --> houtstoppwm1\n"
334     "\n"
335     "HOUT1 PWM generation will be deactivated.\n",
336     CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
337 };
338
339 /** List of commands for hout, defined as external */
340 cmd_des_t const *cmd_list_hout[]={
341   &cmd_des_test_hout_fail,
342   &cmd_des_read_hifbk,
343   &cmd_des_hout_pwm,
344   &cmd_des_hout_pwm_start,
345   &cmd_des_hout_pwm_stop,
346   NULL
347 };