2 * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
33 * Command for processing data from SPI and
34 * lowlevel command for sending data on SPI
44 #include "cmdproc_utils.h"
46 #include <_isfuncdcl.h>
47 #include "spi_resp_transl.h"
50 * @brief Translate SPI response according the command and peripheral type
52 * @param[in] cmd_io Pointer to IO stack
53 * @param[in] des Pointer to command descriptor
54 * @param[in] param Parameters of command
55 * @return 0 when OK or error code
57 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
61 char peripheralName[32];
64 if (sscanf(param[1], "%31s ", peripheralName) != 1)
65 return -CMDERR_BADPAR;
67 const spitr_port_cmds_t *cmds = spitr_get_cmds_by_port_name(peripheralName);
68 if (cmds == NULL) return -CMDERR_BADREG;
71 if (param[2] != NULL) { // More parameters expected
72 if (sscanf(param[2], "%x %x %1s", &command, &response, &spareParams) != 2)
73 return -CMDERR_BADPAR;
75 const spitr_cmd_desc_t *cmd_desc = get_spi_cmd_desc(cmds, command);
77 return -CMDERR_BADCMD;
79 spitr_print_resp(cmd_desc, response);
81 return cmd_opchar_replong(cmd_io, param, 0, 0, 10);
84 return -CMDERR_BADPAR;
88 /* SPI Master testing command */
90 uint8_t spi_test_buf_tx[TEST_BUF];
91 uint8_t spi_test_buf_rx[TEST_BUF];
93 int spimst_print_rx(int status, uint8_t *buf)
98 rpp_sci_printf("spirx failed: %d\n", status);
102 rpp_sci_printf("spirx:(");
103 for (i = 0; i < status; i++) {
104 rpp_sci_printf("%#x", buf[i]);
108 rpp_sci_printf(")\n");
115 * @brief Send SPI command on SPI and receive response
117 * @param[in] cmd_io Pointer to IO stack
118 * @param[in] des Pointer to command descriptor
119 * @param[in] param Parameters of command
120 * @return 0 when OK or error code
122 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
130 int values[MAX_PARAM_VALUES_NUM];
133 if (sscanf(p, "%d", &i) != 1)
134 return -CMDERR_BADPAR;
135 dev = (enum spi_device)i;
136 if (dev >= _SPIDEV_COUNT)
137 return -CMDERR_BADPAR;
138 token = strtok(p, " ");
139 token = strtok(NULL, " ");
141 while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
142 if (sscanf(token, "%x", &values[i]) == EOF)
144 token = strtok(NULL, " ");
145 spi_test_buf_tx[i] = (uint8_t)values[i];
151 ret = spi_transfer(dev, rq_len, spi_test_buf_tx, spi_test_buf_rx);
152 spimst_print_rx(ret, spi_test_buf_rx);
157 /** Command descriptor for SPI response translation */
158 cmd_des_t const cmd_des_spi_translate = {
159 0, CDESM_OPCHR|CDESM_RW,
160 "spitr*","Translate response from an SPI peripheral",
161 "### Command syntax ###\n"
163 " spitr<NAME> <CMD> <RESP>\n"
166 "- `<NAME>` is a string specifying the name of the peripheral (one of DINSPI, LOUT, DAC12, DAC34, HBR, FRAY1 and FRAY2)\n"
167 "- `<CMD>` is a hexadecimal number in range 0 - FFFFFFFF\n"
168 "- `<RESP>` is a hexadecimal number in range 0 - FFFFFFFF\n"
170 "### Description ###\n"
172 "This command translates a response from SPI from many different\n"
173 "peripherals into a human readable form. The SPI response is in the\n"
174 "form of a hexadecimal number, which encodes the information. This\n"
175 "commands takes this response, the command which produced this\n"
176 "response, the name of the peripheral and translates the response into\n"
177 "the attribute-value table.\n"
180 " --> portvalDINSPI 7F 00 00\n"
181 " portvalDINSPI=AAC03F\n"
182 " --> spitrDINSPI 7F0000 3FC0AA\n"
183 " Thermal flag : 0\n"
209 "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
211 "Please notice LSB->MSB conversion of the portval result. The necessity\n"
212 "of the conversion depends on the controller of the examined port.\n",
213 CMD_HANDLER(cmd_do_spi_translate), (void *)&cmd_list_spi
216 /** Command descriptor for SPI trasfer */
217 cmd_des_t const cmd_des_spimst = {
219 "spimst*", "Request SPI master communication",
220 "### Command syntax ###\n"
222 " spimst<SPI> <ADDR> <DATA>\n"
225 "- `<SPI>` is a number in range 0 - 4\n"
226 "- `<ADDR>` is a number in range 0 - 2\n"
227 "- `<DATA>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n"
229 "### Description ###\n"
231 "The command sends given data to the SPI peripheral and prints the\n"
232 "response. The response contains the address and the received data in\n"
237 " --> spimst1 0 7F 00 00\n"
238 " spirx:0x0(0x3f,0xc0,0xff)\n"
240 "Sends reset command (0x7F0000) to the DIN peripheral.\n",
241 CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
244 /** List of commands for SPI, defined as external */
245 cmd_des_t const *cmd_list_spi[] = {
246 &cmd_des_spi_translate,