]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_hout.c
hout: Do not use variably sized local array
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_hout.c
index e5eafbe8f9219722b04b01723a00b4ee25de3750..9a37e9d02f4cb71a3cd941e4f90be65774f52f2d 100644 (file)
@@ -1,28 +1,20 @@
 /*
- * Copyright (C) 2012-2013 Czech Technical University in Prague
+ * Copyright (C) 2012-2013, 2015, 2016 Czech Technical University in Prague
  *
  * Created on: 28.2.2013
  *
  * Authors:
  *     - Michal Horn
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
  *
  * File : cmd_hout.c
  *
  * Abstract:
- *     This file contains commands for HOUT control.
+ *      This file contains commands for HOUT control.
  *
  */
 
@@ -32,8 +24,9 @@
 #ifndef DOCGEN
 
 #include "rpp/rpp.h"
-#include "hal/hal.h"
-#include "drv/drv.h"
+/* FIXME: remove hout.h */
+#include "drv/hout.h"
+#include "drv/port.h"                  /* TODO: Use rpp/port.h */
 #include "cmdproc_utils.h"
 
 
  */
 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
-    uint32_t i;                                                // Cycle control variable
-    port_desc_t* desc;
-    uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
-    int ret;
+       uint32_t i;
+       const struct port_desc *port = &port_desc[PORT_ID_HOUTIFBK];
+       uint16_t values[6];
+       int ret;
 
-    desc = hal_port_get_dsc(PORT_NAME_HOUTIFBK, -1);
+       assert(ARRAY_SIZE(values) == port->numchn);
 
-    ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
-    if (ret < 0)
-       return ret;
+       ret = port->get(port, values, sizeof(values));
+       if (ret < 0)
+               return ret;
 
-    for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
-       rpp_sci_printf("HOUT%d: %d\n", i+1, values[i]);
-    }
-    return 0;
+       for (i = 0; i < port->numchn; i++) {
+               rpp_sci_printf("HOUT%d: %d\n", i+1, values[i]);
+       }
+       return 0;
 }
 
 /**
@@ -75,12 +68,11 @@ int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, ch
 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
        int pin;
-       char *p=param[1];
-       char spareParams;
+       char *p = param[1];
+       char spareParams[2];
 
-       if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
+       if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
                return -CMDERR_BADPAR;
-       }
        pin--;
        if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
 
@@ -103,7 +95,7 @@ int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *pa
 }
 
 /**
- *     @brief  Set or get actual pwm parameters
+ *  @brief     Set or get actual pwm parameters
  *
  * @param[in]  cmd_io  Pointer to IO stack
  * @param[in]  des             Pointer to command descriptor
@@ -112,38 +104,36 @@ int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *pa
  */
 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
-         char *p;
-         uint32_t values[MAX_PARAM_VALUES_NUM];
-         char spareParams;
-         int pin;
+       char *p;
+       uint32_t values[MAX_PARAM_VALUES_NUM];
+       char spareParams[2];
+       int pin;
 
-         p=param[1];
-         if (sscanf(p, "%d", &pin) != 1) {
-                 return -CMDERR_BADPAR;
-         }
-         pin--;
-         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
+       p = param[1];
+       if (sscanf(p, "%d", &pin) != 1)
+               return -CMDERR_BADPAR;
+       pin--;
+       if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
 
-         if(param[2] != NULL){         // More parameters = set values
-           p=param[2];
-           if (sscanf(p, "%d %d %1s", &values[0], &values[1], &spareParams) != 2) {
-               return -CMDERR_BADPAR;
-           }
+       if (param[2] != NULL) {     // More parameters = set values
+               p = param[2];
+               if (sscanf(p, "%d %d %1s", &values[0], &values[1], spareParams) != 2)
+                       return -CMDERR_BADPAR;
                if (values[1] > 100) return -CMDERR_BADPAR;
                hout_pwm_set_signal(pin, (double)values[0], values[1]);
-           return 0;
-         }
-         else{ // No more parameters = get values
+               return 0;
+       }
+       else {  // No more parameters = get values
                double period = hout_pwm_get_period(pin);
                uint32_t duty = hout_pwm_get_duty(pin);
 
-               rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%", pin+1, period, pin+1, duty);
+               rpp_sci_printf("houtpwm%d_period=%g\r\nhoutpwm%d_duty=%u%%\n", pin+1, period, pin+1, duty);
                return 0;
-         }
+       }
 }
 
 /**
- *     @brief  Start PWM, if it was previously set by houtpwm command
+ *  @brief     Start PWM, if it was previously set by houtpwm command
  *
  * @param[in]  cmd_io  Pointer to IO stack
  * @param[in]  des             Pointer to command descriptor
@@ -153,12 +143,11 @@ int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
        int pin;
-       char *p=param[1];
-       char spareParams;
+       char *p = param[1];
+       char spareParams[2];
 
-       if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
+       if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
                return -CMDERR_BADPAR;
-       }
        pin--;
        if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
        int ret = hout_pwm_start(pin);
@@ -166,13 +155,12 @@ int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *par
                rpp_sci_printf("PWM was not initialized.\r\n");
                return -CMDERR_BADCFG;
        }
-       else {
+       else
                return 0;
-       }
 }
 
 /**
- *     @brief  Stop PWM
+ *  @brief     Stop PWM
  *
  * @param[in]  cmd_io  Pointer to IO stack
  * @param[in]  des             Pointer to command descriptor
@@ -182,164 +170,165 @@ int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *par
 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
        int pin;
-       char *p=param[1];
-       char spareParams;
+       char *p = param[1];
+       char spareParams[2];
 
-       if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
+       if (sscanf(p, "%d %1s", &pin, spareParams) != 1)
                return -CMDERR_BADPAR;
-       }
        pin--;
        if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
        hout_pwm_stop(pin);
        return 0;
 }
 
-#endif /* DOCGEN */
+#endif  /* DOCGEN */
 
 /** Command descriptor for test hout fault state command */
 cmd_des_t const cmd_des_test_hout_fail = {
-    0, 0,
-    "houtfail*","Test if some HOUT pin is in the fault state",
-    "=== Command syntax ===\n"
-    "\n"
-    "   houtfail<PIN>\n"
-    "where <PIN> is in range 1-6\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command tests, if HOUT pin is in a good condition. When the\n"
-    "circuit controlling HOUT pin detects some failure, it signals that on\n"
-    "HOUT_DIAG output. This command is supposed to read this output and\n"
-    "print its state.\n"
-    "\n"
-    "Possible outputs of this command:\n"
-    "* OK - no failure detected\n"
-    "* FAIL - a failure detected\n"
-    "* NOT RUNNING - PWM was set set up and started\n"
-    "\n"
-    "Note: Before using this command, houtpwmstart and houtpwm commands\n"
-    "should be called.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> houtpwm6 1000 25\n"
-    "   --> houtpwmstart6\n"
-    "   --> houtfail6\n"
-    "   OK\n"
-    "\n"
-    "Detects the state of the HOUT1 and prints OK, FAIL or NOT RUNNING.\n",
-    CMD_HANDLER(cmd_do_test_hout_fault), (void *)&cmd_list_hout
+       0, 0,
+       "houtfail*","Test if some HOUT pin is in the fault state",
+       "### Command syntax ###\n"
+       "\n"
+       "    houtfail<PIN>\n"
+       "where `<PIN>` is in range 1-6\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command tests, if HOUT pin is in a good condition. When the\n"
+       "circuit controlling HOUT pin detects some failure, it signals that on\n"
+       "HOUT_DIAG output. This command is supposed to read this output and\n"
+       "print its state.\n"
+       "\n"
+       "Possible outputs of this command:\n"
+       "\n"
+       "- OK - no failure detected\n"
+       "- FAIL - a failure detected\n"
+       "- NOT RUNNING - PWM was set set up and started\n"
+       "\n"
+       "Note: Before using this command, houtpwmstart and houtpwm commands\n"
+       "should be called.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> houtpwm6 1000 25\n"
+       "    --> houtpwmstart6\n"
+       "    --> houtfail6\n"
+       "    OK\n"
+       "\n"
+       "Detects the state of the HOUT1 and prints OK, FAIL or NOT RUNNING.\n",
+       CMD_HANDLER(cmd_do_test_hout_fault), (void *)&cmd_list_hout
 };
 
 /** Command descriptor for hout read IFBK command */
-cmd_des_t const cmd_des_read_hifbk={
-    0, 0,
-    "houtifbk","Read values from HOUT current feedback",
-    "=== Command syntax ===\n"
-    "\n"
-    "   houtifbk\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "The command reads analog values from HOUT_IFBK pins and prints\n"
-    "them in a table.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "  --> houtifbk\n"
-    "  HOUT1: 0\n"
-    "  HOUT2: 134223784\n"
-    "  HOUT3: 134223784\n"
-    "  HOUT4: 0\n"
-    "  HOUT5: 38924\n"
-    "  HOUT6: 1342231444\n",
-   CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
+cmd_des_t const cmd_des_read_hifbk = {
+       0, 0,
+       "houtifbk","Read values from HOUT current feedback",
+       "### Command syntax ###\n"
+       "\n"
+       "    houtifbk\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "The command reads analog values from HOUT_IFBK pins and prints\n"
+       "them in a table.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "  --> houtifbk\n"
+       "  HOUT1: 0\n"
+       "  HOUT2: 134223784\n"
+       "  HOUT3: 134223784\n"
+       "  HOUT4: 0\n"
+       "  HOUT5: 38924\n"
+       "  HOUT6: 1342231444\n",
+       CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
 };
 
 /** Command descriptor for HOUT set PWM command */
-cmd_des_t const cmd_des_hout_pwm={
-    0, 0,
-    "houtpwm*","Set or get actual PWM parameters",
-    "=== Command syntax ===\n"
-    "\n"
-    "   houtpwm<PIN> <PER> <DUTY>\n"
-    "   houtpwm<PIN>\n"
-    "where\n"
-    "* <PIN> is a number in range 1-6\n"
-    "* <PER> is a length of the PWM period in microseconds\n"
-    "* <DUTY> is a the PWM duty cycle in percent (0-100)\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command can be used to set or get HOUT PWM parameters.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> houtpwm1 1000 25\n"
-    "\n"
-    "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
-    "this period.\n"
-    "\n"
-    "   --> houtpwm1\n"
-    "   houtpwm1_period=1000\n"
-    "   houtpwm1_duty=25\n"
-    "\n"
-    "Prints the actual period of HOUT1 PWM in microseconds and the duty\n"
-    "cycle in percents.\n",
-    CMD_HANDLER(cmd_do_hout_pwm), (void *)&cmd_list_hout
+cmd_des_t const cmd_des_hout_pwm = {
+       0, 0,
+       "houtpwm*","Set or get actual PWM parameters",
+       "### Command syntax ###\n"
+       "\n"
+       "    houtpwm<PIN> <PER> <DUTY>\n"
+       "    houtpwm<PIN>\n"
+       "where\n"
+       "\n"
+       "- `<PIN>` is a number in range 1-6\n"
+       "- `<PER>` is a length of the PWM period in microseconds\n"
+       "- `<DUTY>` is a the PWM duty cycle in percent (0-100)\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command can be used to set or get HOUT PWM parameters.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> houtpwm1 1000 25\n"
+       "\n"
+       "HOUT1 PWM will have the period of 1ms and will be active for 25% of\n"
+       "this period.\n"
+       "\n"
+       "    --> houtpwm1\n"
+       "    houtpwm1_period=1000\n"
+       "    houtpwm1_duty=25%\n"
+       "\n"
+       "Prints the actual period of HOUT1 PWM in microseconds and the duty\n"
+       "cycle in percents.\n",
+       CMD_HANDLER(cmd_do_hout_pwm), (void *)&cmd_list_hout
 };
 
 /** Command descriptor for PWM start command */
-cmd_des_t const cmd_des_hout_pwm_start={
-    0, 0,
-    "houtstartpwm*","Start generating PWM signal on HOUT",
-    "=== Command syntax ===\n"
-    "\n"
-    "   houtstartpwm<PIN>\n"
-    "where <PIN> is a number in range 1-6\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command starts to generate the PWM signal on the specified HOUT\n"
-    "pin. The HOUT PWM has to be previously set by the houtpwm command,\n"
-    "otherwise an error is printed.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> houtpwm1 1000 25\n"
-    "   --> houtstartpwm1\n"
-    "\n"
-    "HOUT1 PWM generation will be started.\n",
-    CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
+cmd_des_t const cmd_des_hout_pwm_start = {
+       0, 0,
+       "houtstartpwm*","Start generating PWM signal on HOUT",
+       "### Command syntax ###\n"
+       "\n"
+       "    houtstartpwm<PIN>\n"
+       "where `<PIN>` is a number in range 1-6\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command starts to generate the PWM signal on the specified HOUT\n"
+       "pin. The HOUT PWM has to be previously set by the houtpwm command,\n"
+       "otherwise an error is printed.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> houtpwm1 1000 25\n"
+       "    --> houtstartpwm1\n"
+       "\n"
+       "HOUT1 PWM generation will be started.\n",
+       CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
 };
 
 /** Command descriptor for PWM stop command */
-cmd_des_t const cmd_des_hout_pwm_stop={
-    0, 0,
-    "houtstoppwm*","Stop generating of PWM signal on HOUT",
-    "=== Command syntax ===\n"
-    "\n"
-    "   houtstoppwm<PIN>\n"
-    "where <PIN> is a number in range 1-6\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command stops generating the PWM signal on the selected pin.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> houtstoppwm1\n"
-    "\n"
-    "HOUT1 PWM generation will be deactivated.\n",
-    CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
+cmd_des_t const cmd_des_hout_pwm_stop = {
+       0, 0,
+       "houtstoppwm*","Stop generating of PWM signal on HOUT",
+       "### Command syntax ###\n"
+       "\n"
+       "    houtstoppwm<PIN>\n"
+       "where `<PIN>` is a number in range 1-6\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command stops generating the PWM signal on the selected pin.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> houtstoppwm1\n"
+       "\n"
+       "HOUT1 PWM generation will be deactivated.\n",
+       CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
 };
 
 /** List of commands for hout, defined as external */
-cmd_des_t const *cmd_list_hout[]={
-  &cmd_des_test_hout_fail,
-  &cmd_des_read_hifbk,
-  &cmd_des_hout_pwm,
-  &cmd_des_hout_pwm_start,
-  &cmd_des_hout_pwm_stop,
-  NULL
+cmd_des_t const *cmd_list_hout[] = {
+       &cmd_des_test_hout_fail,
+       &cmd_des_read_hifbk,
+       &cmd_des_hout_pwm,
+       &cmd_des_hout_pwm_start,
+       &cmd_des_hout_pwm_stop,
+       NULL
 };