[En-Nut-Discussion] [Q] Any can_dev example code

Henrik Maier hmlists at focus-sw.com
Mon Feb 27 04:39:39 CET 2006


Uwe,

here is the code for a very simple CAN message logger. It prints out 
every CAN message on the console and also sends a dummy frame. If Harald 
doesn't mind, I could even check it into the Ethernut project as an 
example CAN/AT90CAN128 project.


Henrik
http://www.proconx.com


[--- code snippet follows ---]

/*
  * Copyright (c) 2005 FOCUS Software Engineering Pty Ltd <www.focus-sw.com>
  * Copyright (c) 2005 proconX <www.proconx.com>
  *
  * $Id: candemo.c,v 1.1 2005/10/19 00:36:48 henrik Exp $
  *
  * The following source file constitutes example program code and is
  * intended merely to illustrate useful programming techniques.  The user
  * is responsible for applying the code correctly.
  */


// Nut/OS header
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.h>
#include <dev/debug.h>
#include <dev/nicrtl.h>
#include <dev/uartavr.h>
#include <arpa/inet.h>
#include <pro/dhcp.h>
#include <net/errno.h>

#include <dev/atcan.h>



/*****************************************************************************
  * Main
 
*****************************************************************************/

CANFRAME canFrame;
CANINFO *canInfoPtr;


/**
  * Main entry point of application
  */
int main(void)
{
    unsigned long i;
    int result;

    NutRegisterDevice(&devDebug0, 0, 0);
    freopen("uart0", "w", stdout);

    printf("CAN driver test program");

    // Init AT90CAN128 CAN controller
    result = NutRegisterDevice(&devAtCan, 0, 0);
    canInfoPtr = (CANINFO *) devAtCan.dev_dcb;
    printf("NutRegisterDevice=%d\n", result);

    // Set CAN bit rate
    CAN_SetSpeed(&devAtCan, CAN_SPEED_125K);

    printf("Starting CAN RX/TX loop...\n");
    for (i = 0;;i++)
    {
       // Prepare a frame for sending
       memset(&canFrame, 0, sizeof(canFrame));
       canFrame.id = 0x123;
       canFrame.len = 8;
	  canFrame.ext = 0; // Set to 1 to send an extended frame
       canFrame.byte[0] = 0x11;
       canFrame.byte[1] = 0x22;
       canFrame.byte[2] = 0x33;
       canFrame.byte[3] = 0x44;
       canFrame.byte[4] = 0x55;
       canFrame.byte[5] = 0x66;
       canFrame.byte[6] = 0x77;
       canFrame.byte[7] = 0x88;
       CAN_TxFrame(&devAtCan, &canFrame);

       // Check if we did receive a frame
       //if (CAN_RxFrame(&devAtCan, &canFrame) == 0)
       CAN_RxFrame(&devAtCan, &canFrame);
       {
          int8_t j;

          printf("%ld ", canFrame.id);
          for (j = 0; j < canFrame.len; j++)
             printf("%02X ", canFrame.byte[j]);
          printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts,
                 canInfoPtr->can_rx_frames,
                 canInfoPtr->can_tx_frames,
                 canInfoPtr->can_overruns);
       }
    }
}




More information about the En-Nut-Discussion mailing list