2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
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.
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/>.
25 * This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
34 #include "cmdproc_utils.h"
40 * @brief Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
42 * @param[in] cmd_io Pointer to IO stack
43 * @param[in] des Pointer to command descriptor
44 * @param[in] param Parameters of command
45 * @return 0 when OK or error code
47 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
60 if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_up, &active, &can_wake, &spareParams) >= 2) {
63 else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_up, &active, &can_wake, &spareParams) >= 1)
67 return -CMDERR_BADPAR;
69 for (i = pin_min; i <= pin_max; i++) {
70 ret = rpp_din_setup(i, pull_up, active, can_wake);
72 rpp_sci_printf("Pin %d out of range.\n", i);
73 return -CMDERR_BADPAR;
76 rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
77 return -CMDERR_BADPAR;
79 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_up, !!active, !!can_wake);
81 if (rpp_din_update() == FAILURE) {
82 rpp_sci_printf("DIN SPI transfer failed.\n");
89 * @brief Get value from DIN pin
91 * @param[in] cmd_io Pointer to IO stack
92 * @param[in] des Pointer to command descriptor
93 * @param[in] param Parameters of command
94 * @return 0 when OK or error code
96 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
103 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
104 return -CMDERR_BADPAR;
106 if (rpp_din_update() == FAILURE) {
107 rpp_sci_printf("DIN SPI transfer failed.\n");
111 ret = rpp_din_get(pin);
113 rpp_sci_printf("Pin out of range.\n");
114 return -CMDERR_BADPAR;
116 rpp_sci_printf("dinget%d =%x\n", pin, ret);
121 * @brief Read DIN diagnostic value
123 * @param[in] cmd_io Pointer to IO stack
124 * @param[in] des Pointer to command descriptor
125 * @param[in] param Parameters of command
126 * @return 0 when OK or error code
128 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
135 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
136 return -CMDERR_BADPAR;
138 if (rpp_din_update() == FAILURE) {
139 rpp_sci_printf("DIN SPI transfer failed.\n");
143 ret = rpp_din_diag(pin);
145 rpp_sci_printf("Pin out of range.\n");
146 return -CMDERR_BADPAR;
148 rpp_sci_printf("dindiag%d =%x\n", pin, ret);
152 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
154 rpp_sci_printf((const char*)
155 "Digital Inputs Test [0-15]:\r\n"
157 rpp_sci_printf((const char*)
158 "===========================================================\r\n"
160 rpp_sci_printf((const char*)
161 " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 A B C D E F G H\r\n"
162 // 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
165 // Calculate wait time in OS ticks
166 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
167 portTickType last_wake_time = xTaskGetTickCount();
170 while (cmd_io->getc(cmd_io) < 0) {
176 // Terminal needs to be at least 47 chars long
177 for (pin = 0; pin < 16; pin++) {
178 rpp_sci_printf((const char*)" %d ", rpp_din_get(pin));
180 for (pin = 8; pin < 16; pin++) {
181 rpp_sci_printf((const char*)" %d ", rpp_din_get_tr(pin));
183 rpp_sci_printf("\r");
185 vTaskDelayUntil(&last_wake_time, freq_ticks);
188 rpp_sci_printf("\n");
194 /** Command descriptor for din state command */
195 cmd_des_t const cmd_des_din_setup = {
197 "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
198 "### Command syntax ###\n"
200 " dinsetup<PINS> [A [B [C]]]\n"
203 "- PINS is either a number in range 1-16 or a range written as `<min>-<max>`\n"
204 "- A is an optional value - either 0 (pull down/switch to battery) or 1 (pull up/switch to ground). The default is 1.\n"
205 "- B is an optional value - either 0 (tri-state) or 1 (active). The default is 0.\n"
206 "- C is an optional value - either 0 (wake up and IRQ disabled) or 1 (wake up and IRQ disabled enabled). The default is 1.\n"
208 "### Description ###\n"
210 "The command setups properties of one or more DIN pins as specified by\n"
211 "`<PIN>`. Pins 0-7 can be set as pull up (switch to ground) or pull down\n"
212 "(switch to battery), pins 8-15 are hardcoded as switch to ground. All\n"
213 "pins can be set to either tri-state or active state and also can have\n"
214 "wake-up function with IRQ activated or not.\n"
216 "The command always prints the final settings of each set pin as ABC.\n"
217 "The actual configuration cannot be read out of the pin driver.\n"
221 " --> dinsetup1 1 0 0\n"
224 "Sets the DIN1 as switch to ground, active and disables IRQ generation.\n"
229 "Sets the DIN2 as to the default values i.e. switch to battery,\n"
230 "tri-state, wake-up/IRQ enabled.\n"
232 " --> dinsetup0-7 1 1 1\n"
242 "Sets the DIN0 through DIN7 as switch to ground, tri-state, wake-up/IRQ\n"
244 CMD_HANDLER(cmd_do_din_setup), (void*)&cmd_list_din
247 /** Command descriptor for din state command */
248 cmd_des_t const cmd_des_din_get = {
250 "dinget*","Read the open/close status of a DIN pin (with the default treshold)", /* TODO add a command for reading with a treshold */
251 "### Command syntax ###\n"
255 "where PIN is a number in range 0-15\n"
257 "### Description ###\n"
259 "The command reads and prints the status of the DIN pin. Value 0 means\n"
260 "switch is open, value 1 means switch is closed. The mapping between\n"
261 "the DIN voltage and the open/close status depends on the setup of the\n"
262 "pin (see dinsetup command).\n"
269 "DIN1 is in open state.\n",
270 CMD_HANDLER(cmd_do_din_get), (void*)&cmd_list_din
273 /** Command descriptor for din state command */
274 cmd_des_t const cmd_des_din_diag = {
276 "dindiag*","Read diagnostic value from DIN pin",
277 "### Command syntax ###\n"
281 "where `<PIN>` is in range 1-16\n"
283 "### Description ###\n"
285 "The command reads and prints the value of diagnostic bit corresponding\n"
286 "to a DIN pin. TODO check what the value actually mean.\n"
293 "Prints value of the diagnostic bit of DIN1.\n",
294 CMD_HANDLER(cmd_do_din_diag), (void*)&cmd_list_din
297 /** Command descriptor for din state command */
298 cmd_des_t const cmd_des_din_watch = {
300 "dinwatch","Watch status of all DIN pins",
301 "### Command syntax ###\n"
305 "### Description ###\n"
307 "The command reads and prints the status of DIN pins every 100\n"
308 "milliseconds. Columns 0-15 correspond to open/close status of DIN pins\n"
309 "with the default threshold of 4 V, columns A-H represent the logical\n"
310 "values of pins DIN8-15 when read with programmable threshold.\n"
311 "Pin status (open=0, close=1) depends on the pin setup that can be\n"
312 "changed with dinsetup command, programmable threshold can be set with\n"
315 "Press any key to end this command.\n"
320 " Digital Inputs Test [0-15]:\n"
321 " ===========================================================\n"
322 " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 A B C D E F G H\n"
323 " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1\n",
324 CMD_HANDLER(cmd_do_din_watch), (void*)&cmd_list_din
327 /** List of commands for din, defined as external */
328 cmd_des_t const *cmd_list_din[] = {
331 /* &cmd_des_din_diag, */ // Implementation in lower layers not finished