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*
29 #include "drv/digital_io_def.h"
30 #include "cmdproc_utils.h"
33 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
38 rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
39 uint32_t port_cnt = rpp_port_get_port_cnt();
40 const char* port_names[16];
41 rpp_port_get_port_names(port_names, port_cnt);
43 for (i = 0; i < port_cnt; i++) {
44 if (strcmp(port_names[i], DIO_PIN_NAME_UNUSED) == 0) continue;
45 int8_t port_interface = rpp_port_get_interface_type(port_names[i]);
46 if (port_interface == 2)
47 portInterface = "SPI";
48 else if (port_interface == 3)
49 portInterface = "GPIO";
50 else if (port_interface == 1)
51 portInterface = "ADC";
53 portInterface = "UNKNOWN";
54 rpp_sci_printf("%s, %s\r\n", port_names[i], portInterface);
62 * @brief Read values from specified port
64 * @param[in] cmd_io Pointer to IO stack
65 * @param[in] des Pointer to command descriptor
66 * @param[in] param Parameters of command
67 * @return 0 when OK or error code
69 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
74 uint32_t values[MAX_PARAM_VALUES_NUM];
78 int32_t port_pin_cnt = rpp_port_get_pin_cnt(portName);
81 if (sscanf(p, "%31s ", portName) != 1)
82 return -CMDERR_BADPAR;
84 int8_t port_interface = rpp_port_get_interface_type(portName);
85 if (port_interface == FAILURE) return -CMDERR_BADREG;
87 if (param[2] != NULL) { // More parameters = set values
89 if (port_interface == 3)
90 // Information about pin values are encoded as hexadecimal 8b value
91 numParams = port_pin_cnt/8+1;
92 else if (port_interface == 2)
93 // Commands are passed as bytes
94 numParams = port_pin_cnt;
95 else if (port_interface == 1)
96 return -CMDERR_BADPAR; // ADC is read only and no other port is supported
97 token = strtok(p, " ");
99 while (i < numParams && token != NULL) {
100 if (sscanf(token, "%x", &values[i]) == EOF)
102 token = strtok(NULL, " ");
106 if (i != numParams || token != NULL)
107 return -CMDERR_BADPAR;
108 ret = rpp_port_set_val(portName, port_pin_cnt, values);
109 if (ret == FAILURE) {
110 return -CMDERR_WRPERM;
113 return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
116 ret = rpp_port_get_val(portName, port_pin_cnt, values);
117 if (ret == FAILURE) {
118 return -CMDERR_RDPERM;
120 for (i = 0; i < port_pin_cnt; i++) {
121 rpp_sci_printf("%d\r\n", values[i]);
123 rpp_sci_printf("portval%s=%x\n", portName, ret);
130 /** Command descriptor for read values from port command */
131 cmd_des_t const cmd_des_port_val = {
133 "portval*","Read or write values from or to the port",
134 "### Command syntax ###\n"
136 " portval<NAME> <VAL>\n"
140 "- `<NAME>` is a string specifying the name of the port\n"
141 "- `<VAL>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
143 "### Description ###\n"
145 "This command sets or gets values of all pins on the specified port.\n"
146 "If the port is connected to the GPIO interface of the MCU, then\n"
147 "when writing the value, the lowest significant bit of the argument\n"
148 "is assigned to the first pin, the second bit is assigned to the\n"
149 "second pin, etc. The command returns zero.\n"
150 "When reading from the port, the command returns values for each pin.\n"
152 "If the port is connected to the SPI interface of the MCU, then\n"
153 "it is write only and the argument is interpreted as a command for\n"
154 "the port controller. The command returns the response from the\n"
156 "For command examples please refer to the project wiki\n"
158 "If the port is connected to the ADC interface of the MCU, then\n"
159 "it is read only and returns values for each ADC pin.\n"
161 "Port names and interface type can be obtained with the portlist\n"
166 " --> portvalGIOB 3A\n"
176 "This pair of commands sets:\nGIOB"
183 "Which is shown in getter output\n",
184 CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
187 /** Command descriptor for port list printout */
188 cmd_des_t const cmd_des_port_list = {
190 "portlist","Print a list of all port names",
191 "### Command syntax ###\n"
195 "### Description ###\n"
197 "This command prints the list of all defined ports accessible via the\n"
198 "portval command. Each record of the list is a couple of\n"
199 "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
200 "The type of the MCU<->port interface slightly modifies the meaning\n"
201 "of the portval command."
206 " List of all defined ports with its type. Those names can be used by portval command.\n"
211 CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
214 /** List of commands for port, defined as external */
215 cmd_des_t const *cmd_list_port[] = {