]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_spi.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_spi.c
1 /*
2  * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
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.
29  *
30  * File : cmd_spi.c
31  *
32  * Abstract:
33  *      Command for processing data from SPI and
34  *      lowlevel command for sending data on SPI
35  *
36  */
37
38 #include "cmd_spi.h"
39 #include "stdio.h"
40
41 #ifndef DOCGEN
42
43 #include "rpp/rpp.h"
44 #include "cmdproc_utils.h"
45 #include "drv/drv.h"
46 #include <_isfuncdcl.h>
47 #include "spi_resp_transl.h"
48
49 /**
50  *  @brief      Translate SPI response according the command and peripheral type
51  *
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
56  */
57 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
58 {
59         uint32_t command;
60         uint32_t response;
61         char peripheralName[32];
62         char spareParams;
63
64         if (sscanf(param[1], "%31s ", peripheralName) != 1)
65                 return -CMDERR_BADPAR;
66
67         const spitr_port_cmds_t *cmds = spitr_get_cmds_by_port_name(peripheralName);
68         if (cmds == NULL) return -CMDERR_BADREG;
69
70
71         if (param[2] != NULL) {         // More parameters expected
72                 if (sscanf(param[2], "%x %x %1s", &command, &response, &spareParams) != 2)
73                         return -CMDERR_BADPAR;
74
75                 const spitr_cmd_desc_t *cmd_desc = get_spi_cmd_desc(cmds, command);
76                 if (!cmd_desc)
77                         return -CMDERR_BADCMD;
78
79                 spitr_print_resp(cmd_desc, response);
80
81                 return cmd_opchar_replong(cmd_io, param, 0, 0, 10);
82         }
83         else
84                 return -CMDERR_BADPAR;
85 }
86
87
88 /* SPI Master testing command */
89 #define TEST_BUF 64
90 uint8_t spi_test_buf_tx[TEST_BUF];
91 uint8_t spi_test_buf_rx[TEST_BUF];
92
93 int spimst_print_rx(int status, uint8_t *buf)
94 {
95         int i;
96
97         if (status < 0) {
98                 rpp_sci_printf("spirx failed: %d\n", status);
99                 return -1;
100         }
101
102         rpp_sci_printf("spirx:(");
103         for (i = 0; i < status; i++) {
104                 rpp_sci_printf("%#x", buf[i]);
105                 if (i < status-1)
106                         rpp_sci_printf(",");
107         }
108         rpp_sci_printf(")\n");
109
110         return 0;
111 }
112
113
114 /**
115  *  @brief      Send SPI command on SPI and receive response
116  *
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
121  */
122 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
123 {
124         int i;
125         char *p;
126         char *token;
127         int ret;
128         unsigned int rq_len;
129         enum spi_device dev;
130         int values[MAX_PARAM_VALUES_NUM];
131
132         p = param[1];
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, " ");
140         i = 0;
141         while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
142                 if (sscanf(token, "%x", &values[i]) == EOF)
143                         break;
144                 token = strtok(NULL, " ");
145                 spi_test_buf_tx[i] = (uint8_t)values[i];
146                 i++;
147         }
148
149         rq_len = i;
150
151         ret = spi_transfer(dev, rq_len, spi_test_buf_tx, spi_test_buf_rx);
152         spimst_print_rx(ret, spi_test_buf_rx);
153         return 0;
154 }
155
156 #endif  /* DOCGEN */
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"
162         "\n"
163         "    spitr<NAME> <CMD> <RESP>\n"
164         "where\n"
165         "\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"
169         "\n"
170         "### Description ###\n"
171         "\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"
178         "\n"
179         "### Example ###\n"
180         "    --> portvalDINSPI 7F 00 00\n"
181         "    portvalDINSPI=AAC03F\n"
182         "    --> spitrDINSPI 7F0000 3FC0AA\n"
183         "    Thermal flag       : 0\n"
184         "    INT flag   : 0\n"
185         "    SP0  - DIN0        : 1\n"
186         "    SP1  - DIN1        : 1\n"
187         "    SP2  - DIN2        : 1\n"
188         "    SP3  - DIN3        : 1\n"
189         "    SP4  - DIN4        : 1\n"
190         "    SP5  - DIN5        : 1\n"
191         "    SP6  - DIN6        : 1\n"
192         "    SP7  - DIN7        : 1\n"
193         "    SG0  - DIN8        : 0\n"
194         "    SG1  - DIN9        : 1\n"
195         "    SG2  - DIN10       : 0\n"
196         "    SG3  - DIN11       : 1\n"
197         "    SG4  - DIN12       : 0\n"
198         "    SG5  - DIN13       : 1\n"
199         "    SG6  - DIN14       : 0\n"
200         "    SG7  - DIN15       : 1\n"
201         "    SG8  - NA  : 0\n"
202         "    SG9  - NA  : 0\n"
203         "    SG10 - NA  : 0\n"
204         "    SG11 - NA  : 0\n"
205         "    SG12 - NA  : 0\n"
206         "    SG13 - NA  : 0\n"
207         "    spitrDINSPI=24\n"
208         "\n"
209         "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
210         "readable form.\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
214 };
215
216 /** Command descriptor for SPI trasfer */
217 cmd_des_t const cmd_des_spimst = {
218         0, 0,
219         "spimst*", "Request SPI master communication",
220         "### Command syntax ###\n"
221         "\n"
222         "    spimst<SPI> <ADDR> <DATA>\n"
223         "where\n"
224         "\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"
228         "\n"
229         "### Description ###\n"
230         "\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"
233         "parentheses.\n"
234         "\n"
235         "### Example ###\n"
236         "\n"
237         "    --> spimst1 0 7F 00 00\n"
238         "    spirx:0x0(0x3f,0xc0,0xff)\n"
239         "\n"
240         "Sends reset command (0x7F0000) to the DIN peripheral.\n",
241         CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
242 };
243
244 /** List of commands for SPI, defined as external */
245 cmd_des_t const *cmd_list_spi[] = {
246         &cmd_des_spi_translate,
247         &cmd_des_spimst,
248         NULL
249 };