]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_pin.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_pin.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  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  *
30  * File : cmd_pin.c
31  *
32  * Abstract:
33  *      Commands for pin controlling
34  *          - Printing list of available pins
35  *          - Setting and getting value to pins
36  *          - Setting and getting pins direction
37  */
38
39 #include "cmd_pin.h"
40 #include "stdio.h"
41 #include "string.h"
42
43 #ifndef DOCGEN
44
45 #include "rpp/rpp.h"
46 #include "drv/gio_tab.h"
47 #include "cmdproc_utils.h"
48
49 /**
50  * @brief       Print list of pins
51  *
52  * @param[in]   cmd_io  Pointer to IO stack
53  * @param[in]   des             Pointer to command descriptor
54  * @param[in]   param   Parameters of command
55  * @return      0 when OK or error code
56  */
57 int cmd_do_pin_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
58 {
59         uint32_t i;
60
61         rpp_sci_printf("List of all defined pins. Those names can be used by pinval command.\r\n");
62         for (i = 0; i < ARRAY_SIZE(gio_table); i++)
63                 rpp_sci_printf("%s\r\n", gio_table[i].pin_name);
64         return 1;
65 }
66
67 static enum pin_name pin_from_name(const char *pin_name)
68 {
69         uint32_t i;
70
71         for (i = 0; i < ARRAY_SIZE(gio_table); i++)
72                 if (strcmp(pin_name, gio_table[i].pin_name) == 0)
73                         return (enum pin_name)i;
74         return _PIN_INVALID;
75 }
76
77 /**
78  * @brief       Set or get pin value
79  *
80  * @param[in]   cmd_io  Pointer to IO stack
81  * @param[in]   des             Pointer to command descriptor
82  * @param[in]   param   Parameters of command
83  * @return      0 when OK or error code
84  */
85 int cmd_do_pin_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
86 {
87         char *p;
88         long val;
89         char spareParams;
90         char pinName[32];
91
92         p = param[1];
93         if (sscanf(p, "%31s ", pinName) != 1)
94                 return -CMDERR_BADPAR;
95
96         enum pin_name pin = pin_from_name(pinName);
97         if (pin == _PIN_INVALID)
98                 return -CMDERR_BADPAR;
99
100         if (param[2] != NULL) {     // More parameters = set values
101                 p = param[2];
102                 if (sscanf(p, "%d %1s", &val, &spareParams) != 1)
103                         return -CMDERR_BADPAR;
104                 if (val != 0 && val != 1)
105                         return -CMDERR_BADPAR;
106                 if (rpp_gio_set(pin, val) == FAILURE) {
107                         return -CMDERR_BADPAR;;
108                 }
109                 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
110         }
111         else {  // No more parameters = get values
112                 int pin_value = rpp_gio_get(pin);
113                 if (pin_value == FAILURE) {
114                         return -CMDERR_BADPAR;
115                 }
116                 rpp_sci_printf("pinval%s=%d\n", pinName, pin_value);
117                 return 0;
118         }
119 }
120
121 /**
122  * @brief       Set or get pin direction
123  *
124  * @param[in]   cmd_io  Pointer to IO stack
125  * @param[in]   des             Pointer to command descriptor
126  * @param[in]   param   Parameters of command
127  * @return      0 when OK or error code
128  */
129 int cmd_do_pin_dir(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
130 {
131         char *p;
132         uint32_t val;
133         char spareParams;
134         char pinName[32];
135
136         p = param[1];
137         if (sscanf(p, "%31s ", pinName) != 1)
138                 return -CMDERR_BADPAR;
139
140         enum pin_name pin = pin_from_name(pinName);
141         if (pin == _PIN_INVALID)
142                 return -CMDERR_BADPAR;
143
144         if (param[2] != NULL) {     // More parameters = set values
145                 p = param[2];
146                 if (sscanf(p, "%u %1s", &val, &spareParams) != 1)
147                         return -CMDERR_BADPAR;
148                 uint32_t pin_dsc = gio_table[pin].pin_dsc;
149
150                 pin_dsc &= ~GIO_PIN_CONF_DIR_MASK;
151                 pin_dsc |= val ? GIO_PIN_CONF_DIR_OUT : GIO_PIN_CONF_DIR_IN;
152
153                 gio_setup(pin_dsc);
154
155                 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
156         }
157         else {  // No more parameters = get values
158                 int32_t pin_dir = 0/* rpp_gio_is_dir_output(pinName) */;
159 /*              if (pin_dir == FAILURE) { */
160 /*                      return -CMDERR_BADPAR; */
161 /*              } */
162                 rpp_sci_printf("pindir%s=%d FIXME not implemented\n", pinName, pin_dir);
163                 return 0;
164         }
165 }
166
167 #endif  /* DOCGEN */
168
169 /** Command descriptor for pin list */
170 cmd_des_t const cmd_des_pin_list = {
171         0, 0,
172         "pinlist","Print a list of all defined pins.",
173         "### Command syntax ###\n"
174         "\n"
175         "    pinlist\n"
176         "\n"
177         "### Description ###\n"
178         "\n"
179         "The command prints a list of all defined pins accessible by pinval and\n"
180         "pindir commands.\n"
181         "\n"
182         "### Example ###\n"
183         "\n"
184         "    --> pinlist\n"
185         "    List of all defined pins. Those names can be used by pinval command.\n"
186         "    GIOA0\n"
187         "    GIOA1\n"
188         "    GIOA2\n"
189         "    GIOA3\n"
190         "    GIOA4\n"
191         "    GIOA5\n"
192         "    GIOA6\n"
193         "    GIOA7\n"
194         "    GIOB0\n"
195         "    GIOB1\n"
196         "    GIOB2\n"
197         "    GIOB3\n"
198         "    GIOB4\n"
199         "    GIOB5\n"
200         "    GIOB6\n"
201         "    GIOB7\n"
202         "    NHET10\n"
203         "    NHET11\n"
204         "    NHET12\n"
205         "    NHET13\n"
206         "    NHET14\n"
207         "    NHET15\n"
208         "    NHET16\n"
209         "    NHET17\n"
210         "    NHET18\n"
211         "    NHET19\n"
212         "    NHET110\n"
213         "    NHET111\n"
214         "    NHET112\n"
215         "    NHET113\n"
216         "    NHET114\n"
217         "    NHET115\n"
218         "    NHET116\n"
219         "    NHET117\n"
220         "    NHET118\n"
221         "    NHET119\n"
222         "    NHET120\n"
223         "    NHET121\n"
224         "    NHET122\n"
225         "    NHET123\n"
226         "    NHET124\n"
227         "    NHET125\n"
228         "    NHET126\n"
229         "    NHET127\n"
230         "    NHET128\n"
231         "    NHET129\n"
232         "    NHET130\n"
233         "    NHET131\n",
234         CMD_HANDLER(cmd_do_pin_list), (void *)&cmd_list_pin
235 };
236
237 /** Command descriptor for pin get/set value */
238 cmd_des_t const cmd_des_pin_val = {
239         0, 0,
240         "pinval*","Set or get the pin value",
241         "### Command syntax ###\n"
242         "\n"
243         "    pinval<NAME> <VAL>\n"
244         "    pinval<NAME>\n"
245         "where\n"
246         "\n"
247         "- `<NAME>` is a string identifying the pin\n"
248         "- `<VAL>` can be 0 or 1\n"
249         "\n"
250         "### Description ###\n"
251         "\n"
252         "This command is sets or gets a value of the particular pin.\n"
253         "\n"
254         "The list of valid pin names can be obtained with pinlist command.\n"
255         "\n"
256         "### Example ###\n"
257         "\n"
258         "    --> pinvalGIOB0 1\n"
259         "    pinvalGIOB0=1\n"
260         "\n"
261         "Sets the GIOB0 pin to 1.\n"
262         "\n"
263         "    --> pinvalGIOB0\n"
264         "    pinvalGIOB0=1\n"
265         "\n"
266         "Gets a value of the GIOB0 pin.\n",
267         CMD_HANDLER(cmd_do_pin_val), (void *)&cmd_list_pin
268 };
269
270 /** Command descriptor for pin get/set direction */
271 cmd_des_t const cmd_des_pin_dir = {
272         0, 0,
273         "pindir*","Set the pin direction",
274         "### Command syntax ###\n"
275         "\n"
276         "    pindir<NAME> <DIR>\n"
277         "    pindir<NAME>\n"
278         "where\n"
279         "\n"
280         "- `<NAME>` is a string identifying the pin\n"
281         "- DIR is be either 0 (input) or 1 (output)\n"
282         "\n"
283         "### Description ###\n"
284         "\n"
285         "This command is used to set or get direction of the particular pin.\n"
286         "\n"
287         "The list of valid pin names can be obtained with pinlist command.\n"
288         "\n"
289         "### Example ###\n"
290         "\n"
291         "    --> pindirGIOB0 1\n"
292         "    pindirGIOB0=1\n"
293         "\n"
294         "Sets the GIOB0 pin as output.\n"
295         "\n"
296         "    --> pindirGIOB0\n"
297         "    pindirGIOB0=1\n"
298         "\n"
299         "Gets the direction of the GIOB0 pin.\n",
300         CMD_HANDLER(cmd_do_pin_dir), (void *)&cmd_list_pin
301 };
302
303 /** List of commands for pin, defined as external */
304 cmd_des_t const *cmd_list_pin[] = {
305         &cmd_des_pin_list,
306         &cmd_des_pin_val,
307         &cmd_des_pin_dir,
308         NULL
309 };