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