]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - source/cmd_hout.c
Source and Header files modified
[pes-rpp/rpp-test-sw.git] / source / cmd_hout.c
1 /*
2  * cmd_hout.c
3  *
4  *  Created on: 28.2.2013
5  *      Author: Michal Horn
6  */
7
8 #include "cmd_hout.h"
9 #include "cmdio_tisci.h"
10 #include "port_def.h"
11 #include "utils.h"
12 #include "hal_gpio_tms570.h"
13 #include "drv_hout.h"
14
15
16 /**
17  * @brief       Reads values from HOUT_IFBK pins (subset of ADC)
18  *
19  * Command syntax: readhifbk
20  *
21  * @param[in]   cmd_io  Pointer to IO stack
22  * @param[in]   des             Pointer to command descriptor
23  * @param[in]   param   Parameters of command
24  * @return      0 when OK or error code
25  */
26 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
27 {
28         char numBuf[32];                                        // Buffer used for i2str conversion
29     uint32_t i;                                         // Cycle control variable
30     port_desc_t* desc;
31     uint32_t values[PORT_HOUTIFBK_CHANNEL_NUM*2];
32     int ret;
33
34     desc = port_get_dsc(PORT_NAME_HOUTIFBK, -1);
35
36     ret = desc->port_getfnc_ptr(desc->config, PORT_HOUTIFBK_CHANNEL_NUM, values);
37     if (ret < 0)
38         return ret;
39
40     print((uint8_t *)"\r\nHOUT_IFBK: values: ");
41     /* conversion results :                                       */
42     for (i = 0; i < PORT_HOUTIFBK_CHANNEL_NUM; i++) {
43         print((uint8_t *)"\r\nChannel: ");
44         i2str(numBuf, values[i+PORT_HOUTIFBK_CHANNEL_NUM], 1, 10);
45         print((uint8_t *)numBuf);
46         print((uint8_t *)"\tvalue: ");
47         i2str(numBuf, values[i], 1, 10);
48         print((uint8_t *)numBuf);
49         print((uint8_t *)" 0x");
50         i2str(numBuf, values[i], 1, 16);
51         print((uint8_t *)numBuf);
52     }
53     return 0;
54 }
55
56 //TODO: Repair this function - HOUT_DIAG id periodicaly shorted to ground and each 2ms
57 /**
58  * @brief       Runs test of HOUT. Test, weather HOUT is in fault state, which means that HOUT_DIAG is shorted to ground
59  *
60  * Command syntax: houtfail
61  *
62  * @param[in]   cmd_io  Pointer to IO stack
63  * @param[in]   des             Pointer to command descriptor
64  * @param[in]   param   Parameters of command
65  * @return      0 when OK or error code
66  */
67 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
68 {
69         print((uint8_t *)"Testing fault state of HOUT port:");
70         uint8_t i;
71         uint32_t values[] = {1, 1, 1, 1, 1, 1};
72
73         port_desc_t* inDesc = port_get_dsc(PORT_NAME_HOUTIN, -1);
74         port_desc_t* diagDesc = port_get_dsc(PORT_NAME_HOUTDIAG, -1);
75
76         inDesc->port_setfnc_ptr(inDesc->config, inDesc->numValues, values);
77         vTaskDelay(100/portTICK_RATE_MS);
78         for (i = 0; i < inDesc->numValues; i++) {
79                 if (hal_gpio_get_value(diagDesc->config[i]) == values[i]) {
80                         print((uint8_t *)"\r\nOK");
81                 }
82                 else {
83                         print((uint8_t *)"\r\nFAIL");
84                 }
85                 values[i] = 0;
86         }
87         inDesc->port_setfnc_ptr(inDesc->config, inDesc->numValues, values);
88         return 0;
89 }
90
91 /**
92  *      @brief  Set or get actual pwm parameters
93  *
94  *      Command syntax: houtpwm:(p,d)   - set p as a period in us and D as duty cycle in % of the period.
95  *                                      houtpwm?        - Get actual PWM parameters, period in us and duty cycle in % of the period.
96  *
97  * @param[in]   cmd_io  Pointer to IO stack
98  * @param[in]   des             Pointer to command descriptor
99  * @param[in]   param   Parameters of command
100  * @return      0 when OK or error code
101  */
102 int cmd_do_hout_pwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
103 {
104           char *p;
105           int opchar;
106           int i;
107           uint32_t values[MAX_PARAM_VALUES_NUM];
108           char buf[16];
109           int pin;
110
111           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
112
113           p=param[1];
114           if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
115           pin--;
116           if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
117
118           if(opchar==':'){
119             p=param[3];
120                 si_skspace(&p);
121                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 10);
122                 if (i < 0)
123                         return i;
124                 if (i != 2)
125                         return -CMDERR_BADPAR;
126                 if (values[0] < 1 || values[1] > 100) return -CMDERR_BADPAR;
127                 hout_pwm_set_signal(pin, (double)values[0], values[1]);
128             return 0;
129           }
130           else{
131                 double period = hout_pwm_get_period(pin);
132                 uint32_t duty = hout_pwm_get_duty(pin);
133
134                 print((uint8_t *)"hout");
135                 i2str(buf, pin, 0, 10);
136                 print((uint8_t *)"_pwm_period: ");
137                 i2str(buf, period, 0, 10);
138                 print((uint8_t *)buf);
139                 print((uint8_t *)"\r\nhout");
140                 i2str(buf, pin, 0, 10);
141                 print((uint8_t *)"_pwm_duty: ");
142                 i2str(buf, duty, 0, 10);
143                 print((uint8_t *)buf);
144                 print((uint8_t *)"%");
145             return 0;
146           }
147 }
148
149 /**
150  *      @brief  Start PWM, if it was previously set by houtpwm command
151  *
152  *      Command syntax: houtpwmstart
153  *
154  * @param[in]   cmd_io  Pointer to IO stack
155  * @param[in]   des             Pointer to command descriptor
156  * @param[in]   param   Parameters of command
157  * @return      0 when OK or error code
158  */
159 int cmd_do_hout_pwm_start(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
160 {
161         char* p;
162         int pin;
163
164         p=param[1];
165         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
166         pin--;
167         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
168         int ret = hout_pwm_start(pin);
169         if (ret == -1) {
170                 print((uint8_t*)"PWM was not initialized.\r\n");
171                 return -CMDERR_BADCFG;
172         }
173         else {
174                 return 0;
175         }
176 }
177
178 /**
179  *      @brief  Stop PWM
180  *
181  *      Command syntax: houtpwmstop
182  *
183  * @param[in]   cmd_io  Pointer to IO stack
184  * @param[in]   des             Pointer to command descriptor
185  * @param[in]   param   Parameters of command
186  * @return      0 when OK or error code
187  */
188 int cmd_do_hout_pwm_stop(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
189 {
190         char* p;
191         int pin;
192
193         p=param[1];
194         if(si_long(&p,(long*)&pin,0)<0) return -CMDERR_BADPAR;
195         pin--;
196         if (pin < 0 || pin > 5) return -CMDERR_BADPAR;
197         hout_pwm_stop(pin);
198         return 0;
199 }
200 /** Command descriptor for test hout fault state command */
201 cmd_des_t const cmd_des_test_hout_fail = {
202     0, 0,
203     "houtfail","Test if some HOUT port is in fault state",
204     cmd_do_test_hout_fault, (void *)&cmd_list_hout
205 };
206
207 /** Command descriptor for hout read IFBK command */
208 cmd_des_t const cmd_des_read_hifbk={
209     0, 0,
210     "readhifbk","Reads values from HOUT IFBK",
211     cmd_do_read_hout_ifbk_values, (void *)&cmd_list_hout
212 };
213
214 /** Command descriptor for HBR set PWM command */
215 cmd_des_t const cmd_des_hout_pwm={
216     0, CDESM_OPCHR|CDESM_RW,
217     "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.",
218         cmd_do_hout_pwm, (void *)&cmd_list_hout
219 };
220
221 /** Command descriptor for PWM start command */
222 cmd_des_t const cmd_des_hout_pwm_start={
223     0, 0,
224     "houtpwmstart#","\t- houtpwmstartX - Start generating of pwm signal on HOUTX",
225         cmd_do_hout_pwm_start, (void *)&cmd_list_hout
226 };
227
228 /** Command descriptor for PWM stop command */
229 cmd_des_t const cmd_des_hout_pwm_stop={
230     0, 0,
231     "houtpwmstop#","\t- houtpwmstopX - Stop generating of pwm signal on HOUTX",
232         cmd_do_hout_pwm_stop, (void *)&cmd_list_hout
233 };
234
235 /** List of commands for hout, defined as external */
236 cmd_des_t const *cmd_list_hout[]={
237   &cmd_des_test_hout_fail,
238   &cmd_des_read_hifbk,
239   &cmd_des_hout_pwm,
240   &cmd_des_hout_pwm_start,
241   &cmd_des_hout_pwm_stop,
242   NULL
243 };