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