drivers/can_copcican_linux/can_copcican_linux.c
author Edouard Tisserant
Thu, 24 Jan 2019 13:53:01 +0100
changeset 808 de1fc3261f21
parent 629 b9274b595650
permissions -rw-r--r--
Adding -fPIC isn't necessary of xeno-config is set correctly. Backed out changeset b9f1fcda7d30
629
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     1
/*
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     3
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     4
Copyright (C): Cosateq GmbH & Co.KG
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     5
               http://www.cosateq.com/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     6
               http://www.scale-rt.com/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     7
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     8
See COPYING file for copyrights details.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
     9
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    10
This library is free software; you can redistribute it and/or
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    11
modify it under the terms of the GNU Lesser General Public
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    12
License as published by the Free Software Foundation; either
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    13
version 2.1 of the License, or (at your option) any later version.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    14
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    15
This library is distributed in the hope that it will be useful,
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    16
but WITHOUT ANY WARRANTY; without even the implied warranty of
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    18
Lesser General Public License for more details.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    19
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    20
You should have received a copy of the GNU Lesser General Public
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    21
License along with this library; if not, write to the Free Software
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    22
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    23
*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    24
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    25
/*
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    26
	CAN driver interface for CO-PCICAN card.
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    27
*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    28
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    29
#include <stdio.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    30
#include <unistd.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    31
#include <fcntl.h>
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    32
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    33
#define NEED_PRINT_MESSAGE
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    34
#include "can_driver.h"
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    35
#include "def.h"
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    36
#include "co_pcicanops.h"
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    37
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    38
/* at the moment not threadsafe :-( */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    39
static unsigned char selectedChannelRx = 0, selectedChannelTx = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    40
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    41
static int TranslateBaudRate( char* optarg )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    42
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    43
  /* values see documentation of CO-PCICAN */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    44
  if( !strcmp( optarg, "1M"    ) ) return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    45
  if( !strcmp( optarg, "800K"  ) ) return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    46
  if( !strcmp( optarg, "500K"  ) ) return 2;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    47
  if( !strcmp( optarg, "250K"  ) ) return 3;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    48
  if( !strcmp( optarg, "125K"  ) ) return 4;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    49
  if( !strcmp( optarg, "100K"  ) ) return 5;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    50
  if( !strcmp( optarg, "83.3K" ) ) return 6;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    51
  if( !strcmp( optarg, "10K"   ) ) return 7;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    52
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    53
  return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    54
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    55
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    56
/*********CO-PCICAN specific functions to communicate with the board**********/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    57
typedef struct
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    58
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    59
  char used;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    60
  CAN_HANDLE fd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    61
  void* receiveTask;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    62
  void* d;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    63
} CANPort; /* taken from drivers/unix.c */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    64
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    65
int get_fd_of_port( CAN_PORT port )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    66
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    67
  CANPort *thisPort = (CANPort*)port;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    68
  CAN_HANDLE thisHandle;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    69
  int *pfd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    70
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    71
  if( thisPort == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    72
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    73
    MSG("can_copcican_linux: get_fd_of_port(): thisPort is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    74
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    75
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    76
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    77
  thisHandle = thisPort->fd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    78
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    79
  if( thisHandle == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    80
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    81
    MSG("can_copcican_linux: get_fd_of_port(): thisHandle is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    82
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    83
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    84
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    85
  pfd = (int*)thisHandle;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    86
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    87
  /*MSG("can_copcican_linux: get_fd_of_port(): handle is %d\n", *pfd);*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    88
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    89
  return *pfd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    90
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    91
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    92
int co_pcican_enter_run_mode( const int fd )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    93
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    94
  co_pcican_config_t board_config;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    95
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    96
  if( fd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    97
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    98
    MSG("can_copcican_linux: co_pcican_enter_run_mode(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
    99
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   100
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   101
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   102
  memset( &board_config, 0x00, sizeof(co_pcican_config_t) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   103
  board_config.opcode = CMDQ_OPC_ENTER_RUN_MODE;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   104
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   105
  return ioctl( fd, CAN_CONFIG, &board_config );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   106
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   107
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   108
int co_pcican_enter_config_mode( const int fd )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   109
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   110
  co_pcican_config_t board_config;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   111
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   112
  if( fd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   113
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   114
    MSG("can_copcican_linux: co_pcican_enter_config_mode(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   115
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   116
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   117
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   118
  memset( &board_config, 0x00, sizeof(co_pcican_config_t) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   119
  board_config.opcode = CMDQ_OPC_ENTER_CONFIG_MODE;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   120
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   121
  return ioctl( fd, CAN_CONFIG, &board_config );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   122
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   123
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   124
int co_pcican_select_channel( const unsigned char channel, const unsigned int direction )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   125
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   126
  if( channel >= NUM_CAN_CHANNELS )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   127
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   128
    MSG("can_copcican_linux: co_pcican_select_channel(): invalid channel\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   129
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   130
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   131
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   132
  /* at the moment not threadsafe :-( */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   133
  switch( direction )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   134
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   135
    case RX: selectedChannelRx = channel;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   136
             break;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   137
    case TX: selectedChannelTx = channel;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   138
             break;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   139
    default: return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   140
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   141
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   142
  return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   143
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   144
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   145
int co_pcican_configure_selected_channel( const int fd, s_BOARD *board, const unsigned int direction )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   146
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   147
  co_pcican_config_t board_config;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   148
  unsigned int selectedChannel;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   149
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   150
  if( fd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   151
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   152
    MSG("can_copcican_linux: co_pcican_configure_selected_channel(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   153
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   154
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   155
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   156
  if( board == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   157
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   158
    MSG("can_copcican_linux: co_pcican_configure_selected_channel(): board is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   159
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   160
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   161
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   162
  if( board->baudrate == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   163
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   164
    MSG("can_copcican_linux: co_pcican_configure_selected_channel(): baudrate is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   165
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   166
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   167
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   168
  switch( direction )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   169
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   170
    case RX: selectedChannel = selectedChannelRx;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   171
             break;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   172
    case TX: selectedChannel = selectedChannelTx;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   173
             break;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   174
    default: selectedChannel = 0xff;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   175
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   176
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   177
  if( selectedChannel >= NUM_CAN_CHANNELS )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   178
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   179
    MSG("can_copcican_linux: co_pcican_configure_selected_channel(): invalid channel selected\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   180
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   181
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   182
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   183
  memset( &board_config, 0x00, sizeof(co_pcican_config_t) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   184
  board_config.opcode   = CMDQ_OPC_SET_CONFIG_CHANNEL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   185
  board_config.param[0] = selectedChannel;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   186
  board_config.param[1] = TranslateBaudRate( board->baudrate );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   187
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   188
  return ioctl( fd, CAN_CONFIG, &board_config );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   189
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   190
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   191
/*********functions which permit to communicate with the board****************/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   192
UNS8 canReceive_driver( CAN_HANDLE fd0, Message *m )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   193
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   194
  co_pcican_message_t canmsg;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   195
  UNS8 ret = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   196
  int *pfd = (int*)fd0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   197
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   198
  if( pfd == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   199
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   200
    MSG("can_copcican_linux: canReceive_driver(): file descriptor is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   201
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   202
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   203
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   204
  if( *pfd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   205
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   206
    MSG("can_copcican_linux: canReceive_driver(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   207
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   208
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   209
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   210
  if( selectedChannelRx >= NUM_CAN_CHANNELS )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   211
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   212
    MSG("can_copcican_linux: canReceive_driver(): invalid channel selected\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   213
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   214
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   215
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   216
  if( m == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   217
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   218
    MSG("can_copcican_linux: canReceive_driver(): message is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   219
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   220
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   221
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   222
  memset( &canmsg, 0x00, sizeof(co_pcican_message_t) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   223
  canmsg.channelnum = selectedChannelRx;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   224
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   225
  ioctl( *pfd, CAN_READ, &canmsg );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   226
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   227
  if( canmsg.timestamp_lo == 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   228
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   229
    memset( m, 0x00, sizeof(Message) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   230
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   231
    m->cob_id = 0xffff; /* set to invalid so nothing happens */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   232
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   233
  else
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   234
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   235
    m->len = canmsg.size;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   236
    m->cob_id = canmsg.id;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   237
    m->rtr = canmsg.type & MSG_RTR;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   238
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   239
    if( !m->rtr )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   240
    {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   241
      /* this is for safety */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   242
      if( m->len > 8 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   243
        m->len = 8;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   244
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   245
      memcpy( m->data, canmsg.data, m->len);
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   246
    }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   247
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   248
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   249
  return ret;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   250
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   251
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   252
/***************************************************************************/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   253
UNS8 canSend_driver( CAN_HANDLE fd0, Message *m )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   254
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   255
  co_pcican_message_t canmsg;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   256
  UNS8 ret = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   257
  int *pfd = (int*)fd0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   258
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   259
  if( pfd == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   260
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   261
    MSG("can_copcican_linux: canSend_driver(): file descriptor is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   262
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   263
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   264
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   265
  if( *pfd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   266
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   267
    MSG("can_copcican_linux: canSend_driver(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   268
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   269
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   270
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   271
  if( selectedChannelTx >= NUM_CAN_CHANNELS )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   272
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   273
    MSG("can_copcican_linux: canSend_driver(): invalid channel selected\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   274
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   275
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   276
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   277
  if( m == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   278
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   279
    MSG("can_copcican_linux: canSend_driver(): message is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   280
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   281
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   282
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   283
  memset( &canmsg, 0x00, sizeof(co_pcican_message_t) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   284
  canmsg.channelnum = selectedChannelTx;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   285
  canmsg.size = m->len;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   286
  canmsg.id = m->cob_id;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   287
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   288
  if( canmsg.id >= 0x800 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   289
    canmsg.type |= MSG_EXT;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   290
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   291
  if( m->rtr )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   292
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   293
    canmsg.type |= MSG_RTR;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   294
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   295
  else
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   296
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   297
    /* this is for safety */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   298
    if( canmsg.size > 8 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   299
      canmsg.size = 8;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   300
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   301
    memcpy( canmsg.data, m->data, canmsg.size);
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   302
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   303
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   304
  if( ioctl( *pfd, CAN_WRITE, &canmsg ) < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   305
    ret = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   306
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   307
  return ret;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   308
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   309
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   310
/***************************************************************************/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   311
CAN_HANDLE canOpen_driver( s_BOARD *board )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   312
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   313
  int *pfd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   314
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   315
  if( board == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   316
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   317
    MSG("can_copcican_linux: canOpen_driver(): board is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   318
    return NULL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   319
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   320
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   321
  if( board->busname == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   322
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   323
    MSG("can_copcican_linux: canOpen_driver(): busname is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   324
    return NULL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   325
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   326
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   327
  /* create dynamically to avoid global variable */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   328
  pfd = (int*)malloc( sizeof(int) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   329
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   330
  if( pfd == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   331
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   332
    MSG("can_copcican_linux: canOpen_driver(): file descriptor is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   333
    return NULL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   334
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   335
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   336
  *pfd = open( board->busname, O_RDWR, 0 );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   337
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   338
  if( *pfd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   339
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   340
    MSG("can_copcican_linux: canOpen_driver(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   341
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   342
    /* clear resources if open failed */
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   343
    free( pfd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   344
    pfd = NULL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   345
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   346
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   347
  /*MSG("can_copcican_linux: canOpen_driver(): handle is %d\n", *pfd);*/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   348
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   349
  return (CAN_HANDLE)pfd;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   350
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   351
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   352
/***************************************************************************/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   353
int canClose_driver( CAN_HANDLE fd0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   354
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   355
  int *pfd = (int*)fd0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   356
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   357
  if( pfd == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   358
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   359
    MSG("can_copcican_linux: canClose_driver(): file descriptor is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   360
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   361
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   362
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   363
  if( *pfd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   364
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   365
    MSG("can_copcican_linux: canClose_driver(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   366
    return -1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   367
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   368
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   369
  close( *pfd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   370
  free( pfd );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   371
  pfd = NULL;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   372
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   373
  selectedChannelRx = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   374
  selectedChannelTx = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   375
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   376
  return 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   377
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   378
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   379
/***************************************************************************/
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   380
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   381
{
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   382
  s_BOARD board;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   383
  UNS8 ret = 0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   384
  int *pfd = (int*)fd0;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   385
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   386
  if( pfd == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   387
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   388
    MSG("can_copcican_linux: canChangeBaudRate_driver(): file descriptor is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   389
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   390
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   391
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   392
  if( *pfd < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   393
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   394
    MSG("can_copcican_linux: canChangeBaudRate_driver(): invalid file descriptor\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   395
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   396
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   397
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   398
  if( baud == NULL )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   399
  {
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   400
    MSG("can_copcican_linux: canChangeBaudRate_driver(): baud is NULL\n");
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   401
    return 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   402
  }
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   403
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   404
  memset( &board, 0x00, sizeof(s_BOARD) );
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   405
  board.baudrate = baud;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   406
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   407
  if( co_pcican_configure_selected_channel( *pfd, &board, RX ) < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   408
    ret = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   409
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   410
  if( co_pcican_configure_selected_channel( *pfd, &board, TX ) < 0 )
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   411
    ret = 1;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   412
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   413
  return ret;
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   414
}
b9274b595650 CosateQ contribution.
edouard
parents:
diff changeset
   415