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