drivers/can_virtual/can_virtual.c
author etisserant
Tue, 13 Feb 2007 17:21:19 +0100
changeset 92 0d84d95790d9
parent 0 4472ee7c6c3e
child 145 e747d2e26af0
permissions -rw-r--r--
- Some fixes in Makefile.in
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
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
	Virtual CAN driver.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
#include <sys/types.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
#include <unistd.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
#include <stdio.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
#include <applicfg.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
#include "can_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#include "timers_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
#define MAX_NB_CAN_PIPES 10
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
typedef struct {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
  char used;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
  int pipe[2];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
  TASK_HANDLE receiveTask;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
  CO_Data* d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
} CANPipe;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,{0,0},},};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
/*********functions which permit to communicate with the board****************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
	if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
		return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
void canReceiveLoop(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
	CO_Data* d = ((CANPipe*)fd0)->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
	Message m;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
	while (1) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
		if(!canReceive(fd0, &m))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
		{  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
			EnterMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
			canDispatch(d, &m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
			LeaveMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
		}else{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
UNS8 canSend(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
  int i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
  // Send to all readers, except myself
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
  for(i=0; i < MAX_NB_CAN_PIPES; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
  	if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
  	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
		write(canpipes[i].pipe[1], m, sizeof(Message));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
  	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
CAN_HANDLE canOpen(s_BOARD *board)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
  int i;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
  for(i=0; i < MAX_NB_CAN_PIPES; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
  	if(!canpipes[i].used)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
	  	break;
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
  /* Create the pipe.  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
  if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
      fprintf (stderr, "Open failed.\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
      return (CAN_HANDLE)NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
   canpipes[i].used = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
   canpipes[i].d = board->d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
   CreateReceiveTask((CAN_HANDLE) &canpipes[i], &canpipes[i].receiveTask);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
   return (CAN_HANDLE) &canpipes[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
int canClose(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
  close(((CANPipe*)fd0)->pipe[0]);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
  close(((CANPipe*)fd0)->pipe[1]);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
  ((CANPipe*)fd0)->used = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
  WaitReceiveTaskEnd(&((CANPipe*)fd0)->receiveTask);
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