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;
85 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
87 switch (hout_fail(pin)) {
92 rpp_sci_printf("FAIL");
95 rpp_sci_printf("NOT RUNNING");
98 rpp_sci_printf("Bad pin selected");
101 rpp_sci_printf("\n");
106 * @brief Set or get actual pwm parameters
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
113 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
116 uint32_t values[MAX_PARAM_VALUES_NUM];
121 if (sscanf(p, "%d", &pin) != 1) {
122 return -CMDERR_BADPAR;
125 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
127 if(param[2] != NULL){ // More parameters = set values
129 if (sscanf(p, "%d %d %1s", &values[0], &values[1], &spareParams) != 2) {
130 return -CMDERR_BADPAR;
132 if (values[1] > 100) return -CMDERR_BADPAR;
133 hout_pwm_set_signal(pin, (double)values[0], values[1]);
136 else{ // No more parameters = get values
137 double period = hout_pwm_get_period(pin);
138 uint32_t duty = hout_pwm_get_duty(pin);
140 rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%", pin+1, period, pin+1, duty);
146 * @brief Start PWM, if it was previously set by houtpwm command
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
153 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
159 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
160 return -CMDERR_BADPAR;
163 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
164 int ret = hout_pwm_start(pin);
166 rpp_sci_printf("PWM was not initialized.\r\n");
167 return -CMDERR_BADCFG;
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
182 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
188 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
189 return -CMDERR_BADPAR;
192 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
199 /** Command descriptor for test hout fault state command */
200 cmd_des_t const cmd_des_test_hout_fail = {
202 "houtfail*","Test if some HOUT pin is in the fault state",
203 "=== Command syntax ===\n"
206 "where <PIN> is in range 1-6\n"
208 "=== Description ===\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"
215 "Possible outputs of this command:\n"
216 "* OK - no failure detected\n"
217 "* FAIL - a failure detected\n"
218 "* NOT RUNNING - PWM was set set up and started\n"
220 "Note: Before using this command, houtpwmstart and houtpwm commands\n"
221 "should be called.\n"
225 " --> houtpwm6 1000 25\n"
226 " --> houtpwmstart6\n"
230 "Detects the state of the HOUT1 and prints OK, FAIL or NOT RUNNING.\n",
231 CMD_HANDLER(cmd_do_test_hout_fault), (void *)&cmd_list_hout
234 /** Command descriptor for hout read IFBK command */
235 cmd_des_t const cmd_des_read_hifbk={
237 "houtifbk","Read values from HOUT current feedback",
238 "=== Command syntax ===\n"
242 "=== Description ===\n"
244 "The command reads analog values from HOUT_IFBK pins and prints\n"
251 " HOUT2: 134223784\n"
252 " HOUT3: 134223784\n"
255 " HOUT6: 1342231444\n",
256 CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
259 /** Command descriptor for HOUT set PWM command */
260 cmd_des_t const cmd_des_hout_pwm={
262 "houtpwm*","Set or get actual PWM parameters",
263 "=== Command syntax ===\n"
265 " houtpwm<PIN> <PER> <DUTY>\n"
268 "* <PIN> is a number in range 1-6\n"
269 "* <PER> is a length of the PWM period in microseconds\n"
270 "* <DUTY> is a the PWM duty cycle in percent (0-100)\n"
272 "=== Description ===\n"
274 "This command can be used to set or get HOUT PWM parameters.\n"
278 " --> houtpwm1 1000 25\n"
280 "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
284 " houtpwm1_period=1000\n"
285 " houtpwm1_duty=25\n"
287 "Prints the actual period of HOUT1 PWM in microseconds and the duty\n"
288 "cycle in percents.\n",
289 CMD_HANDLER(cmd_do_hout_pwm), (void *)&cmd_list_hout
292 /** Command descriptor for PWM start command */
293 cmd_des_t const cmd_des_hout_pwm_start={
295 "houtstartpwm*","Start generating PWM signal on HOUT",
296 "=== Command syntax ===\n"
298 " houtstartpwm<PIN>\n"
299 "where <PIN> is a number in range 1-6\n"
301 "=== Description ===\n"
303 "This command starts to generate the PWM signal on the specified HOUT\n"
304 "pin. The HOUT PWM has to be previously set by the houtpwm command,\n"
305 "otherwise an error is printed.\n"
309 " --> houtpwm1 1000 25\n"
310 " --> houtstartpwm1\n"
312 "HOUT1 PWM generation will be started.\n",
313 CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
316 /** Command descriptor for PWM stop command */
317 cmd_des_t const cmd_des_hout_pwm_stop={
319 "houtstoppwm*","Stop generating of PWM signal on HOUT",
320 "=== Command syntax ===\n"
322 " houtstoppwm<PIN>\n"
323 "where <PIN> is a number in range 1-6\n"
325 "=== Description ===\n"
327 "This command stops generating the PWM signal on the selected pin.\n"
331 " --> houtstoppwm1\n"
333 "HOUT1 PWM generation will be deactivated.\n",
334 CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
337 /** List of commands for hout, defined as external */
338 cmd_des_t const *cmd_list_hout[]={
339 &cmd_des_test_hout_fail,
342 &cmd_des_hout_pwm_start,
343 &cmd_des_hout_pwm_stop,