2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Commands for port controlling
26 * - Printing list of available ports (not yet available)
27 * - Setting/getting port value*
37 #include "cmdproc_utils.h"
40 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
43 rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
44 const port_def_t* ports = hal_port_get_definitions();
46 for (i = 0; i < PORT_CNT; i++) {
47 if (ports[i].name == PIN_NAME_UNUSED) continue;
48 if (ports[i].desc->interfaceType == PORT_INTERFACE_SPI)
49 portInterface = "SPI";
50 else if (ports[i].desc->interfaceType == PORT_INTERFACE_GPIO)
51 portInterface = "GPIO";
52 else if (ports[i].desc->interfaceType == PORT_INTERFACE_ADC)
53 portInterface = "ADC";
55 portInterface = "UNKNOWN";
56 rpp_sci_printf("%s, %s\r\n", ports[i].name, portInterface);
64 * @brief Read values from specified port
66 * @param[in] cmd_io Pointer to IO stack
67 * @param[in] des Pointer to command descriptor
68 * @param[in] param Parameters of command
69 * @return 0 when OK or error code
71 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
76 uint32_t values[MAX_PARAM_VALUES_NUM];
82 if (sscanf(p, "%31s ", portName) != 1) {
83 return -CMDERR_BADPAR;
86 if((desc = hal_port_get_dsc(portName, -1))==NULL) return -CMDERR_BADREG;
88 if(param[2] != NULL){ // More parameters = set values
90 if (desc->port_setfnc_ptr == NULL) {
91 return -CMDERR_WRPERM;
94 if (desc->interfaceType == PORT_INTERFACE_GPIO) {
95 // Information about pin values are encoded as hexadecimal 8b value
96 numParams = desc->numValues/8+1;
98 else if (desc->interfaceType == PORT_INTERFACE_SPI) {
99 // Commands are passed as bytes
100 numParams = desc->numValues;
102 else if (desc->interfaceType == PORT_INTERFACE_ADC) {
103 return -CMDERR_BADPAR; // ADC is read only and no other port is supported
105 token = strtok(p, " ");
107 while (i < numParams && token != NULL) {
108 if (sscanf(token, "%x", &values[i]) == EOF) {
111 token = strtok(NULL, " ");
115 if (i != numParams || token != NULL) {
116 return -CMDERR_BADPAR;
118 ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
120 return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
123 if (desc->port_getfnc_ptr == NULL) {
124 return -CMDERR_RDPERM;
127 ret = desc->port_getfnc_ptr(desc->config, desc->numValues, values);
128 for (i = 0; i < desc->numValues; i++) {
129 rpp_sci_printf("%d\r\n", values[i]);
132 rpp_sci_printf("portval%s=%x", portName, ret);
139 /** Command descriptor for read values from port command */
140 cmd_des_t const cmd_des_port_val={
142 "portval*","Read or write values from or to the port",
143 "=== Command syntax ===\n"
145 " portval<NAME> <VAL>\n"
148 "* <NAME> is a string specifying the name of the port\n"
149 "* <VAL> is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
151 "=== Description ===\n"
153 "This command sets or gets values of all pins on the specified port.\n"
154 "If the port is connected to the GPIO interface of the MCU, then\n"
155 "when writing the value, the lowest significant bit of the argument\n"
156 "is assigned to the first pin, the second bit is assigned to the\n"
157 "second pin, etc. The command returns zero.\n"
158 "When reading from the port, the command returns values for each pin.\n"
160 "If the port is connected to the SPI interface of the MCU, then\n"
161 "it is write only and the argument is interpreted as a command for\n"
162 "the port controller. The command returns the response from the\n"
164 "For command examples please refer to the project wiki\n"
166 "If the port is connected to the ADC interface of the MCU, then\n"
167 "it is read only and returns values for each ADC pin.\n"
169 "Port names and interface type can be obtained with the portlist\n"
172 "NOTE: For successful communication with the HBR, HBR_EN pin must\n"
177 " --> portvalMOUTIN 3A\n"
179 " --> portvalMOUTIN\n"
187 "This pair of commands sets:\nMOUT1IN"
194 "Which is shown in getter output\n",
195 CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
198 /** Command descriptor for port list printout */
199 cmd_des_t const cmd_des_port_list={
201 "portlist","Print a list of all port names",
202 "=== Command syntax ===\n"
206 "=== Description ===\n"
208 "This command prints the list of all defined ports accessible via the\n"
209 "portval command. Each record of the list is a couple of\n"
210 "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
211 "The type of the MCU<->port interface slightly modifies the meaning\n"
212 "of the portval command."
217 " List of all defined ports with its type. Those names can be used by portval command.\n"
233 CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
236 /** List of commands for port, defined as external */
237 cmd_des_t const *cmd_list_port[]={