]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_din.c
Update/correct dinsetup documentation
[pes-rpp/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  * 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.
13  *
14  * File : cmd_din.c
15  *
16  * Abstract:
17  *      This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
18  *
19  */
20
21 #include "cmd_din.h"
22 #include "stdio.h"
23
24 #ifndef DOCGEN
25
26 #include "cmdproc_utils.h"
27 #include "rpp/rpp.h"
28
29 /**
30  * @brief       Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
31  *
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
36  */
37 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
38 {
39         char *p;
40         int ret;
41         int pin_min, pin_max;
42         int pull_up = 1;
43         int active = 0;
44         int can_wake = 1;
45         char spareParams;
46         int i;
47
48
49         p = param[1];
50         if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_up, &active, &can_wake, &spareParams) >= 2) {
51                 /* Pin range */
52         }
53         else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_up, &active, &can_wake, &spareParams) >= 1)
54                 /* Single pin */
55                 pin_max = pin_min;
56         else
57                 return -CMDERR_BADPAR;
58
59         for (i = pin_min; i <= pin_max; i++) {
60                 ret = rpp_din_setup(i, pull_up, active, can_wake);
61                 if (ret == -1) {
62                         rpp_sci_printf("Pin %d out of range.\n", i);
63                         return -CMDERR_BADPAR;
64                 }
65                 else if (ret == -2) {
66                         rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
67                         return -CMDERR_BADPAR;
68                 }
69                 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_up, !!active, !!can_wake);
70         }
71         if (rpp_din_update() == FAILURE) {
72                 rpp_sci_printf("DIN SPI transfer failed.\n");
73                 return -CMDERR_EIO;
74         }
75         return 0;
76 }
77
78 /**
79  * @brief       Get value from DIN pin
80  *
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
85  */
86 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
87 {
88         int pin;
89         char *p = param[1];
90         char spareParams;
91         int ret;
92
93         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
94                 return -CMDERR_BADPAR;
95
96         if (rpp_din_update() == FAILURE) {
97                 rpp_sci_printf("DIN SPI transfer failed.\n");
98                 return -CMDERR_EIO;
99         }
100
101         ret = rpp_din_get(pin);
102         if (ret == -1) {
103                 rpp_sci_printf("Pin out of range.\n");
104                 return -CMDERR_BADPAR;
105         }
106         rpp_sci_printf("dinget%d =%x\n", pin, ret);
107         return 0;
108 }
109
110 /**
111  * @brief       Read DIN diagnostic value
112  *
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
117  */
118 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
119 {
120         int pin;
121         char *p = param[1];
122         char spareParams;
123         int ret;
124
125         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1)
126                 return -CMDERR_BADPAR;
127
128         if (rpp_din_update() == FAILURE) {
129                 rpp_sci_printf("DIN SPI transfer failed.\n");
130                 return -CMDERR_EIO;
131         }
132
133         ret = rpp_din_diag(pin);
134         if (ret == -1) {
135                 rpp_sci_printf("Pin out of range.\n");
136                 return -CMDERR_BADPAR;
137         }
138         rpp_sci_printf("dindiag%d =%x\n", pin, ret);
139         return 0;
140 }
141
142 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
143 {
144         rpp_sci_printf((const char *)
145                                    "Digital Inputs Test [0-15]:\r\n"
146                                    );
147         rpp_sci_printf((const char *)
148                                    "===========================================================\r\n"
149                                    );
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
153                                    );
154
155         // Calculate wait time in OS ticks
156         static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
157         portTickType last_wake_time = xTaskGetTickCount();
158
159         uint8_t pin;
160         while (cmd_io->getc(cmd_io) < 0) {
161
162                 // Update inputs
163                 rpp_din_update();
164
165                 // Print inputs
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));
169                 }
170                 for (pin = 8; pin < 16; pin++) {
171                         rpp_sci_printf((const char *)" %d ", rpp_din_get_tr(pin));
172                 }
173                 rpp_sci_printf("\r");
174
175                 vTaskDelayUntil(&last_wake_time, freq_ticks);
176         }
177
178         rpp_sci_printf("\n");
179         return 0;
180 }
181
182 #endif  /* DOCGEN */
183
184 /** Command descriptor for din state command */
185 cmd_des_t const cmd_des_din_setup = {
186         0, 0,
187         "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
188         "### Command syntax ###\n"
189         "\n"
190         "    dinsetup<PINS> [PU [ACT [WAKE]]]\n"
191         "where\n"
192         "\n"
193         "- PINS is either a number in range 0-15 or a range written as\n"
194         "  `<min>-<max>`\n"
195         "- PU is an optional value - either 0 (pull down/switch to battery) or 1\n"
196         "  (pull up/switch to ground). The default is 1.\n"
197         "- ACT is an optional value - either 0 (tri-state) or 1 (active). The\n"
198         "  default is 0.\n"
199         "- WAKE is an optional value - either 0 (wake up and IRQ disabled) or 1\n"
200         "  (wake up and IRQ enabled). The default is 1.\n"
201         "\n"
202         "### Description ###\n"
203         "\n"
204         "The command setups properties of one or more DIN pins as specified by\n"
205         "`<PINS>`. Pins 0-7 can be set as pull up (switch to ground) or pull\n"
206         "down (switch to battery), pins 8-15 are hardcoded as switch to ground.\n"
207         "All pins can be set to either tri-state or active state and can have\n"
208         "wake-up function with IRQ activated or not.\n"
209         "\n"
210         "The command always prints the final settings of each set pin as PU ACT\n"
211         "WAKE (without spaces). The actual configuration cannot be read out of\n"
212         "the pin driver.\n"
213         "\n"
214         "### Example ###\n"
215         "\n"
216         "    --> dinsetup1 1 0 0\n"
217         "    dinsetup1=100\n"
218         "\n"
219         "Sets the DIN1 as switch to ground, tri-state and disables IRQ\n"
220         "generation.\n"
221         "\n"
222         "    --> dinsetup2\n"
223         "    dinsetup2=101\n"
224         "\n"
225         "Sets the DIN2 as to the default values i.e. switch to ground,\n"
226         "tri-state, wake-up/IRQ enabled.\n"
227         "\n"
228         "    --> dinsetup0-7 1 1 1\n"
229         "    dinsetup0=111\n"
230         "    dinsetup1=111\n"
231         "    dinsetup2=111\n"
232         "    dinsetup3=111\n"
233         "    dinsetup4=111\n"
234         "    dinsetup5=111\n"
235         "    dinsetup6=111\n"
236         "    dinsetup7=111\n"
237         "\n"
238         "Sets the DIN0 through DIN7 as switch to battery, active, wake-up/IRQ\n"
239         "enabled.\n",
240         CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
241 };
242
243 /** Command descriptor for din state command */
244 cmd_des_t const cmd_des_din_get = {
245         0, 0,
246         "dinget*","Read the open/close status of a DIN pin (with the default treshold)", /* TODO add a command for reading with a treshold */
247         "### Command syntax ###\n"
248         "\n"
249         "    dinget<PIN>\n"
250         "\n"
251         "where PIN is a number in range 0-15\n"
252         "\n"
253         "### Description ###\n"
254         "\n"
255         "The command reads and prints the status of the DIN pin. Value 0 means\n"
256         "switch is open, value 1 means switch is closed. The mapping between\n"
257         "the DIN voltage and the open/close status depends on the setup of the\n"
258         "pin (see dinsetup command).\n"
259         "\n"
260         "### Example ###\n"
261         "\n"
262         "    --> dinget1\n"
263         "    dinget1 =0\n"
264         "\n"
265         "DIN1 is in open state.\n",
266         CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
267 };
268
269 /** Command descriptor for din state command */
270 cmd_des_t const cmd_des_din_diag = {
271         0, 0,
272         "dindiag*","Read diagnostic value from DIN pin",
273         "### Command syntax ###\n"
274         "\n"
275         "    dindiag<PIN>\n"
276         "\n"
277         "where `<PIN>` is in range 1-16\n"
278         "\n"
279         "### Description ###\n"
280         "\n"
281         "The command reads and prints the value of diagnostic bit corresponding\n"
282         "to a DIN pin. TODO check what the value actually mean.\n"
283         "\n"
284         "### Example ###\n"
285         "\n"
286         "    --> dindiag1\n"
287         "    dindiag1 =0\n"
288         "\n"
289         "Prints value of the diagnostic bit of DIN1.\n",
290         CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
291 };
292
293 /** Command descriptor for din state command */
294 cmd_des_t const cmd_des_din_watch = {
295         0, 0,
296         "dinwatch","Watch status of all DIN pins",
297         "### Command syntax ###\n"
298         "\n"
299         "    dinwatch\n"
300         "\n"
301         "### Description ###\n"
302         "\n"
303         "The command reads and prints the status of DIN pins every 100\n"
304         "milliseconds. Columns 0-15 correspond to open/close status of DIN pins\n"
305         "with the default threshold of 4 V, columns A-H represent the logical\n"
306         "values of pins DIN8-15 when read with programmable threshold.\n"
307         "Pin status (open=0, close=1) depends on the pin setup that can be\n"
308         "changed with dinsetup command, programmable threshold can be set with\n"
309         "TODO command.\n"
310         "\n"
311         "Press any key to end this command.\n"
312         "\n"
313         "### Example ###\n"
314         "\n"
315         "    --> dinwatch\n"
316         "    Digital Inputs Test [0-15]:\n"
317         "    ===========================================================\n"
318         "     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"
319         "     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",
320         CMD_HANDLER(cmd_do_din_watch), (void *)&cmd_list_din
321 };
322
323 /** List of commands for din, defined as external */
324 cmd_des_t const *cmd_list_din[] = {
325         &cmd_des_din_setup,
326         &cmd_des_din_get,
327 /*   &cmd_des_din_diag, */ // Implementation in lower layers not finished
328         &cmd_des_din_watch,
329         NULL
330 };