examples/test_copcican_comedi/test_copcican_comedi.c
author Robert Lehmann <robert.lehmann@sitec-systems.de>
Tue, 28 Jul 2015 16:36:55 +0200
changeset 793 72e9e1064432
parent 629 b9274b595650
permissions -rw-r--r--
timers_unix: Fix termination problem of WaitReceiveTaskEnd

The function pthread_kill sends the Signal thread and to the own process.
If you use this construct than the application which calls uses the
canfestival api will terminate at the call of canClose. To avoid that
use pthread_cancel instead of pthread_kill. To use the pthread_cancel call
you need to set the cancel ability in the thread function. That means
you need to call pthread_setcancelstate and pthread_setcanceltype.
For the termination of the thread at any time it is important to set the
cancel type to PTHREAD_CANCEL_ASYNCHRONOUS.
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 <linux/module.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    30
#include <linux/comedi.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    31
#include <linux/comedilib.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    32
#include "canfestival.h"
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    33
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    34
/* only for mdelay() */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    35
#include <linux/delay.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    36
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    37
MODULE_LICENSE("GPL");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    38
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    39
#define CHTX 2 /* channel number of the CO-PCICAN */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    40
#define CHRX 0 /* channel number of the CO-PCICAN */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    41
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    42
void mySyncHandler( CO_Data* d )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    43
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    44
  printk( "test_copcican_comedi:   got a SYNC message...\n" );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    45
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    46
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    47
static int main( void )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    48
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    49
  s_BOARD  bd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    50
  CO_Data  myData;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    51
  CAN_PORT port;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    52
  Message myMessage;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    53
  const char busname[]  = "/dev/comedi0";
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    54
  const char baudrate[] = "1M";
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    55
  comedi_t *dev;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    56
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    57
  memset( &myData, 0x00, sizeof(CO_Data) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    58
  myData.CurrentCommunicationState.csSYNC = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    59
  myData.post_sync = mySyncHandler;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    60
  bd.busname  = (char*)busname;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    61
  bd.baudrate = (char*)baudrate;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    62
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    63
  printk( "test_copcican_comedi: 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
    64
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    65
  /* direction: 0=RX, 1=TX */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    66
  co_pcican_select_channel( CHRX, 0 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    67
  co_pcican_select_channel( CHTX, 1 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    68
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    69
  port = canOpen( &bd, &myData );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    70
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    71
  if( port == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    72
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    73
    /* something strange happenend */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    74
    return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    75
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    76
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    77
  /* get device pointer to control the opened device */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    78
  dev = get_dev_of_port( port );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    79
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    80
  co_pcican_enter_config_mode( dev );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    81
  co_pcican_configure_selected_channel( dev, &bd, 0 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    82
  co_pcican_configure_selected_channel( dev, &bd, 1 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    83
  co_pcican_enter_run_mode( dev );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    84
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    85
  memset( &myMessage, 0x00, sizeof(Message) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    86
  myMessage.cob_id = 0x80; /* SYNC message */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    87
  myMessage.len = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    88
  myMessage.data[0] = 0xA5;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    89
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    90
  /* SEND HERE */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    91
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    92
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    93
  myMessage.data[0] = 0x5A;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    94
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    95
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    96
  myMessage.data[0] = 0xA5;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    97
  canSend( port, &myMessage );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    98
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    99
  /* mySyncHandler() is called by the receive thread and shows a received SYNC message in the kernel log */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   100
  mdelay( 1*1000 ); /* 1s */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   101
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   102
  canClose( &myData );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   103
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   104
  return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   105
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   106
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   107
static int init(void)
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   108
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   109
  printk("test_copcican_comedi for CanFestival loaded\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   110
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   111
  return main();
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   112
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   113
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   114
static void exit(void)
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   115
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   116
  printk("test_copcican_comedi unloaded\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   117
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   118
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   119
module_init(init);
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   120
module_exit(exit);