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