2 * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
4 * Created on: 28.2.2013
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.
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
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.
34 #include "drv/port_def.h"
36 #include "cmdproc_utils.h"
38 #include "spi_resp_transl.h"
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.
45 static Fr_TMS570LS_ClusterConfigType Fr_cluster_config = {
46 .gColdStartAttempts = 0x2,
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,
57 .gdActionPointOffset = 0x4,
58 .gdCASRxLowMax = 0x43,
59 .gdDynamicSlotIdlePhase = 0x1,
61 .gdMinislotActionPointOffset = 0x2,
63 .gdSampleClockPeriod = 0, // 10mbit/sec
65 .gdTSSTransmitter = 0xA,
66 .gdWakeupSymbolRxIdle = 18,
67 .gdWakeupSymbolRxLow = 18,
68 .gdWakeupSymbolRxWindow = 76,
69 .gdWakeupSymbolTxIdle = 180,
70 .gdWakeupSymbolTxLow = 60
75 * This structure contains local configuration of the FlexRay node A.
76 * That is the node that transmits messages to the node B.
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,
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,
101 .pdAcceptedStartupRange = 0x81,
102 .pdListenTimeout = 0x36DA2,
104 .pDecodingCorrection = 0x33
108 * This structure contains local configuration of the FlexRay node B.
109 * That is the node that receives messages from the node A.
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,
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,
134 .pdAcceptedStartupRange = 0x81,
135 .pdListenTimeout = 0x36DA2,
137 .pDecodingCorrection = 0x33
141 * FlexRay node A message RAM configuration.
142 * Reconfiguration is disabled, 2 static buffers are configured.
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
153 * FlexRay node B message RAM configuration.
154 * Reconfiguration is disabled, 2 static buffers are configured.
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
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.
172 static Fr_TMS570LS_BufferConfigType Fr_node_A_static_buffers_config[] = {
174 .channel = FR_CHANNEL_AB,
175 .cycleCounterFiltering = 0,
179 .msgBufferInterrupt = TRUE,
180 .payloadPreambleIndicatorTr = FALSE,
181 .rejectNullFrames = FALSE,
182 .rejectStaticSegment = FALSE,
183 .singleTransmit = FALSE,
187 .channel = FR_CHANNEL_AB,
188 .cycleCounterFiltering = 0,
192 .msgBufferInterrupt = TRUE,
193 .payloadPreambleIndicatorTr = FALSE,
194 .rejectNullFrames = FALSE,
195 .rejectStaticSegment = FALSE,
196 .singleTransmit = TRUE,
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.
208 static Fr_TMS570LS_BufferConfigType Fr_node_B_static_buffers_config[] = {
210 .channel = FR_CHANNEL_AB,
211 .cycleCounterFiltering = 0,
215 .msgBufferInterrupt = TRUE,
216 .payloadPreambleIndicatorTr = FALSE,
217 .rejectNullFrames = FALSE,
218 .rejectStaticSegment = FALSE,
219 .singleTransmit = FALSE,
223 .channel = FR_CHANNEL_AB,
224 .cycleCounterFiltering = 0,
228 .msgBufferInterrupt = TRUE,
229 .payloadPreambleIndicatorTr = FALSE,
230 .rejectNullFrames = FALSE,
231 .rejectStaticSegment = FALSE,
232 .singleTransmit = TRUE,
238 * Unifying configuration structure for the node A.
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
250 * Unifying configuration structure for the node B.
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
262 * Loads data into TX buffer for FlexRay node A.
263 * Set TX Request for the buffer to start the transmission.
265 int8_t transmit_node_a()
270 // Write payload for the buffer
271 for (i = 0; i < 18; i++) {
274 return rpp_fr_transmit_lpdu(0, 1, data, 18);
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
282 int check_message(const uint8_t *data)
286 for (i = 0; i < 18; i++) {
295 * @brief Get FlexRay status in human readable form
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.
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
305 int cmd_do_fray_status(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
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;
316 if (Fr_spi_transfer(pin) != 0) return -CMDERR_BADDIO;
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);
325 spitr_print_resp(cmd_desc, lsbResponse);
330 * @brief Start sending data on the FlexRay as node A
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
337 int cmd_do_test_frayA(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
341 uint32_t msg_cnt = 0;
342 uint32_t count = 100;
343 Fr_TxLPduStatusType status;
344 Fr_POCStatusType poc_status;
346 if (param[1] && sscanf(param[1], "%d", &count) != 1)
347 return -CMDERR_BADPAR;
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;
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;
357 rpp_sci_printf("Fray control node initialized.\r\n");
358 rpp_sci_printf("Waiting for network connection...\r\n");
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;
364 if (rpp_fr_all_slots(0) == FAILURE) {
365 rpp_sci_printf("All slots mode selection failed.\n");
366 return -CMDERR_BADCFG;
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
373 rpp_sci_printf("Absolute timer setting failed.\n");
374 return -CMDERR_BADCFG;
376 for (i = 0; i < count; i++) {
377 if (transmit_node_a() == FAILURE) {
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);
390 if (poc_status.ErrorMode != FR_ERRORMODE_ACTIVE) {
391 rpp_sci_printf("\nFlexRay exited active mode (see command frbtgetpocst)!\n");
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");
400 rpp_sci_printf("FlexRay halted, reset the board to make FlexRay usable again.\r\n");
405 * @brief Start receiving data on the FlexRay as node B
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
412 int cmd_do_test_frayB(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
416 uint32_t msg_cnt = 0;
417 uint32_t count = 100;
419 if (param[1] && sscanf(param[1], "%d", &count) != 1)
420 return -CMDERR_BADPAR;
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;
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;
430 rpp_sci_printf("Fray control node initialized.\r\n");
431 rpp_sci_printf("Waiting for network connection...\r\n");
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;
437 if (rpp_fr_all_slots(0) == FAILURE) {
438 rpp_sci_printf("All slots mode selection failed.\n");
439 return -CMDERR_BADCFG;
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++) {
445 Fr_RxLPduStatusType status;
447 uint8_t cycle1, cycle2;
450 rpp_fr_get_global_time(0, &cycle1, &mtick);
452 if (rpp_fr_receive_lpdu(0, 1, data, &status, &size) == FAILURE) {
453 rpp_sci_printf("\nMessage receiving failed!\n");
456 rpp_fr_get_global_time(0, &cycle2, &mtick);
457 } while (status == FR_NOT_RECEIVED && (cycle2 != ((cycle1-1) & 0x3F)));
459 if (status == FR_NOT_RECEIVED)
461 else if (check_message(data) == 0) {
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");
475 rpp_sci_printf("FlexRay halted\r\n");
481 /** Command descriptor for FlexRay status command */
482 cmd_des_t const cmd_des_fray_stat = {
484 "frayxcvrstat#","Get the status of a FlexRay transceiver in a human readable form",
485 "### Command syntax ###\n"
487 " frayxcvrstat<CHN>\n"
488 "where CHN is a number in range 1-2\n"
490 "### Description ###\n"
492 "The command receives response from a FlexRay transceiver via SPI, and\n"
493 "prints in the form of attribute-value table.\n"
497 " --> frayxcvrstat1\n"
499 "Prints the status of FRAY1 transceiver.\n",
500 CMD_HANDLER(cmd_do_fray_status), (void *)&cmd_list_fray
503 /** Command descriptor for FlexRay 1 test node A */
504 cmd_des_t const cmd_des_test_fray_a = {
506 "fraytestA","Run the FlexRay test as A node",
507 "### Command syntax ###\n"
509 " fraytestA [<COUNT>]\n"
511 "where `<COUNT>` is an optional number of messages to send. The default\n"
514 "### Description ###\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"
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
530 /** Command descriptor for FlexRay 1 test node B */
531 cmd_des_t const cmd_des_test_fray_b = {
533 "fraytestB","Run the FlexRay test as B node",
534 "### Command syntax ###\n"
536 " fraytestB [<COUNT>]\n"
538 "where `<COUNT>` is an optional number of messages to receive. The default\n"
541 "### Description ###\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"
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
557 /** List of commands for din, defined as external */
558 cmd_des_t const *cmd_list_fray[] = {
560 &cmd_des_test_fray_a,
561 &cmd_des_test_fray_b,