]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/cmdproc/src/commands/cmd_hout.c
ccd05e849ea7e2eea4079e527c682de6cd29833d
[pes-rpp/rpp-test-sw.git] / rpp / lib / cmdproc / src / commands / cmd_hout.c
1 /*
2  * cmd_hout.c
3  *
4  *  Created on: 28.2.2013
5  *      Author: Michal Horn
6  */
7
8 #include "commands/cmd_hout.h"
9 #include "rpp/rpp.h"
10 #include "hal/hal.h"
11 #include "drv/drv.h"
12 #include "cmdproc_utils.h"
13
14
15 /**
16  * @brief       Reads values from HOUT_IFBK pins (subset of ADC)
17  *
18  * Command syntax: readhifbk
19  *
20  * @param[in]   cmd_io  Pointer to IO stack
21  * @param[in]   des             Pointer to command descriptor
22  * @param[in]   param   Parameters of command
23  * @return      0 when OK or error code
24  */
25 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
26 {
27     uint32_t i;                                         // Cycle control variable
28     port_desc_t* desc;
29     uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
30     int ret;
31
32     desc = hal_port_get_dsc(PORT_NAME_HOUTIFBK, -1);
33
34     ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
35     if (ret < 0)
36         return ret;
37
38     rpp_sci_printf("HOUT_IFBK: values: ");
39     /* conversion results :                                       */
40     for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
41         rpp_sci_printf("\r\nChannel: %d\tvalue: %d %h", i+1, values[i], values[i]);
42     }
43     return 0;
44 }
45
46 /**
47  * @brief       Runs test of selected HOUT pin.
48  *
49  * This command runs a test to check if HOUT pin is in good or fault condition.
50  * When HOUT is OK, HOUT_DIAG pin has the same value as HOUT_IN pin.
51  * When HOUT is in fault state, HOUT_DIAG periodically follows HOUT_PIN for 2ms and shorts to ground for 2ms.
52  *
53  * Command syntax: houtfail#
54  *
55  * @param[in]   cmd_io  Pointer to IO stack
56  * @param[in]   des             Pointer to command descriptor
57  * @param[in]   param   Parameters of command
58  * @return      0 when OK or error code
59  */
60 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
61 {
62         int pin;
63         char *p=param[1];
64         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
65         pin--;
66         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
67
68         switch (hout_fail(pin)) {
69         case HOUT_OK:
70                 rpp_sci_printf("OK");
71                 break;
72         case HOUT_FAILED:
73                 rpp_sci_printf("FAIL");
74                 break;
75         case HOUT_NOT_ON:
76                 rpp_sci_printf("NOT RUNNING");
77                 break;
78         default:
79                 rpp_sci_printf("Bad pin selected");
80                 break;
81         }
82         return 0;
83 }
84
85 /**
86  *      @brief  Set or get actual pwm parameters
87  *
88  *      Command syntax: houtpwm:(p,d)   - set p as a period in us and D as duty cycle in % of the period.
89  *                                      houtpwm?        - Get actual PWM parameters, period in us and duty cycle in % of the period.
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_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
97 {
98           char *p;
99           int opchar;
100           int i;
101           uint32_t values[MAX_PARAM_VALUES_NUM];
102           int pin;
103
104           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
105
106           p=param[1];
107           if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
108           pin--;
109           if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
110
111           if(opchar==':'){
112             p=param[3];
113                 si_skspace(&p);
114                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 10);
115                 if (i < 0)
116                         return i;
117                 if (i != 2)
118                         return -CMDERR_BADPAR;
119                 if (values[0] < 1 || values[1] > 100) return -CMDERR_BADPAR;
120                 hout_pwm_set_signal(pin, (double)values[0], values[1]);
121             return 0;
122           }
123           else{
124                 double period = hout_pwm_get_period(pin);
125                 uint32_t duty = hout_pwm_get_duty(pin);
126
127                 rpp_sci_printf("hout%d_pwm_period: %d\r\nhout%d_pwm_duty: %d/%", pin, period, pin, duty);
128             return 0;
129           }
130 }
131
132 /**
133  *      @brief  Start PWM, if it was previously set by houtpwm command
134  *
135  *      Command syntax: houtpwmstart
136  *
137  * @param[in]   cmd_io  Pointer to IO stack
138  * @param[in]   des             Pointer to command descriptor
139  * @param[in]   param   Parameters of command
140  * @return      0 when OK or error code
141  */
142 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
143 {
144         char* p;
145         int pin;
146
147         p=param[1];
148         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
149         pin--;
150         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
151         int ret = hout_pwm_start(pin);
152         if (ret == -1) {
153                 rpp_sci_printf("PWM was not initialized.\r\n");
154                 return -CMDERR_BADCFG;
155         }
156         else {
157                 return 0;
158         }
159 }
160
161 /**
162  *      @brief  Stop PWM
163  *
164  *      Command syntax: houtpwmstop
165  *
166  * @param[in]   cmd_io  Pointer to IO stack
167  * @param[in]   des             Pointer to command descriptor
168  * @param[in]   param   Parameters of command
169  * @return      0 when OK or error code
170  */
171 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
172 {
173         char* p;
174         int pin;
175
176         p=param[1];
177         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
178         pin--;
179         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
180         hout_pwm_stop(pin);
181         return 0;
182 }
183 /** Command descriptor for test hout fault state command */
184 cmd_des_t const cmd_des_test_hout_fail = {
185     0, 0,
186     "houtfail","Test if some HOUT port is in fault state",
187     cmd_do_test_hout_fault, (void *)&cmd_list_hout
188 };
189
190 /** Command descriptor for hout read IFBK command */
191 cmd_des_t const cmd_des_read_hifbk={
192     0, 0,
193     "readhifbk","Reads values from HOUT IFBK",
194     cmd_do_read_hout_ifbk_values, (void *)&cmd_list_hout
195 };
196
197 /** Command descriptor for HBR set PWM command */
198 cmd_des_t const cmd_des_hout_pwm={
199     0, CDESM_OPCHR|CDESM_RW,
200     "houtpwm*","\t- houtpwmX:(period,duty) set pwm period in us and duty in % of period\r\n\t\thoutpwmX? read period in us and duty in % of period from HOUTX.",
201         cmd_do_hout_pwm, (void *)&cmd_list_hout
202 };
203
204 /** Command descriptor for PWM start command */
205 cmd_des_t const cmd_des_hout_pwm_start={
206     0, 0,
207     "houtpwmstart#","\t- houtpwmstartX - Start generating of pwm signal on HOUTX",
208         cmd_do_hout_pwm_start, (void *)&cmd_list_hout
209 };
210
211 /** Command descriptor for PWM stop command */
212 cmd_des_t const cmd_des_hout_pwm_stop={
213     0, 0,
214     "houtpwmstop#","\t- houtpwmstopX - Stop generating of pwm signal on HOUTX",
215         cmd_do_hout_pwm_stop, (void *)&cmd_list_hout
216 };
217
218 /** List of commands for hout, defined as external */
219 cmd_des_t const *cmd_list_hout[]={
220   &cmd_des_test_hout_fail,
221   &cmd_des_read_hifbk,
222   &cmd_des_hout_pwm,
223   &cmd_des_hout_pwm_start,
224   &cmd_des_hout_pwm_stop,
225   NULL
226 };