2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
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.
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/>.
25 * Command for processing data from SPI and
26 * lowlevel command for sending data on SPI
30 #include "commands/cmd_spi.h"
32 #include "cmdproc_utils.h"
34 #include <_isfuncdcl.h>
37 * @brief Translate SPI response according the command and peripheral type
39 * Command syntax: spitrNAME?(cmd, resp)
41 * Command take response resp given by last command cmd and translate it according peripheral
42 * type specified by NAME.
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
49 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
54 uint32_t values[MAX_PARAM_VALUES_NUM];
55 uint32_t numFieldDesc;
59 spitr_reg_translate_table_t translatedData;
61 if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
63 const spitr_cmd_map_t* cmdDesc = cmdDesc = get_spi_cmd_map(param[1], param[2]-param[1], &numCmdDesc);
64 if(cmdDesc == NULL) return -CMDERR_BADREG;
70 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
76 const spitr_field_desc_t* fieldDescs = get_spi_field_desc(cmdDesc, numCmdDesc, command, &numFieldDesc);
77 if (fieldDescs == NULL)
78 return -CMDERR_BADPAR;
80 ret = spitr_fill_tr_table(fieldDescs, numFieldDesc, response, &translatedData);
82 for (i = 0; i < translatedData.num_rows; i++) {
83 rpp_sci_printf("%s: %h\r\n", translatedData.row[i].field_name, translatedData.row[i].value);
85 return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
88 return -CMDERR_BADPAR;
93 /* SPI Master testing command */
95 uint8_t spi_test_buf_tx[TEST_BUF];
96 uint8_t spi_test_buf_rx[TEST_BUF];
98 static int test_rd_arr(char **ps, uint8_t *buf, int n)
104 if (si_fndsep(ps, "({") < 0)
105 return -CMDERR_BADSEP;
108 if ((**ps != ')') && (**ps != '}'))
111 return -CMDERR_BADPAR;
112 if (si_long(ps, &val, 16) < 0)
113 return -CMDERR_BADPAR;
116 if ((c = si_fndsep(ps, ",)}")) < 0)
117 return -CMDERR_BADSEP;
123 int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
127 rpp_sci_printf("SPI! %h ", addr);
129 rpp_sci_printf("FAIL ");
131 rpp_sci_printf(" RX(");
132 for (i = 0; i < status; i++) {
133 rpp_sci_printf("%h,", buf[i]);
135 rpp_sci_printf(")\n");
142 * @brief Send SPI command on SPI and receive response
144 * Command syntax: spimst##:ADDR(cmd) - Sends command cmd on SPI ## with address ADDR
146 * @param[in] cmd_io Pointer to IO stack
147 * @param[in] des Pointer to command descriptor
148 * @param[in] param Parameters of command
149 * @return 0 when OK or error code
151 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
161 if (!param[1] || !isdigit(*param[1]))
162 return -CMDERR_BADSUF;
164 if (*param[2] != ':')
165 return -CMDERR_OPCHAR;
167 ifc = spi_find_drv(NULL, *param[1] - '0');
169 return -CMDERR_NODEV;
171 if (!(ifc->flags & SPI_IFC_ON)) {
172 //if (spi_tms570_init(ifc) < 0)
173 return -CMDERR_BADCFG;
180 if (si_long(&p, &val, 16) < 0)
181 return -CMDERR_BADPAR;
185 i = test_rd_arr(&p, spi_test_buf_tx, TEST_BUF);
190 ret = spi_transfer(ifc, addr, rq_len, spi_test_buf_tx, spi_test_buf_rx);
191 spimst_print_rx(ifc, ret, addr, spi_test_buf_rx);
195 /** Command descriptor for SPI response translation */
196 cmd_des_t const cmd_des_spi_translate={
197 0, CDESM_OPCHR|CDESM_RW,
198 "spitr*","Translate response from SPI peripheral. spitrPERNAME?(CMD, RESP)",
199 cmd_do_spi_translate, (void *)&cmd_list_spi
202 /** Command descriptor for SPI trasfer */
203 cmd_des_t const cmd_des_spimst = { 0, CDESM_OPCHR,
204 "spimst#", "SPI master communication request",
205 cmd_do_spimst, (void *)&cmd_list_spi
208 /** List of commands for SPI, defined as external */
209 cmd_des_t const *cmd_list_spi[]={
210 &cmd_des_spi_translate,