2 * Copyright (C) 2012-2015 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 * Commands for port controlling
18 * - Printing list of available ports (not yet available)
19 * - Setting/getting port value*
33 #include "cmdproc_utils.h"
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
41 rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
43 for (i = 0; i < ARRAY_SIZE(port_desc); i++) {
44 const struct port_desc *port = &port_desc[i];
45 const char *rw[4] = { "N/A", "WO", "RO", "RW" };
48 if (port->set == port_spi_set)
49 snprintf(note, sizeof(note), "SPI, %s", port->cfg.spi.chip);
51 rpp_sci_printf("%-10s %s %dx%db%s%s%s\r\n",
53 rw[(port->set ? 1 : 0) | (port->get ? 2 : 0)],
54 port->numchn, port->bpch,
62 static const struct port_desc *port_from_name(const char *port_name)
66 for (i = 0; i < ARRAY_SIZE(port_desc); i++)
67 if (strcmp(port_name, port_desc[i].name) == 0)
73 * @brief Read values from specified port
75 * @param[in] cmd_io Pointer to IO stack
76 * @param[in] des Pointer to command descriptor
77 * @param[in] param Parameters of command
78 * @return 0 when OK or error code
80 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
86 if (sscanf(param[1], "%31s ", portName) != 1)
87 return -CMDERR_BADPAR;
89 const struct port_desc *port = port_from_name(portName);
91 return -CMDERR_BADPAR;
93 if (param[2] != NULL) { // More parameters = set values
95 return -CMDERR_WRPERM;
100 ret = sscanf(param[2], "%i", &value);
101 if (ret == EOF || ret == 0)
102 return -CMDERR_BADPAR;
104 ret = port->set(port, &value, sizeof(value));
106 return -CMDERR_WRPERM;
107 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
111 ret = sscanf(param[2], "%hi", &value);
112 if (ret == EOF || ret == 0)
114 ret = port->set(port, &value, sizeof(value));
116 return -CMDERR_WRPERM;
117 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
121 ret = sscanf(param[2], "%i", &value);
122 if (ret == EOF || ret == 0 || (value & 0xff000000))
123 return -CMDERR_BADPAR;
125 char v[3] = { (value >> 16) & 0xff, (value >> 8) & 0xff, (value >> 0) & 0xff };
127 ret = port->set(port, &v, sizeof(v));
129 return -CMDERR_WRPERM;
130 value = v[0] << 16 | v[1] << 8 | v[2];
131 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
135 ret = sscanf(param[2], "%i", &value);
136 if (ret == EOF || ret == 0)
137 return -CMDERR_BADPAR;
139 char v[4] = { (value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, (value >> 0) & 0xff };
141 ret = port->set(port, &v, sizeof(v));
143 return -CMDERR_WRPERM;
144 value = v[0] << 24 | v[1] << 16 | v[2] << 8 | v[3];
145 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
148 rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
149 return -CMDERR_NODEV;
151 } else { /* Get values from port */
153 return -CMDERR_RDPERM;
155 /* Workaround for cmd_opchar_replong() to work */
156 param[2] = param[0] + strlen(param[0]);
158 switch (port->bpch) {
161 ret = port->get(port, &value, sizeof(value));
163 return -CMDERR_RDPERM;
164 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
167 uint16_t values[16] = {0};
168 if (port->numchn > ARRAY_SIZE(values))
169 return -CMDERR_BADCFG;
170 ret = port->get(port, &values, sizeof(values));
172 return -CMDERR_RDPERM;
174 if (port->numchn == 1)
175 return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
177 rpp_sci_printf("%*s=", param[2] - param[0], param[0]);
178 for (i = 0; i < port->numchn; i++)
179 rpp_sci_printf(" %hx", values[i]);
180 rpp_sci_printf("\n");
185 rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
186 return -CMDERR_NODEV;
194 /** Command descriptor for read values from port command */
195 cmd_des_t const cmd_des_port_val = {
197 "portval*","Read or write values from or to the port",
198 "### Command syntax ###\n"
200 " portval<NAME> <VAL> [<VAL> ...]\n"
204 "- `<NAME>` is a string specifying the name of the port\n"
205 "- `<VAL>` is decimal, hexdecimal (0x) or octal (0) number\n"
207 "### Description ###\n"
209 "This command sets (when VAL is present) or gets (without VAL) values\n"
210 "of all channels of the specified port. For digital IO ports (1 bit per\n"
211 "channel), the least significant bit of the VAL corresponds to the\n"
212 "first pin, the second bit to the second pin, etc.\n"
214 "The set variant of the command returns a value that depends on the\n"
215 "port type. For digital IO, this is the value set, for SPI ports this\n"
216 "is the SPI response.\n"
218 "The get variant returns the value read from the port.\n"
220 "If the port represents an SPI interface of the MCU, then it is write\n"
221 "only and the argument is interpreted as a command for the port\n"
224 "If the port is represents the ADC interface of the MCU, it is read\n"
225 "only and returns values for each ADC pin.\n"
227 "Port names and interface type can be obtained with the portlist\n"
232 " --> portvalGIOB 0x3A\n"
233 " portvalGIOB=0x3a\n"
235 " portvalGIOB=0x3a\n"
236 "This pair of commands sets:\nGIOB"
243 "Which is shown in getter output\n",
244 CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
247 /** Command descriptor for port list printout */
248 cmd_des_t const cmd_des_port_list = {
250 "portlist","Print a list of all port names",
251 "### Command syntax ###\n"
255 "### Description ###\n"
257 "This command prints the list of all defined ports accessible via the\n"
258 "portval command. Each line contains port name, read/write type, number\n"
259 "of channels and width of the channel in bits.\n"
264 " List of all defined ports with its type. Those names can be used by portval command.\n"
269 CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
272 /** List of commands for port, defined as external */
273 cmd_des_t const *cmd_list_port[] = {