2 * Copyright (C) 2012-2015 Czech Technical University in Prague
4 * Created on: 28.2.2013
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
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
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.
33 * Commands for pin controlling
34 * - Printing list of available pins
35 * - Setting and getting value to pins
36 * - Setting and getting pins direction
46 #include "drv/gio_tab.h"
47 #include "cmdproc_utils.h"
50 * @brief Print list of pins
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
57 int cmd_do_pin_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
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);
67 static enum pin_name pin_from_name(const char *pin_name)
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;
78 * @brief Set or get pin value
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
85 int cmd_do_pin_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
93 if (sscanf(p, "%31s ", pinName) != 1)
94 return -CMDERR_BADPAR;
96 enum pin_name pin = pin_from_name(pinName);
97 if (pin == _PIN_INVALID)
98 return -CMDERR_BADPAR;
100 if (param[2] != NULL) { // More parameters = set values
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;;
109 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
111 else { // No more parameters = get values
112 int pin_value = rpp_gio_get(pin);
113 if (pin_value == FAILURE) {
114 return -CMDERR_BADPAR;
116 rpp_sci_printf("pinval%s=%d\n", pinName, pin_value);
122 * @brief Set or get pin direction
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
129 int cmd_do_pin_dir(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
137 if (sscanf(p, "%31s ", pinName) != 1)
138 return -CMDERR_BADPAR;
140 enum pin_name pin = pin_from_name(pinName);
141 if (pin == _PIN_INVALID)
142 return -CMDERR_BADPAR;
144 if (param[2] != NULL) { // More parameters = set values
146 if (sscanf(p, "%u %1s", &val, &spareParams) != 1)
147 return -CMDERR_BADPAR;
148 uint32_t pin_dsc = gio_table[pin].pin_dsc;
150 pin_dsc &= ~GIO_PIN_CONF_DIR_MASK;
151 pin_dsc |= val ? GIO_PIN_CONF_DIR_OUT : GIO_PIN_CONF_DIR_IN;
155 return cmd_opchar_replong(cmd_io, param, val, 0, 0);
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; */
162 rpp_sci_printf("pindir%s=%d FIXME not implemented\n", pinName, pin_dir);
169 /** Command descriptor for pin list */
170 cmd_des_t const cmd_des_pin_list = {
172 "pinlist","Print a list of all defined pins.",
173 "### Command syntax ###\n"
177 "### Description ###\n"
179 "The command prints a list of all defined pins accessible by pinval and\n"
185 " List of all defined pins. Those names can be used by pinval command.\n"
234 CMD_HANDLER(cmd_do_pin_list), (void *)&cmd_list_pin
237 /** Command descriptor for pin get/set value */
238 cmd_des_t const cmd_des_pin_val = {
240 "pinval*","Set or get the pin value",
241 "### Command syntax ###\n"
243 " pinval<NAME> <VAL>\n"
247 "- `<NAME>` is a string identifying the pin\n"
248 "- `<VAL>` can be 0 or 1\n"
250 "### Description ###\n"
252 "This command is sets or gets a value of the particular pin.\n"
254 "The list of valid pin names can be obtained with pinlist command.\n"
258 " --> pinvalGIOB0 1\n"
261 "Sets the GIOB0 pin to 1.\n"
266 "Gets a value of the GIOB0 pin.\n",
267 CMD_HANDLER(cmd_do_pin_val), (void *)&cmd_list_pin
270 /** Command descriptor for pin get/set direction */
271 cmd_des_t const cmd_des_pin_dir = {
273 "pindir*","Set the pin direction",
274 "### Command syntax ###\n"
276 " pindir<NAME> <DIR>\n"
280 "- `<NAME>` is a string identifying the pin\n"
281 "- DIR is be either 0 (input) or 1 (output)\n"
283 "### Description ###\n"
285 "This command is used to set or get direction of the particular pin.\n"
287 "The list of valid pin names can be obtained with pinlist command.\n"
291 " --> pindirGIOB0 1\n"
294 "Sets the GIOB0 pin as output.\n"
299 "Gets the direction of the GIOB0 pin.\n",
300 CMD_HANDLER(cmd_do_pin_dir), (void *)&cmd_list_pin
303 /** List of commands for pin, defined as external */
304 cmd_des_t const *cmd_list_pin[] = {