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("CAN is not initialized\n");
302 return -CMDERR_NODEV;
306 ret = sscanf(p, "%i %i%n", &controller_id, &pdu.id, &l);
309 rpp_sci_printf("Cannot parse parameter %d\n", ret+1);
310 return -CMDERR_BADPAR;
317 ret = sscanf(p, "%2hx%n", &data, &l);
319 pdu.data[i] = data & 0xff;
327 if (rpp_can_write(controller_id-1, &pdu) == SUCCESS)
329 rpp_sci_printf("Sent: can%u\t%X\t[%u]\t", controller_id, pdu.id, pdu.dlc);
330 for (i=0; i<pdu.dlc; i++)
332 rpp_sci_printf("%X ", pdu.data[i]);
337 rpp_sci_printf("Error: rpp_can_write");
339 rpp_sci_printf("\n");
346 int cmd_do_can_dump(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
348 uint32_t controller_id = 0;
350 struct rpp_can_pdu pdu;
356 rpp_sci_printf("CAN is not initialized\n");
357 return -CMDERR_NODEV;
360 if (!(sscanf(param[1], "%u", &controller_id) == 1))
362 rpp_sci_printf("Unable to parse controller ID\n");
366 rpp_can_init(&can_config);
368 while(cmd_io->getc(cmd_io) < 0)
370 rpp_can_check_rx_ind(controller_id-1, &rx_ind);
373 if (rpp_can_read(controller_id-1, &pdu) == SUCCESS)
375 if (pdu.id & CAN_EFF_FLAG)
377 rpp_sci_printf("can%u %08X [%u] ", controller_id & (~CAN_EFF_FLAG), pdu.id, pdu.dlc);
381 rpp_sci_printf("can%u %03X [%u] ", controller_id, pdu.id, pdu.dlc);
384 for (i=0; i<pdu.dlc; i++)
386 rpp_sci_printf("%X ", pdu.data[i]);
388 rpp_sci_printf("\n");
392 rpp_sci_printf("Error rpp_can_read\n");
401 int cmd_do_can_change_baudrate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
404 uint32_t controller_id, baudrate;
406 if ((opchar = cmd_opchar_check(cmd_io, des, param)) < 0)
411 if (!(sscanf(param[1], "%u:%u", &controller_id, &baudrate) == 2))
413 rpp_sci_printf("Unable to parse arguments\n");
417 if (controller_id < 1 || controller_id > 3)
418 return -CMDERR_BADPAR;
420 can_config.ctrl[controller_id-1].baudrate = baudrate;
424 if (!(sscanf(param[1], "%u", &controller_id) == 1))
426 if (controller_id < 1 || controller_id > 3)
427 return -CMDERR_BADPAR;
429 cmd_opchar_replong(cmd_io, param, can_config.ctrl[controller_id-1].baudrate, 0, 10);
439 /** @brief command descriptor for test CAN loopback command */
440 cmd_des_t const cmd_des_test_can_loopback={
442 "cantest*", "Test CAN loopback between two CAN interfaces",
443 "### Command syntax ###\n"
445 " cantest<SRC> <DST>\n"
447 "where `<SRC>` and `<DST>` are different numbers in range 1-3.\n"
449 "### Description ###\n"
451 "This command tests CAN communication by sending and receiving messages\n"
452 "through external loopback. At the beginning, the involved CAN\n"
453 "controller are initialized to the Bus-On state.\n"
455 "The command sends 100 messages and measures the numbers of TX errors,\n"
456 "RX errors and detected timeouts. At the end, these statistics are\n"
457 "printed as well as the status of the involved CAN controllers.\n"
459 "When an error is detected during the test, the status of faulty CAN\n"
460 "controller is printed immediately.\n"
465 " Testing CAN loopback\n"
466 " Messages transmitted: 100/100\n"
467 " Messages received: 100/100\n"
470 " Src TX error counter: 0\n"
471 " Src RX error counter: 0\n"
472 " Dst TX error counter: 0\n"
473 " Dst RX error counter: 0\n"
474 " CAN1 status: Bus-On, ES: 0x8\n"
475 " CAN2 status: Bus-On, ES: 0x10\n",
476 CMD_HANDLER(cmd_do_test_can_loopback), (void *)&cmd_list_can
480 cmd_des_t const cmd_des_can_init={
482 "caninit", "Initialize CAN controllers",
483 "### Command syntax ###\n"
487 "### Description ###\n"
489 "This command (re-)initializes all CAN controllers using current\n"
490 "CAN configuration. This configuration can be changed using\n"
491 "canbaudrate command.\n"
493 "In the default configuration the baudrate of all CAN controllers i set\n"
498 CMD_HANDLER(cmd_do_can_init), (void *)&cmd_list_can
501 cmd_des_t const cmd_des_can_baudrate={
502 0, CDESM_OPCHR|CDESM_RW,
503 "canbaudrate#", "Change baudrate of CAN controller",
504 "### Command syntax ###\n"
506 " canbaudrate<CONTROLLER>?\n"
507 " canbaudrate<CONTROLLER>:<BAUDRATE>\n"
509 "where `<CONTROLLER>` is number in range 1-3 and BAUDRATE is number in\n"
510 "range 1000-10000000 specifying the baurdate in bits per second.\n"
512 "### Description ###\n"
514 "This command is used to set or show the baudrate of a CAN controller.\n"
515 "The baudrate shown is the one which will be used by next invocation of\n"
516 "the caninit command.\n"
520 " --> canbaudrate2?\n"
521 " canbaudrate2=500000\n"
523 " --> canbaudrate2:100000\n",
524 CMD_HANDLER(cmd_do_can_change_baudrate), (void *)&cmd_list_can
527 cmd_des_t const cmd_des_can_send={
529 "cansend", "Test sending message over CAN",
530 "### Command syntax ###\n"
532 " cansend <CONTROLLER> <ID> <DATA>\n"
534 "where `<CONTROLLER>` is number in range 1-3, `<ID>` is a valid CAN ID\n"
535 "and `<DATA>` is 0-8 bytes of data in hexadecimal representation.\n"
536 "There may be any number of spaces between the data bytes.\n"
537 "`<ID>` may be given in octal, decimal or hexadecimal base.\n"
539 "### Description ###\n"
541 "This command sends a CAN frame using specified CAN controller.\n"
543 "The caninit command must be called before using this command.\n"
546 " --> cansend 2 0x123 DEAD BEEF\n"
547 " Sent: can2 123 [4] DE AD BE EF\n",
548 CMD_HANDLER(cmd_do_can_send), (void *)&cmd_list_can
552 cmd_des_t const cmd_des_can_dump={
554 "candump", "Dump all messages received over CAN",
555 "### Command syntax ###\n"
557 " candump <CONTROLLER>\n"
559 "where `<CONTROLLER>` is a number in range 1-3.\n"
561 "### Description ###\n"
563 "This command prints out all CAN messages received via the specified\n"
566 "IDs are zero-filled to length 3 if a message in the standard frame\n"
567 "format is received and to 8 for extended frame format messages.\n"
569 "caninit must be called before using this command.\n"
574 "can2 0000FADE [2] 12 34\n",
575 CMD_HANDLER(cmd_do_can_dump), (void *)&cmd_list_can
580 cmd_des_t const *cmd_list_can[]={
581 &cmd_des_test_can_loopback,
583 &cmd_des_can_baudrate,