]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_spi.c
4bc1e94f0eaa21e2fc70c4f0bdedb966f81d2a36
[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  * This document contains proprietary information belonging to Czech
10  * Technical University in Prague. Passing on and copying of this
11  * document, and communication of its contents is not permitted
12  * without prior written authorization.
13  *
14  * File : cmd_spi.c
15  *
16  * Abstract:
17  *      Command for processing data from SPI and
18  *      lowlevel command for sending data on SPI
19  *
20  */
21
22 #include "cmd_spi.h"
23 #include "stdio.h"
24
25 #ifndef DOCGEN
26
27 #include "rpp/rpp.h"
28 #include "cmdproc_utils.h"
29 #include "drv/drv.h"
30 #include <_isfuncdcl.h>
31 #include "spi_resp_transl.h"
32
33 /**
34  *  @brief      Translate SPI response according the command and peripheral type
35  *
36  * @param[in]   cmd_io  Pointer to IO stack
37  * @param[in]   des             Pointer to command descriptor
38  * @param[in]   param   Parameters of command
39  * @return      0 when OK or error code
40  */
41 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
42 {
43         uint32_t command;
44         uint32_t response;
45         char peripheralName[32];
46         char spareParams;
47
48         if (sscanf(param[1], "%31s ", peripheralName) != 1)
49                 return -CMDERR_BADPAR;
50
51         const spitr_port_cmds_t *cmds = spitr_get_cmds_by_port_name(peripheralName);
52         if (cmds == NULL) return -CMDERR_BADREG;
53
54
55         if (param[2] != NULL) {         // More parameters expected
56                 if (sscanf(param[2], "%x %x %1s", &command, &response, &spareParams) != 2)
57                         return -CMDERR_BADPAR;
58
59                 const spitr_cmd_desc_t *cmd_desc = get_spi_cmd_desc(cmds, command);
60                 if (!cmd_desc)
61                         return -CMDERR_BADCMD;
62
63                 spitr_print_resp(cmd_desc, response);
64
65                 return cmd_opchar_replong(cmd_io, param, 0, 0, 10);
66         }
67         else
68                 return -CMDERR_BADPAR;
69 }
70
71
72 /* SPI Master testing command */
73 #define TEST_BUF 64
74 uint8_t spi_test_buf_tx[TEST_BUF];
75 uint8_t spi_test_buf_rx[TEST_BUF];
76
77 int spimst_print_rx(int status, uint8_t *buf)
78 {
79         int i;
80
81         if (status < 0) {
82                 rpp_sci_printf("spirx failed: %d\n", status);
83                 return -1;
84         }
85
86         rpp_sci_printf("spirx:(");
87         for (i = 0; i < status; i++) {
88                 rpp_sci_printf("%#x", buf[i]);
89                 if (i < status-1)
90                         rpp_sci_printf(",");
91         }
92         rpp_sci_printf(")\n");
93
94         return 0;
95 }
96
97
98 /**
99  *  @brief      Send SPI command on SPI and receive response
100  *
101  * @param[in]   cmd_io  Pointer to IO stack
102  * @param[in]   des             Pointer to command descriptor
103  * @param[in]   param   Parameters of command
104  * @return      0 when OK or error code
105  */
106 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
107 {
108         int i;
109         char *p;
110         char *token;
111         int ret;
112         unsigned int rq_len;
113         enum spi_device dev;
114         int values[MAX_PARAM_VALUES_NUM];
115
116         p = param[1];
117         if (sscanf(p, "%d", &i) != 1)
118                 return -CMDERR_BADPAR;
119         dev = (enum spi_device)i;
120         if (dev >= _SPIDEV_COUNT)
121                 return -CMDERR_BADPAR;
122         token = strtok(p, " ");
123         token = strtok(NULL, " ");
124         i = 0;
125         while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
126                 if (sscanf(token, "%x", &values[i]) == EOF)
127                         break;
128                 token = strtok(NULL, " ");
129                 spi_test_buf_tx[i] = (uint8_t)values[i];
130                 i++;
131         }
132
133         rq_len = i;
134
135         ret = spi_transfer(dev, rq_len, spi_test_buf_tx, spi_test_buf_rx);
136         spimst_print_rx(ret, spi_test_buf_rx);
137         return 0;
138 }
139
140 #endif  /* DOCGEN */
141 /** Command descriptor for SPI response translation */
142 cmd_des_t const cmd_des_spi_translate = {
143         0, CDESM_OPCHR|CDESM_RW,
144         "spitr*","Translate response from an SPI peripheral",
145         "### Command syntax ###\n"
146         "\n"
147         "    spitr<NAME> <CMD> <RESP>\n"
148         "where\n"
149         "\n"
150         "- `<NAME>` is a string specifying the name of the peripheral (one of DINSPI, LOUT, DAC12, DAC34, HBR, FRAY1 and FRAY2)\n"
151         "- `<CMD>` is a hexadecimal number in range 0 - FFFFFFFF\n"
152         "- `<RESP>` is a hexadecimal number in range 0 - FFFFFFFF\n"
153         "\n"
154         "### Description ###\n"
155         "\n"
156         "This command translates a response from SPI from many different\n"
157         "peripherals into a human readable form. The SPI response is in the\n"
158         "form of a hexadecimal number, which encodes the information. This\n"
159         "commands takes this response, the command which produced this\n"
160         "response, the name of the peripheral and translates the response into\n"
161         "the attribute-value table.\n"
162         "\n"
163         "### Example ###\n"
164         "    --> portvalDINSPI 7F 00 00\n"
165         "    portvalDINSPI=AAC03F\n"
166         "    --> spitrDINSPI 7F0000 3FC0AA\n"
167         "    Thermal flag       : 0\n"
168         "    INT flag   : 0\n"
169         "    SP0  - DIN0        : 1\n"
170         "    SP1  - DIN1        : 1\n"
171         "    SP2  - DIN2        : 1\n"
172         "    SP3  - DIN3        : 1\n"
173         "    SP4  - DIN4        : 1\n"
174         "    SP5  - DIN5        : 1\n"
175         "    SP6  - DIN6        : 1\n"
176         "    SP7  - DIN7        : 1\n"
177         "    SG0  - DIN8        : 0\n"
178         "    SG1  - DIN9        : 1\n"
179         "    SG2  - DIN10       : 0\n"
180         "    SG3  - DIN11       : 1\n"
181         "    SG4  - DIN12       : 0\n"
182         "    SG5  - DIN13       : 1\n"
183         "    SG6  - DIN14       : 0\n"
184         "    SG7  - DIN15       : 1\n"
185         "    SG8  - NA  : 0\n"
186         "    SG9  - NA  : 0\n"
187         "    SG10 - NA  : 0\n"
188         "    SG11 - NA  : 0\n"
189         "    SG12 - NA  : 0\n"
190         "    SG13 - NA  : 0\n"
191         "    spitrDINSPI=24\n"
192         "\n"
193         "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
194         "readable form.\n"
195         "Please notice LSB->MSB conversion of the portval result. The necessity\n"
196         "of the conversion depends on the controller of the examined port.\n",
197         CMD_HANDLER(cmd_do_spi_translate), (void *)&cmd_list_spi
198 };
199
200 /** Command descriptor for SPI trasfer */
201 cmd_des_t const cmd_des_spimst = {
202         0, 0,
203         "spimst*", "Request SPI master communication",
204         "### Command syntax ###\n"
205         "\n"
206         "    spimst<SPI> <ADDR> <DATA>\n"
207         "where\n"
208         "\n"
209         "- `<SPI>` is a number in range 0 - 4\n"
210         "- `<ADDR>` is a number in range 0 - 2\n"
211         "- `<DATA>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n"
212         "\n"
213         "### Description ###\n"
214         "\n"
215         "The command sends given data to the SPI peripheral and prints the\n"
216         "response. The response contains the address and the received data in\n"
217         "parentheses.\n"
218         "\n"
219         "### Example ###\n"
220         "\n"
221         "    --> spimst1 0 7F 00 00\n"
222         "    spirx:0x0(0x3f,0xc0,0xff)\n"
223         "\n"
224         "Sends reset command (0x7F0000) to the DIN peripheral.\n",
225         CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
226 };
227
228 /** List of commands for SPI, defined as external */
229 cmd_des_t const *cmd_list_spi[] = {
230         &cmd_des_spi_translate,
231         &cmd_des_spimst,
232         NULL
233 };