]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_tests.c
Lower number of warnings and declare variable for serial number unconditionally.
[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 <spi_drv.h>
14
15 #include <ul_log.h>
16 #include <ul_logreg.h>
17
18 #include "appl_defs.h"
19
20 int cmd_do_test_memusage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
21 {
22   void *maxaddr;
23   char str[40];
24
25   maxaddr = sbrk(0);
26
27   snprintf(str,sizeof(str),"memusage maxaddr 0x%08lx\n",(unsigned long)maxaddr);
28   cmd_io_write(cmd_io,str,strlen(str));
29
30   return 0;
31 }
32
33 int cmd_do_test_adc(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
34 {
35   printf("ADC: %ld %ld %ld %ld %ld\n",(LPC_ADC->DR[0] & 0xFFF0)>>4,
36                                       (LPC_ADC->DR[1] & 0xFFF0)>>4,
37                                       (LPC_ADC->DR[2] & 0xFFF0)>>4,
38                                       (LPC_ADC->DR[3] & 0xFFF0)>>4,
39                                       (LPC_ADC->DR[7] & 0xFFF0)>>4);
40   return 0;
41 }
42
43 #ifdef APPL_WITH_DISTORE_EEPROM_USER
44 int cmd_do_test_distore(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
45 {
46   appl_distore_user_set_check4change();
47   return 0;
48 }
49
50 int cmd_do_test_diload(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
51 {
52   appl_distore_user_restore();
53   return 0;
54 }
55 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
56
57 int cmd_do_test_loglevel_cb(ul_log_domain_t *domain, void *context)
58 {
59   char s[30];
60   cmd_io_t *cmd_io = (cmd_io_t *)context;
61
62   s[sizeof(s)-1]=0;
63   snprintf(s,sizeof(s)-1,"%s (%d)\n\r",domain->name, domain->level);
64   cmd_io_puts(cmd_io, s);
65   return 0;
66 }
67
68 int cmd_do_test_loglevel(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
69 {
70   int res=0;
71   char *line;
72   line = param[1];
73
74   if(!line||(si_skspace(&line),!*line)) {
75     ul_logreg_for_each_domain(cmd_do_test_loglevel_cb, cmd_io);
76   } else {
77     res=ul_log_domain_arg2levels(line);
78   }
79
80   return res>=0?0:CMDERR_BADPAR;
81 }
82
83 int cmd_do_spimst_blocking(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
84 {
85   int res;
86   int opchar;
87   char *p=param[3];
88   int spi_chan = (int)(intptr_t)des->info[0];
89   uint8_t *tx_buff = NULL;
90   uint8_t *rx_buff = NULL;
91   long addr = 0;
92   int len = 0;
93   spi_drv_t *spi_drv;
94
95   if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
96   if(opchar!=':') 
97     return -CMDERR_OPCHAR;
98
99   if(spi_chan==-1)
100     spi_chan=*param[1]-'0';
101
102   spi_drv = spi_find_drv(NULL, spi_chan);
103   if(spi_drv==NULL)
104     return -CMDERR_BADSUF;
105
106   p=param[3];
107
108   si_skspace(&p);
109   if(isdigit((int)*p)){
110     if(si_long(&p,&addr,16)<0) return -CMDERR_BADPAR;
111   }
112   if(si_fndsep(&p,"({")<0) return -CMDERR_BADSEP;
113
114   if((res=si_add_to_arr(&p, (void**)&tx_buff, &len, 16, 1, "})"))<0)
115     return -CMDERR_BADPAR;
116
117   rx_buff=malloc(len);
118
119   res=-1;
120   if(rx_buff!=NULL)
121     res = spi_transfer(spi_drv, addr, len, tx_buff, rx_buff);
122
123   if(res < 0) {
124     printf("SPI! %02lX ERROR\n",addr);
125   } else {
126     int i;
127     printf("SPI! %02lX ",addr);
128     printf("TX(");
129     for(i=0;i<len;i++) printf("%s%02X",i?",":"",tx_buff[i]);
130     printf(") RX(");
131     for(i=0;i<len;i++) printf("%s%02X",i?",":"",rx_buff[i]);
132     printf(")");
133     printf("\n");
134     return 0;
135   }
136
137   if(rx_buff)
138     free(rx_buff);
139   if(tx_buff)
140     free(tx_buff);
141
142   return 0;
143 }
144
145 cmd_des_t const cmd_des_test_memusage={0, 0,
146                         "memusage","report memory usage",cmd_do_test_memusage,
147                         {0,
148                          0}};
149
150 cmd_des_t const cmd_des_test_adc={0, 0,
151                         "testadc","adc test",cmd_do_test_adc,
152                         {0,
153                          0}};
154
155 #ifdef APPL_WITH_DISTORE_EEPROM_USER
156 cmd_des_t const cmd_des_test_distore={0, 0,
157                         "testdistore","test DINFO store",cmd_do_test_distore,
158                         {0,
159                          0}};
160
161 cmd_des_t const cmd_des_test_diload={0, 0,
162                         "testdiload","test DINFO load",cmd_do_test_diload,
163                         {0,
164                          0}};
165 #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
166
167 cmd_des_t const cmd_des_test_loglevel={0, 0,
168                         "loglevel","select logging level",
169                         cmd_do_test_loglevel,{}};
170
171 cmd_des_t const cmd_des_spimst={0, CDESM_OPCHR|CDESM_WR,
172                         "SPIMST","SPI master communication request",
173                         cmd_do_spimst_blocking,{(void*)0}};
174
175 cmd_des_t const cmd_des_spimstx={0, CDESM_OPCHR|CDESM_WR,
176                         "SPIMST#","SPI# master communication request",
177                         cmd_do_spimst_blocking,{(void*)-1}};
178
179 cmd_des_t const *const cmd_appl_tests[]={
180   &cmd_des_test_memusage,
181   &cmd_des_test_adc,
182  #ifdef APPL_WITH_DISTORE_EEPROM_USER
183   &cmd_des_test_distore,
184   &cmd_des_test_diload,
185  #endif /*APPL_WITH_DISTORE_EEPROM_USER*/
186   &cmd_des_test_loglevel,
187   &cmd_des_spimst,
188   &cmd_des_spimstx,
189   NULL
190 };