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