]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_port.c
Update to the reworked gio and port in the rpp-lib
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_port.c
index 6cb8ab4441a8226f010c4e7387bfe10d686299e2..eb7c02ced97ea41a7a10a3c1be854af2ccfbfa93 100644 (file)
 #ifndef DOCGEN
 
 #include "rpp/rpp.h"
-#include "drv/digital_io_def.h"
+#include "drv/port.h"
+#ifdef TARGET_HAS_SPI
+#include "drv/spi.h"
+#endif
 #include "cmdproc_utils.h"
 
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
 
 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");
-       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 (port_interface == 3)
-                       portInterface = "GPIO";
-               else if (port_interface == 1)
-                       portInterface = "ADC";
-               else
-                       portInterface = "UNKNOWN";
-               rpp_sci_printf("%s, %s\r\n", port_names[i], portInterface);
+
+       for (i = 0; i < ARRAY_SIZE(port_desc); i++) {
+               const struct port_desc *port = &port_desc[i];
+               const char *type = "";
+#ifdef TARGET_HAS_SPI
+               if (port->set == port_spi_set)
+                       type = "SPI";
+#endif
+               rpp_sci_printf("%s %dx%db%s%s%s\r\n",
+                                          port->name, port->numchn, port->bpch,
+                                          *type ? " (": "",
+                                          type,
+                                          *type ? ")" : "");
        }
        return 1;
 }
 
+static const struct port_desc *port_from_name(const char *port_name)
+{
+       uint32_t i;
 
+       for (i = 0; i < ARRAY_SIZE(port_desc); i++)
+               if (strcmp(port_name, port_desc[i].name) == 0)
+                       return &port_desc[i];
+       return NULL;
+}
 
 /**
  * @brief      Read values from specified port
@@ -68,59 +76,108 @@ int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
  */
 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];
+       int i;
        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)
+       if (sscanf(param[1], "%31s ", portName) != 1)
                return -CMDERR_BADPAR;
 
-       int8_t port_interface = rpp_port_get_interface_type(portName);
-       if (port_interface == FAILURE) return -CMDERR_BADREG;
+       const struct port_desc *port = port_from_name(portName);
+       if (!port)
+               return -CMDERR_BADPAR;
 
        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 = port_pin_cnt/8+1;
-               else if (port_interface == 2)
-                       // Commands are passed as bytes
-                       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)
-                               break;
-                       token = strtok(NULL, " ");
-                       i++;
-               }
-
-               if (i != numParams || token != NULL)
-                       return -CMDERR_BADPAR;
-               ret = rpp_port_set_val(portName, port_pin_cnt, values);
-               if (ret == FAILURE) {
+               if (!port->set)
                        return -CMDERR_WRPERM;
-               }
 
-               return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
-       }
-       else {
-               ret = rpp_port_get_val(portName, port_pin_cnt, values);
-               if (ret == FAILURE) {
+               switch (port->bpch) {
+               case 1:
+               case 32:
+               {
+                       uint32_t value;
+                       ret = sscanf(param[2], "%i", &value);
+                       if (ret == EOF || ret == 0)
+                               return -CMDERR_BADPAR;
+
+                       ret = port->set(port, &value, sizeof(value));
+                       if (ret == FAILURE)
+                               return -CMDERR_WRPERM;
+                       return cmd_opchar_replong(cmd_io, param, value, 0, 16);
+               }
+               case 8: {
+                       uint8_t values[16] = {0};
+                       if (port->numchn > ARRAY_SIZE(values))
+                               return -CMDERR_BADCFG;
+                       for (i = 0; i < port->numchn; i++) {
+                               ret = sscanf(param[2+i], "%hhi", &values[i]);
+                               if (ret == EOF || ret == 0)
+                                       break;
+                       }
+                       ret = port->set(port, &values, sizeof(values));
+                       if (ret == FAILURE)
+                               return -CMDERR_WRPERM;
+                       return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
+               }
+               case 16: {
+                       uint16_t values[16] = {0};
+                       if (port->numchn > ARRAY_SIZE(values))
+                               return -CMDERR_BADCFG;
+                       for (i = 0; i < port->numchn; i++) {
+                               ret = sscanf(param[2+i], "%hi", &values[i]);
+                               if (ret == EOF || ret == 0)
+                                       break;
+                       }
+                       ret = port->set(port, &values, sizeof(values));
+                       if (ret == FAILURE)
+                               return -CMDERR_WRPERM;
+                       return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
+               }
+               default:
+                       rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
+                       return -CMDERR_NODEV;
+               }
+       } else { /* Get values from port */
+               if (!port->get)
                        return -CMDERR_RDPERM;
+
+               switch (port->bpch) {
+               case 1: {
+                       uint32_t value = 0;
+                       ret = port->get(port, &value, sizeof(value));
+                       if (ret == FAILURE)
+                               return -CMDERR_RDPERM;
+                       rpp_sci_printf("0x%x\n", value);
+                       break;
+               }
+               case 8: {
+                       uint8_t values[16] = {0};
+                       if (port->numchn > ARRAY_SIZE(values))
+                               return -CMDERR_BADCFG;
+                       ret = port->get(port, &values, sizeof(values));
+                       if (ret == FAILURE)
+                               return -CMDERR_RDPERM;
+                       for (i = 0; i < port->numchn; i++)
+                               rpp_sci_printf(" %hhd", values[i]);
+                       rpp_sci_printf("\n");
+                       break;
                }
-               for (i = 0; i < port_pin_cnt; i++) {
-                       rpp_sci_printf("%d\r\n", values[i]);
+               case 16: {
+                       uint16_t values[16] = {0};
+                       if (port->numchn > ARRAY_SIZE(values))
+                               return -CMDERR_BADCFG;
+                       ret = port->get(port, &values, sizeof(values));
+                       if (ret == FAILURE)
+                               return -CMDERR_RDPERM;
+                       for (i = 0; i < port->numchn; i++)
+                               rpp_sci_printf(" %hd", values[i]);
+                       rpp_sci_printf("\n");
+                       break;
+               }
+               default:
+                       rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
+                       return -CMDERR_NODEV;
                }
-               rpp_sci_printf("portval%s=%x\n", portName, ret);
                return 0;
        }
 }
@@ -133,27 +190,24 @@ cmd_des_t const cmd_des_port_val = {
        "portval*","Read or write values from or to the port",
        "### Command syntax ###\n"
        "\n"
-       "     portval<NAME> <VAL>\n"
+       "     portval<NAME> <VAL> [<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"
+       "- `<VAL>` is decimal, hexdecimal (0x) or octal (0) number\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"
+       "For digital IO ports, the least significant bit of the VAL corresponds\n"
+       "to the first pin, the second bit to the second pin, etc. The command\n"
+       "returns zero. When reading from the port, the command returns values\n"
+       "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"
+       "If the port represents an SPI interface of the MCU, then it is write\n"
+       "only and the argument is interpreted as a command for the port\n"
+       "controller. The command returns the response from the port controller.\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"
@@ -163,16 +217,10 @@ cmd_des_t const cmd_des_port_val = {
        "\n"
        "### Example ###\n"
        "\n"
-       "     --> portvalGIOB 3A\n"
+       "     --> portvalGIOB 0x3A\n"
        "     portvalGIOB=0\n"
        "     --> portvalGIOB\n"
-       "     0\n"
-       "     1\n"
-       "     0\n"
-       "     1\n"
-       "     1\n"
-       "     1\n"
-       "\n"
+       "        0x3a\n"
        "This pair of commands sets:\nGIOB"
        "GIOB=0\n"
        "GIOB=1\n"