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