]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_pin.c
Convert doc from MediaWiki syntax to Markdown
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_pin.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_pin.c
23  *
24  * Abstract:
25  *              Commands for pin controlling
26  *              - Printing list of available pins
27  *              - Setting and getting value to pins
28  *              - Setting and getting pins direction
29  */
30
31 #include "cmd_pin.h"
32 #include "stdio.h"
33 #include "string.h"
34
35 #ifndef DOCGEN
36
37 #include "rpp/rpp.h"
38 #include "hal/hal.h"
39 #include "cmdproc_utils.h"
40
41 /**
42  * @brief       Print list of pins
43  *
44  * @param[in]   cmd_io  Pointer to IO stack
45  * @param[in]   des             Pointer to command descriptor
46  * @param[in]   param   Parameters of command
47  * @return      0 when OK or error code
48  */
49 int cmd_do_pin_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
50         uint32_t i;
51         rpp_sci_printf("List of all defined pins. Those names can be used by pinval command.\r\n");
52         for (i = 0; i < MAX_PIN_CNT; i++) {
53                 if (pin_map[i].pin_name == PIN_NAME_UNUSED) continue;
54                 rpp_sci_printf(pin_map[i].pin_name);
55                 rpp_sci_printf("\r\n");
56         }
57         return 1;
58 }
59
60 /**
61  * @brief       Set or get pin value
62  *
63  * @param[in]   cmd_io  Pointer to IO stack
64  * @param[in]   des             Pointer to command descriptor
65  * @param[in]   param   Parameters of command
66  * @return      0 when OK or error code
67  */
68 int cmd_do_pin_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
69           char *p;
70           long val;
71           uint32_t* desc;
72           char spareParams;
73           char pinName[32];
74
75           p = param[1];
76           if (sscanf(p, "%31s ", pinName) != 1) {
77                   return -CMDERR_BADPAR;
78           }
79
80           if((desc = hal_gpio_pin_get_dsc(pinName, -1))==NULL) return -CMDERR_BADREG;
81
82           if(param[2] != NULL){         // More parameters = set values
83                 p=param[2];
84                 if (sscanf(p, "%d %1s", &val, &spareParams) != 1) {
85                         return -CMDERR_BADPAR;
86                 }
87             if (val != 0 && val != 1)
88                       return -CMDERR_BADPAR;
89             hal_gpio_pin_set_value(*desc, (uint32_t) val);
90             return cmd_opchar_replong(cmd_io, param, val, 0, 0);;
91           }
92           else{ // No more parameters = get values
93                 uint32_t pin_value = hal_gpio_pin_get_value(*desc);
94                 rpp_sci_printf("pinval%s=%d\n", pinName, pin_value);
95                 return 0;
96           }
97 }
98
99 /**
100  * @brief       Set or get pin direction
101  *
102  * @param[in]   cmd_io  Pointer to IO stack
103  * @param[in]   des             Pointer to command descriptor
104  * @param[in]   param   Parameters of command
105  * @return      0 when OK or error code
106  */
107 int cmd_do_pin_dir(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
108           char *p;
109           long val;
110           uint32_t* desc;
111           char spareParams;
112           char pinName[32];
113
114           p = param[1];
115           if (sscanf(p, "%31s ", pinName) != 1) {
116                   return -CMDERR_BADPAR;
117           }
118
119           if((desc = hal_gpio_pin_get_dsc(pinName, -1))==NULL) return -CMDERR_BADREG;
120
121           if(param[2] != NULL){         // More parameters = set values
122                 p=param[2];
123                 if (sscanf(p, "%d %1s", &val, &spareParams) != 1) {
124                         return -CMDERR_BADPAR;
125                 }
126             if (val == 1) {
127                 *desc |= PORT_CONF_SET_DIR;
128                 *desc |= PORT_CONF_DIR_OUT;
129             }
130             else if (val == 0) {
131                 *desc &= (~PORT_CONF_DIR_OUT);
132                         *desc |= PORT_CONF_SET_DIR;
133             }
134             else {
135                       return -CMDERR_BADPAR;
136             }
137
138             hal_gpio_pin_conf(*desc);
139             return cmd_opchar_replong(cmd_io, param, val, 0, 0);
140           }
141           else{ // No more parameters = get values
142                 uint32_t pin_dir = hal_gpio_pin_get_direction(*desc);
143                 rpp_sci_printf("pindir%s=%d\n", pinName, pin_dir);
144                 return 0;
145           }
146 }
147
148 #endif  /* DOCGEN */
149
150 /** Command descriptor for pin list */
151 cmd_des_t const cmd_des_pin_list = {
152     0, 0,
153     "pinlist","Print a list of all defined pins.",
154     "### Command syntax ###\n"
155     "\n"
156     "    pinlist\n"
157     "\n"
158     "### Description ###\n"
159     "\n"
160     "The command prints a list of all defined pins accessible by pinval and\n"
161     "pindir commands.\n"
162     "\n"
163     "### Example ###\n"
164     "\n"
165     "    --> pinlist\n"
166     "    List of all defined pins. Those names can be used by pinval command.\n"
167     "    FANCTRL\n"
168     "    ETHRST\n"
169     "    VBAT1EN\n"
170     "    VBAT2EN\n"
171     "    VBAT3EN\n"
172     "    VBATEN\n"
173     "    SPICSA\n"
174     "    SPICSB\n"
175     "    MOUT1EN\n"
176     "    MOUT2EN\n"
177     "    CANNSTB\n"
178     "    CANEN\n"
179     "    LIN2NSLP\n"
180     "    LIN1NSLP\n"
181     "    DININT\n"
182     "    DIN8\n"
183     "    DIN9\n"
184     "    DIN10\n"
185     "    DIN11\n"
186     "    DIN12\n"
187     "    DIN13\n"
188     "    DIN14\n"
189     "    DIN15\n"
190     "    MOUT6EN\n"
191     "    MOUT5EN\n"
192     "    MOUT6IN\n"
193     "    MOUT5IN\n"
194     "    MOUT4EN\n"
195     "    MOUT3EN\n"
196     "    MOUT4IN\n"
197     "    MOUT3IN\n"
198     "    HBREN\n"
199     "    HBRDIR\n"
200     "    HBRPWM\n"
201     "    MOUT1IN\n"
202     "    MOUT2IN\n"
203     "    HOUT1IN\n"
204     "    HOUT1DIAG\n"
205     "    HOUT2IN\n"
206     "    HOUT2DIAG\n"
207     "    HOUT3IN\n"
208     "    HOUT3DIAG\n"
209     "    HOUT4IN\n"
210     "    HOUT4DIAG\n"
211     "    HOUT5IN\n"
212     "    HOUT5DIAG\n"
213     "    HOUT6IN\n"
214     "    HOUT6DIAG\n",
215     CMD_HANDLER(cmd_do_pin_list), (void *)&cmd_list_pin
216 };
217
218 /** Command descriptor for pin get/set value */
219 cmd_des_t const cmd_des_pin_val={
220         0, 0,
221         "pinval*","Set or get the pin value",
222         "### Command syntax ###\n"
223         "\n"
224         "    pinval<NAME> <VAL>\n"
225         "    pinval<NAME>\n"
226         "where\n"
227         "\n"
228         "- `<NAME>` is a string identifying the pin\n"
229         "- `<VAL>` can be 0 or 1\n"
230         "\n"
231         "### Description ###\n"
232         "\n"
233         "This command is sets or gets a value of the particular pin.\n"
234         "\n"
235         "The list of valid pin names can be obtained with pinlist command.\n"
236         "\n"
237         "Most of the pins are accessible indirectly via other highlevel\n"
238         "commands. HBR_EN is, for example, controlled by the hbrenable command.\n"
239         "This command serves as supplement to highlevel commands for testing\n"
240         "purpose.\n"
241         "\n"
242         "### Example ###\n"
243         "\n"
244         "    --> pinvalHBREN 1\n"
245         "    pinvalHBREN=1\n"
246         "\n"
247         "Sets the HBR_EN pin to 1.\n"
248         "\n"
249         "    --> pinvalHBREN\n"
250         "    pinvalHBREN=1\n"
251         "\n"
252         "Gets a value of the HBR_EN pin.\n",
253         CMD_HANDLER(cmd_do_pin_val), (void *)&cmd_list_pin
254 };
255
256 /** Command descriptor for pin get/set direction */
257 cmd_des_t const cmd_des_pin_dir={
258         0, 0,
259         "pindir*","Set the pin direction",
260         "### Command syntax ###\n"
261         "\n"
262         "    pindir<NAME> <DIR>\n"
263         "    pindir<NAME>\n"
264         "where\n"
265         "\n"
266         "- `<NAME>` is a string identifying the pin\n"
267         "- DIR is be either 0 (input) or 1 (output)\n"
268         "\n"
269         "### Description ###\n"
270         "\n"
271         "This command is used to set or get direction of the particular pin.\n"
272         "\n"
273         "The list of valid pin names can be obtained with pinlist command.\n"
274         "\n"
275         "Most of the pins are accessible indirectly via other highlevel\n"
276         "commands HBR_EN is, for example, controlled by the hbrenable command.\n"
277         "This command serves as supplement to highlevel commands for testing\n"
278         "purpose.\n"
279         "\n"
280         "### Example ###\n"
281         "\n"
282         "    --> pindirHBREN 1\n"
283         "    pindirHBREN=1\n"
284         "\n"
285         "Sets the HBR_EN pin as output.\n"
286         "\n"
287         "    --> pindirHBREN\n"
288         "    pindirHBREN=1\n"
289         "\n"
290         "Gets the direction of the HBR_EN pin.\n",
291         CMD_HANDLER(cmd_do_pin_dir), (void *)&cmd_list_pin
292 };
293
294 /** List of commands for pin, defined as external */
295 cmd_des_t const *cmd_list_pin[]={
296   &cmd_des_pin_list,
297   &cmd_des_pin_val,
298   &cmd_des_pin_dir,
299   NULL
300 };