]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-dad.git/blob - sw/app/lx_dad/appl_tests.c
a6ccd3a4ea3ceaea9bda8d269079dba5f120bd9c
[fpga/lx-cpu1/lx-dad.git] / sw / app / lx_dad / 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 <utils.h>
8 #include <cmd_proc.h>
9 #include <hal_gpio.h>
10 #include <hal_mpu.h>
11 #include <hal_machperiph.h>
12 #include <LPC17xx.h>
13 #include <spi_drv.h>
14 #include <lt_timer.h>
15
16 #include <ul_log.h>
17 #include <ul_logreg.h>
18
19 #include "appl_defs.h"
20
21 int cmd_do_test_memusage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
22 {
23   void *maxaddr;
24   char str[40];
25
26   maxaddr = sbrk(0);
27
28   snprintf(str,sizeof(str),"memusage maxaddr 0x%08lx\n",(unsigned long)maxaddr);
29   cmd_io_write(cmd_io,str,strlen(str));
30
31   return 0;
32 }
33
34 int cmd_do_test_adc(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
35 {
36   printf("ADC: %ld %ld %ld %ld %ld\n",(LPC_ADC->DR[0] & 0xFFF0)>>4,
37                                       (LPC_ADC->DR[1] & 0xFFF0)>>4,
38                                       (LPC_ADC->DR[2] & 0xFFF0)>>4,
39                                       (LPC_ADC->DR[3] & 0xFFF0)>>4,
40                                       (LPC_ADC->DR[7] & 0xFFF0)>>4);
41   return 0;
42 }
43
44 #ifdef APPL_WITH_DISTORE_EEPROM_USER
45 int cmd_do_test_distore(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
46 {
47   appl_distore_user_set_check4change();
48   return 0;
49 }
50
51 int cmd_do_test_diload(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
52 {
53   appl_distore_user_restore();
54   return 0;
55 }
56 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
57
58 int cmd_do_test_loglevel_cb(ul_log_domain_t *domain, void *context)
59 {
60   char s[30];
61   cmd_io_t *cmd_io = (cmd_io_t *)context;
62
63   s[sizeof(s)-1]=0;
64   snprintf(s,sizeof(s)-1,"%s (%d)\n\r",domain->name, domain->level);
65   cmd_io_puts(cmd_io, s);
66   return 0;
67 }
68
69 int cmd_do_test_loglevel(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
70 {
71   int res=0;
72   char *line;
73   line = param[1];
74
75   if(!line||(si_skspace(&line),!*line)) {
76     ul_logreg_for_each_domain(cmd_do_test_loglevel_cb, cmd_io);
77   } else {
78     res=ul_log_domain_arg2levels(line);
79   }
80
81   return res>=0?0:CMDERR_BADPAR;
82 }
83
84 int cmd_do_spimst_blocking(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
85 {
86   int res;
87   int opchar;
88   char *p=param[3];
89   int spi_chan = (int)(intptr_t)des->info[0];
90   uint8_t *tx_buff = NULL;
91   uint8_t *rx_buff = NULL;
92   long addr = 0;
93   int len = 0;
94   spi_drv_t *spi_drv;
95
96   if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
97   if(opchar!=':')
98     return -CMDERR_OPCHAR;
99
100   if(spi_chan==-1)
101     spi_chan=*param[1]-'0';
102
103   spi_drv = spi_find_drv(NULL, spi_chan);
104   if(spi_drv==NULL)
105     return -CMDERR_BADSUF;
106
107   p=param[3];
108
109   si_skspace(&p);
110   if(isdigit((int)*p)){
111     if(si_long(&p,&addr,16)<0) return -CMDERR_BADPAR;
112   }
113   if(si_fndsep(&p,"({")<0) return -CMDERR_BADSEP;
114
115   if((res=si_add_to_arr(&p, (void**)&tx_buff, &len, 16, 1, "})"))<0)
116     return -CMDERR_BADPAR;
117
118   rx_buff=malloc(len);
119
120   res=-1;
121   if(rx_buff!=NULL)
122     res = spi_transfer_with_mode(spi_drv, addr, len, tx_buff, rx_buff, SPI_MODE_3);
123
124   if(res < 0) {
125     printf("SPI! %02lX ERROR\n",addr);
126   } else {
127     int i;
128     printf("SPI! %02lX ",addr);
129     printf("TX(");
130     for(i=0;i<len;i++) printf("%s%02X",i?",":"",tx_buff[i]);
131     printf(") RX(");
132     for(i=0;i<len;i++) printf("%s%02X",i?",":"",rx_buff[i]);
133     printf(")");
134     printf("\n");
135     return 0;
136   }
137
138   if(rx_buff)
139     free(rx_buff);
140   if(tx_buff)
141     free(tx_buff);
142
143   return 0;
144 }
145
146 int sdram_access_test(void)
147 {
148   unsigned int *ptr;
149   unsigned int pattern;
150   size_t ramsz = SDRAM_SIZE;
151   size_t cnt;
152   lt_mstime_t tic;
153   size_t blksz, i;
154
155   lt_mstime_update();
156   tic = actual_msec;
157
158   pattern = 0x12abcdef;
159   for (cnt = ramsz/sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;) {
160     *(ptr++) = pattern;
161     pattern = pattern + 0x87654321;
162   }
163
164   lt_mstime_update();
165   printf("SDRAM write %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
166
167   lt_mstime_update();
168   tic = actual_msec;
169
170   pattern = 0x12abcdef;
171   for (cnt = ramsz/sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;) {
172     if(*ptr != pattern) {
173       printf("SDRAM error modify at %p (%08x)\n", ptr, *ptr ^ pattern);
174       return -1;
175     }
176     *(ptr++) = ~pattern;
177     pattern = pattern + 0x87654321;
178   }
179
180   lt_mstime_update();
181   printf("SDRAM modify %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
182
183   lt_mstime_update();
184   tic = actual_msec;
185
186   pattern = 0x12abcdef;
187   for (cnt = ramsz/sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;) {
188     if(*(ptr++) != ~pattern) {
189       printf("SDRAM error read at %p (%08x)\n", ptr, *ptr ^ pattern);
190       return -1;
191     }
192     pattern = pattern + 0x87654321;
193   }
194
195   lt_mstime_update();
196   printf("SDRAM read %d ms\n", (int)(lt_msdiff_t)(actual_msec - tic));
197
198   lt_mstime_update();
199   tic = actual_msec;
200
201   pattern = 0;
202   for (cnt = ramsz/sizeof(*ptr), ptr = (typeof(ptr))SDRAM_BASE; cnt--;) {
203     pattern += *(ptr++);
204   }
205
206   lt_mstime_update();
207   printf("SDRAM sum %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), pattern);
208
209   for (blksz=1; blksz < 256 ; blksz *= 2) {
210     lt_mstime_update();
211     tic = actual_msec;
212
213     pattern = 0;
214     for (cnt = ramsz/sizeof(*ptr); cnt; cnt -= blksz) {
215       ptr = (typeof(ptr))SDRAM_BASE;
216       //ptr = (typeof(ptr))cmd_do_test_memusage;
217       //ptr = (typeof(ptr))&ptr;
218       for (i = blksz; i--; )
219         pattern += *(ptr++);
220     }
221     lt_mstime_update();
222     printf("SDRAM sum %d blksz %d ms res 0x%08x\n", (int)(lt_msdiff_t)(actual_msec - tic), (int)blksz, pattern);
223   }
224
225   return 0;
226 }
227
228 int cmd_do_testsdram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
229 {
230   sdram_access_test();
231   return 0;
232 }
233
234 int cmd_do_testmpu(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
235 {
236   int i;
237   int reg_cnt = hal_mpu_region_count();
238   uint32_t ra, rsz;
239
240   for (i = 0; i < reg_cnt; i++) {
241     MPU->RNR = i;
242     ra = MPU->RBAR & MPU_RBAR_ADDR_Msk;
243     rsz = (1 << (__mfld2val(MPU_RASR_SIZE_Msk, MPU->RASR) + 1)) - 1;
244     printf("R%d %08lX..%08lX %08lX %08lX\n", i, ra, ra + rsz, MPU->RBAR, MPU->RASR);
245   }
246
247   /*printf("IAP version %08X\n", lpcisp_read_partid());*/
248
249   return 0;
250 }
251
252 int cmd_do_goaddr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
253 {
254   char *p;
255   long addr;
256
257   p = param[1];
258
259   si_skspace(&p);
260   if(si_ulong(&p,&addr,16)<0) return -CMDERR_BADPAR;
261
262   printf("Jump to address %08lX\n\n", addr);
263
264   ((void(*)(void))addr)();
265
266   return 0;
267 }
268
269 int cmd_do_mujtest(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
270 {
271         volatile uint32_t *testaddr  = (volatile uint32_t *)0x80004000;
272         int p;
273         p=*testaddr++;
274         printf("read %d\n", p);
275         p=*testaddr++;
276         printf("read %d\n", p);
277         p=*testaddr++;
278         printf("read %d\n", p);
279         p=*testaddr++;
280         printf("read %d\n", p);
281         p=*testaddr++;
282         printf("read %d\n", p);
283         p=*testaddr++;
284         printf("read %d\n", p);
285         p=*testaddr++;
286         printf("read %d\n", p);
287         p=*testaddr++;
288         printf("read %d\n", p);
289         p=*testaddr++;
290         printf("read %d\n", p);
291         
292 }
293
294 int cmd_do_zmer(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
295 {
296         volatile uint32_t *testaddr  = (volatile uint32_t *)0x80004000;
297         int32_t values[2048];
298         uint32_t status, status_old, bank,i;
299         int32_t val;
300         status_old = *testaddr;
301         //while((status_old & (1<<2))==(status & (1<<2))) status=*testaddr;
302         //while (*testaddr &(1<<6));
303         *testaddr = 0xa;
304         while (!(*testaddr & (1<<6))) printf("\ncekam\n");
305          status = *testaddr;
306          testaddr =  (volatile uint32_t *)0x80008000;
307          if (status&(1<<2)){
308                 // printf("bank1\n");
309                 testaddr =  (volatile uint32_t *)0x80009000;
310         } else {
311                 //printf("bank0\n");
312         }
313          for(i=0;i<1024;i++){
314         //       values[i]=*testaddr++; 
315                  val=*testaddr++;
316                  val+=(1<<17);
317                  val&=(1<<18)-1;
318                  val -= (1<<17);
319                  values[i]=val;
320          }
321          if (status&(1<<2)){
322                  printf("bank1\n");
323                 //testaddr =  (volatile uint32_t *)0x80008000;
324         } else {
325                 printf("bank0\n");
326         }
327          for (i=0;i<1024;i++){
328                  if (i==1024){
329                          printf("bank1\n");
330                  }
331                  printf("%d\n",values[i]);
332                  
333          }
334          printf("konec\n");
335         
336          return 0;
337 }
338
339 int cmd_do_runnung_meas(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]){
340         volatile uint32_t *testaddr  = (volatile uint32_t *)0x80004000;
341         int32_t values[1024];
342         uint32_t status, status_old, bank,i;
343         int32_t val;
344         status_old = *testaddr;
345         status = *testaddr;
346         while((status_old & (1<<2))==(status & (1<<2))) {status=*testaddr; printf("dely %i %i",status_old & (1<<2),status & (1<<2));}
347         if (status&(1<<2)){
348                 testaddr =  (volatile uint32_t *)0x80009000;
349         } else {
350                 testaddr =  (volatile uint32_t *)0x80008000;
351         }
352         for(i=0;i<1024;i++){
353         //       values[i]=*testaddr++; 
354                  val=*testaddr++;
355                  val = (val>> 17) == 0 ? val : -1 ^ 0x7FFFF | val;
356                  values[i]=-1*val;
357          }
358         if (status&(1<<2)){
359                  printf("bank1\n");
360                 //testaddr =  (volatile uint32_t *)0x80008000;
361         } else {
362                 printf("bank0\n");
363         }
364         //printf("zacatek\n");
365          for (i=0;i<1024;i++){
366                  printf("%d\n",values[i]);
367          }
368          printf("konec\n");
369          return 0;
370 }
371
372
373 int cmd_do_init(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
374 {
375         printf("init!\n");
376         volatile uint32_t *testaddr  = (volatile uint32_t *)0x80004000;
377         *testaddr++ = 0x9;
378 //      *testaddr++;
379         *testaddr++ = 9;
380         *testaddr++ = 399;
381         *testaddr++ = 409;
382         *testaddr++ = 399;
383         *testaddr++ = 9;
384         *testaddr++ = 599;
385         *testaddr++ = 609;
386         //*testaddr++ = 4879;
387         *testaddr++ = 5023999;
388         *testaddr=499;
389         
390         printf("OK!\n");
391         return 0;
392 }
393
394 int cmd_do_send(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
395 {
396         char *end;
397         volatile uint32_t *testaddr  = (volatile uint32_t *)0x80004000;
398         int read;
399         int p=(int) strtol(param[1], &end, 10);
400         testaddr+=p;
401         p=(int) strtol(end, &end, 10);
402         *testaddr=p;
403         read=*testaddr;
404         printf("sent %d & read %d\n", p, read);
405         return 0;
406 }
407
408 cmd_des_t const cmd_des_test_memusage={0, 0,
409                         "memusage","report memory usage",cmd_do_test_memusage,
410                         {0,
411                          0}};
412
413 cmd_des_t const cmd_des_test_adc={0, 0,
414                         "testadc","adc test",cmd_do_test_adc,
415                         {0,
416                          0}};
417
418 #ifdef APPL_WITH_DISTORE_EEPROM_USER
419 cmd_des_t const cmd_des_test_distore={0, 0,
420                         "testdistore","test DINFO store",cmd_do_test_distore,
421                         {0,
422                          0}};
423
424 cmd_des_t const cmd_des_test_diload={0, 0,
425                         "testdiload","test DINFO load",cmd_do_test_diload,
426                         {0,
427                          0}};
428 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
429
430 cmd_des_t const cmd_des_test_loglevel={0, 0,
431                         "loglevel","select logging level",
432                         cmd_do_test_loglevel,{}};
433
434 cmd_des_t const cmd_des_spimst={0, CDESM_OPCHR|CDESM_WR,
435                         "SPIMST","SPI master communication request",
436                         cmd_do_spimst_blocking,{(void*)0}};
437
438 cmd_des_t const cmd_des_spimstx={0, CDESM_OPCHR|CDESM_WR,
439                         "SPIMST#","SPI# master communication request",
440                         cmd_do_spimst_blocking,{(void*)-1}};
441
442 cmd_des_t const cmd_des_testsdram={0, 0,
443                         "testsdram","test SDRAM",
444                         cmd_do_testsdram,{(void*)0}};
445
446 cmd_des_t const cmd_des_testmpu={0, 0,
447                         "testmpu","test MPU",
448                         cmd_do_testmpu,{(void*)0}};
449
450 cmd_des_t const cmd_des_goaddr={0, 0,
451                         "goaddr","run from address",
452                         cmd_do_goaddr,{(void*)0}};
453                         
454 cmd_des_t const cmd_des_mujtest={0,0,"mujtest","run my simple test", cmd_do_mujtest,{(void*)0}};
455
456 cmd_des_t const cmd_des_zmer={0,0,"zmer","read values from spectrometer (2 readouts)", cmd_do_zmer,{(void*)0x12}};
457
458 cmd_des_t const cmd_des_scan={0,0,"scan","read values from spectrometer (2 readouts)", cmd_do_zmer,{(void*)0x10}};
459
460
461 cmd_des_t const cmd_des_init={0,0,"init", "inicializacni vypis pro zacatek komunikace", cmd_do_init,{(void*)0}};
462
463 cmd_des_t const cmd_des_sendvalue={0,0,"send", "send value to given address", cmd_do_send,{(void*)0}};
464
465 cmd_des_t const cmd_des_runnung_meas = {0,0,"run","read values from spectrometer (running)", cmd_do_runnung_meas,{(void*)0x10}};
466
467 cmd_des_t const *const cmd_appl_tests[]={
468   &cmd_des_test_memusage,
469   &cmd_des_test_adc,
470  #ifdef APPL_WITH_DISTORE_EEPROM_USER
471   &cmd_des_test_distore,
472   &cmd_des_test_diload,
473  #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
474   &cmd_des_test_loglevel,
475   &cmd_des_spimst,
476   &cmd_des_spimstx,
477   &cmd_des_testsdram,
478   &cmd_des_testmpu,
479   &cmd_des_goaddr,
480   &cmd_des_mujtest,
481   &cmd_des_zmer,
482   &cmd_des_scan,
483   &cmd_des_init,
484   &cmd_des_sendvalue,
485   & cmd_des_runnung_meas,
486   NULL
487 };
488