]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_din.c
Change license to MIT
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_din.c
1 /*
2  * Copyright (C) 2012-2013, 2016 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
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.
29  *
30  * File : cmd_din.c
31  *
32  * Abstract:
33  *      This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
34  *
35  */
36
37 #include "cmd_din.h"
38 #include "stdio.h"
39
40 #ifndef DOCGEN
41
42 #include "cmdproc_utils.h"
43 #include "rpp/rpp.h"
44
45 /**
46  * @brief       Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
47  *
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
52  */
53 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
54 {
55         char *p;
56         int ret;
57         int pin_min, pin_max;
58         int pull_up = 1;
59         int active = 0;
60         int can_wake = 1;
61         char spareParams;
62         int i;
63
64
65         p = param[1];
66         if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_up, &active, &can_wake, &spareParams) >= 2) {
67                 /* Pin range */
68         }
69         else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_up, &active, &can_wake, &spareParams) >= 1)
70                 /* Single pin */
71                 pin_max = pin_min;
72         else
73                 return -CMDERR_BADPAR;
74
75         for (i = pin_min; i <= pin_max; i++) {
76                 ret = rpp_din_setup(i, pull_up, active, can_wake);
77                 if (ret == -1) {
78                         rpp_sci_printf("Pin %d out of range.\n", i);
79                         return -CMDERR_BADPAR;
80                 }
81                 else if (ret == -2) {
82                         rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
83                         return -CMDERR_BADPAR;
84                 }
85                 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_up, !!active, !!can_wake);
86         }
87         if (rpp_din_update() == FAILURE) {
88                 rpp_sci_printf("DIN SPI transfer failed.\n");
89                 return -CMDERR_EIO;
90         }
91         return 0;
92 }
93
94 /**
95  * @brief       Get value from DIN pin
96  *
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
101  */
102 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
103 {
104         int pin;
105         char *p = param[1];
106         char spareParams;
107         int ret;
108
109         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
110                 return -CMDERR_BADPAR;
111
112         if (rpp_din_update() == FAILURE) {
113                 rpp_sci_printf("DIN SPI transfer failed.\n");
114                 return -CMDERR_EIO;
115         }
116
117         ret = rpp_din_get(pin);
118         if (ret == -1) {
119                 rpp_sci_printf("Pin out of range.\n");
120                 return -CMDERR_BADPAR;
121         }
122         rpp_sci_printf("dinget%d =%x\n", pin, ret);
123         return 0;
124 }
125
126 /**
127  * @brief       Read DIN diagnostic value
128  *
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
133  */
134 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
135 {
136         int pin;
137         char *p = param[1];
138         char spareParams;
139         int ret;
140
141         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
142                 return -CMDERR_BADPAR;
143
144         if (rpp_din_update() == FAILURE) {
145                 rpp_sci_printf("DIN SPI transfer failed.\n");
146                 return -CMDERR_EIO;
147         }
148
149         ret = rpp_din_diag(pin);
150         if (ret == -1) {
151                 rpp_sci_printf("Pin out of range.\n");
152                 return -CMDERR_BADPAR;
153         }
154         rpp_sci_printf("dindiag%d =%x\n", pin, ret);
155         return 0;
156 }
157
158 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
159 {
160         rpp_sci_printf((const char *)
161                                    "Digital Inputs Test [0-15]:\r\n"
162                                    );
163         rpp_sci_printf((const char *)
164                                    "===========================================================\r\n"
165                                    );
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
169                                    );
170
171         // Calculate wait time in OS ticks
172         static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
173         portTickType last_wake_time = xTaskGetTickCount();
174
175         uint8_t pin;
176         while (cmd_io->getc(cmd_io) < 0) {
177
178                 // Update inputs
179                 rpp_din_update();
180
181                 // Print inputs
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));
185                 }
186                 for (pin = 8; pin < 16; pin++) {
187                         rpp_sci_printf((const char *)" %d ", rpp_din_get_tr(pin));
188                 }
189                 rpp_sci_printf("\r");
190
191                 vTaskDelayUntil(&last_wake_time, freq_ticks);
192         }
193
194         rpp_sci_printf("\n");
195         return 0;
196 }
197
198 #endif  /* DOCGEN */
199
200 /** Command descriptor for din state command */
201 cmd_des_t const cmd_des_din_setup = {
202         0, 0,
203         "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
204         "### Command syntax ###\n"
205         "\n"
206         "    dinsetup<PINS> [PU [ACT [WAKE]]]\n"
207         "where\n"
208         "\n"
209         "- PINS is either a number in range 0-15 or a range written as\n"
210         "  `<min>-<max>`\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"
214         "  default is 0.\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"
217         "\n"
218         "### Description ###\n"
219         "\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"
225         "\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"
228         "the pin driver.\n"
229         "\n"
230         "### Example ###\n"
231         "\n"
232         "    --> dinsetup1 1 0 0\n"
233         "    dinsetup1=100\n"
234         "\n"
235         "Sets the DIN1 as switch to ground, tri-state and disables IRQ\n"
236         "generation.\n"
237         "\n"
238         "    --> dinsetup2\n"
239         "    dinsetup2=101\n"
240         "\n"
241         "Sets the DIN2 as to the default values i.e. switch to ground,\n"
242         "tri-state, wake-up/IRQ enabled.\n"
243         "\n"
244         "    --> dinsetup0-7 1 1 1\n"
245         "    dinsetup0=111\n"
246         "    dinsetup1=111\n"
247         "    dinsetup2=111\n"
248         "    dinsetup3=111\n"
249         "    dinsetup4=111\n"
250         "    dinsetup5=111\n"
251         "    dinsetup6=111\n"
252         "    dinsetup7=111\n"
253         "\n"
254         "Sets the DIN0 through DIN7 as switch to battery, active, wake-up/IRQ\n"
255         "enabled.\n",
256         CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
257 };
258
259 /** Command descriptor for din state command */
260 cmd_des_t const cmd_des_din_get = {
261         0, 0,
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"
264         "\n"
265         "    dinget<PIN>\n"
266         "\n"
267         "where PIN is a number in range 0-15\n"
268         "\n"
269         "### Description ###\n"
270         "\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"
275         "\n"
276         "### Example ###\n"
277         "\n"
278         "    --> dinget1\n"
279         "    dinget1 =0\n"
280         "\n"
281         "DIN1 is in open state.\n",
282         CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
283 };
284
285 /** Command descriptor for din state command */
286 cmd_des_t const cmd_des_din_diag = {
287         0, 0,
288         "dindiag*","Read diagnostic value from DIN pin",
289         "### Command syntax ###\n"
290         "\n"
291         "    dindiag<PIN>\n"
292         "\n"
293         "where `<PIN>` is in range 1-16\n"
294         "\n"
295         "### Description ###\n"
296         "\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"
299         "\n"
300         "### Example ###\n"
301         "\n"
302         "    --> dindiag1\n"
303         "    dindiag1 =0\n"
304         "\n"
305         "Prints value of the diagnostic bit of DIN1.\n",
306         CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
307 };
308
309 /** Command descriptor for din state command */
310 cmd_des_t const cmd_des_din_watch = {
311         0, 0,
312         "dinwatch","Watch status of all DIN pins",
313         "### Command syntax ###\n"
314         "\n"
315         "    dinwatch\n"
316         "\n"
317         "### Description ###\n"
318         "\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"
325         "TODO command.\n"
326         "\n"
327         "Press any key to end this command.\n"
328         "\n"
329         "### Example ###\n"
330         "\n"
331         "    --> dinwatch\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
337 };
338
339 /** List of commands for din, defined as external */
340 cmd_des_t const *cmd_list_din[] = {
341         &cmd_des_din_setup,
342         &cmd_des_din_get,
343 /*   &cmd_des_din_diag, */ // Implementation in lower layers not finished
344         &cmd_des_din_watch,
345         NULL
346 };