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