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