candemo.c

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

© 2000-2010 by contributors - visit http://www.ethernut.de/