]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_port.c
Merge branch 'can' of rtime.felk.cvut.cz:rpp-test-sw
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_port.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * File : cmd_port.c
23  *
24  * Abstract:
25  *               Commands for port controlling
26  *              - Printing list of available ports (not yet available)
27  *              - Setting/getting port value*
28  */
29
30 #include "cmd_port.h"
31 #include "stdio.h"
32
33 #ifndef DOCGEN
34
35 #include "rpp/rpp.h"
36 #include "hal/hal.h"
37 #include "cmdproc_utils.h"
38
39
40 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
41         uint32_t i;
42         char* portInterface;
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();
45
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";
54                 else
55                         portInterface = "UNKNOWN";
56                 rpp_sci_printf("%s, %s\r\n", ports[i].name, portInterface);
57         }
58         return 1;
59 }
60
61
62
63 /**
64  * @brief       Read values from specified port
65  *
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
70  */
71 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
72   char *p;
73   int i;
74   port_desc_t* desc;
75   uint32_t ret;
76   uint32_t values[MAX_PARAM_VALUES_NUM];
77   char portName[32];
78   char* token;
79   uint32_t numParams;
80
81   p = param[1];
82   if (sscanf(p, "%31s ", portName) != 1) {
83           return -CMDERR_BADPAR;
84   }
85
86   if((desc = hal_port_get_dsc(portName, -1))==NULL) return -CMDERR_BADREG;
87
88   if(param[2] != NULL){         // More parameters = set values
89         p=param[2];
90         if (desc->port_setfnc_ptr == NULL) {
91                 return -CMDERR_WRPERM;
92         }
93         else {
94                 if (desc->interfaceType == PORT_INTERFACE_GPIO) {
95                         // Information about pin values are encoded as hexadecimal 8b value
96                         numParams = desc->numValues/8+1;
97                 }
98                 else if (desc->interfaceType == PORT_INTERFACE_SPI) {
99                         // Commands are passed as bytes
100                         numParams = desc->numValues;
101                 }
102                 else if (desc->interfaceType == PORT_INTERFACE_ADC) {
103                         return -CMDERR_BADPAR;  // ADC is read only and no other port is supported
104                 }
105                 token = strtok(p, " ");
106                 i = 0;
107                 while (i < numParams && token != NULL) {
108                         if (sscanf(token, "%x", &values[i]) == EOF) {
109                                 break;
110                         }
111                         token = strtok(NULL, " ");
112                         i++;
113                 }
114
115                 if (i != numParams || token != NULL) {
116                         return -CMDERR_BADPAR;
117                 }
118                 ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
119         }
120         return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
121   }
122   else {
123           if (desc->port_getfnc_ptr == NULL) {
124                         return -CMDERR_RDPERM;
125           }
126           else {
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]);
130                         }
131           }
132                 rpp_sci_printf("portval%s=%x", portName, ret);
133                 return 0;
134   }
135 }
136
137 #endif  /* DOCGEN */
138
139 /** Command descriptor for read values from port command */
140 cmd_des_t const cmd_des_port_val={
141     0, 0,
142     "portval*","Read or write values from or to the port",
143     "=== Command syntax ===\n"
144     "\n"
145     "   portval<NAME> <VAL>\n"
146     "   portval<NAME>\n"
147     "where\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"
150     "\n"
151     "=== Description ===\n"
152     "\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"
159     "\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"
163     "port controller.\n"
164     "For command examples please refer to the project wiki\n"
165     "\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"
168     "\n"
169     "Port names and interface type can be obtained with the portlist\n"
170     "command.\n"
171     "\n"
172     "NOTE: For successful communication with the HBR, HBR_EN pin must\n"
173     "be set first.\n"
174     "\n"
175     "=== Example ===\n"
176     "\n"
177     "   --> portvalMOUTIN 3A\n"
178     "   portvalMOUTIN=0\n"
179     "   --> portvalMOUTIN\n"
180     "   0\n"
181     "   1\n"
182     "   0\n"
183     "   1\n"
184     "   1\n"
185     "   1\n"
186     "\n"
187     "This pair of commands sets:\nMOUT1IN"
188     "MOUT1IN=0\n"
189     "MOUT2IN=1\n"
190     "MOUT3IN=0\n"
191     "MOUT4IN=1\n"
192     "MOUT5IN=1\n"
193     "MOUT6IN=1\n"
194     "Which is shown in getter output\n",
195     CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
196 };
197
198 /** Command descriptor for port list printout */
199 cmd_des_t const cmd_des_port_list={
200     0, 0,
201     "portlist","Print a list of all port names",
202     "=== Command syntax ===\n"
203     "\n"
204     "   portlist\n"
205     "\n"
206     "=== Description ===\n"
207     "\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."
213     "\n"
214     "=== Example ===\n"
215     "\n"
216     "   --> portlist\n"
217     "   List of all defined ports with its type. Those names can be used by portval command.\n"
218     "   DINMCU, GPIO\n"
219     "   DINSPI, SPI\n"
220     "   HOUTDIAG, GPIO\n"
221     "   HOUTIN, GPIO\n"
222     "   HOUTIFBK, ADC\n"
223     "   ADC, ADC\n"
224     "   LOUT, SPI\n"
225     "   DAC12, SPI\n"
226     "   DAC34, SPI\n"
227     "   DACDREF, SPI\n"
228     "   HBR, SPI\n"
229     "   FRAY1, SPI\n"
230     "   FRAY2, SPI\n"
231     "   MOUTEN, GPIO\n"
232     "   MOUTIN, GPIO\n",
233     CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
234 };
235
236 /** List of commands for port, defined as external */
237 cmd_des_t const *cmd_list_port[]={
238   &cmd_des_port_val,
239   &cmd_des_port_list,
240   NULL
241 };