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 FOCUS Software Engineering Pty Ltd <www.focus-sw.com> 00003 * Copyright (c) 2005 proconX <www.proconx.com> 00004 * 00005 * $Id: candemo.c,v 1.1 2006/02/28 09:12:19 hwmaier Exp $ 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 3. Neither the name of the copyright holders nor the names of 00017 * contributors may be used to endorse or promote products derived 00018 * from this software without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS 00021 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00023 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE 00024 * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00026 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00027 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00028 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00030 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 * 00033 * For additional information see http://www.ethernut.de/ 00034 */ 00035 00036 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/heap.h> 00058 #include <sys/thread.h> 00059 #include <sys/timer.h> 00060 #include <sys/socket.h> 00061 #include <dev/debug.h> 00062 #include <dev/nicrtl.h> 00063 #include <dev/uartavr.h> 00064 #include <arpa/inet.h> 00065 #include <pro/dhcp.h> 00066 #include <net/errno.h> 00067 #include <dev/atcan.h> 00068 00069 00070 /***************************************************************************** 00071 * Main 00072 *****************************************************************************/ 00073 00074 CANFRAME canFrame; 00075 CANINFO *canInfoPtr; 00076 00077 00081 int main(void) 00082 { 00083 unsigned long i; 00084 int result; 00085 00086 NutRegisterDevice(&devDebug0, 0, 0); 00087 freopen("uart0", "w", stdout); 00088 00089 printf("CAN driver test program"); 00090 00091 // Init AT90CAN128 CAN controller 00092 result = NutRegisterDevice(&devAtCan, 0, 0); 00093 canInfoPtr = (CANINFO *) devAtCan.dev_dcb; 00094 00095 // Re-configure receive message objects 00096 AtCanEnableRx(8, // 8 CAN objects as RX buffer, 7 remaining as TX buffer 00097 0, // Acceptance code (0 = accept all IDs) 00098 1, // Flag if acceptance code is extended (0 = standard, 1 = extended) 00099 0, // Acceptance code's remote tag (0 or 1) 00100 0, // Acceptance mask 00101 0, // 0 to receive extended and standard frames, 1 if message ID type must match acceptance code flag 00102 0 // 0 to receive remote and standard frames, 1 if remote tag must match acceptance code's remote tag 00103 ); 00104 00105 // Set CAN bit rate 00106 CAN_SetSpeed(&devAtCan, CAN_SPEED_125K); 00107 00108 00109 printf("Starting CAN RX/TX loop...\n"); 00110 for (i = 0;;i++) 00111 { 00112 // Prepare a sample frame for sending 00113 memset(&canFrame, 0, sizeof(canFrame)); 00114 canFrame.id = 0x123; 00115 canFrame.len = 8; 00116 canFrame.ext = 0; // Set to 1 to send an extended frame 00117 canFrame.byte[0] = 0x11; 00118 canFrame.byte[1] = 0x22; 00119 canFrame.byte[2] = 0x33; 00120 canFrame.byte[3] = 0x44; 00121 canFrame.byte[4] = 0x55; 00122 canFrame.byte[5] = 0x66; 00123 canFrame.byte[6] = 0x77; 00124 canFrame.byte[7] = 0x88; 00125 CAN_TxFrame(&devAtCan, &canFrame); 00126 00127 // Check if we did receive a frame 00128 if (CAN_TryRxFrame(&devAtCan, &canFrame) == 0) 00129 { 00130 int8_t j; 00131 00132 printf("%ld ", canFrame.id); 00133 for (j = 0; j < canFrame.len; j++) 00134 printf("%02X ", canFrame.byte[j]); 00135 printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts, 00136 canInfoPtr->can_rx_frames, 00137 canInfoPtr->can_tx_frames, 00138 canInfoPtr->can_overruns); 00139 } 00140 } 00141 } 00142