drivers/can_lincan/can_lincan.c
author lbessard
Mon, 16 Mar 2009 08:30:07 +0100
changeset 537 9abbda25a18b
parent 477 ec7654f71964
child 631 08b6b903f84a
permissions -rw-r--r--
Bug in file 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
  do{
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    41
    res = read(fd0,&canmsg,sizeof(canmsg_t));
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    42
    if((res<0)&&(errno == -EAGAIN)) res = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    43
  }while(res==0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    44
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    45
  if(res != sizeof(canmsg_t)) // No new message
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    46
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    47
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    48
  if(canmsg.flags&MSG_EXT){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    49
    /* There is no mark for extended messages in CanFestival */;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    50
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    51
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 145
diff changeset
    52
  m->cob_id = canmsg.id;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    53
  m->len = canmsg.length;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    54
  if(canmsg.flags&MSG_RTR){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    55
    m->rtr = 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    56
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    57
    m->rtr = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    58
    memcpy(m->data,canmsg.data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    59
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    60
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    61
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    62
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    63
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    64
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    65
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    66
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    67
  int res;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    68
  struct canmsg_t canmsg;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    69
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    70
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    71
  canmsg.flags = 0;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 145
diff changeset
    72
  canmsg.id = m->cob_id;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    73
  canmsg.length = m->len;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    74
  if(m->rtr){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    75
    canmsg.flags |= MSG_RTR;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    76
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    77
    memcpy(canmsg.data,m->data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    78
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    79
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    80
  if(canmsg.id >= 0x800){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    81
    canmsg.flags |= MSG_EXT;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    82
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    83
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
    84
  res = write(fd0,&canmsg,sizeof(canmsg_t));
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    85
  if(res!=sizeof(canmsg_t))
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    86
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    87
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    88
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    89
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    90
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    91
/***************************************************************************/
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    92
int TranslateBaudeRate(char* optarg){
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    93
	if(!strcmp( optarg, "1M")) return 1000000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    94
	if(!strcmp( optarg, "500K")) return 500000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    95
	if(!strcmp( optarg, "250K")) return 250000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    96
	if(!strcmp( optarg, "125K")) return 125000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    97
	if(!strcmp( optarg, "100K")) return 100000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    98
	if(!strcmp( optarg, "50K")) return 50000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
    99
	if(!strcmp( optarg, "20K")) return 20000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   100
	if(!strcmp( optarg, "none")) return 0;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   101
	return 0x0000;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   102
}
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   103
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   104
/***************************************************************************/
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   105
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   106
{
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   107
	struct can_baudparams_t params;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   108
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   109
	params.baudrate = TranslateBaudeRate(baud);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   110
	if(params.baudrate == 0)
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   111
		return 0;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   112
	params.flags = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   113
	params.sjw = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   114
	params.sample_pt = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   115
	if(ioctl((int)fd0, CONF_BAUDPARAMS, &params) < 0)
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   116
	{
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   117
		fprintf(stderr, "canOpen_driver (lincan): IOCTL set speed failed\n");
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   118
		return 0;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   119
	}
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   120
	return 1;
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   121
}
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   122
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   123
/***************************************************************************/
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   124
static const char lnx_can_dev_prefix[] = "/dev/can";
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   125
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   126
CAN_HANDLE canOpen_driver(s_BOARD *board)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   127
{
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   128
	int name_len = strlen(board->busname);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   129
	int prefix_len = strlen(lnx_can_dev_prefix);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   130
	char dev_name[prefix_len+name_len+1];
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   131
	struct can_baudparams_t params;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   132
	int o_flags = 0;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   133
	int fd;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   134
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   135
	/*o_flags = O_NONBLOCK;*/
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   136
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   137
	memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   138
	memcpy(dev_name+prefix_len,board->busname,name_len);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   139
	dev_name[prefix_len+name_len] = 0;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   140
	printf("dev_name %s\n", dev_name);
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   141
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   142
	fd = open(dev_name, O_RDWR|o_flags);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   143
	if(fd < 0)
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   144
	{
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   145
		fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   146
		goto error_ret;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   147
	}
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   148
	printf("fd = %d\n", fd);
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   149
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   150
	// set baudrate
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   151
	params.baudrate = TranslateBaudeRate(board->baudrate);
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   152
	if(params.baudrate == 0)
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   153
		goto error_ret;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   154
	params.flags = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   155
	params.sjw = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   156
	params.sample_pt = -1;	// use driver defaults
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   157
	if(ioctl(fd, CONF_BAUDPARAMS, &params) < 0)
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   158
	{
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   159
		fprintf(stderr, "canOpen_driver (lincan): IOCTL set speed failed\n");
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   160
		goto error_ret;
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   161
	}
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   162
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   163
	return (CAN_HANDLE)fd;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   164
477
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   165
error_ret:
ec7654f71964 "change baudrate" implemented for lincan interface
greg
parents: 384
diff changeset
   166
	return NULL;
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   167
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   168
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   169
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   170
int canClose_driver(CAN_HANDLE fd0)
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   171
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   172
  if(!fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   173
    return 0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 26
diff changeset
   174
  close(fd0);
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   175
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   176
}