]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_port.c
Update to the reworked gio and port in the rpp-lib
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_port.c
1 /*
2  * Copyright (C) 2012-2015 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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.
13  *
14  * File : cmd_port.c
15  *
16  * Abstract:
17  *           Commands for port controlling
18  *          - Printing list of available ports (not yet available)
19  *          - Setting/getting port value*
20  */
21
22 #include "cmd_port.h"
23 #include "stdio.h"
24 #include "string.h"
25
26 #ifndef DOCGEN
27
28 #include "rpp/rpp.h"
29 #include "drv/port.h"
30 #ifdef TARGET_HAS_SPI
31 #include "drv/spi.h"
32 #endif
33 #include "cmdproc_utils.h"
34
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36
37 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
38 {
39         uint32_t i;
40
41         rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
42
43         for (i = 0; i < ARRAY_SIZE(port_desc); i++) {
44                 const struct port_desc *port = &port_desc[i];
45                 const char *type = "";
46 #ifdef TARGET_HAS_SPI
47                 if (port->set == port_spi_set)
48                         type = "SPI";
49 #endif
50                 rpp_sci_printf("%s %dx%db%s%s%s\r\n",
51                                            port->name, port->numchn, port->bpch,
52                                            *type ? " (": "",
53                                            type,
54                                            *type ? ")" : "");
55         }
56         return 1;
57 }
58
59 static const struct port_desc *port_from_name(const char *port_name)
60 {
61         uint32_t i;
62
63         for (i = 0; i < ARRAY_SIZE(port_desc); i++)
64                 if (strcmp(port_name, port_desc[i].name) == 0)
65                         return &port_desc[i];
66         return NULL;
67 }
68
69 /**
70  * @brief       Read values from specified port
71  *
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
76  */
77 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
78 {
79         int32_t ret;
80         int i;
81         char portName[32];
82
83         if (sscanf(param[1], "%31s ", portName) != 1)
84                 return -CMDERR_BADPAR;
85
86         const struct port_desc *port = port_from_name(portName);
87         if (!port)
88                 return -CMDERR_BADPAR;
89
90         if (param[2] != NULL) { // More parameters = set values
91                 if (!port->set)
92                         return -CMDERR_WRPERM;
93
94                 switch (port->bpch) {
95                 case 1:
96                 case 32:
97                 {
98                         uint32_t value;
99                         ret = sscanf(param[2], "%i", &value);
100                         if (ret == EOF || ret == 0)
101                                 return -CMDERR_BADPAR;
102
103                         ret = port->set(port, &value, sizeof(value));
104                         if (ret == FAILURE)
105                                 return -CMDERR_WRPERM;
106                         return cmd_opchar_replong(cmd_io, param, value, 0, 16);
107                 }
108                 case 8: {
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)
115                                         break;
116                         }
117                         ret = port->set(port, &values, sizeof(values));
118                         if (ret == FAILURE)
119                                 return -CMDERR_WRPERM;
120                         return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
121                 }
122                 case 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)
129                                         break;
130                         }
131                         ret = port->set(port, &values, sizeof(values));
132                         if (ret == FAILURE)
133                                 return -CMDERR_WRPERM;
134                         return cmd_opchar_replong(cmd_io, param, values[0], 0, 16);
135                 }
136                 default:
137                         rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
138                         return -CMDERR_NODEV;
139                 }
140         } else { /* Get values from port */
141                 if (!port->get)
142                         return -CMDERR_RDPERM;
143
144                 switch (port->bpch) {
145                 case 1: {
146                         uint32_t value = 0;
147                         ret = port->get(port, &value, sizeof(value));
148                         if (ret == FAILURE)
149                                 return -CMDERR_RDPERM;
150                         rpp_sci_printf("0x%x\n", value);
151                         break;
152                 }
153                 case 8: {
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));
158                         if (ret == FAILURE)
159                                 return -CMDERR_RDPERM;
160                         for (i = 0; i < port->numchn; i++)
161                                 rpp_sci_printf(" %hhd", values[i]);
162                         rpp_sci_printf("\n");
163                         break;
164                 }
165                 case 16: {
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));
170                         if (ret == FAILURE)
171                                 return -CMDERR_RDPERM;
172                         for (i = 0; i < port->numchn; i++)
173                                 rpp_sci_printf(" %hd", values[i]);
174                         rpp_sci_printf("\n");
175                         break;
176                 }
177                 default:
178                         rpp_sci_printf("Unsupported bits-per-channel value: %d\n", port->bpch);
179                         return -CMDERR_NODEV;
180                 }
181                 return 0;
182         }
183 }
184
185 #endif  /* DOCGEN */
186
187 /** Command descriptor for read values from port command */
188 cmd_des_t const cmd_des_port_val = {
189         0, 0,
190         "portval*","Read or write values from or to the port",
191         "### Command syntax ###\n"
192         "\n"
193         "     portval<NAME> <VAL> [<VAL> ...]\n"
194         "     portval<NAME>\n"
195         "where\n"
196         "\n"
197         "- `<NAME>` is a string specifying the name of the port\n"
198         "- `<VAL>` is decimal, hexdecimal (0x) or octal (0) number\n"
199         "\n"
200         "### Description ###\n"
201         "\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"
206         "for each pin.\n"
207         "\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"
211         "\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"
214         "\n"
215         "Port names and interface type can be obtained with the portlist\n"
216         "command.\n"
217         "\n"
218         "### Example ###\n"
219         "\n"
220         "     --> portvalGIOB 0x3A\n"
221         "     portvalGIOB=0\n"
222         "     --> portvalGIOB\n"
223         "        0x3a\n"
224         "This pair of commands sets:\nGIOB"
225         "GIOB=0\n"
226         "GIOB=1\n"
227         "GIOB=0\n"
228         "GIOB=1\n"
229         "GIOB=1\n"
230         "GIOB=1\n"
231         "Which is shown in getter output\n",
232         CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
233 };
234
235 /** Command descriptor for port list printout */
236 cmd_des_t const cmd_des_port_list = {
237         0, 0,
238         "portlist","Print a list of all port names",
239         "### Command syntax ###\n"
240         "\n"
241         "     portlist\n"
242         "\n"
243         "### Description ###\n"
244         "\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."
250         "\n"
251         "### Example ###\n"
252         "\n"
253         "     --> portlist\n"
254         "     List of all defined ports with its type. Those names can be used by portval command.\n"
255         "     GIOA, GPIO\n"
256         "     GIOB, GPIO\n"
257         "     NHET1, GPIO\n"
258         "     ADC, ADC\n",
259         CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
260 };
261
262 /** List of commands for port, defined as external */
263 cmd_des_t const *cmd_list_port[] = {
264         &cmd_des_port_val,
265         &cmd_des_port_list,
266         NULL
267 };