]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_tests.c
RoCoN: TUMBL firmware commutation test.
[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 #include <pxmc.h>
16
17 #include <ul_log.h>
18 #include <ul_logreg.h>
19
20 #include "appl_defs.h"
21 #include "appl_fpga.h"
22
23 int cmd_do_test_memusage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
24 {
25   void *maxaddr;
26   char str[40];
27
28   maxaddr = sbrk(0);
29
30   snprintf(str, sizeof(str), "memusage maxaddr 0x%08lx\n", (unsigned long)maxaddr);
31   cmd_io_write(cmd_io, str, strlen(str));
32
33   return 0;
34 }
35
36 int cmd_do_test_adc(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
37 {
38   printf("ADC: %ld %ld %ld %ld %ld\n", (LPC_ADC->DR[0] & 0xFFF0) >> 4,
39          (LPC_ADC->DR[1] & 0xFFF0) >> 4,
40          (LPC_ADC->DR[2] & 0xFFF0) >> 4,
41          (LPC_ADC->DR[3] & 0xFFF0) >> 4,
42          (LPC_ADC->DR[7] & 0xFFF0) >> 4);
43   return 0;
44 }
45
46 #ifdef APPL_WITH_DISTORE_EEPROM_USER
47 int cmd_do_test_distore(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
48 {
49   appl_distore_user_set_check4change();
50   return 0;
51 }
52
53 int cmd_do_test_diload(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
54 {
55   appl_distore_user_restore();
56   return 0;
57 }
58 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
59
60 int cmd_do_test_loglevel_cb(ul_log_domain_t *domain, void *context)
61 {
62   char s[30];
63   cmd_io_t *cmd_io = (cmd_io_t *)context;
64
65   s[sizeof(s) - 1] = 0;
66   snprintf(s, sizeof(s) - 1, "%s (%d)\n\r", domain->name, domain->level);
67   cmd_io_puts(cmd_io, s);
68   return 0;
69 }
70
71 int cmd_do_test_loglevel(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
72 {
73   int res = 0;
74   char *line;
75   line = param[1];
76
77   if (!line || (si_skspace(&line), !*line))
78   {
79     ul_logreg_for_each_domain(cmd_do_test_loglevel_cb, cmd_io);
80   }
81   else
82   {
83     res = ul_log_domain_arg2levels(line);
84   }
85
86   return res >= 0 ? 0 : CMDERR_BADPAR;
87 }
88
89 int cmd_do_spimst_blocking(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
90 {
91   int res;
92   int opchar;
93   char *p = param[3];
94   int spi_chan = (int)(intptr_t)des->info[0];
95   uint8_t *tx_buff = NULL;
96   uint8_t *rx_buff = NULL;
97   long addr = 0;
98   int len = 0;
99   spi_drv_t *spi_drv;
100
101   if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
102     return opchar;
103
104   if (opchar != ':')
105     return -CMDERR_OPCHAR;
106
107   if (spi_chan == -1)
108     spi_chan = *param[1] - '0';
109
110   spi_drv = spi_find_drv(NULL, spi_chan);
111
112   if (spi_drv == NULL)
113     return -CMDERR_BADSUF;
114
115   p = param[3];
116
117   si_skspace(&p);
118
119   if (isdigit((int)*p))
120   {
121     if (si_long(&p, &addr, 16) < 0)
122       return -CMDERR_BADPAR;
123   }
124
125   if (si_fndsep(&p, "({") < 0)
126     return -CMDERR_BADSEP;
127
128   if ((res = si_add_to_arr(&p, (void **)&tx_buff, &len, 16, 1, "})")) < 0)
129     return -CMDERR_BADPAR;
130
131   rx_buff = malloc(len);
132
133   res = -1;
134
135   if (rx_buff != NULL)
136     res = spi_transfer(spi_drv, addr, len, tx_buff, rx_buff);
137
138   if (res < 0)
139   {
140     printf("SPI! %02lX ERROR\n", addr);
141   }
142   else
143   {
144     int i;
145     printf("SPI! %02lX ", addr);
146     printf("TX(");
147
148     for (i = 0; i < len; i++)
149       printf("%s%02X", i ? "," : "", tx_buff[i]);
150
151     printf(") RX(");
152
153     for (i = 0; i < len; i++)
154       printf("%s%02X", i ? "," : "", rx_buff[i]);
155
156     printf(")");
157     printf("\n");
158     return 0;
159   }
160
161   if (rx_buff)
162     free(rx_buff);
163
164   if (tx_buff)
165     free(tx_buff);
166
167   return 0;
168 }
169
170 #ifdef SDRAM_BASE
171 int sdram_access_test(void)
172 {
173   unsigned int *ptr;
174   unsigned int pattern;
175   size_t ramsz = SDRAM_SIZE;
176   size_t cnt;
177   lt_mstime_t tic;
178   size_t blksz, i;
179
180   lt_mstime_update();
181   tic = actual_msec;
182
183   pattern = 0x12abcdef;
184
185   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
186   {
187     *(ptr++) = pattern;
188     pattern = pattern + 0x87654321;
189   }
190
191   lt_mstime_update();
192   printf("SDRAM write %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
193
194   lt_mstime_update();
195   tic = actual_msec;
196
197   pattern = 0x12abcdef;
198
199   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
200   {
201     if (*ptr != pattern)
202     {
203       printf("SDRAM error modify at %p (%08x)\n", ptr, *ptr ^ pattern);
204       return -1;
205     }
206
207     *(ptr++) = ~pattern;
208     pattern = pattern + 0x87654321;
209   }
210
211   lt_mstime_update();
212   printf("SDRAM modify %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
213
214   lt_mstime_update();
215   tic = actual_msec;
216
217   pattern = 0x12abcdef;
218
219   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
220   {
221     if (*(ptr++) != ~pattern)
222     {
223       printf("SDRAM error read at %p (%08x)\n", ptr, *ptr ^ pattern);
224       return -1;
225     }
226
227     pattern = pattern + 0x87654321;
228   }
229
230   lt_mstime_update();
231   printf("SDRAM read %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
232
233   lt_mstime_update();
234   tic = actual_msec;
235
236   pattern = 0;
237
238   for (cnt = ramsz / sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;)
239   {
240     pattern += *(ptr++);
241   }
242
243   lt_mstime_update();
244   printf("SDRAM sum %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), pattern);
245
246   for (blksz = 1; blksz < 256 ; blksz *= 2)
247   {
248     lt_mstime_update();
249     tic = actual_msec;
250
251     pattern = 0;
252
253     for (cnt = ramsz / sizeof(*ptr); cnt; cnt -= blksz)
254     {
255       ptr = (typeof(ptr))SDRAM_BASE;
256
257       //ptr = (typeof(ptr))cmd_do_test_memusage;
258       //ptr = (typeof(ptr))&ptr;
259       for (i = blksz; i--;)
260         pattern += *(ptr++);
261     }
262
263     lt_mstime_update();
264     printf("SDRAM sum %d blksz %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), (int)blksz, pattern);
265   }
266
267   return 0;
268 }
269
270 int cmd_do_testsdram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
271 {
272   sdram_access_test();
273   return 0;
274 }
275 #endif /*SDRAM_BASE*/
276
277 int cmd_do_testlxpwrrx(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
278 {
279   pxmc_rocon_rx_data_hist_buff = NULL;
280  #ifndef PXMC_ROCON_TIMED_BY_RX_DONE
281   pxmc_rocon_rx_done_isr_setup(pxmc_rocon_rx_done_isr);
282  #endif /*PXMC_ROCON_TIMED_BY_RX_DONE*/
283   pxmc_rocon_rx_data_hist_buff_end = (void *)(FPGA_CONFIGURATION_FILE_ADDRESS +
284                                          0x80000);
285   pxmc_rocon_rx_data_hist_buff = (void *)FPGA_CONFIGURATION_FILE_ADDRESS;
286   return 0;
287 }
288
289 int cmd_do_testlxpwrstat(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
290 {
291   printf("lxpwrrx period %ld latency %ld max %ld\n",
292          (long)pxmc_rocon_rx_cycle_time, (long)pxmc_rocon_rx_irq_latency,
293          (long)pxmc_rocon_rx_irq_latency_max);
294   return 0;
295 }
296
297 #include <math.h>
298
299 int cmd_do_testfncapprox(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
300 {
301   char *ps = param[1];
302   unsigned long fnc;
303   unsigned long val;
304   unsigned long res;
305   long diff;
306   long diff_max = 0;
307   unsigned int  xb;
308   uint32_t x;
309   uint32_t xl;
310   double   xf;
311   int64_t  yl;
312   long count = 1;
313   long step = 1 << (30-7-18+1  +4);
314
315   si_skspace(&ps);
316   if (si_ulong(&ps, &fnc, 0) < 0)
317       return -CMDERR_BADPAR;
318
319   si_skspace(&ps);
320   if (!strcmp(ps, "all")) {
321     val = 0;
322     count = 0x80000000UL / step;
323     if (fnc == 1) {
324       val = 1;
325     }
326   } else {
327     if (si_ulong(&ps, &val, 0) < 0)
328         return -CMDERR_BADPAR;
329   }
330
331   for (; count--;  val += step) {
332
333     if (fnc == 1) {
334       x = val;
335       xb = __builtin_clz(x);
336       xl = x << xb;
337     } else {
338       xl = val;
339     }
340
341     fpga_fncapprox_base[fnc] = xl;
342
343     /* dummy read to provide time to function aproximator to proceed computation */
344     res = fpga_fncapprox_base[fnc];
345     res = fpga_fncapprox_base[fnc];
346
347     switch (fnc) {
348       case 0:
349         yl = xl;
350       case 1:
351         yl = (1LL << 62) / xl;
352         break;
353       case 2:
354         xf = (double)xl * M_PI / 2.0 / (1UL << 30);
355         yl = round(sin(xf) * (1UL << 16));
356         break;
357       case 3:
358         xf = (double)xl * M_PI / 2.0 / (1UL << 30);
359         yl = round(cos(xf) * (1UL << 16));
360         break;
361       default:
362       yl = 0;
363     }
364
365     diff = yl - res;
366
367     if ((diff > 0) && (diff > diff_max))
368       diff_max = diff;
369     else if ((diff < 0) && (-diff > diff_max))
370       diff_max = -diff;
371
372   }
373
374   val -= step;
375
376   printf("fnc=%ld val=0x%08lx res=0x%08lx ref=0x%08lx diff=%ld max %ld\n",
377          fnc, val, res, (unsigned long)yl, diff, diff_max);
378
379   return 0;
380 }
381
382 int cmd_do_testtumblefw(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
383 {
384   char *ps = param[1];
385   long pwm_d;
386   long pwm_q;
387   uint32_t ptofs;
388   uint32_t irc;
389   uint32_t ptirc;
390   uint32_t ptreci;
391   uint64_t ull;
392   pxmc_state_t *mcs = pxmc_main_list.pxml_arr[0];
393
394   fpga_tumbl_dmem[0] = 0;
395
396   si_skspace(&ps);
397   if (si_long(&ps, &pwm_d, 0) < 0)
398       return -CMDERR_BADPAR;
399
400   si_skspace(&ps);
401   if (si_ulong(&ps, &pwm_q, 0) < 0)
402         return -CMDERR_BADPAR;
403
404   irc = fpga_irc[0]->count;
405   ptofs = (int16_t)(mcs->pxms_ptofs - irc) + irc;
406
407   ptirc = mcs->pxms_ptirc;
408   ull = (1ULL << 32) * mcs->pxms_ptper;
409   ptreci = (ull + ptirc / 2) / ptirc;
410
411   fpga_tumbl_dmem[0] = 0;
412   fpga_tumbl_dmem[1] = pwm_d;
413   fpga_tumbl_dmem[2] = pwm_q;
414
415   fpga_tumbl_dmem[6] = ptirc;
416   fpga_tumbl_dmem[7] = ptreci;
417   fpga_tumbl_dmem[8] = ptofs;
418
419   fpga_tumbl_dmem[0] = 1;
420
421   printf("spd %ld\n",mcs->pxms_as);
422
423   return 0;
424 }
425
426
427 cmd_des_t const cmd_des_test_memusage = {0, 0,
428                 "memusage", "report memory usage", cmd_do_test_memusage,
429 {
430   0,
431   0
432 }
433                                         };
434
435 cmd_des_t const cmd_des_test_adc = {0, 0,
436                                     "testadc", "adc test", cmd_do_test_adc,
437 {
438   0,
439   0
440 }
441                                    };
442
443 #ifdef APPL_WITH_DISTORE_EEPROM_USER
444 cmd_des_t const cmd_des_test_distore = {0, 0,
445                                         "testdistore", "test DINFO store", cmd_do_test_distore,
446 {
447   0,
448   0
449 }
450                                        };
451
452 cmd_des_t const cmd_des_test_diload = {0, 0,
453                                        "testdiload", "test DINFO load", cmd_do_test_diload,
454 {
455   0,
456   0
457 }
458                                       };
459 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
460
461 cmd_des_t const cmd_des_test_loglevel = {0, 0,
462                 "loglevel", "select logging level",
463                 cmd_do_test_loglevel, {}
464                                         };
465
466 cmd_des_t const cmd_des_spimst = {0, CDESM_OPCHR | CDESM_WR,
467                                   "SPIMST", "SPI master communication request",
468                                   cmd_do_spimst_blocking, {(void *)0}
469                                  };
470
471 cmd_des_t const cmd_des_spimstx = {0, CDESM_OPCHR | CDESM_WR,
472                                    "SPIMST#", "SPI# master communication request",
473                                    cmd_do_spimst_blocking, {(void *) - 1}
474                                   };
475
476 #ifdef SDRAM_BASE
477 cmd_des_t const cmd_des_testsdram = {0, 0,
478                                      "testsdram", "test SDRAM",
479                                      cmd_do_testsdram, {(void *)0}
480                                     };
481 #endif /*SDRAM_BASE*/
482
483
484 cmd_des_t const cmd_des_testlxpwrrx = {0, 0,
485                                      "testlxpwrrx", "capture data stream from lxpwr",
486                                      cmd_do_testlxpwrrx, {(void *)0}
487                                     };
488
489 cmd_des_t const cmd_des_testlxpwrstat = {0, 0,
490                                      "testlxpwrstat", "lxpwr interrupt statistic",
491                                      cmd_do_testlxpwrstat, {(void *)0}
492                                     };
493
494 cmd_des_t const cmd_des_testfncapprox = {0, 0,
495                                      "testfncapprox", "test of function approximator",
496                                      cmd_do_testfncapprox, {(void *)0}
497                                     };
498
499 cmd_des_t const cmd_des_testtumblefw = {0, 0,
500                                      "testtumblefw", "test Tumble coprocesor firmware",
501                                      cmd_do_testtumblefw, {(void *)0}
502                                     };
503
504 cmd_des_t const *const cmd_appl_tests[] =
505 {
506   &cmd_des_test_memusage,
507   &cmd_des_test_adc,
508 #ifdef APPL_WITH_DISTORE_EEPROM_USER
509   &cmd_des_test_distore,
510   &cmd_des_test_diload,
511 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
512   &cmd_des_test_loglevel,
513   &cmd_des_spimst,
514   &cmd_des_spimstx,
515 #ifdef SDRAM_BASE
516   &cmd_des_testsdram,
517 #endif /*SDRAM_BASE*/
518   &cmd_des_testlxpwrrx,
519   &cmd_des_testlxpwrstat,
520   &cmd_des_testfncapprox,
521   &cmd_des_testtumblefw,
522   NULL
523 };