]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_tests.c
387649e30df8c9ea0fa6e51e5e4346ed4573daf7
[fpga/lx-cpu1/lx-rocon.git] / sw / app / rocon / appl_tests.c
1 #include <system_def.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stddef.h>
7 #include <malloc.h>
8 #include <utils.h>
9 #include <cmd_proc.h>
10 #include <hal_gpio.h>
11 #include <hal_machperiph.h>
12 #include <LPC17xx.h>
13 #include <lpcTIM.h>
14 #include <spi_drv.h>
15
16 #include <ul_log.h>
17 #include <ul_logreg.h>
18
19 #include "appl_defs.h"
20 #include "appl_fpga.h"
21
22 int cmd_do_test_memusage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
23 {
24   void *maxaddr;
25   char str[40];
26
27   maxaddr = sbrk(0);
28
29   snprintf(str, sizeof(str), "memusage maxaddr 0x%08lx\n", (unsigned long)maxaddr);
30   cmd_io_write(cmd_io, str, strlen(str));
31
32   return 0;
33 }
34
35 int cmd_do_test_adc(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
36 {
37   printf("ADC: %ld %ld %ld %ld %ld\n", (LPC_ADC->DR[0] & 0xFFF0) >> 4,
38          (LPC_ADC->DR[1] & 0xFFF0) >> 4,
39          (LPC_ADC->DR[2] & 0xFFF0) >> 4,
40          (LPC_ADC->DR[3] & 0xFFF0) >> 4,
41          (LPC_ADC->DR[7] & 0xFFF0) >> 4);
42   return 0;
43 }
44
45 #ifdef APPL_WITH_DISTORE_EEPROM_USER
46 int cmd_do_test_distore(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
47 {
48   appl_distore_user_set_check4change();
49   return 0;
50 }
51
52 int cmd_do_test_diload(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
53 {
54   appl_distore_user_restore();
55   return 0;
56 }
57 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
58
59 int cmd_do_test_loglevel_cb(ul_log_domain_t *domain, void *context)
60 {
61   char s[30];
62   cmd_io_t *cmd_io = (cmd_io_t *)context;
63
64   s[sizeof(s) - 1] = 0;
65   snprintf(s, sizeof(s) - 1, "%s (%d)\n\r", domain->name, domain->level);
66   cmd_io_puts(cmd_io, s);
67   return 0;
68 }
69
70 int cmd_do_test_loglevel(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
71 {
72   int res = 0;
73   char *line;
74   line = param[1];
75
76   if (!line || (si_skspace(&line), !*line))
77   {
78     ul_logreg_for_each_domain(cmd_do_test_loglevel_cb, cmd_io);
79   }
80   else
81   {
82     res = ul_log_domain_arg2levels(line);
83   }
84
85   return res >= 0 ? 0 : CMDERR_BADPAR;
86 }
87
88 int cmd_do_spimst_blocking(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
89 {
90   int res;
91   int opchar;
92   char *p = param[3];
93   int spi_chan = (int)(intptr_t)des->info[0];
94   uint8_t *tx_buff = NULL;
95   uint8_t *rx_buff = NULL;
96   long addr = 0;
97   int len = 0;
98   spi_drv_t *spi_drv;
99
100   if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
101     return opchar;
102
103   if (opchar != ':')
104     return -CMDERR_OPCHAR;
105
106   if (spi_chan == -1)
107     spi_chan = *param[1] - '0';
108
109   spi_drv = spi_find_drv(NULL, spi_chan);
110
111   if (spi_drv == NULL)
112     return -CMDERR_BADSUF;
113
114   p = param[3];
115
116   si_skspace(&p);
117
118   if (isdigit((int)*p))
119   {
120     if (si_long(&p, &addr, 16) < 0)
121       return -CMDERR_BADPAR;
122   }
123
124   if (si_fndsep(&p, "({") < 0)
125     return -CMDERR_BADSEP;
126
127   if ((res = si_add_to_arr(&p, (void **)&tx_buff, &len, 16, 1, "})")) < 0)
128     return -CMDERR_BADPAR;
129
130   rx_buff = malloc(len);
131
132   res = -1;
133
134   if (rx_buff != NULL)
135     res = spi_transfer(spi_drv, addr, len, tx_buff, rx_buff);
136
137   if (res < 0)
138   {
139     printf("SPI! %02lX ERROR\n", addr);
140   }
141   else
142   {
143     int i;
144     printf("SPI! %02lX ", addr);
145     printf("TX(");
146
147     for (i = 0; i < len; i++)
148       printf("%s%02X", i ? "," : "", tx_buff[i]);
149
150     printf(") RX(");
151
152     for (i = 0; i < len; i++)
153       printf("%s%02X", i ? "," : "", rx_buff[i]);
154
155     printf(")");
156     printf("\n");
157     return 0;
158   }
159
160   if (rx_buff)
161     free(rx_buff);
162
163   if (tx_buff)
164     free(tx_buff);
165
166   return 0;
167 }
168
169 #ifdef SDRAM_BASE
170 int sdram_access_test(void)
171 {
172   unsigned int *ptr;
173   unsigned int pattern;
174   size_t ramsz = SDRAM_SIZE;
175   size_t cnt;
176   lt_mstime_t tic;
177   size_t blksz, i;
178
179   lt_mstime_update();
180   tic = actual_msec;
181
182   pattern = 0x12abcdef;
183
184   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
185   {
186     *(ptr++) = pattern;
187     pattern = pattern + 0x87654321;
188   }
189
190   lt_mstime_update();
191   printf("SDRAM write %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
192
193   lt_mstime_update();
194   tic = actual_msec;
195
196   pattern = 0x12abcdef;
197
198   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
199   {
200     if (*ptr != pattern)
201     {
202       printf("SDRAM error modify at %p (%08x)\n", ptr, *ptr ^ pattern);
203       return -1;
204     }
205
206     *(ptr++) = ~pattern;
207     pattern = pattern + 0x87654321;
208   }
209
210   lt_mstime_update();
211   printf("SDRAM modify %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
212
213   lt_mstime_update();
214   tic = actual_msec;
215
216   pattern = 0x12abcdef;
217
218   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
219   {
220     if (*(ptr++) != ~pattern)
221     {
222       printf("SDRAM error read at %p (%08x)\n", ptr, *ptr ^ pattern);
223       return -1;
224     }
225
226     pattern = pattern + 0x87654321;
227   }
228
229   lt_mstime_update();
230   printf("SDRAM read %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
231
232   lt_mstime_update();
233   tic = actual_msec;
234
235   pattern = 0;
236
237   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
238   {
239     pattern += *(ptr++);
240   }
241
242   lt_mstime_update();
243   printf("SDRAM sum %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), pattern);
244
245   for (blksz = 1; blksz < 256 ; blksz *= 2)
246   {
247     lt_mstime_update();
248     tic = actual_msec;
249
250     pattern = 0;
251
252     for (cnt = ramsz / sizeof(*ptr); cnt; cnt -= blksz)
253     {
254       ptr = (typeof(ptr))SDRAM_BASE;
255
256       //ptr = (typeof(ptr))cmd_do_test_memusage;
257       //ptr = (typeof(ptr))&ptr;
258       for (i = blksz; i--;)
259         pattern += *(ptr++);
260     }
261
262     lt_mstime_update();
263     printf("SDRAM sum %d blksz %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), (int)blksz, pattern);
264   }
265
266   return 0;
267 }
268
269 int cmd_do_testsdram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
270 {
271   sdram_access_test();
272   return 0;
273 }
274 #endif /*SDRAM_BASE*/
275
276 int cmd_do_testlxpwrrx(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
277 {
278   pxmc_rocon_rx_data_hist_buff = NULL;
279  #ifndef PXMC_ROCON_TIMED_BY_RX_DONE
280   pxmc_rocon_rx_done_isr_setup(pxmc_rocon_rx_done_isr);
281  #endif /*PXMC_ROCON_TIMED_BY_RX_DONE*/
282   pxmc_rocon_rx_data_hist_buff_end = (void *)(FPGA_CONFIGURATION_FILE_ADDRESS +
283                                          0x80000);
284   pxmc_rocon_rx_data_hist_buff = (void *)FPGA_CONFIGURATION_FILE_ADDRESS;
285   return 0;
286 }
287
288 int cmd_do_testlxpwrstat(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
289 {
290   printf("lxpwrrx period %ld latency %ld max %ld\n",
291          (long)pxmc_rocon_rx_cycle_time, (long)pxmc_rocon_rx_irq_latency,
292          (long)pxmc_rocon_rx_irq_latency_max);
293   return 0;
294 }
295
296 cmd_des_t const cmd_des_test_memusage = {0, 0,
297                 "memusage", "report memory usage", cmd_do_test_memusage,
298 {
299   0,
300   0
301 }
302                                         };
303
304 cmd_des_t const cmd_des_test_adc = {0, 0,
305                                     "testadc", "adc test", cmd_do_test_adc,
306 {
307   0,
308   0
309 }
310                                    };
311
312 #ifdef APPL_WITH_DISTORE_EEPROM_USER
313 cmd_des_t const cmd_des_test_distore = {0, 0,
314                                         "testdistore", "test DINFO store", cmd_do_test_distore,
315 {
316   0,
317   0
318 }
319                                        };
320
321 cmd_des_t const cmd_des_test_diload = {0, 0,
322                                        "testdiload", "test DINFO load", cmd_do_test_diload,
323 {
324   0,
325   0
326 }
327                                       };
328 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
329
330 cmd_des_t const cmd_des_test_loglevel = {0, 0,
331                 "loglevel", "select logging level",
332                 cmd_do_test_loglevel, {}
333                                         };
334
335 cmd_des_t const cmd_des_spimst = {0, CDESM_OPCHR | CDESM_WR,
336                                   "SPIMST", "SPI master communication request",
337                                   cmd_do_spimst_blocking, {(void *)0}
338                                  };
339
340 cmd_des_t const cmd_des_spimstx = {0, CDESM_OPCHR | CDESM_WR,
341                                    "SPIMST#", "SPI# master communication request",
342                                    cmd_do_spimst_blocking, {(void *) - 1}
343                                   };
344
345 #ifdef SDRAM_BASE
346 cmd_des_t const cmd_des_testsdram = {0, 0,
347                                      "testsdram", "test SDRAM",
348                                      cmd_do_testsdram, {(void *)0}
349                                     };
350 #endif /*SDRAM_BASE*/
351
352
353 cmd_des_t const cmd_des_testlxpwrrx = {0, 0,
354                                      "testlxpwrrx", "capture data stream from lxpwr",
355                                      cmd_do_testlxpwrrx, {(void *)0}
356                                     };
357
358 cmd_des_t const cmd_des_testlxpwrstat = {0, 0,
359                                      "testlxpwrstat", "lxpwr interrupt statistic",
360                                      cmd_do_testlxpwrstat, {(void *)0}
361                                     };
362
363 cmd_des_t const *const cmd_appl_tests[] =
364 {
365   &cmd_des_test_memusage,
366   &cmd_des_test_adc,
367 #ifdef APPL_WITH_DISTORE_EEPROM_USER
368   &cmd_des_test_distore,
369   &cmd_des_test_diload,
370 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
371   &cmd_des_test_loglevel,
372   &cmd_des_spimst,
373   &cmd_des_spimstx,
374 #ifdef SDRAM_BASE
375   &cmd_des_testsdram,
376 #endif /*SDRAM_BASE*/
377   &cmd_des_testlxpwrrx,
378   &cmd_des_testlxpwrstat,
379   NULL
380 };