]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_port.c
Update library
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_port.c
index 12e273c3bc99f56929b8e0e445272986e0bbab24..6cb8ab4441a8226f010c4e7387bfe10d686299e2 100644 (file)
@@ -1,59 +1,57 @@
 /*
- * Copyright (C) 2012-2013 Czech Technical University in Prague
+ * Copyright (C) 2012-2015 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_port.c
  *
  * Abstract:
- *              Commands for port controlling
- *             - Printing list of available ports (not yet available)
- *             - Setting/getting port value*
+ *          Commands for port controlling
+ *          - Printing list of available ports (not yet available)
+ *          - Setting/getting port value*
  */
 
 #include "cmd_port.h"
 #include "stdio.h"
+#include "string.h"
 
 #ifndef DOCGEN
 
 #include "rpp/rpp.h"
-#include "hal/hal.h"
+#include "drv/digital_io_def.h"
 #include "cmdproc_utils.h"
 
 
-int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
+int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
        uint32_t i;
-       char* portInterface;
-       rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
-       const port_def_t* ports = hal_port_get_definitions();
+       char *portInterface;
 
-       for (i = 0; i < PORT_CNT; i++) {
-               if (ports[i].name == PIN_NAME_UNUSED) continue;
-               if (ports[i].desc->interfaceType == PORT_INTERFACE_SPI)
+       rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
+       uint32_t port_cnt = rpp_port_get_port_cnt();
+       const char* port_names[16];
+       rpp_port_get_port_names(port_names, port_cnt);
+
+       for (i = 0; i < port_cnt; i++) {
+               if (strcmp(port_names[i], DIO_PIN_NAME_UNUSED) == 0) continue;
+               int8_t port_interface = rpp_port_get_interface_type(port_names[i]);
+               if (port_interface == 2)
                        portInterface = "SPI";
-               else if (ports[i].desc->interfaceType == PORT_INTERFACE_GPIO)
+               else if (port_interface == 3)
                        portInterface = "GPIO";
-               else if (ports[i].desc->interfaceType == PORT_INTERFACE_ADC)
+               else if (port_interface == 1)
                        portInterface = "ADC";
                else
                        portInterface = "UNKNOWN";
-               rpp_sci_printf("%s, %s\r\n", ports[i].name, portInterface);
+               rpp_sci_printf("%s, %s\r\n", port_names[i], portInterface);
        }
        return 1;
 }
@@ -68,174 +66,154 @@ int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
  * @param[in]  param   Parameters of command
  * @return     0 when OK or error code
  */
-int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
-  char *p;
-  int i;
-  port_desc_t* desc;
-  uint32_t ret;
-  uint32_t values[MAX_PARAM_VALUES_NUM];
-  char portName[32];
-  char* token;
-  uint32_t numParams;
-
-  p = param[1];
-  if (sscanf(p, "%31s ", portName) != 1) {
-         return -CMDERR_BADPAR;
-  }
-
-  if((desc = hal_port_get_dsc(portName, -1))==NULL) return -CMDERR_BADREG;
-
-  if(param[2] != NULL){                // More parameters = set values
-       p=param[2];
-       if (desc->port_setfnc_ptr == NULL) {
-               return -CMDERR_WRPERM;
-       }
-       else {
-               if (desc->interfaceType == PORT_INTERFACE_GPIO) {
+int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       char *p;
+       int i;
+       int32_t ret;
+       uint32_t values[MAX_PARAM_VALUES_NUM];
+       char portName[32];
+       char *token;
+       uint32_t numParams;
+       int32_t port_pin_cnt = rpp_port_get_pin_cnt(portName);
+
+       p = param[1];
+       if (sscanf(p, "%31s ", portName) != 1)
+               return -CMDERR_BADPAR;
+
+       int8_t port_interface = rpp_port_get_interface_type(portName);
+       if (port_interface == FAILURE) return -CMDERR_BADREG;
+
+       if (param[2] != NULL) { // More parameters = set values
+               p = param[2];
+               if (port_interface == 3)
                        // Information about pin values are encoded as hexadecimal 8b value
-                       numParams = desc->numValues/8+1;
-               }
-               else if (desc->interfaceType == PORT_INTERFACE_SPI) {
+                       numParams = port_pin_cnt/8+1;
+               else if (port_interface == 2)
                        // Commands are passed as bytes
-                       numParams = desc->numValues;
-               }
-               else if (desc->interfaceType == PORT_INTERFACE_ADC) {
-                       return -CMDERR_BADPAR;  // ADC is read only and no other port is supported
-               }
+                       numParams = port_pin_cnt;
+               else if (port_interface == 1)
+                       return -CMDERR_BADPAR;  // ADC is read only and no other port is supported
                token = strtok(p, " ");
                i = 0;
                while (i < numParams && token != NULL) {
-                       if (sscanf(token, "%x", &values[i]) == EOF) {
+                       if (sscanf(token, "%x", &values[i]) == EOF)
                                break;
-                       }
                        token = strtok(NULL, " ");
                        i++;
                }
 
-               if (i != numParams || token != NULL) {
+               if (i != numParams || token != NULL)
                        return -CMDERR_BADPAR;
+               ret = rpp_port_set_val(portName, port_pin_cnt, values);
+               if (ret == FAILURE) {
+                       return -CMDERR_WRPERM;
                }
-               ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
+
+               return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
        }
-       return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
-  }
-  else {
-         if (desc->port_getfnc_ptr == NULL) {
+       else {
+               ret = rpp_port_get_val(portName, port_pin_cnt, values);
+               if (ret == FAILURE) {
                        return -CMDERR_RDPERM;
-         }
-         else {
-                       ret = desc->port_getfnc_ptr(desc->config, desc->numValues, values);
-                       for (i = 0; i < desc->numValues; i++) {
-                               rpp_sci_printf("%d\r\n", values[i]);
-                       }
-         }
-               rpp_sci_printf("portval%s=%x", portName, ret);
+               }
+               for (i = 0; i < port_pin_cnt; i++) {
+                       rpp_sci_printf("%d\r\n", values[i]);
+               }
+               rpp_sci_printf("portval%s=%x\n", portName, ret);
                return 0;
-  }
+       }
 }
 
-#endif /* DOCGEN */
+#endif  /* DOCGEN */
 
 /** Command descriptor for read values from port command */
-cmd_des_t const cmd_des_port_val={
-    0, 0,
-    "portval*","Read or write values from or to the port",
-    "=== Command syntax ===\n"
-    "\n"
-    "   portval<NAME> <VAL>\n"
-    "   portval<NAME>\n"
-    "where\n"
-    "* <NAME> is a string specifying the name of the port\n"
-    "* <VAL> is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command sets or gets values of all pins on the specified port.\n"
-    "If the port is connected to the GPIO interface of the MCU, then\n"
-    "when writing the value, the lowest significant bit of the argument\n"
-    "is assigned to the first pin, the second bit is assigned to the\n"
-    "second pin, etc. The command returns zero.\n"
-    "When reading from the port, the command returns values for each pin.\n"
-    "\n"
-    "If the port is connected to the SPI interface of the MCU, then\n"
-    "it is write only and the argument is interpreted as a command for\n"
-    "the port controller. The command returns the response from the\n"
-    "port controller.\n"
-    "For command examples please refer to the project wiki\n"
-    "\n"
-    "If the port is connected to the ADC interface of the MCU, then\n"
-    "it is read only and returns values for each ADC pin.\n"
-    "\n"
-    "Port names and interface type can be obtained with the portlist\n"
-    "command.\n"
-    "\n"
-    "NOTE: For successful communication with the HBR, HBR_EN pin must\n"
-    "be set first.\n"
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> portvalMOUTIN 3A\n"
-    "   portvalMOUTIN=0\n"
-    "   --> portvalMOUTIN\n"
-    "   0\n"
-    "   1\n"
-    "   0\n"
-    "   1\n"
-    "   1\n"
-    "   1\n"
-    "\n"
-    "This pair of commands sets:\nMOUT1IN"
-    "MOUT1IN=0\n"
-    "MOUT2IN=1\n"
-    "MOUT3IN=0\n"
-    "MOUT4IN=1\n"
-    "MOUT5IN=1\n"
-    "MOUT6IN=1\n"
-    "Which is shown in getter output\n",
-    CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
+cmd_des_t const cmd_des_port_val = {
+       0, 0,
+       "portval*","Read or write values from or to the port",
+       "### Command syntax ###\n"
+       "\n"
+       "     portval<NAME> <VAL>\n"
+       "     portval<NAME>\n"
+       "where\n"
+       "\n"
+       "- `<NAME>` is a string specifying the name of the port\n"
+       "- `<VAL>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command sets or gets values of all pins on the specified port.\n"
+       "If the port is connected to the GPIO interface of the MCU, then\n"
+       "when writing the value, the lowest significant bit of the argument\n"
+       "is assigned to the first pin, the second bit is assigned to the\n"
+       "second pin, etc. The command returns zero.\n"
+       "When reading from the port, the command returns values for each pin.\n"
+       "\n"
+       "If the port is connected to the SPI interface of the MCU, then\n"
+       "it is write only and the argument is interpreted as a command for\n"
+       "the port controller. The command returns the response from the\n"
+       "port controller.\n"
+       "For command examples please refer to the project wiki\n"
+       "\n"
+       "If the port is connected to the ADC interface of the MCU, then\n"
+       "it is read only and returns values for each ADC pin.\n"
+       "\n"
+       "Port names and interface type can be obtained with the portlist\n"
+       "command.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "     --> portvalGIOB 3A\n"
+       "     portvalGIOB=0\n"
+       "     --> portvalGIOB\n"
+       "     0\n"
+       "     1\n"
+       "     0\n"
+       "     1\n"
+       "     1\n"
+       "     1\n"
+       "\n"
+       "This pair of commands sets:\nGIOB"
+       "GIOB=0\n"
+       "GIOB=1\n"
+       "GIOB=0\n"
+       "GIOB=1\n"
+       "GIOB=1\n"
+       "GIOB=1\n"
+       "Which is shown in getter output\n",
+       CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
 };
 
 /** Command descriptor for port list printout */
-cmd_des_t const cmd_des_port_list={
-    0, 0,
-    "portlist","Print a list of all port names",
-    "=== Command syntax ===\n"
-    "\n"
-    "   portlist\n"
-    "\n"
-    "=== Description ===\n"
-    "\n"
-    "This command prints the list of all defined ports accessible via the\n"
-    "portval command. Each record of the list is a couple of\n"
-    "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
-    "The type of the MCU<->port interface slightly modifies the meaning\n"
-    "of the portval command."
-    "\n"
-    "=== Example ===\n"
-    "\n"
-    "   --> portlist\n"
-    "   List of all defined ports with its type. Those names can be used by portval command.\n"
-    "   DINMCU, GPIO\n"
-    "   DINSPI, SPI\n"
-    "   HOUTDIAG, GPIO\n"
-    "   HOUTIN, GPIO\n"
-    "   HOUTIFBK, ADC\n"
-    "   ADC, ADC\n"
-    "   LOUT, SPI\n"
-    "   DAC12, SPI\n"
-    "   DAC34, SPI\n"
-    "   DACDREF, SPI\n"
-    "   HBR, SPI\n"
-    "   FRAY1, SPI\n"
-    "   FRAY2, SPI\n"
-    "   MOUTEN, GPIO\n"
-    "   MOUTIN, GPIO\n",
-    CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
+cmd_des_t const cmd_des_port_list = {
+       0, 0,
+       "portlist","Print a list of all port names",
+       "### Command syntax ###\n"
+       "\n"
+       "     portlist\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command prints the list of all defined ports accessible via the\n"
+       "portval command. Each record of the list is a couple of\n"
+       "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
+       "The type of the MCU<->port interface slightly modifies the meaning\n"
+       "of the portval command."
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "     --> portlist\n"
+       "     List of all defined ports with its type. Those names can be used by portval command.\n"
+       "     GIOA, GPIO\n"
+       "     GIOB, GPIO\n"
+       "     NHET1, GPIO\n"
+       "     ADC, ADC\n",
+       CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
 };
 
 /** List of commands for port, defined as external */
-cmd_des_t const *cmd_list_port[]={
-  &cmd_des_port_val,
-  &cmd_des_port_list,
-  NULL
+cmd_des_t const *cmd_list_port[] = {
+       &cmd_des_port_val,
+       &cmd_des_port_list,
+       NULL
 };