drivers/can_lincan/can_lincan.c
author lbessard
Thu, 18 Oct 2007 10:23:45 +0200
changeset 310 1975e546178b
parent 145 e747d2e26af0
child 365 9b76e0881beb
permissions -rw-r--r--
Bugs on EDS import fixed
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
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    53
  m->cob_id.w = canmsg.id;
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;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    73
  canmsg.id = m->cob_id.w;
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
/***************************************************************************/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    93
static const char lnx_can_dev_prefix[] = "/dev/can";
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    94
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    95
CAN_HANDLE canOpen_driver(s_BOARD *board)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    96
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    97
  int name_len = strlen(board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    98
  int prefix_len = strlen(lnx_can_dev_prefix);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    99
  char dev_name[prefix_len+name_len+1];
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   100
  int o_flags = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   101
  CAN_HANDLE fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   102
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   103
  fd0=malloc(sizeof(*fd0));
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   104
  if(fd0==NULL)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   105
    return NULL;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   106
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   107
  /*o_flags = O_NONBLOCK;*/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   108
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   109
  memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   110
  memcpy(dev_name+prefix_len,board->busname,name_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   111
  dev_name[prefix_len+name_len] = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   112
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   113
  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
   114
  if(fd0 < 0){
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   115
    fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   116
    goto error_ret;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   117
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   118
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   119
  return fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   120
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   121
 error_ret:
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   122
  free(fd0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   123
  return NULL;
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
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   127
int canClose_driver(CAN_HANDLE fd0)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   128
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   129
  if(!fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   130
    return 0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   131
  close(fd0);
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   132
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   133
}