]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/_tms570_rpp/cmd_port.c
Modify flexray commands to reflect changes in RPP library
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / _tms570_rpp / 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 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
25 #ifndef DOCGEN
26
27 #include "rpp/rpp.h"
28 #include "hal/hal.h"
29 #include "cmdproc_utils.h"
30
31
32 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
33 {
34         uint32_t i;
35         char *portInterface;
36
37         rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
38         const port_def_t *ports = hal_port_get_definitions();
39
40         for (i = 0; i < PORT_CNT; i++) {
41                 if (ports[i].name == PIN_NAME_UNUSED) continue;
42                 if (ports[i].desc->interfaceType == PORT_INTERFACE_SPI)
43                         portInterface = "SPI";
44                 else if (ports[i].desc->interfaceType == PORT_INTERFACE_GPIO)
45                         portInterface = "GPIO";
46                 else if (ports[i].desc->interfaceType == PORT_INTERFACE_ADC)
47                         portInterface = "ADC";
48                 else
49                         portInterface = "UNKNOWN";
50                 rpp_sci_printf("%s, %s\r\n", ports[i].name, portInterface);
51         }
52         return 1;
53 }
54
55
56
57 /**
58  * @brief       Read values from specified port
59  *
60  * @param[in]   cmd_io  Pointer to IO stack
61  * @param[in]   des             Pointer to command descriptor
62  * @param[in]   param   Parameters of command
63  * @return      0 when OK or error code
64  */
65 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
66 {
67         char *p;
68         int i;
69         port_desc_t *desc;
70         uint32_t ret;
71         uint32_t values[MAX_PARAM_VALUES_NUM];
72         char portName[32];
73         char *token;
74         uint32_t numParams;
75
76         p = param[1];
77         if (sscanf(p, "%31s ", portName) != 1)
78                 return -CMDERR_BADPAR;
79
80         if ((desc = hal_port_get_dsc(portName, -1)) == NULL) return -CMDERR_BADREG;
81
82         if (param[2] != NULL) { // More parameters = set values
83                 p = param[2];
84                 if (desc->port_setfnc_ptr == NULL)
85                         return -CMDERR_WRPERM;
86                 else {
87                         if (desc->interfaceType == PORT_INTERFACE_GPIO)
88                                 // Information about pin values are encoded as hexadecimal 8b value
89                                 numParams = desc->numValues/8+1;
90                         else if (desc->interfaceType == PORT_INTERFACE_SPI)
91                                 // Commands are passed as bytes
92                                 numParams = desc->numValues;
93                         else if (desc->interfaceType == PORT_INTERFACE_ADC)
94                                 return -CMDERR_BADPAR;  // ADC is read only and no other port is supported
95                         token = strtok(p, " ");
96                         i = 0;
97                         while (i < numParams && token != NULL) {
98                                 if (sscanf(token, "%x", &values[i]) == EOF)
99                                         break;
100                                 token = strtok(NULL, " ");
101                                 i++;
102                         }
103
104                         if (i != numParams || token != NULL)
105                                 return -CMDERR_BADPAR;
106                         ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
107                 }
108                 return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
109         }
110         else {
111                 if (desc->port_getfnc_ptr == NULL)
112                         return -CMDERR_RDPERM;
113                 else {
114                         ret = desc->port_getfnc_ptr(desc->config, desc->numValues, values);
115                         for (i = 0; i < desc->numValues; i++) {
116                                 rpp_sci_printf("%d\r\n", values[i]);
117                         }
118                 }
119                 rpp_sci_printf("portval%s=%x", portName, ret);
120                 return 0;
121         }
122 }
123
124 #endif  /* DOCGEN */
125
126 /** Command descriptor for read values from port command */
127 cmd_des_t const cmd_des_port_val = {
128         0, 0,
129         "portval*","Read or write values from or to the port",
130         "### Command syntax ###\n"
131         "\n"
132         "     portval<NAME> <VAL>\n"
133         "     portval<NAME>\n"
134         "where\n"
135         "\n"
136         "- `<NAME>` is a string specifying the name of the port\n"
137         "- `<VAL>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n\n"
138         "\n"
139         "### Description ###\n"
140         "\n"
141         "This command sets or gets values of all pins on the specified port.\n"
142         "If the port is connected to the GPIO interface of the MCU, then\n"
143         "when writing the value, the lowest significant bit of the argument\n"
144         "is assigned to the first pin, the second bit is assigned to the\n"
145         "second pin, etc. The command returns zero.\n"
146         "When reading from the port, the command returns values for each pin.\n"
147         "\n"
148         "If the port is connected to the SPI interface of the MCU, then\n"
149         "it is write only and the argument is interpreted as a command for\n"
150         "the port controller. The command returns the response from the\n"
151         "port controller.\n"
152         "For command examples please refer to the project wiki\n"
153         "\n"
154         "If the port is connected to the ADC interface of the MCU, then\n"
155         "it is read only and returns values for each ADC pin.\n"
156         "\n"
157         "Port names and interface type can be obtained with the portlist\n"
158         "command.\n"
159         "\n"
160         "NOTE: For successful communication with the HBR, HBR_EN pin must\n"
161         "be set first.\n"
162         "\n"
163         "### Example ###\n"
164         "\n"
165         "     --> portvalMOUTIN 3A\n"
166         "     portvalMOUTIN=0\n"
167         "     --> portvalMOUTIN\n"
168         "     0\n"
169         "     1\n"
170         "     0\n"
171         "     1\n"
172         "     1\n"
173         "     1\n"
174         "\n"
175         "This pair of commands sets:\nMOUT1IN"
176         "MOUT1IN=0\n"
177         "MOUT2IN=1\n"
178         "MOUT3IN=0\n"
179         "MOUT4IN=1\n"
180         "MOUT5IN=1\n"
181         "MOUT6IN=1\n"
182         "Which is shown in getter output\n",
183         CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
184 };
185
186 /** Command descriptor for port list printout */
187 cmd_des_t const cmd_des_port_list = {
188         0, 0,
189         "portlist","Print a list of all port names",
190         "### Command syntax ###\n"
191         "\n"
192         "     portlist\n"
193         "\n"
194         "### Description ###\n"
195         "\n"
196         "This command prints the list of all defined ports accessible via the\n"
197         "portval command. Each record of the list is a couple of\n"
198         "PortName-PortInterface, where PortInterface is SPI, ADC or GPIO.\n"
199         "The type of the MCU<->port interface slightly modifies the meaning\n"
200         "of the portval command."
201         "\n"
202         "### Example ###\n"
203         "\n"
204         "     --> portlist\n"
205         "     List of all defined ports with its type. Those names can be used by portval command.\n"
206         "     DINMCU, GPIO\n"
207         "     DINSPI, SPI\n"
208         "     HOUTDIAG, GPIO\n"
209         "     HOUTIN, GPIO\n"
210         "     HOUTIFBK, ADC\n"
211         "     ADC, ADC\n"
212         "     LOUT, SPI\n"
213         "     DAC12, SPI\n"
214         "     DAC34, SPI\n"
215         "     DACDREF, SPI\n"
216         "     HBR, SPI\n"
217         "     FRAY1, SPI\n"
218         "     FRAY2, SPI\n"
219         "     MOUTEN, GPIO\n"
220         "     MOUTIN, GPIO\n",
221         CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
222 };
223
224 /** List of commands for port, defined as external */
225 cmd_des_t const *cmd_list_port[] = {
226         &cmd_des_port_val,
227         &cmd_des_port_list,
228         NULL
229 };