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