2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
17 * This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
26 #include "cmdproc_utils.h"
30 * @brief Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
32 * @param[in] cmd_io Pointer to IO stack
33 * @param[in] des Pointer to command descriptor
34 * @param[in] param Parameters of command
35 * @return 0 when OK or error code
37 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
50 if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_up, &active, &can_wake, &spareParams) >= 2) {
53 else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_up, &active, &can_wake, &spareParams) >= 1)
57 return -CMDERR_BADPAR;
59 for (i = pin_min; i <= pin_max; i++) {
60 ret = rpp_din_setup(i, pull_up, active, can_wake);
62 rpp_sci_printf("Pin %d out of range.\n", i);
63 return -CMDERR_BADPAR;
66 rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
67 return -CMDERR_BADPAR;
69 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_up, !!active, !!can_wake);
71 if (rpp_din_update() == FAILURE) {
72 rpp_sci_printf("DIN SPI transfer failed.\n");
79 * @brief Get value from DIN pin
81 * @param[in] cmd_io Pointer to IO stack
82 * @param[in] des Pointer to command descriptor
83 * @param[in] param Parameters of command
84 * @return 0 when OK or error code
86 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
93 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
94 return -CMDERR_BADPAR;
96 if (rpp_din_update() == FAILURE) {
97 rpp_sci_printf("DIN SPI transfer failed.\n");
101 ret = rpp_din_get(pin);
103 rpp_sci_printf("Pin out of range.\n");
104 return -CMDERR_BADPAR;
106 rpp_sci_printf("dinget%d =%x\n", pin, ret);
111 * @brief Read DIN diagnostic value
113 * @param[in] cmd_io Pointer to IO stack
114 * @param[in] des Pointer to command descriptor
115 * @param[in] param Parameters of command
116 * @return 0 when OK or error code
118 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
125 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
126 return -CMDERR_BADPAR;
128 if (rpp_din_update() == FAILURE) {
129 rpp_sci_printf("DIN SPI transfer failed.\n");
133 ret = rpp_din_diag(pin);
135 rpp_sci_printf("Pin out of range.\n");
136 return -CMDERR_BADPAR;
138 rpp_sci_printf("dindiag%d =%x\n", pin, ret);
142 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
144 rpp_sci_printf((const char *)
145 "Digital Inputs Test [0-15]:\r\n"
147 rpp_sci_printf((const char *)
148 "===========================================================\r\n"
150 rpp_sci_printf((const char *)
151 " 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"
152 // 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
155 // Calculate wait time in OS ticks
156 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
157 portTickType last_wake_time = xTaskGetTickCount();
160 while (cmd_io->getc(cmd_io) < 0) {
166 // Terminal needs to be at least 47 chars long
167 for (pin = 0; pin < 16; pin++) {
168 rpp_sci_printf((const char *)" %d ", rpp_din_get(pin));
170 for (pin = 8; pin < 16; pin++) {
171 rpp_sci_printf((const char *)" %d ", rpp_din_get_tr(pin));
173 rpp_sci_printf("\r");
175 vTaskDelayUntil(&last_wake_time, freq_ticks);
178 rpp_sci_printf("\n");
184 /** Command descriptor for din state command */
185 cmd_des_t const cmd_des_din_setup = {
187 "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
188 "### Command syntax ###\n"
190 " dinsetup<PINS> [A [B [C]]]\n"
193 "- PINS is either a number in range 1-16 or a range written as `<min>-<max>`\n"
194 "- A is an optional value - either 0 (pull down/switch to battery) or 1 (pull up/switch to ground). The default is 1.\n"
195 "- B is an optional value - either 0 (tri-state) or 1 (active). The default is 0.\n"
196 "- 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"
198 "### Description ###\n"
200 "The command setups properties of one or more DIN pins as specified by\n"
201 "`<PIN>`. Pins 0-7 can be set as pull up (switch to ground) or pull down\n"
202 "(switch to battery), pins 8-15 are hardcoded as switch to ground. All\n"
203 "pins can be set to either tri-state or active state and also can have\n"
204 "wake-up function with IRQ activated or not.\n"
206 "The command always prints the final settings of each set pin as ABC.\n"
207 "The actual configuration cannot be read out of the pin driver.\n"
211 " --> dinsetup1 1 0 0\n"
214 "Sets the DIN1 as switch to ground, active and disables IRQ generation.\n"
219 "Sets the DIN2 as to the default values i.e. switch to battery,\n"
220 "tri-state, wake-up/IRQ enabled.\n"
222 " --> dinsetup0-7 1 1 1\n"
232 "Sets the DIN0 through DIN7 as switch to ground, tri-state, wake-up/IRQ\n"
234 CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
237 /** Command descriptor for din state command */
238 cmd_des_t const cmd_des_din_get = {
240 "dinget*","Read the open/close status of a DIN pin (with the default treshold)", /* TODO add a command for reading with a treshold */
241 "### Command syntax ###\n"
245 "where PIN is a number in range 0-15\n"
247 "### Description ###\n"
249 "The command reads and prints the status of the DIN pin. Value 0 means\n"
250 "switch is open, value 1 means switch is closed. The mapping between\n"
251 "the DIN voltage and the open/close status depends on the setup of the\n"
252 "pin (see dinsetup command).\n"
259 "DIN1 is in open state.\n",
260 CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
263 /** Command descriptor for din state command */
264 cmd_des_t const cmd_des_din_diag = {
266 "dindiag*","Read diagnostic value from DIN pin",
267 "### Command syntax ###\n"
271 "where `<PIN>` is in range 1-16\n"
273 "### Description ###\n"
275 "The command reads and prints the value of diagnostic bit corresponding\n"
276 "to a DIN pin. TODO check what the value actually mean.\n"
283 "Prints value of the diagnostic bit of DIN1.\n",
284 CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
287 /** Command descriptor for din state command */
288 cmd_des_t const cmd_des_din_watch = {
290 "dinwatch","Watch status of all DIN pins",
291 "### Command syntax ###\n"
295 "### Description ###\n"
297 "The command reads and prints the status of DIN pins every 100\n"
298 "milliseconds. Columns 0-15 correspond to open/close status of DIN pins\n"
299 "with the default threshold of 4 V, columns A-H represent the logical\n"
300 "values of pins DIN8-15 when read with programmable threshold.\n"
301 "Pin status (open=0, close=1) depends on the pin setup that can be\n"
302 "changed with dinsetup command, programmable threshold can be set with\n"
305 "Press any key to end this command.\n"
310 " Digital Inputs Test [0-15]:\n"
311 " ===========================================================\n"
312 " 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"
313 " 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",
314 CMD_HANDLER(cmd_do_din_watch), (void *)&cmd_list_din
317 /** List of commands for din, defined as external */
318 cmd_des_t const *cmd_list_din[] = {
321 /* &cmd_des_din_diag, */ // Implementation in lower layers not finished