]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/commitdiff
Rework portval command
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 7 Aug 2015 10:03:33 +0000 (12:03 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 7 Aug 2015 18:02:31 +0000 (20:02 +0200)
- Show R/W status
- Remove support for (currently) non-existent bits-per-channel values
- Responses are always prefixed by the original command

rpp-test-sw/commands/cmd_port.c

index eb7c02ced97ea41a7a10a3c1be854af2ccfbfa93..e20d83502fff92100ab974806bfcdaa795ce969c 100644 (file)
@@ -42,13 +42,16 @@ int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 
        for (i = 0; i < ARRAY_SIZE(port_desc); i++) {
                const struct port_desc *port = &port_desc[i];
+               const char *rw[4] = { "N/A", "WO", "RO", "RW" };
                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,
+               rpp_sci_printf("%-10s %s %dx%db%s%s%s\r\n",
+                                          port->name,
+                                          rw[(port->set ? 1 : 0) | (port->get ? 2 : 0)],
+                                          port->numchn, port->bpch,
                                           *type ? " (": "",
                                           type,
                                           *type ? ")" : "");
@@ -92,9 +95,7 @@ int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
                        return -CMDERR_WRPERM;
 
                switch (port->bpch) {
-               case 1:
-               case 32:
-               {
+               case 1: {
                        uint32_t value;
                        ret = sscanf(param[2], "%i", &value);
                        if (ret == EOF || ret == 0)
@@ -105,33 +106,43 @@ int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
                                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));
+               case 16: {
+                       uint16_t value;
+                       ret = sscanf(param[2], "%hi", &value);
+                       if (ret == EOF || ret == 0)
+                               break;
+                       ret = port->set(port, &value, sizeof(value));
                        if (ret == FAILURE)
                                return -CMDERR_WRPERM;
-                       return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
+                       return cmd_opchar_replong(cmd_io, param, value, 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));
+               case 24: {
+                       uint32_t value;
+                       ret = sscanf(param[2], "%i", &value);
+                       if (ret == EOF || ret == 0 || (value & 0xff000000))
+                               return -CMDERR_BADPAR;
+
+                       char v[3] = { (value >> 16) & 0xff, (value >> 8) & 0xff, (value >> 0) & 0xff };
+
+                       ret = port->set(port, &v, sizeof(v));
                        if (ret == FAILURE)
                                return -CMDERR_WRPERM;
-                       return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
+                       value = v[0] << 16 | v[1] << 8 | v[2];
+                       return cmd_opchar_replong(cmd_io, param, value, 0, 16);
+               }
+               case 32: {
+                       uint32_t value;
+                       ret = sscanf(param[2], "%i", &value);
+                       if (ret == EOF || ret == 0)
+                               return -CMDERR_BADPAR;
+
+                       char v[4] = { (value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, (value >> 0) & 0xff };
+
+                       ret = port->set(port, &v, sizeof(v));
+                       if (ret == FAILURE)
+                               return -CMDERR_WRPERM;
+                       value = v[0] << 24 | v[1] << 16 | v[2] << 8 | v[3];
+                       return cmd_opchar_replong(cmd_io, param, value, 0, 16);
                }
                default:
                        rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
@@ -141,26 +152,16 @@ int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
                if (!port->get)
                        return -CMDERR_RDPERM;
 
+               /* Workaround for cmd_opchar_replong() to work */
+               param[2] = param[0] + strlen(param[0]);
+
                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;
+                       return cmd_opchar_replong(cmd_io, param, value, 0, 16);
                }
                case 16: {
                        uint16_t values[16] = {0};
@@ -169,17 +170,23 @@ int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
                        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;
+
+                       if (port->numchn == 1)
+                               return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
+                       else {
+                               rpp_sci_printf("%*s=", param[2] - param[0], param[0]);
+                               for (i = 0; i < port->numchn; i++)
+                                       rpp_sci_printf(" %hx", values[i]);
+                               rpp_sci_printf("\n");
+                               return 0;
+                       }
                }
                default:
                        rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
                        return -CMDERR_NODEV;
                }
-               return 0;
        }
+       return 0;
 }
 
 #endif  /* DOCGEN */
@@ -199,18 +206,23 @@ cmd_des_t const cmd_des_port_val = {
        "\n"
        "### Description ###\n"
        "\n"
-       "This command sets or gets values of all pins on the specified port.\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"
+       "This command sets (when VAL is present) or gets (without VAL) values\n"
+       "of all channels of the specified port. For digital IO ports (1 bit per\n"
+       "channel), the least significant bit of the VAL corresponds to the\n"
+       "first pin, the second bit to the second pin, etc.\n"
+       "\n"
+       "The set variant of the command returns a value that depends on the\n"
+       "port type. For digital IO, this is the value set, for SPI ports this\n"
+       "is the SPI response.\n"
+       "\n"
+       "The get variant returns the value read from the port.\n"
        "\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"
+       "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"
+       "If the port is represents the ADC interface of the MCU, it is read\n"
+       "only and returns values for each ADC pin.\n"
        "\n"
        "Port names and interface type can be obtained with the portlist\n"
        "command.\n"
@@ -218,9 +230,9 @@ cmd_des_t const cmd_des_port_val = {
        "### Example ###\n"
        "\n"
        "     --> portvalGIOB 0x3A\n"
-       "     portvalGIOB=0\n"
+       "     portvalGIOB=0x3a\n"
        "     --> portvalGIOB\n"
-       "        0x3a\n"
+       "     portvalGIOB=0x3a\n"
        "This pair of commands sets:\nGIOB"
        "GIOB=0\n"
        "GIOB=1\n"
@@ -243,19 +255,17 @@ cmd_des_t const cmd_des_port_list = {
        "### 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."
+       "portval command. Each line contains port name, read/write type, number\n"
+       "of channels and width of the channel in bits.\n"
        "\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",
+       "     GIOA      RW 8x1b\n"
+       "     GIOB      RW 8x1b\n"
+       "     NHET      RW 30x1b\n"
+       "     ADC       RO 16x12b\n",
        CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
 };