]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_tests.c
RoCoN: test command to report measured LXPWR cycle period.
[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 #define LXPWR_RX_TIM  LPC_TIM2
277 #define LXPWR_RX_IRQn TIMER2_IRQn
278
279 volatile void *lxpwr_rx_data_hist_buff;
280 volatile void *lxpwr_rx_data_hist_buff_end;
281
282 uint32_t lxpwr_rx_last_irq;
283 uint32_t lxpwr_rx_cycle_time;
284 uint32_t lxpwr_rx_irq_latency;
285 uint32_t lxpwr_rx_irq_latency_max;
286
287 IRQ_HANDLER_FNC(lxpwr_rx_done_isr)
288 {
289   uint32_t ir;
290
291   ir = LXPWR_RX_TIM->IR & LPC_TIM_IR_ALL_m;
292   LXPWR_RX_TIM->IR = ir;
293   if (ir & LPC_TIM_IR_CR1INT_m) {
294     uint32_t cr0, cr1;
295     cr0 = LXPWR_RX_TIM->CR0;
296     cr1 = LXPWR_RX_TIM->CR1;
297
298     lxpwr_rx_cycle_time = cr1 - lxpwr_rx_last_irq;
299     lxpwr_rx_last_irq = cr1;
300
301     hal_gpio_set_value(T2MAT0_PIN, 1);
302     hal_gpio_set_value(T2MAT1_PIN, 0);
303     hal_gpio_set_value(T2MAT0_PIN, 0);
304
305     if (lxpwr_rx_data_hist_buff >= lxpwr_rx_data_hist_buff_end)
306       lxpwr_rx_data_hist_buff = NULL;
307
308     if (lxpwr_rx_data_hist_buff != NULL) {
309       int i;
310       volatile uint32_t *pwm_reg = fpga_lx_master_transmitter_base + 8;
311       volatile uint32_t *rec_reg = fpga_lx_master_receiver_base + 8;
312       uint16_t *pbuf = (uint16_t *)lxpwr_rx_data_hist_buff;
313       for (i = 0; i < 8; i++) {
314         *(pbuf++) = *(rec_reg++);
315       }
316       for (i = 0; i < 8; i++) {
317         *(pbuf++) = *(pwm_reg++);
318       }
319       lxpwr_rx_data_hist_buff = pbuf;
320     }
321
322     lxpwr_rx_irq_latency = LXPWR_RX_TIM->TC - cr1;
323     if (lxpwr_rx_irq_latency > lxpwr_rx_irq_latency_max)
324       lxpwr_rx_irq_latency_max = lxpwr_rx_irq_latency;
325   }
326
327   return IRQ_HANDLED;
328 }
329
330 int lxpwr_rx_done_isr_init(void)
331 {
332
333   disable_irq(LXPWR_RX_IRQn);
334
335   hal_pin_conf_set(T2MAT0_PIN, PORT_CONF_GPIO_OUT_LO);
336   hal_pin_conf_set(T2MAT1_PIN, PORT_CONF_GPIO_OUT_LO);
337   hal_pin_conf(T2CAP0_PIN);
338   hal_pin_conf(T2CAP1_PIN);
339
340   hal_gpio_direction_output(T2MAT0_PIN, 1);
341   hal_gpio_direction_output(T2MAT1_PIN, 0);
342   hal_gpio_set_value(T2MAT0_PIN, 0);
343
344   /* Enable CLKOUT pin function, source CCLK, divide by 1 */
345   LPC_SC->CLKOUTCFG = 0x0100;
346
347   request_irq(LXPWR_RX_IRQn, lxpwr_rx_done_isr, 0, NULL,NULL);
348
349   LXPWR_RX_TIM->TCR = 0;
350   LXPWR_RX_TIM->CTCR = 0;
351   LXPWR_RX_TIM->PR = 0; /* Divide by 1 */
352
353   LXPWR_RX_TIM->CCR = LPC_TIM_CCR_CAP0RE_m | LPC_TIM_CCR_CAP1FE_m |
354                    LPC_TIM_CCR_CAP1I_m;
355
356   LXPWR_RX_TIM->EMR = __val2mfld(LPC_TIM_EMR_EMC0_m, LPC_TIM_EMR_NOP) |
357                    __val2mfld(LPC_TIM_EMR_EMC1_m, LPC_TIM_EMR_NOP);
358
359   LXPWR_RX_TIM->MCR = 0;                        /* No IRQ on MRx */
360   LXPWR_RX_TIM->TCR = LPC_TIM_TCR_CEN_m;        /* Enable timer counting */
361   enable_irq(LXPWR_RX_IRQn);            /* Enable interrupt */
362
363   return 0;
364
365 }
366
367 int cmd_do_testlxpwrrx(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
368 {
369   lxpwr_rx_data_hist_buff = NULL;
370   lxpwr_rx_done_isr_init();
371   lxpwr_rx_data_hist_buff_end = (void *)(FPGA_CONFIGURATION_FILE_ADDRESS +
372                                          0x80000);
373   lxpwr_rx_data_hist_buff = (void *)FPGA_CONFIGURATION_FILE_ADDRESS;
374   return 0;
375 }
376
377 int cmd_do_testlxpwrstat(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
378 {
379   printf("lxpwrrx period %ld latency %ld max %ld\n",
380          (long)lxpwr_rx_cycle_time, (long)lxpwr_rx_irq_latency,
381          (long)lxpwr_rx_irq_latency_max);
382   return 0;
383 }
384
385 cmd_des_t const cmd_des_test_memusage = {0, 0,
386                 "memusage", "report memory usage", cmd_do_test_memusage,
387 {
388   0,
389   0
390 }
391                                         };
392
393 cmd_des_t const cmd_des_test_adc = {0, 0,
394                                     "testadc", "adc test", cmd_do_test_adc,
395 {
396   0,
397   0
398 }
399                                    };
400
401 #ifdef APPL_WITH_DISTORE_EEPROM_USER
402 cmd_des_t const cmd_des_test_distore = {0, 0,
403                                         "testdistore", "test DINFO store", cmd_do_test_distore,
404 {
405   0,
406   0
407 }
408                                        };
409
410 cmd_des_t const cmd_des_test_diload = {0, 0,
411                                        "testdiload", "test DINFO load", cmd_do_test_diload,
412 {
413   0,
414   0
415 }
416                                       };
417 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
418
419 cmd_des_t const cmd_des_test_loglevel = {0, 0,
420                 "loglevel", "select logging level",
421                 cmd_do_test_loglevel, {}
422                                         };
423
424 cmd_des_t const cmd_des_spimst = {0, CDESM_OPCHR | CDESM_WR,
425                                   "SPIMST", "SPI master communication request",
426                                   cmd_do_spimst_blocking, {(void *)0}
427                                  };
428
429 cmd_des_t const cmd_des_spimstx = {0, CDESM_OPCHR | CDESM_WR,
430                                    "SPIMST#", "SPI# master communication request",
431                                    cmd_do_spimst_blocking, {(void *) - 1}
432                                   };
433
434 #ifdef SDRAM_BASE
435 cmd_des_t const cmd_des_testsdram = {0, 0,
436                                      "testsdram", "test SDRAM",
437                                      cmd_do_testsdram, {(void *)0}
438                                     };
439 #endif /*SDRAM_BASE*/
440
441
442 cmd_des_t const cmd_des_testlxpwrrx = {0, 0,
443                                      "testlxpwrrx", "capture data stream from lxpwr",
444                                      cmd_do_testlxpwrrx, {(void *)0}
445                                     };
446
447 cmd_des_t const cmd_des_testlxpwrstat = {0, 0,
448                                      "testlxpwrstat", "lxpwr interrupt statistic",
449                                      cmd_do_testlxpwrstat, {(void *)0}
450                                     };
451
452 cmd_des_t const *const cmd_appl_tests[] =
453 {
454   &cmd_des_test_memusage,
455   &cmd_des_test_adc,
456 #ifdef APPL_WITH_DISTORE_EEPROM_USER
457   &cmd_des_test_distore,
458   &cmd_des_test_diload,
459 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
460   &cmd_des_test_loglevel,
461   &cmd_des_spimst,
462   &cmd_des_spimstx,
463 #ifdef SDRAM_BASE
464   &cmd_des_testsdram,
465 #endif /*SDRAM_BASE*/
466   &cmd_des_testlxpwrrx,
467   &cmd_des_testlxpwrstat,
468   NULL
469 };