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