This program receives CAN messages and logs them on the serial port via devDebug0. It also continuously broadcasts a CAN frame.
00001 /* 00002 * Copyright (c) 2005 proconX Pty Ltd <www.proconx.com> 00003 * 00004 * $Id: candemo.c 2483 2009-02-18 12:18:58Z olereinhardt $ 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 3. Neither the name of the copyright holders nor the names of 00016 * contributors may be used to endorse or promote products derived 00017 * from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS 00020 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00022 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE 00023 * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00024 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00025 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00026 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00027 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00028 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00029 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00030 * SUCH DAMAGE. 00031 * 00032 * For additional information see http://www.ethernut.de/ 00033 */ 00034 00035 00051 // Nut/OS header 00052 #include <stdlib.h> 00053 #include <stdio.h> 00054 #include <string.h> 00055 #include <io.h> 00056 #include <fcntl.h> 00057 #include <sys/timer.h> 00058 #include <sys/thread.h> 00059 #include <dev/debug.h> 00060 #include <dev/uartavr.h> 00061 #include <dev/can_dev.h> 00062 #include <dev/board.h> 00063 00064 #if defined(MCU_AT90CAN128) // Internal AVR MCU CAN controller 00065 # include <dev/atcan.h> 00066 # define DEV_CAN devAtCan 00067 #else 00068 # include <dev/sja1000.h> // External SJA1000 CAN controller 00069 # define DEV_CAN devSJA1000 00070 #endif 00071 00072 00073 /***************************************************************************** 00074 * Main 00075 *****************************************************************************/ 00076 00077 CANFRAME canFrame; 00078 CANINFO *canInfoPtr; 00079 00080 00084 int main(void) 00085 { 00086 #if defined(__AVR__) && defined(__GNUC__) 00087 unsigned long i; 00088 int result; 00089 #endif 00090 u_long baud = 115200; 00091 00092 NutRegisterDevice(&DEV_DEBUG, 0, 0); 00093 freopen(DEV_DEBUG_NAME, "w", stdout); 00094 _ioctl(_fileno(stdout), UART_SETSPEED, &baud); 00095 00096 printf("CAN driver test program\n"); 00097 00098 #if defined(__AVR__) && defined(__GNUC__) 00099 00100 // Init CAN controller 00101 result = NutRegisterDevice(&DEV_CAN, 0, 0); 00102 canInfoPtr = (CANINFO *) DEV_CAN.dev_dcb; 00103 00104 #if defined(MCU_AT90CAN128) 00105 // Re-configure receive message objects 00106 AtCanEnableRx(8, // 8 CAN objects as RX buffer, 7 remaining as TX buffer 00107 0, // Acceptance code (0 = accept all IDs) 00108 1, // Flag if acceptance code is extended (0 = standard, 1 = extended) 00109 0, // Acceptance code's remote tag (0 or 1) 00110 0, // Acceptance mask 00111 0, // 0 to receive extended and standard frames, 1 if message ID type must match acceptance code flag 00112 0 // 0 to receive remote and standard frames, 1 if remote tag must match acceptance code's remote tag 00113 ); 00114 #endif 00115 00116 // Set CAN bit rate 00117 CAN_SetSpeed(&DEV_CAN, CAN_SPEED_125K); 00118 00119 printf("Starting CAN RX/TX loop...\n"); 00120 for (i = 0;;i++) 00121 { 00122 // Prepare a sample frame for sending 00123 memset(&canFrame, 0, sizeof(canFrame)); 00124 canFrame.id = 0x123; 00125 canFrame.len = 8; 00126 canFrame.ext = 0; // Set to 1 to send an extended frame 00127 canFrame.byte[0] = 0x11; 00128 canFrame.byte[1] = 0x22; 00129 canFrame.byte[2] = 0x33; 00130 canFrame.byte[3] = 0x44; 00131 canFrame.byte[4] = 0x55; 00132 canFrame.byte[5] = 0x66; 00133 canFrame.byte[6] = 0x77; 00134 canFrame.byte[7] = 0x88; 00135 CAN_TxFrame(&DEV_CAN, &canFrame); 00136 00137 // Check if we did receive a frame 00138 if (CAN_TryRxFrame(&DEV_CAN, &canFrame) == 0) 00139 { 00140 u_char j; 00141 00142 printf("%ld ", canFrame.id); 00143 for (j = 0; j < canFrame.len; j++) 00144 printf("%02X ", canFrame.byte[j]); 00145 printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts, 00146 canInfoPtr->can_rx_frames, 00147 canInfoPtr->can_tx_frames, 00148 canInfoPtr->can_overruns); 00149 } 00150 NutSleep(10); // Don't overflow the bus and give time to other threads 00151 } 00152 #else /* __AVR__ && __GNUC__ */ 00153 puts("No CAN device available"); 00154 #endif /* __AVR__ && __GNUC__ */ 00155 return 0; 00156 } 00157