examples/test_copcican_linux/test_copcican_linux.c
author Edouard Tisserant
Wed, 13 Apr 2011 16:29:59 +0200
changeset 659 1041153c5fd2
parent 629 b9274b595650
permissions -rw-r--r--
Fix some side effects of EDS import, while editing afterward
629
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     1
/*
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     3
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     4
Copyright (C): Cosateq GmbH & Co.KG
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     5
               http://www.cosateq.com/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     6
               http://www.scale-rt.com/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     7
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     8
See COPYING file for copyrights details.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     9
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    10
This library is free software; you can redistribute it and/or
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    11
modify it under the terms of the GNU Lesser General Public
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    12
License as published by the Free Software Foundation; either
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    13
version 2.1 of the License, or (at your option) any later version.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    14
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    15
This library is distributed in the hope that it will be useful,
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    16
but WITHOUT ANY WARRANTY; without even the implied warranty of
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    18
Lesser General Public License for more details.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    19
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    20
You should have received a copy of the GNU Lesser General Public
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    21
License along with this library; if not, write to the Free Software
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    22
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    23
*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    24
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    25
/*
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    26
	example for application with CO-PCICAN card.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    27
*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    28
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    29
#include <stdio.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    30
#include "canfestival.h"
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    31
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    32
/* only for usleep() */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    33
#include <linux/delay.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    34
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    35
#define CHTX 2 /* channel number of the CO-PCICAN */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    36
#define CHRX 0 /* channel number of the CO-PCICAN */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    37
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    38
void mySyncHandler( CO_Data* d )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    39
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    40
  printf( "  got a SYNC message...\n" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    41
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    42
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    43
int main()
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    44
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    45
  LIB_HANDLE driver;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    46
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    47
  /* handles for additional CO-PCICAN functions */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    48
  int (*get_fd_of_port)( CAN_PORT port );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    49
  int (*co_pcican_enter_run_mode)( const int fd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    50
  int (*co_pcican_enter_config_mode)( const int fd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    51
  int (*co_pcican_select_channel)( const unsigned char channel, const unsigned int direction );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    52
  int (*co_pcican_configure_selected_channel)( const int fd, s_BOARD *board, const unsigned int direction );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    53
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    54
  s_BOARD  bd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    55
  CO_Data  myData;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    56
  CAN_PORT port;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    57
  Message myMessage;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    58
  const char busname[]  = "/dev/co_pcican-0";
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    59
  const char baudrate[] = "1M";
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    60
  int fd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    61
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    62
  memset( &myData, 0x00, sizeof(CO_Data) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    63
  myData.CurrentCommunicationState.csSYNC = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    64
  myData.post_sync = mySyncHandler;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    65
  bd.busname  = (char*)busname;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    66
  bd.baudrate = (char*)baudrate;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    67
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    68
  printf( "This example sends three SYNCs from port#%u to port#%u with a CO-PCICAN card\n", CHTX, CHRX );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    69
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    70
  driver = LoadCanDriver( "/usr/local/lib/libcanfestival_can_copcican_linux.so" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    71
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    72
  if( driver == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    73
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    74
    /* something strange happenend */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    75
    return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    76
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    77
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    78
  /* dynamic load of additional library functions */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    79
  get_fd_of_port = dlsym( driver, "get_fd_of_port" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    80
  co_pcican_enter_run_mode = dlsym( driver, "co_pcican_enter_run_mode" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    81
  co_pcican_enter_config_mode = dlsym( driver, "co_pcican_enter_config_mode" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    82
  co_pcican_select_channel = dlsym( driver, "co_pcican_select_channel" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    83
  co_pcican_configure_selected_channel = dlsym( driver, "co_pcican_configure_selected_channel" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    84
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    85
  /* direction: 0=RX, 1=TX */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    86
  co_pcican_select_channel( CHRX, 0 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    87
  co_pcican_select_channel( CHTX, 1 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    88
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    89
  port = canOpen( &bd, &myData );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    90
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    91
  if( port == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    92
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    93
    UnLoadCanDriver( driver );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    94
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    95
    /* something strange happenend */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    96
    return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    97
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    98
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    99
  /* get file descriptor to control the opened device */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   100
  fd = get_fd_of_port( port );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   101
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   102
  co_pcican_enter_config_mode( fd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   103
  co_pcican_configure_selected_channel( fd, &bd, 0 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   104
  co_pcican_configure_selected_channel( fd, &bd, 1 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   105
  co_pcican_enter_run_mode( fd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   106
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   107
  memset( &myMessage, 0x00, sizeof(Message) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   108
  myMessage.cob_id = 0x80; /* SYNC message */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   109
  myMessage.len = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   110
  myMessage.data[0] = 0xA5;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   111
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   112
  /* SEND HERE */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   113
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   114
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   115
  myMessage.data[0] = 0x5A;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   116
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   117
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   118
  myMessage.data[0] = 0xA5;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   119
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   120
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   121
  /* mySyncHandler() is called by the receive thread and shows a received SYNC message on console */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   122
  usleep( 1*1000*1000 ); /* 1s */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   123
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   124
  canClose( &myData );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   125
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   126
  UnLoadCanDriver( driver );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   127
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   128
  return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   129
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   130