2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * This file contains commands for CAN test
37 /** Semaphore used for blocking task until message is received */
38 extern xSemaphoreHandle canMsgReceived;
39 /** Semaphore used for blocking task until message is sent */
40 extern xSemaphoreHandle canMsgSent;
41 /** Pointer to Destination CAN registers */
42 extern canBASE_t* canDst;
43 /** Pointer to Source CAN registers */
44 extern canBASE_t* canSrc;
45 /** Can message box for received messages */
46 extern uint32_t canMsgBox;
47 /** Error counter for errors in sending */
48 extern uint32_t canSendError;
49 /** Error counter for errors in receiving */
50 extern uint32_t canRecError;
52 static void canPrintStatus(int can_num, uint32_t es)
54 uint32_t errorLevel = es & 0xE0U;
58 case canLEVEL_BUS_OFF:
61 case canLEVEL_PASSIVE:
62 levText = "Error Passive";
64 case canLEVEL_WARNING:
74 rpp_sci_printf("CAN%d status: %s, ES: %#x\n", can_num, levText, es);
77 uint32_t canCheckForError(int can_num, uint32_t es)
79 uint32_t errorLevel = es & 0xE0U;
81 if (errorLevel != 0) {
82 canPrintStatus(can_num, es);
89 * @brief Command for external CAN loopback testing.
91 * @param[in] cmd_io Pointer to IO stack
92 * @param[in] des Pointer to command descriptor
93 * @param[in] param Parameters of command
94 * @return 0 when OK or error code
96 int cmd_do_test_can_loopback(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
98 uint32_t can_src, can_dst;
99 canBASE_t *canBases[3] = {canREG1, canREG2, canREG3};
100 uint32_t canMailBoxes[3] = {canMESSAGE_BOX1, canMESSAGE_BOX2, canMESSAGE_BOX3};
101 uint32_t txTimeOutCnt = 0;
102 uint32_t rxTimeOutCnt = 0;
103 uint32_t messagesTransmitted = 0;
104 uint32_t messagesReceived = 0;
106 uint8_t tx_data[8] = {'T', 'E', 'S', 'T', ' ', 'C', 'A', 'N'};
107 uint8_t rx_data[8] = {0, 0, 0, 0, 0, 0, 0, 0 };
112 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
113 vTaskDelay(50/portTICK_RATE_MS);
114 dmmREG->PC5 = (1<<(15)); // clr CAN_EN
115 vTaskDelay(50/portTICK_RATE_MS);
116 dmmREG->PC5 = (1<<(13)); // clr CAN_NSTB
117 vTaskDelay(50/portTICK_RATE_MS);
118 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
119 vTaskDelay(50/portTICK_RATE_MS);
120 dmmREG->PC4 = (1<<(15)); // set CAN_EN
121 vTaskDelay(50/portTICK_RATE_MS);
124 if (sscanf(p, "%d %d %1s", &can_src, &can_dst, &spareParams) != 2) {
125 return -CMDERR_BADPAR;
128 if (can_src == can_dst) {
129 rpp_sci_printf("ERROR: Destination equals source!\n");
132 if (can_src < 1 | can_dst > 3) {
133 rpp_sci_printf("Parameter out of range <1;3>!\n");
136 canSrc = canBases[can_src-1];
137 canDst = canBases[can_dst-1];
138 canMsgBox = canMailBoxes[can_src-1];
139 canMsgReceived = xSemaphoreCreateCounting(1, 0);
140 canMsgSent = xSemaphoreCreateCounting(1, 0);
142 rpp_sci_printf("Testing CAN loopback\r\n");
143 canREG1->CTL |= 1|(1<<9);
144 canREG2->CTL |= 1|(1<<9);
145 canREG3->CTL |= 1|(1<<9);
147 canDst->CTL |= 1<<15; // Reset
148 while (canDst->CTL & (1<<15)) ; // Wait for reset
150 canSrc->CTL |= 1<<15;
151 while (canSrc->CTL & (1<<15)) ;
155 vTaskDelay(50/portTICK_RATE_MS);
157 i = canSrc->ES; // Reset error flag
158 i = canDst->ES; // Reset error flag
159 canEnableErrorNotification(canDst);
160 canEnableErrorNotification(canSrc);
162 for (i = 0; i < 100; i++) { // Send 100 times the message
163 if (canCheckForError(can_src, canSendError) != canLEVEL_ACTIVE) {
166 canTransmit(canSrc, canMsgBox, tx_data);
167 if (xSemaphoreTake(canMsgSent, 100/portTICK_RATE_MS) == pdFALSE) {
171 if (canCheckForError(can_src, canSendError) & canLEVEL_BUS_OFF) {
175 messagesTransmitted++;
176 if (xSemaphoreTake(canMsgReceived, 100/portTICK_RATE_MS) == pdFALSE) {
180 if (canGetData(canDst, canMsgBox, rx_data))
183 if (canCheckForError(can_dst, canRecError) & canLEVEL_BUS_OFF) {
190 rpp_sci_printf("Messages transmitted: %d/100\r\n", messagesTransmitted);
191 rpp_sci_printf("Messages received: %d/100\r\n", messagesReceived);
192 rpp_sci_printf("TX timeouts: %d\r\n", txTimeOutCnt);
193 rpp_sci_printf("RX timeouts: %d\r\n", rxTimeOutCnt);
194 rpp_sci_printf("Src TX error counter: %d\r\n", canSrc->EERC & 0xFFU);
195 rpp_sci_printf("Src RX error counter: %d\r\n", (canSrc->EERC & 0xFF00U) >> 8);
196 rpp_sci_printf("Dst TX error counter: %d\r\n", canDst->EERC & 0xFFU);
197 rpp_sci_printf("Dst RX error counter: %d\r\n", (canDst->EERC & 0xFF00U) >> 8);
198 canPrintStatus(can_src, canSendError);
199 canPrintStatus(can_dst, canRecError);
201 canDisableErrorNotification(canDst);
202 canDisableErrorNotification(canSrc);
203 vSemaphoreDelete(canMsgReceived);
204 vSemaphoreDelete(canMsgSent);
211 static int can_inited = 0;
213 /* Default CAN timing set to 500kb */
214 static struct rpp_can_timing_cfg can_timing[] = {
239 static struct rpp_can_ctrl_config ctrl_config[] = {
243 .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
244 .timing_config = NULL
249 .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
250 .timing_config = NULL
255 .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
256 .timing_config = NULL
260 static struct rpp_can_tx_config tx_config[] = {
262 .type = RPP_CAN_EXTENDED,
267 .type = RPP_CAN_EXTENDED,
272 .type = RPP_CAN_EXTENDED,
278 static struct rpp_can_rx_config rx_config[] = {
280 .type = RPP_CAN_MIXED,
287 .type = RPP_CAN_MIXED,
294 .type = RPP_CAN_MIXED,
302 static struct rpp_can_config can_config = {
305 .tx_config = tx_config,
306 .rx_config = rx_config,
311 int cmd_do_can_init(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
313 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
314 dmmREG->PC5 = (1<<(15)); // clr CAN_EN
315 dmmREG->PC5 = (1<<(13)); // clr CAN_NSTB
316 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
317 dmmREG->PC4 = (1<<(15)); // set CAN_EN
321 can_inited = (rpp_can_init(&can_config) == SUCCESS ? 1 : 0);
322 return (can_inited ? 0 : 1);
326 int cmd_do_can_send(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
328 uint32_t controller_id;
331 struct rpp_can_pdu pdu;
336 rpp_sci_printf("CAN is not initialized\n");
337 return -CMDERR_NODEV;
341 ret = sscanf(p, "%i %i%n", &controller_id, &pdu.id, &l);
344 rpp_sci_printf("Cannot parse parameter %d\n", ret+1);
345 return -CMDERR_BADPAR;
352 ret = sscanf(p, "%2hx%n", &data, &l);
354 pdu.data[i] = data & 0xff;
362 if (rpp_can_write(controller_id-1, &pdu) == SUCCESS)
364 rpp_sci_printf("Sent: can%u\t%X\t[%u]\t", controller_id, pdu.id, pdu.dlc);
365 for (i=0; i<pdu.dlc; i++)
367 rpp_sci_printf("%X ", pdu.data[i]);
372 rpp_sci_printf("Error: rpp_can_write");
374 rpp_sci_printf("\n");
381 int cmd_do_can_dump(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
383 uint32_t controller_id = 0;
385 struct rpp_can_pdu pdu;
391 rpp_sci_printf("CAN is not initialized\n");
392 return -CMDERR_NODEV;
395 if (!(sscanf(param[1], "%u", &controller_id) == 1))
397 rpp_sci_printf("Unable to parse controller ID\n");
401 rpp_can_init(&can_config);
403 while(cmd_io->getc(cmd_io) < 0)
405 rpp_can_check_rx_ind(controller_id-1, &rx_ind);
408 if (rpp_can_read(controller_id-1, &pdu) == SUCCESS)
410 if (pdu.id & CAN_EFF_FLAG)
412 rpp_sci_printf("can%u %08X [%u] ", controller_id & (~CAN_EFF_FLAG), pdu.id, pdu.dlc);
416 rpp_sci_printf("can%u %03X [%u] ", controller_id, pdu.id, pdu.dlc);
419 for (i=0; i<pdu.dlc; i++)
421 rpp_sci_printf("%X ", pdu.data[i]);
423 rpp_sci_printf("\n");
427 rpp_sci_printf("Error rpp_can_read\n");
436 int cmd_do_can_change_baudrate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
439 uint32_t controller_id, baudrate;
441 if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
446 if (!(sscanf(param[1], "%u:%u", &controller_id, &baudrate) == 2))
448 rpp_sci_printf("Unable to parse arguments\n");
452 if (controller_id < 1 || controller_id > 3)
453 return -CMDERR_BADPAR;
455 can_config.ctrl[controller_id-1].baudrate = baudrate;
456 can_config.ctrl[controller_id-1].timing_config = NULL;
457 can_config.ctrl[controller_id-1].timing_calc_method = RPP_CAN_TIMING_CALC_AUTO;
461 if (!(sscanf(param[1], "%u", &controller_id) == 1))
463 if (controller_id < 1 || controller_id > 3)
464 return -CMDERR_BADPAR;
466 cmd_opchar_replong(cmd_io, param, can_config.ctrl[controller_id-1].baudrate, 0, 10);
472 int cmd_do_can_change_timing(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
475 uint32_t controller_id, brp, prop_seg, phase_seg1, phase_seg2, sjw;
477 if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
482 if (!(sscanf(param[1], "%u:%u %u %u %u %u", &controller_id, &brp, &prop_seg, &phase_seg1, &phase_seg2, &sjw) == 6))
484 rpp_sci_printf("Unable to parse arguments\n");
488 if (controller_id < 1 || controller_id > 3)
489 return -CMDERR_BADPAR;
491 can_config.ctrl[controller_id-1].baudrate = 0;
492 can_config.ctrl[controller_id-1].timing_config = &can_timing[controller_id-1];
493 can_config.ctrl[controller_id-1].timing_config->brp = brp;
494 can_config.ctrl[controller_id-1].timing_config->phase_seg1 = phase_seg1;
495 can_config.ctrl[controller_id-1].timing_config->phase_seg2 = phase_seg2;
496 can_config.ctrl[controller_id-1].timing_config->prop_seg = prop_seg;
497 can_config.ctrl[controller_id-1].timing_config->sjw = sjw;
498 can_config.ctrl[controller_id-1].timing_calc_method = RPP_CAN_TIMING_CALC_MANUAL;
502 if (!(sscanf(param[1], "%u", &controller_id) == 1))
504 if (controller_id < 1 || controller_id > 3)
505 return -CMDERR_BADPAR;
507 if (can_config.ctrl[controller_id-1].timing_config != NULL) {
508 rpp_sci_printf("brp: %u\r\nprop_seg: %u tQ\r\nphase_seg1: %u tQ\r\nphase_seg2: %u tQ\r\nsjw: %u tQ\r\n",
509 can_config.ctrl[controller_id-1].timing_config->brp,
510 can_config.ctrl[controller_id-1].timing_config->prop_seg,
511 can_config.ctrl[controller_id-1].timing_config->phase_seg1,
512 can_config.ctrl[controller_id-1].timing_config->phase_seg2,
513 can_config.ctrl[controller_id-1].timing_config->sjw
518 rpp_sci_printf("CAN timing has not yet been manually specified.\r\n");
530 /** @brief command descriptor for test CAN loopback command */
531 cmd_des_t const cmd_des_test_can_loopback={
533 "cantest*", "Test CAN loopback between two CAN interfaces",
534 "### Command syntax ###\n"
536 " cantest<SRC> <DST>\n"
538 "where `<SRC>` and `<DST>` are different numbers in range 1-3.\n"
540 "### Description ###\n"
542 "This command tests CAN communication by sending and receiving messages\n"
543 "through external loopback. At the beginning, the involved CAN\n"
544 "controller are initialized to the Bus-On state.\n"
546 "The command sends 100 messages and measures the numbers of TX errors,\n"
547 "RX errors and detected timeouts. At the end, these statistics are\n"
548 "printed as well as the status of the involved CAN controllers.\n"
550 "When an error is detected during the test, the status of faulty CAN\n"
551 "controller is printed immediately.\n"
556 " Testing CAN loopback\n"
557 " Messages transmitted: 100/100\n"
558 " Messages received: 100/100\n"
561 " Src TX error counter: 0\n"
562 " Src RX error counter: 0\n"
563 " Dst TX error counter: 0\n"
564 " Dst RX error counter: 0\n"
565 " CAN1 status: Bus-On, ES: 0x8\n"
566 " CAN2 status: Bus-On, ES: 0x10\n",
567 CMD_HANDLER(cmd_do_test_can_loopback), (void *)&cmd_list_can
571 cmd_des_t const cmd_des_can_init={
573 "caninit", "Initialize CAN controllers",
574 "### Command syntax ###\n"
578 "### Description ###\n"
580 "This command (re-)initializes all CAN controllers using current\n"
581 "CAN configuration. This configuration can be changed using\n"
582 "canbaudrate command.\n"
584 "In the default configuration the baudrate of all CAN controllers i set\n"
589 CMD_HANDLER(cmd_do_can_init), (void *)&cmd_list_can
592 cmd_des_t const cmd_des_can_baudrate={
593 0, CDESM_OPCHR|CDESM_RW,
594 "canbaudrate#", "Change baudrate of CAN controller",
595 "### Command syntax ###\n"
597 " canbaudrate<CONTROLLER>?\n"
598 " canbaudrate<CONTROLLER>:<BAUDRATE>\n"
600 "where `<CONTROLLER>` is number in range 1-3 and BAUDRATE is number in\n"
601 "range 1000-10000000 specifying the baudrate in bits per second.\n"
603 "### Description ###\n"
605 "This command is used to set or show the baudrate of a CAN controller.\n"
606 "The baudrate shown is the one which will be used by next invocation of\n"
607 "the caninit command.\n"
608 "The baudrate specified by the command is used to automatic calculation of\n"
609 "the CAN controller timing. If you want to specify the timing manually,\n"
610 "please use the command cantiming.\n"
611 "The automatic calculation might not work properly for some baudrates. In\n"
612 "this case you should calculate the timing manually and specify it by the\n"
617 " --> canbaudrate2?\n"
618 " canbaudrate2=500000\n"
620 " --> canbaudrate2:100000\n",
621 CMD_HANDLER(cmd_do_can_change_baudrate), (void *)&cmd_list_can
624 cmd_des_t const cmd_des_can_timing={
625 0, CDESM_OPCHR|CDESM_RW,
626 "cantiming#", "Change timing of CAN controller manually",
627 "### Command syntax ###\n"
629 " cantiming<CONTROLLER>?\n"
630 " cantiming<CONTROLLER>:<BRP> <PROP_SEG> <PHASE_SEG1> <PHASE_SEG2> <SJW>\n"
633 " - `<CONTROLLER>` is number in range 1-3\n"
634 " - `<BRP>` (baudrate prescaler) is number in range 1-65\n"
635 " - `<PROP_SEG>` (length of propagation segment in tQ) is a number in range 1-8\n"
636 " - `<PHASE_SEG1>` (phase buffer segment 1 in tQ) is a number in range 1-8\n"
637 " - `<PHASE_SEG2>` (phase buffer segment 2 in tQ) is a number in range 1-8\n"
638 " - `<SJW>` (synchronization jump width in tQ) is a number in range 1-4\n"
640 "### Description ###\n"
642 "This command is used to set or show the timing of a CAN controller.\n"
643 "The timing shown is the one which will be used by next invocation of\n"
644 "the caninit command.\n"
645 "The timing configured by this command defines manually the baudrate of\n"
646 "the CAN controller. If you want to calculate the timing automaticaly from\n"
647 "a baudrate, please use the command canbaudrate.\n"
654 " phase_seg1: 5 tQ\n"
655 " phase_seg2: 5 tQ\n"
657 " sample_pt: 875 ns\n"
661 " --> cantiming2:5 8 7 4 1\n",
662 CMD_HANDLER(cmd_do_can_change_timing), (void *)&cmd_list_can
666 cmd_des_t const cmd_des_can_send={
668 "cansend", "Test sending message over CAN",
669 "### Command syntax ###\n"
671 " cansend <CONTROLLER> <ID> <DATA>\n"
673 "where `<CONTROLLER>` is number in range 1-3, `<ID>` is a valid CAN ID\n"
674 "and `<DATA>` is 0-8 bytes of data in hexadecimal representation.\n"
675 "There may be any number of spaces between the data bytes.\n"
676 "`<ID>` may be given in octal, decimal or hexadecimal base.\n"
678 "### Description ###\n"
680 "This command sends a CAN frame using specified CAN controller.\n"
682 "The caninit command must be called before using this command.\n"
685 " --> cansend 2 0x123 DEAD BEEF\n"
686 " Sent: can2 123 [4] DE AD BE EF\n",
687 CMD_HANDLER(cmd_do_can_send), (void *)&cmd_list_can
691 cmd_des_t const cmd_des_can_dump={
693 "candump", "Dump all messages received over CAN",
694 "### Command syntax ###\n"
696 " candump <CONTROLLER>\n"
698 "where `<CONTROLLER>` is a number in range 1-3.\n"
700 "### Description ###\n"
702 "This command prints out all CAN messages received via the specified\n"
705 "IDs are zero-filled to length 3 if a message in the standard frame\n"
706 "format is received and to 8 for extended frame format messages.\n"
708 "caninit must be called before using this command.\n"
713 "can2 0000FADE [2] 12 34\n",
714 CMD_HANDLER(cmd_do_can_dump), (void *)&cmd_list_can
719 cmd_des_t const *cmd_list_can[]={
720 &cmd_des_test_can_loopback,
722 &cmd_des_can_baudrate,