]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - commands/cmd_din.c
Clarify the documentation of dinget and dinwatch commands
[pes-rpp/rpp-test-sw.git] / commands / cmd_din.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * File : cmd_din.c
23  *
24  * Abstract:
25  *      This file contains commands for DIN control. Use can setup DIN pin and read value and diagnosctic value
26  *
27  */
28
29 #include "cmd_din.h"
30 #include "stdio.h"
31
32 #ifndef DOCGEN
33
34 #include "cmdproc_utils.h"
35 #include "drv/din.h"
36 #include "rpp/rpp.h"
37 #include "hal/hal.h"
38
39 /**
40  * @brief       Setup DIN pin parameters (pull up/down, tristate/active, IRQ and wakeup disable/enable
41  *
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
46  */
47 int cmd_do_din_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
48         char *p;
49         int ret;
50         int pin_min, pin_max;
51         int pull_type = 0;
52         int active = 1;
53         int can_wake = 1;
54         char spareParams;
55         int i;
56
57
58         p=param[1];
59         if (sscanf(p, "%d-%d %d %d %d %1s", &pin_min, &pin_max, &pull_type, &active, &can_wake, &spareParams) >= 2) {
60                 /* Pin range */
61         } else if (sscanf(p, "%d %d %d %d %1s", &pin_min, &pull_type, &active, &can_wake, &spareParams) >= 1) {
62                 /* Single pin */
63                 pin_max = pin_min;
64         } else
65                 return -CMDERR_BADPAR;
66
67         for (i = pin_min; i <= pin_max; i++) {
68                 ret = rpp_din_setup(i, pull_type, active, can_wake);
69                 if (ret == -1) {
70                         rpp_sci_printf("Pin %d out of range.\n", i);
71                         return -CMDERR_BADPAR;
72                 }
73                 else if (ret == -2) {
74                         rpp_sci_printf("Pin %d is not programmable to switch to battery.\n", i);
75                         return -CMDERR_BADPAR;
76                 }
77                 if (rpp_din_update() == FAILURE) {
78                         rpp_sci_printf("DIN%d SPI transfer failed.\n", i);
79                         return -CMDERR_EIO;
80                 }
81                 rpp_sci_printf("dinsetup%d=%d%d%d\n", i, !!pull_type, !!active, !!can_wake);
82         }
83         return 0;
84 }
85
86 /**
87  * @brief       Get value from DIN pin
88  *
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
93  */
94 int cmd_do_din_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
95         int pin;
96         char *p=param[1];
97         char spareParams;
98         int ret;
99
100         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
101                 return -CMDERR_BADPAR;
102         }
103
104         if (rpp_din_update() == FAILURE) {
105                 rpp_sci_printf("DIN SPI transfer failed.\n");
106                 return -CMDERR_EIO;
107         }
108
109         ret = rpp_din_get(pin, false);
110         if (ret == -1) {
111                 rpp_sci_printf("Pin out of range.\n");
112                 return -CMDERR_BADPAR;
113         }
114         else if (ret == -2) {
115                 rpp_sci_printf("Selected pin can not be read with threshold\n");
116                 return -CMDERR_BADPAR;
117         }
118         rpp_sci_printf("dinget%d =%x\n", pin, ret);
119         return 0;
120 }
121
122 /**
123  * @brief       Read DIN diagnostic value
124  *
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
129  */
130 int cmd_do_din_diag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
131         int pin;
132         char *p=param[1];
133         char spareParams;
134         int ret;
135
136         if (sscanf(p, "%d %1s", &pin, &spareParams) != 1) {
137                 return -CMDERR_BADPAR;
138         }
139
140         if (rpp_din_update() == FAILURE) {
141                 rpp_sci_printf("DIN SPI transfer failed.\n");
142                 return -CMDERR_EIO;
143         }
144
145         ret = rpp_din_diag(pin);
146         if (ret == -1) {
147                 rpp_sci_printf("Pin out of range.\n");
148                 return -CMDERR_BADPAR;
149         }
150         rpp_sci_printf("dindiag%d =%x\n", pin, ret);
151         return 0;
152 }
153
154 int cmd_do_din_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
155 {
156     rpp_sci_printf((const char*)
157             "Digital Inputs Test [0-15]:\r\n"
158         );
159     rpp_sci_printf((const char*)
160             "===========================================================\r\n"
161         );
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
165         );
166
167     // Calculate wait time in OS ticks
168     static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
169     portTickType last_wake_time = xTaskGetTickCount();
170
171     uint8_t pin;
172     while(cmd_io->getc(cmd_io) < 0) {
173
174         // Update inputs
175         rpp_din_update();
176
177         // Print inputs
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));
181         }
182         for(pin = 8; pin < 16; pin++) {
183             rpp_sci_printf((const char*)" %d ", rpp_din_get(pin, TRUE));
184         }
185         rpp_sci_printf("\r");
186
187         vTaskDelayUntil(&last_wake_time, freq_ticks);
188     }
189
190     rpp_sci_printf("\n");
191     return 0;
192 }
193
194 #endif  /* DOCGEN */
195
196 /** Command descriptor for din state command */
197 cmd_des_t const cmd_des_din_setup={
198         0, 0,
199         "dinsetup*", "Setup DIN pin parameters (Pull up/down, tristate/active, IRQ and wakeup disable/enable)",
200         "=== Command syntax ===\n"
201         "\n"
202         "   dinsetup<PINS> [A [B [C]]]\n"
203         "where\n"
204         "* PIN is either a number in range 1-16 or a range written as\n"
205         "  <min>-<max>\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"
209         "  default is 1.\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"
212         "\n"
213         "=== Description ===\n"
214         "\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"
220         "\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"
223         "\n"
224         "=== Example ===\n"
225         "\n"
226         "   --> dinsetup1 1 0 0\n"
227         "   dinsetup1=100\n"
228         "\n"
229         "Sets the DIN1 as switch to ground, active and disables IRQ generation.\n"
230         "\n"
231         "   --> dinsetup2\n"
232         "   dinsetup2=011\n"
233         "\n"
234         "Sets the DIN2 as to the default values i.e. switch to battery,\n"
235         "tri-state, wake-up/IRQ enabled.\n"
236         "\n"
237         "   --> dinsetup0-7 1 1 1\n"
238         "   dinsetup0=111\n"
239         "   dinsetup1=111\n"
240         "   dinsetup2=111\n"
241         "   dinsetup3=111\n"
242         "   dinsetup4=111\n"
243         "   dinsetup5=111\n"
244         "   dinsetup6=111\n"
245         "   dinsetup7=111\n"
246         "\n"
247         "Sets the DIN0 through DIN7 as switch to ground, tri-state, wake-up/IRQ\n"
248         "enabled.\n",
249         CMD_HANDLER(cmd_do_din_setup), (void *)&cmd_list_din
250 };
251
252 /** Command descriptor for din state command */
253 cmd_des_t const cmd_des_din_get={
254         0, 0,
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"
257         "\n"
258         "   dinget<PIN>\n"
259         "\n"
260         "where PIN is a number in range 0-15\n"
261         "\n"
262         "=== Description ===\n"
263         "\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"
268         "\n"
269         "=== Example ===\n"
270         "\n"
271         "   --> dinget1\n"
272         "   dinget1 =0\n"
273         "\n"
274         "DIN1 is in open state.\n",
275         CMD_HANDLER(cmd_do_din_get), (void *)&cmd_list_din
276 };
277
278 /** Command descriptor for din state command */
279 cmd_des_t const cmd_des_din_diag={
280         0, 0,
281         "dindiag*","Read diagnostic value from DIN pin",
282         "=== Command syntax ===\n"
283         "\n"
284         "   dindiag<PIN>\n"
285         "\n"
286         "where <PIN> is in range 1-16\n"
287         "\n"
288         "=== Description ===\n"
289         "\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"
292         "\n"
293         "=== Example ===\n"
294         "\n"
295         "   --> dindiag1\n"
296         "   dindiag1 =0\n"
297         "\n"
298         "Prints value of the diagnostic bit of DIN1.\n",
299         CMD_HANDLER(cmd_do_din_diag), (void *)&cmd_list_din
300 };
301
302 /** Command descriptor for din state command */
303 cmd_des_t const cmd_des_din_watch={
304         0, 0,
305         "dinwatch","Watch status of all DIN pins",
306         "=== Command syntax ===\n"
307         "\n"
308         "   dinwatch\n"
309         "\n"
310         "=== Description ===\n"
311         "\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"
318         "TODO command.\n"
319         "\n"
320         "Press any key to end this command.\n"
321         "\n"
322         "=== Example ===\n"
323         "\n"
324         "   --> dinwatch\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
330 };
331
332 /** List of commands for din, defined as external */
333 cmd_des_t const *cmd_list_din[]={
334   &cmd_des_din_setup,
335   &cmd_des_din_get,
336   &cmd_des_din_diag,
337   &cmd_des_din_watch,
338   NULL
339 };