2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
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.
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/>.
25 * This file contains commands for HOUT control.
37 #include "cmdproc_utils.h"
41 * @brief Reads values from HOUT_IFBK pins (subset of ADC)
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
48 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
50 uint32_t i; // Cycle control variable
52 uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
55 desc = hal_port_get_dsc(PORT_NAME_HOUTIFBK, -1);
57 ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
61 for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
62 rpp_sci_printf("HOUT%d: %d\n", i+1, values[i]);
68 * @brief Runs test of selected HOUT pin.
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
75 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
81 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
82 return -CMDERR_BADPAR;
84 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
86 switch (hout_fail(pin)) {
91 rpp_sci_printf("FAIL");
94 rpp_sci_printf("NOT RUNNING");
97 rpp_sci_printf("Bad pin selected");
100 rpp_sci_printf("\n");
105 * @brief Set or get actual pwm parameters
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
112 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
115 uint32_t values[MAX_PARAM_VALUES_NUM];
120 if (sscanf(p, "%d", &pin) != 1)
121 return -CMDERR_BADPAR;
123 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
125 if (param[2] != NULL) { // More parameters = set values
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]);
133 else { // No more parameters = get values
134 double period = hout_pwm_get_period(pin);
135 uint32_t duty = hout_pwm_get_duty(pin);
137 rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%", pin+1, period, pin+1, duty);
143 * @brief Start PWM, if it was previously set by houtpwm command
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
150 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
156 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
157 return -CMDERR_BADPAR;
159 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
160 int ret = hout_pwm_start(pin);
162 rpp_sci_printf("PWM was not initialized.\r\n");
163 return -CMDERR_BADCFG;
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
177 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
183 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
184 return -CMDERR_BADPAR;
186 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
193 /** Command descriptor for test hout fault state command */
194 cmd_des_t const cmd_des_test_hout_fail = {
196 "houtfail*","Test if some HOUT pin is in the fault state",
197 "### Command syntax ###\n"
200 "where `<PIN>` is in range 1-6\n"
202 "### Description ###\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"
209 "Possible outputs of this command:\n"
211 "- OK - no failure detected\n"
212 "- FAIL - a failure detected\n"
213 "- NOT RUNNING - PWM was set set up and started\n"
215 "Note: Before using this command, houtpwmstart and houtpwm commands\n"
216 "should be called.\n"
220 " --> houtpwm6 1000 25\n"
221 " --> houtpwmstart6\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
229 /** Command descriptor for hout read IFBK command */
230 cmd_des_t const cmd_des_read_hifbk = {
232 "houtifbk","Read values from HOUT current feedback",
233 "### Command syntax ###\n"
237 "### Description ###\n"
239 "The command reads analog values from HOUT_IFBK pins and prints\n"
246 " HOUT2: 134223784\n"
247 " HOUT3: 134223784\n"
250 " HOUT6: 1342231444\n",
251 CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void*)&cmd_list_hout
254 /** Command descriptor for HOUT set PWM command */
255 cmd_des_t const cmd_des_hout_pwm = {
257 "houtpwm*","Set or get actual PWM parameters",
258 "### Command syntax ###\n"
260 " houtpwm<PIN> <PER> <DUTY>\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"
268 "### Description ###\n"
270 "This command can be used to set or get HOUT PWM parameters.\n"
274 " --> houtpwm1 1000 25\n"
276 "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
280 " houtpwm1_period=1000\n"
281 " houtpwm1_duty=25\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
288 /** Command descriptor for PWM start command */
289 cmd_des_t const cmd_des_hout_pwm_start = {
291 "houtstartpwm*","Start generating PWM signal on HOUT",
292 "### Command syntax ###\n"
294 " houtstartpwm<PIN>\n"
295 "where `<PIN>` is a number in range 1-6\n"
297 "### Description ###\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"
305 " --> houtpwm1 1000 25\n"
306 " --> houtstartpwm1\n"
308 "HOUT1 PWM generation will be started.\n",
309 CMD_HANDLER(cmd_do_hout_pwm_start), (void*)&cmd_list_hout
312 /** Command descriptor for PWM stop command */
313 cmd_des_t const cmd_des_hout_pwm_stop = {
315 "houtstoppwm*","Stop generating of PWM signal on HOUT",
316 "### Command syntax ###\n"
318 " houtstoppwm<PIN>\n"
319 "where `<PIN>` is a number in range 1-6\n"
321 "### Description ###\n"
323 "This command stops generating the PWM signal on the selected pin.\n"
327 " --> houtstoppwm1\n"
329 "HOUT1 PWM generation will be deactivated.\n",
330 CMD_HANDLER(cmd_do_hout_pwm_stop), (void*)&cmd_list_hout
333 /** List of commands for hout, defined as external */
334 cmd_des_t const *cmd_list_hout[] = {
335 &cmd_des_test_hout_fail,
338 &cmd_des_hout_pwm_start,
339 &cmd_des_hout_pwm_stop,