]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/cmdproc/src/commands/cmd_spi.c
Licence header added to all CMD files, comments to functions added
[pes-rpp/rpp-test-sw.git] / rpp / lib / cmdproc / src / 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 "commands/cmd_spi.h"
31 #include "rpp/rpp.h"
32 #include "cmdproc_utils.h"
33 #include "drv/drv.h"
34 #include <_isfuncdcl.h>
35
36 /**
37  *      @brief  Translate SPI response according the command and peripheral type
38  *
39  *      Command syntax: spitrNAME?(cmd, resp)
40  *
41  *      Command take response resp given by last command cmd and translate it according peripheral
42  *      type specified by NAME.
43  *
44  * @param[in]   cmd_io  Pointer to IO stack
45  * @param[in]   des             Pointer to command descriptor
46  * @param[in]   param   Parameters of command
47  * @return      0 when OK or error code
48  */
49 int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
50           char *p;
51           int opchar;
52           int i;
53           uint32_t ret;
54           uint32_t values[MAX_PARAM_VALUES_NUM];
55           uint32_t numFieldDesc;
56           uint32_t numCmdDesc;
57           uint32_t command;
58           uint32_t response;
59           spitr_reg_translate_table_t translatedData;
60
61           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
62
63           const spitr_cmd_map_t* cmdDesc = cmdDesc = get_spi_cmd_map(param[1], param[2]-param[1], &numCmdDesc);
64           if(cmdDesc == NULL) return -CMDERR_BADREG;
65
66
67           if(opchar=='?'){
68             p=param[3];
69                 si_skspace(&p);
70                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
71                 if (i < 0)
72                         return i;
73                 command = values[0];
74                 response = values[1];
75
76                 const spitr_field_desc_t* fieldDescs = get_spi_field_desc(cmdDesc, numCmdDesc, command, &numFieldDesc);
77                 if (fieldDescs == NULL)
78                         return -CMDERR_BADPAR;
79
80                 ret = spitr_fill_tr_table(fieldDescs, numFieldDesc, response, &translatedData);
81
82                 for (i = 0; i < translatedData.num_rows; i++) {
83                         rpp_sci_printf("%s: %h\r\n", translatedData.row[i].field_name, translatedData.row[i].value);
84                 }
85                 return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
86           }
87           else{
88                   return -CMDERR_BADPAR;
89           }
90 }
91
92
93 /* SPI Master testing command */
94 #define TEST_BUF 64
95 uint8_t spi_test_buf_tx[TEST_BUF];
96 uint8_t spi_test_buf_rx[TEST_BUF];
97
98 static int test_rd_arr(char **ps, uint8_t *buf, int n)
99 {
100         long val;
101         int c;
102         int i;
103
104         if (si_fndsep(ps, "({") < 0)
105                 return -CMDERR_BADSEP;
106         i = 0;
107         si_skspace(ps);
108         if ((**ps != ')') && (**ps != '}'))
109                 do {
110                         if (i >= n)
111                                 return -CMDERR_BADPAR;
112                         if (si_long(ps, &val, 16) < 0)
113                                 return -CMDERR_BADPAR;
114                         buf[i] = val;
115                         i++;
116                         if ((c = si_fndsep(ps, ",)}")) < 0)
117                                 return -CMDERR_BADSEP;
118                 } while (c == ',');
119
120         return i;
121 }
122
123 int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
124 {
125         int i;
126
127         rpp_sci_printf("SPI! %h ", addr);
128         if (status < 0)
129                 rpp_sci_printf("FAIL ");
130
131         rpp_sci_printf(" RX(");
132         for (i = 0; i < status; i++) {
133                 rpp_sci_printf("%h,", buf[i]);
134         }
135         rpp_sci_printf(")\n");
136
137         return 0;
138 }
139
140
141 /**
142  *      @brief  Send SPI command on SPI and receive response
143  *
144  *      Command syntax: spimst##:ADDR(cmd) - Sends command cmd on SPI ## with address ADDR
145  *
146  * @param[in]   cmd_io  Pointer to IO stack
147  * @param[in]   des             Pointer to command descriptor
148  * @param[in]   param   Parameters of command
149  * @return      0 when OK or error code
150  */
151 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
152 {
153         spi_drv_t *ifc;
154         int i;
155         char *p;
156         long val;
157         int ret;
158         unsigned int addr;
159         unsigned int rq_len;
160
161         if (!param[1] || !isdigit(*param[1]))
162                 return -CMDERR_BADSUF;
163
164         if (*param[2] != ':')
165                 return -CMDERR_OPCHAR;
166
167         ifc = spi_find_drv(NULL, *param[1] - '0');
168         if (ifc == NULL)
169                 return -CMDERR_NODEV;
170
171         if (!(ifc->flags & SPI_IFC_ON)) {
172                 //if (spi_tms570_init(ifc) < 0)
173                 return -CMDERR_BADCFG;
174         }
175
176         p = param[3];
177
178         si_skspace(&p);
179         if (isdigit(*p)) {
180                 if (si_long(&p, &val, 16) < 0)
181                         return -CMDERR_BADPAR;
182                 addr = val;
183         }
184         si_skspace(&p);
185         i = test_rd_arr(&p, spi_test_buf_tx, TEST_BUF);
186         if (i < 0)
187                 return i;
188         rq_len = i;
189
190         ret = spi_transfer(ifc, addr, rq_len, spi_test_buf_tx, spi_test_buf_rx);
191         spimst_print_rx(ifc, ret, addr, spi_test_buf_rx);
192         return 0;
193 }
194
195 /** Command descriptor for SPI response translation */
196 cmd_des_t const cmd_des_spi_translate={
197         0, CDESM_OPCHR|CDESM_RW,
198         "spitr*","Translate response from SPI peripheral. spitrPERNAME?(CMD, RESP)",
199         cmd_do_spi_translate, (void *)&cmd_list_spi
200 };
201
202 /** Command descriptor for SPI trasfer */
203 cmd_des_t const cmd_des_spimst = { 0, CDESM_OPCHR,
204         "spimst#", "SPI master communication request",
205         cmd_do_spimst, (void *)&cmd_list_spi
206 };
207
208 /** List of commands for SPI, defined as external */
209 cmd_des_t const *cmd_list_spi[]={
210   &cmd_des_spi_translate,
211   &cmd_des_spimst,
212   NULL
213 };
214
215