drivers/can_can4linux/can_can4linux.c
author etisserant
Fri, 25 Jan 2008 19:01:58 +0100
changeset 370 6fecf36df407
parent 365 9b76e0881beb
child 384 83793fc7ce48
permissions -rwxr-xr-x
Fixed some endianization problems caused by switch to UNS16 for cob_id member in CAN messages. To be continued.
364
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     1
/*
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     3
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     4
Copyright (C): Jorge BERZOSA
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     5
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     6
See COPYING file for copyrights details.
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     7
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    12
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    16
Lesser General Public License for more details.
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    17
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    21
*/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    22
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    23
/*
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    24
* can4linux driver
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    25
*/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    26
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    27
#include <stdio.h>
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    28
#include <string.h>
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    29
#include <errno.h>
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    30
#include <fcntl.h>
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    31
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    32
#include "can4linux.h" 
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    33
#include "can_driver.h"
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    34
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    35
struct timeval init_time,current_time;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    36
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    37
/*********functions which permit to communicate with the board****************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    38
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    39
{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    40
  int res,i;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    41
  canmsg_t canmsg;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    42
  long int time_period;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    43
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    44
	canmsg.flags = 0; 
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    45
	do{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    46
   		res = read(fd0,&canmsg,1);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    47
   		if((res<0)&&(errno == -EAGAIN)) res = 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    48
	}while(res==0);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    49
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    50
	if(res !=1) // No new message
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    51
   	return 1;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    52
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    53
	if(canmsg.flags&MSG_EXT){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    54
   		/* There is no mark for extended messages in CanFestival */;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    55
	}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    56
  		
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 364
diff changeset
    57
	m->cob_id = canmsg.id;
364
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    58
	m->len = canmsg.length;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    59
	if(canmsg.flags&MSG_RTR){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    60
 		m->rtr = 1;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    61
	}else{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    62
 		m->rtr = 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    63
 		memcpy(m->data,canmsg.data,8);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    64
	}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    65
	
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    66
	
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    67
 	/*gettimeofday(&current_time,NULL);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    68
 	time_period=(current_time.tv_sec - init_time.tv_sec)* 1000000 + current_time.tv_usec - init_time.tv_usec;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 364
diff changeset
    69
 	printf("%3ld.%3ld.%3ld - Receive ID: %lx ->",time_period/1000000,(time_period%1000000)/1000,time_period%1000,m->cob_id);
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 364
diff changeset
    70
 	printf("Receive ID: %lx ->",m->cob_id);
364
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    71
 	for(i=0; i<canmsg.length;i++)printf("%x, ", m->data[i]);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    72
 	printf("\n");*/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    73
 
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    74
  return 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    75
}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    76
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    77
/***************************************************************************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    78
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    79
{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    80
  int res;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    81
  canmsg_t canmsg;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    82
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    83
  canmsg.flags = 0;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 364
diff changeset
    84
  canmsg.id = m->cob_id;
364
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    85
  canmsg.length = m->len;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    86
  if(m->rtr){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    87
    canmsg.flags |= MSG_RTR;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    88
  }else{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    89
    memcpy(canmsg.data,m->data,8);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    90
  }
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    91
  
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    92
 /*printf("Send ID: %lx ->",canmsg.id); 
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    93
 for(i=0; i<canmsg.length;i++)printf("%x, ", canmsg.data[i]);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    94
 printf("\n");*/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    95
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    96
  if(canmsg.id >= 0x800){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    97
    canmsg.flags |= MSG_EXT;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    98
  }
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
    99
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   100
  res = write(fd0,&canmsg,1);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   101
  if(res!=1)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   102
    return 1;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   103
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   104
  return 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   105
}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   106
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   107
/***************************************************************************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   108
int	set_bitrate( CAN_HANDLE fd, int baud)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   109
{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   110
    Config_par_t  cfg;	
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   111
    volatile Command_par_t cmd;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   112
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   113
    cmd.cmd = CMD_STOP;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   114
    ioctl(fd, COMMAND, &cmd);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   115
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   116
    cfg.target = CONF_TIMING; 
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   117
    cfg.val1   = baud;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   118
    ioctl(fd, CONFIG, &cfg);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   119
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   120
    cmd.cmd = CMD_START;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   121
    ioctl(fd, COMMAND, &cmd);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   122
    return 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   123
}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   124
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   125
/***************************************************************************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   126
int TranslateBaudeRate(char* optarg){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   127
	if(!strcmp( optarg, "1M")) return (int)1000;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   128
	if(!strcmp( optarg, "500K")) return (int)500;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   129
	if(!strcmp( optarg, "250K")) return (int)250;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   130
	if(!strcmp( optarg, "125K")) return (int)125;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   131
	if(!strcmp( optarg, "100K")) return (int)100;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   132
	if(!strcmp( optarg, "50K")) return (int)50;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   133
	if(!strcmp( optarg, "20K")) return (int)20;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   134
	if(!strcmp( optarg, "10K")) return (int)10;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   135
	if(!strcmp( optarg, "5K")) return (int)5;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   136
	if(!strcmp( optarg, "none")) return 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   137
	return 0x0000;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   138
}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   139
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   140
/***************************************************************************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   141
static const char lnx_can_dev_prefix[] = "/dev/can";
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   142
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   143
CAN_HANDLE canOpen_driver(s_BOARD *board)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   144
{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   145
  int name_len = strlen(board->busname);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   146
  int prefix_len = strlen(lnx_can_dev_prefix);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   147
  char dev_name[prefix_len+name_len+1];
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   148
  int o_flags = 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   149
  int baud = TranslateBaudeRate(board->baudrate);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   150
  int fd0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   151
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   152
  
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   153
  /*o_flags = O_NONBLOCK;*/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   154
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   155
  memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   156
  memcpy(dev_name+prefix_len,board->busname,name_len);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   157
  dev_name[prefix_len+name_len] = 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   158
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   159
  fd0 = open(dev_name, O_RDWR|o_flags);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   160
  if(fd0 <= 0){
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   161
    fprintf(stderr,"!!! %s is unknown. See can4linux.c\n", dev_name);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   162
    goto error_ret;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   163
  }
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   164
  
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   165
  set_bitrate((CAN_HANDLE)fd0, baud);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   166
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   167
  printf("CAN device dev/can%s opened. Baudrate=>%s\n",board->busname, board->baudrate);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   168
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   169
  return (CAN_HANDLE)fd0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   170
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   171
 error_ret:
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   172
  return NULL;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   173
}
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   174
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   175
/***************************************************************************/
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   176
int canClose_driver(CAN_HANDLE fd0)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   177
{
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   178
  if((int)fd0<=0)
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   179
    return -1;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   180
  close(fd0);
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   181
  return 0;
17edd8a028fa Added can4linux driver support.
groke6
parents:
diff changeset
   182
}