drivers/can_lincan/can_lincan.c
author frdupin
Mon, 29 Jan 2007 15:39:20 +0100
changeset 81 9ea761516104
parent 26 8340a591acf3
child 145 e747d2e26af0
permissions -rw-r--r--
hcs12 is big_endian
26
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     1
/*
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     3
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     5
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     6
See COPYING file for copyrights details.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     7
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    12
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    16
Lesser General Public License for more details.
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    17
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    21
*/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    22
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    23
#include <stdio.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    24
#include <string.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    25
#include <stdlib.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    26
#include <errno.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    27
#include <stddef.h> /* for NULL */
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    28
#include <sys/ioctl.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    29
#include <fcntl.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    30
#include <signal.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    31
#include <sys/time.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    32
#include <unistd.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    33
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    34
#include "canmsg.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    35
#include "lincan.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    36
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    37
struct CANPort;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    38
#define CAN_HANDLE struct CANPort *
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    39
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    40
#include <applicfg.h>
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    41
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    42
#include "timer.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    43
#include "can_driver.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    44
#include "timers_driver.h"
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    45
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    46
typedef struct CANPort {
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    47
  char used;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    48
  HANDLE fd;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    49
  TASK_HANDLE receiveTask;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    50
  CO_Data* d;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    51
} CANPort;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    52
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    53
/*********functions which permit to communicate with the board****************/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    54
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    55
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    56
  int res;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    57
  struct canmsg_t canmsg;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    58
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    59
  canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    60
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    61
  do{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    62
    res = read(fd0->fd,&canmsg,sizeof(canmsg_t));
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    63
    if((res<0)&&(errno == -EAGAIN)) res = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    64
  }while(res==0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    65
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    66
  if(res != sizeof(canmsg_t)) // No new message
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    67
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    68
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    69
  if(canmsg.flags&MSG_EXT){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    70
    /* There is no mark for extended messages in CanFestival */;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    71
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    72
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    73
  m->cob_id.w = canmsg.id;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    74
  m->len = canmsg.length;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    75
  if(canmsg.flags&MSG_RTR){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    76
    m->rtr = 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    77
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    78
    m->rtr = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    79
    memcpy(m->data,canmsg.data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    80
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    81
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    82
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    83
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    84
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    85
void canReceiveLoop(CAN_HANDLE fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    86
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    87
	CO_Data* d = fd0->d;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    88
	Message m;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    89
	while (fd0->used) {
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    90
		if(!canReceive(fd0, &m))
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    91
		{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    92
			EnterMutex();
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    93
			canDispatch(d, &m);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    94
			LeaveMutex();
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    95
		}else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    96
			printf("canReceive returned error\n");
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    97
			break;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    98
		}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
    99
	}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   100
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   101
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   102
/***************************************************************************/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   103
UNS8 canSend(CAN_HANDLE fd0, Message *m)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   104
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   105
  int res;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   106
  struct canmsg_t canmsg;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   107
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   108
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   109
  canmsg.flags = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   110
  canmsg.id = m->cob_id.w;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   111
  canmsg.length = m->len;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   112
  if(m->rtr){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   113
    canmsg.flags |= MSG_RTR;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   114
  }else{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   115
    memcpy(canmsg.data,m->data,8);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   116
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   117
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   118
  if(canmsg.id >= 0x800){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   119
    canmsg.flags |= MSG_EXT;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   120
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   121
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   122
  res = write(fd0->fd,&canmsg,sizeof(canmsg_t));
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   123
  if(res!=sizeof(canmsg_t))
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   124
    return 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   125
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   126
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   127
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   128
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   129
/***************************************************************************/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   130
static const char lnx_can_dev_prefix[] = "/dev/can";
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   131
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   132
CAN_HANDLE canOpen(s_BOARD *board)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   133
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   134
  int name_len = strlen(board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   135
  int prefix_len = strlen(lnx_can_dev_prefix);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   136
  char dev_name[prefix_len+name_len+1];
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   137
  int o_flags = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   138
  CAN_HANDLE fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   139
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   140
  fd0=malloc(sizeof(*fd0));
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   141
  if(fd0==NULL)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   142
    return NULL;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   143
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   144
  /*o_flags = O_NONBLOCK;*/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   145
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   146
  memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   147
  memcpy(dev_name+prefix_len,board->busname,name_len);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   148
  dev_name[prefix_len+name_len] = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   149
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   150
  fd0->fd = open(dev_name, O_RDWR|o_flags);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   151
  if(fd0->fd < 0){
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   152
    fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   153
    goto error_ret;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   154
  }
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   155
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   156
  fd0->d = board->d;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   157
  fd0->used = 1;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   158
  CreateReceiveTask(fd0, &fd0->receiveTask);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   159
  return fd0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   160
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   161
 error_ret:
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   162
  free(fd0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   163
  return NULL;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   164
}
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   165
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   166
/***************************************************************************/
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   167
int canClose(CAN_HANDLE fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   168
{
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   169
  if(!fd0)
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   170
    return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   171
  fd0->used = 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   172
  WaitReceiveTaskEnd(&fd0->receiveTask);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   173
  close(fd0->fd);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   174
  free(fd0);
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   175
  return 0;
8340a591acf3 Included interfacing code for LinCAN driver.
ppavel
parents:
diff changeset
   176
}