/** \file canlib.c Function (mini) lib for applications \author Cristiano Brudna \date 2003 University of Ulm, Germany This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include "can.h" #include "canlib.h" typedef struct error_code { int code; char name[25]; } error_code_t; /** \brief Error codes + descriptions */ error_code_t errorCodes[] = { {3, "start of frame"}, {2, "ID.28 to ID.21"}, {6, "ID.20 to ID.18"}, {4, "bit SRTR"}, {5, "bit IDE"}, {7, "ID.17 to ID.13"}, {15, "ID.12 to ID.5"}, {14, "ID.4 to ID.0"}, {2, "bit RTR"}, {13, "reserved bit 1"}, {9, "reserved bit 0"}, {11, "data length code"}, {10, "data field"}, {8, "CRC sequence"}, {24, "CRC delimiter"}, {25, "acknowledge slot"}, {27, "acknowledge delimiter"}, {26, "end of frame"}, {18, "intermission"}, {17, "active error flag"}, {22, "passive error flag"}, {19, "tolerate dominant bits"}, {23, "error delimiter"}, {28, "overload flag"} }; /** \brief Decode the error code capture value * * \param ecc the error code value */ void printError (int ecc) { int i; int ecc_low; printf("ERROR_CODE_CAPTURE = %dd (", ecc); if(ecc & 0x20) printf("RX error, "); else printf("TX error, "); ecc_low = ecc & 0x1f; for(i=0; i<24; i++) { if(errorCodes[i].code == ecc_low) { printf("%s)\n", errorCodes[i].name); return; } } printf("?)\n"); } /** \brief Print SJA1000 status * * \param fd file descriptor of the CAN device */ void showStatus (int fd) { unsigned long reg; int ecc; reg = ERROR_CODE_CAPTURE; ecc = ioctl(fd, CAN_IOCRREG, ®); printError(ecc); reg = RX_ERROR_COUNTER; printf("RX_ERROR_COUNTER = %dd\n", ioctl(fd, CAN_IOCRREG, ®)); reg = TX_ERROR_COUNTER; printf("TX_ERROR_COUNTER = %dd\n\n", ioctl(fd, CAN_IOCRREG, ®)); }