2 * Copyright (C) 2012-2013, 2016 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 * This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
42 #include "cmdproc_utils.h"
46 * @brief Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
48 * @param[in] cmd_io Pointer to IO stack
49 * @param[in] des Pointer to command descriptor
50 * @param[in] param Parameters of command
51 * @return 0 when OK or error code
53 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
66 if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_up, &active, &can_wake, &spareParams) >= 2) {
69 else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_up, &active, &can_wake, &spareParams) >= 1)
73 return -CMDERR_BADPAR;
75 for (i = pin_min; i <= pin_max; i++) {
76 ret = rpp_din_setup(i, pull_up, active, can_wake);
78 rpp_sci_printf("Pin %d out of range.\n", i);
79 return -CMDERR_BADPAR;
82 rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
83 return -CMDERR_BADPAR;
85 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_up, !!active, !!can_wake);
87 if (rpp_din_update() == FAILURE) {
88 rpp_sci_printf("DIN SPI transfer failed.\n");
95 * @brief Get value from DIN pin
97 * @param[in] cmd_io Pointer to IO stack
98 * @param[in] des Pointer to command descriptor
99 * @param[in] param Parameters of command
100 * @return 0 when OK or error code
102 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
109 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
110 return -CMDERR_BADPAR;
112 if (rpp_din_update() == FAILURE) {
113 rpp_sci_printf("DIN SPI transfer failed.\n");
117 ret = rpp_din_get(pin);
119 rpp_sci_printf("Pin out of range.\n");
120 return -CMDERR_BADPAR;
122 rpp_sci_printf("dinget%d =%x\n", pin, ret);
127 * @brief Read DIN diagnostic value
129 * @param[in] cmd_io Pointer to IO stack
130 * @param[in] des Pointer to command descriptor
131 * @param[in] param Parameters of command
132 * @return 0 when OK or error code
134 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
141 if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
142 return -CMDERR_BADPAR;
144 if (rpp_din_update() == FAILURE) {
145 rpp_sci_printf("DIN SPI transfer failed.\n");
149 ret = rpp_din_diag(pin);
151 rpp_sci_printf("Pin out of range.\n");
152 return -CMDERR_BADPAR;
154 rpp_sci_printf("dindiag%d =%x\n", pin, ret);
158 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
160 rpp_sci_printf((const char *)
161 "Digital Inputs Test [0-15]:\r\n"
163 rpp_sci_printf((const char *)
164 "===========================================================\r\n"
166 rpp_sci_printf((const char *)
167 " 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"
168 // 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
171 // Calculate wait time in OS ticks
172 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
173 portTickType last_wake_time = xTaskGetTickCount();
176 while (cmd_io->getc(cmd_io) < 0) {
182 // Terminal needs to be at least 47 chars long
183 for (pin = 0; pin < 16; pin++) {
184 rpp_sci_printf((const char *)" %d ", rpp_din_get(pin));
186 for (pin = 8; pin < 16; pin++) {
187 rpp_sci_printf((const char *)" %d ", rpp_din_get_tr(pin));
189 rpp_sci_printf("\r");
191 vTaskDelayUntil(&last_wake_time, freq_ticks);
194 rpp_sci_printf("\n");
200 /** Command descriptor for din state command */
201 cmd_des_t const cmd_des_din_setup = {
203 "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
204 "### Command syntax ###\n"
206 " dinsetup<PINS> [PU [ACT [WAKE]]]\n"
209 "- PINS is either a number in range 0-15 or a range written as\n"
211 "- PU is an optional value - either 0 (pull down/switch to battery) or 1\n"
212 " (pull up/switch to ground). The default is 1.\n"
213 "- ACT is an optional value - either 0 (tri-state) or 1 (active). The\n"
215 "- WAKE is an optional value - either 0 (wake up and IRQ disabled) or 1\n"
216 " (wake up and IRQ enabled). The default is 1.\n"
218 "### Description ###\n"
220 "The command setups properties of one or more DIN pins as specified by\n"
221 "`<PINS>`. Pins 0-7 can be set as pull up (switch to ground) or pull\n"
222 "down (switch to battery), pins 8-15 are hardcoded as switch to ground.\n"
223 "All pins can be set to either tri-state or active state and can have\n"
224 "wake-up function with IRQ activated or not.\n"
226 "The command always prints the final settings of each set pin as PU ACT\n"
227 "WAKE (without spaces). The actual configuration cannot be read out of\n"
232 " --> dinsetup1 1 0 0\n"
235 "Sets the DIN1 as switch to ground, tri-state and disables IRQ\n"
241 "Sets the DIN2 as to the default values i.e. switch to ground,\n"
242 "tri-state, wake-up/IRQ enabled.\n"
244 " --> dinsetup0-7 1 1 1\n"
254 "Sets the DIN0 through DIN7 as switch to battery, active, wake-up/IRQ\n"
256 CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
259 /** Command descriptor for din state command */
260 cmd_des_t const cmd_des_din_get = {
262 "dinget*","Read the open/close status of a DIN pin (with the default treshold)", /* TODO add a command for reading with a treshold */
263 "### Command syntax ###\n"
267 "where PIN is a number in range 0-15\n"
269 "### Description ###\n"
271 "The command reads and prints the status of the DIN pin. Value 0 means\n"
272 "switch is open, value 1 means switch is closed. The mapping between\n"
273 "the DIN voltage and the open/close status depends on the setup of the\n"
274 "pin (see dinsetup command).\n"
281 "DIN1 is in open state.\n",
282 CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
285 /** Command descriptor for din state command */
286 cmd_des_t const cmd_des_din_diag = {
288 "dindiag*","Read diagnostic value from DIN pin",
289 "### Command syntax ###\n"
293 "where `<PIN>` is in range 1-16\n"
295 "### Description ###\n"
297 "The command reads and prints the value of diagnostic bit corresponding\n"
298 "to a DIN pin. TODO check what the value actually mean.\n"
305 "Prints value of the diagnostic bit of DIN1.\n",
306 CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
309 /** Command descriptor for din state command */
310 cmd_des_t const cmd_des_din_watch = {
312 "dinwatch","Watch status of all DIN pins",
313 "### Command syntax ###\n"
317 "### Description ###\n"
319 "The command reads and prints the status of DIN pins every 100\n"
320 "milliseconds. Columns 0-15 correspond to open/close status of DIN pins\n"
321 "with the default threshold of 4 V, columns A-H represent the logical\n"
322 "values of pins DIN8-15 when read with programmable threshold.\n"
323 "Pin status (open=0, close=1) depends on the pin setup that can be\n"
324 "changed with dinsetup command, programmable threshold can be set with\n"
327 "Press any key to end this command.\n"
332 " Digital Inputs Test [0-15]:\n"
333 " ===========================================================\n"
334 " 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"
335 " 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",
336 CMD_HANDLER(cmd_do_din_watch), (void *)&cmd_list_din
339 /** List of commands for din, defined as external */
340 cmd_des_t const *cmd_list_din[] = {
343 /* &cmd_des_din_diag, */ // Implementation in lower layers not finished