]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - source/commands.c
CANLOOPBACK extended to support CAN1, CAN2, CAN3. User can choose vchich CAN is sourc...
[pes-rpp/rpp-test-sw.git] / source / commands.c
1 /** @file commands.c
2 *   @brief Command definitions file
3 *   @date 31.July.2012
4 *
5 *  Command is defined by its function. The function and others parameters are stored as list element.
6 *  All commands represented as elements are stored in lists connected into one main list.
7 */
8
9 /* Include files */
10 #include "cmd_proc.h"
11 #include "sys_common.h"
12 #include "cmdio_tisci.h"
13 #include "string.h"
14 #include "stdio.h"
15 #include "can.h"
16 #include "adc.h"
17 #include "dmm.h"
18 #include "din.h"
19 #include "mout.h"
20 #include "hout.h"
21 #include "vbat.h"
22
23 /** @brief Count of AD channels in ADC1
24 */
25 #define ADC1_CHANNEL_COUNT      12
26 /** @brief Count of AD channels in HOUT port
27 */
28 #define HOUT_IFBK_CHANNEL_COUNT         6
29
30 /** @brief List in which commands are stored */
31 cmd_des_t const **cmd_list;
32 /** @brief Semaphore blocking task when it waits for interrupt signaling message receive */
33 xSemaphoreHandle canMsgReceived;
34 xSemaphoreHandle canMsgSent;
35
36 /** @brief Semaphore blocking task when it waits for interrupt signaling end of the conversion */
37 xSemaphoreHandle adcDataConverted;
38 /** @brief Data structure containing converted data from ADC */
39 ADCData_t adcData;
40
41 /** @brief Set of port adresses for each din pin
42  *
43  * Defined in din.c */
44 extern gioPORT_t * dinPorts[8];
45 /** @brief Set of port adresses for each mout pin
46  *
47  * Defined in mout.c */
48 extern gioPORT_t * moutPorts[12];
49 /** @brief Set of port adresses for each hout pin
50  *
51  * Defined in hout.c */
52 extern gioPORT_t * houtPorts[12];
53
54 /** @brief Set of bit indexes for each din pin
55  *
56  * Each element stores index of a bit in register assigned to din pin.
57  * Defined in din.c */
58 extern uint8_t dinBits[8];
59 /** @brief Set of bit indexes for each mout pin
60  *
61  * Each element stores index of a bit in register assigned to mout pin.
62  * Defined in din.c */
63 extern uint8_t moutBits[12];
64 /** @brief Set of bit indexes for each hout pin
65  *
66  * Each element stores index of a bit in register assigned to hout pin.
67  * Defined in din.c */
68 extern uint8_t houtBits[12];
69 /** @brief Set of bit indexes for each v_bat pin
70  *
71  * Each element stores index of a bit in register assigned to v_bat pin.
72  * Defined in vbat.c */
73 extern uint8_t vbatBits[4];
74
75 canBASE_t* canDst;
76 canBASE_t* canSrc;
77 uint32_t canMsgBox;
78
79
80 /* ------------------------------
81  * User defined command functions
82  * ------------------------------
83  */
84
85 int cmd_do_vbat_set(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
86         vbatSetBit(NULL, vbatBits[0], 1);
87         print((uint8_t *)"V_BAT_EN set.");
88         return 0;
89 }
90
91 int cmd_do_vbat_clr(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
92         vbatSetBit(NULL, vbatBits[0], 0);
93         print((uint8_t *)"V_BAT_EN cleared.");
94         return 0;
95 }
96 int cmd_do_vbat_get(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
97         uint32_t value = vbatGetBit(vbatBits[0]);
98         if (value) {
99                 print((uint8_t *)"1\r\n");
100         }
101         else {
102                 print((uint8_t *)"0\r\n");
103         }
104         return 0;
105 }
106
107 /**     Tests the capacity of connected SDRAM
108  * @param[in]   cmd_io  Pointer to IO stack
109  * @param[in]   des             Pointer to command descriptor
110  * @param[in]   param   Parameters of command
111  * @return      0 when OK or error code
112  */
113 int cmd_do_test_ram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
114         volatile uint32_t* addrPtr = (uint32_t*)0x80000000U;
115         volatile uint32_t* endAddr = (uint32_t*)0x87FFFFFFU;
116         uint32_t pattern = 0x55555555U;
117         uint32_t cnt = 0;
118         uint32_t errCnt = 0;
119         uint32_t readVal = 0;
120
121         while (addrPtr <= endAddr) {
122                         *addrPtr = pattern;
123                         pattern += 0x55555555U;
124                         addrPtr++;
125         }
126
127         addrPtr = (uint32_t *)0x80000000U;
128         pattern = 0x55555555U;
129         while (addrPtr <= endAddr) {
130                 readVal = *addrPtr;
131                 if (pattern == readVal) cnt++;
132                 else if(errCnt++ <= 10) {
133                         print((uint8_t *)"Error at 0x");
134                         char numBuf[10];
135                         i2str(numBuf, (long)addrPtr, 1, 16);
136                         print((uint8_t *)numBuf);
137                         print((uint8_t *)"\r\n");
138                 }
139                 else break;
140                 pattern += 0x55555555U;
141                 addrPtr++;
142         }
143
144         cnt /= (sizeof(uint32_t)*1000000);
145         if (cnt == 0) {
146                 print((uint8_t*)"SDRAM not connected.\r\n");
147         }
148         else {
149                 char numBuf[10];
150                 i2str(numBuf, cnt, 1, 10);
151                 print((uint8_t*)"SDRAM installed: ");
152                 print((uint8_t*)numBuf);
153                 print((uint8_t*)" MB\r\n");
154         }
155
156         return 0;
157 }
158
159 /**     Reads values from MOUT_EN pins and prints them
160  * @param[in]   cmd_io  Pointer to IO stack
161  * @param[in]   des             Pointer to command descriptor
162  * @param[in]   param   Parameters of command
163  * @return      0 when OK or error code
164  */
165 int cmd_do_read_mout_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
166         print((uint8_t *)"\r\nValues from MOUT_EN:");
167         uint32_t portVal = moutGetPort();
168         print((uint8_t *)"\r\npin\t|\tvalue");
169         uint8_t i;
170         for (i = 6, portVal = portVal>>6; i < 12; i++, portVal = portVal>>1) {
171                 uint8_t bit = portVal&1;
172                 char numBuf[2];
173                 i2str(numBuf, i-5, 1, 10);
174                 print((uint8_t *)"\r\n");
175                 print((uint8_t *)numBuf);
176                 print((uint8_t *)"\t|\t");
177                 if (bit) {
178                         print((uint8_t *)"1");
179                 }
180                 else {
181                         print((uint8_t *)"0");
182                 }
183         }
184         return 0;
185 }
186
187 /**     Reads values from DIN pins and prints them
188  * @param[in]   cmd_io  Pointer to IO stack
189  * @param[in]   des             Pointer to command descriptor
190  * @param[in]   param   Parameters of command
191  * @return      0 when OK or error code
192  */
193 int cmd_do_read_din_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
194         print((uint8_t *)"\r\nValues from DIN:");
195         uint32_t portVal = dinGetPort();
196         print((uint8_t *)"\r\npin\t|\tvalue");
197         uint8_t i;
198         for (i = 0; i < 8; i++,portVal = portVal>>1) {
199                 uint8_t bit = portVal&1;
200                 print((uint8_t *)"\r\n");
201                 char numBuf[1];
202                 i2str(numBuf, i+8, 1, 10);
203                 print((uint8_t *)numBuf);
204                 print((uint8_t *)"\t|\t");
205                 if (bit) {
206                         print((uint8_t *)"1");
207                 }
208                 else {
209                         print((uint8_t *)"0");
210                 }
211         }
212         return 0;
213 }
214
215 /**     Reads values from HOUT_DIAG pins and prints them
216  * @param[in]   cmd_io  Pointer to IO stack
217  * @param[in]   des             Pointer to command descriptor
218  * @param[in]   param   Parameters of command
219  * @return      0 when OK or error code
220  */
221 int cmd_do_read_hout_diag_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
222         print((uint8_t *)"\r\nValues from HOUT_DIAG:");
223         uint32_t portVal = houtGetPort();
224         print((uint8_t *)"\r\npin\t|\tvalue");
225         uint8_t i;
226         for (i = 0, portVal = portVal>>6; i < 6; i++,portVal = portVal>>1) {
227                 uint8_t bit = portVal&1;
228                 print((uint8_t *)"\r\n");
229                 char numBuf[1];
230                 i2str(numBuf, i+1, 1, 10);
231                 print((uint8_t *)numBuf);
232                 print((uint8_t *)"\t|\t");
233                 if (bit) {
234                         print((uint8_t *)"1");
235                 }
236                 else {
237                         print((uint8_t *)"0");
238                 }
239         }
240         return 0;
241 }
242
243 /**     Reads values from ADC and stores them to ADCData structure defined as global.
244  * ADCData structure must be defined properly before this function is called.
245  * @param[in]   adc             Pointer to ADC base register
246  * @param[in]   group   ADC group selector
247  * @return      0 when OK or error code
248  */
249 int read_adc(adcBASE_t* adc, uint32_t group) {
250         if (adcData.adc_data == NULL) {
251                 return 1;
252         }
253         adcDataConverted = xSemaphoreCreateCounting(1, 0);
254         adcEnableNotification(adc, group);
255     adcCalibration(adc);
256     adcMidPointCalibration(adc);
257
258         adcStartConversion(adc, group);
259         xSemaphoreTake(adcDataConverted, portMAX_DELAY);
260
261     if (adcData.flags & BAD_CHANNELS_COUNT) {
262         print((uint8_t *)"\r\nERROR: Bad count of channels was read! Can not proceed.");
263         adcData.flags &= ~BAD_CHANNELS_COUNT;
264         adcDisableNotification(adc, group);
265         vSemaphoreDelete(canMsgReceived);
266         return 1;
267     }
268     if (adcData.flags & MEM_OVERRUN) {
269         print((uint8_t *)"\r\nWARNING: ADC Memory overrun detected. Values can be imprecise.");
270         adcData.flags &= ~MEM_OVERRUN;
271     }
272         adcDisableNotification(adc, group);
273         vSemaphoreDelete(canMsgReceived);
274     return 0;
275 }
276
277 /**     Reads values from HOUT_IFBK pins (subset of ADC)
278  * @param[in]   cmd_io  Pointer to IO stack
279  * @param[in]   des             Pointer to command descriptor
280  * @param[in]   param   Parameters of command
281  * @return      0 when OK or error code
282  */
283 int cmd_do_read_hout_ifbk_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
284 {
285         char numBuf[4];                                 // Buffer used for i2str conversion
286     uint32_t i;                                         // Cycle control variable
287
288     // ADCData structure initialization
289     adcData_t adc_data_origin[HOUT_IFBK_CHANNEL_COUNT];
290     adcData.adc_data = adc_data_origin;
291     adcData.ch_count = HOUT_IFBK_CHANNEL_COUNT;
292
293     print((uint8_t *)"\r\nRead HOUT_IFBK values.");
294     if (read_adc(adcREG2, adcGROUP1) == 1)
295         return 1;
296     /* conversion results */
297     for (i = 0; i < adcData.ch_count; i++) {
298         print((uint8_t *)"\r\nHOUT: ");
299         i2str(numBuf, adcData.adc_data[i].id-1, 1, 10);
300         print((uint8_t *)numBuf);
301         print((uint8_t *)"\tvalue: ");
302         i2str(numBuf, adcData.adc_data[i].value, 1, 10);
303         print((uint8_t *)numBuf);
304         print((uint8_t *)" 0x");
305         i2str(numBuf, adcData.adc_data[i].value, 1, 16);
306         print((uint8_t *)numBuf);
307     }
308     adcData.adc_data = NULL;
309     return 0;
310 }
311
312 /**     Reads values from ADC1 pins
313  * @param[in]   cmd_io  Pointer to IO stack
314  * @param[in]   des             Pointer to command descriptor
315  * @param[in]   param   Parameters of command
316  * @return      0 when OK or error code
317  */
318 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
319 {
320         char numBuf[4];                                 // Buffer used for i2str conversion
321     uint32_t i;                                         // Cycle control variable
322
323     /* ADCData structure initialization */
324     adcData_t adc_data_origin[ADC1_CHANNEL_COUNT];
325     adcData.adc_data = adc_data_origin;
326     adcData.ch_count = ADC1_CHANNEL_COUNT;
327
328     print((uint8_t *)"\r\nADC1 values:");
329     if (read_adc(adcREG1, adcGROUP1) == 1)
330         return 1;
331
332     /* conversion results :                                       */
333     for (i = 0; i < adcData.ch_count; i++) {
334         print((uint8_t *)"\r\nChannel: ");
335         i2str(numBuf, adcData.adc_data[i].id, 1, 10);
336         print((uint8_t *)numBuf);
337         print((uint8_t *)"\tvalue: ");
338         i2str(numBuf, adcData.adc_data[i].value, 1, 10);
339         print((uint8_t *)numBuf);
340         print((uint8_t *)" 0x");
341         i2str(numBuf, adcData.adc_data[i].value, 1, 16);
342         print((uint8_t *)numBuf);
343     }
344     adcData.adc_data = NULL;
345     return 0;
346 }
347
348 /**     Read values general command function. According the suffix switch to appropriate reading procedure.
349  * @param[in]   cmd_io  Pointer to IO stack
350  * @param[in]   des             Pointer to command descriptor
351  * @param[in]   param   Parameters of command
352  * @return      0 when OK or error code
353  */
354 int cmd_do_read_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
355         uint32_t suffix = 0;
356         int ret;
357         char* token = NULL;
358         if ((token = strtok(param[1], " ")) == NULL) {
359                 print((uint8_t *)"ERROR: No suffix!");
360                 return 1;
361         }
362
363         if (!strcmp(token, "ADC1"))
364                 suffix = 1;
365         else if (!strcmp(token, "DIN"))
366                 suffix = 2;
367         else if (!strcmp(token, "MOUTE"))
368                 suffix = 3;
369         else if (!strcmp(token, "HOUTD"))
370                 suffix = 4;
371         else if (!strcmp(token, "HOUTF"))
372                 suffix = 5;
373         else {
374                 print((uint8_t *)"ERROR: Wrong suffix!");
375                 return 1;
376         }
377         switch(suffix) {
378         case 1:
379                 ret = cmd_do_read_adc1_values(cmd_io, des, param);
380                 break;
381         case 2:
382                 ret = cmd_do_read_din_values(cmd_io, des, param);
383                 break;
384         case 3:
385                 ret = cmd_do_read_mout_values(cmd_io, des, param);
386                 break;
387         case 4:
388                 ret = cmd_do_read_hout_diag_values(cmd_io, des, param);
389                 break;
390         case 5:
391                 ret = cmd_do_read_hout_ifbk_values(cmd_io, des, param);
392                 break;
393         default:
394                 ret = 1;
395         }
396
397         return ret;
398 }
399
400 /**     Set value general command function. According the suffix sets given bit to given value to appropriate port pin.
401  * @param[in]   cmd_io  Pointer to IO stack
402  * @param[in]   des             Pointer to command descriptor
403  * @param[in]   param   Parameters of command
404  * @return      0 when OK or error code
405  */
406 int cmd_do_set_pin(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
407         void (*setBit)(gioPORT_t *, uint32_t, uint32_t) = NULL;
408         gioPORT_t * port = NULL;
409         char* token = NULL;
410         uint32_t pin;
411         uint32_t bit;
412         uint32_t value;
413         uint32_t suffix = 0;
414
415         // Get suffix
416         if ((token = strtok(param[1], " ")) == NULL) {
417                 print((uint8_t *)"ERROR: No suffix!");
418                 return 1;
419         }
420         if (!strcmp(token, "MOUTI"))
421                 suffix = 1;
422         else if (!strcmp(token, "HOUTI")) {
423                 suffix = 2;
424         }
425         else if (!strcmp(token, "VBAT")) {
426                 suffix = 3;
427         }
428
429         else {
430                 print((uint8_t *)"ERROR: Wrong suffix!");
431                 return 1;
432         }
433         // Get index of the bit
434         if ((token = strtok(param[2], " ")) == NULL) {
435                 print((uint8_t *)"ERROR: Parameter 1 expected.");
436                 return 1;
437         }
438         if (EOF == sscanf(token, "%d", &pin)){
439                 print((uint8_t *)"ERROR: unsigned number expected as param 1!");
440                 return 1;
441         }
442         // Get value
443         if ((token = strtok(NULL, " ")) == NULL) {
444                 print((uint8_t *)"ERROR: Parameter 2 expected.");
445                 return 1;
446         }
447         if (EOF == sscanf(token, "%d", &value)){
448                 print((uint8_t *)"ERROR: unsigned number expected as param 2!");
449                 return 1;
450         }
451         if ((token = strtok(NULL, " ")) != NULL) {
452                 print((uint8_t *)"ERROR: More than 2 parameters detected!");
453                 return 1;
454         }
455
456         switch(suffix) {
457         case 1 :        // MOUTI
458                 setBit=&gioSetBit;
459                 if (pin < 1 || pin > 6) {
460                         print((uint8_t *)"ERROR: 1st parameter out of range <1;6>!");
461                         return 1;
462                 }
463                 if (value != 1 && value != 0) {
464                         print((uint8_t *)"ERROR: 2nd parameter out of range <0;1>!");
465                         return 1;
466                 }
467                 port = moutPorts[pin-1];
468                 bit = moutBits[pin-1];
469                 break;
470         case 2 :        // HOUTI
471                 setBit=&gioSetBit;
472                 if (pin < 1 || pin > 6) {
473                         print((uint8_t *)"ERROR: 1st parameter out of range <1;6>!");
474                         return 1;
475                 }
476                 if (value != 1 && value != 0) {
477                         print((uint8_t *)"ERROR: 2nd parameter out of range <0;1>!");
478                         return 1;
479                 }
480                 port = houtPorts[pin-1];
481                 bit = houtBits[pin-1];
482                 break;
483         case 3 :        // VBAT
484                 setBit=&vbatSetBitAndDir;
485                 if (pin < 1 || pin > 3) {
486                         print((uint8_t *)"ERROR: 1st parameter out of range <1;3>!");
487                         return 1;
488                 }
489                 if (value != 1 && value != 0) {
490                         print((uint8_t *)"ERROR: 2nd parameter out of range <0;1>!");
491                         return 1;
492                 }
493                 port = NULL;
494                 bit = vbatBits[pin];
495                 break;
496         }
497         setBit(port, bit, value);
498         print((uint8_t *)"Bit has been set.");
499         return 0;
500 }
501
502 /**     Get value general command function. According the suffix gets given bit of appropriate port pin.
503  * @param[in]   cmd_io  Pointer to IO stack
504  * @param[in]   des             Pointer to command descriptor
505  * @param[in]   param   Parameters of command
506  * @return      0 when OK or error code
507  */
508 int cmd_do_get_pin(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
509         char* token = NULL;
510         char numBuf[4];
511         uint32_t value;
512         uint32_t pin;
513         uint32_t suffix = 100;
514         // Get suffix
515         if ((token = strtok(param[1], " ")) == NULL) {
516                 print((uint8_t *)"ERROR: No suffix!");
517                 return 1;
518         }
519         if (!strcmp(token, "ADC1"))
520                 suffix = 1;
521         else if (!strcmp(token, "DIN"))
522                 suffix = 2;
523         else if (!strcmp(token, "MOUTE"))
524                 suffix = 3;
525         else if (!strcmp(token, "HOUTD"))
526                 suffix = 4;
527         else if (!strcmp(token, "HOUTF"))
528                 suffix = 5;
529         else if (!strcmp(token, "VBAT"))
530                 suffix = 6;
531         else {
532                 print((uint8_t *)"ERROR: Wrong suffix!");
533                 return 1;
534         }
535         // Get pin
536         if ((token = strtok(param[2], " ")) == NULL) {
537                 print((uint8_t *)"ERROR: Parameter 1 expected.");
538                 return 1;
539         }
540         if (EOF == sscanf(token, "%d", &pin)){
541                 print((uint8_t *)"ERROR: unsigned number expected as param 1!");
542                 return 1;
543         }
544         if ((token = strtok(NULL, " ")) != NULL) {
545                 print((uint8_t *)"ERROR: More than 1 parameters detected!");
546                 return 1;
547         }
548
549         switch(suffix) {
550         case 1 :        // ADC1
551         {
552                 if (pin > 11) {
553                         print((uint8_t *)"ERROR: 1st parameter out of range <0;11>!");
554                         return 1;
555                 }
556
557                 adcData_t adc_data_origin[ADC1_CHANNEL_COUNT];
558             adcData.adc_data = adc_data_origin;
559             adcData.ch_count = ADC1_CHANNEL_COUNT;
560             if (read_adc(adcREG1, adcGROUP1) == 1)
561                 return 1;
562             value = adcData.adc_data[pin].value;
563             adcData.adc_data = NULL;
564                 break;
565         }
566         case 2 :        // DIN
567                 pin -= 7;
568                 if (pin < 1  || pin > 8) {
569                         print((uint8_t *)"ERROR: 1st parameter out of range <8;15>!");
570                         return 1;
571                 }
572                 value = gioGetBit(dinPorts[pin-1], dinBits[pin-1]);
573                 break;
574         case 3 :        // MOUTE
575                 if (pin < 1 || pin > 6) {
576                         print((uint8_t *)"ERROR: 1st parameter out of range <1;6>!");
577                         return 1;
578                 }
579                 value = gioGetBit(moutPorts[pin-1], moutBits[pin-1]);
580                 break;
581         case 4 :        // HOUTD
582                 if (pin < 1 || pin > 6) {
583                         print((uint8_t *)"ERROR: 1st parameter out of range <1;6>!");
584                         return 1;
585                 }
586                 value = gioGetBit(houtPorts[pin-1], houtBits[pin-1]);
587                 break;
588         case 5 :        // HOUTF
589         {
590                 if (pin < 1 || pin > 6) {
591                         print((uint8_t *)"ERROR: 1st parameter out of range <1;6>!");
592                         return 1;
593                 }
594                 adcData_t adc_data_origin[HOUT_IFBK_CHANNEL_COUNT];
595             adcData.adc_data = adc_data_origin;
596             adcData.ch_count = HOUT_IFBK_CHANNEL_COUNT;
597             if (read_adc(adcREG2, adcGROUP1) == 1)
598                 return 1;
599             value = adcData.adc_data[pin].value;
600             adcData.adc_data = NULL;
601                 break;
602         }
603         case 6 :        // VBAT
604         {
605                 if (pin < 1 || pin > 3) {
606                         print((uint8_t *)"ERROR: 1st parameter out of range <1;3>!");
607                         return 1;
608                 }
609                 value = vbatGetBit(vbatBits[pin]);
610                 break;
611         }
612         }
613         i2str(numBuf, value, 1, 10);
614         print((uint8_t *)numBuf);
615         return 0;
616 }
617
618 /**     Runs test of ADC1. Test each channel if it is shorted to REFLO or REFHI
619  * @param[in]   cmd_io  Pointer to IO stack
620  * @param[in]   des             Pointer to command descriptor
621  * @param[in]   param   Parameters of command
622  * @return      0 when OK or error code
623  */
624 int cmd_do_test_adc1_connection(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
625 {
626         uint32_t Vn[ADC1_CHANNEL_COUNT];        // Nominal value, converted from ADC
627         uint32_t Vu[ADC1_CHANNEL_COUNT];        // Upper bound
628         uint32_t Vd[ADC1_CHANNEL_COUNT];        // Lower bound
629         uint32_t* Vptr;                                         // Pointer to value (used in cycle)
630         uint8_t j;                                                      // Cycle control variable
631         uint32_t i;                                                     // Cycle control variable
632         char numBuf[2];                                         // Buffer used for i2str conversion
633
634         // Initialize ADCData structure
635     adcData_t adc_data_origin[ADC1_CHANNEL_COUNT];
636     adcData.adc_data = adc_data_origin;
637     adcData.ch_count = ADC1_CHANNEL_COUNT;
638
639     print((uint8_t *)"\r\nTesting ADC1 connection.");
640
641     // Read and store Nominal voltage, Lowest voltage Highest voltage in that order
642     for (j = 0; j < 3; j++) {
643         // Choose voltage
644                 switch(j) {
645                         case    0:
646                                 adcREG1->CALCR = 0;
647                                 Vptr = &Vn[0];
648                                 break;
649                         case    1:
650                                 adcREG1->CALCR |= (1 << 24);
651                                 Vptr = &Vd[0];
652
653                                 break;
654                         case    2:
655                                 adcREG1->CALCR |= (1 << 8);
656                                 Vptr = &Vu[0];
657                                 break;
658                 }
659                 // Read
660             if (read_adc(adcREG2, adcGROUP1) == 1)
661                 return 1;
662             // Store
663                 for (i = 0; i < ADC1_CHANNEL_COUNT; i++) {
664                         Vptr[i] = adcData.adc_data[i].value;
665                 }
666         }
667
668         print((uint8_t *)"\r\n|\tChannel\t|\tStatus");
669         for (i = 0; i < ADC1_CHANNEL_COUNT; i++) {
670                 i2str(numBuf, i, 2, 10);
671                 print((uint8_t *)"\r\n|\t");
672                 print((uint8_t *)numBuf);
673                 print((uint8_t *)"\t|\t");
674
675                 if(Vu[i] <= Vd[i]) {
676                         if (Vn[i] == 0) {
677                                 print((uint8_t *)"shorted with REFLO");
678                         }
679                         else {
680                                 print((uint8_t *)"shorted with REFHI");
681                         }
682                 }
683                 else {
684                         print((uint8_t *)"GOOD");
685                 }
686         }
687     adcREG1->CALCR = 0;
688         adcData.adc_data = NULL;
689         return 0;
690 }
691
692
693 /**     Runs test of CAN loopback. Send message from CAN1 and receive it on CAN2.
694  * @param[in]   cmd_io  Pointer to IO stack
695  * @param[in]   des             Pointer to command descriptor
696  * @param[in]   param   Parameters of command
697  * @return      0 when OK or error code
698  */
699 int cmd_do_test_can_loopback(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
700 {
701         uint32_t can[2];
702         canBASE_t *canBases[3] = {canREG1, canREG2, canREG3};
703         uint32_t canMailBoxes[3] = {canMESSAGE_BOX1, canMESSAGE_BOX2, canMESSAGE_BOX3};
704         char* token = NULL;
705         uint32_t txTimeOutCnt = 0;
706         uint32_t rxTimeOutCnt = 0;
707         uint32_t messagesTransmitted = 0;
708         uint32_t messagesReceived = 0;
709         uint32_t i;
710         uint8_t tx_data[8] = {'T', 'E', 'S', 'T', ' ', 'C', 'A', 'N'};
711         uint8_t rx_data[8] = {0, 0, 0, 0, 0, 0, 0, 0 };
712
713         // Get can ID 1
714         if ((token = strtok(param[1], " ")) == NULL) {
715
716                 print((uint8_t *)"ERROR: Parameter 1 expected.");
717                 return 1;
718         }
719         if (EOF == sscanf(token, "%d", &can[0])){
720                 print((uint8_t *)"ERROR: unsigned number expected as param 1!");
721                 return 1;
722         }
723         // Get can ID 2
724         if ((token = strtok(NULL, " ")) == NULL) {
725                 print((uint8_t *)"ERROR: Parameter 2 expected.");
726                 return 1;
727         }
728         if (EOF == sscanf(token, "%d", &can[1])){
729                 print((uint8_t *)"ERROR: unsigned number expected as param 2!");
730                 return 1;
731         }
732         if ((token = strtok(NULL, " ")) != NULL) {
733                 print((uint8_t *)"ERROR: More than 2 parameters detected!");
734                 return 1;
735         }
736         if (can[0] == can[1]) {
737                 print((uint8_t *)"ERROR: Destination equals source!);");
738                 return 1;
739         }
740         if (can[0] < 1 | can[1] > 3) {
741                 print((uint8_t *)"Parameter out of range <1;3>!");
742                 return 1;
743         }
744         canSrc = canBases[can[0]-1];
745         canDst = canBases[can[1]-1];
746         canMsgBox = canMailBoxes[can[0]-1];
747         canMsgReceived = xSemaphoreCreateCounting(1, 0);
748         canMsgSent = xSemaphoreCreateCounting(1, 0);
749
750         print((uint8_t *)"\r\nTesting CAN loopback\r\n");
751         i = canSrc->ES; // Reset error flag
752         i = canDst->ES; // Reset error flag
753         canEnableErrorNotification(canDst);
754         canEnableErrorNotification(canSrc);
755
756         for (i = 0; i < 100; i++) {             // Send 100 times the message
757                 if (canGetErrorLevel(canSrc) != canLEVEL_ACTIVE) {
758                         print((uint8_t*)"TX: CAN is not active.\r\n");
759                         break;
760                 }
761                 canTransmit(canSrc, canMsgBox, tx_data);
762                 if (xSemaphoreTake(canMsgSent, 100/portTICK_RATE_MS) == pdFALSE) {
763                         txTimeOutCnt++;
764                 }
765                 else {
766                         if (canGetErrorLevel(canSrc) & canLEVEL_BUS_OFF) {
767                                 print((uint8_t *)"TX: CAN bus is off.\r\n");
768                                 break;
769                         }
770                         else if (canGetErrorLevel(canSrc) & canLEVEL_PASSIVE) {
771                                 print((uint8_t *)"TX: CAN bus in passive mode.\r\n");
772                                 continue;
773                         }
774                         else if (canGetErrorLevel(canSrc) & canLEVEL_WARNING) {
775                                 print((uint8_t *)"TX: CAN bus error counter warning.\r\n");
776                                 continue;
777                         }
778                         else {
779                                 messagesTransmitted++;
780                                 if (xSemaphoreTake(canMsgReceived, 100/portTICK_RATE_MS) == pdFALSE) {
781                                         rxTimeOutCnt++;
782                                 }
783                                 else {
784                                         if (canGetData(canDst, canMsgBox, rx_data))
785                                                 messagesReceived++;
786
787                                         if (canGetErrorLevel(canDst) & canLEVEL_BUS_OFF) {
788                                                 print((uint8_t *)"RX: CAN bus is off.\r\n");
789                                                 break;
790                                         }
791                                         else if (canGetErrorLevel(canDst) & canLEVEL_PASSIVE) {
792                                                 print((uint8_t *)"RX: CAN bus in passive mode.\r\n");
793                                                 continue;
794                                         }
795                                         else if (canGetErrorLevel(canDst) & canLEVEL_WARNING) {
796                                                 print((uint8_t *)"RX: CAN bus error counter reached warning level.\r\n");
797                                                 continue;
798                                         }
799                                 }
800                         }
801                 }
802         }
803         print((uint8_t *)"Messages transmitted: ");
804         char errBuf[10];
805         i2str(errBuf, messagesTransmitted, 1, 10);
806         print((uint8_t *)errBuf);
807         print((uint8_t *)"/100\r\n");
808
809         print((uint8_t *)"Messages received: ");
810         i2str(errBuf, messagesReceived, 1, 10);
811         print((uint8_t *)errBuf);
812         print((uint8_t *)"/100\r\n");
813
814         print((uint8_t *)"TX timeouts: ");
815         i2str(errBuf, txTimeOutCnt, 1, 10);
816         print((uint8_t *)errBuf);
817         print((uint8_t *)"\r\n");
818
819         print((uint8_t *)"RX timeouts: ");
820         i2str(errBuf, rxTimeOutCnt, 1, 10);
821         print((uint8_t *)errBuf);
822         print((uint8_t *)"\r\n");
823
824         print((uint8_t *)"TX error counter: ");
825         uint32_t errCnt = canSrc->EERC & 0xFFU;
826         i2str(errBuf, errCnt, 1, 10);
827         print((uint8_t *)errBuf);
828         print((uint8_t *)"\r\n");
829
830         print((uint8_t *)"RX error counter: ");
831         errCnt = canSrc->EERC & 0xFF00U;
832         i2str(errBuf, errCnt, 1, 10);
833         print((uint8_t *)errBuf);
834         print((uint8_t *)"\r\n");
835
836         canDisableErrorNotification(canDst);
837         canDisableErrorNotification(canSrc);
838         vSemaphoreDelete(canMsgReceived);
839         vSemaphoreDelete(canMsgSent);
840         return 0;
841 }
842
843 /**     Runs test of HOUT. Test, weather HOUT is in fault state, which means that HOUT_DIAG is shorted to ground
844  * @param[in]   cmd_io  Pointer to IO stack
845  * @param[in]   des             Pointer to command descriptor
846  * @param[in]   param   Parameters of command
847  * @return      0 when OK or error code
848  */
849 int cmd_do_test_hout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
850 {
851         print((uint8_t *)"Testing fault state of HOUT port:");
852         uint8_t i;
853         for (i = 0; i < 6; i++) {
854                 HOUTSetBit(houtPorts[i], houtBits[i], 1);
855                 vTaskDelay(100/portTICK_RATE_MS);
856                 if (HOUTGetBit(houtPorts[i+6], houtBits[i+6]) == 1) {
857                         print((uint8_t *)"\r\nOK");
858                 }
859                 else {
860                         print((uint8_t *)"\r\nFAIL");
861                 }
862                 HOUTSetBit(houtPorts[i], houtBits[i], 0);
863         }
864         return 0;
865 }
866
867 /**     Runs test of MOUT. Test, weather MOUT is in fault state, which means that MOUT_EN is shorted to ground
868  * @param[in]   cmd_io  Pointer to IO stack
869  * @param[in]   des             Pointer to command descriptor
870  * @param[in]   param   Parameters of command
871  * @return      0 when OK or error code
872  */
873 int cmd_do_test_mout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
874 {
875         print((uint8_t *)"Testing fault state of MOUT port:");
876         uint8_t i;
877         for (i = 0; i < 6; i++) {
878                 MOUTSetBit(moutPorts[i], moutBits[i], 1);
879                 vTaskDelay(100/portTICK_RATE_MS);
880                 if (MOUTGetBit(moutPorts[i+6], moutBits[i+6]) == 1) {
881                         print((uint8_t *)"\r\nOK");
882                 }
883                 else {
884                         print((uint8_t *)"\r\nFAIL");
885                 }
886                 MOUTSetBit(moutPorts[i], moutBits[i], 0);
887         }
888         return 0;
889 }
890
891 /* ------------------------------
892  * User defined command variables
893  * ------------------------------
894  */
895
896 /* -------------------
897  * Command definitions
898  * -------------------
899  */
900
901 cmd_des_t const cmd_des_vbat_set={
902     0, 0,
903     "VBATSET","Set V_BAT_EN.",
904     cmd_do_vbat_set, (void *)&cmd_list
905 };
906
907 cmd_des_t const cmd_des_vbat_clr={
908             0, 0,
909             "VBATCLR","Clear V_BAT_EN.",
910             cmd_do_vbat_clr, (void *)&cmd_list
911         };
912
913 /** @brief command descriptor for test SDRAM */
914 cmd_des_t const cmd_des_vbat_get={
915     0, 0,
916     "VBATGET","Get value from V_BAT_EN.",
917     cmd_do_vbat_get, (void *)&cmd_list
918 };
919
920 /** @brief command descriptor for test SDRAM */
921 cmd_des_t const cmd_des_test_sdram={
922     0, 0,
923     "TESTSDRAM","Test if SDRAM module is connected and if it is, measure capacity",
924     cmd_do_test_ram, (void *)&cmd_list
925 };
926 /** @brief command descriptor for get value of pin command */
927 cmd_des_t const cmd_des_get_pin={
928     0, 0,
929     "GETPIN*","Get value of pin at port given as suffix\r\n\t"
930                         "-suffix ADC1 pin<0;11>  - read value from ADC1[pin]\r\n\t"
931                         "-suffix DIN pin<8;15>   - read values from DIN[pin]\r\n\t"
932                         "-suffix HOUTD pin<1;6> - read values from HOUT_DIAG[pin]\r\n\t"
933                         "-suffix HOUTF pin<1;6> - read values from HOUT_IFBK[pin]\r\n\t"
934                         "-suffix MOUTE pin<1;6> - read values from MOUT_EN[pin]\r\n\t"
935                 "-suffix VBAT pin<1;3> - reads values from V_BAT_[pin]_EN.",
936     cmd_do_get_pin, (void *)&cmd_list
937 };
938 /** @brief command descriptor for set value to pin command */
939 cmd_des_t const cmd_des_set_pin={
940     0, 0,
941     "SETPIN*","Set value to pin at port given as suffix\r\n\t"
942                         "-suffix: MOUTI pin<1;6> value <0;1> - MOUT[pin]_IN <- value\r\n\t"
943                         "-suffix: HOUTI pin<1;6> value <0;1> - HOUT[pin]_IN <- value\r\n\t"
944                         "-suffix: VBAT id<1;3> value <0;1> - V_BAT_[id]_EN <- value\r\n\t\t"
945                                         "iff value = 1 then V_Bat is set as an input and value to log 1\r\n\t\t"
946                                         "iff value = 0 than V_Bat is set as an output and value to log 0",
947     cmd_do_set_pin, (void *)&cmd_list
948 };
949 /** @brief command descriptor for read values from port command */
950 cmd_des_t const cmd_des_read={
951     0, 0,
952     "READVAL*","Read values from port\r\n\t"
953                         "-suffix ADC1  - read values from ADC1\r\n\t"
954                         "-suffix DIN   - read values from DIN\r\n\t"
955                                 "-suffix HOUTD - read values from HOUT_DIAG\r\n\t"
956                         "-suffix HOUTF - read values from HOUT_IFBK\r\n\t"
957                         "-suffix MOUTE - read values from MOUT_EN",
958     cmd_do_read_values, (void *)&cmd_list
959 };
960 /** @brief command descriptor for test ADC connection command */
961 cmd_des_t const cmd_des_test_adc_connetion={
962     0, 0,
963     "ADCCON","Test fitness of each ADC channel connection",
964     cmd_do_test_adc1_connection, (void *)&cmd_list
965 };
966 /** @brief command descriptor for test CAN loopback command */
967 cmd_des_t const cmd_des_test_can_loopback={
968     0, 0,
969     "CANLOOPBACK","Test CAN loopback",
970     cmd_do_test_can_loopback, (void *)&cmd_list
971 };
972 /** @brief command descriptor for test mout fault state command */
973 cmd_des_t const cmd_des_test_mout_fail={
974     0, 0,
975     "MOUTFAIL","Test if some MOUT port is in fault state",
976     cmd_do_test_mout_fault, (void *)&cmd_list
977 };
978 /** @brief command descriptor for test hout fault state command */
979 cmd_des_t const cmd_des_test_hout_fail={
980     0, 0,
981     "HOUTFAIL","Test if some HOUT port is in fault state",
982     cmd_do_test_hout_fault, (void *)&cmd_list
983 };
984 /** @brief command descriptor for show help command */
985 cmd_des_t const cmd_des_help={
986     0, 0,
987     "HELP","prints help for commands\r\n",
988     cmd_do_help, (void *)&cmd_list
989 };
990
991 /*  ------------------------
992  *  Command lists definitons
993  *  ------------------------
994  */
995 /** @brief Main list of commands */
996 cmd_des_t const *cmd_list_main[]={
997   &cmd_des_help,
998   &cmd_des_get_pin,
999   &cmd_des_read,
1000   &cmd_des_set_pin,
1001   &cmd_des_vbat_clr,
1002   &cmd_des_vbat_get,
1003   &cmd_des_vbat_set,
1004   &cmd_des_test_adc_connetion,
1005   &cmd_des_test_can_loopback,
1006   &cmd_des_test_hout_fail,
1007   &cmd_des_test_mout_fail,
1008   &cmd_des_test_sdram,
1009   NULL
1010 };
1011
1012 /** @brief pointer to the main list */
1013 cmd_des_t const **cmd_list = cmd_list_main;