drivers/can_vscom/can_vscom.c
changeset 455 26f762d4116d
child 631 08b6b903f84a
equal deleted inserted replaced
454:bc000083297a 455:26f762d4116d
       
     1 /*
       
     2 This file is part of CanFestival, a library implementing CanOpen Stack. 
       
     3 
       
     4 Copyright (C): VScom
       
     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  * @file can_vscom.c
       
    25  * @author VScom (http://www.vscom.de)
       
    26  * @date 17/04/08
       
    27  *
       
    28  * This file implements interface between CanFestival and the VSCAN API 
       
    29  * (supported VScom products SER-CAN, USB-CAN, NET-CAN). 
       
    30  *
       
    31  * To build this interface following files will needed:
       
    32  * vs_can_api.h
       
    33  * va_can_api.lib (for Windows builds)
       
    34 */
       
    35 
       
    36 #include <stdio.h>
       
    37 #include <string.h>
       
    38 #include <errno.h>
       
    39 #include <fcntl.h>
       
    40 
       
    41 // VSCAN API header
       
    42 #include "vs_can_api.h" // for CAN_HANDLE
       
    43 
       
    44 #include "can_driver.h"
       
    45 
       
    46 /*********functions which permit to communicate with the board****************/
       
    47 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
       
    48 {
       
    49   VSCAN_MSG Msg[1];
       
    50   UNS8 i; 
       
    51   DWORD dwRead; /* number of read frames */
       
    52 
       
    53 
       
    54   if (VSCAN_Read((VSCAN_HANDLE)fd0, Msg, 1, &dwRead) != VSCAN_ERR_OK)
       
    55   {
       
    56     printf("canReceive_driver (VScom): error receiving frame)\n");
       
    57     return 1;
       
    58   }
       
    59 
       
    60   /* identifier of the CAN frame */
       
    61   m->cob_id = Msg[0].Id;   
       
    62 
       
    63   /* CAN frame type */
       
    64   if (Msg[0].Flags == VSCAN_FLAGS_STANDARD)
       
    65     m->rtr = 0;
       
    66   else 
       
    67     m->rtr = 1;
       
    68 
       
    69   /* width of the data bytes */
       
    70   m->len = Msg[0].Size;	
       
    71   
       
    72   /* copy data bytes from the CAN frame, up to 8 */
       
    73   for(i = 0  ; i < Msg[0].Size ; i++)             			
       
    74   {
       
    75     m->data[i] = Msg[0].Data[i];           
       
    76   }
       
    77 
       
    78   return 0;
       
    79 }
       
    80 
       
    81 /***************************************************************************/
       
    82 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
       
    83 {
       
    84   VSCAN_MSG Msg[1];
       
    85   UNS8 i;
       
    86   DWORD dwWritten; /* number of written frames */
       
    87 
       
    88   /* identifier of the CAN frame */
       
    89   Msg[0].Id = m->cob_id;
       
    90 
       
    91   /* CAN frame type */
       
    92   if(m->rtr == 0)	
       
    93     Msg[0].Flags = VSCAN_FLAGS_STANDARD;
       
    94   else
       
    95     Msg[0].Flags = VSCAN_FLAGS_REMOTE;
       
    96 
       
    97   /* width of the data bytes */
       
    98   Msg[0].Size = m->len;
       
    99 
       
   100   /* copy data bytes to the CAN frame, up to 8 */
       
   101   for(i = 0 ; i <  m->len; i++)
       
   102   	Msg[0].Data[i] = m->data[i];         	
       
   103 
       
   104   /* copy CAN frame to the output buffer */
       
   105   if (!(VSCAN_Write((VSCAN_HANDLE)fd0, Msg, (DWORD)1, &dwWritten) == VSCAN_ERR_OK && dwWritten))
       
   106   {
       
   107     perror("canSend_driver (VScom): error writing to output buffer.\n");
       
   108     return 1;
       
   109   }
       
   110   
       
   111   /* really send CAN frame */
       
   112   if(VSCAN_Flush((VSCAN_HANDLE)fd0) != VSCAN_ERR_OK)
       
   113   {
       
   114     perror("canSend_driver (VScom): error flushing.\n");
       
   115     return 1;
       
   116   }
       
   117 
       
   118   return 0;
       
   119 }
       
   120 
       
   121 
       
   122 /***************************************************************************/
       
   123 int TranslateBaudeRate(char* optarg){
       
   124 	if(!strcmp( optarg, "1M")) return (int)VSCAN_SPEED_1M;
       
   125 	if(!strcmp( optarg, "500K")) return (int)VSCAN_SPEED_500K;
       
   126 	if(!strcmp( optarg, "250K")) return (int)VSCAN_SPEED_250K;
       
   127 	if(!strcmp( optarg, "125K")) return (int)VSCAN_SPEED_125K;
       
   128 	if(!strcmp( optarg, "100K")) return (int)VSCAN_SPEED_100K;
       
   129 	if(!strcmp( optarg, "50K")) return (int)VSCAN_SPEED_50K;
       
   130 	if(!strcmp( optarg, "20K")) return (int)VSCAN_SPEED_20K;
       
   131 	if(!strcmp( optarg, "none")) return 0;
       
   132 	return 0x0000;
       
   133 }
       
   134 
       
   135 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
       
   136 {
       
   137   int baudrate;
       
   138 
       
   139   baudrate = TranslateBaudeRate(baud);
       
   140   if(baudrate == 0)
       
   141     return 0;
       
   142 
       
   143   if (VSCAN_Ioctl((VSCAN_HANDLE)fd, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
       
   144   {
       
   145     fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
       
   146     return 0;
       
   147   }
       
   148 
       
   149   return 1;
       
   150 }
       
   151 
       
   152 /***************************************************************************/
       
   153 CAN_HANDLE canOpen_driver(s_BOARD *board)
       
   154 {
       
   155   VSCAN_HANDLE fd0 = 0;
       
   156   char busname[64];
       
   157   char* pEnd;
       
   158   int i;  
       
   159   int baudrate;
       
   160   
       
   161   printf("bus %s ", board->busname);
       
   162   fd0 = VSCAN_Open(board->busname, VSCAN_MODE_NORMAL);
       
   163   if(fd0 <= 0)
       
   164   {
       
   165     fprintf(stderr, "canOpen_driver (VScom): error opening %s\n", board->busname);
       
   166     return (CAN_HANDLE)fd0;
       
   167   }
       
   168   printf("(fd = %d)\n", fd0);
       
   169   baudrate = TranslateBaudeRate(board->baudrate);
       
   170   if(baudrate == 0)
       
   171     return 0;
       
   172 
       
   173   if (VSCAN_Ioctl((VSCAN_HANDLE)fd0, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
       
   174   {
       
   175     fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
       
   176     return 0;
       
   177   }
       
   178 
       
   179    return (CAN_HANDLE)fd0;
       
   180 }
       
   181 
       
   182 /***************************************************************************/
       
   183 int canClose_driver(CAN_HANDLE fd0)
       
   184 {
       
   185   VSCAN_Close((VSCAN_HANDLE)fd0);
       
   186   return 0;
       
   187 }