canbus/candemo.c

Example program to demonstrate the use of the CAN bus and the ATCAN driver on the XNUT-105 DIN rail single board computer with CAN and embedded Ethernet. The XNUT-105 module features the AT90CAN128 AVR CPU with built-in CAN controller and is running the Nut/OS.

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.3 2006/08/31 19:14:44 haraldkipp 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  * Main
00071  *****************************************************************************/
00072 
00073 CANFRAME canFrame;
00074 CANINFO *canInfoPtr;
00075 
00076 
00080 int main(void)
00081 {
00082    unsigned long i;
00083    int result;
00084 
00085 #if defined(__AVR__)
00086    NutRegisterDevice(&devDebug0, 0, 0);
00087 #endif
00088    freopen("uart0", "w", stdout);
00089 
00090    printf("CAN driver test program");
00091 
00092 #if defined(MCU_AT90CAN128)
00093 
00094    // Init AT90CAN128 CAN controller
00095    result = NutRegisterDevice(&devAtCan, 0, 0);
00096    canInfoPtr = (CANINFO *) devAtCan.dev_dcb;
00097 
00098    // Re-configure receive message objects
00099    AtCanEnableRx(8, // 8 CAN objects as RX buffer, 7 remaining as TX buffer
00100                  0, // Acceptance code (0 = accept all IDs)
00101                  1, // Flag if acceptance code is extended (0 = standard, 1 = extended)
00102                  0, // Acceptance code's remote tag (0 or 1)
00103                  0, // Acceptance mask
00104                  0, // 0 to receive extended and standard frames, 1 if message ID type must match acceptance code flag
00105                  0  // 0 to receive remote and standard frames, 1 if remote tag must match acceptance code's remote tag
00106                  ); 
00107 
00108    // Set CAN bit rate
00109    CAN_SetSpeed(&devAtCan, CAN_SPEED_125K);
00110 
00111 
00112    printf("Starting CAN RX/TX loop...\n");
00113    for (i = 0;;i++)
00114    {
00115       // Prepare a sample frame for sending
00116       memset(&canFrame, 0, sizeof(canFrame));
00117       canFrame.id = 0x123;
00118       canFrame.len = 8;
00119       canFrame.ext = 0; // Set to 1 to send an extended frame
00120       canFrame.byte[0] = 0x11;
00121       canFrame.byte[1] = 0x22;
00122       canFrame.byte[2] = 0x33;
00123       canFrame.byte[3] = 0x44;
00124       canFrame.byte[4] = 0x55;
00125       canFrame.byte[5] = 0x66;
00126       canFrame.byte[6] = 0x77;
00127       canFrame.byte[7] = 0x88;
00128       CAN_TxFrame(&devAtCan, &canFrame);
00129 
00130       // Check if we did receive a frame
00131       if (CAN_TryRxFrame(&devAtCan, &canFrame) == 0)
00132       {
00133          int8_t j;
00134 
00135          printf("%ld ", canFrame.id);
00136          for (j = 0; j < canFrame.len; j++)
00137             printf("%02X ", canFrame.byte[j]);
00138          printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts,
00139                 canInfoPtr->can_rx_frames,
00140                 canInfoPtr->can_tx_frames,
00141                 canInfoPtr->can_overruns);
00142       }
00143    }
00144 #else   /* MCU_AT90CAN128 */
00145 #warning "This sample requires an AT90CAN128 MCU"
00146    i = 0;
00147    result = 0;
00148    for (;;);
00149 #endif  /* MCU_AT90CAN128 */
00150 }
00151 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/