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