]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/cmdproc/src/commands/cmd_din.c
cb96eca0f7a4b97d58935f10e3d4b4e272286275
[pes-rpp/rpp-test-sw.git] / rpp / lib / cmdproc / src / commands / cmd_din.c
1 /*
2  * cmd_din.c
3  *
4  *  Created on: 28.2.2013
5  *      Author: Michal Horn
6  *
7  *  Commands for DIN port control.
8  *  - Setting pins to tri-state or not
9  *  - Enabling interrupt and wake-up function on pins
10  *  - Reseting DIN
11  *  - Switching status on DIN
12  *  - Setting programable pins as switch-to-ground and switch-to-battery
13  *  - Getting port state as a number
14  *  - Getting port state in human readable form
15  */
16
17 #include "commands/cmd_din.h"
18 #include "cmdproc_utils.h"
19 #include "drv/din.h"
20 #include "rpp/rpp.h"
21 #include "hal/hal.h"
22
23 /**     @brief  Set pins on DIN port to be active or tri-state.
24  *
25  * Function expects two hexa numbers after ':' in brackets, ie command:(BB,AA).
26  * First number sets state of pins SG0 - SG13, second number sets state of pins SP0 - SP7.
27  * Bits are assigned from the left to the right pins SP0, SP1...SP7 and SG0, SG1..SG13.
28  * 1 set the pin to be tri-state, 0 set the pin to be active.
29  *
30  * Function creates, sends two SPI commands. One is for SP and the second one is for SG pins.
31  * Command receive response from the second command.
32  *
33  * @param[in]   cmd_io  Pointer to IO stack
34  * @param[in]   des             Pointer to command descriptor
35  * @param[in]   param   Parameters of command
36  * @return      0 when OK or error code
37  */
38 int cmd_do_din_st_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
39           char *p;
40           int opchar;
41           int i;
42           uint32_t spi_resp;
43           uint32_t values[MAX_PARAM_VALUES_NUM];
44
45           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
46
47           if(opchar==':'){
48             p=param[3];
49                 si_skspace(&p);
50                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
51                 if (i != 2)
52                         return -CMDERR_BADPAR;
53             din_set_stat(values[1], values[0]);
54             spi_resp=din_spi_transfer();
55             return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
56           }
57           else return -CMDERR_OPCHAR;
58 }
59
60 /**     @brief  Enables or disables wake-up and interrupt function for pins on DIN port.
61  *
62  * Function expects two hexa numbers after ':' in brackets, ie command:(BB,AA).
63  * First number sets pins SG0 - SG13, second number sets pins SP0 - SP7.
64  * Bits are assigned from the left to the right to pins SP0, SP1...SP7 and SG0, SG1..SG13.
65  * 1 - enable interrupt and wake-up function on the pin, 0 - disable those function on the pin.
66  *
67  * Function creates, sends two SPI commands. One is for SP and the second one is for SG pins.
68  * Command receive response from the second command.
69  *
70  * @param[in]   cmd_io  Pointer to IO stack
71  * @param[in]   des             Pointer to command descriptor
72  * @param[in]   param   Parameters of command
73  * @return      0 when OK or error code
74  */
75 int cmd_do_din_int_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
76           char *p;
77           int opchar;
78           int i;
79           uint32_t spi_resp;
80           uint32_t values[MAX_PARAM_VALUES_NUM];
81
82           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
83
84           if(opchar==':'){
85             p=param[3];
86                 si_skspace(&p);
87                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
88                 if (i != 2)
89                         return -CMDERR_BADPAR;
90
91             din_set_int(values[1], values[0]);
92             spi_resp=din_spi_transfer();
93             return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
94           }
95           else return -CMDERR_OPCHAR;
96 }
97
98 /**     @brief  Send reset command to DIN
99  *
100  * Function creates, sends SPI command that resets chip MC33972 and receive response.
101  *
102  * @param[in]   cmd_io  Pointer to IO stack
103  * @param[in]   des             Pointer to command descriptor
104  * @param[in]   param   Parameters of command
105  * @return      0 when OK or error code
106  */
107 int cmd_do_din_reset(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
108         din_reset();
109         uint32_t spi_resp = din_spi_transfer();
110         rpp_sci_printf("dinreset: %h", spi_resp);
111         return 0;
112 }
113
114 /**     @brief  Send switch status command to DIN
115  *
116  * Function creates, sends SPI command that poll new switches state and receive response.
117  *
118  * @param[in]   cmd_io  Pointer to IO stack
119  * @param[in]   des             Pointer to command descriptor
120  * @param[in]   param   Parameters of command
121  * @return      0 when OK or error code
122  */
123 int cmd_do_din_switch_st(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
124         din_switch_st();
125         uint32_t spi_resp = din_spi_transfer();
126         rpp_sci_printf("dinswst: %h", spi_resp);
127         return 0;
128 }
129
130 /**     @brief  Set programable pins as switch-to-battery or switch-to-ground
131  *
132  * Function expects one hexa number after ':' in brackets, ie command:(AA).
133  * The number sets pins SP0 - SP7.
134  * Bits are assigned from the left to the right to pins SP0, SP1...SP7.
135  * 1 - set pin as switch-to-battery, 0 - set pin as switch-to-ground.
136  *
137  * Function creates, sends SPI command and receive response.
138  *
139  * @param[in]   cmd_io  Pointer to IO stack
140  * @param[in]   des             Pointer to command descriptor
141  * @param[in]   param   Parameters of command
142  * @return      0 when OK or error code
143  */
144 int cmd_do_din_pr_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
145           char *p;
146           int opchar;
147           int i;
148           uint32_t spi_resp;
149           uint32_t values[MAX_PARAM_VALUES_NUM];
150
151           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
152
153           if(opchar==':'){
154             p=param[3];
155                 si_skspace(&p);
156                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
157                 if (i != 1)
158                         return -CMDERR_BADPAR;
159
160             din_set_pr(values[0]);
161             spi_resp=din_spi_transfer();
162             return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
163           }
164           else return -CMDERR_OPCHAR;
165 }
166
167 /**     @brief  Get state of DIN port as 16-b word
168  *
169  * Function creates, sends SPI command that poll new switches state,
170  * receives response and filters just the pins, that are connected to the MC33972.
171  *
172  * @param[in]   cmd_io  Pointer to IO stack
173  * @param[in]   des             Pointer to command descriptor
174  * @param[in]   param   Parameters of command
175  * @return      0 when OK or error code
176  */
177 int cmd_do_din_val_word(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
178           int opchar;
179           uint32_t spi_resp;
180
181           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
182
183           if(opchar=='?') {
184             din_switch_st();
185                 din_spi_transfer();
186                 spi_resp=din_get_val_word();
187             return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
188           }
189           else return -CMDERR_OPCHAR;
190 }
191
192 /**     @brief  Get state of DIN port in human readable form
193  *
194  * Function creates, sends SPI command that poll new switches state,
195  * receives response and translates it to human readable form.
196  *
197  * @param[in]   cmd_io  Pointer to IO stack
198  * @param[in]   des             Pointer to command descriptor
199  * @param[in]   param   Parameters of command
200  * @return      0 when OK or error code
201  */
202 int cmd_do_din_status(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
203         uint32_t numCmdDesc;
204         int i;
205         uint32_t numFieldDesc;
206
207         din_switch_st();
208         uint32_t din_spi_resp = din_spi_transfer();
209
210         spitr_reg_translate_table_t translatedData;
211
212         const spitr_cmd_map_t* cmdDesc = get_spi_cmd_map(PORT_NAME_DINSPI, -1, &numCmdDesc);
213         if(cmdDesc == NULL) return -CMDERR_BADREG;
214
215         const spitr_field_desc_t* fieldDescs = get_spi_field_desc(cmdDesc, numCmdDesc, din_spi_get_cmd(), &numFieldDesc);
216         if (fieldDescs == NULL)
217                 return -CMDERR_BADPAR;
218         uint32_t lsbResponse = 0 | ((din_spi_resp & 0xFF) << 16 ) | ((din_spi_resp & 0xFF0000) >> 16) | (din_spi_resp & 0xFF00);
219         spitr_fill_tr_table(fieldDescs, numFieldDesc, lsbResponse, &translatedData);
220
221         for (i = 0; i < translatedData.num_rows; i++) {
222                 rpp_sci_printf("%s: %h\r\n", translatedData.row[i].field_name, translatedData.row[i].value);
223         }
224         return cmd_opchar_replong(cmd_io, param, din_spi_resp, 0, 16);
225 }
226
227
228
229 /** Command descriptor for din state command */
230 cmd_des_t const cmd_des_din_st_word={
231         0, CDESM_OPCHR|CDESM_WR,
232         "dinportst","\tdinportst:(wrsg,wrsp) - writes word wr (8b) into din sg and sp. 1 - tri-state, 0 - active.",
233         cmd_do_din_st_wr, (void *)&cmd_list_din
234 };
235
236 /** Command descriptor for din switch-to-ground/battery command */
237 cmd_des_t const cmd_des_din_pr_word={
238         0, CDESM_OPCHR|CDESM_WR,
239         "dinportpr","\tdinportpr:(wr) - writes word wr (8b) into din. 1 - switch-to-bat, 0 - switch-to-gnd.",
240         cmd_do_din_pr_wr, (void *)&cmd_list_din
241 };
242
243 /** Command descriptor for din enable interrupts command */
244 cmd_des_t const cmd_des_din_int_word={
245         0, CDESM_OPCHR|CDESM_WR,
246         "dinportint","\tdinportint:(wrsg, wrsp) - writes word wr (16b) into din sg and sp. 1 - enable interrupt, 0 - disable interrupt.",
247         cmd_do_din_int_wr, (void *)&cmd_list_din
248 };
249
250 /** Command descriptor for din reset command */
251 cmd_des_t const cmd_des_din_rst={
252         0, 0,
253         "dinreset","\tdinreset - send reset command to DIN",
254         cmd_do_din_reset, (void *)&cmd_list_din
255 };
256
257 /** Command descriptor for din switch state command */
258 cmd_des_t const cmd_des_din_sw_st={
259         0, 0,
260         "dinswst","\tdinswst - Send switch status to DIN",
261         cmd_do_din_switch_st, (void *)&cmd_list_din
262 };
263
264 /** Command descriptor for din get value command */
265 cmd_des_t const cmd_des_din_val_word={
266         0, CDESM_OPCHR|CDESM_RD,
267         "dinportval","\tdinportval? - reads values from DIN port as 16b word",
268         cmd_do_din_val_word, (void *)&cmd_list_din
269 };
270
271 /** Command descriptor for din get state command */
272 cmd_des_t const cmd_des_din_stat={
273         0, 0,
274         "dinstat","\tdinstat - get DIN status in human readable form",
275         cmd_do_din_status, (void *)&cmd_list_din
276 };
277
278 /** List of commands for din, defined as external */
279 cmd_des_t const *cmd_list_din[]={
280   &cmd_des_din_st_word,
281   &cmd_des_din_pr_word,
282   &cmd_des_din_int_word,
283   &cmd_des_din_rst,
284   &cmd_des_din_sw_st,
285   &cmd_des_din_val_word,
286   &cmd_des_din_stat,
287   NULL
288 };