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 static struct rpp_can_ctrl_config ctrl_config[] = {
225 static struct rpp_can_tx_config tx_config[] = {
227 .type = RPP_CAN_EXTENDED,
232 .type = RPP_CAN_EXTENDED,
237 .type = RPP_CAN_EXTENDED,
243 static struct rpp_can_rx_config rx_config[] = {
245 .type = RPP_CAN_MIXED,
252 .type = RPP_CAN_MIXED,
259 .type = RPP_CAN_MIXED,
267 static struct rpp_can_config can_config = {
270 .tx_config = tx_config,
271 .rx_config = rx_config,
276 int cmd_do_can_init(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
278 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
279 dmmREG->PC5 = (1<<(15)); // clr CAN_EN
280 dmmREG->PC5 = (1<<(13)); // clr CAN_NSTB
281 dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
282 dmmREG->PC4 = (1<<(15)); // set CAN_EN
286 can_inited = (rpp_can_init(&can_config) == SUCCESS ? 1 : 0);
287 return (can_inited ? 0 : 1);
291 int cmd_do_can_send(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
293 uint32_t controller_id;
296 struct rpp_can_pdu pdu;
301 rpp_sci_printf("WARNING: CAN is not initialized\n");
305 ret = sscanf(p, "%i %i%n", &controller_id, &pdu.id, &l);
308 rpp_sci_printf("Cannot parse parameter %d\n", ret+1);
309 return -CMDERR_BADPAR;
315 ret = sscanf(p, "%2hhx%n", &pdu.data[i], &l);
324 if (rpp_can_write(controller_id-1, &pdu) == SUCCESS)
326 rpp_sci_printf("Sent: can%u\t%X\t[%u]\t", controller_id, pdu.id, pdu.dlc);
327 for (i=0; i<pdu.dlc; i++)
329 rpp_sci_printf("%X ", pdu.data[i]);
334 rpp_sci_printf("Error: rpp_can_write");
336 rpp_sci_printf("\n");
343 int cmd_do_can_dump(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
345 uint32_t controller_id = 0;
347 struct rpp_can_pdu pdu;
353 rpp_sci_printf("WARNING: CAN is not initialized\n");
356 if (!(sscanf(param[1], "%u", &controller_id) == 1))
358 rpp_sci_printf("Unable to parse controller ID\n");
362 rpp_can_init(&can_config);
364 while(cmd_io->getc(cmd_io) < 0)
366 rpp_can_check_rx_ind(controller_id-1, &rx_ind);
369 if (rpp_can_read(controller_id-1, &pdu) == SUCCESS)
371 if (pdu.id & CAN_EFF_FLAG)
373 rpp_sci_printf("can%u %08X [%u] ", controller_id & (~CAN_EFF_FLAG), pdu.id, pdu.dlc);
377 rpp_sci_printf("can%u %03X [%u] ", controller_id, pdu.id, pdu.dlc);
380 for (i=0; i<pdu.dlc; i++)
382 rpp_sci_printf("%X ", pdu.data[i]);
384 rpp_sci_printf("\n");
388 rpp_sci_printf("Error rpp_can_read\n");
397 int cmd_do_can_change_baudrate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
400 uint32_t controller_id, baudrate;
402 if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
407 if (!(sscanf(param[1], "%u:%u", &controller_id, &baudrate) == 2))
409 rpp_sci_printf("Unable to parse arguments\n");
413 if (controller_id < 1 || controller_id > 3)
414 return -CMDERR_BADPAR;
416 can_config.ctrl[controller_id-1].baudrate = baudrate;
420 if (!(sscanf(param[1], "%u", &controller_id) == 1))
422 if (controller_id < 1 || controller_id > 3)
423 return -CMDERR_BADPAR;
425 cmd_opchar_replong(cmd_io, param, can_config.ctrl[controller_id-1].baudrate, 0, 10);
435 /** @brief command descriptor for test CAN loopback command */
436 cmd_des_t const cmd_des_test_can_loopback={
438 "cantest*", "Test CAN loopback between two CAN interfaces",
439 "=== Command syntax ===\n"
441 " cantest<SOURCE> <DESTINATION>\n"
443 "where <SOURCE> and <DESTINATION> are different numbers in range 1-3.\n"
445 "=== Description ===\n"
447 "This command tests CAN communication by sending and receiving messages\n"
448 "through external loopback. At the beginning, the involved CAN\n"
449 "controller are initialized to the Bus-On state.\n"
451 "The command sends 100 messages and measures the numbers of TX errors,\n"
452 "RX errors and detected timeouts. At the end, these statistics are\n"
453 "printed as well as the status of the involved CAN controllers.\n"
455 "When an error is detected during the test, the status of faulty CAN\n"
456 "controller is printed immediately.\n"
461 " Testing CAN loopback\n"
462 " Messages transmitted: 100/100\n"
463 " Messages received: 100/100\n"
466 " Src TX error counter: 0\n"
467 " Src RX error counter: 0\n"
468 " Dst TX error counter: 0\n"
469 " Dst RX error counter: 0\n"
470 " CAN1 status: Bus-On, ES: 0x8\n"
471 " CAN2 status: Bus-On, ES: 0x10\n",
472 CMD_HANDLER(cmd_do_test_can_loopback), (void *)&cmd_list_can
476 cmd_des_t const cmd_des_can_init={
478 "caninit", "Initialize CAN controllers",
479 "=== Command syntax ===\n"
483 "=== Description ===\n"
485 "This command (re-)initializes all CAN controllers using current\n"
486 "CAN configuration. This configuration can be changed using\n"
487 "canbaudrate command.\n"
489 "Default configuration sets baudrate of all CAN controllers to 0.5 Mbit/s\n"
493 CMD_HANDLER(cmd_do_can_init), (void *)&cmd_list_can
496 cmd_des_t const cmd_des_can_baudrate={
497 0, CDESM_OPCHR|CDESM_RW,
498 "canbaudrate#", "Change baudrate of CAN controller",
499 "=== Command syntax ===\n"
501 " canbaudrate<CONTROLLER>?\n"
502 " canbaudrate<CONTROLLER>:<BAUDRATE>\n"
504 "where <CONTROLLER> is number in range 1-3 and BAUDRATE is number\n"
505 "in range 1000-10000000.\n"
507 "=== Description ===\n"
509 "This command is used to set or show baudrate of CAN controller.\n"
510 "The baudrate shown is the one which would be set if caninit was\n"
515 " --> canbaudrate2?\n"
516 " canbaudrate2=500000\n"
518 " --> canbaudrate2:100000\n",
519 CMD_HANDLER(cmd_do_can_change_baudrate), (void *)&cmd_list_can
522 cmd_des_t const cmd_des_can_send={
524 "cansend", "Test sending message over CAN",
525 "=== Command syntax ===\n"
527 " cansend <CONTROLLER> <ID> <DATA>\n"
529 "where <CONTROLLER> is number in range 1-3, <ID> is valid CAN ID\n"
530 "and <DATA> is 0-8 bytes of data in hexadecimal representation.\n"
531 "There may be any number of spaces between data bytes.\n"
532 "<ID> may be given in octal, decimal or hexadecimal base.\n"
534 "=== Description ===\n"
536 "This command sends a CAN frame using specified CAN controller.\n"
538 "caninit shall be called before using this command, sending may\n"
539 "behave unexpectedly otherwise.\n"
542 " --> cansend 2 0x123 DEAD BEEF\n"
543 " Sent: can2 123 [4] DE AD BE EF\n",
544 CMD_HANDLER(cmd_do_can_send), (void *)&cmd_list_can
548 cmd_des_t const cmd_des_can_dump={
550 "candump", "Dump all messages received over CAN",
551 "=== Command syntax ===\n"
553 " candump <CONTROLLER>\n"
555 "where <CONTROLLER> is number in range 1-3.\n"
557 "=== Description ===\n"
559 "This command prints out any CAN message received via specified\n"
562 "IDs are zero-filled to length 3 if standard ID is used and to 8\n"
563 "if extended ID is used.\n"
565 "caninit shall be called before using this command, dump may\n"
566 "behave unexpectedly otherwise.\n"
571 "can2 0000FADE [2] 12 34\n",
572 CMD_HANDLER(cmd_do_can_dump), (void *)&cmd_list_can
577 cmd_des_t const *cmd_list_can[]={
578 &cmd_des_test_can_loopback,
580 &cmd_des_can_baudrate,