]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - commands/cmd_hout.c
Move commands to toplevel directory (outside of lib)
[pes-rpp/rpp-test-sw.git] / commands / cmd_hout.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_hout.c
23  *
24  * Abstract:
25  *      This file contains commands for HOUT control.
26  *
27  */
28
29 #include "cmd_hout.h"
30
31 #ifndef DOCGEN
32
33 #include "rpp/rpp.h"
34 #include "hal/hal.h"
35 #include "drv/drv.h"
36 #include "cmdproc_utils.h"
37
38
39 /**
40  * @brief       Reads values from HOUT_IFBK pins (subset of ADC)
41  *
42  * Command syntax: readhifbk
43  *
44  * @param[in]   cmd_io  Pointer to IO stack
45  * @param[in]   des             Pointer to command descriptor
46  * @param[in]   param   Parameters of command
47  * @return      0 when OK or error code
48  */
49 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
50 {
51     uint32_t i;                                         // Cycle control variable
52     port_desc_t* desc;
53     uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
54     int ret;
55
56     desc = hal_port_get_dsc(PORT_NAME_HOUTIFBK, -1);
57
58     ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
59     if (ret < 0)
60         return ret;
61
62     rpp_sci_printf("HOUT_IFBK: values: ");
63     /* conversion results :                                       */
64     for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
65         rpp_sci_printf("\r\nChannel: %d\tvalue: %d %h", i+1, values[i], values[i]);
66     }
67     return 0;
68 }
69
70 /**
71  * @brief       Runs test of selected HOUT pin.
72  *
73  * This command runs a test to check if HOUT pin is in good or fault condition.
74  * When HOUT is OK, HOUT_DIAG pin has the same value as HOUT_IN pin.
75  * When HOUT is in fault state, HOUT_DIAG periodically follows HOUT_PIN for 2ms and shorts to ground for 2ms.
76  *
77  * Command syntax: houtfail#
78  *
79  * @param[in]   cmd_io  Pointer to IO stack
80  * @param[in]   des             Pointer to command descriptor
81  * @param[in]   param   Parameters of command
82  * @return      0 when OK or error code
83  */
84 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
85 {
86         int pin;
87         char *p=param[1];
88         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
89         pin--;
90         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
91
92         switch (hout_fail(pin)) {
93         case HOUT_OK:
94                 rpp_sci_printf("OK");
95                 break;
96         case HOUT_FAILED:
97                 rpp_sci_printf("FAIL");
98                 break;
99         case HOUT_NOT_ON:
100                 rpp_sci_printf("NOT RUNNING");
101                 break;
102         default:
103                 rpp_sci_printf("Bad pin selected");
104                 break;
105         }
106         return 0;
107 }
108
109 /**
110  *      @brief  Set or get actual pwm parameters
111  *
112  *      Command syntax: houtpwm:(p,d)   - set p as a period in us and D as duty cycle in % of the period.
113  *                                      houtpwm?        - Get actual PWM parameters, period in us and duty cycle in % of the period.
114  *
115  * @param[in]   cmd_io  Pointer to IO stack
116  * @param[in]   des             Pointer to command descriptor
117  * @param[in]   param   Parameters of command
118  * @return      0 when OK or error code
119  */
120 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
121 {
122           char *p;
123           int opchar;
124           int i;
125           uint32_t values[MAX_PARAM_VALUES_NUM];
126           int pin;
127
128           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
129
130           p=param[1];
131           if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
132           pin--;
133           if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
134
135           if(opchar==':'){
136             p=param[3];
137                 si_skspace(&p);
138                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 10);
139                 if (i < 0)
140                         return i;
141                 if (i != 2)
142                         return -CMDERR_BADPAR;
143                 if (values[0] < 1 || values[1] > 100) return -CMDERR_BADPAR;
144                 hout_pwm_set_signal(pin, (double)values[0], values[1]);
145             return 0;
146           }
147           else{
148                 double period = hout_pwm_get_period(pin);
149                 uint32_t duty = hout_pwm_get_duty(pin);
150
151                 rpp_sci_printf("hout%d_pwm_period: %d\r\nhout%d_pwm_duty: %d/%", pin, period, pin, duty);
152             return 0;
153           }
154 }
155
156 /**
157  *      @brief  Start PWM, if it was previously set by houtpwm command
158  *
159  *      Command syntax: houtpwmstart
160  *
161  * @param[in]   cmd_io  Pointer to IO stack
162  * @param[in]   des             Pointer to command descriptor
163  * @param[in]   param   Parameters of command
164  * @return      0 when OK or error code
165  */
166 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
167 {
168         char* p;
169         int pin;
170
171         p=param[1];
172         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
173         pin--;
174         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
175         int ret = hout_pwm_start(pin);
176         if (ret == -1) {
177                 rpp_sci_printf("PWM was not initialized.\r\n");
178                 return -CMDERR_BADCFG;
179         }
180         else {
181                 return 0;
182         }
183 }
184
185 /**
186  *      @brief  Stop PWM
187  *
188  *      Command syntax: houtpwmstop
189  *
190  * @param[in]   cmd_io  Pointer to IO stack
191  * @param[in]   des             Pointer to command descriptor
192  * @param[in]   param   Parameters of command
193  * @return      0 when OK or error code
194  */
195 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
196 {
197         char* p;
198         int pin;
199
200         p=param[1];
201         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
202         pin--;
203         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
204         hout_pwm_stop(pin);
205         return 0;
206 }
207
208 #endif  /* DOCGEN */
209
210 /** Command descriptor for test hout fault state command */
211 cmd_des_t const cmd_des_test_hout_fail = {
212     0, 0,
213     "houtfail","Test if some HOUT port is in fault state",
214     "=== Description ===\n"
215     "\n"
216     "This command tests, if HOUT pin is in good condition. When a circuit,\n"
217     "controlling HOUT pin, detects some fault, it starts to generate signal\n"
218     "on HOUT_DIAG pin.\n"
219     "\n"
220     "=== Command syntax ===\n"
221     "\n"
222     "   --> houtfail pin\n"
223     "where pin is in range 1-6\n",
224     CMD_HANDLER(cmd_do_test_hout_fault), (void *)&cmd_list_hout
225 };
226
227 /** Command descriptor for hout read IFBK command */
228 cmd_des_t const cmd_des_read_hifbk={
229     0, 0,
230     "readhifbk","Reads values from HOUT IFBK",
231     "=== Description ===\n"
232     "\n"
233     "Command reads analog values on HOUT_IFBK pins.\n"
234     "\n"
235     "\n"
236     "=== Command syntax ===\n"
237     "\n"
238     "   --> readhifbk\n",
239     CMD_HANDLER(cmd_do_read_hout_ifbk_values), (void *)&cmd_list_hout
240 };
241
242 /** Command descriptor for HBR set PWM command */
243 cmd_des_t const cmd_des_hout_pwm={
244     0, CDESM_OPCHR|CDESM_RW,
245     "houtpwm*","Set or get actual PWM parameters",
246     "=== Description ===\n"
247     "\n"
248     "This command can be used for setting or getting HOUT PWM parameters.\n"
249     "Command expects HOUT pin number as first parameter, operation\n"
250     "character : (setter) or ? (getter) and in case of setter, another two\n"
251     "parameters are mandatory. The first one is PWM period in us, the\n"
252     "second one is duty cycle in % of the period.\n"
253     "\n"
254     "=== Command syntax ===\n"
255     "\n"
256     "   --> houtpwmPIN[:|?][(PER,DUTY)]\n"
257     "where\n"
258     "* PIN is in range 1-6\n"
259     "* : means set parameters and parameters in brackets are mandatory, ? means get parameters\n"
260     "* PER is greater than 0\n"
261     "* DUTY is in range 0-100\n"
262     "\n"
263     "=== Example ===\n"
264     "\n"
265     "   --> houtpwm1:(1000,25)\n"
266     "\n"
267     "HOUT1 PWM will have period 1ms and will be active for 25% of this\n"
268     "period.\n"
269     "\n"
270     "   --> hbrpwm1?\n"
271     "\n"
272     "Actual period of HOUT1 PWM in us and duty cycle in % will be printed.\n",
273     CMD_HANDLER(cmd_do_hout_pwm), (void *)&cmd_list_hout
274 };
275
276 /** Command descriptor for PWM start command */
277 cmd_des_t const cmd_des_hout_pwm_start={
278     0, 0,
279     "houtpwmstart#","houtpwmstartX - Start generating of pwm signal on HOUTX",
280     "=== Description ===\n"
281     "\n"
282     "If HOUT PWM was previously set by houtpwm command, this command starts\n"
283     "generating PWM signal on specified pin.\n"
284     "\n"
285     "=== Command syntax ===\n"
286     "\n"
287     "   --> houtpwmstartPIN\n"
288     "where PIN is in range 1-6\n",
289     CMD_HANDLER(cmd_do_hout_pwm_start), (void *)&cmd_list_hout
290 };
291
292 /** Command descriptor for PWM stop command */
293 cmd_des_t const cmd_des_hout_pwm_stop={
294     0, 0,
295     "houtpwmstop#","houtpwmstopX - Stop generating of pwm signal on HOUTX",
296     "=== Description ===\n"
297     "\n"
298     "This command stops generating PWM signal on selected pin.\n"
299     "\n"
300     "=== Command syntax ===\n"
301     "\n"
302     "   --> houtpwmstopPIN\n"
303     "where PIN is in range 1-6\n",
304     CMD_HANDLER(cmd_do_hout_pwm_stop), (void *)&cmd_list_hout
305 };
306
307 /** List of commands for hout, defined as external */
308 cmd_des_t const *cmd_list_hout[]={
309   &cmd_des_test_hout_fail,
310   &cmd_des_read_hifbk,
311   &cmd_des_hout_pwm,
312   &cmd_des_hout_pwm_start,
313   &cmd_des_hout_pwm_stop,
314   NULL
315 };