2 * Copyright (C) 2012-2013, 2015, 2016 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
17 * This file contains commands for HOUT control.
27 /* FIXME: remove hout.h */
29 #include "drv/port.h" /* TODO: Use rpp/port.h */
30 #include "cmdproc_utils.h"
34 * @brief Reads values from HOUT_IFBK pins (subset of ADC)
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
41 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
44 const struct port_desc *port = &port_desc[PORT_ID_HOUTIFBK];
48 assert(ARRAY_SIZE(values) == port->numchn);
50 ret = port->get(port, values, sizeof(values));
54 for (i = 0; i < port->numchn; i++) {
55 rpp_sci_printf("HOUT%d: %d\n", i+1, values[i]);
61 * @brief Runs test of selected HOUT pin.
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
68 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
74 if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
75 return -CMDERR_BADPAR;
77 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
79 switch (hout_fail(pin)) {
84 rpp_sci_printf("FAIL");
87 rpp_sci_printf("NOT RUNNING");
90 rpp_sci_printf("Bad pin selected");
98 * @brief Set or get actual pwm parameters
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
105 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
108 uint32_t values[MAX_PARAM_VALUES_NUM];
113 if (sscanf(p, "%d", &pin) != 1)
114 return -CMDERR_BADPAR;
116 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
118 if (param[2] != NULL) { // More parameters = set values
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]);
126 else { // No more parameters = get values
127 double period = hout_pwm_get_period(pin);
128 uint32_t duty = hout_pwm_get_duty(pin);
130 rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%\n", pin+1, period, pin+1, duty);
136 * @brief Start PWM, if it was previously set by houtpwm command
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
143 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
149 if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
150 return -CMDERR_BADPAR;
152 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
153 int ret = hout_pwm_start(pin);
155 rpp_sci_printf("PWM was not initialized.\r\n");
156 return -CMDERR_BADCFG;
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
170 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
176 if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
177 return -CMDERR_BADPAR;
179 if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
186 /** Command descriptor for test hout fault state command */
187 cmd_des_t const cmd_des_test_hout_fail = {
189 "houtfail*","Test if some HOUT pin is in the fault state",
190 "### Command syntax ###\n"
193 "where `<PIN>` is in range 1-6\n"
195 "### Description ###\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"
202 "Possible outputs of this command:\n"
204 "- OK - no failure detected\n"
205 "- FAIL - a failure detected\n"
206 "- NOT RUNNING - PWM was set set up and started\n"
208 "Note: Before using this command, houtpwmstart and houtpwm commands\n"
209 "should be called.\n"
213 " --> houtpwm6 1000 25\n"
214 " --> houtpwmstart6\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
222 /** Command descriptor for hout read IFBK command */
223 cmd_des_t const cmd_des_read_hifbk = {
225 "houtifbk","Read values from HOUT current feedback",
226 "### Command syntax ###\n"
230 "### Description ###\n"
232 "The command reads analog values from HOUT_IFBK pins and prints\n"
239 " HOUT2: 134223784\n"
240 " HOUT3: 134223784\n"
243 " HOUT6: 1342231444\n",
244 CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
247 /** Command descriptor for HOUT set PWM command */
248 cmd_des_t const cmd_des_hout_pwm = {
250 "houtpwm*","Set or get actual PWM parameters",
251 "### Command syntax ###\n"
253 " houtpwm<PIN> <PER> <DUTY>\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"
261 "### Description ###\n"
263 "This command can be used to set or get HOUT PWM parameters.\n"
267 " --> houtpwm1 1000 25\n"
269 "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
273 " houtpwm1_period=1000\n"
274 " houtpwm1_duty=25%\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
281 /** Command descriptor for PWM start command */
282 cmd_des_t const cmd_des_hout_pwm_start = {
284 "houtstartpwm*","Start generating PWM signal on HOUT",
285 "### Command syntax ###\n"
287 " houtstartpwm<PIN>\n"
288 "where `<PIN>` is a number in range 1-6\n"
290 "### Description ###\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"
298 " --> houtpwm1 1000 25\n"
299 " --> houtstartpwm1\n"
301 "HOUT1 PWM generation will be started.\n",
302 CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
305 /** Command descriptor for PWM stop command */
306 cmd_des_t const cmd_des_hout_pwm_stop = {
308 "houtstoppwm*","Stop generating of PWM signal on HOUT",
309 "### Command syntax ###\n"
311 " houtstoppwm<PIN>\n"
312 "where `<PIN>` is a number in range 1-6\n"
314 "### Description ###\n"
316 "This command stops generating the PWM signal on the selected pin.\n"
320 " --> houtstoppwm1\n"
322 "HOUT1 PWM generation will be deactivated.\n",
323 CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
326 /** List of commands for hout, defined as external */
327 cmd_des_t const *cmd_list_hout[] = {
328 &cmd_des_test_hout_fail,
331 &cmd_des_hout_pwm_start,
332 &cmd_des_hout_pwm_stop,