]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_port.c
Update library
[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/digital_io_def.h"
30 #include "cmdproc_utils.h"
31
32
33 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
34 {
35         uint32_t i;
36         char *portInterface;
37
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);
42
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";
52                 else
53                         portInterface = "UNKNOWN";
54                 rpp_sci_printf("%s, %s\r\n", port_names[i], portInterface);
55         }
56         return 1;
57 }
58
59
60
61 /**
62  * @brief       Read values from specified port
63  *
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
68  */
69 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
70 {
71         char *p;
72         int i;
73         int32_t ret;
74         uint32_t values[MAX_PARAM_VALUES_NUM];
75         char portName[32];
76         char *token;
77         uint32_t numParams;
78         int32_t port_pin_cnt = rpp_port_get_pin_cnt(portName);
79
80         p = param[1];
81         if (sscanf(p, "%31s ", portName) != 1)
82                 return -CMDERR_BADPAR;
83
84         int8_t port_interface = rpp_port_get_interface_type(portName);
85         if (port_interface == FAILURE) return -CMDERR_BADREG;
86
87         if (param[2] != NULL) { // More parameters = set values
88                 p = param[2];
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, " ");
98                 i = 0;
99                 while (i < numParams && token != NULL) {
100                         if (sscanf(token, "%x", &values[i]) == EOF)
101                                 break;
102                         token = strtok(NULL, " ");
103                         i++;
104                 }
105
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;
111                 }
112
113                 return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
114         }
115         else {
116                 ret = rpp_port_get_val(portName, port_pin_cnt, values);
117                 if (ret == FAILURE) {
118                         return -CMDERR_RDPERM;
119                 }
120                 for (i = 0; i < port_pin_cnt; i++) {
121                         rpp_sci_printf("%d\r\n", values[i]);
122                 }
123                 rpp_sci_printf("portval%s=%x\n", portName, ret);
124                 return 0;
125         }
126 }
127
128 #endif  /* DOCGEN */
129
130 /** Command descriptor for read values from port command */
131 cmd_des_t const cmd_des_port_val = {
132         0, 0,
133         "portval*","Read or write values from or to the port",
134         "### Command syntax ###\n"
135         "\n"
136         "     portval<NAME> <VAL>\n"
137         "     portval<NAME>\n"
138         "where\n"
139         "\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"
142         "\n"
143         "### Description ###\n"
144         "\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"
151         "\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"
155         "port controller.\n"
156         "For command examples please refer to the project wiki\n"
157         "\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"
160         "\n"
161         "Port names and interface type can be obtained with the portlist\n"
162         "command.\n"
163         "\n"
164         "### Example ###\n"
165         "\n"
166         "     --> portvalGIOB 3A\n"
167         "     portvalGIOB=0\n"
168         "     --> portvalGIOB\n"
169         "     0\n"
170         "     1\n"
171         "     0\n"
172         "     1\n"
173         "     1\n"
174         "     1\n"
175         "\n"
176         "This pair of commands sets:\nGIOB"
177         "GIOB=0\n"
178         "GIOB=1\n"
179         "GIOB=0\n"
180         "GIOB=1\n"
181         "GIOB=1\n"
182         "GIOB=1\n"
183         "Which is shown in getter output\n",
184         CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
185 };
186
187 /** Command descriptor for port list printout */
188 cmd_des_t const cmd_des_port_list = {
189         0, 0,
190         "portlist","Print a list of all port names",
191         "### Command syntax ###\n"
192         "\n"
193         "     portlist\n"
194         "\n"
195         "### Description ###\n"
196         "\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."
202         "\n"
203         "### Example ###\n"
204         "\n"
205         "     --> portlist\n"
206         "     List of all defined ports with its type. Those names can be used by portval command.\n"
207         "     GIOA, GPIO\n"
208         "     GIOB, GPIO\n"
209         "     NHET1, GPIO\n"
210         "     ADC, ADC\n",
211         CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
212 };
213
214 /** List of commands for port, defined as external */
215 cmd_des_t const *cmd_list_port[] = {
216         &cmd_des_port_val,
217         &cmd_des_port_list,
218         NULL
219 };