drivers/can_peak/can_peak.c
author leonid
Wed, 14 Feb 2007 22:19:53 +0100
changeset 109 46010c1b6c6e
parent 35 88812de1d7cc
permissions -rw-r--r--
file resource.h was initially added on branch BRANCH_WIN32PORT.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
#include <stdio.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
#include <string.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <errno.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
#include <stddef.h> /* for NULL */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
#include <sys/ioctl.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
#include <fcntl.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
#include <signal.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
#include <sys/time.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
#include <unistd.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
/* driver pcan pci for Peak board */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
//#include "libpcan.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
//#include "pcan.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
#include "libpcan.h" // for CAN_HANDLE
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
#include <applicfg.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
#include "can_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
#include "timers_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
#define MAX_NB_CAN_PORTS 16
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
typedef struct {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
  char used;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
  HANDLE fd;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
  TASK_HANDLE receiveTask;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
  CO_Data* d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
} CANPort;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
// Define for rtr CAN message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
#define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
/*********functions which permit to communicate with the board****************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
  UNS8 data; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
  TPCANMsg peakMsg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
  if ((errno = CAN_Read(((CANPort*)fd0)->fd, & peakMsg))) {		// Blocks until no new message or error.
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 33
diff changeset
    65
    if(errno != -EIDRM && errno != -EPERM) // error is not "Can Port closed while reading" 
33
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 0
diff changeset
    66
    {
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 0
diff changeset
    67
    	perror("!!! Peak board : error of reading. (from f_can_receive function) \n");
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 0
diff changeset
    68
    }
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
    return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
  m->cob_id.w = peakMsg.ID;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
  if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)         	/* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
    m->rtr = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
  else 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
    m->rtr = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
  m->len = peakMsg.LEN;					/* count of data bytes (0..8) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
  for(data = 0  ; data < peakMsg.LEN ; data++)             			
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
    m->data[data] = peakMsg.DATA[data];         	/* data bytes, up to 8 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
void canReceiveLoop(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
	CO_Data* d = ((CANPort*)fd0)->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
	Message m;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
	while (1) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
		if(!canReceive(fd0, &m))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
		{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
			EnterMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
			canDispatch(d, &m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
			LeaveMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
		}else{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
//			printf("canReceive returned error\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
UNS8 canSend(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
  UNS8 data;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
  TPCANMsg peakMsg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
  peakMsg.ID=m -> cob_id.w;              			/* 11/29 bit code */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
  if(m->rtr == 0)	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
    peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;       /* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
  else {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
    peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;       /* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
  peakMsg.LEN = m->len;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
          			/* count of data bytes (0..8) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
  for(data = 0 ; data <  m->len; data ++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
  	peakMsg.DATA[data] = m->data[data];         	/* data bytes, up to 8 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
  if((errno = CAN_Write(((CANPort*)fd0)->fd, & peakMsg))) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
    perror("!!! Peak board : error of writing. (from canSend function) \n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
    return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
CAN_HANDLE canOpen(s_BOARD *board)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
  HANDLE fd0 = NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
  char busname[64];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
  char* pEnd;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
  int i;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
  for(i=0; i < MAX_NB_CAN_PORTS; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
  	if(!canports[i].used)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
	  	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
  if(strtol(board->busname, &pEnd,0) >= 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
    sprintf(busname,"/dev/pcan%s",board->busname);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
    fd0 = LINUX_CAN_Open(busname, O_RDWR);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
  if (i==MAX_NB_CAN_PORTS || fd0 == NULL)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
      fprintf (stderr, "Open failed.\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
      return (CAN_HANDLE)NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
   CAN_Init(fd0, board->baudrate, CAN_INIT_TYPE_ST);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
   canports[i].used = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
   canports[i].fd = fd0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
   canports[i].d = board->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
   CreateReceiveTask((CANPort*) &canports[i], &canports[i].receiveTask);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
   return (CANPort*) &canports[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
int canClose(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
  CAN_Close(((CANPort*)fd0)->fd);
33
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 0
diff changeset
   165
  WaitReceiveTaskEnd(&((CANPort*)fd0)->receiveTask);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
  ((CANPort*)fd0)->used = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
}