drivers/can_lincan/can_lincan.c
author etisserant
Mon, 18 Feb 2008 10:36:01 +0100
changeset 398 c928fd8cb759
parent 384 83793fc7ce48
child 477 ec7654f71964
permissions -rw-r--r--
added configure option to force Wx detection.
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     1
/*
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     3
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     5
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     6
See COPYING file for copyrights details.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     7
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    12
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    16
Lesser General Public License for more details.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    17
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    21
*/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    22
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    23
#include <stdio.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    24
#include <string.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    25
#include <errno.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    26
#include <fcntl.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    27
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    28
#include "canmsg.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    29
#include "lincan.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    30
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    31
#include "can_driver.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    32
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    33
/*********functions which permit to communicate with the board****************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    34
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    35
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    36
  int res;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    37
  struct canmsg_t canmsg;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    38
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    39
  canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    40
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    41
  do{
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    42
    res = read(fd0,&canmsg,sizeof(canmsg_t));
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    43
    if((res<0)&&(errno == -EAGAIN)) res = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    44
  }while(res==0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    45
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    46
  if(res != sizeof(canmsg_t)) // No new message
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    47
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    48
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    49
  if(canmsg.flags&MSG_EXT){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    50
    /* There is no mark for extended messages in CanFestival */;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    51
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    52
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 145
diff changeset
    53
  m->cob_id = canmsg.id;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    54
  m->len = canmsg.length;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    55
  if(canmsg.flags&MSG_RTR){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    56
    m->rtr = 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    57
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    58
    m->rtr = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    59
    memcpy(m->data,canmsg.data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    60
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    61
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    62
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    63
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    64
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    65
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    66
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    67
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    68
  int res;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    69
  struct canmsg_t canmsg;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    70
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    71
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    72
  canmsg.flags = 0;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 145
diff changeset
    73
  canmsg.id = m->cob_id;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    74
  canmsg.length = m->len;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    75
  if(m->rtr){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    76
    canmsg.flags |= MSG_RTR;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    77
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    78
    memcpy(canmsg.data,m->data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    79
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    80
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    81
  if(canmsg.id >= 0x800){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    82
    canmsg.flags |= MSG_EXT;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    83
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    84
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    85
  res = write(fd0,&canmsg,sizeof(canmsg_t));
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    86
  if(res!=sizeof(canmsg_t))
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    87
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    88
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    89
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    90
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    91
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    92
/***************************************************************************/
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    93
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    94
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    95
	printf("canChangeBaudRate not yet supported by this driver\n");
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    96
	return 0;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    97
}
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    98
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
    99
/***************************************************************************/
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   100
static const char lnx_can_dev_prefix[] = "/dev/can";
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   101
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   102
CAN_HANDLE canOpen_driver(s_BOARD *board)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   103
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   104
  int name_len = strlen(board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   105
  int prefix_len = strlen(lnx_can_dev_prefix);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   106
  char dev_name[prefix_len+name_len+1];
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   107
  int o_flags = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   108
  CAN_HANDLE fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   109
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   110
  fd0=malloc(sizeof(*fd0));
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   111
  if(fd0==NULL)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   112
    return NULL;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   113
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   114
  /*o_flags = O_NONBLOCK;*/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   115
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   116
  memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   117
  memcpy(dev_name+prefix_len,board->busname,name_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   118
  dev_name[prefix_len+name_len] = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   119
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   120
  fd0 = open(dev_name, O_RDWR|o_flags);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   121
  if(fd0 < 0){
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   122
    fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   123
    goto error_ret;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   124
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   125
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   126
  return fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   127
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   128
 error_ret:
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   129
  free(fd0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   130
  return NULL;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   131
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   132
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   133
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   134
int canClose_driver(CAN_HANDLE fd0)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   135
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   136
  if(!fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   137
    return 0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   138
  close(fd0);
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   139
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   140
}