00001 /* 00002 * Copyright (c) 2005 proconX Pty Ltd <www.proconx.com> 00003 * 00004 * $Id: candemo.c,v 1.7 2008/01/31 09:38:15 haraldkipp Exp $ 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 unsigned long i; 00087 int result; 00088 u_long baud = 115200; 00089 00090 NutRegisterDevice(&DEV_DEBUG, 0, 0); 00091 freopen(DEV_UART_NAME, "w", stdout); 00092 _ioctl(_fileno(stdout), UART_SETSPEED, &baud); 00093 00094 printf("CAN driver test program\n"); 00095 00096 #if defined(__AVR__) && defined(__GNUC__) 00097 00098 // Init CAN controller 00099 result = NutRegisterDevice(&DEV_CAN, 0, 0); 00100 canInfoPtr = (CANINFO *) DEV_CAN.dev_dcb; 00101 00102 #if defined(MCU_AT90CAN128) 00103 // Re-configure receive message objects 00104 AtCanEnableRx(8, // 8 CAN objects as RX buffer, 7 remaining as TX buffer 00105 0, // Acceptance code (0 = accept all IDs) 00106 1, // Flag if acceptance code is extended (0 = standard, 1 = extended) 00107 0, // Acceptance code's remote tag (0 or 1) 00108 0, // Acceptance mask 00109 0, // 0 to receive extended and standard frames, 1 if message ID type must match acceptance code flag 00110 0 // 0 to receive remote and standard frames, 1 if remote tag must match acceptance code's remote tag 00111 ); 00112 #endif 00113 00114 // Set CAN bit rate 00115 CAN_SetSpeed(&DEV_CAN, CAN_SPEED_125K); 00116 00117 printf("Starting CAN RX/TX loop...\n"); 00118 for (i = 0;;i++) 00119 { 00120 // Prepare a sample frame for sending 00121 memset(&canFrame, 0, sizeof(canFrame)); 00122 canFrame.id = 0x123; 00123 canFrame.len = 8; 00124 canFrame.ext = 0; // Set to 1 to send an extended frame 00125 canFrame.byte[0] = 0x11; 00126 canFrame.byte[1] = 0x22; 00127 canFrame.byte[2] = 0x33; 00128 canFrame.byte[3] = 0x44; 00129 canFrame.byte[4] = 0x55; 00130 canFrame.byte[5] = 0x66; 00131 canFrame.byte[6] = 0x77; 00132 canFrame.byte[7] = 0x88; 00133 CAN_TxFrame(&DEV_CAN, &canFrame); 00134 00135 // Check if we did receive a frame 00136 if (CAN_TryRxFrame(&DEV_CAN, &canFrame) == 0) 00137 { 00138 u_char j; 00139 00140 printf("%ld ", canFrame.id); 00141 for (j = 0; j < canFrame.len; j++) 00142 printf("%02X ", canFrame.byte[j]); 00143 printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts, 00144 canInfoPtr->can_rx_frames, 00145 canInfoPtr->can_tx_frames, 00146 canInfoPtr->can_overruns); 00147 } 00148 NutSleep(10); // Don't overflow the bus and give time to other threads 00149 } 00150 #else /* __AVR__ && __GNUC__ */ 00151 puts("No CAN device available"); 00152 #endif /* __AVR__ && __GNUC__ */ 00153 return 0; 00154 } 00155