]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_fray.c
c8dd93eca07af507fbc3a985e0f0464f40b79240
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_fray.c
1 /*
2  * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * This document contains proprietary information belonging to Czech
10  * Technical University in Prague. Passing on and copying of this
11  * document, and communication of its contents is not permitted
12  * without prior written authorization.
13  *
14  * File : cmd_fray.c
15  *
16  * Abstract:
17  *  Commands for control FlexRay
18  *  - Getting status of FlexRay from chips on SPI
19  *  - Sending test data on FRAY
20  *  - Receiving test data on FRAY
21  *
22  *  There are FlexRay test commands, to test the connection between two FlexRay nodes A and B
23  *  The node A is sending a message 100 times with some period.
24  *  The node B is receiving this message and check if it fits the pattern.
25  *  Commands are counting the number of errors, which is printed at the end of the test.
26  *  After 100 transmissions both nodes are halted.
27  */
28
29 #include "cmd_fray.h"
30 #include "stdio.h"
31
32 #ifndef DOCGEN
33
34 #include "drv/port_def.h"
35 #include "rpp/rpp.h"
36 #include "cmdproc_utils.h"
37 #include "cmdproc.h"
38 #include "spi_resp_transl.h"
39
40 /**
41  * This structure contains global FlexRay configuration.
42  * All nodes in the network have to use the same values for
43  * all parameters of this structure.
44  */
45 static Fr_TMS570LS_ClusterConfigType Fr_cluster_config = {
46         .gColdStartAttempts = 0x2,
47         .gListenNoise = 0xF,
48         .gMacroPerCycle = 0x15E0,   // Macrotick = 1us
49         .gMaxWithoutClockCorrectionFatal = 0xF,
50         .gMaxWithoutClockCorrectionPassive = 0xF,
51         .gNetworkManagementVectorLength = 12,
52         .gNumberOfMinislots = 0x15A,
53         .gNumberOfStaticSlots = 0x8,
54         .gOffsetCorrectionStart = 0xAE4,
55         .gPayloadLengthStatic = 0x9,
56         .gSyncNodeMax = 0xF,
57         .gdActionPointOffset = 0x4,
58         .gdCASRxLowMax = 0x43,
59         .gdDynamicSlotIdlePhase = 0x1,
60         .gdMinislot = 0x4,
61         .gdMinislotActionPointOffset = 0x2,
62         .gdNIT = 0xAE3,
63         .gdSampleClockPeriod = 0,       // 10mbit/sec
64         .gdStaticSlot = 0x56,
65         .gdTSSTransmitter = 0xA,
66         .gdWakeupSymbolRxIdle = 18,
67         .gdWakeupSymbolRxLow = 18,
68         .gdWakeupSymbolRxWindow = 76,
69         .gdWakeupSymbolTxIdle = 180,
70         .gdWakeupSymbolTxLow = 60
71 };
72
73
74 /**
75  * This structure contains local configuration of the FlexRay node A.
76  * That is the node that transmits messages to the node B.
77  */
78 static Fr_TMS570LS_NodeConfigType Fr_node_A_config = {
79         .pAllowHaltDueToClock = 0,
80         .pAllowPassiveToActive = 0xF,
81         .pChannels = FR_CHANNEL_AB,
82         .pClusterDriftDamping = 0x1,
83         .pDelayCompensationA = 0x3,
84         .pDelayCompensationB = 0x3,
85         .pExternOffsetCorrection = 0,
86         .pExternRateCorrection = 0,
87         .pKeySlotUsedForStartup = TRUE,
88         .pKeySlotUsedForSync = TRUE,
89         .pLatestTx = 0x10D,
90         .pMacroInitialOffsetA = 0x6,
91         .pMacroInitialOffsetB = 0x6,
92         .pMicroInitialOffsetA = 0x18,
93         .pMicroInitialOffsetB = 0x18,
94         .pMicroPerCycle = 0x36B00,      // (cycle period, 5.6ms), uT = 25ns@10Mbit/sec,  25ns@5Mbit/sec, 50ns@2.5Mbit/sec
95         .pRateCorrectionOut = 0xCD,
96         .pOffsetCorrectionOut = 0x151,
97         .pSamplesPerMicrotick = 0,      // 10 mbit/sec
98         .pSingleSlotEnabled = TRUE,
99         .pWakeupChannel = FR_CHANNEL_A,
100         .pWakeupPattern = 2,
101         .pdAcceptedStartupRange = 0x81,
102         .pdListenTimeout = 0x36DA2,
103         .pdMaxDrift = 0x151,
104         .pDecodingCorrection = 0x33
105 };
106
107 /**
108  * This structure contains local configuration of the FlexRay node B.
109  * That is the node that receives messages from the node A.
110  */
111 static Fr_TMS570LS_NodeConfigType Fr_node_B_config = {
112         .pAllowHaltDueToClock = 0,
113         .pAllowPassiveToActive = 0xF,
114         .pChannels = FR_CHANNEL_AB,
115         .pClusterDriftDamping = 0x1,
116         .pDelayCompensationA = 0x3,
117         .pDelayCompensationB = 0x3,
118         .pExternOffsetCorrection = 0,
119         .pExternRateCorrection = 0,
120         .pKeySlotUsedForStartup = TRUE,
121         .pKeySlotUsedForSync = TRUE,
122         .pLatestTx = 0x10D,
123         .pMacroInitialOffsetA = 0x6,
124         .pMacroInitialOffsetB = 0x6,
125         .pMicroInitialOffsetA = 0x18,
126         .pMicroInitialOffsetB = 0x18,
127         .pMicroPerCycle = 0x36B00,
128         .pRateCorrectionOut = 0xCD,
129         .pOffsetCorrectionOut = 0x151,
130         .pSamplesPerMicrotick = 0,          // 10 mbit/sec
131         .pSingleSlotEnabled = TRUE,
132         .pWakeupChannel = FR_CHANNEL_A,
133         .pWakeupPattern = 2,
134         .pdAcceptedStartupRange = 0x81,
135         .pdListenTimeout = 0x36DA2,
136         .pdMaxDrift = 0x151,
137         .pDecodingCorrection = 0x33
138 };
139
140 /**
141  * FlexRay node A message RAM configuration.
142  * Reconfiguration is disabled, 2 static buffers are configured.
143  */
144 static Fr_TMS570LS_MsgRAMConfig Fr_node_A_msgRAM_config = {
145         .dynSegmentBufferCount = 0,
146         .fifoBufferCount = 0,
147         .secureBuffers = FR_SB_ALL_REC_DISABLED,
148         .statSegmentBufferCount = 2,
149         .syncFramePayloadMultiplexEnabled = 0
150 };
151
152 /**
153  * FlexRay node B message RAM configuration.
154  * Reconfiguration is disabled, 2 static buffers are configured.
155  */
156 static Fr_TMS570LS_MsgRAMConfig Fr_node_B_msgRAM_config = {
157         .dynSegmentBufferCount = 0,
158         .fifoBufferCount = 0,
159         .secureBuffers = FR_SB_ALL_REC_DISABLED,
160         .statSegmentBufferCount = 2,
161         .syncFramePayloadMultiplexEnabled = 0
162 };
163
164 /**
165  * The FlexRay node A is a sync node and coldstarter. That is why the first buffer
166  * (buffer 0) is configured as TX. All coldstarters and sync nodes must have the
167  * first buffer configured as TX. The FlexRay network has to contain at least two
168  * sync nodes and coldstarters.
169  * The second buffer is used for messages transmission. It is TX and configured as
170  * single-shot to send only one message after some period.
171  */
172 static Fr_TMS570LS_BufferConfigType Fr_node_A_static_buffers_config[] = {
173         {
174                 .channel = FR_CHANNEL_AB,
175                 .cycleCounterFiltering = 0,
176                 .isTx = TRUE,
177                 .fidMask = 0,
178                 .maxPayload = 9,
179                 .msgBufferInterrupt = TRUE,
180                 .payloadPreambleIndicatorTr = FALSE,
181                 .rejectNullFrames = FALSE,
182                 .rejectStaticSegment = FALSE,
183                 .singleTransmit = FALSE,
184                 .slotId = 1
185         },
186         {
187                 .channel = FR_CHANNEL_AB,
188                 .cycleCounterFiltering = 0,
189                 .isTx = TRUE,
190                 .fidMask = 0,
191                 .maxPayload = 9,
192                 .msgBufferInterrupt = TRUE,
193                 .payloadPreambleIndicatorTr = FALSE,
194                 .rejectNullFrames = FALSE,
195                 .rejectStaticSegment = FALSE,
196                 .singleTransmit = TRUE,
197                 .slotId = 3
198         }
199 };
200
201 /**
202  * The FlexRay node B is a sync node and coldstarter. That is why the first buffer
203  * (buffer 0) is configured as TX. All coldstarters and sync nodes must have the
204  * first buffer configured as TX. The FlexRay network has to contain at least two
205  * sync nodes and coldstarters.
206  * The second buffer is used for messages receiving.
207  */
208 static Fr_TMS570LS_BufferConfigType Fr_node_B_static_buffers_config[] = {
209         {
210                 .channel = FR_CHANNEL_AB,
211                 .cycleCounterFiltering = 0,
212                 .isTx = TRUE,
213                 .fidMask = 0,
214                 .maxPayload = 9,
215                 .msgBufferInterrupt = TRUE,
216                 .payloadPreambleIndicatorTr = FALSE,
217                 .rejectNullFrames = FALSE,
218                 .rejectStaticSegment = FALSE,
219                 .singleTransmit = FALSE,
220                 .slotId = 2
221         },
222         {
223                 .channel = FR_CHANNEL_AB,
224                 .cycleCounterFiltering = 0,
225                 .isTx = FALSE,
226                 .fidMask = 0,
227                 .maxPayload = 9,
228                 .msgBufferInterrupt = TRUE,
229                 .payloadPreambleIndicatorTr = FALSE,
230                 .rejectNullFrames = FALSE,
231                 .rejectStaticSegment = FALSE,
232                 .singleTransmit = TRUE,
233                 .slotId = 3
234         }
235 };
236
237 /**
238  * Unifying configuration structure for the node A.
239  */
240 static Fr_ConfigType Fr_config_node_A = {
241         .clusterConfiguration = &Fr_cluster_config,
242         .dynamicBufferConfigs = NULL,
243         .fifoBufferConfigs = NULL,
244         .msgRAMConfig = &Fr_node_A_msgRAM_config,
245         .nodeConfiguration = &Fr_node_A_config,
246         .staticBufferConfigs = Fr_node_A_static_buffers_config
247 };
248
249 /**
250  * Unifying configuration structure for the node B.
251  */
252 static Fr_ConfigType Fr_config_node_B = {
253         .clusterConfiguration = &Fr_cluster_config,
254         .dynamicBufferConfigs = NULL,
255         .fifoBufferConfigs = NULL,
256         .msgRAMConfig = &Fr_node_B_msgRAM_config,
257         .nodeConfiguration = &Fr_node_B_config,
258         .staticBufferConfigs = Fr_node_B_static_buffers_config
259 };
260
261 /**
262  *  Loads data into TX buffer for FlexRay node A.
263  *  Set TX Request for the buffer to start the transmission.
264  */
265 int8_t transmit_node_a()
266 {
267         uint8_t data[18];
268         int i;
269
270         // Write payload for the buffer
271         for (i = 0; i < 18; i++) {
272                 data[i] = i;
273         }
274         return rpp_fr_transmit_lpdu(0, 1, data, 18);
275 }
276
277 /**
278  *      Check data of the received message for correctness.
279  *      @return  0 if message was received and is correct
280  *                       1 if message was received and is incorrect
281  */
282 int check_message(const uint8_t *data)
283 {
284         int i;
285
286         for (i = 0; i < 18; i++) {
287                 if (data[i] != i)
288                         return 1;
289         }
290         return 0;
291 }
292
293
294 /**
295  *  @brief      Get FlexRay status in human readable form
296  *
297  *  Command sends some data on SPI (data do not matters, chips are read only) and retreives a response.
298  *  The response is translated and displayed as readable text.
299  *
300  * @param[in]   cmd_io  Pointer to IO stack
301  * @param[in]   des             Pointer to command descriptor
302  * @param[in]   param   Parameters of command
303  * @return      0 when OK or error code
304  */
305 int cmd_do_fray_status(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
306 {
307         char *p;
308         int pin;
309
310         p = param[1];
311         if (sscanf(p, "%d", &pin) != 1)
312                 return -CMDERR_BADPAR;
313         pin--; // Highlevel command has pin number mapped according the board scheme, this switches to internal representation
314         if (pin > FRAY_NUM_PORTS || pin < 0) return -CMDERR_BADPAR;
315
316         if (Fr_spi_transfer(pin) != 0) return -CMDERR_BADDIO;
317
318         const spitr_cmd_desc_t *cmd_desc = get_spi_cmd_desc(&spitr_port_cmds[PORT_ID_FRAY1],
319                                                                                                                 Fr_spi_get_cmd(pin));
320         if (cmd_desc == NULL)
321                 return -CMDERR_BADPAR;
322         uint32_t fr_spi_response = Fr_spi_response(pin);
323         uint32_t lsbResponse = 0 | ((fr_spi_response & 0xFF) << 8) | ((fr_spi_response & 0xFF00) >> 8);
324
325         spitr_print_resp(cmd_desc, lsbResponse);
326         return 0;
327 }
328
329 /**
330  *  @brief      Start sending data on the FlexRay as node A
331  *
332  * @param[in]   cmd_io  Pointer to IO stack
333  * @param[in]   des             Pointer to command descriptor
334  * @param[in]   param   Parameters of command
335  * @return      0 when OK or error code
336  */
337 int cmd_do_test_frayA(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
338 {
339         uint32_t i;
340         uint32_t error = 0;
341         uint32_t msg_cnt = 0;
342         uint32_t count = 100;
343         Fr_TxLPduStatusType status;
344         Fr_POCStatusType poc_status;
345
346         if (param[1] && sscanf(param[1], "%d", &count) != 1)
347                 return -CMDERR_BADPAR;
348
349         if (rpp_fr_init_driver(&Fr_config_node_A, &error) == FAILURE) {
350                 rpp_sci_printf("Fray driver initialization failed: %#x.\n", error);
351                 return -CMDERR_BADCFG;
352         }
353         if (rpp_fr_init_controller(0, &error) == FAILURE) {
354                 rpp_sci_printf("Fray control node initialization failed: %#x.\n", error);
355                 return -CMDERR_BADCFG;
356         }
357         rpp_sci_printf("Fray control node initialized.\r\n");
358         rpp_sci_printf("Waiting for network connection...\r\n");
359
360         if (rpp_fr_start_communication(0, &error) == FAILURE) {
361                 rpp_sci_printf("Integration to the network failed: %#x.\n", error);
362                 return -CMDERR_BADCFG;
363         }
364         if (rpp_fr_all_slots(0) == FAILURE) {
365                 rpp_sci_printf("All slots mode selection failed.\n");
366                 return -CMDERR_BADCFG;
367         }
368
369         rpp_sci_printf("Connected. Frames transmission is starting. O means OK, X means Error\r\n");
370         if (rpp_fr_clear_timer_irq(0,0) == FAILURE ||
371                 rpp_fr_set_timer(0,0,10,1) == FAILURE
372                 ) {
373                 rpp_sci_printf("Absolute timer setting failed.\n");
374                 return -CMDERR_BADCFG;
375         }
376         for (i = 0; i < count; i++) {
377                 if (transmit_node_a() == FAILURE) {
378                         rpp_sci_printf("X");
379                         error++;
380                 }
381                 else
382                         rpp_sci_printf("O");
383                 msg_cnt++;
384
385                 do {
386                         rpp_fr_check_tx_lpdu_status(0, 1, &status);
387                         rpp_fr_get_poc_status(0, &poc_status);
388                 } while (status == FR_NOT_TRANSMITTED && poc_status.ErrorMode == FR_ERRORMODE_ACTIVE);
389
390                 if (poc_status.ErrorMode != FR_ERRORMODE_ACTIVE) {
391                         rpp_sci_printf("\nFlexRay exited active mode (see command frbtgetpocst)!\n");
392                         break;
393                 }
394
395         }
396         rpp_sci_printf("\r\nTransmitted %d messages with %d errors.\r\n", msg_cnt, error);
397         if (rpp_fr_halt_communication(0) != SUCCESS)
398                 rpp_sci_printf("FlexRay HALT command failed, please reset the board to stop transmission.\r\n");
399         else
400                 rpp_sci_printf("FlexRay halted, reset the board to make FlexRay usable again.\r\n");
401         return 0;
402 }
403
404 /**
405  *  @brief      Start receiving data on the FlexRay as node B
406  *
407  * @param[in]   cmd_io  Pointer to IO stack
408  * @param[in]   des             Pointer to command descriptor
409  * @param[in]   param   Parameters of command
410  * @return      0 when OK or error code
411  */
412 int cmd_do_test_frayB(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
413 {
414         uint32_t i;
415         uint32_t error = 0;
416         uint32_t msg_cnt = 0;
417         uint32_t count = 100;
418
419         if (param[1] && sscanf(param[1], "%d", &count) != 1)
420                 return -CMDERR_BADPAR;
421
422         if (rpp_fr_init_driver(&Fr_config_node_B, &error) == FAILURE) {
423                 rpp_sci_printf("Fray driver initialization failed: %#x.\n", error);
424                 return -CMDERR_BADCFG;
425         }
426         if (rpp_fr_init_controller(0, &error) == FAILURE) {
427                 rpp_sci_printf("Fray control node initialization failed: %#x.\n", error);
428                 return -CMDERR_BADCFG;
429         }
430         rpp_sci_printf("Fray control node initialized.\r\n");
431         rpp_sci_printf("Waiting for network connection...\r\n");
432
433         if (rpp_fr_start_communication(0, &error) == FAILURE) {
434                 rpp_sci_printf("Integration to the network failed: %#x.\n", error);
435                 return -CMDERR_BADCFG;
436         }
437         if (rpp_fr_all_slots(0) == FAILURE) {
438                 rpp_sci_printf("All slots mode selection failed.\n");
439                 return -CMDERR_BADCFG;
440         }
441
442         rpp_sci_printf("Connected. Frames transmission is starting. O means OK, X means Error, T means timeout.\r\n");
443         for (i = 0; i < count; i++) {
444                 uint8_t data[18];
445                 Fr_RxLPduStatusType status;
446                 uint8_t size;
447                 uint8_t cycle1, cycle2;
448                 uint16_t mtick;
449
450                 rpp_fr_get_global_time(0, &cycle1, &mtick);
451                 do {
452                         if (rpp_fr_receive_lpdu(0, 1, data, &status, &size) == FAILURE) {
453                                 rpp_sci_printf("\nMessage receiving failed!\n");
454                                 break;
455                         }
456                         rpp_fr_get_global_time(0, &cycle2, &mtick);
457                 } while (status == FR_NOT_RECEIVED && (cycle2 != ((cycle1-1) & 0x3F)));
458
459                 if (status == FR_NOT_RECEIVED)
460                         rpp_sci_printf("T");
461                 else if (check_message(data) == 0) {
462                         rpp_sci_printf("O");
463                         msg_cnt++;
464                 }
465                 else {
466                         rpp_sci_printf("X");
467                         error++;
468                         msg_cnt++;
469                 }
470         }
471         rpp_sci_printf("\r\nReceived %d messages with %d errors.\r\n", msg_cnt, error);
472         if (rpp_fr_halt_communication(0) != SUCCESS)
473                 rpp_sci_printf("FlexRay HALT command failed, please reset the board to stop transmission.\r\n");
474         else
475                 rpp_sci_printf("FlexRay halted\r\n");
476         return 0;
477 }
478
479 #endif  /* DOCGEN */
480
481 /** Command descriptor for FlexRay status command */
482 cmd_des_t const cmd_des_fray_stat = {
483         0, 0,
484         "frayxcvrstat#","Get the status of a FlexRay transceiver in a human readable form",
485         "### Command syntax ###\n"
486         "\n"
487         "    frayxcvrstat<CHN>\n"
488         "where CHN is a number in range 1-2\n"
489         "\n"
490         "### Description ###\n"
491         "\n"
492         "The command receives response from a FlexRay transceiver via SPI, and\n"
493         "prints in the form of attribute-value table.\n"
494         "\n"
495         "### Example ###\n"
496         "\n"
497         "    --> frayxcvrstat1\n"
498         "\n"
499         "Prints the status of FRAY1 transceiver.\n",
500         CMD_HANDLER(cmd_do_fray_status), (void *)&cmd_list_fray
501 };
502
503 /** Command descriptor for FlexRay 1 test node A */
504 cmd_des_t const cmd_des_test_fray_a = {
505         0, 0,
506         "fraytestA","Run the FlexRay test as A node",
507         "### Command syntax ###\n"
508         "\n"
509         "    fraytestA [<COUNT>]\n"
510         "\n"
511         "where `<COUNT>` is an optional number of messages to send. The default\n"
512         "is 100.\n"
513         "\n"
514         "### Description ###\n"
515         "\n"
516         "The commands creates FlexRay node A and starts sending a message\n"
517         "approximately every communication cycle. COUNT messages are sent for the\n"
518         "test of the connection. The command should be run with two devices\n"
519         "connected by a FlexRay bus and the second device should be running the\n"
520         "fraytestB command (it is necessary to run both commands shortly after\n"
521         "each other).\n"
522         "\n"
523         "When the command transmits a message a character is printed. O means\n"
524         "that the message was transmitted correctly, X signals a transmission\n"
525         "error. The number of TX errors and successfully transmitted messages\n"
526         "is maintained during the test and printed at the end.\n",
527         CMD_HANDLER(cmd_do_test_frayA), (void *)&cmd_list_fray
528 };
529
530 /** Command descriptor for FlexRay 1 test node B */
531 cmd_des_t const cmd_des_test_fray_b = {
532         0, 0,
533         "fraytestB","Run the FlexRay test as B node",
534         "### Command syntax ###\n"
535         "\n"
536         "    fraytestB [<COUNT>]\n"
537         "\n"
538         "where `<COUNT>` is an optional number of messages to receive. The default\n"
539         "is 100.\n"
540         "\n"
541         "### Description ###\n"
542         "\n"
543         "The commands creates FlexRay node B and starts receiving a messages.\n"
544         "100 messages should be received. The command should be run with two\n"
545         "devices connected by a FlexRay bus and the second device should be\n"
546         "running the fraytestA command (it is necessary to run both commands\n"
547         "shortly after each other).\n"
548         "\n"
549         "When the command receives a message a character is printed. O means\n"
550         "that the message was received correctly, X signals an error in data, T\n"
551         "means timeout (i.e. no message was received within 63 cycles). The\n"
552         "number of RX errors and successfully received messages is maintained\n"
553         "during the test and printed at the end.\n",
554         CMD_HANDLER(cmd_do_test_frayB), (void *)&cmd_list_fray
555 };
556
557 /** List of commands for din, defined as external */
558 cmd_des_t const *cmd_list_fray[] = {
559         &cmd_des_fray_stat,
560         &cmd_des_test_fray_a,
561         &cmd_des_test_fray_b,
562         NULL
563 };