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