4 * Created on: 28.2.2013
7 * Commands for DIN port control.
8 * - Setting pins to tri-state or not
9 * - Enabling interrupt and wake-up function on pins
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
17 #include "commands/cmd_din.h"
18 #include "cmdproc_utils.h"
23 /** @brief Set pins on DIN port to be active or tri-state.
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.
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.
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
38 int cmd_do_din_st_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
43 uint32_t values[MAX_PARAM_VALUES_NUM];
45 if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
50 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
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);
57 else return -CMDERR_OPCHAR;
60 /** @brief Enables or disables wake-up and interrupt function for pins on DIN port.
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.
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.
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
75 int cmd_do_din_int_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
80 uint32_t values[MAX_PARAM_VALUES_NUM];
82 if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
87 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
89 return -CMDERR_BADPAR;
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);
95 else return -CMDERR_OPCHAR;
98 /** @brief Send reset command to DIN
100 * Function creates, sends SPI command that resets chip MC33972 and receive response.
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
107 int cmd_do_din_reset(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
109 uint32_t spi_resp = din_spi_transfer();
110 rpp_sci_printf("dinreset: %h", spi_resp);
114 /** @brief Send switch status command to DIN
116 * Function creates, sends SPI command that poll new switches state and receive response.
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
123 int cmd_do_din_switch_st(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
125 uint32_t spi_resp = din_spi_transfer();
126 rpp_sci_printf("dinswst: %h", spi_resp);
130 /** @brief Set programable pins as switch-to-battery or switch-to-ground
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.
137 * Function creates, sends SPI command and receive response.
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
144 int cmd_do_din_pr_wr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
149 uint32_t values[MAX_PARAM_VALUES_NUM];
151 if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
156 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
158 return -CMDERR_BADPAR;
160 din_set_pr(values[0]);
161 spi_resp=din_spi_transfer();
162 return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
164 else return -CMDERR_OPCHAR;
167 /** @brief Get state of DIN port as 16-b word
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.
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
177 int cmd_do_din_val_word(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
181 if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
186 spi_resp=din_get_val_word();
187 return cmd_opchar_replong(cmd_io, param, spi_resp, 0, 16);
189 else return -CMDERR_OPCHAR;
192 /** @brief Get state of DIN port in human readable form
194 * Function creates, sends SPI command that poll new switches state,
195 * receives response and translates it to human readable form.
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
202 int cmd_do_din_status(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
205 uint32_t numFieldDesc;
208 uint32_t din_spi_resp = din_spi_transfer();
210 spitr_reg_translate_table_t translatedData;
212 const spitr_cmd_map_t* cmdDesc = get_spi_cmd_map(PORT_NAME_DINSPI, -1, &numCmdDesc);
213 if(cmdDesc == NULL) return -CMDERR_BADREG;
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);
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);
224 return cmd_opchar_replong(cmd_io, param, din_spi_resp, 0, 16);
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
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
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
250 /** Command descriptor for din reset command */
251 cmd_des_t const cmd_des_din_rst={
253 "dinreset","\tdinreset - send reset command to DIN",
254 cmd_do_din_reset, (void *)&cmd_list_din
257 /** Command descriptor for din switch state command */
258 cmd_des_t const cmd_des_din_sw_st={
260 "dinswst","\tdinswst - Send switch status to DIN",
261 cmd_do_din_switch_st, (void *)&cmd_list_din
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
271 /** Command descriptor for din get state command */
272 cmd_des_t const cmd_des_din_stat={
274 "dinstat","\tdinstat - get DIN status in human readable form",
275 cmd_do_din_status, (void *)&cmd_list_din
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,
285 &cmd_des_din_val_word,