drivers/can_peak/can_peak.c
author etisserant
Wed, 10 May 2006 21:05:57 +0200
changeset 1 b3dc740b4b04
parent 0 4472ee7c6c3e
child 33 c767eabbaaac
permissions -rw-r--r--
Commit some Fix from Francis on sdo.c and abjacces.c. Fix endianisation problems while accessing OD. Also fix SDO abord handling bug.
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.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
    perror("!!! Peak board : error of reading. (from f_can_receive function) \n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
    return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
  m->cob_id.w = peakMsg.ID;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
  if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)         	/* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
    m->rtr = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
  else 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
    m->rtr = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
  m->len = peakMsg.LEN;					/* count of data bytes (0..8) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
  for(data = 0  ; data < peakMsg.LEN ; data++)             			
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
    m->data[data] = peakMsg.DATA[data];         	/* data bytes, up to 8 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
void canReceiveLoop(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
	CO_Data* d = ((CANPort*)fd0)->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
	Message m;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
	while (1) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
		if(!canReceive(fd0, &m))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
		{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
			EnterMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
			canDispatch(d, &m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
			LeaveMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
		}else{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
//			printf("canReceive returned error\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
}
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
UNS8 canSend(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
  UNS8 data;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
  TPCANMsg peakMsg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
  peakMsg.ID=m -> cob_id.w;              			/* 11/29 bit code */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
  if(m->rtr == 0)	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
    peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;       /* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
  else {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
    peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;       /* bits of MSGTYPE_*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
  peakMsg.LEN = m->len;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
          			/* count of data bytes (0..8) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
  for(data = 0 ; data <  m->len; data ++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
  	peakMsg.DATA[data] = m->data[data];         	/* data bytes, up to 8 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
  if((errno = CAN_Write(((CANPort*)fd0)->fd, & peakMsg))) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
    perror("!!! Peak board : error of writing. (from canSend function) \n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
    return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
CAN_HANDLE canOpen(s_BOARD *board)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
  HANDLE fd0 = NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
  char busname[64];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
  char* pEnd;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
  int i;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
  for(i=0; i < MAX_NB_CAN_PORTS; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
  	if(!canports[i].used)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
	  	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
  if(strtol(board->busname, &pEnd,0) >= 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
    sprintf(busname,"/dev/pcan%s",board->busname);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
    fd0 = LINUX_CAN_Open(busname, O_RDWR);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
  if (i==MAX_NB_CAN_PORTS || fd0 == NULL)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
      fprintf (stderr, "Open failed.\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
      return (CAN_HANDLE)NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
   CAN_Init(fd0, board->baudrate, CAN_INIT_TYPE_ST);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
   canports[i].used = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
   canports[i].fd = fd0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
   canports[i].d = board->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
   CreateReceiveTask((CANPort*) &canports[i], &canports[i].receiveTask);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
   return (CANPort*) &canports[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
int canClose(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
  CAN_Close(((CANPort*)fd0)->fd);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
  ((CANPort*)fd0)->used = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
  WaitReceiveTaskEnd(&((CANPort*)fd0)->receiveTask);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   165
}