drivers/can_vscom/can_vscom.c
author greg
Fri, 09 May 2008 09:47:56 +0200
changeset 463 f7455fef585f
parent 455 26f762d4116d
child 631 08b6b903f84a
permissions -rw-r--r--
add rt_task_join for receive task and timerloop task
455
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     1
/*
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     3
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     4
Copyright (C): VScom
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     5
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     6
See COPYING file for copyrights details.
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     7
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    12
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    16
Lesser General Public License for more details.
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    17
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    21
*/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    22
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    23
/**
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    24
 * @file can_vscom.c
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    25
 * @author VScom (http://www.vscom.de)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    26
 * @date 17/04/08
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    27
 *
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    28
 * This file implements interface between CanFestival and the VSCAN API 
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    29
 * (supported VScom products SER-CAN, USB-CAN, NET-CAN). 
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    30
 *
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    31
 * To build this interface following files will needed:
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    32
 * vs_can_api.h
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    33
 * va_can_api.lib (for Windows builds)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    34
*/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    35
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    36
#include <stdio.h>
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    37
#include <string.h>
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    38
#include <errno.h>
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    39
#include <fcntl.h>
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    40
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    41
// VSCAN API header
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    42
#include "vs_can_api.h" // for CAN_HANDLE
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    43
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    44
#include "can_driver.h"
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    45
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    46
/*********functions which permit to communicate with the board****************/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    47
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    48
{
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    49
  VSCAN_MSG Msg[1];
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    50
  UNS8 i; 
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    51
  DWORD dwRead; /* number of read frames */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    52
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    53
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    54
  if (VSCAN_Read((VSCAN_HANDLE)fd0, Msg, 1, &dwRead) != VSCAN_ERR_OK)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    55
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    56
    printf("canReceive_driver (VScom): error receiving frame)\n");
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    57
    return 1;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    58
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    59
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    60
  /* identifier of the CAN frame */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    61
  m->cob_id = Msg[0].Id;   
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    62
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    63
  /* CAN frame type */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    64
  if (Msg[0].Flags == VSCAN_FLAGS_STANDARD)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    65
    m->rtr = 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    66
  else 
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    67
    m->rtr = 1;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    68
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    69
  /* width of the data bytes */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    70
  m->len = Msg[0].Size;	
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    71
  
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    72
  /* copy data bytes from the CAN frame, up to 8 */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    73
  for(i = 0  ; i < Msg[0].Size ; i++)             			
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    74
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    75
    m->data[i] = Msg[0].Data[i];           
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    76
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    77
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    78
  return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    79
}
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    80
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    81
/***************************************************************************/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    82
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    83
{
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    84
  VSCAN_MSG Msg[1];
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    85
  UNS8 i;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    86
  DWORD dwWritten; /* number of written frames */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    87
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    88
  /* identifier of the CAN frame */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    89
  Msg[0].Id = m->cob_id;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    90
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    91
  /* CAN frame type */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    92
  if(m->rtr == 0)	
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    93
    Msg[0].Flags = VSCAN_FLAGS_STANDARD;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    94
  else
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    95
    Msg[0].Flags = VSCAN_FLAGS_REMOTE;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    96
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    97
  /* width of the data bytes */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    98
  Msg[0].Size = m->len;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
    99
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   100
  /* copy data bytes to the CAN frame, up to 8 */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   101
  for(i = 0 ; i <  m->len; i++)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   102
  	Msg[0].Data[i] = m->data[i];         	
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   103
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   104
  /* copy CAN frame to the output buffer */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   105
  if (!(VSCAN_Write((VSCAN_HANDLE)fd0, Msg, (DWORD)1, &dwWritten) == VSCAN_ERR_OK && dwWritten))
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   106
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   107
    perror("canSend_driver (VScom): error writing to output buffer.\n");
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   108
    return 1;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   109
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   110
  
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   111
  /* really send CAN frame */
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   112
  if(VSCAN_Flush((VSCAN_HANDLE)fd0) != VSCAN_ERR_OK)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   113
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   114
    perror("canSend_driver (VScom): error flushing.\n");
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   115
    return 1;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   116
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   117
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   118
  return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   119
}
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   120
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   121
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   122
/***************************************************************************/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   123
int TranslateBaudeRate(char* optarg){
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   124
	if(!strcmp( optarg, "1M")) return (int)VSCAN_SPEED_1M;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   125
	if(!strcmp( optarg, "500K")) return (int)VSCAN_SPEED_500K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   126
	if(!strcmp( optarg, "250K")) return (int)VSCAN_SPEED_250K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   127
	if(!strcmp( optarg, "125K")) return (int)VSCAN_SPEED_125K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   128
	if(!strcmp( optarg, "100K")) return (int)VSCAN_SPEED_100K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   129
	if(!strcmp( optarg, "50K")) return (int)VSCAN_SPEED_50K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   130
	if(!strcmp( optarg, "20K")) return (int)VSCAN_SPEED_20K;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   131
	if(!strcmp( optarg, "none")) return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   132
	return 0x0000;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   133
}
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   134
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   135
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   136
{
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   137
  int baudrate;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   138
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   139
  baudrate = TranslateBaudeRate(baud);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   140
  if(baudrate == 0)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   141
    return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   142
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   143
  if (VSCAN_Ioctl((VSCAN_HANDLE)fd, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   144
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   145
    fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   146
    return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   147
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   148
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   149
  return 1;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   150
}
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   151
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   152
/***************************************************************************/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   153
CAN_HANDLE canOpen_driver(s_BOARD *board)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   154
{
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   155
  VSCAN_HANDLE fd0 = 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   156
  char busname[64];
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   157
  char* pEnd;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   158
  int i;  
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   159
  int baudrate;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   160
  
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   161
  printf("bus %s ", board->busname);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   162
  fd0 = VSCAN_Open(board->busname, VSCAN_MODE_NORMAL);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   163
  if(fd0 <= 0)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   164
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   165
    fprintf(stderr, "canOpen_driver (VScom): error opening %s\n", board->busname);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   166
    return (CAN_HANDLE)fd0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   167
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   168
  printf("(fd = %d)\n", fd0);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   169
  baudrate = TranslateBaudeRate(board->baudrate);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   170
  if(baudrate == 0)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   171
    return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   172
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   173
  if (VSCAN_Ioctl((VSCAN_HANDLE)fd0, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   174
  {
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   175
    fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   176
    return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   177
  }
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   178
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   179
   return (CAN_HANDLE)fd0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   180
}
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   181
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   182
/***************************************************************************/
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   183
int canClose_driver(CAN_HANDLE fd0)
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   184
{
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   185
  VSCAN_Close((VSCAN_HANDLE)fd0);
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   186
  return 0;
26f762d4116d Add support for USB-CAN devices from VScom
greg
parents:
diff changeset
   187
}