2 * Copyright (C) 2012-2013 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*
22 #include "../cmd_port.h"
29 #include "cmdproc_utils.h"
32 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
37 rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
38 const port_def_t *ports = hal_port_get_definitions();
40 for (i = 0; i < PORT_CNT; i++) {
41 if (ports[i].name == PIN_NAME_UNUSED) continue;
42 if (ports[i].desc->interfaceType == PORT_INTERFACE_SPI)
43 portInterface = "SPI";
44 else if (ports[i].desc->interfaceType == PORT_INTERFACE_GPIO)
45 portInterface = "GPIO";
46 else if (ports[i].desc->interfaceType == PORT_INTERFACE_ADC)
47 portInterface = "ADC";
49 portInterface = "UNKNOWN";
50 rpp_sci_printf("%s, %s\r\n", ports[i].name, portInterface);
58 * @brief Read values from specified port
60 * @param[in] cmd_io Pointer to IO stack
61 * @param[in] des Pointer to command descriptor
62 * @param[in] param Parameters of command
63 * @return 0 when OK or error code
65 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
71 uint32_t values[MAX_PARAM_VALUES_NUM];
77 if (sscanf(p, "%31s ", portName) != 1)
78 return -CMDERR_BADPAR;
80 if ((desc = hal_port_get_dsc(portName, -1)) == NULL) return -CMDERR_BADREG;
82 if (param[2] != NULL) { // More parameters = set values
84 if (desc->port_setfnc_ptr == NULL)
85 return -CMDERR_WRPERM;
87 if (desc->interfaceType == PORT_INTERFACE_GPIO)
88 // Information about pin values are encoded as hexadecimal 8b value
89 numParams = desc->numValues/8+1;
90 else if (desc->interfaceType == PORT_INTERFACE_SPI)
91 // Commands are passed as bytes
92 numParams = desc->numValues;
93 else if (desc->interfaceType == PORT_INTERFACE_ADC)
94 return -CMDERR_BADPAR; // ADC is read only and no other port is supported
95 token = strtok(p, " ");
97 while (i < numParams && token != NULL) {
98 if (sscanf(token, "%x", &values[i]) == EOF)
100 token = strtok(NULL, " ");
104 if (i != numParams || token != NULL)
105 return -CMDERR_BADPAR;
106 ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
108 return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
111 if (desc->port_getfnc_ptr == NULL)
112 return -CMDERR_RDPERM;
114 ret = desc->port_getfnc_ptr(desc->config, desc->numValues, values);
115 for (i = 0; i < desc->numValues; i++) {
116 rpp_sci_printf("%d\r\n", values[i]);
119 rpp_sci_printf("portval%s=%x", portName, ret);
126 /** Command descriptor for read values from port command */
127 cmd_des_t const cmd_des_port_val = {
129 "portval*","Read or write values from or to the port",
130 "### Command syntax ###\n"
132 " portval<NAME> <VAL>\n"
136 "- `<NAME>` is a string specifying the name of the port\n"
137 "- `<VAL>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
139 "### Description ###\n"
141 "This command sets or gets values of all pins on the specified port.\n"
142 "If the port is connected to the GPIO interface of the MCU, then\n"
143 "when writing the value, the lowest significant bit of the argument\n"
144 "is assigned to the first pin, the second bit is assigned to the\n"
145 "second pin, etc. The command returns zero.\n"
146 "When reading from the port, the command returns values for each pin.\n"
148 "If the port is connected to the SPI interface of the MCU, then\n"
149 "it is write only and the argument is interpreted as a command for\n"
150 "the port controller. The command returns the response from the\n"
152 "For command examples please refer to the project wiki\n"
154 "If the port is connected to the ADC interface of the MCU, then\n"
155 "it is read only and returns values for each ADC pin.\n"
157 "Port names and interface type can be obtained with the portlist\n"
160 "NOTE: For successful communication with the HBR, HBR_EN pin must\n"
165 " --> portvalMOUTIN 3A\n"
167 " --> portvalMOUTIN\n"
175 "This pair of commands sets:\nMOUT1IN"
182 "Which is shown in getter output\n",
183 CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
186 /** Command descriptor for port list printout */
187 cmd_des_t const cmd_des_port_list = {
189 "portlist","Print a list of all port names",
190 "### Command syntax ###\n"
194 "### Description ###\n"
196 "This command prints the list of all defined ports accessible via the\n"
197 "portval command. Each record of the list is a couple of\n"
198 "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
199 "The type of the MCU<->port interface slightly modifies the meaning\n"
200 "of the portval command."
205 " List of all defined ports with its type. Those names can be used by portval command.\n"
221 CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
224 /** List of commands for port, defined as external */
225 cmd_des_t const *cmd_list_port[] = {