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
36 #include "cmdproc_utils.h"
38 #include <_isfuncdcl.h>
41 * @brief Translate SPI response according the command and peripheral type
43 * @param[in] cmd_io Pointer to IO stack
44 * @param[in] des Pointer to command descriptor
45 * @param[in] param Parameters of command
46 * @return 0 when OK or error code
48 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
53 uint32_t values[MAX_PARAM_VALUES_NUM];
54 uint32_t numFieldDesc;
58 spitr_reg_translate_table_t translatedData;
59 char peripheralName[32];
63 if (sscanf(p, "%31s ", peripheralName) != 1)
64 return -CMDERR_BADPAR;
66 const spitr_cmd_map_t *cmdDesc = cmdDesc = get_spi_cmd_map(peripheralName, -1, &numCmdDesc);
67 if (cmdDesc == NULL) return -CMDERR_BADREG;
70 if (param[2] != NULL) { // More parameters expected
72 if (sscanf(p, "%x %x %1s", &values[0], &values[1], &spareParams) != 2)
73 return -CMDERR_BADPAR;
77 const spitr_field_desc_t *fieldDescs = get_spi_field_desc(cmdDesc, numCmdDesc, command, &numFieldDesc);
78 if (fieldDescs == NULL)
79 return -CMDERR_BADPAR;
81 ret = spitr_fill_tr_table(fieldDescs, numFieldDesc, response, &translatedData);
83 for (i = 0; i < translatedData.num_rows; i++) {
84 rpp_sci_printf("%s: %x\r\n", translatedData.row[i].field_name, translatedData.row[i].value);
86 return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
89 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 int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
103 rpp_sci_printf("spirx failed: %d\n", status);
107 rpp_sci_printf("spirx:%#x(", addr);
108 for (i = 0; i < status; i++) {
109 rpp_sci_printf("%#x", buf[i]);
113 rpp_sci_printf(")\n");
120 * @brief Send SPI command on SPI and receive response
122 * @param[in] cmd_io Pointer to IO stack
123 * @param[in] des Pointer to command descriptor
124 * @param[in] param Parameters of command
125 * @return 0 when OK or error code
127 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
137 int values[MAX_PARAM_VALUES_NUM];
140 if (sscanf(p, "%d", &spi) != 1)
141 return -CMDERR_BADPAR;
143 if (sscanf(p, " %d", &addr) != 1)
144 return -CMDERR_BADPAR;
145 token = strtok(p, " ");
146 token = strtok(NULL, " ");
148 while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
149 if (sscanf(token, "%x", &values[i]) == EOF)
151 token = strtok(NULL, " ");
152 spi_test_buf_tx[i] = (uint8_t)values[i];
157 ifc = spi_find_drv(NULL, spi);
159 return -CMDERR_NODEV;
161 if (!(ifc->flags & SPI_IFC_ON))
162 //if (spi_tms570_init(ifc) < 0)
163 return -CMDERR_BADCFG;
167 ret = spi_transfer(ifc, addr, rq_len, spi_test_buf_tx, spi_test_buf_rx);
168 spimst_print_rx(ifc, ret, addr, spi_test_buf_rx);
173 /** Command descriptor for SPI response translation */
174 cmd_des_t const cmd_des_spi_translate = {
175 0, CDESM_OPCHR|CDESM_RW,
176 "spitr*","Translate response from an SPI peripheral",
177 "### Command syntax ###\n"
179 " spitr<NAME> <CMD> <RESP>\n"
182 "- `<NAME>` is a string specifying the name of the peripheral (one of DINSPI, LOUT, DAC12, DAC34, HBR, FRAY1 and FRAY2)\n"
183 "- `<CMD>` is a hexadecimal number in range 0 - FFFFFFFF\n"
184 "- `<RESP>` is a hexadecimal number in range 0 - FFFFFFFF\n"
186 "### Description ###\n"
188 "This command translates a response from SPI from many different\n"
189 "peripherals into a human readable form. The SPI response is in the\n"
190 "form of a hexadecimal number, which encodes the information. This\n"
191 "commands takes this response, the command which produced this\n"
192 "response, the name of the peripheral and translates the response into\n"
193 "the attribute-value table.\n"
196 " --> portvalDINSPI 7F 00 00\n"
197 " portvalDINSPI=AAC03F\n"
198 " --> spitrDINSPI 7F0000 3FC0AA\n"
199 " Thermal flag : 0\n"
225 "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
227 "Please notice LSB->MSB conversion of the portval result. The necessity\n"
228 "of the conversion depends on the controller of the examined port.\n",
229 CMD_HANDLER(cmd_do_spi_translate), (void *)&cmd_list_spi
232 /** Command descriptor for SPI trasfer */
233 cmd_des_t const cmd_des_spimst = {
235 "spimst*", "Request SPI master communication",
236 "### Command syntax ###\n"
238 " spimst<SPI> <ADDR> <DATA>\n"
241 "- `<SPI>` is a number in range 0 - 4\n"
242 "- `<ADDR>` is a number in range 0 - 2\n"
243 "- `<DATA>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n"
245 "### Description ###\n"
247 "The command sends given data to the SPI peripheral and prints the\n"
248 "response. The response contains the address and the received data in\n"
253 " --> spimst1 0 7F 00 00\n"
254 " spirx:0x0(0x3f,0xc0,0xff)\n"
256 "Sends reset command (0x7F0000) to the DIN peripheral.\n",
257 CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
260 /** List of commands for SPI, defined as external */
261 cmd_des_t const *cmd_list_spi[] = {
262 &cmd_des_spi_translate,