]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_pin.c
Update library
[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  * 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_pin.c
15  *
16  * Abstract:
17  *      Commands for pin controlling
18  *          - Printing list of available pins
19  *          - Setting and getting value to pins
20  *          - Setting and getting pins direction
21  */
22
23 #include "cmd_pin.h"
24 #include "stdio.h"
25 #include "string.h"
26
27 #ifndef DOCGEN
28
29 #include "rpp/rpp.h"
30 #include "drv/digital_io_def.h"
31 #include "cmdproc_utils.h"
32
33 /**
34  * @brief       Print list of pins
35  *
36  * @param[in]   cmd_io  Pointer to IO stack
37  * @param[in]   des             Pointer to command descriptor
38  * @param[in]   param   Parameters of command
39  * @return      0 when OK or error code
40  */
41 int cmd_do_pin_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
42 {
43         uint32_t i;
44
45         rpp_sci_printf("List of all defined pins. Those names can be used by pinval command.\r\n");
46         uint32_t pin_cnt = rpp_gio_get_pin_cnt();
47         const char* pin_names[128];
48         rpp_gio_get_pin_names(pin_names, pin_cnt);
49         for (i = 0; i < pin_cnt; i++) {
50                 if (strcmp(pin_names[i], DIO_PIN_NAME_UNUSED) == 0) continue;
51                 rpp_sci_printf(pin_names[i]);
52                 rpp_sci_printf("\r\n");
53         }
54         return 1;
55 }
56
57 /**
58  * @brief       Set or get pin value
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_pin_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
66 {
67         char *p;
68         long val;
69         char spareParams;
70         char pinName[32];
71
72         p = param[1];
73         if (sscanf(p, "%31s ", pinName) != 1)
74                 return -CMDERR_BADPAR;
75
76
77         if (param[2] != NULL) {     // More parameters = set values
78                 p = param[2];
79                 if (sscanf(p, "%d %1s", &val, &spareParams) != 1)
80                         return -CMDERR_BADPAR;
81                 if (val != 0 && val != 1)
82                         return -CMDERR_BADPAR;
83                 if (rpp_gio_set_val(pinName, val) == FAILURE) {
84                         return -CMDERR_BADPAR;;
85                 }
86                 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
87         }
88         else {  // No more parameters = get values
89                 int32_t pin_value = rpp_gio_get_val(pinName);
90                 if (pin_value == FAILURE) {
91                         return -CMDERR_BADPAR;
92                 }
93                 rpp_sci_printf("pinval%s=%d\n", pinName, pin_value);
94                 return 0;
95         }
96 }
97
98 /**
99  * @brief       Set or get pin direction
100  *
101  * @param[in]   cmd_io  Pointer to IO stack
102  * @param[in]   des             Pointer to command descriptor
103  * @param[in]   param   Parameters of command
104  * @return      0 when OK or error code
105  */
106 int cmd_do_pin_dir(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
107 {
108         char *p;
109         uint32_t val;
110         char spareParams;
111         char pinName[32];
112
113         p = param[1];
114         if (sscanf(p, "%31s ", pinName) != 1)
115                 return -CMDERR_BADPAR;
116
117         if (param[2] != NULL) {     // More parameters = set values
118                 p = param[2];
119                 if (sscanf(p, "%u %1s", &val, &spareParams) != 1)
120                         return -CMDERR_BADPAR;
121                 if (val == 0) {
122                         if (rpp_gio_set_input(pinName) == FAILURE) {
123                                 return -CMDERR_BADPAR;
124                         }
125                 }
126                 else if (val == 1) {
127                         if (rpp_gio_set_output(pinName, 0) == FAILURE) {
128                                 return -CMDERR_BADPAR;
129                         }
130                 }
131                 else {
132                         return -CMDERR_BADPAR;
133                 }
134
135                 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
136         }
137         else {  // No more parameters = get values
138                 int32_t pin_dir = rpp_gio_is_dir_output(pinName);
139                 if (pin_dir == FAILURE) {
140                         return -CMDERR_BADPAR;
141                 }
142                 rpp_sci_printf("pindir%s=%d\n", pinName, pin_dir);
143                 return 0;
144         }
145 }
146
147 #endif  /* DOCGEN */
148
149 /** Command descriptor for pin list */
150 cmd_des_t const cmd_des_pin_list = {
151         0, 0,
152         "pinlist","Print a list of all defined pins.",
153         "### Command syntax ###\n"
154         "\n"
155         "    pinlist\n"
156         "\n"
157         "### Description ###\n"
158         "\n"
159         "The command prints a list of all defined pins accessible by pinval and\n"
160         "pindir commands.\n"
161         "\n"
162         "### Example ###\n"
163         "\n"
164         "    --> pinlist\n"
165         "    List of all defined pins. Those names can be used by pinval command.\n"
166         "    GIOA0\n"
167         "    GIOA1\n"
168         "    GIOA2\n"
169         "    GIOA3\n"
170         "    GIOA4\n"
171         "    GIOA5\n"
172         "    GIOA6\n"
173         "    GIOA7\n"
174         "    GIOB0\n"
175         "    GIOB1\n"
176         "    GIOB2\n"
177         "    GIOB3\n"
178         "    GIOB4\n"
179         "    GIOB5\n"
180         "    GIOB6\n"
181         "    GIOB7\n"
182         "    NHET10\n"
183         "    NHET11\n"
184         "    NHET12\n"
185         "    NHET13\n"
186         "    NHET14\n"
187         "    NHET15\n"
188         "    NHET16\n"
189         "    NHET17\n"
190         "    NHET18\n"
191         "    NHET19\n"
192         "    NHET110\n"
193         "    NHET111\n"
194         "    NHET112\n"
195         "    NHET113\n"
196         "    NHET114\n"
197         "    NHET115\n"
198         "    NHET116\n"
199         "    NHET117\n"
200         "    NHET118\n"
201         "    NHET119\n"
202         "    NHET120\n"
203         "    NHET121\n"
204         "    NHET122\n"
205         "    NHET123\n"
206         "    NHET124\n"
207         "    NHET125\n"
208         "    NHET126\n"
209         "    NHET127\n"
210         "    NHET128\n"
211         "    NHET129\n"
212         "    NHET130\n"
213         "    NHET131\n",
214         CMD_HANDLER(cmd_do_pin_list), (void *)&cmd_list_pin
215 };
216
217 /** Command descriptor for pin get/set value */
218 cmd_des_t const cmd_des_pin_val = {
219         0, 0,
220         "pinval*","Set or get the pin value",
221         "### Command syntax ###\n"
222         "\n"
223         "    pinval<NAME> <VAL>\n"
224         "    pinval<NAME>\n"
225         "where\n"
226         "\n"
227         "- `<NAME>` is a string identifying the pin\n"
228         "- `<VAL>` can be 0 or 1\n"
229         "\n"
230         "### Description ###\n"
231         "\n"
232         "This command is sets or gets a value of the particular pin.\n"
233         "\n"
234         "The list of valid pin names can be obtained with pinlist command.\n"
235         "\n"
236         "### Example ###\n"
237         "\n"
238         "    --> pinvalGIOB0 1\n"
239         "    pinvalGIOB0=1\n"
240         "\n"
241         "Sets the GIOB0 pin to 1.\n"
242         "\n"
243         "    --> pinvalGIOB0\n"
244         "    pinvalGIOB0=1\n"
245         "\n"
246         "Gets a value of the GIOB0 pin.\n",
247         CMD_HANDLER(cmd_do_pin_val), (void *)&cmd_list_pin
248 };
249
250 /** Command descriptor for pin get/set direction */
251 cmd_des_t const cmd_des_pin_dir = {
252         0, 0,
253         "pindir*","Set the pin direction",
254         "### Command syntax ###\n"
255         "\n"
256         "    pindir<NAME> <DIR>\n"
257         "    pindir<NAME>\n"
258         "where\n"
259         "\n"
260         "- `<NAME>` is a string identifying the pin\n"
261         "- DIR is be either 0 (input) or 1 (output)\n"
262         "\n"
263         "### Description ###\n"
264         "\n"
265         "This command is used to set or get direction of the particular pin.\n"
266         "\n"
267         "The list of valid pin names can be obtained with pinlist command.\n"
268         "\n"
269         "### Example ###\n"
270         "\n"
271         "    --> pindirGIOB0 1\n"
272         "    pindirGIOB0=1\n"
273         "\n"
274         "Sets the GIOB0 pin as output.\n"
275         "\n"
276         "    --> pindirGIOB0\n"
277         "    pindirGIOB0=1\n"
278         "\n"
279         "Gets the direction of the GIOB0 pin.\n",
280         CMD_HANDLER(cmd_do_pin_dir), (void *)&cmd_list_pin
281 };
282
283 /** List of commands for pin, defined as external */
284 cmd_des_t const *cmd_list_pin[] = {
285         &cmd_des_pin_list,
286         &cmd_des_pin_val,
287         &cmd_des_pin_dir,
288         NULL
289 };