#define CCT_TIMEOUT 100000 /**< Timeout for RPP CAN test steps. */
-/** Semaphore used for blocking task until message is received */
-extern xSemaphoreHandle canMsgReceived;
-/** Semaphore used for blocking task until message is sent */
-extern xSemaphoreHandle canMsgSent;
-/** Pointer to Destination CAN registers */
-extern canBASE_t* canDst;
-/** Pointer to Source CAN registers */
-extern canBASE_t* canSrc;
-/** Can message box for received messages */
-extern uint32_t canMsgBox;
-/** Error counter for errors in sending */
-extern uint32_t canSendError;
-/** Error counter for errors in receiving */
-extern uint32_t canRecError;
-
-static void canPrintStatus(int can_num, uint32_t es)
-{
- uint32_t errorLevel = es & 0xE0U;
- char* levText;
-
- switch (errorLevel) {
- case canLEVEL_BUS_OFF:
- levText = "Bus-Off";
- break;
- case canLEVEL_PASSIVE:
- levText = "Error Passive";
- break;
- case canLEVEL_WARNING:
- levText = "Warning";
- break;
- case canLEVEL_ACTIVE:
- levText = "Bus-On";
- break;
- default:
- levText = "Unknown";
- }
-
- rpp_sci_printf("CAN%d status: %s, ES: %#x\n", can_num, levText, es);
-}
-
-uint32_t canCheckForError(int can_num, uint32_t es)
-{
- uint32_t errorLevel = es & 0xE0U;
-
- if (errorLevel != 0) {
- canPrintStatus(can_num, es);
- }
-
- return errorLevel;
-}
-
-/**
- * @brief Command for external CAN loopback testing.
- *
- * @param[in] cmd_io Pointer to IO stack
- * @param[in] des Pointer to command descriptor
- * @param[in] param Parameters of command
- * @return 0 when OK or error code
- */
-int cmd_do_test_can_loopback(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
-{
- uint32_t can_src, can_dst;
- canBASE_t *canBases[3] = {canREG1, canREG2, canREG3};
- uint32_t canMailBoxes[3] = {canMESSAGE_BOX1, canMESSAGE_BOX2, canMESSAGE_BOX3};
- uint32_t txTimeOutCnt = 0;
- uint32_t rxTimeOutCnt = 0;
- uint32_t messagesTransmitted = 0;
- uint32_t messagesReceived = 0;
- uint32_t i;
- uint8_t tx_data[8] = {'T', 'E', 'S', 'T', ' ', 'C', 'A', 'N'};
- uint8_t rx_data[8] = {0, 0, 0, 0, 0, 0, 0, 0 };
- char *p=param[1];
- char spareParams;
-
-
- dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
- vTaskDelay(50/portTICK_RATE_MS);
- dmmREG->PC5 = (1<<(15)); // clr CAN_EN
- vTaskDelay(50/portTICK_RATE_MS);
- dmmREG->PC5 = (1<<(13)); // clr CAN_NSTB
- vTaskDelay(50/portTICK_RATE_MS);
- dmmREG->PC4 = (1<<(13)); // set CAN_NSTB
- vTaskDelay(50/portTICK_RATE_MS);
- dmmREG->PC4 = (1<<(15)); // set CAN_EN
- vTaskDelay(50/portTICK_RATE_MS);
-
-
- if (sscanf(p, "%d %d %1s", &can_src, &can_dst, &spareParams) != 2) {
- return -CMDERR_BADPAR;
- }
-
- if (can_src == can_dst) {
- rpp_sci_printf("ERROR: Destination equals source!\n");
- return 1;
- }
- if (can_src < 1 | can_dst > 3) {
- rpp_sci_printf("Parameter out of range <1;3>!\n");
- return 1;
- }
- canSrc = canBases[can_src-1];
- canDst = canBases[can_dst-1];
- canMsgBox = canMailBoxes[can_src-1];
- canMsgReceived = xSemaphoreCreateCounting(1, 0);
- canMsgSent = xSemaphoreCreateCounting(1, 0);
-
- rpp_sci_printf("Testing CAN loopback\r\n");
- canREG1->CTL |= 1|(1<<9);
- canREG2->CTL |= 1|(1<<9);
- canREG3->CTL |= 1|(1<<9);
- canRecError = 0;
- canDst->CTL |= 1<<15; // Reset
- while (canDst->CTL & (1<<15)) ; // Wait for reset
- canSendError = 0;
- canSrc->CTL |= 1<<15;
- while (canSrc->CTL & (1<<15)) ;
- canInit();
- canDst->CTL |= 1<<9;
- canSrc->CTL |= 1<<9;
- vTaskDelay(50/portTICK_RATE_MS);
-
- i = canSrc->ES; // Reset error flag
- i = canDst->ES; // Reset error flag
- canEnableErrorNotification(canDst);
- canEnableErrorNotification(canSrc);
-
- for (i = 0; i < 100; i++) { // Send 100 times the message
- if (canCheckForError(can_src, canSendError) != canLEVEL_ACTIVE) {
- break;
- }
- canTransmit(canSrc, canMsgBox, tx_data);
- if (xSemaphoreTake(canMsgSent, 100/portTICK_RATE_MS) == pdFALSE) {
- txTimeOutCnt++;
- }
- else {
- if (canCheckForError(can_src, canSendError) & canLEVEL_BUS_OFF) {
- break;
- }
- else {
- messagesTransmitted++;
- if (xSemaphoreTake(canMsgReceived, 100/portTICK_RATE_MS) == pdFALSE) {
- rxTimeOutCnt++;
- }
- else {
- if (canGetData(canDst, canMsgBox, rx_data))
- messagesReceived++;
-
- if (canCheckForError(can_dst, canRecError) & canLEVEL_BUS_OFF) {
- break;
- }
- }
- }
- }
-
- rpp_sci_printf("Messages transmitted: %d/100\r\n", messagesTransmitted);
- rpp_sci_printf("Messages received: %d/100\r\n", messagesReceived);
- rpp_sci_printf("TX timeouts: %d\r\n", txTimeOutCnt);
- rpp_sci_printf("RX timeouts: %d\r\n", rxTimeOutCnt);
- rpp_sci_printf("Src TX error counter: %d\r\n", canSrc->EERC & 0xFFU);
- rpp_sci_printf("Src RX error counter: %d\r\n", (canSrc->EERC & 0xFF00U) >> 8);
- rpp_sci_printf("Dst TX error counter: %d\r\n", canDst->EERC & 0xFFU);
- rpp_sci_printf("Dst RX error counter: %d\r\n", (canDst->EERC & 0xFF00U) >> 8);
- canPrintStatus(can_src, canSendError);
- canPrintStatus(can_dst, canRecError);
-
- canDisableErrorNotification(canDst);
- canDisableErrorNotification(canSrc);
- vSemaphoreDelete(canMsgReceived);
- vSemaphoreDelete(canMsgSent);
- return 0;
- }
- return 0;
-}
-
-
static int can_inited = 0;
/* Default CAN timing set to 500kb */
#endif /* DOCGEN */
-/** @brief command descriptor for test CAN loopback command */
-cmd_des_t const cmd_des_test_can_loopback={
- 0, 0,
- "cantest*", "Test CAN loopback between two CAN interfaces",
- "### Command syntax ###\n"
- "\n"
- " cantest<SRC> <DST>\n"
- "\n"
- "where `<SRC>` and `<DST>` are different numbers in range 1-3.\n"
- "\n"
- "### Description ###\n"
- "\n"
- "This command tests CAN communication by sending and receiving messages\n"
- "through external loopback. At the beginning, the involved CAN\n"
- "controller are initialized to the Bus-On state.\n"
- "\n"
- "The command sends 100 messages and measures the numbers of TX errors,\n"
- "RX errors and detected timeouts. At the end, these statistics are\n"
- "printed as well as the status of the involved CAN controllers.\n"
- "\n"
- "When an error is detected during the test, the status of faulty CAN\n"
- "controller is printed immediately.\n"
- "\n"
- "### Example ###\n"
- "\n"
- " --> cantest1 2\n"
- " Testing CAN loopback\n"
- " Messages transmitted: 100/100\n"
- " Messages received: 100/100\n"
- " TX timeouts: 0\n"
- " RX timeouts: 0\n"
- " Src TX error counter: 0\n"
- " Src RX error counter: 0\n"
- " Dst TX error counter: 0\n"
- " Dst RX error counter: 0\n"
- " CAN1 status: Bus-On, ES: 0x8\n"
- " CAN2 status: Bus-On, ES: 0x10\n",
- CMD_HANDLER(cmd_do_test_can_loopback), (void *)&cmd_list_can
-};
-
-
cmd_des_t const cmd_des_can_init={
0, 0,
"caninit", "Initialize CAN controllers",
cmd_des_t const *cmd_list_can[]={
- &cmd_des_test_can_loopback,
&cmd_des_can_init,
&cmd_des_can_baudrate,
&cmd_des_can_timing,