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[]) {
59 if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_type, &active, &can_wake, &spareParams) >= 2) {
61 } else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_type, &active, &can_wake, &spareParams) >= 1) {
65 return -CMDERR_BADPAR;
67 for (i = pin_min; i <= pin_max; i++) {
68 ret = rpp_din_setup(i, pull_type, active, can_wake);
70 rpp_sci_printf("Pin %d out of range.\n", i);
71 return -CMDERR_BADPAR;
74 rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
75 return -CMDERR_BADPAR;
77 if (rpp_din_update() == FAILURE) {
78 rpp_sci_printf("DIN%d SPI transfer failed.\n", i);
81 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_type, !!active, !!can_wake);
87 * @brief Get value from DIN pin
89 * @param[in] cmd_io Pointer to IO stack
90 * @param[in] des Pointer to command descriptor
91 * @param[in] param Parameters of command
92 * @return 0 when OK or error code
94 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
100 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
101 return -CMDERR_BADPAR;
104 if (rpp_din_update() == FAILURE) {
105 rpp_sci_printf("DIN SPI transfer failed.\n");
109 ret = rpp_din_get(pin, false);
111 rpp_sci_printf("Pin out of range.\n");
112 return -CMDERR_BADPAR;
114 else if (ret == -2) {
115 rpp_sci_printf("Selected pin can not be read with threshold\n");
116 return -CMDERR_BADPAR;
118 rpp_sci_printf("dinget%d =%x\n", pin, ret);
123 * @brief Read DIN diagnostic value
125 * @param[in] cmd_io Pointer to IO stack
126 * @param[in] des Pointer to command descriptor
127 * @param[in] param Parameters of command
128 * @return 0 when OK or error code
130 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
136 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
137 return -CMDERR_BADPAR;
140 if (rpp_din_update() == FAILURE) {
141 rpp_sci_printf("DIN SPI transfer failed.\n");
145 ret = rpp_din_diag(pin);
147 rpp_sci_printf("Pin out of range.\n");
148 return -CMDERR_BADPAR;
150 rpp_sci_printf("dindiag%d =%x\n", pin, ret);
154 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
156 rpp_sci_printf((const char*)
157 "Digital Inputs Test [0-15]:\r\n"
159 rpp_sci_printf((const char*)
160 "===========================================================\r\n"
162 rpp_sci_printf((const char*)
163 " 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"
164 // 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
167 // Calculate wait time in OS ticks
168 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
169 portTickType last_wake_time = xTaskGetTickCount();
172 while(cmd_io->getc(cmd_io) < 0) {
178 // Terminal needs to be at least 47 chars long
179 for(pin = 0; pin < 16; pin++) {
180 rpp_sci_printf((const char*)" %d ", rpp_din_get(pin, FALSE));
182 for(pin = 8; pin < 16; pin++) {
183 rpp_sci_printf((const char*)" %d ", rpp_din_get(pin, TRUE));
185 rpp_sci_printf("\r");
187 vTaskDelayUntil(&last_wake_time, freq_ticks);
190 rpp_sci_printf("\n");
196 /** Command descriptor for din state command */
197 cmd_des_t const cmd_des_din_setup={
199 "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
200 "=== Command syntax ===\n"
202 " dinsetup<PINS> [A [B [C]]]\n"
204 "* PIN is either a number in range 1-16 or a range written as\n"
206 "* A is an optional value - either 1 (pull up/switch to ground) or 0\n"
207 " (pull down/switch to battery). The default is 0.\n"
208 "* B is an optional value - either 0 (active) or 1 (tri-state). The\n"
210 "* C is an optional value - either 0 (wake up and IRQ disabled) or 1\n"
211 " (can wake up, IRQ enable). The default is 1.\n"
213 "=== Description ===\n"
215 "The command setups properties of one or more DIN pins as specified by\n"
216 "<PIN>. Pins 0-7 can be set as pull up (switch to ground) or pull down\n"
217 "(switch to battery), pins 8-15 are hardcoded as switch to ground. All\n"
218 "pins can be set to either tri-state or active state and also can have\n"
219 "wake-up function with IRQ activated or not.\n"
221 "The command always prints the final settings of each set pin as ABC.\n"
222 "The actual configuration cannot be read out of the pin driver.\n"
226 " --> dinsetup1 1 0 0\n"
229 "Sets the DIN1 as switch to ground, active and disables IRQ generation.\n"
234 "Sets the DIN2 as to the default values i.e. switch to battery,\n"
235 "tri-state, wake-up/IRQ enabled.\n"
237 " --> dinsetup0-7 1 1 1\n"
247 "Sets the DIN0 through DIN7 as switch to ground, tri-state, wake-up/IRQ\n"
249 CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
252 /** Command descriptor for din state command */
253 cmd_des_t const cmd_des_din_get={
255 "dinget*","Read the open/close status of a DIN pin (with the default treshold)", /* TODO add a command for reading with a treshold */
256 "=== Command syntax ===\n"
260 "where PIN is a number in range 0-15\n"
262 "=== Description ===\n"
264 "The command reads and prints the status of the DIN pin. Value 0 means\n"
265 "switch is open, value 1 means switch is closed. The mapping between\n"
266 "the DIN voltage and the open/close status depends on the setup of the\n"
267 "pin (see dinsetup command).\n"
274 "DIN1 is in open state.\n",
275 CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
278 /** Command descriptor for din state command */
279 cmd_des_t const cmd_des_din_diag={
281 "dindiag*","Read diagnostic value from DIN pin",
282 "=== Command syntax ===\n"
286 "where <PIN> is in range 1-16\n"
288 "=== Description ===\n"
290 "The command reads and prints the value of diagnostic bit corresponding\n"
291 "to a DIN pin. TODO check what the value actually mean.\n"
298 "Prints value of the diagnostic bit of DIN1.\n",
299 CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
302 /** Command descriptor for din state command */
303 cmd_des_t const cmd_des_din_watch={
305 "dinwatch","Watch status of all DIN pins",
306 "=== Command syntax ===\n"
310 "=== Description ===\n"
312 "The command reads and prints the status of DIN pins every 100\n"
313 "milliseconds. Columns 0-15 correspond to open/close status of DIN pins\n"
314 "with the default threshold of 4 V, columns A-H represent the logical\n"
315 "values of pins DIN8-15 when read with programmable threshold.\n"
316 "Pin status (open=0, close=1) depends on the pin setup that can be\n"
317 "changed with dinsetup command, programmable threshold can be set with\n"
320 "Press any key to end this command.\n"
325 " Digital Inputs Test [0-15]:\n"
326 " ===========================================================\n"
327 " 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"
328 " 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",
329 CMD_HANDLER(cmd_do_din_watch), (void *)&cmd_list_din
332 /** List of commands for din, defined as external */
333 cmd_des_t const *cmd_list_din[]={