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 *type = "";
47 if (port->set == port_spi_set)
50 rpp_sci_printf("%s %dx%db%s%s%s\r\n",
51 port->name, port->numchn, port->bpch,
59 static const struct port_desc *port_from_name(const char *port_name)
63 for (i = 0; i < ARRAY_SIZE(port_desc); i++)
64 if (strcmp(port_name, port_desc[i].name) == 0)
70 * @brief Read values from specified port
72 * @param[in] cmd_io Pointer to IO stack
73 * @param[in] des Pointer to command descriptor
74 * @param[in] param Parameters of command
75 * @return 0 when OK or error code
77 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
83 if (sscanf(param[1], "%31s ", portName) != 1)
84 return -CMDERR_BADPAR;
86 const struct port_desc *port = port_from_name(portName);
88 return -CMDERR_BADPAR;
90 if (param[2] != NULL) { // More parameters = set values
92 return -CMDERR_WRPERM;
99 ret = sscanf(param[2], "%i", &value);
100 if (ret == EOF || ret == 0)
101 return -CMDERR_BADPAR;
103 ret = port->set(port, &value, sizeof(value));
105 return -CMDERR_WRPERM;
106 return cmd_opchar_replong(cmd_io, param, value, 0, 16);
109 uint8_t values[16] = {0};
110 if (port->numchn > ARRAY_SIZE(values))
111 return -CMDERR_BADCFG;
112 for (i = 0; i < port->numchn; i++) {
113 ret = sscanf(param[2+i], "%hhi", &values[i]);
114 if (ret == EOF || ret == 0)
117 ret = port->set(port, &values, sizeof(values));
119 return -CMDERR_WRPERM;
120 return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
123 uint16_t values[16] = {0};
124 if (port->numchn > ARRAY_SIZE(values))
125 return -CMDERR_BADCFG;
126 for (i = 0; i < port->numchn; i++) {
127 ret = sscanf(param[2+i], "%hi", &values[i]);
128 if (ret == EOF || ret == 0)
131 ret = port->set(port, &values, sizeof(values));
133 return -CMDERR_WRPERM;
134 return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
137 rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
138 return -CMDERR_NODEV;
140 } else { /* Get values from port */
142 return -CMDERR_RDPERM;
144 switch (port->bpch) {
147 ret = port->get(port, &value, sizeof(value));
149 return -CMDERR_RDPERM;
150 rpp_sci_printf("0x%x\n", value);
154 uint8_t values[16] = {0};
155 if (port->numchn > ARRAY_SIZE(values))
156 return -CMDERR_BADCFG;
157 ret = port->get(port, &values, sizeof(values));
159 return -CMDERR_RDPERM;
160 for (i = 0; i < port->numchn; i++)
161 rpp_sci_printf(" %hhd", values[i]);
162 rpp_sci_printf("\n");
166 uint16_t values[16] = {0};
167 if (port->numchn > ARRAY_SIZE(values))
168 return -CMDERR_BADCFG;
169 ret = port->get(port, &values, sizeof(values));
171 return -CMDERR_RDPERM;
172 for (i = 0; i < port->numchn; i++)
173 rpp_sci_printf(" %hd", values[i]);
174 rpp_sci_printf("\n");
178 rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
179 return -CMDERR_NODEV;
187 /** Command descriptor for read values from port command */
188 cmd_des_t const cmd_des_port_val = {
190 "portval*","Read or write values from or to the port",
191 "### Command syntax ###\n"
193 " portval<NAME> <VAL> [<VAL> ...]\n"
197 "- `<NAME>` is a string specifying the name of the port\n"
198 "- `<VAL>` is decimal, hexdecimal (0x) or octal (0) number\n"
200 "### Description ###\n"
202 "This command sets or gets values of all pins on the specified port.\n"
203 "For digital IO ports, the least significant bit of the VAL corresponds\n"
204 "to the first pin, the second bit to the second pin, etc. The command\n"
205 "returns zero. When reading from the port, the command returns values\n"
208 "If the port represents an SPI interface of the MCU, then it is write\n"
209 "only and the argument is interpreted as a command for the port\n"
210 "controller. The command returns the response from the port controller.\n"
212 "If the port is connected to the ADC interface of the MCU, then\n"
213 "it is read only and returns values for each ADC pin.\n"
215 "Port names and interface type can be obtained with the portlist\n"
220 " --> portvalGIOB 0x3A\n"
224 "This pair of commands sets:\nGIOB"
231 "Which is shown in getter output\n",
232 CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
235 /** Command descriptor for port list printout */
236 cmd_des_t const cmd_des_port_list = {
238 "portlist","Print a list of all port names",
239 "### Command syntax ###\n"
243 "### Description ###\n"
245 "This command prints the list of all defined ports accessible via the\n"
246 "portval command. Each record of the list is a couple of\n"
247 "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
248 "The type of the MCU<->port interface slightly modifies the meaning\n"
249 "of the portval command."
254 " List of all defined ports with its type. Those names can be used by portval command.\n"
259 CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
262 /** List of commands for port, defined as external */
263 cmd_des_t const *cmd_list_port[] = {