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);
210 /** @brief command descriptor for test CAN loopback command */
211 cmd_des_t const cmd_des_test_can_loopback={
213 "cantest*", "Test CAN loopback between two CAN interfaces",
214 "=== Command syntax ===\n"
216 " cantest<SOURCE> <DESTINATION>\n"
218 "where <SOURCE> and <DESTINATION> are different numbers in range 1-3.\n"
220 "=== Description ===\n"
222 "This command tests CAN communication by sending and receiving messages\n"
223 "through external loopback. At the beginning, the involved CAN\n"
224 "controller are initialized to the Bus-On state.\n"
226 "The command sends 100 messages and measures the numbers of TX errors,\n"
227 "RX errors and detected timeouts. At the end, these statistics are\n"
228 "printed as well as the status of the involved CAN controllers.\n"
230 "When an error is detected during the test, the status of faulty CAN\n"
231 "controller is printed immediately.\n"
236 " Testing CAN loopback\n"
237 " Messages transmitted: 100/100\n"
238 " Messages received: 100/100\n"
241 " Src TX error counter: 0\n"
242 " Src RX error counter: 0\n"
243 " Dst TX error counter: 0\n"
244 " Dst RX error counter: 0\n"
245 " CAN1 status: Bus-On, ES: 0x8\n"
246 " CAN2 status: Bus-On, ES: 0x10\n",
247 CMD_HANDLER(cmd_do_test_can_loopback), (void *)&cmd_list_can
250 cmd_des_t const *cmd_list_can[]={
251 &cmd_des_test_can_loopback,